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: * dumb model class to provide default values for sites. michael@0: * link parameter/model object expected to have a .url property, and optionally .title michael@0: */ michael@0: function Site(aLink) { michael@0: if (!aLink.url) { michael@0: throw Cr.NS_ERROR_INVALID_ARG; michael@0: } michael@0: this._link = aLink; michael@0: } michael@0: michael@0: Site.prototype = { michael@0: icon: '', michael@0: get url() { michael@0: return this._link.url; michael@0: }, michael@0: get title() { michael@0: // use url if no title was recorded michael@0: return this._link.title || this._link.url; michael@0: }, michael@0: get label() { michael@0: // alias for .title michael@0: return this.title; michael@0: }, michael@0: get pinned() { michael@0: return NewTabUtils.pinnedLinks.isPinned(this); michael@0: }, michael@0: get contextActions() { michael@0: return [ michael@0: 'delete', // delete means hide here michael@0: this.pinned ? 'unpin' : 'pin' michael@0: ]; michael@0: }, michael@0: get blocked() { michael@0: return NewTabUtils.blockedLinks.isBlocked(this); michael@0: }, michael@0: get attributeValues() { michael@0: return { michael@0: value: this.url, michael@0: label: this.title, michael@0: pinned: this.pinned ? true : undefined, michael@0: selected: this.selected, michael@0: customColor: this.color, michael@0: customImage: this.backgroundImage, michael@0: iconURI: this.icon, michael@0: "data-contextactions": this.contextActions.join(',') michael@0: }; michael@0: }, michael@0: applyToTileNode: function(aNode) { michael@0: // apply this site's properties as attributes on a tile element michael@0: // the decorated node acts as a view-model for the tile binding michael@0: let attrs = this.attributeValues; michael@0: for (let key in attrs) { michael@0: if (undefined === attrs[key]) { michael@0: aNode.removeAttribute(key); michael@0: } else { michael@0: aNode.setAttribute(key, attrs[key]); michael@0: } michael@0: } michael@0: // is binding already applied? michael@0: if ('refresh' in aNode) { michael@0: // just update it michael@0: aNode.refresh(); michael@0: } else { michael@0: // these attribute values will get picked up later when the binding is applied michael@0: } michael@0: } michael@0: };