IndexController.js 2.87 KB
Newer Older
lepdou committed
1 2
index_module.controller('IndexController', ['$scope', '$window', 'toastr', 'AppService', 'AppUtil', 'EnvService',
        function ($scope, $window, toastr, AppService, AppUtil, EnvService) {
lepdou committed
3

4 5 6 7 8 9 10
            $scope.envs = [];
            $scope.selectedEnv = '';
            EnvService.find_all_envs().then(function (result) {
                $scope.envs = result;
                //default select first env
                $scope.switchEnv($scope.envs[0]);
            }, function (result) {
lepdou committed
11
                    toastr.error(AppUtil.errorMsg(result), "load env error");
12
            });
lepdou committed
13

lepdou committed
14 15


16 17 18 19
            $scope.switchEnv = function (env) {
                $scope.selectedEnv = env;
                loadApps(env);
            };
lepdou committed
20 21 22

            var sourceApps = [];

23 24
            function loadApps(env){
                AppService.find_all_app(env).then(function (result) {
lepdou committed
25 26 27
                    sourceApps = sortApps(result);
                    $scope.apps = sourceApps;
                    $scope.appsCount = sourceApps.length;
28 29
                    $scope.selectedEnv = env;
                }, function (result) {
lepdou committed
30 31 32 33 34 35 36 37 38
                    toastr.error(AppUtil.errorMsg(result), "load apps error");
                });
            }

            var VISITED_APPS_STORAGE_KEY = "VisitedApps";
            //访问过的App放在列表最前面,方便用户选择
            function sortApps(sourceApps) {
                var visitedApps = JSON.parse(localStorage.getItem(VISITED_APPS_STORAGE_KEY));
                if (!visitedApps){
39
                    return sourceApps;
lepdou committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
                }
                var existedVisitedAppsMap = {};
                visitedApps.forEach(function (app) {
                    existedVisitedAppsMap[app] = true;
                });

                var sortedApps = [];
                sourceApps.forEach(function (app) {
                    if (existedVisitedAppsMap[app.appId]){
                        sortedApps.push(app);
                    }
                });
                sourceApps.forEach(function (app) {
                    if (!existedVisitedAppsMap[app.appId]){
                        sortedApps.push(app);
                    }
                });
                return sortedApps;
lepdou committed
58
            }
lepdou committed
59

60
            $scope.search = function () {
lepdou committed
61
                    var key = $scope.searchKey.toLocaleLowerCase();
62
                    if (key == '') {
lepdou committed
63
                            $scope.apps = sourceApps;
64 65 66
                            return;
                    }
                    var result = [];
lepdou committed
67
                    sourceApps.forEach(function (item) {
lepdou committed
68 69
                            if (item.appId.toLocaleLowerCase().indexOf(key) >= 0 ||
                                item.name.toLocaleLowerCase().indexOf(key) >= 0) {
70 71 72 73 74 75
                                    result.push(item);
                            }
                    });

                    $scope.apps = result;
            };
lepdou committed
76 77

        }]);