2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4 * Copyright (C) 2009 Joseph Pecoraro
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 WebInspector.ElementsPanel = function()
33 WebInspector.Panel.call(this, "elements");
35 this.contentElement = document.createElement("div");
36 this.contentElement.id = "elements-content";
37 this.contentElement.className = "outline-disclosure source-code";
39 this.treeOutline = new WebInspector.ElementsTreeOutline();
40 this.treeOutline.panel = this;
41 this.treeOutline.includeRootDOMNode = false;
42 this.treeOutline.selectEnabled = true;
44 this.treeOutline.focusedNodeChanged = function(forceUpdate)
46 if (this.panel.visible && WebInspector.currentFocusElement !== document.getElementById("search"))
47 WebInspector.currentFocusElement = this.element;
49 this.panel.updateBreadcrumb(forceUpdate);
51 for (var pane in this.panel.sidebarPanes)
52 this.panel.sidebarPanes[pane].needsUpdate = true;
54 this.panel.updateStyles(true);
55 this.panel.updateMetrics();
56 this.panel.updateProperties();
57 this.panel.updateEventListeners();
59 if (this._focusedDOMNode) {
60 DOMAgent.addInspectedNode(this._focusedDOMNode.id);
61 WebInspector.extensionServer.notifyObjectSelected(this.panel.name);
65 this.contentElement.appendChild(this.treeOutline.element);
67 this.crumbsElement = document.createElement("div");
68 this.crumbsElement.className = "crumbs";
69 this.crumbsElement.addEventListener("mousemove", this._mouseMovedInCrumbs.bind(this), false);
70 this.crumbsElement.addEventListener("mouseout", this._mouseMovedOutOfCrumbs.bind(this), false);
72 this.sidebarPanes = {};
73 this.sidebarPanes.computedStyle = new WebInspector.ComputedStyleSidebarPane();
74 this.sidebarPanes.styles = new WebInspector.StylesSidebarPane(this.sidebarPanes.computedStyle);
75 this.sidebarPanes.metrics = new WebInspector.MetricsSidebarPane();
76 this.sidebarPanes.properties = new WebInspector.PropertiesSidebarPane();
77 if (Preferences.nativeInstrumentationEnabled)
78 this.sidebarPanes.domBreakpoints = WebInspector.createDOMBreakpointsSidebarPane();
79 this.sidebarPanes.eventListeners = new WebInspector.EventListenersSidebarPane();
81 this.sidebarPanes.styles.onexpand = this.updateStyles.bind(this);
82 this.sidebarPanes.metrics.onexpand = this.updateMetrics.bind(this);
83 this.sidebarPanes.properties.onexpand = this.updateProperties.bind(this);
84 this.sidebarPanes.eventListeners.onexpand = this.updateEventListeners.bind(this);
86 this.sidebarPanes.styles.expanded = true;
88 this.sidebarPanes.styles.addEventListener("style edited", this._stylesPaneEdited, this);
89 this.sidebarPanes.styles.addEventListener("style property toggled", this._stylesPaneEdited, this);
90 this.sidebarPanes.metrics.addEventListener("metrics edited", this._metricsPaneEdited, this);
91 WebInspector.cssModel.addEventListener("stylesheet changed", this._styleSheetChanged, this);
93 this.sidebarElement = document.createElement("div");
94 this.sidebarElement.id = "elements-sidebar";
96 for (var pane in this.sidebarPanes)
97 this.sidebarElement.appendChild(this.sidebarPanes[pane].element);
99 this.sidebarResizeElement = document.createElement("div");
100 this.sidebarResizeElement.className = "sidebar-resizer-vertical";
101 this.sidebarResizeElement.addEventListener("mousedown", this.rightSidebarResizerDragStart.bind(this), false);
103 this._nodeSearchButton = new WebInspector.StatusBarButton(WebInspector.UIString("Select an element in the page to inspect it."), "node-search-status-bar-item");
104 this._nodeSearchButton.addEventListener("click", this.toggleSearchingForNode.bind(this), false);
106 this.element.appendChild(this.contentElement);
107 this.element.appendChild(this.sidebarElement);
108 this.element.appendChild(this.sidebarResizeElement);
110 this._registerShortcuts();
115 WebInspector.ElementsPanel.prototype = {
116 get toolbarItemLabel()
118 return WebInspector.UIString("Elements");
123 return [this._nodeSearchButton.element, this.crumbsElement];
126 get defaultFocusedElement()
128 return this.treeOutline.element;
131 updateStatusBarItems: function()
133 this.updateBreadcrumbSizes();
138 WebInspector.Panel.prototype.show.call(this);
139 this.sidebarResizeElement.style.right = (this.sidebarElement.offsetWidth - 3) + "px";
140 this.updateBreadcrumb();
141 this.treeOutline.updateSelection();
142 if (this.recentlyModifiedNodes.length)
143 this.updateModifiedNodes();
148 WebInspector.Panel.prototype.hide.call(this);
150 WebInspector.highlightDOMNode(0);
151 this.setSearchingForNode(false);
156 this.treeOutline.updateSelection();
157 this.updateBreadcrumbSizes();
162 if (this.focusedDOMNode)
163 this._selectedPathOnReset = this.focusedDOMNode.path();
165 this.rootDOMNode = null;
166 this.focusedDOMNode = null;
168 WebInspector.highlightDOMNode(0);
170 this.recentlyModifiedNodes = [];
172 delete this.currentQuery;
175 setDocument: function(inspectedRootDocument)
178 this.searchCanceled();
180 if (!inspectedRootDocument)
183 inspectedRootDocument.addEventListener("DOMNodeInserted", this._nodeInserted.bind(this));
184 inspectedRootDocument.addEventListener("DOMNodeRemoved", this._nodeRemoved.bind(this));
185 inspectedRootDocument.addEventListener("DOMAttrModified", this._attributesUpdated.bind(this));
186 inspectedRootDocument.addEventListener("DOMCharacterDataModified", this._characterDataModified.bind(this));
188 this.rootDOMNode = inspectedRootDocument;
190 function selectNode(candidateFocusNode)
192 if (!candidateFocusNode)
193 candidateFocusNode = inspectedRootDocument.body || inspectedRootDocument.documentElement;
195 if (!candidateFocusNode)
198 this.focusedDOMNode = candidateFocusNode;
199 if (this.treeOutline.selectedTreeElement)
200 this.treeOutline.selectedTreeElement.expand();
203 function selectLastSelectedNode(nodeId)
205 if (this.focusedDOMNode) {
206 // Focused node has been explicitly set while reaching out for the last selected node.
209 var node = nodeId ? WebInspector.domAgent.nodeForId(nodeId) : 0;
210 selectNode.call(this, node);
213 if (this._selectedPathOnReset)
214 DOMAgent.pushNodeByPathToFrontend(this._selectedPathOnReset, selectLastSelectedNode.bind(this));
216 selectNode.call(this);
217 delete this._selectedPathOnReset;
220 searchCanceled: function()
222 delete this._searchQuery;
223 this._hideSearchHighlights();
225 WebInspector.searchController.updateSearchMatchesCount(0, this);
227 delete this._currentSearchResultIndex;
228 this._searchResults = [];
229 DOMAgent.searchCanceled();
232 performSearch: function(query)
234 // Call searchCanceled since it will reset everything we need before doing a new search.
235 this.searchCanceled();
237 const whitespaceTrimmedQuery = query.trim();
238 if (!whitespaceTrimmedQuery.length)
241 this._updatedMatchCountOnce = false;
242 this._matchesCountUpdateTimeout = null;
243 this._searchQuery = query;
245 DOMAgent.performSearch(whitespaceTrimmedQuery, false);
248 populateHrefContextMenu: function(contextMenu, event, anchorElement)
250 if (!anchorElement.href)
253 var resourceURL = WebInspector.resourceURLForRelatedNode(this.focusedDOMNode, anchorElement.href);
257 // Add resource-related actions.
258 contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), WebInspector.openResource.bind(null, resourceURL, false));
259 if (WebInspector.resourceForURL(resourceURL))
260 contextMenu.appendItem(WebInspector.UIString("Open Link in Resources Panel"), WebInspector.openResource.bind(null, resourceURL, true));
264 switchToAndFocus: function(node)
266 // Reset search restore.
267 WebInspector.searchController.cancelSearch();
268 WebInspector.currentPanel = this;
269 this.focusedDOMNode = node;
272 _updateMatchesCount: function()
274 WebInspector.searchController.updateSearchMatchesCount(this._searchResults.length, this);
275 this._matchesCountUpdateTimeout = null;
276 this._updatedMatchCountOnce = true;
279 _updateMatchesCountSoon: function()
281 if (!this._updatedMatchCountOnce)
282 return this._updateMatchesCount();
283 if (this._matchesCountUpdateTimeout)
285 // Update the matches count every half-second so it doesn't feel twitchy.
286 this._matchesCountUpdateTimeout = setTimeout(this._updateMatchesCount.bind(this), 500);
289 addNodesToSearchResult: function(nodeIds)
294 var oldSearchResultIndex = this._currentSearchResultIndex;
295 for (var i = 0; i < nodeIds.length; ++i) {
296 var nodeId = nodeIds[i];
297 var node = WebInspector.domAgent.nodeForId(nodeId);
301 this._currentSearchResultIndex = 0;
302 this._searchResults.push(node);
305 // Avoid invocations of highlighting for every chunk of nodeIds.
306 if (oldSearchResultIndex !== this._currentSearchResultIndex)
307 this._highlightCurrentSearchResult();
308 this._updateMatchesCountSoon();
311 jumpToNextSearchResult: function()
313 if (!this._searchResults || !this._searchResults.length)
316 if (++this._currentSearchResultIndex >= this._searchResults.length)
317 this._currentSearchResultIndex = 0;
318 this._highlightCurrentSearchResult();
321 jumpToPreviousSearchResult: function()
323 if (!this._searchResults || !this._searchResults.length)
326 if (--this._currentSearchResultIndex < 0)
327 this._currentSearchResultIndex = (this._searchResults.length - 1);
328 this._highlightCurrentSearchResult();
331 _highlightCurrentSearchResult: function()
333 this._hideSearchHighlights();
334 var node = this._searchResults[this._currentSearchResultIndex];
335 var treeElement = this.treeOutline.findTreeElement(node);
337 treeElement.highlightSearchResults(this._searchQuery);
338 treeElement.reveal();
342 _hideSearchHighlights: function(node)
344 for (var i = 0; this._searchResults && i < this._searchResults.length; ++i) {
345 var node = this._searchResults[i];
346 var treeElement = this.treeOutline.findTreeElement(node);
348 treeElement.highlightSearchResults(null);
352 renameSelector: function(oldIdentifier, newIdentifier, oldSelector, newSelector)
354 // TODO: Implement Shifting the oldSelector, and its contents to a newSelector
359 return this.treeOutline.rootDOMNode;
364 this.treeOutline.rootDOMNode = x;
369 return this.treeOutline.focusedDOMNode;
372 set focusedDOMNode(x)
374 this.treeOutline.focusedDOMNode = x;
377 _attributesUpdated: function(event)
379 this.recentlyModifiedNodes.push({node: event.target, updated: true});
381 this._updateModifiedNodesSoon();
383 if (!this.sidebarPanes.styles.isModifyingStyle && event.target === this.focusedDOMNode)
384 this._styleSheetChanged();
387 _characterDataModified: function(event)
389 this.recentlyModifiedNodes.push({node: event.target, updated: true});
391 this._updateModifiedNodesSoon();
394 _nodeInserted: function(event)
396 this.recentlyModifiedNodes.push({node: event.target, parent: event.relatedNode, inserted: true});
398 this._updateModifiedNodesSoon();
401 _nodeRemoved: function(event)
403 this.recentlyModifiedNodes.push({node: event.target, parent: event.relatedNode, removed: true});
405 this._updateModifiedNodesSoon();
408 _updateModifiedNodesSoon: function()
410 if ("_updateModifiedNodesTimeout" in this)
412 this._updateModifiedNodesTimeout = setTimeout(this.updateModifiedNodes.bind(this), 0);
415 updateModifiedNodes: function()
417 if ("_updateModifiedNodesTimeout" in this) {
418 clearTimeout(this._updateModifiedNodesTimeout);
419 delete this._updateModifiedNodesTimeout;
422 var updatedParentTreeElements = [];
423 var updateBreadcrumbs = false;
425 for (var i = 0; i < this.recentlyModifiedNodes.length; ++i) {
426 var replaced = this.recentlyModifiedNodes[i].replaced;
427 var parent = this.recentlyModifiedNodes[i].parent;
428 var node = this.recentlyModifiedNodes[i].node;
430 if (this.recentlyModifiedNodes[i].updated) {
431 var nodeItem = this.treeOutline.findTreeElement(node);
433 nodeItem.updateTitle();
440 var parentNodeItem = this.treeOutline.findTreeElement(parent);
441 if (parentNodeItem && !parentNodeItem.alreadyUpdatedChildren) {
442 parentNodeItem.updateChildren(replaced);
443 parentNodeItem.alreadyUpdatedChildren = true;
444 updatedParentTreeElements.push(parentNodeItem);
447 if (!updateBreadcrumbs && (this.focusedDOMNode === parent || isAncestorNode(this.focusedDOMNode, parent)))
448 updateBreadcrumbs = true;
451 for (var i = 0; i < updatedParentTreeElements.length; ++i)
452 delete updatedParentTreeElements[i].alreadyUpdatedChildren;
454 this.recentlyModifiedNodes = [];
456 if (updateBreadcrumbs)
457 this.updateBreadcrumb(true);
460 _stylesPaneEdited: function()
462 // Once styles are edited, the Metrics pane should be updated.
463 this.sidebarPanes.metrics.needsUpdate = true;
464 this.updateMetrics();
467 _metricsPaneEdited: function()
469 // Once metrics are edited, the Styles pane should be updated.
470 this.sidebarPanes.styles.needsUpdate = true;
471 this.updateStyles(true);
474 _styleSheetChanged: function()
476 this._metricsPaneEdited();
477 this._stylesPaneEdited();
480 _mouseMovedInCrumbs: function(event)
482 var nodeUnderMouse = document.elementFromPoint(event.pageX, event.pageY);
483 var crumbElement = nodeUnderMouse.enclosingNodeOrSelfWithClass("crumb");
485 WebInspector.highlightDOMNode(crumbElement ? crumbElement.representedObject.id : 0);
487 if ("_mouseOutOfCrumbsTimeout" in this) {
488 clearTimeout(this._mouseOutOfCrumbsTimeout);
489 delete this._mouseOutOfCrumbsTimeout;
493 _mouseMovedOutOfCrumbs: function(event)
495 var nodeUnderMouse = document.elementFromPoint(event.pageX, event.pageY);
496 if (nodeUnderMouse && nodeUnderMouse.isDescendant(this.crumbsElement))
499 WebInspector.highlightDOMNode(0);
501 this._mouseOutOfCrumbsTimeout = setTimeout(this.updateBreadcrumbSizes.bind(this), 1000);
504 updateBreadcrumb: function(forceUpdate)
509 var crumbs = this.crumbsElement;
512 var foundRoot = false;
513 var crumb = crumbs.firstChild;
515 if (crumb.representedObject === this.rootDOMNode)
519 crumb.addStyleClass("dimmed");
521 crumb.removeStyleClass("dimmed");
523 if (crumb.representedObject === this.focusedDOMNode) {
524 crumb.addStyleClass("selected");
527 crumb.removeStyleClass("selected");
530 crumb = crumb.nextSibling;
533 if (handled && !forceUpdate) {
534 // We don't need to rebuild the crumbs, but we need to adjust sizes
535 // to reflect the new focused or root node.
536 this.updateBreadcrumbSizes();
540 crumbs.removeChildren();
544 function selectCrumbFunction(event)
546 var crumb = event.currentTarget;
547 if (crumb.hasStyleClass("collapsed")) {
548 // Clicking a collapsed crumb will expose the hidden crumbs.
549 if (crumb === panel.crumbsElement.firstChild) {
550 // If the focused crumb is the first child, pick the farthest crumb
551 // that is still hidden. This allows the user to expose every crumb.
552 var currentCrumb = crumb;
553 while (currentCrumb) {
554 var hidden = currentCrumb.hasStyleClass("hidden");
555 var collapsed = currentCrumb.hasStyleClass("collapsed");
556 if (!hidden && !collapsed)
558 crumb = currentCrumb;
559 currentCrumb = currentCrumb.nextSibling;
563 panel.updateBreadcrumbSizes(crumb);
565 // Clicking a dimmed crumb or double clicking (event.detail >= 2)
566 // will change the root node in addition to the focused node.
567 if (event.detail >= 2 || crumb.hasStyleClass("dimmed"))
568 panel.rootDOMNode = crumb.representedObject.parentNode;
569 panel.focusedDOMNode = crumb.representedObject;
572 event.preventDefault();
576 for (var current = this.focusedDOMNode; current; current = current.parentNode) {
577 if (current.nodeType === Node.DOCUMENT_NODE)
580 if (current === this.rootDOMNode)
583 var crumb = document.createElement("span");
584 crumb.className = "crumb";
585 crumb.representedObject = current;
586 crumb.addEventListener("mousedown", selectCrumbFunction, false);
589 switch (current.nodeType) {
590 case Node.ELEMENT_NODE:
591 this.decorateNodeLabel(current, crumb);
595 if (isNodeWhitespace.call(current))
596 crumbTitle = WebInspector.UIString("(whitespace)");
598 crumbTitle = WebInspector.UIString("(text)");
601 case Node.COMMENT_NODE:
602 crumbTitle = "<!-->";
605 case Node.DOCUMENT_TYPE_NODE:
606 crumbTitle = "<!DOCTYPE>";
610 crumbTitle = this.treeOutline.nodeNameToCorrectCase(current.nodeName);
613 if (!crumb.childNodes.length) {
614 var nameElement = document.createElement("span");
615 nameElement.textContent = crumbTitle;
616 crumb.appendChild(nameElement);
617 crumb.title = crumbTitle;
621 crumb.addStyleClass("dimmed");
622 if (current === this.focusedDOMNode)
623 crumb.addStyleClass("selected");
624 if (!crumbs.childNodes.length)
625 crumb.addStyleClass("end");
627 crumbs.appendChild(crumb);
630 if (crumbs.hasChildNodes())
631 crumbs.lastChild.addStyleClass("start");
633 this.updateBreadcrumbSizes();
636 decorateNodeLabel: function(node, parentElement)
638 var title = this.treeOutline.nodeNameToCorrectCase(node.nodeName);
640 var nameElement = document.createElement("span");
641 nameElement.textContent = title;
642 parentElement.appendChild(nameElement);
644 var idAttribute = node.getAttribute("id");
646 var idElement = document.createElement("span");
647 parentElement.appendChild(idElement);
649 var part = "#" + idAttribute;
651 idElement.appendChild(document.createTextNode(part));
653 // Mark the name as extra, since the ID is more important.
654 nameElement.className = "extra";
657 var classAttribute = node.getAttribute("class");
658 if (classAttribute) {
659 var classes = classAttribute.split(/\s+/);
660 var foundClasses = {};
662 if (classes.length) {
663 var classesElement = document.createElement("span");
664 classesElement.className = "extra";
665 parentElement.appendChild(classesElement);
667 for (var i = 0; i < classes.length; ++i) {
668 var className = classes[i];
669 if (className && !(className in foundClasses)) {
670 var part = "." + className;
672 classesElement.appendChild(document.createTextNode(part));
673 foundClasses[className] = true;
678 parentElement.title = title;
681 linkifyNodeReference: function(node)
683 var link = document.createElement("span");
684 link.className = "node-link";
685 this.decorateNodeLabel(node, link);
686 WebInspector.wireElementWithDOMNode(link, node.id);
690 linkifyNodeById: function(nodeId)
692 var node = WebInspector.domAgent.nodeForId(nodeId);
694 return document.createTextNode(WebInspector.UIString("<node>"));
695 return this.linkifyNodeReference(node);
698 updateBreadcrumbSizes: function(focusedCrumb)
703 if (document.body.offsetWidth <= 0) {
704 // The stylesheet hasn't loaded yet or the window is closed,
705 // so we can't calculate what is need. Return early.
709 var crumbs = this.crumbsElement;
710 if (!crumbs.childNodes.length || crumbs.offsetWidth <= 0)
711 return; // No crumbs, do nothing.
713 // A Zero index is the right most child crumb in the breadcrumb.
714 var selectedIndex = 0;
715 var focusedIndex = 0;
719 var crumb = crumbs.firstChild;
721 // Find the selected crumb and index.
722 if (!selectedCrumb && crumb.hasStyleClass("selected")) {
723 selectedCrumb = crumb;
727 // Find the focused crumb index.
728 if (crumb === focusedCrumb)
731 // Remove any styles that affect size before
732 // deciding to shorten any crumbs.
733 if (crumb !== crumbs.lastChild)
734 crumb.removeStyleClass("start");
735 if (crumb !== crumbs.firstChild)
736 crumb.removeStyleClass("end");
738 crumb.removeStyleClass("compact");
739 crumb.removeStyleClass("collapsed");
740 crumb.removeStyleClass("hidden");
742 crumb = crumb.nextSibling;
746 // Restore the start and end crumb classes in case they got removed in coalesceCollapsedCrumbs().
747 // The order of the crumbs in the document is opposite of the visual order.
748 crumbs.firstChild.addStyleClass("end");
749 crumbs.lastChild.addStyleClass("start");
751 function crumbsAreSmallerThanContainer()
753 var rightPadding = 20;
754 var errorWarningElement = document.getElementById("error-warning-count");
755 if (!WebInspector.drawer.visible && errorWarningElement)
756 rightPadding += errorWarningElement.offsetWidth;
757 return ((crumbs.totalOffsetLeft + crumbs.offsetWidth + rightPadding) < window.innerWidth);
760 if (crumbsAreSmallerThanContainer())
761 return; // No need to compact the crumbs, they all fit at full size.
764 var AncestorSide = -1;
767 function makeCrumbsSmaller(shrinkingFunction, direction, significantCrumb)
769 if (!significantCrumb)
770 significantCrumb = (focusedCrumb || selectedCrumb);
772 if (significantCrumb === selectedCrumb)
773 var significantIndex = selectedIndex;
774 else if (significantCrumb === focusedCrumb)
775 var significantIndex = focusedIndex;
777 var significantIndex = 0;
778 for (var i = 0; i < crumbs.childNodes.length; ++i) {
779 if (crumbs.childNodes[i] === significantCrumb) {
780 significantIndex = i;
786 function shrinkCrumbAtIndex(index)
788 var shrinkCrumb = crumbs.childNodes[index];
789 if (shrinkCrumb && shrinkCrumb !== significantCrumb)
790 shrinkingFunction(shrinkCrumb);
791 if (crumbsAreSmallerThanContainer())
792 return true; // No need to compact the crumbs more.
796 // Shrink crumbs one at a time by applying the shrinkingFunction until the crumbs
797 // fit in the container or we run out of crumbs to shrink.
799 // Crumbs are shrunk on only one side (based on direction) of the signifcant crumb.
800 var index = (direction > 0 ? 0 : crumbs.childNodes.length - 1);
801 while (index !== significantIndex) {
802 if (shrinkCrumbAtIndex(index))
804 index += (direction > 0 ? 1 : -1);
807 // Crumbs are shrunk in order of descending distance from the signifcant crumb,
808 // with a tie going to child crumbs.
810 var endIndex = crumbs.childNodes.length - 1;
811 while (startIndex != significantIndex || endIndex != significantIndex) {
812 var startDistance = significantIndex - startIndex;
813 var endDistance = endIndex - significantIndex;
814 if (startDistance >= endDistance)
815 var index = startIndex++;
817 var index = endIndex--;
818 if (shrinkCrumbAtIndex(index))
823 // We are not small enough yet, return false so the caller knows.
827 function coalesceCollapsedCrumbs()
829 var crumb = crumbs.firstChild;
830 var collapsedRun = false;
831 var newStartNeeded = false;
832 var newEndNeeded = false;
834 var hidden = crumb.hasStyleClass("hidden");
836 var collapsed = crumb.hasStyleClass("collapsed");
837 if (collapsedRun && collapsed) {
838 crumb.addStyleClass("hidden");
839 crumb.removeStyleClass("compact");
840 crumb.removeStyleClass("collapsed");
842 if (crumb.hasStyleClass("start")) {
843 crumb.removeStyleClass("start");
844 newStartNeeded = true;
847 if (crumb.hasStyleClass("end")) {
848 crumb.removeStyleClass("end");
855 collapsedRun = collapsed;
858 newEndNeeded = false;
859 crumb.addStyleClass("end");
863 crumb = crumb.nextSibling;
866 if (newStartNeeded) {
867 crumb = crumbs.lastChild;
869 if (!crumb.hasStyleClass("hidden")) {
870 crumb.addStyleClass("start");
873 crumb = crumb.previousSibling;
878 function compact(crumb)
880 if (crumb.hasStyleClass("hidden"))
882 crumb.addStyleClass("compact");
885 function collapse(crumb, dontCoalesce)
887 if (crumb.hasStyleClass("hidden"))
889 crumb.addStyleClass("collapsed");
890 crumb.removeStyleClass("compact");
892 coalesceCollapsedCrumbs();
895 function compactDimmed(crumb)
897 if (crumb.hasStyleClass("dimmed"))
901 function collapseDimmed(crumb)
903 if (crumb.hasStyleClass("dimmed"))
908 // When not focused on a crumb we can be biased and collapse less important
909 // crumbs that the user might not care much about.
911 // Compact child crumbs.
912 if (makeCrumbsSmaller(compact, ChildSide))
915 // Collapse child crumbs.
916 if (makeCrumbsSmaller(collapse, ChildSide))
919 // Compact dimmed ancestor crumbs.
920 if (makeCrumbsSmaller(compactDimmed, AncestorSide))
923 // Collapse dimmed ancestor crumbs.
924 if (makeCrumbsSmaller(collapseDimmed, AncestorSide))
928 // Compact ancestor crumbs, or from both sides if focused.
929 if (makeCrumbsSmaller(compact, (focusedCrumb ? BothSides : AncestorSide)))
932 // Collapse ancestor crumbs, or from both sides if focused.
933 if (makeCrumbsSmaller(collapse, (focusedCrumb ? BothSides : AncestorSide)))
939 // Compact the selected crumb.
940 compact(selectedCrumb);
941 if (crumbsAreSmallerThanContainer())
944 // Collapse the selected crumb as a last resort. Pass true to prevent coalescing.
945 collapse(selectedCrumb, true);
948 updateStyles: function(forceUpdate)
950 var stylesSidebarPane = this.sidebarPanes.styles;
951 var computedStylePane = this.sidebarPanes.computedStyle;
952 if ((!stylesSidebarPane.expanded && !computedStylePane.expanded) || !stylesSidebarPane.needsUpdate)
955 stylesSidebarPane.update(this.focusedDOMNode, null, forceUpdate);
956 stylesSidebarPane.needsUpdate = false;
959 updateMetrics: function()
961 var metricsSidebarPane = this.sidebarPanes.metrics;
962 if (!metricsSidebarPane.expanded || !metricsSidebarPane.needsUpdate)
965 metricsSidebarPane.update(this.focusedDOMNode);
966 metricsSidebarPane.needsUpdate = false;
969 updateProperties: function()
971 var propertiesSidebarPane = this.sidebarPanes.properties;
972 if (!propertiesSidebarPane.expanded || !propertiesSidebarPane.needsUpdate)
975 propertiesSidebarPane.update(this.focusedDOMNode);
976 propertiesSidebarPane.needsUpdate = false;
979 updateEventListeners: function()
981 var eventListenersSidebarPane = this.sidebarPanes.eventListeners;
982 if (!eventListenersSidebarPane.expanded || !eventListenersSidebarPane.needsUpdate)
985 eventListenersSidebarPane.update(this.focusedDOMNode);
986 eventListenersSidebarPane.needsUpdate = false;
989 _registerShortcuts: function()
991 var shortcut = WebInspector.KeyboardShortcut;
992 var section = WebInspector.shortcutsHelp.section(WebInspector.UIString("Elements Panel"));
994 shortcut.shortcutToString(shortcut.Keys.Up),
995 shortcut.shortcutToString(shortcut.Keys.Down)
997 section.addRelatedKeys(keys, WebInspector.UIString("Navigate elements"));
999 shortcut.shortcutToString(shortcut.Keys.Right),
1000 shortcut.shortcutToString(shortcut.Keys.Left)
1002 section.addRelatedKeys(keys, WebInspector.UIString("Expand/collapse"));
1003 section.addKey(shortcut.shortcutToString(shortcut.Keys.Enter), WebInspector.UIString("Edit attribute"));
1005 this.sidebarPanes.styles.registerShortcuts();
1008 handleShortcut: function(event)
1010 // Cmd/Control + Shift + C should be a shortcut to clicking the Node Search Button.
1011 // This shortcut matches Firebug.
1012 if (event.keyIdentifier === "U+0043") { // C key
1013 if (WebInspector.isMac())
1014 var isNodeSearchKey = event.metaKey && !event.ctrlKey && !event.altKey && event.shiftKey;
1016 var isNodeSearchKey = event.ctrlKey && !event.metaKey && !event.altKey && event.shiftKey;
1018 if (isNodeSearchKey) {
1019 this.toggleSearchingForNode();
1020 event.handled = true;
1026 handleCopyEvent: function(event)
1028 // Don't prevent the normal copy if the user has a selection.
1029 if (!window.getSelection().isCollapsed)
1031 event.clipboardData.clearData();
1032 event.preventDefault();
1033 DOMAgent.copyNode(this.focusedDOMNode.id);
1036 rightSidebarResizerDragStart: function(event)
1038 WebInspector.elementDragStart(this.sidebarElement, this.rightSidebarResizerDrag.bind(this), this.rightSidebarResizerDragEnd.bind(this), event, "col-resize");
1041 rightSidebarResizerDragEnd: function(event)
1043 WebInspector.elementDragEnd(event);
1044 this.saveSidebarWidth();
1047 rightSidebarResizerDrag: function(event)
1049 var x = event.pageX;
1050 var newWidth = Number.constrain(window.innerWidth - x, Preferences.minElementsSidebarWidth, window.innerWidth * 0.66);
1051 this.setSidebarWidth(newWidth);
1052 event.preventDefault();
1055 setSidebarWidth: function(newWidth)
1057 this.sidebarElement.style.width = newWidth + "px";
1058 this.contentElement.style.right = newWidth + "px";
1059 this.sidebarResizeElement.style.right = (newWidth - 3) + "px";
1060 this.treeOutline.updateSelection();
1063 updateFocusedNode: function(nodeId)
1065 var node = WebInspector.domAgent.nodeForId(nodeId);
1069 this.focusedDOMNode = node;
1070 this._nodeSearchButton.toggled = false;
1073 _setSearchingForNode: function(enabled)
1075 this._nodeSearchButton.toggled = enabled;
1078 setSearchingForNode: function(enabled)
1080 InspectorAgent.setSearchingForNode(enabled, this._setSearchingForNode.bind(this));
1083 toggleSearchingForNode: function()
1085 this.setSearchingForNode(!this._nodeSearchButton.toggled);
1088 elementsToRestoreScrollPositionsFor: function()
1090 return [ this.contentElement, this.sidebarElement ];
1094 WebInspector.ElementsPanel.prototype.__proto__ = WebInspector.Panel.prototype;