1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-01-08 05:44:11 +01:00

Move :downloads button logic into binding classes.

--HG--
extra : transplant_source : %CF.%29Y%88%B5%0AU%18%E0%B9%EA%85%94%F1%99%16b9%F3
This commit is contained in:
Kris Maglione
2011-01-23 23:25:08 -05:00
parent 2956548352
commit 8e270cdb0c
4 changed files with 95 additions and 29 deletions

View File

@@ -13,6 +13,68 @@ defineModule("template", {
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", {
add: function add(a, b) a + b,
join: function join(c) function (a, b) a + c + b,
@@ -36,6 +98,25 @@ var Template = Module("Template", {
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)
<>
{