comparison js/black-box.js @ 100:c486d35fad27

added more mocks; black-box now talks to a really simple fake bugzilla 'server'.
author Atul Varma <avarma@mozilla.com>
date Tue, 27 Apr 2010 23:06:29 -0700
parents 544d339d2b4c
children 106a0018e601
comparison
equal deleted inserted replaced
99:544d339d2b4c 100:c486d35fad27
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
1 function onDashboardLoaded(dashboard, options) { 25 function onDashboardLoaded(dashboard, options) {
2 var require = Require.build(); 26 var require = Require.build(Require.modules, {window: window});
3 27
4 // Needed for Firebug, which won't log iframe errors to the console. 28 // Needed for Firebug, which won't log iframe errors to the console.
5 $(dashboard).error( 29 $(dashboard).error(
6 function(event) { 30 function(event) {
7 console.warn("An error occurred in the dashboard iframe."); 31 console.warn("An error occurred in the dashboard iframe.");
8 }); 32 });
9 33
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
10 var moduleExports = {}; 79 var moduleExports = {};
11 var dbrequire = dashboard.Require.build(dashboard.Require.modules, 80 var dbrequire = dashboard.Require.build(dashboard.Require.modules,
12 moduleExports); 81 moduleExports);
13 options.cache = require("mocks/cache").create(); 82
14 options.Bugzilla = require("mocks/bugzilla").create(options.Bugzilla); 83 function delegate(method, args) {
84 //console.log(method, args);
85 }
86
87 var ajaxImpl = require("mocks/bugzilla/trivial").makeAjaxImpl();
88 options.cache = require("mocks/cache").create(delegate);
89 options.Bugzilla = require("mocks/bugzilla").create(options.Bugzilla,
90 ajaxImpl,
91 delegate);
15 dbrequire("date-utils").now = function() { 92 dbrequire("date-utils").now = function() {
16 return new Date("Tue Apr 27 2010 09:00:00 GMT"); 93 return new Date("Tue Apr 27 2010 09:00:00 GMT");
17 }; 94 };
18 dbrequire("app/loader").init(moduleExports, options); 95 dbrequire("app/loader").init(moduleExports, options);
19 } 96 }