directive.js 14.8 KB
Newer Older
L
lepdou 已提交
1
/** navbar */
L
abtest  
lepdou 已提交
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
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) {

                                       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 已提交
120 121 122 123 124

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

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

142 143 144 145 146 147 148 149 150 151
            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 已提交
152
                            //default checked
153 154 155
                            cluster.checked = scope.defaultAllChecked ||
                                              (cluster.env == scope.defaultCheckedEnv && cluster.name
                                                                                         == scope.defaultCheckedCluster);
L
lepdou 已提交
156
                            //not checked
L
abtest  
lepdou 已提交
157
                            if (cluster.env == scope.notCheckedEnv && cluster.name == scope.notCheckedCluster) {
L
lepdou 已提交
158 159 160
                                cluster.checked = false;
                            }

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

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

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

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

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

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

L
lepdou 已提交
200 201 202 203
        }
    }

});
L
lepdou 已提交
204

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

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

L
lepdou 已提交
233 234 235 236 237 238 239 240
            scope.$watch("detail", function () {
                scope.detailAsHtml = $sce.trustAsHtml(scope.detail);
            });

            if (!scope.confirmBtnText) {
                scope.confirmBtnText = '确认';
            }
            
L
lepdou 已提交
241
            scope.confirm = function () {
L
abtest  
lepdou 已提交
242
                if (scope.doConfirm) {
L
lepdou 已提交
243 244
                    scope.doConfirm();
                }
L
lepdou 已提交
245 246 247
            };
            

L
abtest  
lepdou 已提交
248

L
lepdou 已提交
249 250
        }
    }
L
lepdou 已提交
251
});
L
lepdou 已提交
252 253 254 255 256

/** entrance */
directive_module.directive('apolloentrance', function ($compile, $window) {
    return {
        restrict: 'E',
L
lepdou 已提交
257
        templateUrl: '../../views/component/entrance.html',
L
lepdou 已提交
258 259 260 261 262 263 264 265 266 267 268 269
        transclude: true,
        replace: true,
        scope: {
            imgSrc: '=apolloImgSrc',
            title: '=apolloTitle',
            href: '=apolloHref'
        },
        link: function (scope, element, attrs) {
        }
    }
});

L
lepdou 已提交
270 271 272 273
/** entrance */
directive_module.directive('apollouserselector', function ($compile, $window) {
    return {
        restrict: 'E',
L
lepdou 已提交
274
        templateUrl: '../../views/component/user-selector.html',
L
lepdou 已提交
275 276 277
        transclude: true,
        replace: true,
        scope: {
L
lepdou 已提交
278 279
            id: '=apolloId',
            disabled: '='
L
lepdou 已提交
280 281 282 283 284
        },
        link: function (scope, element, attrs) {

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

L
lepdou 已提交
285
            var select2Options = {
L
lepdou 已提交
286 287 288 289 290 291
                ajax: {
                    url: '/users',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
L
abtest  
lepdou 已提交
292
                            keyword: params.term ? params.term : '',
L
lepdou 已提交
293 294 295 296 297 298 299 300
                            limit: 100
                        }
                    },
                    processResults: function (data, params) {
                        var users = [];
                        data.forEach(function (user) {
                            users.push({
                                           id: user.userId,
301
                                           text: user.userId + " | " + user.name
L
lepdou 已提交
302 303 304 305 306 307 308 309 310 311 312 313
                                       })
                        });
                        return {
                            results: users
                        }

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

L
abtest  
lepdou 已提交
314
            function initSelect2() {
L
lepdou 已提交
315
                $('.' + scope.id).select2(select2Options);
L
lepdou 已提交
316
            }
L
lepdou 已提交
317
            
L
lepdou 已提交
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
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 已提交
372