- this.opkg = {
- updateLists: _luci2.rpc.declare({
- object: 'luci2.opkg',
- method: 'update',
- expect: { '': { } }
- }),
-
- _allPackages: _luci2.rpc.declare({
- object: 'luci2.opkg',
- method: 'list',
- params: [ 'offset', 'limit', 'pattern' ],
- expect: { '': { } }
- }),
-
- _installedPackages: _luci2.rpc.declare({
- object: 'luci2.opkg',
- method: 'list_installed',
- params: [ 'offset', 'limit', 'pattern' ],
- expect: { '': { } }
- }),
-
- _findPackages: _luci2.rpc.declare({
- object: 'luci2.opkg',
- method: 'find',
- params: [ 'offset', 'limit', 'pattern' ],
- expect: { '': { } }
- }),
-
- _fetchPackages: function(action, offset, limit, pattern)
- {
- var packages = [ ];
-
- return action(offset, limit, pattern).then(function(list) {
- if (!list.total || !list.packages)
- return { length: 0, total: 0 };
-
- packages.push.apply(packages, list.packages);
- packages.total = list.total;
-
- if (limit <= 0)
- limit = list.total;
-
- if (packages.length >= limit)
- return packages;
-
- _luci2.rpc.batch();
-
- for (var i = offset + packages.length; i < limit; i += 100)
- action(i, (Math.min(i + 100, limit) % 100) || 100, pattern);
-
- return _luci2.rpc.flush();
- }).then(function(lists) {
- for (var i = 0; i < lists.length; i++)
- {
- if (!lists[i].total || !lists[i].packages)
- continue;
-
- packages.push.apply(packages, lists[i].packages);
- packages.total = lists[i].total;
- }
-
- return packages;
- });
- },
-
- listPackages: function(offset, limit, pattern)
- {
- return _luci2.opkg._fetchPackages(_luci2.opkg._allPackages, offset, limit, pattern);
- },
-
- installedPackages: function(offset, limit, pattern)
- {
- return _luci2.opkg._fetchPackages(_luci2.opkg._installedPackages, offset, limit, pattern);
- },
-
- findPackages: function(offset, limit, pattern)
- {
- return _luci2.opkg._fetchPackages(_luci2.opkg._findPackages, offset, limit, pattern);
- },
-
- installPackage: _luci2.rpc.declare({
- object: 'luci2.opkg',
- method: 'install',
- params: [ 'package' ],
- expect: { '': { } }
- }),
-
- removePackage: _luci2.rpc.declare({
- object: 'luci2.opkg',
- method: 'remove',
- params: [ 'package' ],
- expect: { '': { } }
- }),
-
- getConfig: _luci2.rpc.declare({
- object: 'luci2.opkg',
- method: 'config_get',
- expect: { config: '' }
- }),
-
- setConfig: _luci2.rpc.declare({
- object: 'luci2.opkg',
- method: 'config_set',
- params: [ 'data' ]
- })
- };
-
- this.session = {
-
- login: _luci2.rpc.declare({
- object: 'session',
- method: 'login',
- params: [ 'username', 'password' ],
- expect: { '': { } }
- }),
-
- access: _luci2.rpc.declare({
- object: 'session',
- method: 'access',
- params: [ 'scope', 'object', 'function' ],
- expect: { access: false }
- }),
-
- isAlive: function()
- {
- return _luci2.session.access('ubus', 'session', 'access');
- },
-
- startHeartbeat: function()
- {
- this._hearbeatInterval = window.setInterval(function() {
- _luci2.session.isAlive().then(function(alive) {
- if (!alive)
- {
- _luci2.session.stopHeartbeat();
- _luci2.ui.login(true);
- }
-
- });
- }, _luci2.globals.timeout * 2);
- },
-
- stopHeartbeat: function()
- {
- if (typeof(this._hearbeatInterval) != 'undefined')
- {
- window.clearInterval(this._hearbeatInterval);
- delete this._hearbeatInterval;
- }
- },
-
-
- _acls: { },
-
- _fetch_acls: _luci2.rpc.declare({
- object: 'session',
- method: 'access',
- expect: { '': { } }
- }),
-
- _fetch_acls_cb: function(acls)
- {
- _luci2.session._acls = acls;
- },
-
- updateACLs: function()
- {
- return _luci2.session._fetch_acls()
- .then(_luci2.session._fetch_acls_cb);
- },
-
- hasACL: function(scope, object, func)
- {
- var acls = _luci2.session._acls;
-
- if (typeof(func) == 'undefined')
- return (acls && acls[scope] && acls[scope][object]);
-
- if (acls && acls[scope] && acls[scope][object])
- for (var i = 0; i < acls[scope][object].length; i++)
- if (acls[scope][object][i] == func)
- return true;
-
- return false;
- }
- };
-
- this.ui = {
-
- saveScrollTop: function()
- {
- this._scroll_top = $(document).scrollTop();
- },
-
- restoreScrollTop: function()
- {
- if (typeof(this._scroll_top) == 'undefined')
- return;
-
- $(document).scrollTop(this._scroll_top);
-
- delete this._scroll_top;
- },
-
- loading: function(enable)
- {
- var win = $(window);
- var body = $('body');
-
- var state = _luci2.ui._loading || (_luci2.ui._loading = {