comparison js/black-box.js @ 102:00b02ba5236c

made all delegates have a dotted name (namespaced), modified black-box.js a bit
author Atul Varma <avarma@mozilla.com>
date Wed, 28 Apr 2010 20:06:26 -0700
parents 106a0018e601
children a994fc89f341
comparison
equal deleted inserted replaced
101:106a0018e601 102:00b02ba5236c
1 function getXPathForElement(el, xml) {
2 var xpath = "";
3 var pos, tempitem2;
4
5 while (el !== xml.documentElement) {
6 pos = 0;
7 tempitem2 = el;
8 while (tempitem2) {
9 if (tempitem2.nodeType === 1 && tempitem2.nodeName === el.nodeName) {
10 // If it is ELEMENT_NODE of the same name
11 pos += 1;
12 }
13 tempitem2 = tempitem2.previousSibling;
14 }
15
16 xpath = el.nodeName + "[" + pos + "]" + "/" + xpath;
17
18 el = el.parentNode;
19 }
20 xpath = "/" + xml.documentElement.nodeName + "/" + xpath;
21 xpath = xpath.replace(/\/$/, '');
22 return xpath;
23 }
24
25 function onDashboardLoaded(dashboard, options) { 1 function onDashboardLoaded(dashboard, options) {
26 var require = Require.build(Require.modules, {window: window}); 2 var require = Require.build(Require.modules, {window: window});
27 3
28 // Needed for Firebug, which won't log iframe errors to the console. 4 // Needed for Firebug, which won't log iframe errors to the console.
29 $(dashboard).error( 5 $(dashboard).error(
30 function(event) { 6 function(event) {
31 console.warn("An error occurred in the dashboard iframe."); 7 console.warn("An error occurred in the dashboard iframe.");
32 }); 8 });
33 9
34 function DOMElementToCSSSelector(element) {
35 if (element.id)
36 return "#" + element.id;
37
38 var document = element.ownerDocument;
39
40 function isUnique(selector) {
41 return (document.querySelectorAll(selector).length == 1);
42 }
43
44 var parent = element.parentNode;
45 while (parent) {
46 if (parent.id)
47 break;
48 parent = parent.parentNode;
49 }
50
51 if (parent && parent.id) {
52 var selector = "#" + parent.id;
53
54 var list = element.classList;
55 for (var i = 0; i < list.length; i++) {
56 selector += " ." + list[i];
57 if (isUnique(selector))
58 return selector;
59 }
60 }
61 return null;
62 }
63
64 dashboard.addEventListener(
65 "mousedown",
66 function(event) {
67 var document = event.target.ownerDocument;
68 var xpath = getXPathForElement(event.target, document);
69 var result = document.evaluate(xpath, document, null,
70 XPathResult.ANY_TYPE, null);
71 console.log("mousedown", event.target,
72 DOMElementToCSSSelector(event.target),
73 xpath,
74 result.iterateNext());
75 },
76 true
77 );
78
79 var moduleExports = {}; 10 var moduleExports = {};
80 var dbrequire = dashboard.Require.build(dashboard.Require.modules, 11 var dbrequire = dashboard.Require.build(dashboard.Require.modules,
81 moduleExports); 12 moduleExports);
82 13
83 function delegate(method, args) { 14 function delegate(method, args) {
84 //console.log(method, args); 15 console.log(method, args);
85 } 16 }
86 17
87 // Get rid of any form values cached by the browser. 18 // Get rid of any form values cached by the browser.
88 options.jQuery("input[type=text], input[type=password]").val(""); 19 options.jQuery("input[type=text], input[type=password]").val("");
89 20