browser/metro/base/content/helperui/ItemPinHelper.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:5d90b79010cd
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 "use strict";
7
8 function ItemPinHelper(aUnpinnedPrefName) {
9 this._prefKey = aUnpinnedPrefName;
10 }
11
12 // Cache preferences on a static variable shared
13 // by all instances registered to the same pref key.
14 ItemPinHelper._prefValue = {};
15
16 ItemPinHelper.prototype = {
17 _getPrefValue: function _getPrefValue() {
18 if (ItemPinHelper._prefValue[this._prefKey])
19 return ItemPinHelper._prefValue[this._prefKey];
20
21 try {
22 // getComplexValue throws if pref never set. Really.
23 let prefValue = Services.prefs.getComplexValue(this._prefKey, Ci.nsISupportsString);
24 ItemPinHelper._prefValue[this._prefKey] = JSON.parse(prefValue.data);
25 } catch(e) {
26 ItemPinHelper._prefValue[this._prefKey] = [];
27 }
28
29 return ItemPinHelper._prefValue[this._prefKey];
30 },
31
32 _setPrefValue: function _setPrefValue(aNewValue) {
33 let stringified = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
34 stringified.data = JSON.stringify(aNewValue);
35
36 Services.prefs.setComplexValue(this._prefKey, Ci.nsISupportsString, stringified);
37 ItemPinHelper._prefValue[this._prefKey] = aNewValue;
38 },
39
40 isPinned: function isPinned(aItemId) {
41 // Bookmarks are visible on StartUI (pinned) by default
42 return this._getPrefValue().indexOf(aItemId) === -1;
43 },
44
45 setUnpinned: function setPinned(aItemId) {
46 let unpinned = this._getPrefValue();
47 unpinned.push(aItemId);
48 this._setPrefValue(unpinned);
49 },
50
51 setPinned: function unsetPinned(aItemId) {
52 let unpinned = this._getPrefValue();
53
54 let index = unpinned.indexOf(aItemId);
55 unpinned.splice(index, 1);
56
57 this._setPrefValue(unpinned);
58 },
59 }

mercurial