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

12 13 14 15
                                       CommonService.getPageSetting().then(function (setting) {
                                           scope.pageSetting = setting;
                                       });

L
abtest  
lepdou 已提交
16
                                       scope.sourceApps = [];
陈慧 已提交
17
                                       scope.copiedApps = [];
L
abtest  
lepdou 已提交
18 19 20 21 22 23

                                       AppService.find_apps().then(function (result) {
                                           result.forEach(function (app) {
                                               app.selected = false;
                                               scope.sourceApps.push(app);
                                           });
陈慧 已提交
24
                                           scope.copiedApps = angular.copy(scope.sourceApps);
L
abtest  
lepdou 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
                                       }, 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 () {
陈慧 已提交
39
                                           scope.copiedApps = [];
L
abtest  
lepdou 已提交
40 41 42 43
                                           var searchKey = scope.searchKey.toLocaleLowerCase();
                                           scope.sourceApps.forEach(function (app) {
                                               if (app.name.toLocaleLowerCase().indexOf(searchKey) > -1
                                                   || app.appId.toLocaleLowerCase().indexOf(searchKey) > -1) {
陈慧 已提交
44
                                                   scope.copiedApps.push(app);
L
abtest  
lepdou 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
                                               }
                                           });
                                           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) {
陈慧 已提交
66
                                               if (selectedAppIdx < scope.copiedApps.length - 1) {
L
abtest  
lepdou 已提交
67
                                                   clearAppsSelectedStatus();
陈慧 已提交
68
                                                   scope.copiedApps[++selectedAppIdx].selected = true;
L
abtest  
lepdou 已提交
69 70 71 72
                                               }
                                           } else if (event.keyCode == 38) {
                                               if (selectedAppIdx >= 1) {
                                                   clearAppsSelectedStatus();
陈慧 已提交
73
                                                   scope.copiedApps[--selectedAppIdx].selected = true;
L
abtest  
lepdou 已提交
74 75 76
                                               }
                                           } else if (event.keyCode == 13) {
                                               if (scope.shouldShowAppList && selectedAppIdx > -1) {
陈慧 已提交
77
                                                   select(scope.copiedApps[selectedAppIdx]);
L
abtest  
lepdou 已提交
78 79 80 81 82 83 84 85
                                                   event.preventDefault();
                                               } else {
                                                   scope.jumpToConfigPage();
                                               }

                                           }
                                           //强制刷新
                                           scope.$apply(function () {
86
                                               scope.copiedApps = scope.copiedApps;
L
abtest  
lepdou 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100
                                           });
                                       });

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

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

                                       function clearAppsSelectedStatus() {
101
                                           scope.copiedApps.forEach(function (app) {
L
abtest  
lepdou 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                                               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) {

                                       });
121 122 123 124

                                       PermissionService.has_root_permission().then(function(result) {
                                           scope.hasRootPermission = result.hasPermission;
                                       })
L
abtest  
lepdou 已提交
125 126 127 128
                                   }
                               }

                           });
L
lepdou 已提交
129 130 131 132 133

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

L
lepdou 已提交
148 149
            scope.$watch("defaultCheckedEnv", refreshClusterList);
            scope.$watch("defaultCheckedCluster", refreshClusterList);
L
lepdou 已提交
150

151 152 153 154 155 156 157 158 159 160
            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 已提交
161
                            //default checked
162 163 164
                            cluster.checked = scope.defaultAllChecked ||
                                              (cluster.env == scope.defaultCheckedEnv && cluster.name
                                                                                         == scope.defaultCheckedCluster);
L
lepdou 已提交
165
                            //not checked
L
abtest  
lepdou 已提交
166
                            if (cluster.env == scope.notCheckedEnv && cluster.name == scope.notCheckedCluster) {
L
lepdou 已提交
167 168 169
                                cluster.checked = false;
                            }

170 171 172 173 174 175 176 177
                            scope.clusters.push(cluster);
                        })
                    });
                    scope.select(collectSelectedClusters());
                });
            }

            scope.envAllSelected = scope.defaultAllChecked;
L
lepdou 已提交
178 179 180 181 182 183 184 185 186

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

L
lepdou 已提交
187
            scope.switchSelect = function (o, $event) {
L
lepdou 已提交
188
                o.checked = !o.checked;
L
lepdou 已提交
189 190 191 192 193 194
                $event.stopPropagation();
                scope.select(collectSelectedClusters());
            };

            scope.toggleClusterCheckedStatus = function (cluster) {
                cluster.checked = !cluster.checked;
L
lepdou 已提交
195 196 197 198 199 200
                scope.select(collectSelectedClusters());
            };

            function collectSelectedClusters() {
                var selectedClusters = [];
                scope.clusters.forEach(function (cluster) {
L
lepdou 已提交
201
                    if (cluster.checked) {
L
lepdou 已提交
202 203 204 205 206 207
                        cluster.clusterName = cluster.name;
                        selectedClusters.push(cluster);
                    }
                });
                return selectedClusters;
            }
L
lepdou 已提交
208

L
lepdou 已提交
209 210 211 212
        }
    }

});
L
lepdou 已提交
213

L
lepdou 已提交
214
/** 必填项*/
L
abtest  
lepdou 已提交
215
directive_module.directive('apollorequiredfield', function ($compile, $window) {
L
lepdou 已提交
216 217
    return {
        restrict: 'E',
L
abtest  
lepdou 已提交
218
        template: '<strong style="color: red">*</strong>',
L
lepdou 已提交
219 220 221
        transclude: true,
        replace: true
    }
L
lepdou 已提交
222
});
L
lepdou 已提交
223

L
lepdou 已提交
224
/**  确认框 */
L
lepdou 已提交
225
directive_module.directive('apolloconfirmdialog', function ($compile, $window, $sce) {
L
lepdou 已提交
226 227
    return {
        restrict: 'E',
L
lepdou 已提交
228
        templateUrl: '../../views/component/confirm-dialog.html',
L
lepdou 已提交
229 230 231 232 233 234 235
        transclude: true,
        replace: true,
        scope: {
            dialogId: '=apolloDialogId',
            title: '=apolloTitle',
            detail: '=apolloDetail',
            showCancelBtn: '=apolloShowCancelBtn',
L
abtest  
lepdou 已提交
236
            doConfirm: '=apolloConfirm',
L
lepdou 已提交
237
            confirmBtnText: '=?',
238
            cancel: '='
L
lepdou 已提交
239 240
        },
        link: function (scope, element, attrs) {
L
abtest  
lepdou 已提交
241

L
lepdou 已提交
242 243 244 245 246 247 248 249
            scope.$watch("detail", function () {
                scope.detailAsHtml = $sce.trustAsHtml(scope.detail);
            });

            if (!scope.confirmBtnText) {
                scope.confirmBtnText = '确认';
            }
            
L
lepdou 已提交
250
            scope.confirm = function () {
L
abtest  
lepdou 已提交
251
                if (scope.doConfirm) {
L
lepdou 已提交
252 253
                    scope.doConfirm();
                }
L
lepdou 已提交
254 255 256
            };
            

L
abtest  
lepdou 已提交
257

L
lepdou 已提交
258 259
        }
    }
L
lepdou 已提交
260
});
L
lepdou 已提交
261 262 263 264 265

/** entrance */
directive_module.directive('apolloentrance', function ($compile, $window) {
    return {
        restrict: 'E',
L
lepdou 已提交
266
        templateUrl: '../../views/component/entrance.html',
L
lepdou 已提交
267 268 269 270 271 272 273 274 275 276 277 278
        transclude: true,
        replace: true,
        scope: {
            imgSrc: '=apolloImgSrc',
            title: '=apolloTitle',
            href: '=apolloHref'
        },
        link: function (scope, element, attrs) {
        }
    }
});

L
lepdou 已提交
279 280 281 282
/** entrance */
directive_module.directive('apollouserselector', function ($compile, $window) {
    return {
        restrict: 'E',
L
lepdou 已提交
283
        templateUrl: '../../views/component/user-selector.html',
L
lepdou 已提交
284 285 286
        transclude: true,
        replace: true,
        scope: {
L
lepdou 已提交
287 288
            id: '=apolloId',
            disabled: '='
L
lepdou 已提交
289 290 291 292 293
        },
        link: function (scope, element, attrs) {

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

L
lepdou 已提交
294
            var select2Options = {
L
lepdou 已提交
295 296 297 298 299 300
                ajax: {
                    url: '/users',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
L
abtest  
lepdou 已提交
301
                            keyword: params.term ? params.term : '',
L
lepdou 已提交
302 303 304 305 306 307 308 309
                            limit: 100
                        }
                    },
                    processResults: function (data, params) {
                        var users = [];
                        data.forEach(function (user) {
                            users.push({
                                           id: user.userId,
310
                                           text: user.userId + " | " + user.name
L
lepdou 已提交
311 312 313 314 315 316 317 318 319 320 321 322
                                       })
                        });
                        return {
                            results: users
                        }

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

L
abtest  
lepdou 已提交
323
            function initSelect2() {
L
lepdou 已提交
324
                $('.' + scope.id).select2(select2Options);
L
lepdou 已提交
325
            }
L
lepdou 已提交
326
            
L
lepdou 已提交
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
directive_module.directive('apollomultipleuserselector', function ($compile, $window) {
    return {
        restrict: 'E',
        templateUrl: '../../views/component/multiple-user-selector.html',
        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 {
                            keyword: params.term ? params.term : '',
                            limit: 100
                        }
                    },
                    processResults: function (data, params) {
                        var users = [];
                        data.forEach(function (user) {
                            users.push({
                                           id: user.userId,
                                           text: user.userId + " | " + user.name
                                       })
                        });
                        return {
                            results: users
                        }

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

            function initSelect2() {
                $('.' + scope.id).select2(searchUsersAjax);
            }
        }
    }
});

L
lepdou 已提交
381