prj-common-directive.js 46.9 KB
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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
// 自定义指令,指令模版在dt目录下


angular.module('ScheduleApp').directive('loadingWidget', ['requestNotificationChannel', function(requestNotificationChannel) {
    return {
        restrict: 'A',
        link: function(scope, element) {
            // 初始隐藏loading界面
            element.hide();

            // 开始请求通知处理
            requestNotificationChannel.onRequestStarted(scope, function() {
                element.show();
            });
            // 请求结束通知处理
            requestNotificationChannel.onRequestEnded(scope, function() {
                element.hide();
            });
        }
    };
}]);

angular.module('ScheduleApp').directive("saSelect", ['$timeout', function($timeout) {
    return {
        restrict: 'E',
        templateUrl: '/pages/scheduleApp/module/other/MyDictionarySelectTemplate.html',
        scope: {
            model: "="
        },
        controllerAs: "$saSelectCtrl",
        bindToController: true,
        controller: function() {
            var self = this;
            self.datas = []; // 关联的字典数据,内部格式 {code:{值},name:{名字}}
        },
        /**
         * 此阶段可以改dom结构,此时angular还没扫描指令,
         * 这里就可以动态添加其他angularjs的指令字符串,如required指令字符串。
         * @param tElem
         * @param tAttrs
         * @returns {{pre: Function, post: Function}}
         */
        compile: function(tElem, tAttrs) {
            // 确定是否使用angularjs required验证
            // 属性 required
            // 如果没有填写,内部不添加验证,如果填写了,并且等于true添加验证,否则不添加
            var required_attr = tAttrs["required"];
            if (required_attr) {
                if (required_attr == "true") {
                    // 添加required属性指令
                    tElem.find("ui-select").attr("required", "");
                } else {
                    // 不等于true,不添加required属性指令
                }
            } else {
                // 不添加required属性指令
            }

            //console.log("saSelect" + ":compile = >" + tElem.html());

            return {
                pre: function(scope, element, attr) {
                    // TODO:
                },
                /**
                 * 相当于link函数。
                 *
                 * 重要属性如下:
                 * model 是绑定外部值。
                 * dicgroup 字典组的类型
                 * name input name属性值
                 */
                post: function(scope, element, attr) {
                    // 1、获取属性
                    var dicgroup_attr = attr['dicgroup']; // 字典组的类型
                    var name_attr = attr['name']; // input name属性值
                    var dicname_attr = attr['dicname']; // model关联的字典名字段
                    var codename_attr = attr['codename']; // model关联的字典值字段
                    var placeholder_attr = attr['placeholder']; // select placeholder提示

                    // 系统的字典对象,使用dictionaryUtils类获取
                    var origin_dicgroup;
                    var dic_key; // 字典key

                    if (dicgroup_attr) { // 赋值指定的字典数据
                        origin_dicgroup = dictionaryUtils.getByGroup(dicgroup_attr);
                        for (dic_key in origin_dicgroup) {
                            var data = {}; // 重新组合的字典元素对象
                            if (dic_key == "true")
                                data.code = true;
                            else
                                data.code = dic_key;
                            data.name = origin_dicgroup[dic_key];
                            scope["$saSelectCtrl"].datas.push(data);
                        }
                    }

                    if (name_attr) {
                        scope["$saSelectCtrl"].nv = name_attr;
                    }
                    if (placeholder_attr) {
                        scope["$saSelectCtrl"].ph = placeholder_attr;
                    }

                    scope["$saSelectCtrl"].select = function($item) {
                        if (codename_attr) {
                            scope["$saSelectCtrl"].model[codename_attr] = $item.code;
                        }
                        if (dicname_attr) {
                            scope["$saSelectCtrl"].model[dicname_attr] = $item.name;
                        }
                    };

                    scope["$saSelectCtrl"].remove = function() {
                        if (codename_attr) {
                            scope["$saSelectCtrl"].model[codename_attr] = null;
                        }
                        if (dicname_attr) {
                            scope["$saSelectCtrl"].model[dicname_attr] = null;
                        }
                        scope["$saSelectCtrl"].cmodel = null;
                    };

                    $timeout(function() {
                        // 创建内部使用的绑定对象
                        var model_code = scope["$saSelectCtrl"].model[codename_attr];
                        scope["$saSelectCtrl"].cmodel = model_code;
                    }, 0);
                }
            }
        }
    };
}]);

/**
 * saRadiogroup指令
 * 属性如下:
 * model(必须):独立作用域,外部绑定的一个值,如:ctrl.timeTableManageForForm.isEnableDisTemplate
 * dicgroup(必须):关联的字典数据type(TODO:以后增加其他数据源)
 * name(必须):控件的名字
 * required(可选):是否要用required验证
 * disabled(可选):标示单选框是否可选
 *
 */
angular.module('ScheduleApp').directive("saRadiogroup", [function() {
    /**
     * 使用字典数据的单选按钮组的指令。
     * 指令名称:truefalse-Dic
     */
    return {
        restrict: 'E',
        templateUrl: '/pages/scheduleApp/module/common/dt/MyRadioGroupWrapTemplate.html',
        scope: {
            model: "="
        },
        controllerAs: "$saRadiogroupCtrl",
        bindToController: true,
        controller: function($scope) {
            //$scope["model"] = {selectedOption: null};
            //console.log("controller");
            //console.log("controller:" + $scope["model"]);

            var self = this;
            self.$$data = null; // 内部数据
        },

        /**
         * 此阶段可以改dom结构,此时angular还没扫描指令,
         * 这里就可以动态添加其他angularjs的指令字符串,如required指令字符串。
         * @param tElem
         * @param tAttrs
         * @returns {{pre: Function, post: Function}}
         */
        compile: function(tElem, tAttrs) {
            // 获取属性
            var $dicgroup_attr = tAttrs["dicgroup"]; // 关联的字典数据type
            var $name_attr = tAttrs["name"]; // 控件的名字
            var $required_attr = tAttrs["required"]; // 是否要用required验证
            var $disabled_attr = tAttrs["disabled"]; // 标示单选框是否可选

            // controlAs名字
            var ctrlAs = "$saRadiogroupCtrl";

            // 如果有required属性,添加angularjs required验证
            if ($required_attr != undefined) {
                tElem.find("input").attr("required", "");
            }

            return {
                pre: function(scope, element, attr) {

                },

                /**
                 * 相当于link函数。
                 * @param scope
                 * @param element
                 * @param attr
                 */
                post: function(scope, element, attr) {
                    //console.log("link");
                    //console.log("link:" + scope.model);
                    //scope["model"] = {selectedOption: null};

                    if ($name_attr) {
                        scope[ctrlAs].nv = $name_attr;
                    }

                    if ($disabled_attr) {
                        scope[ctrlAs].disabled = true;
                    }
                    if ($dicgroup_attr) {
                        var obj = dictionaryUtils.getByGroup($dicgroup_attr);
                        scope[ctrlAs].$$data = obj;
                        // 处理 scope["dic"] key值
                        scope[ctrlAs].dicvalueCalcu = function(value) {
                            if (value == "true") {
                                //console.log(value);
                                return true;
                            } else if (value == "false") {
                                //console.log(value);
                                return false;
                            } else {
                                return value;
                            }
                        };
                    }
                }
            };
        }
    };
}]);

angular.module('ScheduleApp').directive("remoteValidaton", [
    'BusInfoManageService_g',
    'EmployeeInfoManageService_g',
    'TimeTableManageService_g',
    function(
        busInfoManageService_g,
        employeeInfoManageService_g,
        timeTableManageService_g
    ) {
        /**
         * 远端验证指令,依赖于ngModel
         * 指令名称 remote-Validation
         * 需要属性 rvtype 表示验证类型
         */
        return {
            restrict: "A",
            require: "^ngModel",
            link: function(scope, element, attr, ngModelCtrl) {
                element.bind("keyup", function() {
                    var modelValue = ngModelCtrl.$modelValue;
                    var rv1_attr = attr["rv1"];
                    if (attr["rvtype"]) {

                        // 根据rvtype的值,确定使用那个远端验证的url,
                        // rv1, rv2, rv3是关联比较值,暂时使用rv1
                        // 这个貌似没法通用,根据业务变换
                        // TODO:暂时有点乱以后改
                        if (attr["rvtype"] == "insideCode") {
                            busInfoManageService_g.validate.insideCode(
                                {"insideCode_eq": modelValue, type: "equale"},
                                function(result) {
                                    //console.log(result);
                                    if (result.status == "SUCCESS") {
                                        ngModelCtrl.$setValidity('remote', true);
                                    } else {
                                        ngModelCtrl.$setValidity('remote', false);
                                    }
                                },
                                function(result) {
                                    //console.log(result);
                                    ngModelCtrl.$setValidity('remote', true);
                                }
                            );
                        } else if (attr["rvtype"] == "jobCode") {
                            if (!rv1_attr) {
                                ngModelCtrl.$setValidity('remote', false);
                                return;
                            }

                            employeeInfoManageService_g.validate.jobCode(
                                {"jobCode_eq": modelValue, "companyCode_eq": rv1_attr, type: "equale"},
                                function(result) {
                                    //console.log(result);
                                    if (result.status == "SUCCESS") {
                                        ngModelCtrl.$setValidity('remote', true);
                                    } else {
                                        ngModelCtrl.$setValidity('remote', false);
                                    }
                                },
                                function(result) {
                                    //console.log(result);
                                    ngModelCtrl.$setValidity('remote', true);
                                }
                            );
                        } else if (attr["rvtype"] == "ttinfoname") {
                            if (!rv1_attr) {
                                ngModelCtrl.$setValidity('remote', false);
                                return;
                            }

                            timeTableManageService_g.validate.ttinfoname(
                                {"name_eq": modelValue, "xl.id_eq": rv1_attr, type: "equale"},
                                function(result) {
                                    //console.log(result);
                                    if (result.status == "SUCCESS") {
                                        ngModelCtrl.$setValidity('remote', true);
                                    } else {
                                        ngModelCtrl.$setValidity('remote', false);
                                    }
                                },
                                function(result) {
                                    //console.log(result);
                                    ngModelCtrl.$setValidity('remote', true);
                                }
                            );

                        }
                    } else {
                        // 没有rvtype,就不用远端验证了
                        ngModelCtrl.$setValidity('remote', true);
                    }

                    attr.$observe("rv1", function(value) {
                        if (attr["rvtype"] == "jobCode") {
                            if (!value) {
                                ngModelCtrl.$setValidity('remote', false);
                                return;
                            }

                            employeeInfoManageService_g.validate.jobCode(
                                {"jobCode_eq": modelValue, "companyCode_eq": rv1_attr, type: "equale"},
                                function(result) {
                                    //console.log(result);
                                    if (result.status == "SUCCESS") {
                                        ngModelCtrl.$setValidity('remote', true);
                                    } else {
                                        ngModelCtrl.$setValidity('remote', false);
                                    }
                                },
                                function(result) {
                                    //console.log(result);
                                    ngModelCtrl.$setValidity('remote', true);
                                }
                            );
                        } else if (attr["rvtype"] == "ttinfoname") {
                            if (!value) {
                                ngModelCtrl.$setValidity('remote', false);
                                return;
                            }

                            console.log("rv1:" + value);

                            timeTableManageService_g.validate.ttinfoname(
                                {"name_eq": modelValue, "xl.id_eq": value, type: "equale"},
                                function(result) {
                                    //console.log(result);
                                    if (result.status == "SUCCESS") {
                                        ngModelCtrl.$setValidity('remote', true);
                                    } else {
                                        ngModelCtrl.$setValidity('remote', false);
                                    }
                                },
                                function(result) {
                                    //console.log(result);
                                    ngModelCtrl.$setValidity('remote', true);
                                }
                            );
                        }

                    });
                });
            }
        };
    }]);


/**
 * saSelect2指令,根据属性值,动态载入数据,然后支持拼音搜索,点击右边的按钮清除选择并重新载入数据。
 * 1、compile阶段使用的属性如下:
 *      required:用于和表单验证连接,指定成required="true"才有效。
 * 2、link阶段使用的属性如下
 *      model:关联的模型对象
 *      name:表单验证时需要的名字
 *      type:关联的那种数据值(xl/cl/ry)-> 对应线路信息/车辆信息/人员信息,后面有的继续加
 *      modelcolname1:关联的模型字段名字1(一般应该是编码字段)
 *      modelcolname2:关联的模型字段名字2(一般应该是名字字段)
 *      datacolname1;内部数据对应的字段名字1(与模型字段1对应)
 *      datacolname2:内部数据对应的字段名字2(与模型字段2对应)
 *      showcolname:下拉框显示的内部数据字段名(注意:不是模型数据字段名),TODO:以后考虑放动态表达式,并在compile阶段使用
 *      placeholder:select placeholder字符串描述
 *
 * $$pyFilter,内部的filter指令,结合简拼音进行拼音过滤。
 * $$SearchInfoService_g,内部使用的数据服务
 */
// saSelect2指令使用的内部信service
angular.module('ScheduleApp').factory('$$SearchInfoService_g', ['$resource', function($resource) {
    return {
        xl: $resource(
            '/line/:type',
            {order: 'name', direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        ry: $resource(
            '/personnel/:type',
            {order: 'personnelName', direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        cl: $resource(
            '/cars/:type',
            {order: "insideCode", direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        ttInfo: $resource(
            '/tic/:type',
            {order: "name", direction: 'ASC'},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        ),
        cci: $resource(
            '/cci/cars',
            {},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }

        ),
        cci2: $resource(
            '/cci/:type',
            {},
            {
                list: {
                    method: 'GET',
                    isArray: true
                }
            }
        )
    }
}]);
angular.module('ScheduleApp').filter("$$pyFilter", function() {
    return function(items, props) {
        var out = [];
        var limit = props["limit"] || 20; // 默认20条记录

        if (angular.isArray(items)) {
            items.forEach(function(item) {
                if (out.length < limit) {
                    if (props.search) {
                        var upTerm = props.search.toUpperCase();
                        if(item.fullChars.indexOf(upTerm) != -1
                            || item.camelChars.indexOf(upTerm) != -1) {
                            out.push(item);
                        }
                    }
                }
            });
        }

        return out;
    };
});
angular.module('ScheduleApp').directive("saSelect2", [
    '$timeout', '$$SearchInfoService_g',
    function($timeout, $$searchInfoService_g) {
        return {
            restrict: 'E',
            templateUrl: '/pages/scheduleApp/module/other/MySearchSelectTemplate.html',
            scope: {
                model: "=" // 独立作用域,关联外部的模型对象
            },
            controllerAs: "$saSelectCtrl",
            bindToController: true,
            controller: function($scope) {
                var self = this;
                self.$$data = []; // 内部关联的数据
            },
            /**
             * 此阶段可以改dom结构,此时angular还没扫描指令,
             * 这里就可以动态添加其他angularjs的指令字符串,如required指令字符串。
             * @param tElem
             * @param tAttrs
             * @returns {{pre: Function, post: Function}}
             */
            compile: function(tElem, tAttrs) {
                // 1、获取此阶段使用的属性
                var $required_attr = tAttrs["required"]; // 用于和表单验证连接,指定成required="true"才有效。

                // 2、处理属性

                // 确定是否使用angularjs required验证
                // 属性 required
                // 如果没有填写,内部不添加验证,如果填写了,并且等于true添加验证,否则不添加
                if ($required_attr) {
                    if ($required_attr == "true") {
                        // 添加required属性指令
                        tElem.find("ui-select").attr("required", "");
                    } else {
                        // 不等于true,不添加required属性指令
                    }
                } else {
                    // 不添加required属性指令
                }

                //console.log("saSelect" + ":compile = >" + tElem.html());

                return {
                    pre: function(scope, element, attr) {
                        // TODO:
                    },
                    /**
                     * 相当于link函数。
                     *
                     * 重要属性如下:
                     * model 是绑定外部值。
                     * dicgroup 字典组的类型
                     * name input name属性值
                     */
                    post: function(scope, element, attr) {
                        // 1、获取此阶段使用的属性
                        var $name_attr = attr["name"]; // 表单验证时需要的名字
                        var $type_attr = attr["type"]; // 关联的那种数据值(xl/cl/ry)-> 对应线路信息/车辆信息/人员信息,后面有的继续加
                        var $modelcolname1_attr = attr["modelcolname1"]; // 关联的模型字段名字1(一般应该是编码字段)
                        var $modelcolname2_attr = attr["modelcolname2"]; // 关联的模型字段名字2(一般应该是名字字段)
                        var $datacolname1_attr = attr["datacolname1"]; // 内部数据对应的字段名字1(与模型字段1对应)
                        var $datacolname2_attr = attr["datacolname2"]; // 内部数据对应的字段名字2(与模型字段2对应)
                        var $showcolname_attr = attr["showcolname"]; // 下拉框显示的内部数据字段名
                        var $placeholder_attr = attr["placeholder"]; // select placeholder字符串描述

                        // 2、处理属性、转换成$saSelectCtrl内部使用的属性
                        if ($name_attr) {
                            scope["$saSelectCtrl"].$name_attr = $name_attr;
                        }
                        if ($placeholder_attr) {
                            scope["$saSelectCtrl"].$placeholder_attr = $placeholder_attr;
                        }
                        if ($showcolname_attr) {
                            scope["$saSelectCtrl"].$showcolname_attr = $showcolname_attr;
                        }

                        // 2-1、添加内部方法,根据type值,改变$$data的值
                        scope["$saSelectCtrl"].$$internal_data_change_fn = function() {
                            // 根据type属性动态载入数据
                            if ($type_attr) {
                                $$searchInfoService_g[$type_attr].list(
                                    {type: "all"},
                                    function(result) {
                                        scope["$saSelectCtrl"].$$data = [];
                                        for (var i = 0; i < result.length; i ++) {
                                            var data = {}; // data是result的一部分属性集合,根据配置来确定
                                            if ($datacolname1_attr) {
                                                data[$datacolname1_attr] = result[i][$datacolname1_attr];
                                            }
                                            if ($datacolname2_attr) {
                                                data[$datacolname2_attr] = result[i][$datacolname2_attr];
                                            }
                                            if ($showcolname_attr) {
                                                // 动态添加基于名字的拼音
                                                data[$showcolname_attr] = result[i][$showcolname_attr];
                                                if (data[$showcolname_attr]) {
                                                    data["fullChars"] = pinyin.getFullChars(result[i][$showcolname_attr]).toUpperCase(); // 全拼
                                                    data["camelChars"] = pinyin.getCamelChars(result[i][$showcolname_attr]); // 简拼
                                                }
                                            }
                                            if (data["fullChars"])
                                                scope["$saSelectCtrl"].$$data.push(data);
                                        }
                                    },
                                    function(result) {

                                    }
                                );
                            }
                        };

                        // 3、选择、删除事件映射模型和内部数据对应的字段
                        scope["$saSelectCtrl"].$select_fn_attr = function($item) {
                            if ($modelcolname1_attr && $datacolname1_attr) {
                                scope["$saSelectCtrl"].model[$modelcolname1_attr] = $item[$datacolname1_attr];
                            }
                            if ($modelcolname2_attr && $datacolname2_attr) {
                                scope["$saSelectCtrl"].model[$modelcolname2_attr] = $item[$datacolname2_attr];
                            }
                        };
                        scope["$saSelectCtrl"].$remove_fn_attr = function() {
                            if ($modelcolname1_attr) {
                                scope["$saSelectCtrl"].model[$modelcolname1_attr] = null;
                            }
                            if ($modelcolname2_attr) {
                                scope["$saSelectCtrl"].model[$modelcolname2_attr] = null;
                            }
                            scope["$saSelectCtrl"].$$cmodel = null; // 内部模型清空

                            scope["$saSelectCtrl"].$$internal_data_change_fn();
                        };

                        // 4、搜索事件
                        scope["$saSelectCtrl"].$refreshdata_fn_attr = function($search) {
                            //var fullChars = pinyin.getFullChars($search).toUpperCase();
                            //var camelChars = pinyin.getCamelChars($search);
                            //
                            //console.log(fullChars + " " + camelChars);
                            // TODO:事件暂时没用,放着以后再说
                        };

                        // 5、全部载入后,输入的
                        $timeout(function() {
                            // 创建内部使用的绑定对象,用于确认选中那个值
                            scope["$saSelectCtrl"].$$cmodel = scope["$saSelectCtrl"].model[$modelcolname1_attr];

                            scope["$saSelectCtrl"].$$internal_data_change_fn();
                        }, 0);
                    }
                }
            }
        };
    }
]);

/**
 * saSelect3指令
 * 属性如下:
 * model(必须):指定一个外部object,独立作用域,如:model=ctrl.employeeInfoForSave
 * name(必须):控件的名字
 * placeholder(可选):占位符字符串
 * dcvalue(必须):绑定的model字段值,如:dcvalue={{ctrl.employeeInfoForSave.xl.id}}
 * dcname(必须):绑定的model字段名,如:dcname=xl.id
 * icname(必须):内部与之对应的字段名,如:icname=code
 * dcname2(可选):其他需要赋值的model字段名2,如:dcname2=xl.name
 * icname2(可选):内部与之对应的字段名2,如:icname2=name
 * icnames(必须):用于用于显示,以及简评处理的内部数据字段,如:icnames=name
 * required(可选):是否要用required验证
 * datatype(必须):业务数据类型,有字典类型,动态数据类型,暂时写的死点
 * mlp(可选):是否多级属性(这里假设外部model如果多级,内部model也是多级)
 *
 * 高级属性:
 * dataassociate(可选):数据源是否关联属性(内部数据随外部指定的参数变化而变化)
 * dataparam(可选):数据源关联的外部参数对象
 *
 */
angular.module('ScheduleApp').directive("saSelect3", [
    '$timeout',
    '$$SearchInfoService_g',
    function($timeout, $$searchInfoService_g) {
        return {
            restrict: 'E',
            templateUrl: '/pages/scheduleApp/module/common/dt/MyUiSelectWrapTemplate1.html',
            scope: {
                model: "=" // 独立作用域,关联外部的模型object
            },
            controllerAs: "$saSelectCtrl",
            bindToController: true,
            controller: function($scope) {
                var self = this;
                self.$$data = []; // ui-select显示用的数据源
                self.$$data_real= []; // 内部真实的数据源
            },

            /**
             * 此阶段可以改dom结构,此时angular还没扫描指令,
             * 这里就可以动态添加其他angularjs的指令字符串,如required指令字符串。
             * @param tElem
             * @param tAttrs
             * @returns {{pre: Function, post: Function}}
             */
            compile: function(tElem, tAttrs) {
                // 获取所有的属性
                var $name_attr = tAttrs["name"]; // 控件的名字
                var $placeholder_attr = tAttrs["placeholder"]; // 占位符的名字
                var $dcname_attr = tAttrs["dcname"]; // 绑定的model字段名
                var $icname_attr = tAttrs["icname"]; // 内部与之对应的字段名
                var $dcname2_attr = tAttrs["dcname2"]; // 其他需要赋值的model字段名2
                var $icname2_attr = tAttrs["icname2"]; // 内部与之对应的字段名2
                var $icname_s_attr = tAttrs["icnames"]; // 用于用于显示,以及简评处理的内部数据字段
                var $datatype_attr = tAttrs["datatype"]; // 内部业务数据类型
                var $required_attr = tAttrs["required"]; // 是否需要required验证
                var $mlp_attr = tAttrs["mlp"]; // 是否多级属性
                var $dataassociate_attr = tAttrs["dataassociate"]; // 数据源是否关联属性

                // controlAs名字
                var ctrlAs = "$saSelectCtrl";

                // 数据源初始化标志
                var $$data_init = false;
                // 如果有required属性,添加angularjs required验证
                if ($required_attr != undefined) {
                    tElem.find("ui-select").attr("required", "");
                }

                // 由于有的属性是多级的如xl.name,所以要在compile阶段重写属性绑定属性定义
                // 原来的设置:{{$select.selected[$saSelectCtrl.$icname_s]}}
                tElem.find("ui-select-match").html("{{$select.selected" + "." + $icname_s_attr + "}}");
                // 原来的设置:item[$saSelectCtrl.$icname] as item in $saSelectCtrl.$$data
                tElem.find("ui-select-choices").attr("repeat", "item" + "." + $icname_attr + " as item in $saSelectCtrl.$$data");
                // 原来的设置:item[$saSelectCtrl.$icname_s]
                tElem.find("ui-select-choices span").attr("ng-bind", "item" + "." + $icname_s_attr);
                // 原来的设置:{{$saSelectCtrl.$name}}
                tElem.find("ui-select").attr("name", $name_attr);
                // 原来的设置:{{$saSelectCtrl.$placeholder}}
                tElem.find("ui-select-match").attr("placeholder", $placeholder_attr);

                return {
                    pre: function(scope, element, attr) {
                        // TODO:
                    },
                    /**
                     * 相当于link函数。
                     * @param scope
                     * @param element
                     * @param attr
                     */
                    post: function(scope, element, attr) {
                        // 添加选中事件处理函数
                        scope[ctrlAs].$$internal_select_fn = function($item) {
                            if ($dcname_attr && $icname_attr) {
                                if ($mlp_attr) {
                                    eval("scope[ctrlAs].model" + "." + $dcname_attr + " = $item" + "." + $icname_attr + ";");
                                } else {
                                    scope[ctrlAs].model[$dcname_attr] = $item[$icname_attr];
                                }
                            }
                            if ($dcname2_attr && $icname2_attr) {
                                if ($mlp_attr) {
                                    eval("scope[ctrlAs].model" + "." + $dcname2_attr + " = $item" + "." + $icname2_attr + ";");
                                } else {
                                    scope[ctrlAs].model[$dcname2_attr] = $item[$icname2_attr];
                                }
                            }
                        };

                        // 删除选中事件处理函数
                        scope[ctrlAs].$$internal_remove_fn = function() {
                            scope[ctrlAs].$$internalmodel = undefined;
                            if ($mlp_attr) {
                                eval("scope[ctrlAs].model" + "." + $dcname_attr + " = undefined;");
                            } else {
                                scope[ctrlAs].model[$dcname_attr] = undefined;
                            }

                            if ($dcname2_attr) {
                                if ($mlp_attr) {
                                    eval("scope[ctrlAs].model" + "." + $dcname2_attr + " = undefined;");
                                } else {
                                    scope[ctrlAs].model[$dcname2_attr] = undefined;
                                }
                            }
                        };

                        /**
                         * 内部方法,读取字典数据作为数据源。
                         * @param dicgroup 字典类型,如:gsType
                         * @param ccol 代码字段名
                         * @param ncol 名字字段名
                         */
                        scope[ctrlAs].$$internal_dic_data = function(dicgroup, ccol, ncol) {
                            var origin_dicgroup = dictionaryUtils.getByGroup(dicgroup);
                            var dic_key; // 字典key
                            // 清空内部数据
                            scope[ctrlAs].$$data_real = [];
                            for (dic_key in origin_dicgroup) {
                                var data = {}; // 重新组合的字典元素对象
                                if (dic_key == "true")
                                    data[ccol] = true;
                                else
                                    data[ccol] = dic_key;
                                data[ncol] = origin_dicgroup[dic_key];
                                scope[ctrlAs].$$data_real.push(data);
                            }
                            // 这里直接将$$data_real数据深拷贝到$$data
                            angular.copy(scope[ctrlAs].$$data_real, scope[ctrlAs].$$data);
                        };

                        /**
                         * TODO:这个方法有性能问题,result一多就会卡一卡,之后再解决把
                         * 内部方法,读取字典数据作为数据源。
                         * @param result 原始数据
                         * @param dcvalue 传入的关联数据
                         */
                        scope[ctrlAs].$$internal_other_data = function(result, dcvalue) {
                            //console.log("start");
                            // 清空内部数据
                            scope[ctrlAs].$$data_real = [];
                            scope[ctrlAs].$$data = []
                            for (var i = 0; i < result.length; i ++) {
                                if ($icname_s_attr) {
                                    if ($mlp_attr) {
                                        if (eval("result[i]" + "." + $icname_s_attr)) {
                                            // 全拼
                                            result[i]["fullChars"] = pinyin.getFullChars(eval("result[i]" + "." + $icname_s_attr)).toUpperCase();
                                            // 简拼
                                            result[i]["camelChars"] = pinyin.getCamelChars(eval("result[i]" + "." + $icname_s_attr));
                                        }
                                    } else {
                                        if (result[i][$icname_s_attr]) {
                                            // 全拼
                                            result[i]["fullChars"] = pinyin.getFullChars(result[i][$icname_s_attr]).toUpperCase();
                                            // 简拼
                                            result[i]["camelChars"] = pinyin.getCamelChars(result[i][$icname_s_attr]);
                                        }
                                    }
                                }

                                if (result[i]["fullChars"]) { // 有拼音的加入数据源
                                    scope[ctrlAs].$$data_real.push(result[i]);
                                }

                            }
                            //console.log("start2");

                            // 数量太大取前10条记录作为显示
                            if (angular.isArray(scope[ctrlAs].$$data_real)) {
                                // 先迭代循环查找已经传过来的值
                                if (scope[ctrlAs].$$data_real.length > 0) {
                                    if (dcvalue) {
                                        for (var j = 0; j < scope[ctrlAs].$$data_real.length; j++) {
                                            if (scope[ctrlAs].$$data_real[j][$icname_attr] == dcvalue) {
                                                scope[ctrlAs].$$data.push(angular.copy(scope[ctrlAs].$$data_real[j]));
                                                break;
                                            }
                                        }
                                    }
                                }
                                // 在插入剩余的数据
                                for (var k = 0; k < scope[ctrlAs].$$data_real.length; k++) {
                                    if (scope[ctrlAs].$$data.length < 10) {
                                        if ($mlp_attr) {
                                            if (eval("scope[ctrlAs].$$data_real[k]" + "." + $icname_attr + " != dcvalue")) {
                                                scope[ctrlAs].$$data.push(angular.copy(scope[ctrlAs].$$data_real[k]));
                                            }
                                        } else {
                                            if (scope[ctrlAs].$$data_real[k][$icname_attr] != dcvalue) {
                                                scope[ctrlAs].$$data.push(angular.copy(scope[ctrlAs].$$data_real[k]));
                                            }
                                        }
                                    } else {
                                        break;
                                    }
                                }
                            }

                            //console.log("end");
                        };

                        /**
                         * 判定一个对象是否为空对象。
                         * @param Obj
                         */
                        scope[ctrlAs].$$internal_isEmpty_obj = function(obj) {
                            console.log(typeof obj);

                            if (typeof obj === "object" && !(obj instanceof Array)) {
                                for (var prop in obj) {
                                    if (obj.hasOwnProperty(prop)) {
                                        return false;
                                    }
                                }
                                return true;
                            } else {
                                throw "必须是对象";
                            }
                        };

                        // 刷新数据
                        scope[ctrlAs].$$internal_refresh_fn = function(search) {
                            // 绑定的model字段值,此属性是绑定属性,只能在link阶段获取
                            var $dcvalue_attr = attr["dcvalue"];

                            console.log("刷新数据:" + $dcvalue_attr);

                            if (!$$data_init) { // 只初始化$$data_real一次,重新载入页面才能重新初始化
                                if (dictionaryUtils.getByGroup($datatype_attr)) { // 判定是否字典类型数据源
                                    scope[ctrlAs].$$internal_dic_data(
                                        $datatype_attr, $icname_attr, $icname_s_attr);
                                    if ($dcvalue_attr) {
                                        scope[ctrlAs].$$internalmodel = $dcvalue_attr;
                                    }
                                } else { // 非字典类型数据源
                                    if (!$dataassociate_attr) {
                                        $$searchInfoService_g[$datatype_attr].list(
                                            {type: "all"},
                                            function(result) {
                                                //console.log("ok:" + $datatype_attr);
                                                scope[ctrlAs].$$internal_other_data(result, $dcvalue_attr);
                                                //console.log("ok2:" + $datatype_attr);
                                                if ($dcvalue_attr) {
                                                    scope[ctrlAs].$$internalmodel = $dcvalue_attr;
                                                }

                                                $$data_init = true;
                                            },
                                            function(result) {

                                            }
                                        );
                                    }
                                }
                            }

                            if ($$data_init) {
                                if (search && search != "") { // 有search值
                                    if (!dictionaryUtils.getByGroup($datatype_attr)) { // 其他数据源
                                        // 处理search
                                        console.log("search:" + search);

                                        scope[ctrlAs].$$data = [];
                                        for (var k = 0; k < scope[ctrlAs].$$data_real.length; k++) {
                                            var upTerm = search.toUpperCase();
                                            if (scope[ctrlAs].$$data.length < 10) {
                                                if (scope[ctrlAs].$$data_real[k].fullChars.indexOf(upTerm) != -1
                                                    || scope[ctrlAs].$$data_real[k].camelChars.indexOf(upTerm) != -1) {
                                                    scope[ctrlAs].$$data.push(angular.copy(scope[ctrlAs].$$data_real[k]));
                                                }
                                            } else {
                                                break;
                                            }
                                        }
                                    }
                                }

                            }

                        };










                        // TODO:

                        // dom全部载入后调用
                        $timeout(function() {
                            console.log("dom全部载入后调用");
                        }, 0);
                        // 监控dcvalue model值变换
                        attr.$observe("dcvalue", function(value) {
                                console.log("监控dc1 model值变换:" + value);
                                scope[ctrlAs].$$internalmodel = value;
                            }
                        );
                        // 监控获取数据参数变换
                        attr.$observe("dataparam", function(value) {
                                // 判定是否空对象
                                console.log(value);
                                var obj = JSON.parse(value);
                                var $dcvalue_attr = attr["dcvalue"];
                                if (!scope[ctrlAs].$$internal_isEmpty_obj(obj)) {
                                    console.log("dataparam:" + obj);

                                    //

                                    obj["type"] = "all";

                                    $$data_init = false;
                                    $$searchInfoService_g[$datatype_attr].list(
                                        obj,
                                        function(result) {
                                            //console.log("ok:" + $datatype_attr);
                                            scope[ctrlAs].$$internal_other_data(result, $dcvalue_attr);
                                            //console.log("ok2:" + $datatype_attr);
                                            if ($dcvalue_attr) {
                                                scope[ctrlAs].$$internalmodel = $dcvalue_attr;
                                            }

                                            $$data_init = true;
                                        },
                                        function(result) {

                                        }
                                    );
                                }
                            }
                        );
                    }
                };
            }
        };

    }
]);