1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 20:27:58 +01:00

Move :downloads button logic into binding classes.

--HG--
branch : key-processing
This commit is contained in:
Kris Maglione
2011-01-23 23:25:08 -05:00
parent df4fc65102
commit 3cd40141f9
5 changed files with 100 additions and 32 deletions

View File

@@ -1083,7 +1083,6 @@ var CommandLine = Module("commandline", {
// TODO: Wouldn't multiple handlers be cleaner? --djk // TODO: Wouldn't multiple handlers be cleaner? --djk
if (event.type == "click" && event.target instanceof HTMLAnchorElement) { if (event.type == "click" && event.target instanceof HTMLAnchorElement) {
util.dump(event.getPreventDefault(), event.target);
if (event.getPreventDefault()) if (event.getPreventDefault())
return; return;

View File

@@ -92,7 +92,11 @@ var ProcessorStack = Class("ProcessorStack", {
else if (this.actions.length) { else if (this.actions.length) {
if (actions.length == 0) if (actions.length == 0)
dactyl.beep(); dactyl.beep();
result = this.actions[0]() === Events.PASS ? Events.PASS : Events.KILL;
if (modes.replaying && !events.waitForPageLoad())
result = Events.KILL;
else
result = this.actions[0]() === Events.PASS ? Events.PASS : Events.KILL;
} }
else if (result !== Events.KILL && processors.some(function (p) !p.main.passUnknown)) { else if (result !== Events.KILL && processors.some(function (p) !p.main.passUnknown)) {
result = Events.KILL; result = Events.KILL;
@@ -175,8 +179,6 @@ var KeyProcessor = Class("KeyProcessor", {
return KeyArgProcessor(this, map, false, "arg"); return KeyArgProcessor(this, map, false, "arg");
else if (map.motion) else if (map.motion)
return KeyArgProcessor(this, map, true, "motion"); return KeyArgProcessor(this, map, true, "motion");
else if (modes.replaying && !events.waitForPageLoad())
return Events.KILL;
return this.execute(map, { count: this.count, command: this.command, events: this.events }); return this.execute(map, { count: this.count, command: this.command, events: this.events });
} }

View File

@@ -25,7 +25,9 @@ var Download = Class("Download", {
this.instance = this; this.instance = this;
this.list = list; this.list = list;
this.nodes = {}; this.nodes = {
commandTarget: self
};
util.xmlToDom( util.xmlToDom(
<li highlight="Download" key="row" xmlns:dactyl={NS} xmlns={XHTML}> <li highlight="Download" key="row" xmlns:dactyl={NS} xmlns={XHTML}>
<span highlight="DownloadTitle"> <span highlight="DownloadTitle">
@@ -54,22 +56,6 @@ var Download = Class("Download", {
</li>, </li>,
this.list.document, this.nodes); this.list.document, this.nodes);
for (let [key, node] in Iterator(this.nodes)) {
node.dactylDownload = self;
if (node.getAttributeNS(NS, "highlight") == "Button") {
node.setAttributeNS(NS, "command", "download.command");
update(node, {
set collapsed(collapsed) {
if (collapsed)
this.setAttribute("collapsed", "true");
else
this.removeAttribute("collapsed");
},
get collapsed() !!this.getAttribute("collapsed")
});
}
}
self.updateStatus(); self.updateStatus();
return self; return self;
}, },
@@ -289,12 +275,6 @@ var Downloads = Module("downloads", {
if (modules.commandline.savingOutput) if (modules.commandline.savingOutput)
util.waitFor(function () downloads.document); util.waitFor(function () downloads.document);
}); });
},
dactyl: function (dactyl, modules, window) {
dactyl.commands["download.command"] = function (event) {
let elem = event.originalTarget;
elem.dactylDownload.command(elem.getAttribute("key"));
}
} }
}); });

View File

@@ -13,6 +13,68 @@ defineModule("template", {
default xml namespace = XHTML; default xml namespace = XHTML;
var Binding = Class("Binding", {
init: function (node) {
this.node = node;
node.dactylBinding = this;
Object.defineProperties(node, this.constructor.properties);
for (let [event, handler] in values(this.constructor.events))
node.addEventListener(event, handler, false);
},
set collapsed(collapsed) {
if (collapsed)
this.setAttribute("collapsed", "true");
else
this.removeAttribute("collapsed");
},
get collapsed() !!this.getAttribute("collapsed"),
__noSuchMethod__: function __noSuchMethod__(meth, args) {
return this.node[meth].apply(this.node, args);
}
}, {
get bindings() {
let bindingProto = Object.getPrototypeOf(Binding.prototype);
for (let obj = this.prototype; obj !== bindingProto; obj = Object.getPrototypeOf(obj))
yield obj;
},
bind: function bind(func) function bound() {
try {
return func.apply(this.dactylBinding, arguments);
}
catch (e) {
util.reportError(e);
throw e;
}
},
events: Class.memoize(function () {
let res = [];
for (let obj in this.bindings)
if (Object.getOwnPropertyDescriptor(obj, "events"))
for (let [event, handler] in Iterator(obj.events))
res.push([event, this.bind(handler)]);
return res;
}),
properties: Class.memoize(function () {
let res = {};
for (let obj in this.bindings)
for (let prop in properties(obj)) {
let desc = Object.getOwnPropertyDescriptor(obj, prop);
for (let k in values(["get", "set", "value"]))
if (typeof desc[k] === "function")
desc[k] = this.bind(desc[k]);
res[prop] = desc;
}
return res;
})
});
var Template = Module("Template", { var Template = Module("Template", {
add: function add(a, b) a + b, add: function add(a, b) a + b,
join: function join(c) function (a, b) a + c + b, join: function join(c) function (a, b) a + c + b,
@@ -36,6 +98,25 @@ var Template = Module("Template", {
return ret; return ret;
}, },
bindings: {
Button: Class("Button", Binding, {
init: function init(node, params) {
init.supercall(this, node);
this.target = params.commandTarget;
if (callable(this.target))
this.target = { command: this.target }
},
events: {
"click": function onClick(event) {
event.preventDefault();
this.target.command(this.getAttribute("key"));
}
}
})
},
bookmarkDescription: function (item, text) bookmarkDescription: function (item, text)
<> <>
{ {

View File

@@ -1587,27 +1587,33 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
XML.prettyPrinting = false; XML.prettyPrinting = false;
if (typeof node === "string") // Sandboxes can't currently pass us XML objects. if (typeof node === "string") // Sandboxes can't currently pass us XML objects.
node = XML(node); node = XML(node);
if (node.length() != 1) { if (node.length() != 1) {
let domnode = doc.createDocumentFragment(); let domnode = doc.createDocumentFragment();
for each (let child in node) for each (let child in node)
domnode.appendChild(xmlToDom(child, doc, nodes)); domnode.appendChild(xmlToDom(child, doc, nodes));
return domnode; return domnode;
} }
switch (node.nodeKind()) { switch (node.nodeKind()) {
case "text": case "text":
return doc.createTextNode(String(node)); return doc.createTextNode(String(node));
case "element": case "element":
let domnode = doc.createElementNS(node.namespace(), node.localName()); let domnode = doc.createElementNS(node.namespace(), node.localName());
for each (let attr in node.@*::*)
if (attr.name() != "highlight")
domnode.setAttributeNS(attr.namespace(), attr.localName(), String(attr));
else
highlight.highlightNode(domnode, String(attr));
for each (let child in node.*::*) for each (let child in node.*::*)
domnode.appendChild(xmlToDom(child, doc, nodes)); domnode.appendChild(xmlToDom(child, doc, nodes));
if (nodes && node.@key) if (nodes && node.@key)
nodes[node.@key] = domnode; nodes[node.@key] = domnode;
for each (let attr in node.@*::*)
if (attr.name() != "highlight")
domnode.setAttributeNS(attr.namespace(), attr.localName(), String(attr));
else {
highlight.highlightNode(domnode, String(attr));
if (attr in template.bindings)
template.bindings[attr](domnode, nodes);
}
return domnode; return domnode;
default: default:
return null; return null;