summaryrefslogtreecommitdiffstats
path: root/js/app/Config.js
blob: 17b823218794fef702a54cd972172ce22c958336 (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
/**
 * 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';

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

    // constants
    $provide.constant('REFRESH_RATE', 60);  // seconds
    $provide.constant('ITEM_BATCH_SIZE', 50);  // how many items to autopage by
    $provide.constant('BASE_URL', OC.generateUrl('/apps/news'));
    $provide.constant('FEED_TYPE', feedType);

    // make sure that the CSRF header is only sent to the ownCloud domain
    $provide.factory('CSRFInterceptor', ($q, BASE_URL) => {
        return {
            request: (config) => {
                if (config.url.indexOf(BASE_URL) === 0) {
                    config.headers.requesttoken = csrfToken;
                }

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

    // routing
    const getResolve = (type) => {
        return {
            // request to items also returns feeds
            data: [
                '$http',
                '$route',
                '$q',
                'BASE_URL',
                'ITEM_BATCH_SIZE',
                ($http, $route, $q, BASE_URL, ITEM_BATCH_SIZE) => {

                    const parameters = {
                        type: type,
                        limit: ITEM_BATCH_SIZE
                    };

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

                    let deferred = $q.defer();

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

                    return deferred.promise;
                }
            ]
        };
    };

    $routeProvider
        .when('/items', {
            controller: 'ContentController',
            templateUrl: 'content.html',
            resolve: getResolve(feedType.SUBSCRIPTIONS)
        })
        .when('/items/starred', {
            controller: 'ContentController',
            templateUrl: 'content.html',
            resolve: getResolve(feedType.STARRED)
        })
        .when('/items/feeds/:id', {
            controller: 'ContentController',
            templateUrl: 'content.html',
            resolve: getResolve(feedType.FEED)
        })
        .when('/items/folders/:id', {
            controller: 'ContentController',
            templateUrl: 'content.html',
            resolve: getResolve(feedType.FOLDER)
        })
        .otherwise({
            redirectTo: '/items'
        });

});