2012-01-25 14:40:18 -08:00
|
|
|
#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
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() this._node,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The site's link.
|
|
|
|
*/
|
|
|
|
get link() this._link,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The url of the site's link.
|
|
|
|
*/
|
|
|
|
get url() this.link.url,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The title of the site's link.
|
|
|
|
*/
|
|
|
|
get title() this.link.title,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
pin: function Site_pin(aIndex) {
|
|
|
|
if (typeof aIndex == "undefined")
|
|
|
|
aIndex = this.cell.index;
|
|
|
|
|
|
|
|
this._updateAttributes(true);
|
|
|
|
gPinnedLinks.pin(this._link, aIndex);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unpins the site and calls the given callback when done.
|
|
|
|
*/
|
2012-03-27 03:02:55 -07:00
|
|
|
unpin: function Site_unpin() {
|
2012-01-25 14:40:18 -08:00
|
|
|
if (this.isPinned()) {
|
|
|
|
this._updateAttributes(false);
|
|
|
|
gPinnedLinks.unpin(this._link);
|
2012-03-27 03:02:55 -07:00
|
|
|
gUpdater.updateGrid();
|
2012-01-25 14:40:18 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-03-27 03:02:55 -07:00
|
|
|
block: function Site_block() {
|
2012-04-05 15:33:47 -07:00
|
|
|
if (!gBlockedLinks.isBlocked(this._link)) {
|
2012-08-23 15:34:52 -07:00
|
|
|
gUndoDialog.show(this);
|
2012-03-12 01:51:00 -07:00
|
|
|
gBlockedLinks.block(this._link);
|
2012-03-27 03:02:55 -07:00
|
|
|
gUpdater.updateGrid();
|
2012-03-12 01:51:00 -07:00
|
|
|
}
|
2012-01-25 14:40:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2012-03-12 19:23:01 -07:00
|
|
|
let control = this._querySelector(".newtab-control-pin");
|
2012-01-25 14:40:18 -08:00
|
|
|
|
|
|
|
if (aPinned) {
|
2012-03-12 19:23:01 -07:00
|
|
|
control.setAttribute("pinned", true);
|
|
|
|
control.setAttribute("title", newTabString("unpin"));
|
2012-01-25 14:40:18 -08:00
|
|
|
} else {
|
2012-03-12 19:23:01 -07:00
|
|
|
control.removeAttribute("pinned");
|
|
|
|
control.setAttribute("title", newTabString("pin"));
|
2012-01-25 14:40:18 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the site's data (fills the HTML fragment).
|
|
|
|
*/
|
|
|
|
_render: function Site_render() {
|
2012-04-13 15:43:29 -07:00
|
|
|
let url = this.url;
|
|
|
|
let title = this.title || url;
|
|
|
|
let tooltip = (title == url ? title : title + "\n" + url);
|
|
|
|
|
2012-03-12 19:23:01 -07:00
|
|
|
let link = this._querySelector(".newtab-link");
|
2012-04-13 15:43:29 -07:00
|
|
|
link.setAttribute("title", tooltip);
|
|
|
|
link.setAttribute("href", url);
|
2012-03-12 19:23:01 -07:00
|
|
|
this._querySelector(".newtab-title").textContent = title;
|
2012-01-25 14:40:18 -08:00
|
|
|
|
|
|
|
if (this.isPinned())
|
|
|
|
this._updateAttributes(true);
|
|
|
|
|
2012-03-12 19:23:01 -07:00
|
|
|
let thumbnailURL = PageThumbs.getThumbnailURL(this.url);
|
|
|
|
let thumbnail = this._querySelector(".newtab-thumbnail");
|
|
|
|
thumbnail.style.backgroundImage = "url(" + thumbnailURL + ")";
|
2012-01-25 14:40:18 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds event handlers for the site and its buttons.
|
|
|
|
*/
|
|
|
|
_addEventHandlers: function Site_addEventHandlers() {
|
|
|
|
// Register drag-and-drop event handlers.
|
2012-03-12 19:23:01 -07:00
|
|
|
this._node.addEventListener("dragstart", this, false);
|
|
|
|
this._node.addEventListener("dragend", this, false);
|
2013-03-26 07:00:59 -07:00
|
|
|
this._node.addEventListener("mouseover", this, false);
|
2012-01-25 14:40:18 -08:00
|
|
|
|
2012-03-12 19:23:01 -07:00
|
|
|
let controls = this.node.querySelectorAll(".newtab-control");
|
|
|
|
for (let i = 0; i < controls.length; i++)
|
|
|
|
controls[i].addEventListener("click", this, false);
|
2012-01-25 14:40:18 -08:00
|
|
|
},
|
|
|
|
|
2013-03-21 07:11:57 -07:00
|
|
|
/**
|
|
|
|
* Speculatively opens a connection to the current site.
|
|
|
|
*/
|
|
|
|
_speculativeConnect: function Site_speculativeConnect() {
|
|
|
|
let sc = Services.io.QueryInterface(Ci.nsISpeculativeConnect);
|
|
|
|
let uri = Services.io.newURI(this.url, null, null);
|
|
|
|
sc.speculativeConnect(uri, null);
|
|
|
|
},
|
|
|
|
|
2012-01-25 14:40:18 -08:00
|
|
|
/**
|
2012-03-12 19:23:01 -07:00
|
|
|
* Handles all site events.
|
2012-01-25 14:40:18 -08:00
|
|
|
*/
|
2012-03-12 19:23:01 -07:00
|
|
|
handleEvent: function Site_handleEvent(aEvent) {
|
|
|
|
switch (aEvent.type) {
|
|
|
|
case "click":
|
|
|
|
aEvent.preventDefault();
|
|
|
|
if (aEvent.target.classList.contains("newtab-control-block"))
|
|
|
|
this.block();
|
|
|
|
else if (this.isPinned())
|
|
|
|
this.unpin();
|
|
|
|
else
|
|
|
|
this.pin();
|
|
|
|
break;
|
2013-03-26 07:00:59 -07:00
|
|
|
case "mouseover":
|
|
|
|
this._node.removeEventListener("mouseover", this, false);
|
2013-03-21 07:11:57 -07:00
|
|
|
this._speculativeConnect();
|
|
|
|
break;
|
2012-03-12 19:23:01 -07:00
|
|
|
case "dragstart":
|
|
|
|
gDrag.start(this, aEvent);
|
|
|
|
break;
|
|
|
|
case "drag":
|
|
|
|
gDrag.drag(this, aEvent);
|
|
|
|
break;
|
|
|
|
case "dragend":
|
|
|
|
gDrag.end(this, aEvent);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-25 14:40:18 -08:00
|
|
|
}
|
|
|
|
};
|