1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/metro/base/content/helperui/ItemPinHelper.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,59 @@ 1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +"use strict"; 1.10 + 1.11 +function ItemPinHelper(aUnpinnedPrefName) { 1.12 + this._prefKey = aUnpinnedPrefName; 1.13 +} 1.14 + 1.15 +// Cache preferences on a static variable shared 1.16 +// by all instances registered to the same pref key. 1.17 +ItemPinHelper._prefValue = {}; 1.18 + 1.19 +ItemPinHelper.prototype = { 1.20 + _getPrefValue: function _getPrefValue() { 1.21 + if (ItemPinHelper._prefValue[this._prefKey]) 1.22 + return ItemPinHelper._prefValue[this._prefKey]; 1.23 + 1.24 + try { 1.25 + // getComplexValue throws if pref never set. Really. 1.26 + let prefValue = Services.prefs.getComplexValue(this._prefKey, Ci.nsISupportsString); 1.27 + ItemPinHelper._prefValue[this._prefKey] = JSON.parse(prefValue.data); 1.28 + } catch(e) { 1.29 + ItemPinHelper._prefValue[this._prefKey] = []; 1.30 + } 1.31 + 1.32 + return ItemPinHelper._prefValue[this._prefKey]; 1.33 + }, 1.34 + 1.35 + _setPrefValue: function _setPrefValue(aNewValue) { 1.36 + let stringified = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); 1.37 + stringified.data = JSON.stringify(aNewValue); 1.38 + 1.39 + Services.prefs.setComplexValue(this._prefKey, Ci.nsISupportsString, stringified); 1.40 + ItemPinHelper._prefValue[this._prefKey] = aNewValue; 1.41 + }, 1.42 + 1.43 + isPinned: function isPinned(aItemId) { 1.44 + // Bookmarks are visible on StartUI (pinned) by default 1.45 + return this._getPrefValue().indexOf(aItemId) === -1; 1.46 + }, 1.47 + 1.48 + setUnpinned: function setPinned(aItemId) { 1.49 + let unpinned = this._getPrefValue(); 1.50 + unpinned.push(aItemId); 1.51 + this._setPrefValue(unpinned); 1.52 + }, 1.53 + 1.54 + setPinned: function unsetPinned(aItemId) { 1.55 + let unpinned = this._getPrefValue(); 1.56 + 1.57 + let index = unpinned.indexOf(aItemId); 1.58 + unpinned.splice(index, 1); 1.59 + 1.60 + this._setPrefValue(unpinned); 1.61 + }, 1.62 +}