#ifdef 0 /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #endif const THUMBNAIL_PLACEHOLDER_ENABLED = Services.prefs.getBoolPref("browser.newtabpage.thumbnailPlaceholder"); /** * This class represents a site that is contained in a cell and can be pinned, * moved around or deleted. */ function Site(aNode, aLink) { this._node = aNode; this._node._newtabSite = this; this._link = aLink; this._render(); this._addEventHandlers(); } Site.prototype = { /** * The site's DOM node. */ get node() { return this._node; }, /** * The site's link. */ get link() { return this._link; }, /** * The url of the site's link. */ get url() { return this.link.url; }, /** * The title of the site's link. */ get title() { return this.link.title || this.link.url; }, /** * The site's parent cell. */ get cell() { let parentNode = this.node.parentNode; return parentNode && parentNode._newtabCell; }, /** * Pins the site on its current or a given index. * @param aIndex The pinned index (optional). * @return true if link changed type after pin */ pin: function Site_pin(aIndex) { if (typeof aIndex == "undefined") aIndex = this.cell.index; this._updateAttributes(true); let changed = gPinnedLinks.pin(this._link, aIndex); if (changed) { // render site again to remove suggested/sponsored tags this._render(); } return changed; }, /** * Unpins the site and calls the given callback when done. */ unpin: function Site_unpin() { if (this.isPinned()) { this._updateAttributes(false); gPinnedLinks.unpin(this._link); gUpdater.updateGrid(); } }, /** * Checks whether this site is pinned. * @return Whether this site is pinned. */ isPinned: function Site_isPinned() { return gPinnedLinks.isPinned(this._link); }, /** * Blocks the site (removes it from the grid) and calls the given callback * when done. */ block: function Site_block() { if (!gBlockedLinks.isBlocked(this._link)) { gUndoDialog.show(this); gBlockedLinks.block(this._link); gUpdater.updateGrid(); } }, /** * Gets the DOM node specified by the given query selector. * @param aSelector The query selector. * @return The DOM node we found. */ _querySelector: function Site_querySelector(aSelector) { return this.node.querySelector(aSelector); }, /** * Updates attributes for all nodes which status depends on this site being * pinned or unpinned. * @param aPinned Whether this site is now pinned or unpinned. */ _updateAttributes: function (aPinned) { let control = this._querySelector(".newtab-control-pin"); if (aPinned) { this.node.setAttribute("pinned", true); control.setAttribute("title", newTabString("unpin")); } else { this.node.removeAttribute("pinned"); control.setAttribute("title", newTabString("pin")); } }, _newTabString: function(str, substrArr) { let regExp = /%[0-9]\$S/g; let matches; while ((matches = regExp.exec(str))) { let match = matches[0]; let index = match.charAt(1); // Get the digit in the regExp. str = str.replace(match, substrArr[index - 1]); } return str; }, _getSuggestedTileExplanation: function() { let targetedName = ` ${this.link.targetedName} `; let targetedSite = ` ${this.link.targetedSite} `; if (this.link.explanation) { return this._newTabString(this.link.explanation, [targetedName, targetedSite]); } return newTabString("suggested.button", [targetedName]); }, /** * Checks for and modifies link at campaign end time */ _checkLinkEndTime: function Site_checkLinkEndTime() { if (this.link.endTime && this.link.endTime < Date.now()) { let oldUrl = this.url; // chop off the path part from url this.link.url = Services.io.newURI(this.url, null, null).resolve("/"); // clear supplied images - this triggers thumbnail download for new url delete this.link.imageURI; delete this.link.enhancedImageURI; // remove endTime to avoid further time checks delete this.link.endTime; // clear enhanced-content image that may still exist in preloaded page this._querySelector(".enhanced-content").style.backgroundImage = ""; gPinnedLinks.replace(oldUrl, this.link); } }, /** * Renders the site's data (fills the HTML fragment). */ _render: function Site_render() { // first check for end time, as it may modify the link this._checkLinkEndTime(); // setup display variables let url = this.url; let title = this.link.type == "history" ? this.link.baseDomain : this.title; let tooltip = (this.title == url ? this.title : this.title + "\n" + url); let link = this._querySelector(".newtab-link"); link.setAttribute("title", tooltip); link.setAttribute("href", url); this.node.setAttribute("type", this.link.type); let titleNode = this._querySelector(".newtab-title"); titleNode.textContent = title; if (this.link.titleBgColor) { titleNode.style.backgroundColor = this.link.titleBgColor; } // remove "suggested" attribute to avoid showing "suggested" tag // after site was pinned or dropped this.node.removeAttribute("suggested"); if (this.link.targetedSite) { if (this.node.getAttribute("type") != "sponsored") { this._querySelector(".newtab-sponsored").textContent = newTabString("suggested.tag"); } this.node.setAttribute("suggested", true); let explanation = this._getSuggestedTileExplanation(); this._querySelector(".newtab-suggested").innerHTML = `