~wasta-linux/wasta-core-wily/master

« back to all changes in this revision

Viewing changes to install-files/firefox-addons/extensions/{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}/chrome/content/ui/filters-subscriptionview.js

  • Committer: Rik Shaw
  • Date: 2015-11-01 13:28:40 UTC
  • Revision ID: git-v1:59c62c9b2e4f4f1cf62db1f5dc1cf630feb99933
initial 15.10 commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of Adblock Plus <https://adblockplus.org/>,
 
3
 * Copyright (C) 2006-2015 Eyeo GmbH
 
4
 *
 
5
 * Adblock Plus is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License version 3 as
 
7
 * published by the Free Software Foundation.
 
8
 *
 
9
 * Adblock Plus is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
/**
 
19
 * Fills a list of filter groups and keeps it updated.
 
20
 * @param {Element} list  richlistbox element to be filled
 
21
 * @param {Node} template  template to use for the groups
 
22
 * @param {Function} filter  filter to decide which lists should be included
 
23
 * @param {Function} listener  function to be called on changes
 
24
 * @constructor
 
25
 */
 
26
function ListManager(list, template, filter, listener)
 
27
{
 
28
  this._list = list;
 
29
  this._template = template;
 
30
  this._filter = filter;
 
31
  this._listener = listener || function(){};
 
32
 
 
33
  this._deck = this._list.parentNode;
 
34
 
 
35
  this._list.listManager = this;
 
36
  this.reload();
 
37
 
 
38
  let me = this;
 
39
  let proxy = function()
 
40
  {
 
41
    return me._onChange.apply(me, arguments);
 
42
  };
 
43
  FilterNotifier.addListener(proxy);
 
44
  window.addEventListener("unload", function()
 
45
  {
 
46
    FilterNotifier.removeListener(proxy);
 
47
  }, false);
 
48
}
 
49
ListManager.prototype =
 
50
{
 
51
  /**
 
52
   * List element being managed.
 
53
   * @type Element
 
54
   */
 
55
  _list: null,
 
56
  /**
 
57
   * Template used for the groups.
 
58
   * @type Node
 
59
   */
 
60
  _template: null,
 
61
  /**
 
62
   * Filter function to decide which subscriptions should be included.
 
63
   * @type Function
 
64
   */
 
65
  _filter: null,
 
66
  /**
 
67
   * Function to be called whenever list contents change.
 
68
   * @type Function
 
69
   */
 
70
  _listener: null,
 
71
  /**
 
72
   * Deck switching between list display and "no entries" message.
 
73
   * @type Element
 
74
   */
 
75
  _deck: null,
 
76
 
 
77
  /**
 
78
   * Completely rebuilds the list.
 
79
   */
 
80
  reload: function()
 
81
  {
 
82
    // Remove existing entries if any
 
83
    while (this._list.firstChild)
 
84
      this._list.removeChild(this._list.firstChild);
 
85
 
 
86
    // Now add all subscriptions
 
87
    let subscriptions = FilterStorage.subscriptions.filter(this._filter, this);
 
88
    if (subscriptions.length)
 
89
    {
 
90
      for (let subscription of subscriptions)
 
91
        this.addSubscription(subscription, null);
 
92
 
 
93
      // Make sure first list item is selected after list initialization
 
94
      Utils.runAsync(() => this._list.selectItem(this._list.getItemAtIndex(this._list.getIndexOfFirstVisibleRow())));
 
95
    }
 
96
 
 
97
    this._deck.selectedIndex = (subscriptions.length ? 1 : 0);
 
98
    this._listener();
 
99
  },
 
100
 
 
101
  /**
 
102
   * Adds a filter subscription to the list.
 
103
   */
 
104
  addSubscription: function(/**Subscription*/ subscription, /**Node*/ insertBefore) /**Node*/
 
105
  {
 
106
    let disabledFilters = 0;
 
107
    for (let i = 0, l = subscription.filters.length; i < l; i++)
 
108
      if (subscription.filters[i] instanceof ActiveFilter && subscription.filters[i].disabled)
 
109
        disabledFilters++;
 
110
 
 
111
    let node = Templater.process(this._template, {
 
112
      __proto__: null,
 
113
      subscription: subscription,
 
114
      isExternal: subscription instanceof ExternalSubscription,
 
115
      downloading: Synchronizer.isExecuting(subscription.url),
 
116
      disabledFilters: disabledFilters
 
117
    });
 
118
    if (insertBefore)
 
119
      this._list.insertBefore(node, insertBefore);
 
120
    else
 
121
      this._list.appendChild(node);
 
122
    return node;
 
123
  },
 
124
 
 
125
  /**
 
126
   * Map indicating subscriptions that need their "disabledFilters" property to
 
127
   * be updated by next updateDisabled() call.
 
128
   * @type Object
 
129
   */
 
130
  _scheduledUpdateDisabled: null,
 
131
 
 
132
  /**
 
133
   * Updates subscriptions that had some of their filters enabled/disabled.
 
134
   */
 
135
  updateDisabled: function()
 
136
  {
 
137
    let list = this._scheduledUpdateDisabled;
 
138
    this._scheduledUpdateDisabled = null;
 
139
    for (let url in list)
 
140
    {
 
141
      let subscription = Subscription.fromURL(url);
 
142
      let subscriptionNode = Templater.getNodeForData(this._list, "subscription", subscription);
 
143
      if (subscriptionNode)
 
144
      {
 
145
        let data = Templater.getDataForNode(subscriptionNode);
 
146
        let disabledFilters = 0;
 
147
        for (let i = 0, l = subscription.filters.length; i < l; i++)
 
148
          if (subscription.filters[i] instanceof ActiveFilter && subscription.filters[i].disabled)
 
149
            disabledFilters++;
 
150
 
 
151
        if (disabledFilters != data.disabledFilters)
 
152
        {
 
153
          data.disabledFilters = disabledFilters;
 
154
          Templater.update(this._template, subscriptionNode);
 
155
 
 
156
          if (!document.commandDispatcher.focusedElement)
 
157
            this._list.focus();
 
158
        }
 
159
      }
 
160
    }
 
161
  },
 
162
 
 
163
  /**
 
164
   * Subscriptions change processing.
 
165
   * @see FilterNotifier.addListener()
 
166
   */
 
167
  _onChange: function(action, item, param1, param2)
 
168
  {
 
169
    if ((action == "subscription.added" || action == "subscription.removed") && item.url == Prefs.subscriptions_exceptionsurl)
 
170
      E("acceptableAds").checked = FilterStorage.subscriptions.some(s => s.url == Prefs.subscriptions_exceptionsurl);
 
171
 
 
172
    if (action == "filter.disabled")
 
173
    {
 
174
      if (this._scheduledUpdateDisabled == null)
 
175
      {
 
176
        this._scheduledUpdateDisabled = Object.create(null);
 
177
        Utils.runAsync(() => this.updateDisabled());
 
178
      }
 
179
      for (let i = 0; i < item.subscriptions.length; i++)
 
180
        this._scheduledUpdateDisabled[item.subscriptions[i].url] = true;
 
181
      return;
 
182
    }
 
183
 
 
184
    if (action != "load" && !this._filter(item))
 
185
      return;
 
186
 
 
187
    switch (action)
 
188
    {
 
189
      case "load":
 
190
      {
 
191
        this.reload();
 
192
        break;
 
193
      }
 
194
      case "subscription.added":
 
195
      {
 
196
        let index = FilterStorage.subscriptions.indexOf(item);
 
197
        if (index >= 0)
 
198
        {
 
199
          let insertBefore = null;
 
200
          for (index++; index < FilterStorage.subscriptions.length && !insertBefore; index++)
 
201
            insertBefore = Templater.getNodeForData(this._list, "subscription", FilterStorage.subscriptions[index]);
 
202
          this.addSubscription(item, insertBefore);
 
203
          this._deck.selectedIndex = 1;
 
204
          this._listener();
 
205
        }
 
206
        break;
 
207
      }
 
208
      case "subscription.removed":
 
209
      {
 
210
        let node = Templater.getNodeForData(this._list, "subscription", item);
 
211
        if (node)
 
212
        {
 
213
          let newSelection = node.nextSibling || node.previousSibling;
 
214
          node.parentNode.removeChild(node);
 
215
          if (!this._list.firstChild)
 
216
          {
 
217
            this._deck.selectedIndex = 0;
 
218
            this._list.selectedIndex = -1;
 
219
          }
 
220
          else if (newSelection)
 
221
          {
 
222
            this._list.ensureElementIsVisible(newSelection);
 
223
            this._list.selectedItem = newSelection;
 
224
          }
 
225
          this._listener();
 
226
        }
 
227
        break
 
228
      }
 
229
      case "subscription.moved":
 
230
      {
 
231
        let node = Templater.getNodeForData(this._list, "subscription", item);
 
232
        if (node)
 
233
        {
 
234
          node.parentNode.removeChild(node);
 
235
          let insertBefore = null;
 
236
          let index = FilterStorage.subscriptions.indexOf(item);
 
237
          if (index >= 0)
 
238
            for (index++; index < FilterStorage.subscriptions.length && !insertBefore; index++)
 
239
              insertBefore = Templater.getNodeForData(this._list, "subscription", FilterStorage.subscriptions[index]);
 
240
          this._list.insertBefore(node, insertBefore);
 
241
          this._list.ensureElementIsVisible(node);
 
242
          this._listener();
 
243
        }
 
244
        break;
 
245
      }
 
246
      case "subscription.title":
 
247
      case "subscription.disabled":
 
248
      case "subscription.homepage":
 
249
      case "subscription.lastDownload":
 
250
      case "subscription.downloadStatus":
 
251
      {
 
252
        let subscriptionNode = Templater.getNodeForData(this._list, "subscription", item);
 
253
        if (subscriptionNode)
 
254
        {
 
255
          Templater.getDataForNode(subscriptionNode).downloading = Synchronizer.isExecuting(item.url);
 
256
          Templater.update(this._template, subscriptionNode);
 
257
 
 
258
          if (!document.commandDispatcher.focusedElement)
 
259
            this._list.focus();
 
260
          this._listener();
 
261
        }
 
262
        break;
 
263
      }
 
264
      case "subscription.fixedTitle":
 
265
      {
 
266
        SubscriptionActions.updateCommands();
 
267
        break;
 
268
      }
 
269
      case "subscription.updated":
 
270
      {
 
271
        if (this._scheduledUpdateDisabled == null)
 
272
        {
 
273
          this._scheduledUpdateDisabled = Object.create(null);
 
274
          Utils.runAsync(() => this.updateDisabled());
 
275
        }
 
276
        this._scheduledUpdateDisabled[item.url] = true;
 
277
        break;
 
278
      }
 
279
    }
 
280
  }
 
281
};
 
282
 
 
283
/**
 
284
 * Attaches list managers to the lists.
 
285
 */
 
286
ListManager.init = function()
 
287
{
 
288
  new ListManager(E("subscriptions"),
 
289
                  E("subscriptionTemplate"),
 
290
                  s => s instanceof RegularSubscription && !(ListManager.acceptableAdsCheckbox && s.url == Prefs.subscriptions_exceptionsurl),
 
291
                  SubscriptionActions.updateCommands);
 
292
  new ListManager(E("groups"),
 
293
                  E("groupTemplate"),
 
294
                  s => s instanceof SpecialSubscription,
 
295
                  SubscriptionActions.updateCommands);
 
296
  E("acceptableAds").checked = FilterStorage.subscriptions.some(s => s.url == Prefs.subscriptions_exceptionsurl);
 
297
  E("acceptableAds").parentNode.hidden = !ListManager.acceptableAdsCheckbox;
 
298
};
 
299
 
 
300
/**
 
301
 * Defines whether the "acceptable ads" subscription needs special treatment.
 
302
 * @type Boolean
 
303
 */
 
304
ListManager.acceptableAdsCheckbox = Prefs.subscriptions_exceptionscheckbox;
 
305
 
 
306
/**
 
307
 * Adds or removes filter subscription allowing acceptable ads.
 
308
 */
 
309
ListManager.allowAcceptableAds = function(/**Boolean*/ allow)
 
310
{
 
311
  let subscription = Subscription.fromURL(Prefs.subscriptions_exceptionsurl);
 
312
  if (!subscription)
 
313
    return;
 
314
 
 
315
  subscription.disabled = false;
 
316
  subscription.title = "Allow non-intrusive advertising";
 
317
  if (allow)
 
318
  {
 
319
    FilterStorage.addSubscription(subscription);
 
320
    if (subscription instanceof DownloadableSubscription && !subscription.lastDownload)
 
321
      Synchronizer.execute(subscription);
 
322
  }
 
323
  else
 
324
    FilterStorage.removeSubscription(subscription);
 
325
};
 
326
 
 
327
window.addEventListener("load", ListManager.init, false);