summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2015-01-05 19:16:18 +0100
committerJo-Philipp Wich <jow@openwrt.org>2015-01-05 19:16:18 +0100
commit863e738b445e2e53f8d84dd12535781b2d6fc081 (patch)
tree2b373bca2d7bddbba20af03f74aed91addf3a5a1
parent09c7591f609cd31c4a1edc5fe45b78338e19d2e9 (diff)
downloadluci2-ui-863e738b445e2e53f8d84dd12535781b2d6fc081.tar.gz
luci2: implement Class.require() and Class.instantiate()
The two functions allow dynamic loading of additional components. Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
-rw-r--r--luci2/htdocs/luci2/luci2.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/luci2/htdocs/luci2/luci2.js b/luci2/htdocs/luci2/luci2.js
index 284f880..432248c 100644
--- a/luci2/htdocs/luci2/luci2.js
+++ b/luci2/htdocs/luci2/luci2.js
@@ -226,6 +226,55 @@ function LuCI2()
return _class;
};
+ Class.require = function(name)
+ {
+ var path = '/' + name.replace(/\./g, '/') + '.js';
+
+ return $.ajax(path, {
+ method: 'GET',
+ async: false,
+ cache: true,
+ dataType: 'text'
+ }).then(function(text) {
+ var code = '%s\n\n//@ sourceURL=%s/%s'.format(text, window.location.origin, path);
+ var construct = eval(code);
+
+ var parts = name.split(/\./);
+ var cparent = L.Class || (L.Class = { });
+
+ for (var i = 1; i < parts.length - 1; i++)
+ {
+ cparent = cparent[parts[i]];
+
+ if (!cparent)
+ throw "Missing parent class";
+ }
+
+ cparent[parts[i]] = construct;
+ });
+ };
+
+ Class.instantiate = function(name)
+ {
+ Class.require(name).then(function() {
+ var parts = name.split(/\./);
+ var iparent = L;
+ var construct = L.Class;
+
+ for (var i = 1; i < parts.length - 1; i++)
+ {
+ iparent = iparent[parts[i]];
+ construct = construct[parts[i]];
+
+ if (!iparent)
+ throw "Missing parent class";
+ }
+
+ if (construct[parts[i]])
+ iparent[parts[i]] = new construct[parts[i]]();
+ });
+ };
+
this.defaults = function(obj, def)
{
for (var key in def)