Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
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/. */
6 "use strict";
8 function ItemPinHelper(aUnpinnedPrefName) {
9 this._prefKey = aUnpinnedPrefName;
10 }
12 // Cache preferences on a static variable shared
13 // by all instances registered to the same pref key.
14 ItemPinHelper._prefValue = {};
16 ItemPinHelper.prototype = {
17 _getPrefValue: function _getPrefValue() {
18 if (ItemPinHelper._prefValue[this._prefKey])
19 return ItemPinHelper._prefValue[this._prefKey];
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 }
29 return ItemPinHelper._prefValue[this._prefKey];
30 },
32 _setPrefValue: function _setPrefValue(aNewValue) {
33 let stringified = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
34 stringified.data = JSON.stringify(aNewValue);
36 Services.prefs.setComplexValue(this._prefKey, Ci.nsISupportsString, stringified);
37 ItemPinHelper._prefValue[this._prefKey] = aNewValue;
38 },
40 isPinned: function isPinned(aItemId) {
41 // Bookmarks are visible on StartUI (pinned) by default
42 return this._getPrefValue().indexOf(aItemId) === -1;
43 },
45 setUnpinned: function setPinned(aItemId) {
46 let unpinned = this._getPrefValue();
47 unpinned.push(aItemId);
48 this._setPrefValue(unpinned);
49 },
51 setPinned: function unsetPinned(aItemId) {
52 let unpinned = this._getPrefValue();
54 let index = unpinned.indexOf(aItemId);
55 unpinned.splice(index, 1);
57 this._setPrefValue(unpinned);
58 },
59 }