luci2: add LuCI2.ui.view.repeat() to allow implementing periodic tasks
[project/luci2/ui.git] / luci2 / htdocs / luci2 / luci2.js
index fc5d84a..9f40257 100644 (file)
@@ -946,6 +946,37 @@ function LuCI2()
                        object: 'luci2.network',
                        method: 'conntrack_count',
                        expect: { '': { count: 0, limit: 0 } }
+               }),
+
+               listSwitchNames: _luci2.rpc.declare({
+                       object: 'luci2.network',
+                       method: 'switch_list',
+                       expect: { switches: [ ] }
+               }),
+
+               getSwitchInfo: _luci2.rpc.declare({
+                       object: 'luci2.network',
+                       method: 'switch_info',
+                       params: [ 'switch' ],
+                       expect: { info: { } },
+                       filter: function(data, params) {
+                               data['attrs']      = data['switch'];
+                               data['vlan_attrs'] = data['vlan'];
+                               data['port_attrs'] = data['port'];
+                               data['switch']     = params['switch'];
+
+                               delete data.vlan;
+                               delete data.port;
+
+                               return data;
+                       }
+               }),
+
+               getSwitchStatus: _luci2.rpc.declare({
+                       object: 'luci2.network',
+                       method: 'switch_status',
+                       params: [ 'switch' ],
+                       expect: { ports: [ ] }
                })
        };
 
@@ -2037,6 +2068,9 @@ function LuCI2()
                {
                        var name = node.view.split(/\//).join('.');
 
+                       if (_luci2.globals.currentView)
+                               _luci2.globals.currentView.finish();
+
                        _luci2.ui.renderViewMenu();
 
                        if (!_luci2._views)
@@ -2045,7 +2079,10 @@ function LuCI2()
                        _luci2.setHash('view', node.view);
 
                        if (_luci2._views[name] instanceof _luci2.ui.view)
+                       {
+                               _luci2.globals.currentView = _luci2._views[name];
                                return _luci2._views[name].render();
+                       }
 
                        var url = _luci2.globals.resource + '/view/' + name + '.js';
 
@@ -2056,7 +2093,7 @@ function LuCI2()
                        }).then(function(data) {
                                try {
                                        var viewConstructorSource = (
-                                               '(function(L, $) {\n' +
+                                               '(function(L, $) { ' +
                                                        'return %s' +
                                                '})(_luci2, $);\n\n' +
                                                '//@ sourceURL=%s'
@@ -2069,6 +2106,7 @@ function LuCI2()
                                                acls: node.write || { }
                                        });
 
+                                       _luci2.globals.currentView = _luci2._views[name];
                                        return _luci2._views[name].render();
                                }
                                catch(e) {
@@ -2121,7 +2159,7 @@ function LuCI2()
                                                        break;
 
                                                case 'add':
-                                                       log.push('uci add %s (= <ins><strong>%s</strong></ins>)'.format(config, c[1]));
+                                                       log.push('uci add %s <ins>%s</ins> (= <ins><strong>%s</strong></ins>)'.format(config, c[2], c[1]));
                                                        break;
 
                                                case 'list-add':
@@ -2249,6 +2287,42 @@ function LuCI2()
                        return this._fetch_template().then(function() {
                                return _luci2.deferrable(self.execute());
                        });
+               },
+
+               repeat: function(func, interval)
+               {
+                       var self = this;
+
+                       if (!self._timeouts)
+                               self._timeouts = [ ];
+
+                       var index = self._timeouts.length;
+
+                       if (typeof(interval) != 'number')
+                               interval = 5000;
+
+                       var setTimer, runTimer;
+
+                       setTimer = function() {
+                               self._timeouts[index] = window.setTimeout(runTimer, interval);
+                       };
+
+                       runTimer = function() {
+                               _luci2.deferrable(func.call(self)).then(setTimer);
+                       };
+
+                       runTimer();
+               },
+
+               finish: function()
+               {
+                       if ($.isArray(this._timeouts))
+                       {
+                               for (var i = 0; i < this._timeouts.length; i++)
+                                       window.clearTimeout(this._timeouts[i]);
+
+                               delete this._timeouts;
+                       }
                }
        });
 
@@ -2302,7 +2376,10 @@ function LuCI2()
                                var child = this.firstChildView(nodes[i]);
                                if (child)
                                {
-                                       $.extend(node, child);
+                                       for (var key in child)
+                                               if (!node.hasOwnProperty(key) && child.hasOwnProperty(key))
+                                                       node[key] = child[key];
+
                                        return node;
                                }
                        }
@@ -3581,12 +3658,10 @@ function LuCI2()
 
                        if (chg)
                        {
-                               val = val ? this.options.enabled : this.options.disabled;
-
                                if (this.options.optional && val == this.options.initial)
                                        this.map.set(uci.config, uci.section, uci.option, undefined);
                                else
-                                       this.map.set(uci.config, uci.section, uci.option, val);
+                                       this.map.set(uci.config, uci.section, uci.option, val ? this.options.enabled : this.options.disabled);
                        }
 
                        return chg;