hopglass/lib/filters/genericnode.js

59 lines
1 KiB
JavaScript
Raw Normal View History

2017-03-18 15:33:49 +01:00
define(["helper"], function (helper) {
2016-05-27 23:59:01 +02:00
"use strict";
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) {
2017-03-18 15:33:49 +01:00
var o = helper.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
};
};
});