browserPlacesViews.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  5. Components.utils.import("resource://gre/modules/Services.jsm");
  6. /**
  7. * The base view implements everything that's common to the toolbar and
  8. * menu views.
  9. */
  10. function PlacesViewBase(aPlace) {
  11. this.place = aPlace;
  12. this._controller = new PlacesController(this);
  13. this._viewElt.controllers.appendController(this._controller);
  14. }
  15. PlacesViewBase.prototype = {
  16. // The xul element that holds the entire view.
  17. _viewElt: null,
  18. get viewElt() this._viewElt,
  19. get associatedElement() this._viewElt,
  20. get controllers() this._viewElt.controllers,
  21. // The xul element that represents the root container.
  22. _rootElt: null,
  23. // Set to true for views that are represented by native widgets (i.e.
  24. // the native mac menu).
  25. _nativeView: false,
  26. QueryInterface: XPCOMUtils.generateQI(
  27. [Components.interfaces.nsINavHistoryResultObserver,
  28. Components.interfaces.nsISupportsWeakReference]),
  29. _place: "",
  30. get place() this._place,
  31. set place(val) {
  32. this._place = val;
  33. let history = PlacesUtils.history;
  34. let queries = { }, options = { };
  35. history.queryStringToQueries(val, queries, { }, options);
  36. if (!queries.value.length)
  37. queries.value = [history.getNewQuery()];
  38. let result = history.executeQueries(queries.value, queries.value.length,
  39. options.value);
  40. result.addObserver(this, false);
  41. return val;
  42. },
  43. _result: null,
  44. get result() this._result,
  45. set result(val) {
  46. if (this._result == val)
  47. return val;
  48. if (this._result) {
  49. this._result.removeObserver(this);
  50. this._resultNode.containerOpen = false;
  51. }
  52. if (this._rootElt.localName == "menupopup")
  53. this._rootElt._built = false;
  54. this._result = val;
  55. if (val) {
  56. this._resultNode = val.root;
  57. this._rootElt._placesNode = this._resultNode;
  58. this._domNodes = new Map();
  59. this._domNodes.set(this._resultNode, this._rootElt);
  60. // This calls _rebuild through invalidateContainer.
  61. this._resultNode.containerOpen = true;
  62. }
  63. else {
  64. this._resultNode = null;
  65. delete this._domNodes;
  66. }
  67. return val;
  68. },
  69. /**
  70. * Gets the DOM node used for the given places node.
  71. *
  72. * @param aPlacesNode
  73. * a places result node.
  74. * @throws if there is no DOM node set for aPlacesNode.
  75. */
  76. _getDOMNodeForPlacesNode:
  77. function(aPlacesNode) {
  78. let node = this._domNodes.get(aPlacesNode, null);
  79. if (!node) {
  80. throw new Error("No DOM node set for aPlacesNode.\nnode.type: " +
  81. aPlacesNode.type + ". node.parent: " + aPlacesNode);
  82. }
  83. return node;
  84. },
  85. get controller() this._controller,
  86. get selType() "single",
  87. selectItems: function() { },
  88. selectAll: function() { },
  89. get selectedNode() {
  90. if (this._contextMenuShown) {
  91. let anchor = this._contextMenuShown.triggerNode;
  92. if (!anchor)
  93. return null;
  94. if (anchor._placesNode)
  95. return this._rootElt == anchor ? null : anchor._placesNode;
  96. anchor = anchor.parentNode;
  97. return this._rootElt == anchor ? null : (anchor._placesNode || null);
  98. }
  99. return null;
  100. },
  101. get hasSelection() this.selectedNode != null,
  102. get selectedNodes() {
  103. let selectedNode = this.selectedNode;
  104. return selectedNode ? [selectedNode] : [];
  105. },
  106. get removableSelectionRanges() {
  107. // On static content the current selectedNode would be the selection's
  108. // parent node. We don't want to allow removing a node when the
  109. // selection is not explicit.
  110. if (document.popupNode &&
  111. (document.popupNode == "menupopup" || !document.popupNode._placesNode))
  112. return [];
  113. return [this.selectedNodes];
  114. },
  115. get draggableSelection() [this._draggedElt],
  116. get insertionPoint() {
  117. // There is no insertion point for history queries, so bail out now and
  118. // save a lot of work when updating commands.
  119. let resultNode = this._resultNode;
  120. if (PlacesUtils.nodeIsQuery(resultNode) &&
  121. PlacesUtils.asQuery(resultNode).queryOptions.queryType ==
  122. Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY)
  123. return null;
  124. // By default, the insertion point is at the top level, at the end.
  125. let index = PlacesUtils.bookmarks.DEFAULT_INDEX;
  126. let container = this._resultNode;
  127. let orientation = Ci.nsITreeView.DROP_BEFORE;
  128. let isTag = false;
  129. let selectedNode = this.selectedNode;
  130. if (selectedNode) {
  131. let popup = document.popupNode;
  132. if (!popup._placesNode || popup._placesNode == this._resultNode ||
  133. popup._placesNode.itemId == -1) {
  134. // If a static menuitem is selected, or if the root node is selected,
  135. // the insertion point is inside the folder, at the end.
  136. container = selectedNode;
  137. orientation = Ci.nsITreeView.DROP_ON;
  138. }
  139. else {
  140. // In all other cases the insertion point is before that node.
  141. container = selectedNode.parent;
  142. index = container.getChildIndex(selectedNode);
  143. isTag = PlacesUtils.nodeIsTagQuery(container);
  144. }
  145. }
  146. if (PlacesControllerDragHelper.disallowInsertion(container))
  147. return null;
  148. return new InsertionPoint(PlacesUtils.getConcreteItemId(container),
  149. index, orientation, isTag);
  150. },
  151. buildContextMenu: function(aPopup) {
  152. this._contextMenuShown = aPopup;
  153. window.updateCommands("places");
  154. return this.controller.buildContextMenu(aPopup);
  155. },
  156. destroyContextMenu: function(aPopup) {
  157. this._contextMenuShown = null;
  158. },
  159. _cleanPopup: function(aPopup, aDelay) {
  160. // Remove Places nodes from the popup.
  161. let child = aPopup._startMarker;
  162. while (child.nextSibling != aPopup._endMarker) {
  163. let sibling = child.nextSibling;
  164. if (sibling._placesNode && !aDelay) {
  165. aPopup.removeChild(sibling);
  166. }
  167. else if (sibling._placesNode && aDelay) {
  168. // HACK (bug 733419): the popups originating from the OS X native
  169. // menubar don't live-update while open, thus we don't clean it
  170. // until the next popupshowing, to avoid zombie menuitems.
  171. if (!aPopup._delayedRemovals)
  172. aPopup._delayedRemovals = [];
  173. aPopup._delayedRemovals.push(sibling);
  174. child = child.nextSibling;
  175. }
  176. else {
  177. child = child.nextSibling;
  178. }
  179. }
  180. },
  181. _rebuildPopup: function(aPopup) {
  182. let resultNode = aPopup._placesNode;
  183. if (!resultNode.containerOpen)
  184. return;
  185. if (this.controller.hasCachedLivemarkInfo(resultNode)) {
  186. this._setEmptyPopupStatus(aPopup, false);
  187. aPopup._built = true;
  188. this._populateLivemarkPopup(aPopup);
  189. return;
  190. }
  191. this._cleanPopup(aPopup);
  192. let cc = resultNode.childCount;
  193. if (cc > 0) {
  194. this._setEmptyPopupStatus(aPopup, false);
  195. for (let i = 0; i < cc; ++i) {
  196. let child = resultNode.getChild(i);
  197. this._insertNewItemToPopup(child, aPopup, null);
  198. }
  199. }
  200. else {
  201. this._setEmptyPopupStatus(aPopup, true);
  202. }
  203. aPopup._built = true;
  204. },
  205. _removeChild: function(aChild) {
  206. // If document.popupNode pointed to this child, null it out,
  207. // otherwise controller's command-updating may rely on the removed
  208. // item still being "selected".
  209. if (document.popupNode == aChild)
  210. document.popupNode = null;
  211. aChild.parentNode.removeChild(aChild);
  212. },
  213. _setEmptyPopupStatus:
  214. function(aPopup, aEmpty) {
  215. if (!aPopup._emptyMenuitem) {
  216. let label = PlacesUIUtils.getString("bookmarksMenuEmptyFolder");
  217. aPopup._emptyMenuitem = document.createElement("menuitem");
  218. aPopup._emptyMenuitem.setAttribute("label", label);
  219. aPopup._emptyMenuitem.setAttribute("disabled", true);
  220. }
  221. if (aEmpty) {
  222. aPopup.setAttribute("emptyplacesresult", "true");
  223. // Don't add the menuitem if there is static content.
  224. if (!aPopup._startMarker.previousSibling &&
  225. !aPopup._endMarker.nextSibling)
  226. aPopup.insertBefore(aPopup._emptyMenuitem, aPopup._endMarker);
  227. }
  228. else {
  229. aPopup.removeAttribute("emptyplacesresult");
  230. try {
  231. aPopup.removeChild(aPopup._emptyMenuitem);
  232. } catch (ex) {}
  233. }
  234. },
  235. _createMenuItemForPlacesNode:
  236. function(aPlacesNode) {
  237. this._domNodes.delete(aPlacesNode);
  238. let element;
  239. let type = aPlacesNode.type;
  240. if (type == Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR) {
  241. element = document.createElement("menuseparator");
  242. }
  243. else {
  244. let itemId = aPlacesNode.itemId;
  245. if (type == Ci.nsINavHistoryResultNode.RESULT_TYPE_URI) {
  246. element = document.createElement("menuitem");
  247. element.className = "menuitem-iconic bookmark-item menuitem-with-favicon";
  248. element.setAttribute("scheme",
  249. PlacesUIUtils.guessUrlSchemeForUI(aPlacesNode.uri));
  250. }
  251. else if (PlacesUtils.containerTypes.indexOf(type) != -1) {
  252. element = document.createElement("menu");
  253. element.setAttribute("container", "true");
  254. if (aPlacesNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY) {
  255. element.setAttribute("query", "true");
  256. if (PlacesUtils.nodeIsTagQuery(aPlacesNode))
  257. element.setAttribute("tagContainer", "true");
  258. else if (PlacesUtils.nodeIsDay(aPlacesNode))
  259. element.setAttribute("dayContainer", "true");
  260. else if (PlacesUtils.nodeIsHost(aPlacesNode))
  261. element.setAttribute("hostContainer", "true");
  262. }
  263. else if (itemId != -1) {
  264. PlacesUtils.livemarks.getLivemark({ id: itemId })
  265. .then(aLivemark => {
  266. element.setAttribute("livemark", "true");
  267. this.controller.cacheLivemarkInfo(aPlacesNode, aLivemark);
  268. }, () => undefined);
  269. }
  270. let popup = document.createElement("menupopup");
  271. popup._placesNode = PlacesUtils.asContainer(aPlacesNode);
  272. if (!this._nativeView) {
  273. popup.setAttribute("placespopup", "true");
  274. }
  275. element.appendChild(popup);
  276. element.className = "menu-iconic bookmark-item";
  277. this._domNodes.set(aPlacesNode, popup);
  278. }
  279. else
  280. throw "Unexpected node";
  281. element.setAttribute("label", PlacesUIUtils.getBestTitle(aPlacesNode));
  282. let icon = aPlacesNode.icon;
  283. if (icon)
  284. element.setAttribute("image",
  285. PlacesUIUtils.getImageURLForResolution(window, icon));
  286. }
  287. element._placesNode = aPlacesNode;
  288. if (!this._domNodes.has(aPlacesNode))
  289. this._domNodes.set(aPlacesNode, element);
  290. return element;
  291. },
  292. _insertNewItemToPopup:
  293. function(aNewChild, aPopup, aBefore) {
  294. let element = this._createMenuItemForPlacesNode(aNewChild);
  295. let before = aBefore || aPopup._endMarker;
  296. aPopup.insertBefore(element, before);
  297. return element;
  298. },
  299. _setLivemarkSiteURIMenuItem:
  300. function(aPopup) {
  301. let livemarkInfo = this.controller.getCachedLivemarkInfo(aPopup._placesNode);
  302. let siteUrl = livemarkInfo && livemarkInfo.siteURI ?
  303. livemarkInfo.siteURI.spec : null;
  304. if (!siteUrl && aPopup._siteURIMenuitem) {
  305. aPopup.removeChild(aPopup._siteURIMenuitem);
  306. aPopup._siteURIMenuitem = null;
  307. aPopup.removeChild(aPopup._siteURIMenuseparator);
  308. aPopup._siteURIMenuseparator = null;
  309. }
  310. else if (siteUrl && !aPopup._siteURIMenuitem) {
  311. // Add "Open (Feed Name)" menuitem.
  312. aPopup._siteURIMenuitem = document.createElement("menuitem");
  313. aPopup._siteURIMenuitem.className = "openlivemarksite-menuitem";
  314. aPopup._siteURIMenuitem.setAttribute("targetURI", siteUrl);
  315. aPopup._siteURIMenuitem.setAttribute("oncommand",
  316. "openUILink(this.getAttribute('targetURI'), event);");
  317. // If a user middle-clicks this item we serve the oncommand event.
  318. // We are using checkForMiddleClick because of Bug 246720.
  319. // Note: stopPropagation is needed to avoid serving middle-click
  320. // with BT_onClick that would open all items in tabs.
  321. aPopup._siteURIMenuitem.setAttribute("onclick",
  322. "checkForMiddleClick(this, event); event.stopPropagation();");
  323. let label =
  324. PlacesUIUtils.getFormattedString("menuOpenLivemarkOrigin.label",
  325. [aPopup.parentNode.getAttribute("label")])
  326. aPopup._siteURIMenuitem.setAttribute("label", label);
  327. aPopup.insertBefore(aPopup._siteURIMenuitem, aPopup._startMarker);
  328. aPopup._siteURIMenuseparator = document.createElement("menuseparator");
  329. aPopup.insertBefore(aPopup._siteURIMenuseparator, aPopup._startMarker);
  330. }
  331. },
  332. /**
  333. * Add, update or remove the livemark status menuitem.
  334. * @param aPopup
  335. * The livemark container popup
  336. * @param aStatus
  337. * The livemark status
  338. */
  339. _setLivemarkStatusMenuItem:
  340. function(aPopup, aStatus) {
  341. let statusMenuitem = aPopup._statusMenuitem;
  342. if (!statusMenuitem) {
  343. // Create the status menuitem and cache it in the popup object.
  344. statusMenuitem = document.createElement("menuitem");
  345. statusMenuitem.className = "livemarkstatus-menuitem";
  346. statusMenuitem.setAttribute("disabled", true);
  347. aPopup._statusMenuitem = statusMenuitem;
  348. }
  349. if (aStatus == Ci.mozILivemark.STATUS_LOADING ||
  350. aStatus == Ci.mozILivemark.STATUS_FAILED) {
  351. // Status has changed, update the cached status menuitem.
  352. let stringId = aStatus == Ci.mozILivemark.STATUS_LOADING ?
  353. "bookmarksLivemarkLoading" : "bookmarksLivemarkFailed";
  354. statusMenuitem.setAttribute("label", PlacesUIUtils.getString(stringId));
  355. if (aPopup._startMarker.nextSibling != statusMenuitem)
  356. aPopup.insertBefore(statusMenuitem, aPopup._startMarker.nextSibling);
  357. }
  358. else {
  359. // The livemark has finished loading.
  360. if (aPopup._statusMenuitem.parentNode == aPopup)
  361. aPopup.removeChild(aPopup._statusMenuitem);
  362. }
  363. },
  364. toggleCutNode: function(aPlacesNode, aValue) {
  365. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  366. // We may get the popup for menus, but we need the menu itself.
  367. if (elt.localName == "menupopup")
  368. elt = elt.parentNode;
  369. if (aValue)
  370. elt.setAttribute("cutting", "true");
  371. else
  372. elt.removeAttribute("cutting");
  373. },
  374. nodeURIChanged: function(aPlacesNode, aURIString) {
  375. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  376. // Here we need the <menu>.
  377. if (elt.localName == "menupopup")
  378. elt = elt.parentNode;
  379. elt.setAttribute("scheme", PlacesUIUtils.guessUrlSchemeForUI(aURIString));
  380. },
  381. nodeIconChanged: function(aPlacesNode) {
  382. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  383. // There's no UI representation for the root node, thus there's nothing to
  384. // be done when the icon changes.
  385. if (elt == this._rootElt)
  386. return;
  387. // Here we need the <menu>.
  388. if (elt.localName == "menupopup")
  389. elt = elt.parentNode;
  390. let icon = aPlacesNode.icon;
  391. if (!icon)
  392. elt.removeAttribute("image");
  393. else if (icon != elt.getAttribute("image"))
  394. elt.setAttribute("image",
  395. PlacesUIUtils.getImageURLForResolution(window, icon));
  396. },
  397. nodeAnnotationChanged:
  398. function(aPlacesNode, aAnno) {
  399. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  400. // All livemarks have a feedURI, so use it as our indicator of a livemark
  401. // being modified.
  402. if (aAnno == PlacesUtils.LMANNO_FEEDURI) {
  403. let menu = elt.parentNode;
  404. if (!menu.hasAttribute("livemark")) {
  405. menu.setAttribute("livemark", "true");
  406. }
  407. PlacesUtils.livemarks.getLivemark({ id: aPlacesNode.itemId })
  408. .then(aLivemark => {
  409. // Controller will use this to build the meta data for the node.
  410. this.controller.cacheLivemarkInfo(aPlacesNode, aLivemark);
  411. this.invalidateContainer(aPlacesNode);
  412. }, () => undefined);
  413. }
  414. },
  415. nodeTitleChanged:
  416. function(aPlacesNode, aNewTitle) {
  417. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  418. // There's no UI representation for the root node, thus there's
  419. // nothing to be done when the title changes.
  420. if (elt == this._rootElt)
  421. return;
  422. // Here we need the <menu>.
  423. if (elt.localName == "menupopup")
  424. elt = elt.parentNode;
  425. if (!aNewTitle && elt.localName != "toolbarbutton") {
  426. // Many users consider toolbars as shortcuts containers, so explicitly
  427. // allow empty labels on toolbarbuttons. For any other element try to be
  428. // smarter, guessing a title from the uri.
  429. elt.setAttribute("label", PlacesUIUtils.getBestTitle(aPlacesNode));
  430. }
  431. else {
  432. elt.setAttribute("label", aNewTitle);
  433. }
  434. },
  435. nodeRemoved:
  436. function(aParentPlacesNode, aPlacesNode, aIndex) {
  437. let parentElt = this._getDOMNodeForPlacesNode(aParentPlacesNode);
  438. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  439. // Here we need the <menu>.
  440. if (elt.localName == "menupopup")
  441. elt = elt.parentNode;
  442. if (parentElt._built) {
  443. parentElt.removeChild(elt);
  444. // Figure out if we need to show the "<Empty>" menu-item.
  445. // TODO Bug 517701: This doesn't seem to handle the case of an empty
  446. // root.
  447. if (parentElt._startMarker.nextSibling == parentElt._endMarker)
  448. this._setEmptyPopupStatus(parentElt, true);
  449. }
  450. },
  451. nodeHistoryDetailsChanged:
  452. function(aPlacesNode, aTime, aCount) {
  453. if (aPlacesNode.parent &&
  454. this.controller.hasCachedLivemarkInfo(aPlacesNode.parent)) {
  455. // Find the node in the parent.
  456. let popup = this._getDOMNodeForPlacesNode(aPlacesNode.parent);
  457. for (let child = popup._startMarker.nextSibling;
  458. child != popup._endMarker;
  459. child = child.nextSibling) {
  460. if (child._placesNode && child._placesNode.uri == aPlacesNode.uri) {
  461. if (aCount)
  462. child.setAttribute("visited", "true");
  463. else
  464. child.removeAttribute("visited");
  465. break;
  466. }
  467. }
  468. }
  469. },
  470. nodeTagsChanged: function() { },
  471. nodeDateAddedChanged: function() { },
  472. nodeLastModifiedChanged: function() { },
  473. nodeKeywordChanged: function() { },
  474. sortingChanged: function() { },
  475. batching: function() { },
  476. nodeInserted:
  477. function(aParentPlacesNode, aPlacesNode, aIndex) {
  478. let parentElt = this._getDOMNodeForPlacesNode(aParentPlacesNode);
  479. if (!parentElt._built)
  480. return;
  481. let index = Array.indexOf(parentElt.childNodes, parentElt._startMarker) +
  482. aIndex + 1;
  483. this._insertNewItemToPopup(aPlacesNode, parentElt,
  484. parentElt.childNodes[index]);
  485. this._setEmptyPopupStatus(parentElt, false);
  486. },
  487. nodeMoved:
  488. function(aPlacesNode,
  489. aOldParentPlacesNode, aOldIndex,
  490. aNewParentPlacesNode, aNewIndex) {
  491. // Note: the current implementation of moveItem does not actually
  492. // use this notification when the item in question is moved from one
  493. // folder to another. Instead, it calls nodeRemoved and nodeInserted
  494. // for the two folders. Thus, we can assume old-parent == new-parent.
  495. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  496. // Here we need the <menu>.
  497. if (elt.localName == "menupopup")
  498. elt = elt.parentNode;
  499. // If our root node is a folder, it might be moved. There's nothing
  500. // we need to do in that case.
  501. if (elt == this._rootElt)
  502. return;
  503. let parentElt = this._getDOMNodeForPlacesNode(aNewParentPlacesNode);
  504. if (parentElt._built) {
  505. // Move the node.
  506. parentElt.removeChild(elt);
  507. let index = Array.indexOf(parentElt.childNodes, parentElt._startMarker) +
  508. aNewIndex + 1;
  509. parentElt.insertBefore(elt, parentElt.childNodes[index]);
  510. }
  511. },
  512. containerStateChanged:
  513. function(aPlacesNode, aOldState, aNewState) {
  514. if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_OPENED ||
  515. aNewState == Ci.nsINavHistoryContainerResultNode.STATE_CLOSED) {
  516. this.invalidateContainer(aPlacesNode);
  517. if (PlacesUtils.nodeIsFolder(aPlacesNode)) {
  518. let queryOptions = PlacesUtils.asQuery(this._result.root).queryOptions;
  519. if (queryOptions.excludeItems) {
  520. return;
  521. }
  522. PlacesUtils.livemarks.getLivemark({ id: aPlacesNode.itemId })
  523. .then(aLivemark => {
  524. let shouldInvalidate =
  525. !this.controller.hasCachedLivemarkInfo(aPlacesNode);
  526. this.controller.cacheLivemarkInfo(aPlacesNode, aLivemark);
  527. if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_OPENED) {
  528. aLivemark.registerForUpdates(aPlacesNode, this);
  529. // Prioritize the current livemark.
  530. aLivemark.reload();
  531. PlacesUtils.livemarks.reloadLivemarks();
  532. if (shouldInvalidate)
  533. this.invalidateContainer(aPlacesNode);
  534. }
  535. else {
  536. aLivemark.unregisterForUpdates(aPlacesNode);
  537. }
  538. }, () => undefined);
  539. }
  540. }
  541. },
  542. _populateLivemarkPopup: function(aPopup)
  543. {
  544. this._setLivemarkSiteURIMenuItem(aPopup);
  545. // Show the loading status only if there are no entries yet.
  546. if (aPopup._startMarker.nextSibling == aPopup._endMarker)
  547. this._setLivemarkStatusMenuItem(aPopup, Ci.mozILivemark.STATUS_LOADING);
  548. PlacesUtils.livemarks.getLivemark({ id: aPopup._placesNode.itemId })
  549. .then(aLivemark => {
  550. let placesNode = aPopup._placesNode;
  551. if (!placesNode.containerOpen)
  552. return;
  553. if (aLivemark.status != Ci.mozILivemark.STATUS_LOADING)
  554. this._setLivemarkStatusMenuItem(aPopup, aLivemark.status);
  555. this._cleanPopup(aPopup,
  556. this._nativeView && aPopup.parentNode.hasAttribute("open"));
  557. let children = aLivemark.getNodesForContainer(placesNode);
  558. for (let i = 0; i < children.length; i++) {
  559. let child = children[i];
  560. this.nodeInserted(placesNode, child, i);
  561. if (child.accessCount)
  562. this._getDOMNodeForPlacesNode(child).setAttribute("visited", true);
  563. else
  564. this._getDOMNodeForPlacesNode(child).removeAttribute("visited");
  565. }
  566. }, Components.utils.reportError);
  567. },
  568. invalidateContainer: function(aPlacesNode) {
  569. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  570. elt._built = false;
  571. // If the menupopup is open we should live-update it.
  572. if (elt.parentNode.open)
  573. this._rebuildPopup(elt);
  574. },
  575. uninit: function() {
  576. if (this._result) {
  577. this._result.removeObserver(this);
  578. this._resultNode.containerOpen = false;
  579. this._resultNode = null;
  580. this._result = null;
  581. }
  582. if (this._controller) {
  583. this._controller.terminate();
  584. // Removing the controller will fail if it is already no longer there.
  585. // This can happen if the view element was removed/reinserted without
  586. // our knowledge. There is no way to check for that having happened
  587. // without the possibility of an exception. :-(
  588. try {
  589. this._viewElt.controllers.removeController(this._controller);
  590. } catch (ex) {
  591. } finally {
  592. this._controller = null;
  593. }
  594. }
  595. delete this._viewElt._placesView;
  596. },
  597. get isRTL() {
  598. if ("_isRTL" in this)
  599. return this._isRTL;
  600. return this._isRTL = document.defaultView
  601. .getComputedStyle(this.viewElt, "")
  602. .direction == "rtl";
  603. },
  604. get ownerWindow() window,
  605. /**
  606. * Adds an "Open All in Tabs" menuitem to the bottom of the popup.
  607. * @param aPopup
  608. * a Places popup.
  609. */
  610. _mayAddCommandsItems: function(aPopup) {
  611. // The command items are never added to the root popup.
  612. if (aPopup == this._rootElt)
  613. return;
  614. let hasMultipleURIs = false;
  615. // Check if the popup contains at least 2 menuitems with places nodes.
  616. // We don't currently support opening multiple uri nodes when they are not
  617. // populated by the result.
  618. if (aPopup._placesNode.childCount > 0) {
  619. let currentChild = aPopup.firstChild;
  620. let numURINodes = 0;
  621. while (currentChild) {
  622. if (currentChild.localName == "menuitem" && currentChild._placesNode) {
  623. if (++numURINodes == 2)
  624. break;
  625. }
  626. currentChild = currentChild.nextSibling;
  627. }
  628. hasMultipleURIs = numURINodes > 1;
  629. }
  630. let isLiveMark = false;
  631. if (this.controller.hasCachedLivemarkInfo(aPopup._placesNode)) {
  632. hasMultipleURIs = true;
  633. isLiveMark = true;
  634. }
  635. if (!hasMultipleURIs) {
  636. // We don't have to show any option.
  637. if (aPopup._endOptOpenAllInTabs) {
  638. aPopup.removeChild(aPopup._endOptOpenAllInTabs);
  639. aPopup._endOptOpenAllInTabs = null;
  640. aPopup.removeChild(aPopup._endOptSeparator);
  641. aPopup._endOptSeparator = null;
  642. }
  643. }
  644. else if (!aPopup._endOptOpenAllInTabs) {
  645. // Create a separator before options.
  646. aPopup._endOptSeparator = document.createElement("menuseparator");
  647. aPopup._endOptSeparator.className = "bookmarks-actions-menuseparator";
  648. aPopup.appendChild(aPopup._endOptSeparator);
  649. // Add the "Open All in Tabs" menuitem.
  650. aPopup._endOptOpenAllInTabs = document.createElement("menuitem");
  651. aPopup._endOptOpenAllInTabs.className = "openintabs-menuitem";
  652. if (isLiveMark) {
  653. aPopup._endOptOpenAllInTabs.setAttribute("oncommand",
  654. "PlacesUIUtils.openLiveMarkNodesInTabs(this.parentNode._placesNode, event, " +
  655. "PlacesUIUtils.getViewForNode(this));");
  656. } else {
  657. aPopup._endOptOpenAllInTabs.setAttribute("oncommand",
  658. "PlacesUIUtils.openContainerNodeInTabs(this.parentNode._placesNode, event, " +
  659. "PlacesUIUtils.getViewForNode(this));");
  660. }
  661. aPopup._endOptOpenAllInTabs.setAttribute("onclick",
  662. "checkForMiddleClick(this, event); event.stopPropagation();");
  663. aPopup._endOptOpenAllInTabs.setAttribute("label",
  664. gNavigatorBundle.getString("menuOpenAllInTabs.label"));
  665. aPopup.appendChild(aPopup._endOptOpenAllInTabs);
  666. }
  667. },
  668. _ensureMarkers: function(aPopup) {
  669. if (aPopup._startMarker)
  670. return;
  671. // _startMarker is an hidden menuseparator that lives before places nodes.
  672. aPopup._startMarker = document.createElement("menuseparator");
  673. aPopup._startMarker.hidden = true;
  674. aPopup.insertBefore(aPopup._startMarker, aPopup.firstChild);
  675. // _endMarker is an hidden menuseparator that lives after places nodes.
  676. aPopup._endMarker = document.createElement("menuseparator");
  677. aPopup._endMarker.hidden = true;
  678. aPopup.appendChild(aPopup._endMarker);
  679. // Move the markers to the right position.
  680. let firstNonStaticNodeFound = false;
  681. for (let i = 0; i < aPopup.childNodes.length; i++) {
  682. let child = aPopup.childNodes[i];
  683. // Menus that have static content at the end, but are initially empty,
  684. // use a special "builder" attribute to figure out where to start
  685. // inserting places nodes.
  686. if (child.getAttribute("builder") == "end") {
  687. aPopup.insertBefore(aPopup._endMarker, child);
  688. break;
  689. }
  690. if (child._placesNode && !firstNonStaticNodeFound) {
  691. firstNonStaticNodeFound = true;
  692. aPopup.insertBefore(aPopup._startMarker, child);
  693. }
  694. }
  695. if (!firstNonStaticNodeFound) {
  696. aPopup.insertBefore(aPopup._startMarker, aPopup._endMarker);
  697. }
  698. },
  699. _onPopupShowing: function(aEvent) {
  700. // Avoid handling popupshowing of inner views.
  701. let popup = aEvent.originalTarget;
  702. this._ensureMarkers(popup);
  703. // Remove any delayed element, see _cleanPopup for details.
  704. if ("_delayedRemovals" in popup) {
  705. while (popup._delayedRemovals.length > 0) {
  706. popup.removeChild(popup._delayedRemovals.shift());
  707. }
  708. }
  709. if (popup._placesNode && PlacesUIUtils.getViewForNode(popup) == this) {
  710. if (!popup._placesNode.containerOpen)
  711. popup._placesNode.containerOpen = true;
  712. if (!popup._built)
  713. this._rebuildPopup(popup);
  714. this._mayAddCommandsItems(popup);
  715. }
  716. },
  717. _addEventListeners:
  718. function(aObject, aEventNames, aCapturing) {
  719. for (let i = 0; i < aEventNames.length; i++) {
  720. aObject.addEventListener(aEventNames[i], this, aCapturing);
  721. }
  722. },
  723. _removeEventListeners:
  724. function(aObject, aEventNames, aCapturing) {
  725. for (let i = 0; i < aEventNames.length; i++) {
  726. aObject.removeEventListener(aEventNames[i], this, aCapturing);
  727. }
  728. },
  729. };
  730. function PlacesToolbar(aPlace) {
  731. let startTime = Date.now();
  732. // Add some smart getters for our elements.
  733. let thisView = this;
  734. [
  735. ["_viewElt", "PlacesToolbar"],
  736. ["_rootElt", "PlacesToolbarItems"],
  737. ["_dropIndicator", "PlacesToolbarDropIndicator"],
  738. ["_chevron", "PlacesChevron"],
  739. ["_chevronPopup", "PlacesChevronPopup"]
  740. ].forEach(function(elementGlobal) {
  741. let [name, id] = elementGlobal;
  742. thisView.__defineGetter__(name, function() {
  743. let element = document.getElementById(id);
  744. if (!element)
  745. return null;
  746. delete thisView[name];
  747. return thisView[name] = element;
  748. });
  749. });
  750. this._viewElt._placesView = this;
  751. this._addEventListeners(this._viewElt, this._cbEvents, false);
  752. this._addEventListeners(this._rootElt, ["popupshowing", "popuphidden"], true);
  753. this._addEventListeners(this._rootElt, ["overflow", "underflow"], true);
  754. this._addEventListeners(window, ["resize", "unload"], false);
  755. // If personal-bookmarks has been dragged to the tabs toolbar,
  756. // we have to track addition and removals of tabs, to properly
  757. // recalculate the available space for bookmarks.
  758. // TODO (bug 734730): Use a performant mutation listener when available.
  759. if (this._viewElt.parentNode.parentNode == document.getElementById("TabsToolbar")) {
  760. this._addEventListeners(gBrowser.tabContainer, ["TabOpen", "TabClose"], false);
  761. }
  762. PlacesViewBase.call(this, aPlace);
  763. }
  764. PlacesToolbar.prototype = {
  765. __proto__: PlacesViewBase.prototype,
  766. _cbEvents: ["dragstart", "dragover", "dragexit", "dragend", "drop",
  767. "mousemove", "mouseover", "mouseout"],
  768. QueryInterface: function(aIID) {
  769. if (aIID.equals(Ci.nsIDOMEventListener) ||
  770. aIID.equals(Ci.nsITimerCallback))
  771. return this;
  772. return PlacesViewBase.prototype.QueryInterface.apply(this, arguments);
  773. },
  774. uninit: function() {
  775. this._removeEventListeners(this._viewElt, this._cbEvents, false);
  776. this._removeEventListeners(this._rootElt, ["popupshowing", "popuphidden"],
  777. true);
  778. this._removeEventListeners(this._rootElt, ["overflow", "underflow"], true);
  779. this._removeEventListeners(window, ["resize", "unload"], false);
  780. this._removeEventListeners(gBrowser.tabContainer, ["TabOpen", "TabClose"], false);
  781. PlacesViewBase.prototype.uninit.apply(this, arguments);
  782. },
  783. _openedMenuButton: null,
  784. _allowPopupShowing: true,
  785. _rebuild: function() {
  786. // Clear out references to existing nodes, since they will be removed
  787. // and re-added.
  788. if (this._overFolder.elt)
  789. this._clearOverFolder();
  790. this._openedMenuButton = null;
  791. while (this._rootElt.hasChildNodes()) {
  792. this._rootElt.removeChild(this._rootElt.firstChild);
  793. }
  794. let cc = this._resultNode.childCount;
  795. for (let i = 0; i < cc; ++i) {
  796. this._insertNewItem(this._resultNode.getChild(i), null);
  797. }
  798. if (this._chevronPopup.hasAttribute("type")) {
  799. // Chevron has already been initialized, but since we are forcing
  800. // a rebuild of the toolbar, it has to be rebuilt.
  801. // Otherwise, it will be initialized when the toolbar overflows.
  802. this._chevronPopup.place = this.place;
  803. }
  804. },
  805. _insertNewItem:
  806. function(aChild, aBefore) {
  807. this._domNodes.delete(aChild);
  808. let type = aChild.type;
  809. let button;
  810. if (type == Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR) {
  811. button = document.createElement("toolbarseparator");
  812. }
  813. else {
  814. button = document.createElement("toolbarbutton");
  815. button.className = "bookmark-item";
  816. button.setAttribute("label", aChild.title || "");
  817. let icon = aChild.icon;
  818. if (icon)
  819. button.setAttribute("image",
  820. PlacesUIUtils.getImageURLForResolution(window, icon));
  821. if (PlacesUtils.containerTypes.indexOf(type) != -1) {
  822. button.setAttribute("type", "menu");
  823. button.setAttribute("container", "true");
  824. if (PlacesUtils.nodeIsQuery(aChild)) {
  825. button.setAttribute("query", "true");
  826. if (PlacesUtils.nodeIsTagQuery(aChild))
  827. button.setAttribute("tagContainer", "true");
  828. }
  829. else if (PlacesUtils.nodeIsFolder(aChild)) {
  830. PlacesUtils.livemarks.getLivemark({ id: aChild.itemId })
  831. .then(aLivemark => {
  832. button.setAttribute("livemark", "true");
  833. this.controller.cacheLivemarkInfo(aChild, aLivemark);
  834. }, () => undefined);
  835. }
  836. let popup = document.createElement("menupopup");
  837. popup.setAttribute("placespopup", "true");
  838. button.appendChild(popup);
  839. popup._placesNode = PlacesUtils.asContainer(aChild);
  840. popup.setAttribute("context", "placesContext");
  841. this._domNodes.set(aChild, popup);
  842. }
  843. else if (PlacesUtils.nodeIsURI(aChild)) {
  844. button.setAttribute("scheme",
  845. PlacesUIUtils.guessUrlSchemeForUI(aChild.uri));
  846. }
  847. }
  848. button._placesNode = aChild;
  849. if (!this._domNodes.has(aChild))
  850. this._domNodes.set(aChild, button);
  851. if (aBefore)
  852. this._rootElt.insertBefore(button, aBefore);
  853. else
  854. this._rootElt.appendChild(button);
  855. },
  856. _updateChevronPopupNodesVisibility:
  857. function() {
  858. for (let i = 0, node = this._chevronPopup._startMarker.nextSibling;
  859. node != this._chevronPopup._endMarker;
  860. i++, node = node.nextSibling) {
  861. node.hidden = this._rootElt.childNodes[i].style.visibility != "hidden";
  862. }
  863. },
  864. _onChevronPopupShowing:
  865. function(aEvent) {
  866. // Handle popupshowing only for the chevron popup, not for nested ones.
  867. if (aEvent.target != this._chevronPopup)
  868. return;
  869. if (!this._chevron._placesView)
  870. this._chevron._placesView = new PlacesMenu(aEvent, this.place);
  871. this._updateChevronPopupNodesVisibility();
  872. },
  873. handleEvent: function(aEvent) {
  874. switch (aEvent.type) {
  875. case "unload":
  876. this.uninit();
  877. break;
  878. case "resize":
  879. // This handler updates nodes visibility in both the toolbar
  880. // and the chevron popup when a window resize does not change
  881. // the overflow status of the toolbar.
  882. this.updateChevron();
  883. break;
  884. case "overflow":
  885. if (aEvent.target != aEvent.currentTarget)
  886. return;
  887. // Ignore purely vertical overflows.
  888. if (aEvent.detail == 0)
  889. return;
  890. // Attach the popup binding to the chevron popup if it has not yet
  891. // been initialized.
  892. if (!this._chevronPopup.hasAttribute("type")) {
  893. this._chevronPopup.setAttribute("place", this.place);
  894. this._chevronPopup.setAttribute("type", "places");
  895. }
  896. this._chevron.collapsed = false;
  897. this.updateChevron();
  898. break;
  899. case "underflow":
  900. if (aEvent.target != aEvent.currentTarget)
  901. return;
  902. // Ignore purely vertical underflows.
  903. if (aEvent.detail == 0)
  904. return;
  905. this.updateChevron();
  906. this._chevron.collapsed = true;
  907. break;
  908. case "TabOpen":
  909. case "TabClose":
  910. this.updateChevron();
  911. break;
  912. case "dragstart":
  913. this._onDragStart(aEvent);
  914. break;
  915. case "dragover":
  916. this._onDragOver(aEvent);
  917. break;
  918. case "dragexit":
  919. this._onDragExit(aEvent);
  920. break;
  921. case "dragend":
  922. this._onDragEnd(aEvent);
  923. break;
  924. case "drop":
  925. this._onDrop(aEvent);
  926. break;
  927. case "mouseover":
  928. this._onMouseOver(aEvent);
  929. break;
  930. case "mousemove":
  931. this._onMouseMove(aEvent);
  932. break;
  933. case "mouseout":
  934. this._onMouseOut(aEvent);
  935. break;
  936. case "popupshowing":
  937. this._onPopupShowing(aEvent);
  938. break;
  939. case "popuphidden":
  940. this._onPopupHidden(aEvent);
  941. break;
  942. default:
  943. throw "Trying to handle unexpected event.";
  944. }
  945. },
  946. updateChevron: function() {
  947. // If the chevron is collapsed there's nothing to update.
  948. if (this._chevron.collapsed)
  949. return;
  950. // Update the chevron on a timer. This will avoid repeated work when
  951. // lot of changes happen in a small timeframe.
  952. if (this._updateChevronTimer)
  953. this._updateChevronTimer.cancel();
  954. this._updateChevronTimer = this._setTimer(100);
  955. },
  956. _updateChevronTimerCallback: function() {
  957. let scrollRect = this._rootElt.getBoundingClientRect();
  958. let childOverflowed = false;
  959. for (let i = 0; i < this._rootElt.childNodes.length; i++) {
  960. let child = this._rootElt.childNodes[i];
  961. // Once a child overflows, all the next ones will.
  962. if (!childOverflowed) {
  963. let childRect = child.getBoundingClientRect();
  964. childOverflowed = this.isRTL ? (childRect.left < scrollRect.left)
  965. : (childRect.right > scrollRect.right);
  966. }
  967. child.style.visibility = childOverflowed ? "hidden" : "visible";
  968. }
  969. // We rebuild the chevron on popupShowing, so if it is open
  970. // we must update it.
  971. if (this._chevron.open)
  972. this._updateChevronPopupNodesVisibility();
  973. },
  974. nodeInserted:
  975. function(aParentPlacesNode, aPlacesNode, aIndex) {
  976. let parentElt = this._getDOMNodeForPlacesNode(aParentPlacesNode);
  977. if (parentElt == this._rootElt) {
  978. let children = this._rootElt.childNodes;
  979. this._insertNewItem(aPlacesNode,
  980. aIndex < children.length ? children[aIndex] : null);
  981. this.updateChevron();
  982. return;
  983. }
  984. PlacesViewBase.prototype.nodeInserted.apply(this, arguments);
  985. },
  986. nodeRemoved:
  987. function(aParentPlacesNode, aPlacesNode, aIndex) {
  988. let parentElt = this._getDOMNodeForPlacesNode(aParentPlacesNode);
  989. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  990. // Here we need the <menu>.
  991. if (elt.localName == "menupopup")
  992. elt = elt.parentNode;
  993. if (parentElt == this._rootElt) {
  994. this._removeChild(elt);
  995. this.updateChevron();
  996. return;
  997. }
  998. PlacesViewBase.prototype.nodeRemoved.apply(this, arguments);
  999. },
  1000. nodeMoved:
  1001. function(aPlacesNode,
  1002. aOldParentPlacesNode, aOldIndex,
  1003. aNewParentPlacesNode, aNewIndex) {
  1004. let parentElt = this._getDOMNodeForPlacesNode(aNewParentPlacesNode);
  1005. if (parentElt == this._rootElt) {
  1006. // Container is on the toolbar.
  1007. // Move the element.
  1008. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  1009. // Here we need the <menu>.
  1010. if (elt.localName == "menupopup")
  1011. elt = elt.parentNode;
  1012. this._removeChild(elt);
  1013. this._rootElt.insertBefore(elt, this._rootElt.childNodes[aNewIndex]);
  1014. // The chevron view may get nodeMoved after the toolbar. In such a case,
  1015. // we should ensure (by manually swapping menuitems) that the actual nodes
  1016. // are in the final position before updateChevron tries to updates their
  1017. // visibility, or the chevron may go out of sync.
  1018. // Luckily updateChevron runs on a timer, so, by the time it updates
  1019. // nodes, the menu has already handled the notification.
  1020. this.updateChevron();
  1021. return;
  1022. }
  1023. PlacesViewBase.prototype.nodeMoved.apply(this, arguments);
  1024. },
  1025. nodeAnnotationChanged:
  1026. function(aPlacesNode, aAnno) {
  1027. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  1028. if (elt == this._rootElt)
  1029. return;
  1030. // We're notified for the menupopup, not the containing toolbarbutton.
  1031. if (elt.localName == "menupopup")
  1032. elt = elt.parentNode;
  1033. if (elt.parentNode == this._rootElt) {
  1034. // Node is on the toolbar.
  1035. // All livemarks have a feedURI, so use it as our indicator.
  1036. if (aAnno == PlacesUtils.LMANNO_FEEDURI) {
  1037. elt.setAttribute("livemark", true);
  1038. PlacesUtils.livemarks.getLivemark({ id: aPlacesNode.itemId })
  1039. .then(aLivemark => {
  1040. this.controller.cacheLivemarkInfo(aPlacesNode, aLivemark);
  1041. this.invalidateContainer(aPlacesNode);
  1042. }, Components.utils.reportError);
  1043. }
  1044. }
  1045. else {
  1046. // Node is in a submenu.
  1047. PlacesViewBase.prototype.nodeAnnotationChanged.apply(this, arguments);
  1048. }
  1049. },
  1050. nodeTitleChanged: function(aPlacesNode, aNewTitle) {
  1051. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  1052. // There's no UI representation for the root node, thus there's
  1053. // nothing to be done when the title changes.
  1054. if (elt == this._rootElt)
  1055. return;
  1056. PlacesViewBase.prototype.nodeTitleChanged.apply(this, arguments);
  1057. // Here we need the <menu>.
  1058. if (elt.localName == "menupopup")
  1059. elt = elt.parentNode;
  1060. if (elt.parentNode == this._rootElt) {
  1061. // Node is on the toolbar
  1062. this.updateChevron();
  1063. }
  1064. },
  1065. invalidateContainer: function(aPlacesNode) {
  1066. let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
  1067. if (elt == this._rootElt) {
  1068. // Container is the toolbar itself.
  1069. this._rebuild();
  1070. return;
  1071. }
  1072. PlacesViewBase.prototype.invalidateContainer.apply(this, arguments);
  1073. },
  1074. _overFolder: { elt: null,
  1075. openTimer: null,
  1076. hoverTime: 350,
  1077. closeTimer: null },
  1078. _clearOverFolder: function() {
  1079. // The mouse is no longer dragging over the stored menubutton.
  1080. // Close the menubutton, clear out drag styles, and clear all
  1081. // timers for opening/closing it.
  1082. if (this._overFolder.elt && this._overFolder.elt.lastChild) {
  1083. if (!this._overFolder.elt.lastChild.hasAttribute("dragover")) {
  1084. this._overFolder.elt.lastChild.hidePopup();
  1085. }
  1086. this._overFolder.elt.removeAttribute("dragover");
  1087. this._overFolder.elt = null;
  1088. }
  1089. if (this._overFolder.openTimer) {
  1090. this._overFolder.openTimer.cancel();
  1091. this._overFolder.openTimer = null;
  1092. }
  1093. if (this._overFolder.closeTimer) {
  1094. this._overFolder.closeTimer.cancel();
  1095. this._overFolder.closeTimer = null;
  1096. }
  1097. },
  1098. /**
  1099. * This function returns information about where to drop when dragging over
  1100. * the toolbar. The returned object has the following properties:
  1101. * - ip: the insertion point for the bookmarks service.
  1102. * - beforeIndex: child index to drop before, for the drop indicator.
  1103. * - folderElt: the folder to drop into, if applicable.
  1104. */
  1105. _getDropPoint: function(aEvent) {
  1106. let result = this.result;
  1107. if (!PlacesUtils.nodeIsFolder(this._resultNode))
  1108. return null;
  1109. let dropPoint = { ip: null, beforeIndex: null, folderElt: null };
  1110. let elt = aEvent.target;
  1111. if (elt._placesNode && elt != this._rootElt &&
  1112. elt.localName != "menupopup") {
  1113. let eltRect = elt.getBoundingClientRect();
  1114. let eltIndex = Array.indexOf(this._rootElt.childNodes, elt);
  1115. if (PlacesUtils.nodeIsFolder(elt._placesNode) &&
  1116. !PlacesUIUtils.isContentsReadOnly(elt._placesNode)) {
  1117. // This is a folder.
  1118. // If we are in the middle of it, drop inside it.
  1119. // Otherwise, drop before it, with regards to RTL mode.
  1120. let threshold = eltRect.width * 0.25;
  1121. if (this.isRTL ? (aEvent.clientX > eltRect.right - threshold)
  1122. : (aEvent.clientX < eltRect.left + threshold)) {
  1123. // Drop before this folder.
  1124. dropPoint.ip =
  1125. new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
  1126. eltIndex, Ci.nsITreeView.DROP_BEFORE);
  1127. dropPoint.beforeIndex = eltIndex;
  1128. }
  1129. else if (this.isRTL ? (aEvent.clientX > eltRect.left + threshold)
  1130. : (aEvent.clientX < eltRect.right - threshold)) {
  1131. // Drop inside this folder.
  1132. dropPoint.ip =
  1133. new InsertionPoint(PlacesUtils.getConcreteItemId(elt._placesNode),
  1134. -1, Ci.nsITreeView.DROP_ON,
  1135. PlacesUtils.nodeIsTagQuery(elt._placesNode));
  1136. dropPoint.beforeIndex = eltIndex;
  1137. dropPoint.folderElt = elt;
  1138. }
  1139. else {
  1140. // Drop after this folder.
  1141. let beforeIndex =
  1142. (eltIndex == this._rootElt.childNodes.length - 1) ?
  1143. -1 : eltIndex + 1;
  1144. dropPoint.ip =
  1145. new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
  1146. beforeIndex, Ci.nsITreeView.DROP_BEFORE);
  1147. dropPoint.beforeIndex = beforeIndex;
  1148. }
  1149. }
  1150. else {
  1151. // This is a non-folder node or a read-only folder.
  1152. // Drop before it with regards to RTL mode.
  1153. let threshold = eltRect.width * 0.5;
  1154. if (this.isRTL ? (aEvent.clientX > eltRect.left + threshold)
  1155. : (aEvent.clientX < eltRect.left + threshold)) {
  1156. // Drop before this bookmark.
  1157. dropPoint.ip =
  1158. new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
  1159. eltIndex, Ci.nsITreeView.DROP_BEFORE);
  1160. dropPoint.beforeIndex = eltIndex;
  1161. }
  1162. else {
  1163. // Drop after this bookmark.
  1164. let beforeIndex =
  1165. eltIndex == this._rootElt.childNodes.length - 1 ?
  1166. -1 : eltIndex + 1;
  1167. dropPoint.ip =
  1168. new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
  1169. beforeIndex, Ci.nsITreeView.DROP_BEFORE);
  1170. dropPoint.beforeIndex = beforeIndex;
  1171. }
  1172. }
  1173. }
  1174. else {
  1175. // We are most likely dragging on the empty area of the
  1176. // toolbar, we should drop after the last node.
  1177. dropPoint.ip =
  1178. new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
  1179. -1, Ci.nsITreeView.DROP_BEFORE);
  1180. dropPoint.beforeIndex = -1;
  1181. }
  1182. return dropPoint;
  1183. },
  1184. _setTimer: function(aTime) {
  1185. let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  1186. timer.initWithCallback(this, aTime, timer.TYPE_ONE_SHOT);
  1187. return timer;
  1188. },
  1189. notify: function(aTimer) {
  1190. if (aTimer == this._updateChevronTimer) {
  1191. this._updateChevronTimer = null;
  1192. this._updateChevronTimerCallback();
  1193. }
  1194. // * Timer to turn off indicator bar.
  1195. else if (aTimer == this._ibTimer) {
  1196. this._dropIndicator.collapsed = true;
  1197. this._ibTimer = null;
  1198. }
  1199. // * Timer to open a menubutton that's being dragged over.
  1200. else if (aTimer == this._overFolder.openTimer) {
  1201. // Set the autoopen attribute on the folder's menupopup so that
  1202. // the menu will automatically close when the mouse drags off of it.
  1203. this._overFolder.elt.lastChild.setAttribute("autoopened", "true");
  1204. this._overFolder.elt.open = true;
  1205. this._overFolder.openTimer = null;
  1206. }
  1207. // * Timer to close a menubutton that's been dragged off of.
  1208. else if (aTimer == this._overFolder.closeTimer) {
  1209. // Close the menubutton if we are not dragging over it or one of
  1210. // its children. The autoopened attribute will let the menu know to
  1211. // close later if the menu is still being dragged over.
  1212. let currentPlacesNode = PlacesControllerDragHelper.currentDropTarget;
  1213. let inHierarchy = false;
  1214. while (currentPlacesNode) {
  1215. if (currentPlacesNode == this._rootElt) {
  1216. inHierarchy = true;
  1217. break;
  1218. }
  1219. currentPlacesNode = currentPlacesNode.parentNode;
  1220. }
  1221. // The _clearOverFolder() function will close the menu for
  1222. // _overFolder.elt. So null it out if we don't want to close it.
  1223. if (inHierarchy)
  1224. this._overFolder.elt = null;
  1225. // Clear out the folder and all associated timers.
  1226. this._clearOverFolder();
  1227. }
  1228. },
  1229. _onMouseOver: function(aEvent) {
  1230. let button = aEvent.target;
  1231. if (button.parentNode == this._rootElt && button._placesNode &&
  1232. PlacesUtils.nodeIsURI(button._placesNode))
  1233. window.XULBrowserWindow.setOverLink(aEvent.target._placesNode.uri, null);
  1234. },
  1235. _onMouseOut: function(aEvent) {
  1236. window.XULBrowserWindow.setOverLink("", null);
  1237. },
  1238. _cleanupDragDetails: function() {
  1239. // Called on dragend and drop.
  1240. PlacesControllerDragHelper.currentDropTarget = null;
  1241. this._draggedElt = null;
  1242. if (this._ibTimer)
  1243. this._ibTimer.cancel();
  1244. this._dropIndicator.collapsed = true;
  1245. },
  1246. _onDragStart: function(aEvent) {
  1247. // Sub menus have their own d&d handlers.
  1248. let draggedElt = aEvent.target;
  1249. if (draggedElt.parentNode != this._rootElt || !draggedElt._placesNode)
  1250. return;
  1251. if (draggedElt.localName == "toolbarbutton" &&
  1252. draggedElt.getAttribute("type") == "menu") {
  1253. // If the drag gesture on a container is toward down we open instead
  1254. // of dragging.
  1255. let translateY = this._cachedMouseMoveEvent.clientY - aEvent.clientY;
  1256. let translateX = this._cachedMouseMoveEvent.clientX - aEvent.clientX;
  1257. if ((translateY) >= Math.abs(translateX/2)) {
  1258. // Don't start the drag.
  1259. aEvent.preventDefault();
  1260. // Open the menu.
  1261. draggedElt.open = true;
  1262. return;
  1263. }
  1264. // If the menu is open, close it.
  1265. if (draggedElt.open) {
  1266. draggedElt.lastChild.hidePopup();
  1267. draggedElt.open = false;
  1268. }
  1269. }
  1270. // Activate the view and cache the dragged element.
  1271. this._draggedElt = draggedElt._placesNode;
  1272. this._rootElt.focus();
  1273. this._controller.setDataTransfer(aEvent);
  1274. aEvent.stopPropagation();
  1275. },
  1276. _onDragOver: function(aEvent) {
  1277. // Cache the dataTransfer
  1278. PlacesControllerDragHelper.currentDropTarget = aEvent.target;
  1279. let dt = aEvent.dataTransfer;
  1280. let dropPoint = this._getDropPoint(aEvent);
  1281. if (!dropPoint || !dropPoint.ip ||
  1282. !PlacesControllerDragHelper.canDrop(dropPoint.ip, dt)) {
  1283. this._dropIndicator.collapsed = true;
  1284. aEvent.stopPropagation();
  1285. return;
  1286. }
  1287. if (this._ibTimer) {
  1288. this._ibTimer.cancel();
  1289. this._ibTimer = null;
  1290. }
  1291. if (dropPoint.folderElt || aEvent.originalTarget == this._chevron) {
  1292. // Dropping over a menubutton or chevron button.
  1293. // Set styles and timer to open relative menupopup.
  1294. let overElt = dropPoint.folderElt || this._chevron;
  1295. if (this._overFolder.elt != overElt) {
  1296. this._clearOverFolder();
  1297. this._overFolder.elt = overElt;
  1298. this._overFolder.openTimer = this._setTimer(this._overFolder.hoverTime);
  1299. }
  1300. if (!this._overFolder.elt.hasAttribute("dragover"))
  1301. this._overFolder.elt.setAttribute("dragover", "true");
  1302. this._dropIndicator.collapsed = true;
  1303. }
  1304. else {
  1305. // Dragging over a normal toolbarbutton,
  1306. // show indicator bar and move it to the appropriate drop point.
  1307. let ind = this._dropIndicator;
  1308. let halfInd = ind.clientWidth / 2;
  1309. let translateX;
  1310. if (this.isRTL) {
  1311. halfInd = Math.ceil(halfInd);
  1312. translateX = 0 - this._rootElt.getBoundingClientRect().right - halfInd;
  1313. if (this._rootElt.firstChild) {
  1314. if (dropPoint.beforeIndex == -1)
  1315. translateX += this._rootElt.lastChild.getBoundingClientRect().left;
  1316. else {
  1317. translateX += this._rootElt.childNodes[dropPoint.beforeIndex]
  1318. .getBoundingClientRect().right;
  1319. }
  1320. }
  1321. }
  1322. else {
  1323. halfInd = Math.floor(halfInd);
  1324. translateX = 0 - this._rootElt.getBoundingClientRect().left +
  1325. halfInd;
  1326. if (this._rootElt.firstChild) {
  1327. if (dropPoint.beforeIndex == -1)
  1328. translateX += this._rootElt.lastChild.getBoundingClientRect().right;
  1329. else {
  1330. translateX += this._rootElt.childNodes[dropPoint.beforeIndex]
  1331. .getBoundingClientRect().left;
  1332. }
  1333. }
  1334. }
  1335. ind.style.transform = "translate(" + Math.round(translateX) + "px)";
  1336. ind.style.MozMarginStart = (-ind.clientWidth) + "px";
  1337. ind.collapsed = false;
  1338. // Clear out old folder information.
  1339. this._clearOverFolder();
  1340. }
  1341. aEvent.preventDefault();
  1342. aEvent.stopPropagation();
  1343. },
  1344. _onDrop: function(aEvent) {
  1345. PlacesControllerDragHelper.currentDropTarget = aEvent.target;
  1346. let dropPoint = this._getDropPoint(aEvent);
  1347. if (dropPoint && dropPoint.ip) {
  1348. PlacesControllerDragHelper.onDrop(dropPoint.ip, aEvent.dataTransfer)
  1349. aEvent.preventDefault();
  1350. }
  1351. this._cleanupDragDetails();
  1352. aEvent.stopPropagation();
  1353. },
  1354. _onDragExit: function(aEvent) {
  1355. PlacesControllerDragHelper.currentDropTarget = null;
  1356. // Set timer to turn off indicator bar (if we turn it off
  1357. // here, dragenter might be called immediately after, creating
  1358. // flicker).
  1359. if (this._ibTimer)
  1360. this._ibTimer.cancel();
  1361. this._ibTimer = this._setTimer(10);
  1362. // If we hovered over a folder, close it now.
  1363. if (this._overFolder.elt)
  1364. this._overFolder.closeTimer = this._setTimer(this._overFolder.hoverTime);
  1365. },
  1366. _onDragEnd: function(aEvent) {
  1367. this._cleanupDragDetails();
  1368. },
  1369. _onPopupShowing: function(aEvent) {
  1370. if (!this._allowPopupShowing) {
  1371. this._allowPopupShowing = true;
  1372. aEvent.preventDefault();
  1373. return;
  1374. }
  1375. let parent = aEvent.target.parentNode;
  1376. if (parent.localName == "toolbarbutton")
  1377. this._openedMenuButton = parent;
  1378. PlacesViewBase.prototype._onPopupShowing.apply(this, arguments);
  1379. },
  1380. _onPopupHidden: function(aEvent) {
  1381. let popup = aEvent.target;
  1382. let placesNode = popup._placesNode;
  1383. // Avoid handling popuphidden of inner views
  1384. if (placesNode && PlacesUIUtils.getViewForNode(popup) == this) {
  1385. // UI performance: folder queries are cheap, keep the resultnode open
  1386. // so we don't rebuild its contents whenever the popup is reopened.
  1387. // Though, we want to always close feed containers so their expiration
  1388. // status will be checked at next opening.
  1389. if (!PlacesUtils.nodeIsFolder(placesNode) ||
  1390. this.controller.hasCachedLivemarkInfo(placesNode)) {
  1391. placesNode.containerOpen = false;
  1392. }
  1393. }
  1394. let parent = popup.parentNode;
  1395. if (parent.localName == "toolbarbutton") {
  1396. this._openedMenuButton = null;
  1397. // Clear the dragover attribute if present, if we are dragging into a
  1398. // folder in the hierachy of current opened popup we don't clear
  1399. // this attribute on clearOverFolder. See Notify for closeTimer.
  1400. if (parent.hasAttribute("dragover"))
  1401. parent.removeAttribute("dragover");
  1402. }
  1403. },
  1404. _onMouseMove: function(aEvent) {
  1405. // Used in dragStart to prevent dragging folders when dragging down.
  1406. this._cachedMouseMoveEvent = aEvent;
  1407. if (this._openedMenuButton == null ||
  1408. PlacesControllerDragHelper.getSession())
  1409. return;
  1410. let target = aEvent.originalTarget;
  1411. if (this._openedMenuButton != target &&
  1412. target.localName == "toolbarbutton" &&
  1413. target.type == "menu") {
  1414. this._openedMenuButton.open = false;
  1415. target.open = true;
  1416. }
  1417. }
  1418. };
  1419. /**
  1420. * View for Places menus. This object should be created during the first
  1421. * popupshowing that's dispatched on the menu.
  1422. */
  1423. function PlacesMenu(aPopupShowingEvent, aPlace) {
  1424. this._rootElt = aPopupShowingEvent.target; // <menupopup>
  1425. this._viewElt = this._rootElt.parentNode; // <menu>
  1426. this._viewElt._placesView = this;
  1427. this._addEventListeners(this._rootElt, ["popupshowing", "popuphidden"], true);
  1428. this._addEventListeners(window, ["unload"], false);
  1429. PlacesViewBase.call(this, aPlace);
  1430. this._onPopupShowing(aPopupShowingEvent);
  1431. }
  1432. PlacesMenu.prototype = {
  1433. __proto__: PlacesViewBase.prototype,
  1434. QueryInterface: function(aIID) {
  1435. if (aIID.equals(Ci.nsIDOMEventListener))
  1436. return this;
  1437. return PlacesViewBase.prototype.QueryInterface.apply(this, arguments);
  1438. },
  1439. _removeChild: function(aChild) {
  1440. PlacesViewBase.prototype._removeChild.apply(this, arguments);
  1441. },
  1442. uninit: function() {
  1443. this._removeEventListeners(this._rootElt, ["popupshowing", "popuphidden"],
  1444. true);
  1445. this._removeEventListeners(window, ["unload"], false);
  1446. PlacesViewBase.prototype.uninit.apply(this, arguments);
  1447. },
  1448. handleEvent: function(aEvent) {
  1449. switch (aEvent.type) {
  1450. case "unload":
  1451. this.uninit();
  1452. break;
  1453. case "popupshowing":
  1454. this._onPopupShowing(aEvent);
  1455. break;
  1456. case "popuphidden":
  1457. this._onPopupHidden(aEvent);
  1458. break;
  1459. }
  1460. },
  1461. _onPopupHidden: function(aEvent) {
  1462. // Avoid handling popuphidden of inner views.
  1463. let popup = aEvent.originalTarget;
  1464. let placesNode = popup._placesNode;
  1465. if (!placesNode || PlacesUIUtils.getViewForNode(popup) != this)
  1466. return;
  1467. // UI performance: folder queries are cheap, keep the resultnode open
  1468. // so we don't rebuild its contents whenever the popup is reopened.
  1469. // Though, we want to always close feed containers so their expiration
  1470. // status will be checked at next opening.
  1471. if (!PlacesUtils.nodeIsFolder(placesNode) ||
  1472. this.controller.hasCachedLivemarkInfo(placesNode))
  1473. placesNode.containerOpen = false;
  1474. // The autoopened attribute is set for folders which have been
  1475. // automatically opened when dragged over. Turn off this attribute
  1476. // when the folder closes because it is no longer applicable.
  1477. popup.removeAttribute("autoopened");
  1478. popup.removeAttribute("dragstart");
  1479. }
  1480. };