IndexController.js 5.3 KB
Newer Older
1 2 3
index_module.controller('IndexController', ['$scope', '$window', 'toastr', 'AppUtil', 'AppService',
                                            'UserService', 'FavoriteService',
                                            IndexController]);
L
lepdou 已提交
4

5
function IndexController($scope, $window, toastr, AppUtil, AppService, UserService, FavoriteService) {
L
lepdou 已提交
6

7
    $scope.userId = '';
L
lepdou 已提交
8

9 10
    $scope.getUserCreatedApps = getUserCreatedApps;
    $scope.getUserFavorites = getUserFavorites;
L
lepdou 已提交
11

12 13 14 15 16
    $scope.goToAppHomePage = goToAppHomePage;
    $scope.goToCreateAppPage = goToCreateAppPage;
    $scope.toggleOperationBtn = toggleOperationBtn;
    $scope.toTop = toTop;
    $scope.deleteFavorite = deleteFavorite;
L
lepdou 已提交
17

18
    function  initCreateApplicationPermission() {
19 20 21 22 23
        AppService.has_create_application_role($scope.userId).then(
            function (value) {
                $scope.hasCreateApplicationPermission = value.hasCreateApplicationPermission;
            },
            function (reason) {
24
                toastr.warning(AppUtil.errorMsg(reason), "获取创建应用权限信息失败");
25 26 27 28
            }
        )
    }

29 30 31 32 33 34 35 36 37 38 39
    UserService.load_user().then(function (result) {
        $scope.userId = result.userId;

        $scope.createdAppPage = 0;
        $scope.createdApps = [];
        $scope.hasMoreCreatedApps = true;
        $scope.favoritesPage = 0;
        $scope.favorites = [];
        $scope.hasMoreFavorites = true;
        $scope.visitedApps = [];

40 41
        initCreateApplicationPermission();

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        getUserCreatedApps();

        getUserFavorites();

        initUserVisitedApps();
    });

    function getUserCreatedApps() {
        var size = 10;
        AppService.find_app_by_owner($scope.userId, $scope.createdAppPage, size)
            .then(function (result) {
                $scope.createdAppPage += 1;
                $scope.hasMoreCreatedApps = result.length == size;

                if (!result || result.length == 0) {
                    return;
L
lepdou 已提交
58
                }
59 60
                result.forEach(function (app) {
                    $scope.createdApps.push(app);
L
lepdou 已提交
61 62
                });

63 64 65 66 67 68 69 70 71 72
            })
    }

    function getUserFavorites() {
        var size = 11;
        FavoriteService.findFavorites($scope.userId, '', $scope.favoritesPage, size)
            .then(function (result) {
                $scope.favoritesPage += 1;
                $scope.hasMoreFavorites = result.length == size;

73
                if ($scope.favoritesPage == 1){
L
abtest  
lepdou 已提交
74 75 76
                    $("#app-list").removeClass("hidden");
                }

77 78 79 80 81 82
                if (!result || result.length == 0) {
                    return;
                }
                var appIds = [];
                result.forEach(function (favorite) {
                    appIds.push(favorite.appId);
L
lepdou 已提交
83
                });
84

L
abtest  
lepdou 已提交
85

86 87 88 89 90 91 92 93 94
                AppService.find_apps(appIds.join(","))
                    .then(function (apps) {
                        //sort
                        var appIdMapApp = {};
                        apps.forEach(function (app) {
                            appIdMapApp[app.appId] = app;
                        });
                        result.forEach(function (favorite) {
                            var app = appIdMapApp[favorite.appId];
95
                            if (!app){
J
Jason Song 已提交
96 97
                                return;
                            }
98 99 100
                            app.favoriteId = favorite.id;
                            $scope.favorites.push(app);
                        });
L
abtest  
lepdou 已提交
101

102
                    });
103 104 105 106 107 108 109 110 111 112 113 114 115 116
            })
    }

    function initUserVisitedApps() {
        var VISITED_APPS_STORAGE_KEY = "VisitedAppsV2";
        var visitedAppsObject = JSON.parse(localStorage.getItem(VISITED_APPS_STORAGE_KEY));
        if (!visitedAppsObject) {
            visitedAppsObject = {};
        }

        var userVisitedApps = visitedAppsObject[$scope.userId];
        if (userVisitedApps && userVisitedApps.length > 0) {
            AppService.find_apps(userVisitedApps.join(","))
                .then(function (apps) {
117 118
                    //sort
                    var appIdMapApp = {};
119
                    apps.forEach(function (app) {
120 121 122 123 124
                        appIdMapApp[app.appId] = app;
                    });

                    userVisitedApps.forEach(function (appId) {
                        var app = appIdMapApp[appId];
125
                        if (app){
126 127
                            $scope.visitedApps.push(app);
                        }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                    });
                });
        }

    }

    function goToCreateAppPage() {
        $window.location.href = "/app.html";
    }

    function goToAppHomePage(appId) {
        $window.location.href = "/config.html?#/appid=" + appId;
    }

    function toggleOperationBtn(app) {
        app.showOperationBtn = !app.showOperationBtn;
    }

    function toTop(favoriteId) {
        FavoriteService.toTop(favoriteId).then(function () {
148
            toastr.success("置顶成功");
149
            refreshFavorites();
150 151 152 153 154 155

        })
    }

    function deleteFavorite(favoriteId) {
        FavoriteService.deleteFavorite(favoriteId).then(function () {
156
            toastr.success("取消收藏成功");
157
            refreshFavorites();
158 159 160
        })
    }

161 162 163 164
    function refreshFavorites() {
        $scope.favoritesPage = 0;
        $scope.favorites = [];
        $scope.hasMoreFavorites = true;
165

166
        getUserFavorites();
167
    }
L
lepdou 已提交
168

169
}