summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2019-02-07 18:19:58 +0100
committerJo-Philipp Wich <jo@mein.io>2019-07-07 15:25:49 +0200
commite4c29c6db4da21e5d6abb7b89969ebbbae74566c (patch)
tree42443dc4987aff985440dc0b4b42c31d0543678e /modules/luci-base
parent4a9f784a2a075ecb96a7e45d6a60160038f968a9 (diff)
luci-base: luci.js: add L.dom.data()
Add a new data() function which allows to attach arbitrary JS data to DOM elements. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base')
-rw-r--r--modules/luci-base/htdocs/luci-static/resources/luci.js91
1 files changed, 91 insertions, 0 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js
index b02710bcb6..1653bcfce2 100644
--- a/modules/luci-base/htdocs/luci-static/resources/luci.js
+++ b/modules/luci-base/htdocs/luci-static/resources/luci.js
@@ -761,6 +761,11 @@
if (!this.elem(node))
return null;
+ var dataNodes = node.querySelectorAll('[data-idref]');
+
+ for (var i = 0; i < dataNodes.length; i++)
+ delete this.registry[dataNodes[i].getAttribute('data-idref')];
+
while (node.firstChild)
node.removeChild(node.firstChild);
@@ -828,6 +833,92 @@
this.append(elem, data);
return elem;
+ },
+
+ registry: {},
+
+ data: function(node, key, val) {
+ var id = node.getAttribute('data-idref');
+
+ /* clear all data */
+ if (arguments.length > 1 && key == null) {
+ if (id != null) {
+ node.removeAttribute('data-idref');
+ val = this.registry[id]
+ delete this.registry[id];
+ return val;
+ }
+
+ return null;
+ }
+
+ /* clear a key */
+ else if (arguments.length > 2 && key != null && val == null) {
+ if (id != null) {
+ val = this.registry[id][key];
+ delete this.registry[id][key];
+ return val;
+ }
+
+ return null;
+ }
+
+ /* set a key */
+ else if (arguments.length > 2 && key != null && val != null) {
+ if (id == null) {
+ do { id = Math.floor(Math.random() * 0xffffffff).toString(16) }
+ while (this.registry.hasOwnProperty(id));
+
+ node.setAttribute('data-idref', id);
+ this.registry[id] = {};
+ }
+
+ return (this.registry[id][key] = val);
+ }
+
+ /* get all data */
+ else if (arguments.length == 1) {
+ if (id != null)
+ return this.registry[id];
+
+ return null;
+ }
+
+ /* get a key */
+ else if (arguments.length == 2) {
+ if (id != null)
+ return this.registry[id][key];
+ }
+
+ return null;
+ },
+
+ bindClassInstance: function(node, inst) {
+ if (!(inst instanceof Class))
+ L.error('TypeError', 'Argument must be a class instance');
+
+ return this.data(node, '_class', inst);
+ },
+
+ findClassInstance: function(node) {
+ var inst = null;
+
+ do {
+ inst = this.data(node, '_class');
+ node = node.parentNode;
+ }
+ while (!(inst instanceof Class) && node != null);
+
+ return inst;
+ },
+
+ callClassMethod: function(node, method /*, ... */) {
+ var inst = this.findClassInstance(node);
+
+ if (inst == null || typeof(inst[method]) != 'function')
+ return null;
+
+ return inst[method].apply(inst, inst.varargs(arguments, 2));
}
}),