2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @extends {WebInspector.Object}
34 * @param {!WebInspector.LayerTreeModel} model
35 * @param {!TreeOutline} treeOutline
37 WebInspector.LayerTree = function(model, treeOutline)
39 WebInspector.Object.call(this);
41 this._treeOutline = treeOutline;
42 this._treeOutline.childrenListElement.addEventListener("mousemove", this._onMouseMove.bind(this), false);
43 this._treeOutline.childrenListElement.addEventListener("mouseout", this._onMouseMove.bind(this), false);
44 this._treeOutline.childrenListElement.addEventListener("contextmenu", this._onContextMenu.bind(this), true);
45 this._model.addEventListener(WebInspector.LayerTreeModel.Events.LayerTreeChanged, this._update.bind(this));
46 this._lastHoveredNode = null;
52 WebInspector.LayerTree.Events = {
53 LayerHovered: "LayerHovered",
54 LayerSelected: "LayerSelected"
57 WebInspector.LayerTree.prototype = {
59 * @param {!WebInspector.Layer} layer
61 selectLayer: function(layer)
63 this.hoverLayer(null);
64 var node = layer && this._treeOutline.getCachedTreeElement(layer);
66 node.revealAndSelect(true);
67 else if (this._treeOutline.selectedTreeElement)
68 this._treeOutline.selectedTreeElement.deselect();
72 * @param {?WebInspector.Layer} layer
74 hoverLayer: function(layer)
76 var node = layer && this._treeOutline.getCachedTreeElement(layer);
77 if (node === this._lastHoveredNode)
79 if (this._lastHoveredNode)
80 this._lastHoveredNode.setHovered(false);
82 node.setHovered(true);
83 this._lastHoveredNode = node;
91 * @param {!WebInspector.Layer} layer
92 * @this {WebInspector.LayerTree}
94 function updateLayer(layer)
98 console.assert(false, "Duplicate layer id: " + id);
99 seenLayers[id] = true;
100 var node = this._treeOutline.getCachedTreeElement(layer);
101 var parent = layer === this._model.contentRoot() ? this._treeOutline : this._treeOutline.getCachedTreeElement(layer.parent());
103 console.assert(false, "Parent is not in the tree");
105 node = new WebInspector.LayerTreeElement(this, layer);
106 parent.appendChild(node);
108 var oldParentId = node.parent.representedObject && node.parent.representedObject.id();
109 if (oldParentId !== layer.parentId()) {
110 (node.parent || this._treeOutline).removeChild(node);
111 parent.appendChild(node);
116 if (this._model.contentRoot())
117 this._model.forEachLayer(updateLayer.bind(this), this._model.contentRoot());
118 // Cleanup layers that don't exist anymore from tree.
119 for (var node = /** @type {!TreeElement|!TreeOutline|null} */(this._treeOutline.children[0]); node && !node.root;) {
120 if (seenLayers[node.representedObject.id()]) {
121 node = node.traverseNextTreeElement(false);
123 var nextNode = node.nextSibling || node.parent;
124 node.parent.removeChild(node);
125 if (node === this._lastHoveredNode)
126 this._lastHoveredNode = null;
133 * @param {?Event} event
135 _onMouseMove: function(event)
137 var node = this._treeOutline.treeElementFromPoint(event.pageX, event.pageY);
138 if (node === this._lastHoveredNode)
140 this.dispatchEventToListeners(WebInspector.LayerTree.Events.LayerHovered, node && node.representedObject);
144 * @param {!WebInspector.LayerTreeElement} node
146 _selectedNodeChanged: function(node)
148 var layer = /** @type {!WebInspector.Layer} */ (node.representedObject);
149 this.dispatchEventToListeners(WebInspector.LayerTree.Events.LayerSelected, layer);
153 * @param {?Event} event
155 _onContextMenu: function(event)
157 var node = this._treeOutline.treeElementFromPoint(event.pageX, event.pageY);
158 if (!node || !node.representedObject)
160 var layer = /** @type {!WebInspector.Layer} */ (node.representedObject);
163 var nodeId = layer.nodeId();
166 var domNode = WebInspector.domAgent.nodeForId(nodeId);
169 var contextMenu = new WebInspector.ContextMenu(event);
170 contextMenu.appendApplicableItems(domNode);
174 __proto__: WebInspector.Object.prototype
179 * @param {!WebInspector.LayerTree} tree
180 * @param {!WebInspector.Layer} layer
181 * @extends {TreeElement}
183 WebInspector.LayerTreeElement = function(tree, layer)
185 TreeElement.call(this, "", layer);
186 this._layerTree = tree;
190 WebInspector.LayerTreeElement.prototype = {
193 var selection = document.createElement("div");
194 selection.className = "selection";
195 this.listItemElement.insertBefore(selection, this.listItemElement.firstChild);
200 var layer = /** @type {!WebInspector.Layer} */ (this.representedObject);
201 var nodeId = layer.nodeIdForSelfOrAncestor();
202 var node = nodeId ? WebInspector.domAgent.nodeForId(nodeId) : null;
203 var title = document.createDocumentFragment();
204 title.createChild("div", "selection");
205 title.appendChild(document.createTextNode(node ? WebInspector.DOMPresentationUtils.fullQualifiedSelector(node, false) : "#" + layer.id()));
206 var details = title.createChild("span", "dimmed");
207 details.textContent = WebInspector.UIString(" (%d × %d)", layer.width(), layer.height());
217 this._layerTree._selectedNodeChanged(this);
222 * @param {boolean} hovered
224 setHovered: function(hovered)
226 this.listItemElement.enableStyleClass("hovered", hovered);
229 __proto__: TreeElement.prototype