business-unit.js 12.4 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
basicDataModule.directive('businessUnit', ['$log', 'SweetAlert', '$translate',
    function ($log, SweetAlert, $translate) {
        'use strict';
        $log.debug('businessUnit.ctor()...');

        return {
            restrict: 'E',
            templateUrl: '/app/admin/basicData/masterData/businessUnit/business-unit.html' + '?_=' + Math.random(),
            scope:
            {
                //addCtrlFn: '&addBsFn',
                //removeCtrlFn: '&removeBsFn',
                //editCtrlFn: '&editBsFn',
                //stopCtrlFn: '&stopBsFn',
                //attr: '@',
                //list:'=bind'
            },
            controller: 'businessUnitController',
            //link: function (scope, element, attrs, $element) {
            //    var originalContent, target, isEdit, isAdd;
            //    var originalArray = [];
            //    var isNodeActive = $translate.instant('OrganizationStructureActive');
            //    var isNodeDisabled = $translate.instant('OrganizationStructureDisabled');
            //    var disableNode = $translate.instant('OrganizationStructureDisabledNode');
            //    var activeNode = $translate.instant('OrganizationStructureEnableNode');
            //    var emptyNote = $translate.instant('BusinessUnitEmptyNode');
            //    var outOfLengthNote = $translate.instant('BusinessUnitOutOfLengthNode');
            //    var depulicateNote = $translate.instant('BusinessUnitDuplicateNode');
            //    var addInvalidNote = $translate.instant('OrganizationStructureOperationAddInvalid');
            //    var editInvalidNote = $translate.instant('OrganizationStructureOperationEditInvalid');

            //    //添加行的时候删除行
            //    $(document).on('click', '.remove-bs-row', function () {
            //        var tr = $(this).parent().parent();
            //        var id = $(tr).children('td').eq(2).text();
            //        $(tr).remove();

            //        if (id !== '') {
            //            scope.removeCtrlFn({ id: id });
            //        }

            //        if ($('.remove-bs-row').length == 0) {
            //            $(".bs-save-group").css('display', 'none');
            //            isAdd = false;
            //        }
            //    });

            //    //选中行
            //    $(document).on('click', '#businessUnitTable tr:not(.header)', function (evt) {
            //        $("#businessUnitTable tr").removeClass("selected");
            //        $(this).toggleClass('selected');

            //        target = $("#businessUnitTable tr.selected td");

            //    });

            //    //编辑
            //    scope.editBusinessUnit = function (e, org) {

            //        if (isAdd) {
            //            SweetAlert.warning(editInvalidNote);
            //            return;
            //        }

            //        var elem;

            //        isEdit = true; isAdd = false;
            //        $(".bs-save-group").css('display', 'block');

            //        elem = (e.srcElement || e.target);  //for IE: e.srcElement, others, e.target
            //        target = $(elem).parent().parent().parent().children('td');

            //        scope.currentTarget = target;
            //        scope.currentOrg = org;

            //        var element = $(target).eq(0);
            //        originalContent = $(element).text();
            //        var result = $.grep(originalArray, function (e) { return e.id === org.id });
            //        if (result.length === 0 || originalContent != '') {
            //            //add to originalArray for the cancel 
            //            originalArray.push({ 'id': org.id, 'content': originalContent });

            //            $(element).addClass("cellEditing");
            //            $(element).html("<input type='text' class='form-control' value='" + originalContent + "' /></div>");
            //            $(element).children().first().focus();
            //            $(element).children().first().keypress(function (e) {
            //                if (e.which == 13) {
            //                    var newContent = $(element).children().first().val();
            //                    $(element).text(newContent);
            //                    $(element).removeClass("cellEditing");
            //                }
            //            });
            //        }
            //    };

            //    //取消保存
            //    scope.cancelBusinessUnit = function (e) {
            //        var elem = (e.srcElement || e.target);  //for IE: e.srcElement, others, e.target
            //        if (isEdit) {

            //            var element = $(".cellEditing");
            //            element.each(function (index) {
            //                var td = $(this).parent().children('td').eq(2);
            //                var id = $(td).text();
            //                var obj = $.grep(originalArray, function (x) { return x.id === id });
            //                if (obj.length > 0) {
            //                    $(this).text(obj[0].content);
            //                    $(this).removeClass("cellEditing");
            //                }
            //            });
            //            originalArray = [];
            //            isEdit = false;
            //        } else {
            //            $('.new-add-bs').remove();
            //            isAdd = false;
            //        }
            //        $(".bs-save-group").css('display', 'none');
            //    };

            //    $("#btnAddBusinessUnit").on('click', function () {
            //        if (isEdit) {
            //            SweetAlert.warning(addInvalidNote);
            //            return;
            //        }
            //        isEdit = false; isAdd = true;
            //        $(".bs-save-group").css('display', 'block');
            //        $('#businessUnitTable tr:last').after('<tr class="new-add-bs"><td><input type="text" class="form-control new-add-input" value="" /></td><td>' + isNodeActive + '</td><td><i class="fa fa-times remove-bs-row" aria-hidden="true"></i></td></tr>');

            //    });

            //    //保存
            //    scope.saveBusinessUnit = function (e) {
            //        if (isEdit) {
            //            //编辑保存
            //            var elem = (e.srcElement || e.target);  //for IE: e.srcElement, others, e.target
            //            var element = $(".cellEditing");
            //            var newValues = [];
            //            var idArray = [];

            //            element.each(function (index) {
            //                var td = $(this).parent().children('td');
            //                var newContent = $(td).eq(0).children().first().val();
            //                var isActive = $(td).eq(1).text();
            //                var id = $(td).eq(2).text();
            //                idArray.push(id);
            //                newValues.push({
            //                    id: id, name: newContent, isActive: isActive
            //                });
            //            });

            //            var validationResult = formValidation(newValues);

            //            if (!validationResult.duplicateFlag && !validationResult.emptyFlag && !validationResult.outOfLengthFlag) {
            //                isEdit = false;
            //                $(".bs-save-group").css('display', 'none');

            //                element.each(function (index) {
            //                    var td = $(this).parent().children('td');
            //                    var firstTd = $(td).eq(0);
            //                    var newVal = $(firstTd).children().first().val();
            //                    $(firstTd).text(newVal);
            //                    $(firstTd).removeClass("cellEditing");
            //                });

            //                scope.editCtrlFn({
            //                    newcontent: newValues
            //                });

            //                originalArray = [];
            //            }

            //        } else {
            //            //新增层级
            //            var newaddTarget = $('.new-add-input');
            //            var values = [];
            //            var hasName = true;
            //            var outOfLength = false;
            //            var duplicate = false;
            //            var newValues = [];

            //            $(newaddTarget).each(function () {
            //                var guid = PWC.newGuid();
            //                var name = $(this).val();

            //                newValues.push({
            //                    id: guid, name: name, isActive: true
            //                });
            //            });

            //            var validationResult = formValidation(newValues);

            //            if (!validationResult.duplicateFlag && !validationResult.emptyFlag && !validationResult.outOfLengthFlag) {
            //                $(".bs-save-group").css('display', 'none');
            //                $('.new-add-bs').remove();
            //                var newAdd = {
            //                    newrow: newValues
            //                };
            //                newValues.forEach(function (item) {
            //                    scope.businessUnitList.push(item);
            //                });
            //                scope.addCtrlFn(newAdd);
            //                originalArray = [];
            //            }
            //        }
            //    };

            //    //停用和启用
            //    var currentId;
            //    scope.stopOrActiveBusinessUnit = function (org) {

            //        currentId = org.id;
            //        var name = org.name;
            //        var id = org.id;
            //        var updateArray = [];
            //        var values = { id: id, name: name, isActive: !org.isActive };
            //        updateArray.push(values);

            //        scope.stopCtrlFn({ newcontent: updateArray });

            //        scope.$on('to-child', function (event, successFlag) {
            //            if (successFlag) {
            //                var item = _.where(scope.businessUnitList, { id: currentId });
            //                item[0].isActive = values.isActive;
            //            }
            //        });
            //    };

            //    //form validation before saving add/edit operation
            //    var formValidation = function (newValues) {

            //        var duplicateFlag = false;
            //        var emptyFlag = false;
            //        var outOfLengthFlag = false;

            //        //判断同时输入多个的时候不能重复
            //        var uniqueList = _.uniq(newValues, function (item, key, name) {
            //            return item.name;
            //        });

            //        if (uniqueList.length != newValues.length) {
            //            duplicateFlag = true;
            //        }

            //        newValues.forEach(function (item) {

            //            var filterResult = _.find(scope.businessUnitList, { name: item.name });
            //            //如果找的这个object和newvalue的id是同一个的话,不是重复 
            //            if (filterResult != undefined && filterResult.id !== item.id) {
            //                duplicateFlag = true;
            //            } else if (item.name === '') {
            //                emptyFlag = true;
            //            } else if (PWC.getLength(item.name) > constant.businessUnit.BUmaxLength) {
            //                outOfLengthFlag = true;
            //            }
            //        });

            //        //give notice to user
            //        if (emptyFlag) {
            //            SweetAlert.warning(emptyNote);
            //        } else if (duplicateFlag) {
            //            SweetAlert.warning(depulicateNote);
            //        } else if (outOfLengthFlag) {
            //            SweetAlert.warning(outOfLengthNote);
            //        }

            //        return {
            //            duplicateFlag: duplicateFlag,
            //            emptyFlag: emptyFlag,
            //            outOfLengthFlag: outOfLengthFlag
            //        };
            //    };
            //}
        };
    }
]);