const Cu = Components.utils;
Cu.import("resource:///modules/devtools/SideMenuWidget.jsm");
Cu.import("resource:///modules/devtools/ViewHelpers.jsm");
// Listen for clicks on the first toolbar
let toolbar = document.getElementById("memtool-toolbar");
let currentView = "";
toolbar.addEventListener("click", function (event) {
let view = event.target.getAttribute("view");
if (view) {
currentView = view;
update(view);
}
});
// Listen for clicks on the second toolbar
let actions = document.getElementById("memtool-actions");
actions.addEventListener("click", function (event) {
let action = event.target.getAttribute("action");
if (action == "refresh")
update(currentView);
else if (action)
execute(action);
});
// Listen for clicks on the menu list
var menu = new SideMenuWidget(document.getElementById("sources"));
menu.addEventListener("click", function (e) {
let object = e.target.object;
if (object)
dumpObject(object);
});
// Listen for clicks on links
let currentAnalyzer = null;
document.documentElement.addEventListener("click", function (e) {
let a = e.target;
if (a.tagName == "a" && a.object) {
// Check if already inserted, remove instead of adding
let div = a.parentNode.querySelector("div");
if (div)
div.parentNode.removeChild(div);
else
insertObject(a.parentNode, a.object);
// Prevent displaying an non-existant document
e.preventDefault();
}
}, true);
function updateObjectList(analyzer, objects) {
menu.removeAllItems();
currentAnalyzer = analyzer;
objects.forEach(function (obj) {
let label = document.createElement("label");
label.className = "side-menu-widget-item-label";
label.setAttribute("crop", "start");
label.setAttribute("flex", "1");
let description = obj.description;
if (!description) {
let m = obj.location.filename.match(/\/(\w+\.\w+)$/);
description = m[1] + ":" + obj.location.line;
}
label.setAttribute("value", description);
label.object = obj;
menu.insertItemAt(-1, label, description);
});
var container = document.getElementById("root");
container.innerHTML = "Done. <---- Now select an object to inspect.";
}
function dumpObject(obj) {
var container = document.getElementById("root");
container.innerHTML = "";
var li = document.createElementNS("http://www.w3.org/1999/xhtml", "li");
container.appendChild(li);
insertObject(li, obj);
}
function insertObject(parent, obj) {
var title;
if (obj.description)
title = obj.description;
else if (obj.location)
title = obj.location.filename + ":"+ obj.location.line;
var container = document.createElementNS("http://www.w3.org/1999/xhtml", "div");
parent.appendChild(container);
if (title) {
let b = document.createElementNS("http://www.w3.org/1999/xhtml", "b");
b.textContent = title;
container.appendChild(b);
container.appendChild(document.createElementNS("http://www.w3.org/1999/xhtml", "br"));
}
if (obj.pathToFragment) {
container.appendChild(document.createTextNode("path to the fragment:"));
var list = document.createElementNS("http://www.w3.org/1999/xhtml", "ul");
obj.pathToFragment.forEach(function (description) {
var el = document.createElementNS("http://www.w3.org/1999/xhtml", "li");
el.textContent = description;
list.appendChild(el);
});
container.appendChild(list);
}
container.appendChild(document.createTextNode("owners:"));
container.appendChild(document.createElementNS("http://www.w3.org/1999/xhtml", "br"));
if (obj.owners.length == 0) {
container.appendChild(document.createTextNode("none."));
return;
}
var owners = document.createElementNS("http://www.w3.org/1999/xhtml", "ul");
obj.owners.map(getOwnerDescription).forEach(function (o) {
var el = document.createElementNS("http://www.w3.org/1999/xhtml", "li");
if (o.kind == "scope") {
var desc = document.createElementNS("http://www.w3.org/1999/xhtml", "div");
desc.textContent = "Referenced by scope:";
el.appendChild(desc);
var pre = document.createElementNS("http://www.w3.org/1999/xhtml", "div");
pre.style.whiteSpace = "pre";
pre.style.background = "#f0f0f0";
pre.style.border = "1px solid #e0e0e0";
var source = escape(o.data.source).replace(
o.data.varname,
"" + escape(o.data.varname) + "",
"g");
pre.innerHTML = source;
el.appendChild(pre);
}
else if (o.kind == "generic") {
el.innerHTML = "Simple reference:
";
var a = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
a.object = o.owner;
a.textContent = o.data;
el.appendChild(a);
el.appendChild(document.createTextNode("." + o.name));
}
owners.appendChild(el);
});
container.appendChild(owners);
}