sidebar.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. var Services = require("Services");
  6. var {Task} = require("devtools/shared/task");
  7. var EventEmitter = require("devtools/shared/event-emitter");
  8. var Telemetry = require("devtools/client/shared/telemetry");
  9. const {LocalizationHelper} = require("devtools/shared/l10n");
  10. const L10N = new LocalizationHelper("devtools/client/locales/toolbox.properties");
  11. const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  12. /**
  13. * ToolSidebar provides methods to register tabs in the sidebar.
  14. * It's assumed that the sidebar contains a xul:tabbox.
  15. * Typically, you'll want the tabbox parameter to be a XUL tabbox like this:
  16. *
  17. * <tabbox id="inspector-sidebar" handleCtrlTab="false" class="devtools-sidebar-tabs">
  18. * <tabs/>
  19. * <tabpanels flex="1"/>
  20. * </tabbox>
  21. *
  22. * The ToolSidebar API has a method to add new tabs, so the tabs and tabpanels
  23. * nodes can be empty. But they can also already contain items before the
  24. * ToolSidebar is created.
  25. *
  26. * Tabs added through the addTab method are only identified by an ID and a URL
  27. * which is used as the href of an iframe node that is inserted in the newly
  28. * created tabpanel.
  29. * Tabs already present before the ToolSidebar is created may contain anything.
  30. * However, these tabs must have ID attributes if it is required for the various
  31. * methods that accept an ID as argument to work here.
  32. *
  33. * @param {Node} tabbox
  34. * <tabbox> node;
  35. * @param {ToolPanel} panel
  36. * Related ToolPanel instance;
  37. * @param {String} uid
  38. * Unique ID
  39. * @param {Object} options
  40. * - hideTabstripe: Should the tabs be hidden. Defaults to false
  41. * - showAllTabsMenu: Should a drop-down menu be displayed in case tabs
  42. * become hidden. Defaults to false.
  43. * - disableTelemetry: By default, switching tabs on and off in the sidebar
  44. * will record tool usage in telemetry, pass this option to true to avoid it.
  45. *
  46. * Events raised:
  47. * - new-tab-registered : After a tab has been added via addTab. The tab ID
  48. * is passed with the event. This however, is raised before the tab iframe
  49. * is fully loaded.
  50. * - <tabid>-ready : After the tab iframe has been loaded
  51. * - <tabid>-selected : After tab <tabid> was selected
  52. * - select : Same as above, but for any tab, the ID is passed with the event
  53. * - <tabid>-unselected : After tab <tabid> is unselected
  54. */
  55. function ToolSidebar(tabbox, panel, uid, options = {}) {
  56. EventEmitter.decorate(this);
  57. this._tabbox = tabbox;
  58. this._uid = uid;
  59. this._panelDoc = this._tabbox.ownerDocument;
  60. this._toolPanel = panel;
  61. this._options = options;
  62. this._onTabBoxOverflow = this._onTabBoxOverflow.bind(this);
  63. this._onTabBoxUnderflow = this._onTabBoxUnderflow.bind(this);
  64. try {
  65. this._width = Services.prefs.getIntPref("devtools.toolsidebar-width." + this._uid);
  66. } catch (e) {}
  67. if (!options.disableTelemetry) {
  68. this._telemetry = new Telemetry();
  69. }
  70. this._tabbox.tabpanels.addEventListener("select", this, true);
  71. this._tabs = new Map();
  72. // Check for existing tabs in the DOM and add them.
  73. this.addExistingTabs();
  74. if (this._options.hideTabstripe) {
  75. this._tabbox.setAttribute("hidetabs", "true");
  76. }
  77. if (this._options.showAllTabsMenu) {
  78. this.addAllTabsMenu();
  79. }
  80. this._toolPanel.emit("sidebar-created", this);
  81. }
  82. exports.ToolSidebar = ToolSidebar;
  83. ToolSidebar.prototype = {
  84. TAB_ID_PREFIX: "sidebar-tab-",
  85. TABPANEL_ID_PREFIX: "sidebar-panel-",
  86. /**
  87. * Add a "…" button at the end of the tabstripe that toggles a dropdown menu
  88. * containing the list of all tabs if any become hidden due to lack of room.
  89. *
  90. * If the ToolSidebar was created with the "showAllTabsMenu" option set to
  91. * true, this is already done automatically. If not, you may call this
  92. * function at any time to add the menu.
  93. */
  94. addAllTabsMenu: function () {
  95. if (this._allTabsBtn) {
  96. return;
  97. }
  98. let tabs = this._tabbox.tabs;
  99. // Create a container and insert it first in the tabbox
  100. let allTabsContainer = this._panelDoc.createElementNS(XULNS, "stack");
  101. this._tabbox.insertBefore(allTabsContainer, tabs);
  102. // Move the tabs inside and make them flex
  103. allTabsContainer.appendChild(tabs);
  104. tabs.setAttribute("flex", "1");
  105. // Create the dropdown menu next to the tabs
  106. this._allTabsBtn = this._panelDoc.createElementNS(XULNS, "toolbarbutton");
  107. this._allTabsBtn.setAttribute("class", "devtools-sidebar-alltabs");
  108. this._allTabsBtn.setAttribute("end", "0");
  109. this._allTabsBtn.setAttribute("top", "0");
  110. this._allTabsBtn.setAttribute("width", "15");
  111. this._allTabsBtn.setAttribute("type", "menu");
  112. this._allTabsBtn.setAttribute("tooltiptext",
  113. L10N.getStr("sidebar.showAllTabs.tooltip"));
  114. this._allTabsBtn.setAttribute("hidden", "true");
  115. allTabsContainer.appendChild(this._allTabsBtn);
  116. let menuPopup = this._panelDoc.createElementNS(XULNS, "menupopup");
  117. this._allTabsBtn.appendChild(menuPopup);
  118. // Listening to tabs overflow event to toggle the alltabs button
  119. tabs.addEventListener("overflow", this._onTabBoxOverflow, false);
  120. tabs.addEventListener("underflow", this._onTabBoxUnderflow, false);
  121. // Add menuitems to the alltabs menu if there are already tabs in the
  122. // sidebar
  123. for (let [id, tab] of this._tabs) {
  124. let item = this._addItemToAllTabsMenu(id, tab, {
  125. selected: tab.hasAttribute("selected")
  126. });
  127. if (tab.hidden) {
  128. item.hidden = true;
  129. }
  130. }
  131. },
  132. removeAllTabsMenu: function () {
  133. if (!this._allTabsBtn) {
  134. return;
  135. }
  136. let tabs = this._tabbox.tabs;
  137. tabs.removeEventListener("overflow", this._onTabBoxOverflow, false);
  138. tabs.removeEventListener("underflow", this._onTabBoxUnderflow, false);
  139. // Moving back the tabs as a first child of the tabbox
  140. this._tabbox.insertBefore(tabs, this._tabbox.tabpanels);
  141. this._tabbox.querySelector("stack").remove();
  142. this._allTabsBtn = null;
  143. },
  144. _onTabBoxOverflow: function () {
  145. this._allTabsBtn.removeAttribute("hidden");
  146. },
  147. _onTabBoxUnderflow: function () {
  148. this._allTabsBtn.setAttribute("hidden", "true");
  149. },
  150. /**
  151. * Add an item in the allTabs menu for a given tab.
  152. */
  153. _addItemToAllTabsMenu: function (id, tab, options) {
  154. if (!this._allTabsBtn) {
  155. return;
  156. }
  157. let item = this._panelDoc.createElementNS(XULNS, "menuitem");
  158. let idPrefix = "sidebar-alltabs-item-";
  159. item.setAttribute("id", idPrefix + id);
  160. item.setAttribute("label", tab.getAttribute("label"));
  161. item.setAttribute("type", "checkbox");
  162. if (options.selected) {
  163. item.setAttribute("checked", true);
  164. }
  165. // The auto-checking of menuitems in this menu doesn't work, so let's do
  166. // it manually
  167. item.setAttribute("autocheck", false);
  168. let menu = this._allTabsBtn.querySelector("menupopup");
  169. if (options.insertBefore) {
  170. let referenceItem = menu.querySelector(`#${idPrefix}${options.insertBefore}`);
  171. menu.insertBefore(item, referenceItem);
  172. } else {
  173. menu.appendChild(item);
  174. }
  175. item.addEventListener("click", () => {
  176. this._tabbox.selectedTab = tab;
  177. }, false);
  178. tab.allTabsMenuItem = item;
  179. return item;
  180. },
  181. /**
  182. * Register a tab. A tab is a document.
  183. * The document must have a title, which will be used as the name of the tab.
  184. *
  185. * @param {string} id The unique id for this tab.
  186. * @param {string} url The URL of the document to load in this new tab.
  187. * @param {Object} options A set of options for this new tab:
  188. * - {Boolean} selected Set to true to make this new tab selected by default.
  189. * - {String} insertBefore By default, the new tab is appended at the end of the
  190. * tabbox, pass the ID of an existing tab to insert it before that tab instead.
  191. */
  192. addTab: function (id, url, options = {}) {
  193. let iframe = this._panelDoc.createElementNS(XULNS, "iframe");
  194. iframe.className = "iframe-" + id;
  195. iframe.setAttribute("flex", "1");
  196. iframe.setAttribute("src", url);
  197. iframe.tooltip = "aHTMLTooltip";
  198. // Creating the tab and adding it to the tabbox
  199. let tab = this._panelDoc.createElementNS(XULNS, "tab");
  200. tab.setAttribute("id", this.TAB_ID_PREFIX + id);
  201. tab.setAttribute("crop", "end");
  202. // Avoid showing "undefined" while the tab is loading
  203. tab.setAttribute("label", "");
  204. if (options.insertBefore) {
  205. let referenceTab = this.getTab(options.insertBefore);
  206. this._tabbox.tabs.insertBefore(tab, referenceTab);
  207. } else {
  208. this._tabbox.tabs.appendChild(tab);
  209. }
  210. // Add the tab to the allTabs menu if exists
  211. let allTabsItem = this._addItemToAllTabsMenu(id, tab, options);
  212. let onIFrameLoaded = (event) => {
  213. let doc = event.target;
  214. let win = doc.defaultView;
  215. tab.setAttribute("label", doc.title);
  216. if (allTabsItem) {
  217. allTabsItem.setAttribute("label", doc.title);
  218. }
  219. iframe.removeEventListener("load", onIFrameLoaded, true);
  220. if ("setPanel" in win) {
  221. win.setPanel(this._toolPanel, iframe);
  222. }
  223. this.emit(id + "-ready");
  224. };
  225. iframe.addEventListener("load", onIFrameLoaded, true);
  226. let tabpanel = this._panelDoc.createElementNS(XULNS, "tabpanel");
  227. tabpanel.setAttribute("id", this.TABPANEL_ID_PREFIX + id);
  228. tabpanel.appendChild(iframe);
  229. if (options.insertBefore) {
  230. let referenceTabpanel = this.getTabPanel(options.insertBefore);
  231. this._tabbox.tabpanels.insertBefore(tabpanel, referenceTabpanel);
  232. } else {
  233. this._tabbox.tabpanels.appendChild(tabpanel);
  234. }
  235. this._tooltip = this._panelDoc.createElementNS(XULNS, "tooltip");
  236. this._tooltip.id = "aHTMLTooltip";
  237. tabpanel.appendChild(this._tooltip);
  238. this._tooltip.page = true;
  239. tab.linkedPanel = this.TABPANEL_ID_PREFIX + id;
  240. // We store the index of this tab.
  241. this._tabs.set(id, tab);
  242. if (options.selected) {
  243. this._selectTabSoon(id);
  244. }
  245. this.emit("new-tab-registered", id);
  246. },
  247. untitledTabsIndex: 0,
  248. /**
  249. * Search for existing tabs in the markup that aren't know yet and add them.
  250. */
  251. addExistingTabs: function () {
  252. let knownTabs = [...this._tabs.values()];
  253. for (let tab of this._tabbox.tabs.querySelectorAll("tab")) {
  254. if (knownTabs.indexOf(tab) !== -1) {
  255. continue;
  256. }
  257. // Find an ID for this unknown tab
  258. let id = tab.getAttribute("id") || "untitled-tab-" + (this.untitledTabsIndex++);
  259. // If the existing tab contains the tab ID prefix, extract the ID of the
  260. // tab
  261. if (id.startsWith(this.TAB_ID_PREFIX)) {
  262. id = id.split(this.TAB_ID_PREFIX).pop();
  263. }
  264. // Register the tab
  265. this._tabs.set(id, tab);
  266. this.emit("new-tab-registered", id);
  267. }
  268. },
  269. /**
  270. * Remove an existing tab.
  271. * @param {String} tabId The ID of the tab that was used to register it, or
  272. * the tab id attribute value if the tab existed before the sidebar got created.
  273. * @param {String} tabPanelId Optional. If provided, this ID will be used
  274. * instead of the tabId to retrieve and remove the corresponding <tabpanel>
  275. */
  276. removeTab: Task.async(function* (tabId, tabPanelId) {
  277. // Remove the tab if it can be found
  278. let tab = this.getTab(tabId);
  279. if (!tab) {
  280. return;
  281. }
  282. let win = this.getWindowForTab(tabId);
  283. if (win && ("destroy" in win)) {
  284. yield win.destroy();
  285. }
  286. tab.remove();
  287. // Also remove the tabpanel
  288. let panel = this.getTabPanel(tabPanelId || tabId);
  289. if (panel) {
  290. panel.remove();
  291. }
  292. this._tabs.delete(tabId);
  293. this.emit("tab-unregistered", tabId);
  294. }),
  295. /**
  296. * Show or hide a specific tab.
  297. * @param {Boolean} isVisible True to show the tab/tabpanel, False to hide it.
  298. * @param {String} id The ID of the tab to be hidden.
  299. */
  300. toggleTab: function (isVisible, id) {
  301. // Toggle the tab.
  302. let tab = this.getTab(id);
  303. if (!tab) {
  304. return;
  305. }
  306. tab.hidden = !isVisible;
  307. // Toggle the item in the allTabs menu.
  308. if (this._allTabsBtn) {
  309. this._allTabsBtn.querySelector("#sidebar-alltabs-item-" + id).hidden = !isVisible;
  310. }
  311. },
  312. /**
  313. * Select a specific tab.
  314. */
  315. select: function (id) {
  316. let tab = this.getTab(id);
  317. if (tab) {
  318. this._tabbox.selectedTab = tab;
  319. }
  320. },
  321. /**
  322. * Hack required to select a tab right after it was created.
  323. *
  324. * @param {String} id
  325. * The sidebar tab id to select.
  326. */
  327. _selectTabSoon: function (id) {
  328. this._panelDoc.defaultView.setTimeout(() => {
  329. this.select(id);
  330. }, 0);
  331. },
  332. /**
  333. * Return the id of the selected tab.
  334. */
  335. getCurrentTabID: function () {
  336. let currentID = null;
  337. for (let [id, tab] of this._tabs) {
  338. if (this._tabbox.tabs.selectedItem == tab) {
  339. currentID = id;
  340. break;
  341. }
  342. }
  343. return currentID;
  344. },
  345. /**
  346. * Returns the requested tab panel based on the id.
  347. * @param {String} id
  348. * @return {DOMNode}
  349. */
  350. getTabPanel: function (id) {
  351. // Search with and without the ID prefix as there might have been existing
  352. // tabpanels by the time the sidebar got created
  353. return this._tabbox.tabpanels.querySelector("#" + this.TABPANEL_ID_PREFIX + id + ", #" + id);
  354. },
  355. /**
  356. * Return the tab based on the provided id, if one was registered with this id.
  357. * @param {String} id
  358. * @return {DOMNode}
  359. */
  360. getTab: function (id) {
  361. return this._tabs.get(id);
  362. },
  363. /**
  364. * Event handler.
  365. */
  366. handleEvent: function (event) {
  367. if (event.type !== "select" || this._destroyed) {
  368. return;
  369. }
  370. if (this._currentTool == this.getCurrentTabID()) {
  371. // Tool hasn't changed.
  372. return;
  373. }
  374. let previousTool = this._currentTool;
  375. this._currentTool = this.getCurrentTabID();
  376. if (previousTool) {
  377. if (this._telemetry) {
  378. this._telemetry.toolClosed(previousTool);
  379. }
  380. this.emit(previousTool + "-unselected");
  381. }
  382. if (this._telemetry) {
  383. this._telemetry.toolOpened(this._currentTool);
  384. }
  385. this.emit(this._currentTool + "-selected");
  386. this.emit("select", this._currentTool);
  387. // Handlers for "select"/"...-selected"/"...-unselected" events might have
  388. // destroyed the sidebar in the meantime.
  389. if (this._destroyed) {
  390. return;
  391. }
  392. // Handle menuitem selection if the allTabsMenu is there by unchecking all
  393. // items except the selected one.
  394. let tab = this._tabbox.selectedTab;
  395. if (tab.allTabsMenuItem) {
  396. for (let otherItem of this._allTabsBtn.querySelectorAll("menuitem")) {
  397. otherItem.removeAttribute("checked");
  398. }
  399. tab.allTabsMenuItem.setAttribute("checked", true);
  400. }
  401. },
  402. /**
  403. * Toggle sidebar's visibility state.
  404. */
  405. toggle: function () {
  406. if (this._tabbox.hasAttribute("hidden")) {
  407. this.show();
  408. } else {
  409. this.hide();
  410. }
  411. },
  412. /**
  413. * Show the sidebar.
  414. *
  415. * @param {String} id
  416. * The sidebar tab id to select.
  417. */
  418. show: function (id) {
  419. if (this._width) {
  420. this._tabbox.width = this._width;
  421. }
  422. this._tabbox.removeAttribute("hidden");
  423. // If an id is given, select the corresponding sidebar tab and record the
  424. // tool opened.
  425. if (id) {
  426. this._currentTool = id;
  427. if (this._telemetry) {
  428. this._telemetry.toolOpened(this._currentTool);
  429. }
  430. this._selectTabSoon(id);
  431. }
  432. this.emit("show");
  433. },
  434. /**
  435. * Show the sidebar.
  436. */
  437. hide: function () {
  438. Services.prefs.setIntPref("devtools.toolsidebar-width." + this._uid, this._tabbox.width);
  439. this._tabbox.setAttribute("hidden", "true");
  440. this._panelDoc.activeElement.blur();
  441. this.emit("hide");
  442. },
  443. /**
  444. * Return the window containing the tab content.
  445. */
  446. getWindowForTab: function (id) {
  447. if (!this._tabs.has(id)) {
  448. return null;
  449. }
  450. // Get the tabpanel and make sure it contains an iframe
  451. let panel = this.getTabPanel(id);
  452. if (!panel || !panel.firstChild || !panel.firstChild.contentWindow) {
  453. return;
  454. }
  455. return panel.firstChild.contentWindow;
  456. },
  457. /**
  458. * Clean-up.
  459. */
  460. destroy: Task.async(function* () {
  461. if (this._destroyed) {
  462. return;
  463. }
  464. this._destroyed = true;
  465. Services.prefs.setIntPref("devtools.toolsidebar-width." + this._uid, this._tabbox.width);
  466. if (this._allTabsBtn) {
  467. this.removeAllTabsMenu();
  468. }
  469. this._tabbox.tabpanels.removeEventListener("select", this, true);
  470. // Note that we check for the existence of this._tabbox.tabpanels at each
  471. // step as the container window may have been closed by the time one of the
  472. // panel's destroy promise resolves.
  473. while (this._tabbox.tabpanels && this._tabbox.tabpanels.hasChildNodes()) {
  474. let panel = this._tabbox.tabpanels.firstChild;
  475. let win = panel.firstChild.contentWindow;
  476. if (win && ("destroy" in win)) {
  477. yield win.destroy();
  478. }
  479. panel.remove();
  480. }
  481. while (this._tabbox.tabs && this._tabbox.tabs.hasChildNodes()) {
  482. this._tabbox.tabs.removeChild(this._tabbox.tabs.firstChild);
  483. }
  484. if (this._currentTool && this._telemetry) {
  485. this._telemetry.toolClosed(this._currentTool);
  486. }
  487. this._toolPanel.emit("sidebar-destroyed", this);
  488. this._tabs = null;
  489. this._tabbox = null;
  490. this._panelDoc = null;
  491. this._toolPanel = null;
  492. })
  493. };