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

5
function IndexController($scope, $window, toastr, AppUtil, AppService, UserService, FavoriteService) {
lepdou committed
6

7
    $scope.userId = '';
lepdou committed
8

9 10
    $scope.getUserCreatedApps = getUserCreatedApps;
    $scope.getUserFavorites = getUserFavorites;
lepdou committed
11

12 13 14 15 16
    $scope.goToAppHomePage = goToAppHomePage;
    $scope.goToCreateAppPage = goToCreateAppPage;
    $scope.toggleOperationBtn = toggleOperationBtn;
    $scope.toTop = toTop;
    $scope.deleteFavorite = deleteFavorite;
lepdou committed
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
    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 = [];

        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;
lepdou committed
45
                }
46 47
                result.forEach(function (app) {
                    $scope.createdApps.push(app);
lepdou committed
48 49
                });

50 51 52 53 54 55 56 57 58 59
            })
    }

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

lepdou committed
60 61 62 63
                if ($scope.favoritesPage == 1){
                    $("#app-list").removeClass("hidden");
                }

64 65 66 67 68 69
                if (!result || result.length == 0) {
                    return;
                }
                var appIds = [];
                result.forEach(function (favorite) {
                    appIds.push(favorite.appId);
lepdou committed
70
                });
71

lepdou committed
72

73 74 75 76 77 78 79 80 81
                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];
82 83 84
                            if (!app){
                                return;
                            }
85 86 87
                            app.favoriteId = favorite.id;
                            $scope.favorites.push(app);
                        });
lepdou committed
88

89
                    });
90 91 92 93 94 95 96 97 98 99 100 101 102 103
            })
    }

    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) {
104 105
                    //sort
                    var appIdMapApp = {};
106
                    apps.forEach(function (app) {
107 108 109 110 111
                        appIdMapApp[app.appId] = app;
                    });

                    userVisitedApps.forEach(function (appId) {
                        var app = appIdMapApp[appId];
112 113 114
                        if (app){
                            $scope.visitedApps.push(app);
                        }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                    });
                });
        }

    }

    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 () {
            toastr.success("置顶成功");
136
            refreshFavorites();
137 138 139 140 141 142 143

        })
    }

    function deleteFavorite(favoriteId) {
        FavoriteService.deleteFavorite(favoriteId).then(function () {
            toastr.success("取消收藏成功");
144
            refreshFavorites();
145 146 147
        })
    }

148 149 150 151
    function refreshFavorites() {
        $scope.favoritesPage = 0;
        $scope.favorites = [];
        $scope.hasMoreFavorites = true;
152

153
        getUserFavorites();
154
    }
lepdou committed
155

156
}