2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009 Joseph Pecoraro
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 WebInspector.ObjectPropertiesSection = function(object, title, subtitle, emptyPlaceholder, ignoreHasOwnProperty, extraProperties, treeElementConstructor)
29 this.emptyPlaceholder = (emptyPlaceholder || WebInspector.UIString("No Properties"));
31 this.ignoreHasOwnProperty = ignoreHasOwnProperty;
32 this.extraProperties = extraProperties;
33 this.treeElementConstructor = treeElementConstructor || WebInspector.ObjectPropertyTreeElement;
36 WebInspector.PropertiesSection.call(this, title, subtitle);
39 WebInspector.ObjectPropertiesSection.prototype = {
40 onpopulate: function()
48 var callback = function(properties) {
51 self.updateProperties(properties);
53 this.object.getProperties(this.ignoreHasOwnProperty, true, callback);
56 updateProperties: function(properties, rootTreeElementConstructor, rootPropertyComparer)
58 if (!rootTreeElementConstructor)
59 rootTreeElementConstructor = this.treeElementConstructor;
61 if (!rootPropertyComparer)
62 rootPropertyComparer = WebInspector.ObjectPropertiesSection.CompareProperties;
64 if (this.extraProperties)
65 for (var i = 0; i < this.extraProperties.length; ++i)
66 properties.push(this.extraProperties[i]);
68 properties.sort(rootPropertyComparer);
70 this.propertiesTreeOutline.removeChildren();
72 for (var i = 0; i < properties.length; ++i) {
73 properties[i].parentObject = this.object;
74 this.propertiesTreeOutline.appendChild(new rootTreeElementConstructor(properties[i]));
77 if (!this.propertiesTreeOutline.children.length) {
78 var title = "<div class=\"info\">" + this.emptyPlaceholder + "</div>";
79 var infoElement = new TreeElement(null, null, false);
80 infoElement.titleHTML = title;
81 this.propertiesTreeOutline.appendChild(infoElement);
83 this.propertiesForTest = properties;
87 WebInspector.ObjectPropertiesSection.prototype.__proto__ = WebInspector.PropertiesSection.prototype;
89 WebInspector.ObjectPropertiesSection.CompareProperties = function(propertyA, propertyB)
91 var a = propertyA.name;
92 var b = propertyB.name;
93 if (a === "__proto__")
95 if (b === "__proto__")
98 // if used elsewhere make sure to
99 // - convert a and b to strings (not needed here, properties are all strings)
100 // - check if a == b (not needed here, no two properties can be the same)
103 var chunk = /^\d+|^\D+/;
104 var chunka, chunkb, anum, bnum;
110 chunka = a.match(chunk)[0];
111 chunkb = b.match(chunk)[0];
112 anum = !isNaN(chunka);
113 bnum = !isNaN(chunkb);
119 diff = chunka - chunkb;
120 if (diff === 0 && chunka.length !== chunkb.length) {
121 if (!+chunka && !+chunkb) // chunks are strings of all 0s (special case)
122 return chunka.length - chunkb.length;
124 return chunkb.length - chunka.length;
126 } else if (chunka !== chunkb)
127 return (chunka < chunkb) ? -1 : 1;
128 a = a.substring(chunka.length);
129 b = b.substring(chunkb.length);
134 WebInspector.ObjectPropertyTreeElement = function(property)
136 this.property = property;
138 // Pass an empty title, the title gets made later in onattach.
139 TreeElement.call(this, "", null, false);
142 WebInspector.ObjectPropertyTreeElement.prototype = {
143 onpopulate: function()
145 if (this.children.length && !this.shouldRefreshChildren)
148 var callback = function(properties) {
149 this.removeChildren();
153 properties.sort(WebInspector.ObjectPropertiesSection.CompareProperties);
154 for (var i = 0; i < properties.length; ++i) {
155 this.appendChild(new this.treeOutline.section.treeElementConstructor(properties[i]));
158 this.property.value.getOwnProperties(true, callback.bind(this));
161 ondblclick: function(event)
173 this.nameElement = document.createElement("span");
174 this.nameElement.className = "name";
175 this.nameElement.textContent = this.property.name;
177 var separatorElement = document.createElement("span");
178 separatorElement.className = "separator";
179 separatorElement.textContent = ": ";
181 this.valueElement = document.createElement("span");
182 this.valueElement.className = "value";
184 var description = this.property.value.description;
185 // Render \n as a nice unicode cr symbol.
186 if (this.property.value.type === "string" && typeof description === "string")
187 description = description.replace(/\n/g, "\u21B5");
188 this.valueElement.textContent = description;
190 if (this.property.isGetter)
191 this.valueElement.addStyleClass("dimmed");
192 if (this.property.value.isError())
193 this.valueElement.addStyleClass("error");
194 if (this.property.value.type)
195 this.valueElement.addStyleClass("console-formatted-" + this.property.value.type);
196 if (this.property.value.type === "node")
197 this.valueElement.addEventListener("contextmenu", this._contextMenuEventFired.bind(this), false);
199 this.listItemElement.removeChildren();
201 this.listItemElement.appendChild(this.nameElement);
202 this.listItemElement.appendChild(separatorElement);
203 this.listItemElement.appendChild(this.valueElement);
204 this.hasChildren = this.property.value.hasChildren;
207 _contextMenuEventFired: function()
209 function selectNode(nodeId)
212 WebInspector.panels.elements.switchToAndFocus(WebInspector.domAgent.nodeForId(nodeId));
216 function revealElement()
218 this.property.value.pushNodeToFrontend(selectNode);
221 var contextMenu = new WebInspector.ContextMenu();
222 contextMenu.appendItem(WebInspector.UIString("Reveal in Elements Panel"), revealElement.bind(this));
223 contextMenu.show(event);
226 updateSiblings: function()
228 if (this.parent.root)
229 this.treeOutline.section.update();
231 this.parent.shouldRefreshChildren = true;
234 startEditing: function()
236 if (WebInspector.isBeingEdited(this.valueElement) || !this.treeOutline.section.editable)
239 var context = { expanded: this.expanded };
241 // Lie about our children to prevent expanding on double click and to collapse subproperties.
242 this.hasChildren = false;
244 this.listItemElement.addStyleClass("editing-sub-part");
246 WebInspector.startEditing(this.valueElement, {
248 commitHandler: this.editingCommitted.bind(this),
249 cancelHandler: this.editingCancelled.bind(this)
253 editingEnded: function(context)
255 this.listItemElement.scrollLeft = 0;
256 this.listItemElement.removeStyleClass("editing-sub-part");
257 if (context.expanded)
261 editingCancelled: function(element, context)
264 this.editingEnded(context);
267 editingCommitted: function(element, userInput, previousContent, context)
269 if (userInput === previousContent)
270 return this.editingCancelled(element, context); // nothing changed, so cancel
272 this.applyExpression(userInput, true);
274 this.editingEnded(context);
277 applyExpression: function(expression, updateInterface)
279 expression = expression.trim();
280 var expressionLength = expression.length;
282 var callback = function(success) {
283 if (!updateInterface)
289 if (!expressionLength) {
290 // The property was deleted, so remove this tree element.
291 self.parent.removeChild(this);
293 // Call updateSiblings since their value might be based on the value that just changed.
294 self.updateSiblings();
297 this.property.parentObject.setPropertyValue(this.property.name, expression.trim(), callback);
301 WebInspector.ObjectPropertyTreeElement.prototype.__proto__ = TreeElement.prototype;