directive.js 14.8 KB
Newer Older
1
/** navbar */
lepdou committed
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) {

                                       });
                                   }
                               }

                           });
120 121 122 123 124

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

lepdou committed
139 140
            scope.$watch("defaultCheckedEnv", refreshClusterList);
            scope.$watch("defaultCheckedCluster", refreshClusterList);
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;
lepdou committed
152
                            //default checked
153 154 155
                            cluster.checked = scope.defaultAllChecked ||
                                              (cluster.env == scope.defaultCheckedEnv && cluster.name
                                                                                         == scope.defaultCheckedCluster);
lepdou committed
156
                            //not checked
lepdou committed
157
                            if (cluster.env == scope.notCheckedEnv && cluster.name == scope.notCheckedCluster) {
lepdou committed
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;
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());
            };

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

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

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

200 201 202 203
        }
    }

});
lepdou committed
204

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

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

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

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

lepdou committed
248

lepdou committed
249 250
        }
    }
lepdou committed
251
});
lepdou committed
252 253 254 255 256

/** entrance */
directive_module.directive('apolloentrance', function ($compile, $window) {
    return {
        restrict: 'E',
lepdou committed
257
        templateUrl: '../../views/component/entrance.html',
lepdou committed
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) {
        }
    }
});

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

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

285
            var select2Options = {
286 287 288 289 290 291
                ajax: {
                    url: '/users',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
lepdou committed
292
                            keyword: params.term ? params.term : '',
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
302 303 304 305 306 307 308 309 310 311 312 313
                                       })
                        });
                        return {
                            results: users
                        }

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

lepdou committed
314
            function initSelect2() {
315
                $('.' + scope.id).select2(select2Options);
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
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);
            }
        }
    }
});

372