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

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

lepdou committed
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
                                       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) {

                                       });
                                   }
                               }

                           });
124 125 126 127 128

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

lepdou committed
143 144
            scope.$watch("defaultCheckedEnv", refreshClusterList);
            scope.$watch("defaultCheckedCluster", refreshClusterList);
145

146 147 148 149 150 151 152 153 154 155
            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
156
                            //default checked
157 158 159
                            cluster.checked = scope.defaultAllChecked ||
                                              (cluster.env == scope.defaultCheckedEnv && cluster.name
                                                                                         == scope.defaultCheckedCluster);
lepdou committed
160
                            //not checked
lepdou committed
161
                            if (cluster.env == scope.notCheckedEnv && cluster.name == scope.notCheckedCluster) {
lepdou committed
162 163 164
                                cluster.checked = false;
                            }

165 166 167 168 169 170 171 172
                            scope.clusters.push(cluster);
                        })
                    });
                    scope.select(collectSelectedClusters());
                });
            }

            scope.envAllSelected = scope.defaultAllChecked;
173 174 175 176 177 178 179 180 181

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

lepdou committed
182
            scope.switchSelect = function (o, $event) {
183
                o.checked = !o.checked;
lepdou committed
184 185 186 187 188 189
                $event.stopPropagation();
                scope.select(collectSelectedClusters());
            };

            scope.toggleClusterCheckedStatus = function (cluster) {
                cluster.checked = !cluster.checked;
190 191 192 193 194 195
                scope.select(collectSelectedClusters());
            };

            function collectSelectedClusters() {
                var selectedClusters = [];
                scope.clusters.forEach(function (cluster) {
lepdou committed
196
                    if (cluster.checked) {
197 198 199 200 201 202
                        cluster.clusterName = cluster.name;
                        selectedClusters.push(cluster);
                    }
                });
                return selectedClusters;
            }
lepdou committed
203

204 205 206 207
        }
    }

});
lepdou committed
208

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

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

lepdou committed
237 238 239 240 241 242 243 244
            scope.$watch("detail", function () {
                scope.detailAsHtml = $sce.trustAsHtml(scope.detail);
            });

            if (!scope.confirmBtnText) {
                scope.confirmBtnText = '确认';
            }
            
lepdou committed
245
            scope.confirm = function () {
lepdou committed
246
                if (scope.doConfirm) {
lepdou committed
247 248
                    scope.doConfirm();
                }
lepdou committed
249 250 251
            };
            

lepdou committed
252

lepdou committed
253 254
        }
    }
lepdou committed
255
});
lepdou committed
256 257 258 259 260

/** entrance */
directive_module.directive('apolloentrance', function ($compile, $window) {
    return {
        restrict: 'E',
lepdou committed
261
        templateUrl: '../../views/component/entrance.html',
lepdou committed
262 263 264 265 266 267 268 269 270 271 272 273
        transclude: true,
        replace: true,
        scope: {
            imgSrc: '=apolloImgSrc',
            title: '=apolloTitle',
            href: '=apolloHref'
        },
        link: function (scope, element, attrs) {
        }
    }
});

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

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

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

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

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

376