diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-07-31 09:14:51 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-08-14 22:58:15 +0200 |
commit | d528a96947f4f83cf121094c5570e4397fc02958 (patch) | |
tree | 628fc7ba4a003caa0d346a9f7a07e4519d668e56 /modules | |
parent | 00ec399fa134f7e76f3a911ab18f66a5b88a1558 (diff) |
luci-base: network.js: support dynamically loaded protocol classes
Port the existing server side protocol support framework to the client
side network.js.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/network.js | 58 | ||||
-rw-r--r-- | modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json | 1 |
2 files changed, 53 insertions, 6 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/network.js b/modules/luci-base/htdocs/luci-static/resources/network.js index 93896549b..61fdafb9a 100644 --- a/modules/luci-base/htdocs/luci-static/resources/network.js +++ b/modules/luci-base/htdocs/luci-static/resources/network.js @@ -83,6 +83,12 @@ var callNetworkDeviceStatus = rpc.declare({ expect: { '': {} } }); +var callGetProtoHandlers = rpc.declare({ + object: 'network', + method: 'get_proto_handlers', + expect: { '': {} } +}); + var _cache = {}, _flush = true, _state = null, @@ -166,6 +172,29 @@ function getBoardState(flush) { return Promise.resolve(_cache.board); } +function getProtocolHandlers(flush) { + if (_cache.protocols == null || flush) + return callGetProtoHandlers().then(function(protos) { + if (!L.isObject(protos)) + throw !1; + + _cache.protocols = protos; + + return Promise.all(Object.keys(protos).map(function(p) { + return Promise.resolve(L.require('protocol.%s'.format(p))).catch(function(err) { + if (L.isObject(err) && err.name != 'NetworkError') + L.error(err); + }); + })).then(function() { + return _cache.protocols; + }); + }).catch(function() { + return (_cache.protocols = {}); + }); + + return Promise.resolve(_cache.protocols); +} + function getWifiStateBySid(sid) { var s = uci.get('wireless', sid); @@ -606,7 +635,7 @@ Network = L.Class.extend({ getProtocol: function(protoname, netname) { var v = _protocols[protoname]; if (v != null) - return v(netname || '__dummy__'); + return new v(netname || '__dummy__'); return null; }, @@ -615,18 +644,35 @@ Network = L.Class.extend({ var rv = []; for (var protoname in _protocols) - rv.push(_protocols[protoname]('__dummy__')); + rv.push(new _protocols[protoname]('__dummy__')); return rv; }, registerProtocol: function(protoname, methods) { - var proto = Protocol.extend(Object.assign({}, methods, { + var spec = L.isObject(_cache.protocols) ? _cache.protocols[protoname] : null; + var proto = Protocol.extend(Object.assign({ + getI18n: function() { + return protoname; + }, + + isFloating: function() { + return false; + }, + + isVirtual: function() { + return (L.isObject(spec) && spec.no_device == true); + }, + + renderFormOptions: function(section) { + + } + }, methods, { __init__: function(name) { this.sid = name; }, - proto: function() { + getProtocol: function() { return protoname; } })); @@ -1202,7 +1248,7 @@ Protocol = L.Class.extend({ if (this.isFloating()) ifname = this._ubus('l3_device'); else - ifname = this._ubus('device'); + ifname = this._ubus('device') || this._ubus('l3_device'); if (ifname != null) return ifname; @@ -1212,7 +1258,7 @@ Protocol = L.Class.extend({ }, getProtocol: function() { - return 'none'; + return null; }, getI18n: function() { diff --git a/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json b/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json index de145ce78..e58c9947b 100644 --- a/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json +++ b/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json @@ -17,6 +17,7 @@ "network.device": [ "status" ], "network.interface": [ "dump" ], "network.wireless": [ "status" ], + "network": [ "get_proto_handlers" ], "uci": [ "changes", "get" ] }, "uci": [ "*" ] |