hopglass/lib/filters/genericnode.js

57 lines
1 KiB
JavaScript
Raw Normal View History

2015-07-08 21:17:00 +02:00
define([], function () {
2015-07-08 00:36:57 +02:00
return function (name, key, value, f) {
var negate = false;
var refresh;
2015-07-08 17:20:56 +02:00
var label = document.createElement("label");
var strong = document.createElement("strong");
label.textContent = name + " ";
label.appendChild(strong);
2015-07-08 20:50:26 +02:00
2015-07-08 00:36:57 +02:00
function run(d) {
var o = dictGet(d, key.slice(0));
2015-07-08 00:36:57 +02:00
if (f) {
o = f(o);
}
2015-07-08 00:36:57 +02:00
return o === value ? !negate : negate;
2015-07-08 17:20:56 +02:00
}
function setRefresh(f) {
refresh = f;
2015-07-08 00:36:57 +02:00
}
2015-07-08 17:20:56 +02:00
function draw(el) {
if (negate) {
el.parentNode.classList.add("not");
} else {
el.parentNode.classList.remove("not");
}
2015-07-08 20:50:26 +02:00
strong.textContent = (negate ? "¬" : "" ) + value;
2015-07-08 00:36:57 +02:00
}
function render(el) {
el.appendChild(label);
draw(el);
2015-07-08 17:20:56 +02:00
label.onclick = function () {
negate = !negate;
2015-07-08 17:20:56 +02:00
draw(el);
2015-07-08 17:20:56 +02:00
if (refresh) {
refresh();
}
};
2015-07-08 00:36:57 +02:00
}
return {
run: run,
setRefresh: setRefresh,
render: render
};
};
});