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]; }); }); } };