summaryrefslogtreecommitdiffstats
path: root/js/gui/ExternSubscription.js
blob: 84fa650db8666a3521300a518967b797c2850285 (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
/**
 * 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
 */

/**
 * This prefills the add feed section if an external link has ?subsribe_to
 * filled out
 */
(function (window, document, navigator, url, $, undefined) {
    'use strict';

    // register reader as feed reader in firefox
    var location = window.location;
    var storage = window.localStorage;

    // if isContentHandlerRegistered is not implemented (Firefox I'm looking
    // at you) we use localstorage to prevent registering the feed reader twice
    var registerHandler = function (mime, url, title) {
        var registered = navigator.isContentHandlerRegistered;

        var isRegistered = function (mime, url) {
            if (registered) {
                return registered(mime, url) !== 'new';
            } else {
                return storage.getItem('registeredHandler') === url;
            }
        };

        if (navigator.registerContentHandler && !isRegistered(mime, url)) {
            navigator.registerContentHandler(mime, url, title);
            if (!registered) {
                storage.setItem('registeredHandler', url);
            }
        }
    };

    var cleanUrl = location.protocol + '//' + location.host + location.pathname;

    var subscribeUrl = cleanUrl + '?subscribe_to=%s';
    var mimeType = 'application/vnd.mozilla.maybe.feed';
    var title = 'ownCloud News @ ' + cleanUrl;

    registerHandler(mimeType, subscribeUrl, title);


    $(document).ready(function () {
        var subscription = url('?subscribe_to');
        if (subscription && subscription !== 'undefined') {
            $('#new-feed').show();

            var input = $('input[ng-model="Navigation.feed.url"]');
            input.val(subscription);
            input.trigger('input');

            // hacky way to focus because initial loading of a feed
            // steals the focus
            setTimeout(function() {
                input.focus();
            }, 1000);
        }
    });

})(window, document, navigator, url, $);