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

            scope.sourceApps = [];
            scope.copyedApps = [];
lepdou committed
12

lepdou committed
13 14 15 16
            AppService.find_all_app().then(function (result) {
                result.forEach(function (app) {
                    app.selected = false;
                    scope.sourceApps.push(app);
lepdou committed
17
                });
lepdou committed
18
                scope.copyedApps = angular.copy(scope.sourceApps);
lepdou committed
19
            }, function (result) {
lepdou committed
20
                toastr.error(AppUtil.errorMsg(result), "load apps error");
lepdou committed
21 22 23 24 25 26 27 28 29 30 31
            });

            scope.searchKey = '';
            scope.shouldShowAppList = false;
            var selectedApp = {};
            scope.selectApp = function (app) {
                select(app);
            };

            scope.changeSearchKey = function () {
                scope.copyedApps = [];
lepdou committed
32 33 34 35 36
                scope.sourceApps.forEach(function (app) {
                    if (app.name.indexOf(scope.searchKey) > -1 || app.appId.indexOf(scope.searchKey) > -1) {
                        scope.copyedApps.push(app);
                    }
                });
lepdou committed
37 38
                scope.shouldShowAppList = true;
            };
lepdou committed
39

lepdou committed
40
            scope.jumpToConfigPage = function () {
lepdou committed
41
                if (selectedApp.appId) {
lepdou committed
42
                    var needReloadPage = false;
lepdou committed
43
                    if ($window.location.href.indexOf("config.html") > -1) {
lepdou committed
44 45 46 47
                        needReloadPage = true;
                    }
                    $window.location.href = '/config.html?#appid=' + selectedApp.appId;

lepdou committed
48
                    if (needReloadPage) {
lepdou committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
                        $window.location.reload();
                    }
                }
            };

            //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) {
lepdou committed
69
                    if (scope.shouldShowAppList && selectedAppIdx > -1) {
lepdou committed
70 71
                        select(scope.copyedApps[selectedAppIdx]);
                        event.preventDefault();
lepdou committed
72
                    } else {
lepdou committed
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
                        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;

            }
lepdou committed
108 109

            UserService.load_user().then(function (result) {
lepdou committed
110
                scope.userName = result.userId;
lepdou committed
111 112 113
            }, function (result) {

            });
lepdou committed
114 115 116 117
        }
    }

});
118 119 120 121 122 123 124 125 126 127

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

138 139
            refreshClusterList();

lepdou committed
140
            ////// load env //////
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            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;
                            cluster.checked = scope.defaultAllChecked ||
                                              (cluster.env == scope.defaultCheckedEnv && cluster.name
                                                                                         == scope.defaultCheckedCluster);
                            scope.clusters.push(cluster);
                        })
                    });
                    scope.select(collectSelectedClusters());
                }, function (result) {
                    toastr.error(AppUtil.errorMsg(result), "加载环境信息出错");
                });
            }


            scope.envAllSelected = scope.defaultAllChecked;
163 164 165 166 167 168 169 170 171

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

lepdou committed
172
            scope.switchSelect = function (o, $event) {
173
                o.checked = !o.checked;
lepdou committed
174 175 176 177 178 179
                $event.stopPropagation();
                scope.select(collectSelectedClusters());
            };

            scope.toggleClusterCheckedStatus = function (cluster) {
                cluster.checked = !cluster.checked;
180 181 182 183 184 185
                scope.select(collectSelectedClusters());
            };

            function collectSelectedClusters() {
                var selectedClusters = [];
                scope.clusters.forEach(function (cluster) {
lepdou committed
186
                    if (cluster.checked) {
187 188 189 190 191 192
                        cluster.clusterName = cluster.name;
                        selectedClusters.push(cluster);
                    }
                });
                return selectedClusters;
            }
lepdou committed
193

194 195 196 197
        }
    }

});
lepdou committed
198

lepdou committed
199
/** 必填项*/
lepdou committed
200 201 202 203 204 205 206
directive_module.directive('apollorequiredfiled', function ($compile, $window) {
    return {
        restrict: 'E',
        template: '<span style="color: red">*</span>',
        transclude: true,
        replace: true
    }
lepdou committed
207
});
lepdou committed
208

lepdou committed
209
/**  确认框 */
lepdou committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
directive_module.directive('apolloconfirmdialog', function ($compile, $window) {
    return {
        restrict: 'E',
        templateUrl: '../views/component/confirm-dialog.html',
        transclude: true,
        replace: true,
        scope: {
            dialogId: '=apolloDialogId',
            title: '=apolloTitle',
            detail: '=apolloDetail',
            showCancelBtn: '=apolloShowCancelBtn',
            doConfirm: '=apolloConfirm'
        },
        link: function (scope, element, attrs) {
            
            scope.confirm = function () {
                if (scope.doConfirm){
                    scope.doConfirm();
                }
            }
            
        }
    }
lepdou committed
233
});
lepdou committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

/** entrance */
directive_module.directive('apolloentrance', function ($compile, $window) {
    return {
        restrict: 'E',
        templateUrl: '../views/component/entrance.html',
        transclude: true,
        replace: true,
        scope: {
            imgSrc: '=apolloImgSrc',
            title: '=apolloTitle',
            href: '=apolloHref'
        },
        link: function (scope, element, attrs) {
        }
    }
});