Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / EventListenersSidebarPane.js
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2009 Joseph Pecoraro
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /**
31  * @constructor
32  * @extends {WebInspector.SidebarPane}
33  */
34 WebInspector.EventListenersSidebarPane = function()
35 {
36     WebInspector.SidebarPane.call(this, WebInspector.UIString("Event Listeners"));
37     this.bodyElement.classList.add("events-pane");
38
39     this.sections = [];
40
41     var refreshButton = document.createElement("button");
42     refreshButton.className = "pane-title-button refresh";
43     refreshButton.addEventListener("click", this._refreshButtonClicked.bind(this), false);
44     refreshButton.title = WebInspector.UIString("Refresh");
45     this.titleElement.appendChild(refreshButton);
46
47     this.settingsSelectElement = document.createElement("select");
48     this.settingsSelectElement.className = "select-filter";
49
50     var option = document.createElement("option");
51     option.value = "all";
52     option.label = WebInspector.UIString("All Nodes");
53     this.settingsSelectElement.appendChild(option);
54
55     option = document.createElement("option");
56     option.value = "selected";
57     option.label = WebInspector.UIString("Selected Node Only");
58     this.settingsSelectElement.appendChild(option);
59
60     var filter = WebInspector.settings.eventListenersFilter.get();
61     if (filter === "all")
62         this.settingsSelectElement[0].selected = true;
63     else if (filter === "selected")
64         this.settingsSelectElement[1].selected = true;
65     this.settingsSelectElement.addEventListener("click", function(event) { event.consume() }, false);
66     this.settingsSelectElement.addEventListener("change", this._changeSetting.bind(this), false);
67
68     this.titleElement.appendChild(this.settingsSelectElement);
69
70     this._linkifier = new WebInspector.Linkifier();
71 }
72
73 WebInspector.EventListenersSidebarPane._objectGroupName = "event-listeners-sidebar-pane";
74
75 WebInspector.EventListenersSidebarPane.prototype = {
76     update: function(node)
77     {
78         RuntimeAgent.releaseObjectGroup(WebInspector.EventListenersSidebarPane._objectGroupName);
79         this._linkifier.reset();
80
81         var body = this.bodyElement;
82         body.removeChildren();
83         this.sections = [];
84
85         var self = this;
86         function callback(error, eventListeners) {
87             if (error)
88                 return;
89
90             var selectedNodeOnly = "selected" === WebInspector.settings.eventListenersFilter.get();
91             var sectionNames = [];
92             var sectionMap = {};
93             for (var i = 0; i < eventListeners.length; ++i) {
94                 var eventListener = eventListeners[i];
95                 if (selectedNodeOnly && (node.id !== eventListener.nodeId))
96                     continue;
97                 eventListener.node = WebInspector.domModel.nodeForId(eventListener.nodeId);
98                 delete eventListener.nodeId; // no longer needed
99                 if (/^function _inspectorCommandLineAPI_logEvent\(/.test(eventListener.handlerBody.toString()))
100                     continue; // ignore event listeners generated by monitorEvent
101                 var type = eventListener.type;
102                 var section = sectionMap[type];
103                 if (!section) {
104                     section = new WebInspector.EventListenersSection(type, node.id, self._linkifier);
105                     sectionMap[type] = section;
106                     sectionNames.push(type);
107                     self.sections.push(section);
108                 }
109                 section.addListener(eventListener);
110             }
111
112             if (sectionNames.length === 0) {
113                 var div = document.createElement("div");
114                 div.className = "info";
115                 div.textContent = WebInspector.UIString("No Event Listeners");
116                 body.appendChild(div);
117                 return;
118             }
119
120             sectionNames.sort();
121             for (var i = 0; i < sectionNames.length; ++i) {
122                 var section = sectionMap[sectionNames[i]];
123                 body.appendChild(section.element);
124             }
125         }
126
127         if (node)
128             node.eventListeners(WebInspector.EventListenersSidebarPane._objectGroupName, callback);
129         this._selectedNode = node;
130     },
131
132     willHide: function()
133     {
134         delete this._selectedNode;
135     },
136
137     _refreshButtonClicked: function()
138     {
139         if (!this._selectedNode)
140             return;
141         this.update(this._selectedNode);
142     },
143
144     _changeSetting: function()
145     {
146         var selectedOption = this.settingsSelectElement[this.settingsSelectElement.selectedIndex];
147         WebInspector.settings.eventListenersFilter.set(selectedOption.value);
148         this.update(this._selectedNode);
149     },
150
151     __proto__: WebInspector.SidebarPane.prototype
152 }
153
154 /**
155  * @constructor
156  * @extends {WebInspector.PropertiesSection}
157  */
158 WebInspector.EventListenersSection = function(title, nodeId, linkifier)
159 {
160     this.eventListeners = [];
161     this._nodeId = nodeId;
162     this._linkifier = linkifier;
163     WebInspector.PropertiesSection.call(this, title);
164
165     // Changed from a Properties List
166     this.propertiesElement.remove();
167     delete this.propertiesElement;
168     delete this.propertiesTreeOutline;
169
170     this._eventBars = document.createElement("div");
171     this._eventBars.className = "event-bars";
172     this.element.appendChild(this._eventBars);
173 }
174
175 WebInspector.EventListenersSection.prototype = {
176     addListener: function(eventListener)
177     {
178         var eventListenerBar = new WebInspector.EventListenerBar(eventListener, this._nodeId, this._linkifier);
179         this._eventBars.appendChild(eventListenerBar.element);
180     },
181
182     __proto__: WebInspector.PropertiesSection.prototype
183 }
184
185 /**
186  * @constructor
187  * @extends {WebInspector.ObjectPropertiesSection}
188  */
189 WebInspector.EventListenerBar = function(eventListener, nodeId, linkifier)
190 {
191     WebInspector.ObjectPropertiesSection.call(this, WebInspector.RemoteObject.fromPrimitiveValue(""));
192
193     this.eventListener = eventListener;
194     this._nodeId = nodeId;
195     this._setNodeTitle();
196     this._setFunctionSubtitle(linkifier);
197     this.editable = false;
198     this.element.className = "event-bar"; /* Changed from "section" */
199     this.headerElement.classList.add("source-code");
200     this.propertiesElement.className = "event-properties properties-tree source-code"; /* Changed from "properties" */
201 }
202
203 WebInspector.EventListenerBar.prototype = {
204     update: function()
205     {
206         /**
207          * @param {?WebInspector.RemoteObject} nodeObject
208          * @this {WebInspector.EventListenerBar}
209          */
210         function updateWithNodeObject(nodeObject)
211         {
212             var properties = [];
213
214             properties.push(WebInspector.RemoteObjectProperty.fromPrimitiveValue("type", this.eventListener.type));
215             properties.push(WebInspector.RemoteObjectProperty.fromPrimitiveValue("useCapture", this.eventListener.useCapture));
216             properties.push(WebInspector.RemoteObjectProperty.fromPrimitiveValue("isAttribute", this.eventListener.isAttribute));
217             if (nodeObject)
218                 properties.push(new WebInspector.RemoteObjectProperty("node", nodeObject));
219             if (typeof this.eventListener.handler !== "undefined") {
220                 var remoteObject = WebInspector.RemoteObject.fromPayload(this.eventListener.handler);
221                 properties.push(new WebInspector.RemoteObjectProperty("handler", remoteObject));
222             }
223             properties.push(WebInspector.RemoteObjectProperty.fromPrimitiveValue("listenerBody", this.eventListener.handlerBody));
224             if (this.eventListener.sourceName)
225                 properties.push(WebInspector.RemoteObjectProperty.fromPrimitiveValue("sourceName", this.eventListener.sourceName));
226             properties.push(WebInspector.RemoteObjectProperty.fromPrimitiveValue("lineNumber", this.eventListener.location.lineNumber + 1));
227
228             this.updateProperties(properties);
229         }
230         WebInspector.RemoteObject.resolveNode(this.eventListener.node, WebInspector.EventListenersSidebarPane._objectGroupName, updateWithNodeObject.bind(this));
231     },
232
233     _setNodeTitle: function()
234     {
235         var node = this.eventListener.node;
236         if (!node)
237             return;
238
239         if (node.nodeType() === Node.DOCUMENT_NODE) {
240             this.titleElement.textContent = "document";
241             return;
242         }
243
244         if (node.id === this._nodeId) {
245             this.titleElement.textContent = WebInspector.DOMPresentationUtils.simpleSelector(node);
246             return;
247         }
248
249         this.titleElement.removeChildren();
250         this.titleElement.appendChild(WebInspector.DOMPresentationUtils.linkifyNodeReference(this.eventListener.node));
251     },
252
253     _setFunctionSubtitle: function(linkifier)
254     {
255         this.subtitleElement.removeChildren();
256         var urlElement = linkifier.linkifyRawLocation(this.eventListener.location);
257         if (!urlElement) {
258             var url = this.eventListener.sourceName;
259             var lineNumber = this.eventListener.location.lineNumber;
260             var columnNumber = 0;
261             urlElement = linkifier.linkifyLocation(url, lineNumber, columnNumber);
262         }
263         this.subtitleElement.appendChild(urlElement);
264     },
265
266     __proto__: WebInspector.ObjectPropertiesSection.prototype
267 }