diff 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
line wrap: on
line diff
--- a/js/black-box.js	Tue Apr 27 09:59:15 2010 -0700
+++ b/js/black-box.js	Tue Apr 27 23:06:29 2010 -0700
@@ -1,5 +1,29 @@
+function getXPathForElement(el, xml) {
+  var xpath = "";
+  var pos, tempitem2;
+  
+  while (el !== xml.documentElement) {    
+    pos = 0;
+    tempitem2 = el;
+    while (tempitem2) {
+      if (tempitem2.nodeType === 1 && tempitem2.nodeName === el.nodeName) {
+        // If it is ELEMENT_NODE of the same name
+        pos += 1;
+      }
+      tempitem2 = tempitem2.previousSibling;
+    }
+    
+    xpath = el.nodeName + "[" + pos + "]" + "/" + xpath;
+
+    el = el.parentNode;
+  }
+  xpath = "/" + xml.documentElement.nodeName + "/" + xpath;
+  xpath = xpath.replace(/\/$/, '');
+  return xpath;
+}
+
 function onDashboardLoaded(dashboard, options) {
-  var require = Require.build();
+  var require = Require.build(Require.modules, {window: window});
 
   // Needed for Firebug, which won't log iframe errors to the console.
   $(dashboard).error(
@@ -7,11 +31,64 @@
       console.warn("An error occurred in the dashboard iframe.");
     });
 
+  function DOMElementToCSSSelector(element) {
+    if (element.id)
+      return "#" + element.id;
+
+    var document = element.ownerDocument;
+
+    function isUnique(selector) {
+      return (document.querySelectorAll(selector).length == 1);
+    }
+
+    var parent = element.parentNode;
+    while (parent) {
+      if (parent.id)
+        break;
+      parent = parent.parentNode;
+    }
+
+    if (parent && parent.id) {
+      var selector = "#" + parent.id;
+
+      var list = element.classList;
+      for (var i = 0; i < list.length; i++) {
+        selector += " ." + list[i];
+        if (isUnique(selector))
+          return selector;
+      }
+    }
+    return null;
+  }
+
+  dashboard.addEventListener(
+    "mousedown",
+    function(event) {
+      var document = event.target.ownerDocument;
+      var xpath = getXPathForElement(event.target, document);
+      var result = document.evaluate(xpath, document, null,
+                                     XPathResult.ANY_TYPE, null);
+      console.log("mousedown", event.target,
+                  DOMElementToCSSSelector(event.target),
+                  xpath,
+                  result.iterateNext());
+    },
+    true
+  );
+
   var moduleExports = {};
   var dbrequire = dashboard.Require.build(dashboard.Require.modules,
                                           moduleExports);
-  options.cache = require("mocks/cache").create();
-  options.Bugzilla = require("mocks/bugzilla").create(options.Bugzilla);
+
+  function delegate(method, args) {
+    //console.log(method, args);
+  }
+
+  var ajaxImpl = require("mocks/bugzilla/trivial").makeAjaxImpl();
+  options.cache = require("mocks/cache").create(delegate);
+  options.Bugzilla = require("mocks/bugzilla").create(options.Bugzilla,
+                                                      ajaxImpl,
+                                                      delegate);
   dbrequire("date-utils").now = function() {
     return new Date("Tue Apr 27 2010 09:00:00 GMT");
   };