browser/devtools/inspector/breadcrumbs.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 const {Cc, Cu, Ci} = require("chrome");
michael@0 8
michael@0 9 const PSEUDO_CLASSES = [":hover", ":active", ":focus"];
michael@0 10 const ENSURE_SELECTION_VISIBLE_DELAY = 50; // ms
michael@0 11
michael@0 12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 13 Cu.import("resource:///modules/devtools/DOMHelpers.jsm");
michael@0 14 Cu.import("resource://gre/modules/Services.jsm");
michael@0 15 Cu.import("resource:///modules/devtools/ViewHelpers.jsm");
michael@0 16 const ELLIPSIS = Services.prefs.getComplexValue("intl.ellipsis", Ci.nsIPrefLocalizedString).data;
michael@0 17 const MAX_LABEL_LENGTH = 40;
michael@0 18
michael@0 19 let promise = require("devtools/toolkit/deprecated-sync-thenables");
michael@0 20
michael@0 21 const LOW_PRIORITY_ELEMENTS = {
michael@0 22 "HEAD": true,
michael@0 23 "BASE": true,
michael@0 24 "BASEFONT": true,
michael@0 25 "ISINDEX": true,
michael@0 26 "LINK": true,
michael@0 27 "META": true,
michael@0 28 "SCRIPT": true,
michael@0 29 "STYLE": true,
michael@0 30 "TITLE": true,
michael@0 31 };
michael@0 32
michael@0 33 function resolveNextTick(value) {
michael@0 34 let deferred = promise.defer();
michael@0 35 Services.tm.mainThread.dispatch(() => {
michael@0 36 try {
michael@0 37 deferred.resolve(value);
michael@0 38 } catch(ex) {
michael@0 39 console.error(ex);
michael@0 40 }
michael@0 41 }, Ci.nsIThread.DISPATCH_NORMAL);
michael@0 42 return deferred.promise;
michael@0 43 }
michael@0 44
michael@0 45 ///////////////////////////////////////////////////////////////////////////
michael@0 46 //// HTML Breadcrumbs
michael@0 47
michael@0 48 /**
michael@0 49 * Display the ancestors of the current node and its children.
michael@0 50 * Only one "branch" of children are displayed (only one line).
michael@0 51 *
michael@0 52 * FIXME: Bug 822388 - Use the BreadcrumbsWidget in the Inspector.
michael@0 53 *
michael@0 54 * Mechanism:
michael@0 55 * . If no nodes displayed yet:
michael@0 56 * then display the ancestor of the selected node and the selected node;
michael@0 57 * else select the node;
michael@0 58 * . If the selected node is the last node displayed, append its first (if any).
michael@0 59 */
michael@0 60 function HTMLBreadcrumbs(aInspector)
michael@0 61 {
michael@0 62 this.inspector = aInspector;
michael@0 63 this.selection = this.inspector.selection;
michael@0 64 this.chromeWin = this.inspector.panelWin;
michael@0 65 this.chromeDoc = this.inspector.panelDoc;
michael@0 66 this.DOMHelpers = new DOMHelpers(this.chromeWin);
michael@0 67 this._init();
michael@0 68 }
michael@0 69
michael@0 70 exports.HTMLBreadcrumbs = HTMLBreadcrumbs;
michael@0 71
michael@0 72 HTMLBreadcrumbs.prototype = {
michael@0 73 get walker() this.inspector.walker,
michael@0 74
michael@0 75 _init: function BC__init()
michael@0 76 {
michael@0 77 this.container = this.chromeDoc.getElementById("inspector-breadcrumbs");
michael@0 78
michael@0 79 // These separators are used for CSS purposes only, and are positioned
michael@0 80 // off screen, but displayed with -moz-element.
michael@0 81 this.separators = this.chromeDoc.createElement("box");
michael@0 82 this.separators.className = "breadcrumb-separator-container";
michael@0 83 this.separators.innerHTML =
michael@0 84 "<box id='breadcrumb-separator-before'></box>" +
michael@0 85 "<box id='breadcrumb-separator-after'></box>" +
michael@0 86 "<box id='breadcrumb-separator-normal'></box>";
michael@0 87 this.container.parentNode.appendChild(this.separators);
michael@0 88
michael@0 89 this.container.addEventListener("mousedown", this, true);
michael@0 90 this.container.addEventListener("keypress", this, true);
michael@0 91
michael@0 92 // We will save a list of already displayed nodes in this array.
michael@0 93 this.nodeHierarchy = [];
michael@0 94
michael@0 95 // Last selected node in nodeHierarchy.
michael@0 96 this.currentIndex = -1;
michael@0 97
michael@0 98 // By default, hide the arrows. We let the <scrollbox> show them
michael@0 99 // in case of overflow.
michael@0 100 this.container.removeAttribute("overflows");
michael@0 101 this.container._scrollButtonUp.collapsed = true;
michael@0 102 this.container._scrollButtonDown.collapsed = true;
michael@0 103
michael@0 104 this.onscrollboxreflow = function() {
michael@0 105 if (this.container._scrollButtonDown.collapsed)
michael@0 106 this.container.removeAttribute("overflows");
michael@0 107 else
michael@0 108 this.container.setAttribute("overflows", true);
michael@0 109 }.bind(this);
michael@0 110
michael@0 111 this.container.addEventListener("underflow", this.onscrollboxreflow, false);
michael@0 112 this.container.addEventListener("overflow", this.onscrollboxreflow, false);
michael@0 113
michael@0 114 this.update = this.update.bind(this);
michael@0 115 this.updateSelectors = this.updateSelectors.bind(this);
michael@0 116 this.selection.on("new-node-front", this.update);
michael@0 117 this.selection.on("pseudoclass", this.updateSelectors);
michael@0 118 this.selection.on("attribute-changed", this.updateSelectors);
michael@0 119 this.inspector.on("markupmutation", this.update);
michael@0 120 this.update();
michael@0 121 },
michael@0 122
michael@0 123 /**
michael@0 124 * Include in a promise's then() chain to reject the chain
michael@0 125 * when the breadcrumbs' selection has changed while the promise
michael@0 126 * was outstanding.
michael@0 127 */
michael@0 128 selectionGuard: function() {
michael@0 129 let selection = this.selection.nodeFront;
michael@0 130 return (result) => {
michael@0 131 if (selection != this.selection.nodeFront) {
michael@0 132 return promise.reject("selection-changed");
michael@0 133 }
michael@0 134 return result;
michael@0 135 }
michael@0 136 },
michael@0 137
michael@0 138 /**
michael@0 139 * Print any errors (except selection guard errors).
michael@0 140 */
michael@0 141 selectionGuardEnd: function(err) {
michael@0 142 if (err != "selection-changed") {
michael@0 143 console.error(err);
michael@0 144 }
michael@0 145 promise.reject(err);
michael@0 146 },
michael@0 147
michael@0 148 /**
michael@0 149 * Build a string that represents the node: tagName#id.class1.class2.
michael@0 150 *
michael@0 151 * @param aNode The node to pretty-print
michael@0 152 * @returns a string
michael@0 153 */
michael@0 154 prettyPrintNodeAsText: function BC_prettyPrintNodeText(aNode)
michael@0 155 {
michael@0 156 let text = aNode.tagName.toLowerCase();
michael@0 157 if (aNode.id) {
michael@0 158 text += "#" + aNode.id;
michael@0 159 }
michael@0 160
michael@0 161 if (aNode.className) {
michael@0 162 let classList = aNode.className.split(/\s+/);
michael@0 163 for (let i = 0; i < classList.length; i++) {
michael@0 164 text += "." + classList[i];
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168 for (let pseudo of aNode.pseudoClassLocks) {
michael@0 169 text += pseudo;
michael@0 170 }
michael@0 171
michael@0 172 return text;
michael@0 173 },
michael@0 174
michael@0 175
michael@0 176 /**
michael@0 177 * Build <label>s that represent the node:
michael@0 178 * <label class="breadcrumbs-widget-item-tag">tagName</label>
michael@0 179 * <label class="breadcrumbs-widget-item-id">#id</label>
michael@0 180 * <label class="breadcrumbs-widget-item-classes">.class1.class2</label>
michael@0 181 *
michael@0 182 * @param aNode The node to pretty-print
michael@0 183 * @returns a document fragment.
michael@0 184 */
michael@0 185 prettyPrintNodeAsXUL: function BC_prettyPrintNodeXUL(aNode)
michael@0 186 {
michael@0 187 let fragment = this.chromeDoc.createDocumentFragment();
michael@0 188
michael@0 189 let tagLabel = this.chromeDoc.createElement("label");
michael@0 190 tagLabel.className = "breadcrumbs-widget-item-tag plain";
michael@0 191
michael@0 192 let idLabel = this.chromeDoc.createElement("label");
michael@0 193 idLabel.className = "breadcrumbs-widget-item-id plain";
michael@0 194
michael@0 195 let classesLabel = this.chromeDoc.createElement("label");
michael@0 196 classesLabel.className = "breadcrumbs-widget-item-classes plain";
michael@0 197
michael@0 198 let pseudosLabel = this.chromeDoc.createElement("label");
michael@0 199 pseudosLabel.className = "breadcrumbs-widget-item-pseudo-classes plain";
michael@0 200
michael@0 201 let tagText = aNode.tagName.toLowerCase();
michael@0 202 let idText = aNode.id ? ("#" + aNode.id) : "";
michael@0 203 let classesText = "";
michael@0 204
michael@0 205 if (aNode.className) {
michael@0 206 let classList = aNode.className.split(/\s+/);
michael@0 207 for (let i = 0; i < classList.length; i++) {
michael@0 208 classesText += "." + classList[i];
michael@0 209 }
michael@0 210 }
michael@0 211
michael@0 212 // XXX: Until we have pseudoclass lock in the node.
michael@0 213 for (let pseudo of aNode.pseudoClassLocks) {
michael@0 214
michael@0 215 }
michael@0 216
michael@0 217 // Figure out which element (if any) needs ellipsing.
michael@0 218 // Substring for that element, then clear out any extras
michael@0 219 // (except for pseudo elements).
michael@0 220 let maxTagLength = MAX_LABEL_LENGTH;
michael@0 221 let maxIdLength = MAX_LABEL_LENGTH - tagText.length;
michael@0 222 let maxClassLength = MAX_LABEL_LENGTH - tagText.length - idText.length;
michael@0 223
michael@0 224 if (tagText.length > maxTagLength) {
michael@0 225 tagText = tagText.substr(0, maxTagLength) + ELLIPSIS;
michael@0 226 idText = classesText = "";
michael@0 227 } else if (idText.length > maxIdLength) {
michael@0 228 idText = idText.substr(0, maxIdLength) + ELLIPSIS;
michael@0 229 classesText = "";
michael@0 230 } else if (classesText.length > maxClassLength) {
michael@0 231 classesText = classesText.substr(0, maxClassLength) + ELLIPSIS;
michael@0 232 }
michael@0 233
michael@0 234 tagLabel.textContent = tagText;
michael@0 235 idLabel.textContent = idText;
michael@0 236 classesLabel.textContent = classesText;
michael@0 237 pseudosLabel.textContent = aNode.pseudoClassLocks.join("");
michael@0 238
michael@0 239 fragment.appendChild(tagLabel);
michael@0 240 fragment.appendChild(idLabel);
michael@0 241 fragment.appendChild(classesLabel);
michael@0 242 fragment.appendChild(pseudosLabel);
michael@0 243
michael@0 244 return fragment;
michael@0 245 },
michael@0 246
michael@0 247 /**
michael@0 248 * Open the sibling menu.
michael@0 249 *
michael@0 250 * @param aButton the button representing the node.
michael@0 251 * @param aNode the node we want the siblings from.
michael@0 252 */
michael@0 253 openSiblingMenu: function BC_openSiblingMenu(aButton, aNode)
michael@0 254 {
michael@0 255 // We make sure that the targeted node is selected
michael@0 256 // because we want to use the nodemenu that only works
michael@0 257 // for inspector.selection
michael@0 258 this.selection.setNodeFront(aNode, "breadcrumbs");
michael@0 259
michael@0 260 let title = this.chromeDoc.createElement("menuitem");
michael@0 261 title.setAttribute("label", this.inspector.strings.GetStringFromName("breadcrumbs.siblings"));
michael@0 262 title.setAttribute("disabled", "true");
michael@0 263
michael@0 264 let separator = this.chromeDoc.createElement("menuseparator");
michael@0 265
michael@0 266 let items = [title, separator];
michael@0 267
michael@0 268 this.walker.siblings(aNode, {
michael@0 269 whatToShow: Ci.nsIDOMNodeFilter.SHOW_ELEMENT
michael@0 270 }).then(siblings => {
michael@0 271 let nodes = siblings.nodes;
michael@0 272 for (let i = 0; i < nodes.length; i++) {
michael@0 273 let item = this.chromeDoc.createElement("menuitem");
michael@0 274 if (nodes[i] === aNode) {
michael@0 275 item.setAttribute("disabled", "true");
michael@0 276 item.setAttribute("checked", "true");
michael@0 277 }
michael@0 278
michael@0 279 item.setAttribute("type", "radio");
michael@0 280 item.setAttribute("label", this.prettyPrintNodeAsText(nodes[i]));
michael@0 281
michael@0 282 let selection = this.selection;
michael@0 283 item.onmouseup = (function(aNode) {
michael@0 284 return function() {
michael@0 285 selection.setNodeFront(aNode, "breadcrumbs");
michael@0 286 }
michael@0 287 })(nodes[i]);
michael@0 288
michael@0 289 items.push(item);
michael@0 290 this.inspector.showNodeMenu(aButton, "before_start", items);
michael@0 291 }
michael@0 292 });
michael@0 293 },
michael@0 294
michael@0 295 /**
michael@0 296 * Generic event handler.
michael@0 297 *
michael@0 298 * @param nsIDOMEvent event
michael@0 299 * The DOM event object.
michael@0 300 */
michael@0 301 handleEvent: function BC_handleEvent(event)
michael@0 302 {
michael@0 303 if (event.type == "mousedown" && event.button == 0) {
michael@0 304 // on Click and Hold, open the Siblings menu
michael@0 305
michael@0 306 let timer;
michael@0 307 let container = this.container;
michael@0 308
michael@0 309 function openMenu(event) {
michael@0 310 cancelHold();
michael@0 311 let target = event.originalTarget;
michael@0 312 if (target.tagName == "button") {
michael@0 313 target.onBreadcrumbsHold();
michael@0 314 }
michael@0 315 }
michael@0 316
michael@0 317 function handleClick(event) {
michael@0 318 cancelHold();
michael@0 319 let target = event.originalTarget;
michael@0 320 if (target.tagName == "button") {
michael@0 321 target.onBreadcrumbsClick();
michael@0 322 }
michael@0 323 }
michael@0 324
michael@0 325 let window = this.chromeWin;
michael@0 326 function cancelHold(event) {
michael@0 327 window.clearTimeout(timer);
michael@0 328 container.removeEventListener("mouseout", cancelHold, false);
michael@0 329 container.removeEventListener("mouseup", handleClick, false);
michael@0 330 }
michael@0 331
michael@0 332 container.addEventListener("mouseout", cancelHold, false);
michael@0 333 container.addEventListener("mouseup", handleClick, false);
michael@0 334 timer = window.setTimeout(openMenu, 500, event);
michael@0 335 }
michael@0 336
michael@0 337 if (event.type == "keypress" && this.selection.isElementNode()) {
michael@0 338 let node = null;
michael@0 339
michael@0 340
michael@0 341 this._keyPromise = this._keyPromise || promise.resolve(null);
michael@0 342
michael@0 343 this._keyPromise = (this._keyPromise || promise.resolve(null)).then(() => {
michael@0 344 switch (event.keyCode) {
michael@0 345 case this.chromeWin.KeyEvent.DOM_VK_LEFT:
michael@0 346 if (this.currentIndex != 0) {
michael@0 347 node = promise.resolve(this.nodeHierarchy[this.currentIndex - 1].node);
michael@0 348 }
michael@0 349 break;
michael@0 350 case this.chromeWin.KeyEvent.DOM_VK_RIGHT:
michael@0 351 if (this.currentIndex < this.nodeHierarchy.length - 1) {
michael@0 352 node = promise.resolve(this.nodeHierarchy[this.currentIndex + 1].node);
michael@0 353 }
michael@0 354 break;
michael@0 355 case this.chromeWin.KeyEvent.DOM_VK_UP:
michael@0 356 node = this.walker.previousSibling(this.selection.nodeFront, {
michael@0 357 whatToShow: Ci.nsIDOMNodeFilter.SHOW_ELEMENT
michael@0 358 });
michael@0 359 break;
michael@0 360 case this.chromeWin.KeyEvent.DOM_VK_DOWN:
michael@0 361 node = this.walker.nextSibling(this.selection.nodeFront, {
michael@0 362 whatToShow: Ci.nsIDOMNodeFilter.SHOW_ELEMENT
michael@0 363 });
michael@0 364 break;
michael@0 365 }
michael@0 366
michael@0 367 return node.then((node) => {
michael@0 368 if (node) {
michael@0 369 this.selection.setNodeFront(node, "breadcrumbs");
michael@0 370 }
michael@0 371 });
michael@0 372 });
michael@0 373 event.preventDefault();
michael@0 374 event.stopPropagation();
michael@0 375 }
michael@0 376 },
michael@0 377
michael@0 378 /**
michael@0 379 * Remove nodes and delete properties.
michael@0 380 */
michael@0 381 destroy: function BC_destroy()
michael@0 382 {
michael@0 383 this.selection.off("new-node-front", this.update);
michael@0 384 this.selection.off("pseudoclass", this.updateSelectors);
michael@0 385 this.selection.off("attribute-changed", this.updateSelectors);
michael@0 386 this.inspector.off("markupmutation", this.update);
michael@0 387
michael@0 388 this.container.removeEventListener("underflow", this.onscrollboxreflow, false);
michael@0 389 this.container.removeEventListener("overflow", this.onscrollboxreflow, false);
michael@0 390 this.onscrollboxreflow = null;
michael@0 391
michael@0 392 this.empty();
michael@0 393 this.container.removeEventListener("mousedown", this, true);
michael@0 394 this.container.removeEventListener("keypress", this, true);
michael@0 395 this.container = null;
michael@0 396
michael@0 397 this.separators.remove();
michael@0 398 this.separators = null;
michael@0 399
michael@0 400 this.nodeHierarchy = null;
michael@0 401 },
michael@0 402
michael@0 403 /**
michael@0 404 * Empty the breadcrumbs container.
michael@0 405 */
michael@0 406 empty: function BC_empty()
michael@0 407 {
michael@0 408 while (this.container.hasChildNodes()) {
michael@0 409 this.container.removeChild(this.container.firstChild);
michael@0 410 }
michael@0 411 },
michael@0 412
michael@0 413 /**
michael@0 414 * Set which button represent the selected node.
michael@0 415 *
michael@0 416 * @param aIdx Index of the displayed-button to select
michael@0 417 */
michael@0 418 setCursor: function BC_setCursor(aIdx)
michael@0 419 {
michael@0 420 // Unselect the previously selected button
michael@0 421 if (this.currentIndex > -1 && this.currentIndex < this.nodeHierarchy.length) {
michael@0 422 this.nodeHierarchy[this.currentIndex].button.removeAttribute("checked");
michael@0 423 }
michael@0 424 if (aIdx > -1) {
michael@0 425 this.nodeHierarchy[aIdx].button.setAttribute("checked", "true");
michael@0 426 if (this.hadFocus)
michael@0 427 this.nodeHierarchy[aIdx].button.focus();
michael@0 428 }
michael@0 429 this.currentIndex = aIdx;
michael@0 430 },
michael@0 431
michael@0 432 /**
michael@0 433 * Get the index of the node in the cache.
michael@0 434 *
michael@0 435 * @param aNode
michael@0 436 * @returns integer the index, -1 if not found
michael@0 437 */
michael@0 438 indexOf: function BC_indexOf(aNode)
michael@0 439 {
michael@0 440 let i = this.nodeHierarchy.length - 1;
michael@0 441 for (let i = this.nodeHierarchy.length - 1; i >= 0; i--) {
michael@0 442 if (this.nodeHierarchy[i].node === aNode) {
michael@0 443 return i;
michael@0 444 }
michael@0 445 }
michael@0 446 return -1;
michael@0 447 },
michael@0 448
michael@0 449 /**
michael@0 450 * Remove all the buttons and their references in the cache
michael@0 451 * after a given index.
michael@0 452 *
michael@0 453 * @param aIdx
michael@0 454 */
michael@0 455 cutAfter: function BC_cutAfter(aIdx)
michael@0 456 {
michael@0 457 while (this.nodeHierarchy.length > (aIdx + 1)) {
michael@0 458 let toRemove = this.nodeHierarchy.pop();
michael@0 459 this.container.removeChild(toRemove.button);
michael@0 460 }
michael@0 461 },
michael@0 462
michael@0 463 /**
michael@0 464 * Build a button representing the node.
michael@0 465 *
michael@0 466 * @param aNode The node from the page.
michael@0 467 * @returns aNode The <button>.
michael@0 468 */
michael@0 469 buildButton: function BC_buildButton(aNode)
michael@0 470 {
michael@0 471 let button = this.chromeDoc.createElement("button");
michael@0 472 button.appendChild(this.prettyPrintNodeAsXUL(aNode));
michael@0 473 button.className = "breadcrumbs-widget-item";
michael@0 474
michael@0 475 button.setAttribute("tooltiptext", this.prettyPrintNodeAsText(aNode));
michael@0 476
michael@0 477 button.onkeypress = function onBreadcrumbsKeypress(e) {
michael@0 478 if (e.charCode == Ci.nsIDOMKeyEvent.DOM_VK_SPACE ||
michael@0 479 e.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_RETURN)
michael@0 480 button.click();
michael@0 481 }
michael@0 482
michael@0 483 button.onBreadcrumbsClick = function onBreadcrumbsClick() {
michael@0 484 this.selection.setNodeFront(aNode, "breadcrumbs");
michael@0 485 }.bind(this);
michael@0 486
michael@0 487 button.onclick = (function _onBreadcrumbsRightClick(event) {
michael@0 488 button.focus();
michael@0 489 if (event.button == 2) {
michael@0 490 this.openSiblingMenu(button, aNode);
michael@0 491 }
michael@0 492 }).bind(this);
michael@0 493
michael@0 494 button.onBreadcrumbsHold = (function _onBreadcrumbsHold() {
michael@0 495 this.openSiblingMenu(button, aNode);
michael@0 496 }).bind(this);
michael@0 497 return button;
michael@0 498 },
michael@0 499
michael@0 500 /**
michael@0 501 * Connecting the end of the breadcrumbs to a node.
michael@0 502 *
michael@0 503 * @param aNode The node to reach.
michael@0 504 */
michael@0 505 expand: function BC_expand(aNode)
michael@0 506 {
michael@0 507 let fragment = this.chromeDoc.createDocumentFragment();
michael@0 508 let toAppend = aNode;
michael@0 509 let lastButtonInserted = null;
michael@0 510 let originalLength = this.nodeHierarchy.length;
michael@0 511 let stopNode = null;
michael@0 512 if (originalLength > 0) {
michael@0 513 stopNode = this.nodeHierarchy[originalLength - 1].node;
michael@0 514 }
michael@0 515 while (toAppend && toAppend != stopNode) {
michael@0 516 if (toAppend.tagName) {
michael@0 517 let button = this.buildButton(toAppend);
michael@0 518 fragment.insertBefore(button, lastButtonInserted);
michael@0 519 lastButtonInserted = button;
michael@0 520 this.nodeHierarchy.splice(originalLength, 0, {node: toAppend, button: button});
michael@0 521 }
michael@0 522 toAppend = toAppend.parentNode();
michael@0 523 }
michael@0 524 this.container.appendChild(fragment, this.container.firstChild);
michael@0 525 },
michael@0 526
michael@0 527 /**
michael@0 528 * Get a child of a node that can be displayed in the breadcrumbs
michael@0 529 * and that is probably visible. See LOW_PRIORITY_ELEMENTS.
michael@0 530 *
michael@0 531 * @param aNode The parent node.
michael@0 532 * @returns nsIDOMNode|null
michael@0 533 */
michael@0 534 getInterestingFirstNode: function BC_getInterestingFirstNode(aNode)
michael@0 535 {
michael@0 536 let deferred = promise.defer();
michael@0 537
michael@0 538 var fallback = null;
michael@0 539
michael@0 540 var moreChildren = () => {
michael@0 541 this.walker.children(aNode, {
michael@0 542 start: fallback,
michael@0 543 maxNodes: 10,
michael@0 544 whatToShow: Ci.nsIDOMNodeFilter.SHOW_ELEMENT
michael@0 545 }).then(this.selectionGuard()).then(response => {
michael@0 546 for (let node of response.nodes) {
michael@0 547 if (!(node.tagName in LOW_PRIORITY_ELEMENTS)) {
michael@0 548 deferred.resolve(node);
michael@0 549 return;
michael@0 550 }
michael@0 551 if (!fallback) {
michael@0 552 fallback = node;
michael@0 553 }
michael@0 554 }
michael@0 555 if (response.hasLast) {
michael@0 556 deferred.resolve(fallback);
michael@0 557 return;
michael@0 558 } else {
michael@0 559 moreChildren();
michael@0 560 }
michael@0 561 }).then(null, this.selectionGuardEnd);
michael@0 562 }
michael@0 563 moreChildren();
michael@0 564 return deferred.promise;
michael@0 565 },
michael@0 566
michael@0 567 /**
michael@0 568 * Find the "youngest" ancestor of a node which is already in the breadcrumbs.
michael@0 569 *
michael@0 570 * @param aNode
michael@0 571 * @returns Index of the ancestor in the cache
michael@0 572 */
michael@0 573 getCommonAncestor: function BC_getCommonAncestor(aNode)
michael@0 574 {
michael@0 575 let node = aNode;
michael@0 576 while (node) {
michael@0 577 let idx = this.indexOf(node);
michael@0 578 if (idx > -1) {
michael@0 579 return idx;
michael@0 580 } else {
michael@0 581 node = node.parentNode();
michael@0 582 }
michael@0 583 }
michael@0 584 return -1;
michael@0 585 },
michael@0 586
michael@0 587 /**
michael@0 588 * Make sure that the latest node in the breadcrumbs is not the selected node
michael@0 589 * if the selected node still has children.
michael@0 590 */
michael@0 591 ensureFirstChild: function BC_ensureFirstChild()
michael@0 592 {
michael@0 593 // If the last displayed node is the selected node
michael@0 594 if (this.currentIndex == this.nodeHierarchy.length - 1) {
michael@0 595 let node = this.nodeHierarchy[this.currentIndex].node;
michael@0 596 return this.getInterestingFirstNode(node).then(child => {
michael@0 597 // If the node has a child
michael@0 598 if (child) {
michael@0 599 // Show this child
michael@0 600 this.expand(child);
michael@0 601 }
michael@0 602 });
michael@0 603 }
michael@0 604
michael@0 605 return resolveNextTick(true);
michael@0 606 },
michael@0 607
michael@0 608 /**
michael@0 609 * Ensure the selected node is visible.
michael@0 610 */
michael@0 611 scroll: function BC_scroll()
michael@0 612 {
michael@0 613 // FIXME bug 684352: make sure its immediate neighbors are visible too.
michael@0 614
michael@0 615 let scrollbox = this.container;
michael@0 616 let element = this.nodeHierarchy[this.currentIndex].button;
michael@0 617
michael@0 618 // Repeated calls to ensureElementIsVisible would interfere with each other
michael@0 619 // and may sometimes result in incorrect scroll positions.
michael@0 620 this.chromeWin.clearTimeout(this._ensureVisibleTimeout);
michael@0 621 this._ensureVisibleTimeout = this.chromeWin.setTimeout(function() {
michael@0 622 scrollbox.ensureElementIsVisible(element);
michael@0 623 }, ENSURE_SELECTION_VISIBLE_DELAY);
michael@0 624 },
michael@0 625
michael@0 626 updateSelectors: function BC_updateSelectors()
michael@0 627 {
michael@0 628 for (let i = this.nodeHierarchy.length - 1; i >= 0; i--) {
michael@0 629 let crumb = this.nodeHierarchy[i];
michael@0 630 let button = crumb.button;
michael@0 631
michael@0 632 while(button.hasChildNodes()) {
michael@0 633 button.removeChild(button.firstChild);
michael@0 634 }
michael@0 635 button.appendChild(this.prettyPrintNodeAsXUL(crumb.node));
michael@0 636 button.setAttribute("tooltiptext", this.prettyPrintNodeAsText(crumb.node));
michael@0 637 }
michael@0 638 },
michael@0 639
michael@0 640 /**
michael@0 641 * Update the breadcrumbs display when a new node is selected.
michael@0 642 */
michael@0 643 update: function BC_update(reason)
michael@0 644 {
michael@0 645 if (reason !== "markupmutation") {
michael@0 646 this.inspector.hideNodeMenu();
michael@0 647 }
michael@0 648
michael@0 649 let cmdDispatcher = this.chromeDoc.commandDispatcher;
michael@0 650 this.hadFocus = (cmdDispatcher.focusedElement &&
michael@0 651 cmdDispatcher.focusedElement.parentNode == this.container);
michael@0 652
michael@0 653 if (!this.selection.isConnected()) {
michael@0 654 this.cutAfter(-1); // remove all the crumbs
michael@0 655 return;
michael@0 656 }
michael@0 657
michael@0 658 if (!this.selection.isElementNode()) {
michael@0 659 this.setCursor(-1); // no selection
michael@0 660 return;
michael@0 661 }
michael@0 662
michael@0 663 let idx = this.indexOf(this.selection.nodeFront);
michael@0 664
michael@0 665 // Is the node already displayed in the breadcrumbs?
michael@0 666 // (and there are no mutations that need re-display of the crumbs)
michael@0 667 if (idx > -1 && reason !== "markupmutation") {
michael@0 668 // Yes. We select it.
michael@0 669 this.setCursor(idx);
michael@0 670 } else {
michael@0 671 // No. Is the breadcrumbs display empty?
michael@0 672 if (this.nodeHierarchy.length > 0) {
michael@0 673 // No. We drop all the element that are not direct ancestors
michael@0 674 // of the selection
michael@0 675 let parent = this.selection.nodeFront.parentNode();
michael@0 676 let idx = this.getCommonAncestor(parent);
michael@0 677 this.cutAfter(idx);
michael@0 678 }
michael@0 679 // we append the missing button between the end of the breadcrumbs display
michael@0 680 // and the current node.
michael@0 681 this.expand(this.selection.nodeFront);
michael@0 682
michael@0 683 // we select the current node button
michael@0 684 idx = this.indexOf(this.selection.nodeFront);
michael@0 685 this.setCursor(idx);
michael@0 686 }
michael@0 687
michael@0 688 let doneUpdating = this.inspector.updating("breadcrumbs");
michael@0 689 // Add the first child of the very last node of the breadcrumbs if possible.
michael@0 690 this.ensureFirstChild().then(this.selectionGuard()).then(() => {
michael@0 691 this.updateSelectors();
michael@0 692
michael@0 693 // Make sure the selected node and its neighbours are visible.
michael@0 694 this.scroll();
michael@0 695 return resolveNextTick().then(() => {
michael@0 696 this.inspector.emit("breadcrumbs-updated", this.selection.nodeFront);
michael@0 697 doneUpdating();
michael@0 698 });
michael@0 699 }).then(null, err => {
michael@0 700 doneUpdating(this.selection.nodeFront);
michael@0 701 this.selectionGuardEnd(err);
michael@0 702 });
michael@0 703 }
michael@0 704 };
michael@0 705
michael@0 706 XPCOMUtils.defineLazyGetter(this, "DOMUtils", function () {
michael@0 707 return Cc["@mozilla.org/inspector/dom-utils;1"].getService(Ci.inIDOMUtils);
michael@0 708 });

mercurial