summaryrefslogtreecommitdiffstats
path: root/js/app/Config.js
blob: 415fd9d3bc5c15fefe6108c8c8aa190dd713badd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Bernhard Posselt 2014
 */
app.config(function ($routeProvider, $provide, $httpProvider) {
    'use strict';

    var feedType = {
        FEED: 0,
        FOLDER: 1,
        STARRED: 2,
        SUBSCRIPTIONS: 3,
        SHARED: 4,
        EXPLORE: 5
    };

    // constants
    $provide.constant('REFRESH_RATE', 60);  // seconds
    $provide.constant('ITEM_BATCH_SIZE', 40);  // how many items should be
                                               // fetched initially
    $provide.constant('ITEM_AUTO_PAGE_SIZE', 20);
    $provide.constant('BASE_URL', OC.generateUrl('/apps/news'));
    $provide.constant('FEED_TYPE', feedType);
    $provide.constant('MARK_READ_TIMEOUT', 0.5);
    $provide.constant('SCROLL_TIMEOUT', 0.1);

    // make sure that the CSRF header is only sent to the ownCloud domain
    $provide.factory('CSRFInterceptor', function ($q, BASE_URL, $window) {
        return {
            request: function (config) {
                var domain =
                    $window.location.href.split($window.location.pathname)[0];
                if (config.url.indexOf(BASE_URL) === 0 ||
                    config.url.indexOf(domain) === 0) {
                    config.headers.requesttoken = csrfToken;
                }

                return config || $q.when(config);
            }
        };
    });
    $httpProvider.interceptors.push('CSRFInterceptor');

    // routing
    var getItemResolve = function (type) {
        return {
            // request to items also returns feeds
            data: /* @ngInject */ function (
            $http, $route, $q, BASE_URL, ITEM_BATCH_SIZE, SettingsResource) {

                var showAll = SettingsResource.get('showAll');
                var oldestFirst = SettingsResource.get('oldestFirst');

                var deferred = $q.defer();

                // if those two values are null it means we did not receive
                // the settings request from the server so dont query the server
                if (showAll === null || oldestFirst === null) {
                    deferred.resolve({});
                } else {
                    var parameters = {
                        type: type,
                        limit: ITEM_BATCH_SIZE,
                        showAll: showAll,
                        oldestFirst: oldestFirst
                    };

                    if ($route.current.params.id !== undefined) {
                        parameters.id = $route.current.params.id;
                    }


                    $http({
                        url:  BASE_URL + '/items',
                        method: 'GET',
                        params: parameters
                    }).success(function (data) {
                        deferred.resolve(data);
                    });
                }

                return deferred.promise;
            }
        };
    };

    var getExploreResolve = function () {
        return {
            sites: /* @ngInject */ function (
            $http, $q, BASE_URL, Publisher, SettingsResource) {
                var deferred = $q.defer();

                $http.get(BASE_URL + '/settings').then(function (data) {
                    Publisher.publishAll(data);

                    var url = SettingsResource.get('exploreUrl');
                    var language = SettingsResource.get('language');
                    return $http({
                        url: url,
                        method: 'GET',
                        params: {
                            lang: language
                        }
                    });

                }).then(function (data) {
                    deferred.resolve(data.data);
                }).catch(function () {
                    deferred.reject();
                });

                return deferred.promise;
            }
        };
    };

    $routeProvider
        .when('/items', {
            controller: 'ContentController as Content',
            templateUrl: 'content.html',
            resolve: getItemResolve(feedType.SUBSCRIPTIONS),
            type: feedType.SUBSCRIPTIONS
        })
        .when('/items/starred', {
            controller: 'ContentController as Content',
            templateUrl: 'content.html',
            resolve: getItemResolve(feedType.STARRED),
            type: feedType.STARRED
        })
        .when('/items/feeds/:id', {
            controller: 'ContentController as Content',
            templateUrl: 'content.html',
            resolve: getItemResolve(feedType.FEED),
            type: feedType.FEED
        })
        .when('/items/folders/:id', {
            controller: 'ContentController as Content',
            templateUrl: 'content.html',
            resolve: getItemResolve(feedType.FOLDER),
            type: feedType.FOLDER
        }).when('/explore', {
            controller: 'ExploreController as Explore',
            templateUrl: 'explore.html',
            resolve: getExploreResolve(),
            type: feedType.EXPLORE
        }).when('/shortcuts', {
            templateUrl: 'shortcuts.html',
            type: -1
        });

});