summaryrefslogtreecommitdiff
path: root/web/javascript/feed.js
blob: 1f8c40f0d0a73b4fd6b15055572ba80a33d79dfa (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
function Feed(feedListParent, obj) {
    this.initialize(feedListParent, obj);
}

Feed.prototype =
{
	/*--------------------------------------------
	 *
	 *  C O N S T R U C T O R
	 *
	 *--------------------------------------------*/
	initialize: function(feedListParent, obj) {
	    this.matches = obj.matches;
	    this.url     = obj.url;
	    this.id      = obj.id;

        var curThis = this;
	    var top_e = document.createElement('li');
	    var feed_div = document.createElement('div');
	    feed_div.appendChild(document.createTextNode(this.url));
	    var create_new = document.createElement('button');
	    create_new.appendChild(document.createTextNode("Add Matcher"));
	    create_new.onclick = function() {
	        curThis.showAddMatcher();
	    };
	    feed_div.appendChild(create_new);
	    top_e.appendChild(feed_div);
	    var sub_ul = document.createElement("ul");
	    feed_div.onclick = function() {
	        $(sub_ul).toggle(300);
	    }
	    this._sub_ul = sub_ul;
	    $(this.matches).each(function(index, ele) {
	        curThis.addMatcherUI(ele);
	    });
	    top_e.appendChild(sub_ul);
	    feedListParent.appendChild(top_e);
	},
	
	addMatcherUI: function(obj) {
        var li = document.createElement('li');
        var del = document.createElement('button');
        del.appendChild(document.createTextNode("Delete"));
        var curThis = this;
        del.onclick = function() {
	        sendMessage('feed-set', {matchRemove: [obj.id], ids: [curThis.id]},
	            function(arguments) {
                    curThis.refresh();
	            });
        };
        li.appendChild(del);
        li.appendChild(document.createTextNode(obj.value));
        this._sub_ul.appendChild(li);
        obj.li = li;
	},

	showAddMatcher: function() {
	    this._add_matcher = document.createElement('li');
        var input = document.createElement('input');
        input.type = "text";
        input.value = ".*match some text via a Regular Expression.*";
	    this._add_matcher.appendChild(input);
	    var button = document.createElement('button');
	    button.appendChild(document.createTextNode("Add"));
	    var curThis = this;
	    button.onclick = function() {
	        $(button).attr("disabled", "disabled");
	        sendMessage('feed-set', {matchAdd: [{white: true, value: input.value}], ids: [curThis.id]}, function(arguments) {
                curThis.hideAddMatcher();
                curThis.refresh();
	        });
	    }
	    this._add_matcher.appendChild(button);
	    this._sub_ul.appendChild(this._add_matcher);
	},
	
	hideAddMatcher: function() {
	    if (!this._add_matcher)
	        return;
        $(this._add_matcher).remove();
        this._add_matcher = undefined;
	},

	refresh: function() {
	    var curThis = this;
	    sendMessage('feed-get', {fields: ['matches'], ids: [this.id]}, function(arguments) {
	        if (arguments.feeds.length < 1)
	            return
            $.each(arguments.feeds[0].matches, function(index, ele) {
                for (var i = 0; i < curThis.matches.length; i++) {
                    if (curThis.matches[i].id === ele.id)
                        return;
                }
                curThis.matches[curThis.matches.length] = ele;
                curThis.addMatcherUI(ele);
            });
            var toDelete = [];
            var args = arguments;
            $.each(curThis.matches, function(index, ele) {
                for (var i = 0; i < args.feeds[0].matches.length; i++) {
                    if (args.feeds[0].matches[i].id === ele.id)
                        return;
                }
                toDelete[toDelete.length] = index;
            });
            $.each(toDelete, function(index, ele) {
                $(curThis.matches[ele].li).remove();
                delete curThis.matches[ele];
            });
	    });
	}
};