michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: /** michael@0: * singleton to provide data-level functionality to the views michael@0: */ michael@0: let TopSites = { michael@0: prepareCache: function(aForce){ michael@0: // front to the NewTabUtils' links cache michael@0: // -ensure NewTabUtils.links links are pre-cached michael@0: michael@0: // avoid re-fetching links data while a fetch is in flight michael@0: if (this._promisedCache && !aForce) { michael@0: return this._promisedCache; michael@0: } michael@0: let deferred = Promise.defer(); michael@0: this._promisedCache = deferred.promise; michael@0: michael@0: NewTabUtils.links.populateCache(function () { michael@0: deferred.resolve(); michael@0: this._promisedCache = null; michael@0: this._sites = null; // reset our sites cache so they are built anew michael@0: this._sitesDirty.clear(); michael@0: }.bind(this), true); michael@0: return this._promisedCache; michael@0: }, michael@0: michael@0: _sites: null, michael@0: _sitesDirty: new Set(), michael@0: getSites: function() { michael@0: if (this._sites) { michael@0: return this._sites; michael@0: } michael@0: michael@0: let links = NewTabUtils.links.getLinks(); michael@0: let sites = links.map(function(aLink){ michael@0: let site = new Site(aLink); michael@0: return site; michael@0: }); michael@0: michael@0: // reset state michael@0: this._sites = sites; michael@0: this._sitesDirty.clear(); michael@0: return this._sites; michael@0: }, michael@0: michael@0: /** michael@0: * Get list of top site as in need of update/re-render michael@0: * @param aSite Optionally add Site arguments to be refreshed/updated michael@0: */ michael@0: dirty: function() { michael@0: // add any arguments for more fine-grained updates rather than invalidating the whole collection michael@0: for (let i=0; i -1) { michael@0: NewTabUtils.pinnedLinks.pin(site, pinIndex); michael@0: } michael@0: } michael@0: // clear out the cache, we'll fetch and re-render michael@0: this._sites = null; michael@0: this._sitesDirty.clear(); michael@0: this.update(); michael@0: }, michael@0: michael@0: _linkFromNode: function _linkFromNode(aNode) { michael@0: return { michael@0: url: aNode.getAttribute("value"), michael@0: title: aNode.getAttribute("label") michael@0: }; michael@0: } michael@0: }; michael@0: