luci-base: ui.js: add LuCI.ui.menu helper class
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit 930f28b606
)
This commit is contained in:
parent
8daa236263
commit
5ce7daacda
1 changed files with 80 additions and 0 deletions
|
@ -2951,6 +2951,84 @@ var UIFileUpload = UIElement.extend(/** @lends LuCI.ui.FileUpload.prototype */ {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle menu.
|
||||||
|
*
|
||||||
|
* @constructor menu
|
||||||
|
* @memberof LuCI.ui
|
||||||
|
*
|
||||||
|
* @classdesc
|
||||||
|
*
|
||||||
|
* Handles menus.
|
||||||
|
*/
|
||||||
|
var UIMenu = baseclass.singleton(/** @lends LuCI.ui.menu.prototype */ {
|
||||||
|
/**
|
||||||
|
* @typedef {Object} MenuNode
|
||||||
|
* @memberof LuCI.ui.menu
|
||||||
|
|
||||||
|
* @property {string} name - The internal name of the node, as used in the URL
|
||||||
|
* @property {number} order - The sort index of the menu node
|
||||||
|
* @property {string} [title] - The title of the menu node, `null` if the node should be hidden
|
||||||
|
* @property {satisified} boolean - Boolean indicating whether the menu enries dependencies are satisfied
|
||||||
|
* @property {readonly} [boolean] - Boolean indicating whether the menu entries underlying ACLs are readonly
|
||||||
|
* @property {LuCI.ui.menu.MenuNode[]} [children] - Array of child menu nodes.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load and cache current menu tree.
|
||||||
|
*
|
||||||
|
* @returns {Promise<LuCI.ui.menu.MenuNode>}
|
||||||
|
* Returns a promise resolving to the root element of the menu tree.
|
||||||
|
*/
|
||||||
|
load: function() {
|
||||||
|
if (this.menu == null)
|
||||||
|
this.menu = session.getLocalData('menu');
|
||||||
|
|
||||||
|
if (!L.isObject(this.menu)) {
|
||||||
|
this.menu = request.get(L.url('admin/menu')).then(L.bind(function(menu) {
|
||||||
|
this.menu = menu.json();
|
||||||
|
session.setLocalData('menu', this.menu);
|
||||||
|
|
||||||
|
return this.menu;
|
||||||
|
}, this));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve(this.menu);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {LuCI.ui.menu.MenuNode} [node]
|
||||||
|
* The menu node to retrieve the children for. Defaults to the menu's
|
||||||
|
* internal root node if omitted.
|
||||||
|
*
|
||||||
|
* @returns {LuCI.ui.menu.MenuNode[]}
|
||||||
|
* Returns an array of child menu nodes.
|
||||||
|
*/
|
||||||
|
getChildren: function(node) {
|
||||||
|
var children = [];
|
||||||
|
|
||||||
|
if (node == null)
|
||||||
|
node = this.menu;
|
||||||
|
|
||||||
|
for (var k in node.children) {
|
||||||
|
if (!node.children.hasOwnProperty(k))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!node.children[k].satisfied)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!node.children[k].hasOwnProperty('title'))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
children.push(Object.assign(node.children[k], { name: k }));
|
||||||
|
}
|
||||||
|
|
||||||
|
return children.sort(function(a, b) {
|
||||||
|
return ((a.order || 1000) - (b.order || 1000));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class ui
|
* @class ui
|
||||||
* @memberof LuCI
|
* @memberof LuCI
|
||||||
|
@ -4317,6 +4395,8 @@ var UI = baseclass.extend(/** @lends LuCI.ui.prototype */ {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
menu: UIMenu,
|
||||||
|
|
||||||
AbstractElement: UIElement,
|
AbstractElement: UIElement,
|
||||||
|
|
||||||
/* Widgets */
|
/* Widgets */
|
||||||
|
|
Loading…
Reference in a new issue