directive.js 12.9 KB
Newer Older
L
lepdou 已提交
1
/** navbar */
L
abtest  
lepdou 已提交
2 3 4 5 6 7 8 9 10
directive_module.directive('apollonav',
                           function ($compile, $window, toastr, AppUtil, AppService, EnvService, UserService) {
                               return {
                                   restrict: 'E',
                                   templateUrl: '../../views/common/nav.html',
                                   transclude: true,
                                   replace: true,
                                   link: function (scope, element, attrs) {

11

L
abtest  
lepdou 已提交
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
                                       scope.sourceApps = [];
                                       scope.copyedApps = [];

                                       AppService.find_apps().then(function (result) {
                                           result.forEach(function (app) {
                                               app.selected = false;
                                               scope.sourceApps.push(app);
                                           });
                                           scope.copyedApps = angular.copy(scope.sourceApps);
                                       }, function (result) {
                                           toastr.error(AppUtil.errorMsg(result), "load apps error");
                                       });

                                       scope.searchKey = '';
                                       scope.shouldShowAppList = false;

                                       var selectedApp = {};
                                       scope.selectApp = function (app) {
                                           select(app);
                                           scope.jumpToConfigPage();
                                       };

                                       scope.changeSearchKey = function () {
                                           scope.copyedApps = [];
                                           var searchKey = scope.searchKey.toLocaleLowerCase();
                                           scope.sourceApps.forEach(function (app) {
                                               if (app.name.toLocaleLowerCase().indexOf(searchKey) > -1
                                                   || app.appId.toLocaleLowerCase().indexOf(searchKey) > -1) {
                                                   scope.copyedApps.push(app);
                                               }
                                           });
                                           scope.shouldShowAppList = true;
                                       };

                                       scope.jumpToConfigPage = function () {
                                           if (selectedApp.appId) {
                                               if ($window.location.href.indexOf("config.html") > -1) {
                                                   $window.location.hash = "appid=" + selectedApp.appId;
                                                   $window.location.reload();
                                               } else {
                                                   $window.location.href = '/config.html?#appid=' + selectedApp.appId;
                                               }
                                           }
                                       };

                                       //up:38 down:40 enter:13
                                       var selectedAppIdx = -1;
                                       element.bind("keydown keypress", function (event) {

                                           if (event.keyCode == 40) {
                                               if (selectedAppIdx < scope.copyedApps.length - 1) {
                                                   clearAppsSelectedStatus();
                                                   scope.copyedApps[++selectedAppIdx].selected = true;
                                               }
                                           } else if (event.keyCode == 38) {
                                               if (selectedAppIdx >= 1) {
                                                   clearAppsSelectedStatus();
                                                   scope.copyedApps[--selectedAppIdx].selected = true;
                                               }
                                           } else if (event.keyCode == 13) {
                                               if (scope.shouldShowAppList && selectedAppIdx > -1) {
                                                   select(scope.copyedApps[selectedAppIdx]);
                                                   event.preventDefault();
                                               } else {
                                                   scope.jumpToConfigPage();
                                               }

                                           }
                                           //强制刷新
                                           scope.$apply(function () {
                                               scope.copyedApps = scope.copyedApps;
                                           });
                                       });

                                       $(".search-input").on("click", function (event) {
                                           event.stopPropagation();
                                       });

                                       $(document).on('click', function () {
                                           scope.$apply(function () {
                                               scope.shouldShowAppList = false;
                                           });
                                       });

                                       function clearAppsSelectedStatus() {
                                           scope.copyedApps.forEach(function (app) {
                                               app.selected = false;
                                           })

                                       }

                                       function select(app) {
                                           selectedApp = app;
                                           scope.searchKey = app.name;
                                           scope.shouldShowAppList = false;
                                           clearAppsSelectedStatus();
                                           selectedAppIdx = -1;

                                       }

                                       UserService.load_user().then(function (result) {
                                           scope.userName = result.userId;
                                       }, function (result) {

                                       });
                                   }
                               }

                           });
L
lepdou 已提交
121 122 123 124 125

/** env cluster selector*/
directive_module.directive('apolloclusterselector', function ($compile, $window, AppService, AppUtil, toastr) {
    return {
        restrict: 'E',
L
lepdou 已提交
126
        templateUrl: '../../views/component/env-selector.html',
L
lepdou 已提交
127 128 129 130
        transclude: true,
        replace: true,
        scope: {
            appId: '=apolloAppId',
131 132 133
            defaultAllChecked: '=apolloDefaultAllChecked',
            select: '=apolloSelect',
            defaultCheckedEnv: '=apolloDefaultCheckedEnv',
L
lepdou 已提交
134
            defaultCheckedCluster: '=apolloDefaultCheckedCluster',
L
abtest  
lepdou 已提交
135
            notCheckedEnv: '=apolloNotCheckedEnv',
L
lepdou 已提交
136
            notCheckedCluster: '=apolloNotCheckedCluster'
L
lepdou 已提交
137 138
        },
        link: function (scope, element, attrs) {
L
abtest  
lepdou 已提交
139

L
lepdou 已提交
140 141
            scope.$watch("defaultCheckedEnv", refreshClusterList);
            scope.$watch("defaultCheckedCluster", refreshClusterList);
L
lepdou 已提交
142

143 144 145 146 147 148 149 150 151 152
            refreshClusterList();

            function refreshClusterList() {
                AppService.load_nav_tree(scope.appId).then(function (result) {
                    scope.clusters = [];
                    var envClusterInfo = AppUtil.collectData(result);
                    envClusterInfo.forEach(function (node) {
                        var env = node.env;
                        node.clusters.forEach(function (cluster) {
                            cluster.env = env;
L
lepdou 已提交
153
                            //default checked
154 155 156
                            cluster.checked = scope.defaultAllChecked ||
                                              (cluster.env == scope.defaultCheckedEnv && cluster.name
                                                                                         == scope.defaultCheckedCluster);
L
lepdou 已提交
157
                            //not checked
L
abtest  
lepdou 已提交
158
                            if (cluster.env == scope.notCheckedEnv && cluster.name == scope.notCheckedCluster) {
L
lepdou 已提交
159 160 161
                                cluster.checked = false;
                            }

162 163 164 165 166 167 168 169
                            scope.clusters.push(cluster);
                        })
                    });
                    scope.select(collectSelectedClusters());
                });
            }

            scope.envAllSelected = scope.defaultAllChecked;
L
lepdou 已提交
170 171 172 173 174 175 176 177 178

            scope.toggleEnvsCheckedStatus = function () {
                scope.envAllSelected = !scope.envAllSelected;
                scope.clusters.forEach(function (cluster) {
                    cluster.checked = scope.envAllSelected;
                });
                scope.select(collectSelectedClusters());
            };

L
lepdou 已提交
179
            scope.switchSelect = function (o, $event) {
L
lepdou 已提交
180
                o.checked = !o.checked;
L
lepdou 已提交
181 182 183 184 185 186
                $event.stopPropagation();
                scope.select(collectSelectedClusters());
            };

            scope.toggleClusterCheckedStatus = function (cluster) {
                cluster.checked = !cluster.checked;
L
lepdou 已提交
187 188 189 190 191 192
                scope.select(collectSelectedClusters());
            };

            function collectSelectedClusters() {
                var selectedClusters = [];
                scope.clusters.forEach(function (cluster) {
L
lepdou 已提交
193
                    if (cluster.checked) {
L
lepdou 已提交
194 195 196 197 198 199
                        cluster.clusterName = cluster.name;
                        selectedClusters.push(cluster);
                    }
                });
                return selectedClusters;
            }
L
lepdou 已提交
200

L
lepdou 已提交
201 202 203 204
        }
    }

});
L
lepdou 已提交
205

L
lepdou 已提交
206
/** 必填项*/
L
abtest  
lepdou 已提交
207
directive_module.directive('apollorequiredfield', function ($compile, $window) {
L
lepdou 已提交
208 209
    return {
        restrict: 'E',
L
abtest  
lepdou 已提交
210
        template: '<strong style="color: red">*</strong>',
L
lepdou 已提交
211 212 213
        transclude: true,
        replace: true
    }
L
lepdou 已提交
214
});
L
lepdou 已提交
215

L
lepdou 已提交
216
/**  确认框 */
L
lepdou 已提交
217 218 219
directive_module.directive('apolloconfirmdialog', function ($compile, $window) {
    return {
        restrict: 'E',
L
lepdou 已提交
220
        templateUrl: '../../views/component/confirm-dialog.html',
L
lepdou 已提交
221 222 223 224 225 226 227
        transclude: true,
        replace: true,
        scope: {
            dialogId: '=apolloDialogId',
            title: '=apolloTitle',
            detail: '=apolloDetail',
            showCancelBtn: '=apolloShowCancelBtn',
L
abtest  
lepdou 已提交
228 229
            doConfirm: '=apolloConfirm',
            cancel:'='
L
lepdou 已提交
230 231
        },
        link: function (scope, element, attrs) {
L
abtest  
lepdou 已提交
232

L
lepdou 已提交
233
            scope.confirm = function () {
L
abtest  
lepdou 已提交
234
                if (scope.doConfirm) {
L
lepdou 已提交
235 236 237
                    scope.doConfirm();
                }
            }
L
abtest  
lepdou 已提交
238

L
lepdou 已提交
239 240
        }
    }
L
lepdou 已提交
241
});
L
lepdou 已提交
242 243 244 245 246

/** entrance */
directive_module.directive('apolloentrance', function ($compile, $window) {
    return {
        restrict: 'E',
L
lepdou 已提交
247
        templateUrl: '../../views/component/entrance.html',
L
lepdou 已提交
248 249 250 251 252 253 254 255 256 257 258 259
        transclude: true,
        replace: true,
        scope: {
            imgSrc: '=apolloImgSrc',
            title: '=apolloTitle',
            href: '=apolloHref'
        },
        link: function (scope, element, attrs) {
        }
    }
});

L
lepdou 已提交
260 261 262 263
/** entrance */
directive_module.directive('apollouserselector', function ($compile, $window) {
    return {
        restrict: 'E',
L
lepdou 已提交
264
        templateUrl: '../../views/component/user-selector.html',
L
lepdou 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        transclude: true,
        replace: true,
        scope: {
            id: '=apolloId'
        },
        link: function (scope, element, attrs) {

            scope.$watch("id", initSelect2);

            var searchUsersAjax = {
                ajax: {
                    url: '/users',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
L
abtest  
lepdou 已提交
281
                            keyword: params.term ? params.term : '',
L
lepdou 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                            limit: 100
                        }
                    },
                    processResults: function (data, params) {
                        var users = [];
                        data.forEach(function (user) {
                            users.push({
                                           id: user.userId,
                                           text: user.userId + " | " + user.name + " | " + user.email
                                       })
                        });
                        return {
                            results: users
                        }

                    },
                    cache: true,
                    minimumInputLength: 5
                }
            };

L
abtest  
lepdou 已提交
303
            function initSelect2() {
L
lepdou 已提交
304 305 306 307 308 309 310 311
                $('.' + scope.id).select2(searchUsersAjax);
            }

        }
    }
});