Skip to content

Commit fcc6ff4

Browse files
committed
Properly listen to data-modified events
1 parent f76297a commit fcc6ff4

File tree

4 files changed

+65
-49
lines changed

4 files changed

+65
-49
lines changed

chrome/resources/jquery2-patch.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
newValues: newValues
2626
};*/
2727
// send event for each matched element
28-
this.each(function() {
29-
var event = new CustomEvent("firequery-event"/*, { "detail": detail }*/);
30-
this.dispatchEvent(event);
31-
});
28+
//this.each(function() {
29+
// var event = new CustomEvent("firequery-event"/*, { "detail": detail }*/);
30+
// this.dispatchEvent(event);
31+
//});
3232
// simulate original return value
3333
return res;
3434
};
3535
};
3636

37-
$.fn.data = wrap(originalDataFn);
38-
$.fn.removeData = wrap(originalRemoveDataFn);
37+
//$.fn.data = wrap(originalDataFn);
38+
//$.fn.removeData = wrap(originalRemoveDataFn);
3939

4040
// wrap data calls on jQuery object
4141

@@ -55,8 +55,11 @@
5555
newValues: newValues
5656
};*/
5757
// send event
58-
var event = new CustomEvent("firequery-event"/*, { "detail": detail }*/);
59-
elem.dispatchEvent(event);
58+
if (elem instanceof HTMLElement) {
59+
var event = new CustomEvent("firequery-event"/*, { "detail": detail }*/);
60+
elem.dispatchEvent(event);
61+
}
62+
6063
// simulate original return value
6164
return res;
6265
};

lib/firequery-actor.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
// Add-on SDK
12-
const { Cu, components } = require("chrome");
12+
const { Cu, Ci, components } = require("chrome");
1313

1414
// DevTools
1515
const { DebuggerServer } = Cu.import("resource://gre/modules/devtools/dbg-server.jsm", {});
@@ -315,12 +315,19 @@ var FireQueryActor = ActorClass(
315315
return;
316316
}
317317

318+
if (!(event.target instanceof Ci.nsIDOMElement)) {
319+
return;
320+
}
321+
318322
let walkerActor = this.conn.getActor(this.walkerActorID);
319323
let data = walkerActor.attachElement(event.target);
320324
let available = !!hasJQueryData(event.target);
321325

322-
Trace.sysout("FireQueryActor.onDataModified; " + win.location, {
323-
data: data,
326+
Trace.sysout("FireQueryActor.onDataModified; " + event.target + " " +
327+
win.location, {
328+
nodeName: event.target.nodeName,
329+
nodeId: event.target.id,
330+
nodeClass: event.target.className,
324331
hasJQueryData: available
325332
});
326333

lib/firequery-toolbox-overlay.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ const FireQueryToolboxOverlay = Class(
103103
Rdp.registerTabActor(client, config).then(({registrar, front}) => {
104104
Trace.sysout("FireQueryToolboxOverlay.attach; READY", this);
105105

106-
this.front = front;
107-
108106
// xxxHonza: Unregister at shutdown
109107
this.registrar = registrar;
108+
this.front = front;
110109

111110
// Patch jQuery on the backend and resolve when it's done.
111+
// xxxHonza: This should be done as part of the 'attach' packet.
112112
this.patchJQuery().then(() => {
113113
emit(this, "attach", front);
114114
this.deferredAttach.resolve(front);

lib/inspector-overlay.js

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const self = require("sdk/self");
1111
const { Cu, Ci } = require("chrome");
1212
const { Class } = require("sdk/core/heritage");
1313
const { loadSheet, removeSheet } = require("sdk/stylesheet/utils");
14-
const { on, off } = require("sdk/event/core");
14+
const { on, off, once } = require("sdk/event/core");
15+
const { defer } = require("sdk/core/promise");
1516

1617
// DevTools
1718
const { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
@@ -70,20 +71,12 @@ const InspectorOverlay = Class(
7071
// Tooltip events
7172
this.onClickTooltip = this.onClickTooltip.bind(this);
7273

73-
// FireQueryToolboxOverlay events
74-
this.onAttach = this.onAttach.bind(this);
75-
this.onDetach = this.onDetach.bind(this);
76-
7774
// Backend events
7875
this.onDataModified = this.onDataModified.bind(this);
7976
},
8077

8178
destroy: function() {
8279
Trace.sysout("InspectorOverlay.destroy;", arguments);
83-
84-
let toolboxOverlay = this.context.getOverlay(ToolboxOverlayId);
85-
off(toolboxOverlay, "attach", this.onAttach);
86-
off(toolboxOverlay, "detach", this.onDetach);
8780
},
8881

8982
// Overlay Events
@@ -99,11 +92,27 @@ const InspectorOverlay = Class(
9992
this.panel.on("inspector-updated", this.onInspectorUpdated);
10093
this.panel.on("container-created", this.onMarkupViewContainerCreated)
10194

102-
// Listen to {@FireQueryToolboxOverlay} events related to
103-
// backend actor attach and detach.
95+
// Listen to data-modified events from the backend actor. These
96+
// events are used to update the indicator icon for elements
97+
// with jQuery.data
98+
this.getFrontWhenReady().then(front => {
99+
front.on("data-modified", this.onDataModified);
100+
});
101+
102+
// Update the Inspector panel when 'attach' is fired. The event
103+
// might be already gone at this point, which is fine and in
104+
// that case the panel will be properly initialized since
105+
// the backend is already setup.
106+
// Otherwise, if the 'Inspector' panel is the default the backend
107+
// registration needs usually more time and happens when the
108+
// content is already there. In this case we need to refresh
109+
// panel content to make sure jQuery.data indicators (icons) are
110+
// also rendered.
111+
// xxxHonza: request better API (e.g. this.panel.refresh());
104112
let toolboxOverlay = this.context.getOverlay(ToolboxOverlayId);
105-
on(toolboxOverlay, "attach", this.onAttach);
106-
on(toolboxOverlay, "detach", this.onDetach);
113+
once(toolboxOverlay, "attach", () => {
114+
this.panel.onNewRoot();
115+
});
107116

108117
// Monkey patch the InspectorPanel.
109118
// xxxHonza: what about extension uninstall/disable?
@@ -117,6 +126,22 @@ const InspectorOverlay = Class(
117126
Trace.sysout("InspectorOverlay.onReady;", options);
118127
},
119128

129+
// FireQueryFront
130+
131+
getFrontWhenReady: function() {
132+
let deferred = defer();
133+
let toolboxOverlay = this.context.getOverlay(ToolboxOverlayId);
134+
let front = toolboxOverlay.front;
135+
if (front) {
136+
deferred.resolve(front);
137+
} else {
138+
once(toolboxOverlay, "attach", (front) => {
139+
deferred.resolve(front);
140+
});
141+
}
142+
return deferred.promise;
143+
},
144+
120145
// MarkupView Event Handlers
121146

122147
/**
@@ -255,36 +280,17 @@ const InspectorOverlay = Class(
255280
});
256281
},
257282

258-
// ToolboxOverlay Events
259-
260-
onAttach: function(front) {
261-
Trace.sysout("InspectorOverlay.onAttach;", arguments);
262-
263-
front.on("data-modified", this.onDataModified);
264-
265-
// Update the markup view. It might happen that the backend
266-
// registration is done after the panel is already displayed.
267-
// To improve the first-time experience - the panel is auto
268-
// refreshed so, all jQuery.data indicators are properly
269-
// rendered within the panel.
270-
// xxxHonza: request better API (e.g. this.panel.refresh());
271-
this.panel.onNewRoot();
272-
},
273-
274-
onDetach: function() {
275-
Trace.sysout("InspectorOverlay.onDetach;", arguments);
276-
},
277-
278283
// Backend Events
279284

280285
onDataModified: function(nodeData, hasJQueryData) {
281-
Trace.sysout("InspectorOverlay.onDataModified; has data: " +
282-
hasJQueryData);
283-
284286
let markupView = this.panel.markup;
285287
let client = this.toolbox.target.client;
286288
let nodeFront = nodeData.node;
287289

290+
Trace.sysout("InspectorOverlay.onDataModified; " +
291+
nodeFront.nodeName + "#" + nodeFront.id +
292+
", has data: " + hasJQueryData, nodeFront);
293+
288294
// The container doesn't have to be always visible/available.
289295
let container = markupView.getContainer(nodeFront);
290296
if (!container) {

0 commit comments

Comments
 (0)