This commit is contained in:
2021-11-18 20:48:04 +03:00
parent 4e9df6c81b
commit 5e7b4c6707
22 changed files with 1160 additions and 345 deletions
+316 -312
View File
@@ -121,12 +121,14 @@ __PluginManager__.register('menu',function(module, exports, require, global) {
};
Menu.prototype.setPosition = function () {
//console.log('setPosition ' + this.id);
//console.log('setPosition', this.id);
if(!(this.getParent() instanceof Element)) {
console.warn('Menu.options.parent NOT instanceof Element');
return false;
}
if(!support.isA(this.align, align.Class)){
return;
}
this.node.style.position = 'absolute';
this.node.style.width = 'fit-content';
@@ -163,7 +165,7 @@ __PluginManager__.register('menu',function(module, exports, require, global) {
*/
Menu.prototype.getParentMenu = function() {
var p = this.getParent();
if(p instanceof Element && p.hasOwnProperty('kit') && p.kit[MENU_ITEM_PLUGIN_NAME] instanceof MenuItemClass) {
if(p instanceof Element && p.hasOwnProperty('kit') && p.kit[MENU_ITEM_PLUGIN_NAME] instanceof MenuItem) {
return p.kit[MENU_ITEM_PLUGIN_NAME].getMenu();
}
};
@@ -181,9 +183,9 @@ __PluginManager__.register('menu',function(module, exports, require, global) {
* @param options {kitMenuItemOptions}
* @return {MenuItem}
*/
Menu.prototype.addItem = function (options = {}) {
Menu.prototype.addItem = function (options = {}){
var id;
if(options instanceof MenuItemClass) {
if(options instanceof MenuItem) {
options.parentId = this.id;
id = options.options.id;
this.itemList[id] = options;
@@ -192,25 +194,26 @@ __PluginManager__.register('menu',function(module, exports, require, global) {
options.id = support.uniqueId(8);
}
id = options.id;
this.itemList[id] = (new this.MenuItem(this.id)).setOptions(options).init();
this.itemList[id] = (new MenuItem(this.id)).setOptions(options).init();
}
return this.itemList[id];
};
Menu.prototype.addItemList = function(itemList = []){
var result = [];
itemList.forEach((function(element, index, array){
this.addItem(element);
result.push(this.addItem(element));
}).bind(this));
return this;
return result;
};
Menu.prototype.sortItems = function() {
this.itemList = Object.entries(this.itemList)
.sort(function(a, b){
return a[1].options.sort >>> 0 - b[1].options.sort >>> 0;
return Number(a[1].options.sort) - Number(b[1].options.sort);
})
.reduce((r, [k, v]) => ({ ...r, [k]: v }), {});
};
Menu.prototype.render = function() {
//console.log('Menu: render');
//console.log('Menu: render', this.id);
this.sortItems();
this.node.innerHTML = '';
if(global.config.debug) {
@@ -334,315 +337,315 @@ __PluginManager__.register('menu',function(module, exports, require, global) {
return this;
};
var MenuItemClass = function () {
function MenuItem(parentId) {
var _thisMenuItem = this;
this.parentId = Number(parentId);
this.node = Element;
this.bInit = false;
this.bActive = false;
this.child = null;
/**
* @class kit.menu.MenuItem
* @param parentId
*/
function MenuItem(parentId) {
var _thisMenuItem = this;
this.parentId = Number(parentId);
this.node = Element;
this.bInit = false;
this.bActive = false;
this.child = null;
/**
* @class kitMenuItemOptions
*/
this.options = {
id: '',
icon: libModule.svg.options,
arrow: libModule.svg.arrowMenu,
label: 'menu',
href: 'javascript:void(0);',
class: '',
sort: 500,
visible: true,
/** @type {kitMenuItemHandler} */
hover: this.defaultHoverHandler,
/** @type {kitMenuItemHandler} */
click: this.defaultClickHandler,
dblclick: this.defaultDblClickHandler,
};
/**
* @class kitMenuItemOptions
*/
this.options = {
id: '',
icon: libModule.svg.options,
arrow: libModule.svg.arrowMenu,
label: 'menu',
href: 'javascript:void(0);',
class: '',
sort: 500,
visible: true,
/** @type {kitMenuItemHandler} */
hover: this.defaultHoverHandler,
/** @type {kitMenuItemHandler} */
click: this.defaultClickHandler,
dblclick: this.defaultDblClickHandler,
};
this.handleEvent = function(event) {
event.stopPropagation();
switch(event.type) {
case 'mouseover':
this.options.hover(event, _thisMenuItem, MenuList[_thisMenuItem.parentId]);
break;
case 'click':
this.options.click(event, _thisMenuItem, MenuList[_thisMenuItem.parentId]);
break;
case 'dblclick':
this.options.dblclick(event, _thisMenuItem, MenuList[_thisMenuItem.parentId]);
break;
this.handleEvent = function(event) {
event.stopPropagation();
switch(event.type) {
case 'mouseover':
this.options.hover(event, _thisMenuItem, MenuList[_thisMenuItem.parentId]);
break;
case 'click':
this.options.click(event, _thisMenuItem, MenuList[_thisMenuItem.parentId]);
break;
case 'dblclick':
this.options.dblclick(event, _thisMenuItem, MenuList[_thisMenuItem.parentId]);
break;
}
};
this.createNode();
}
/**
* @param event {MouseEvent}
* @param menuItem {MenuItem}
* @param menu {Menu}
*/
MenuItem.prototype.defaultHoverHandler = function (event, menuItem, menu) {
console.log('hover: ' + menuItem.options.label);
};
/**
* @param event {MouseEvent}
* @param menuItem {MenuItem}
* @param menu {Menu}
*/
MenuItem.prototype.defaultClickHandler = function (event, menuItem, menu) {
console.log('click: ' + menuItem.options.label);
menu.hide();
};
/**
* @param event {MouseEvent}
* @param menuItem {MenuItem}
* @param menu {Menu}
*/
MenuItem.prototype.defaultDblClickHandler = function (event, menuItem, menu) {
console.log('dblclick: ' + menuItem.options.label);
};
MenuItem.prototype.getMenu = function () {
return MenuList[this.parentId];
};
MenuItem.prototype.getParent = function () {
if(MenuList[this.parentId] instanceof Menu) {
return MenuList[this.parentId].node;
}
return null;
};
MenuItem.prototype.getId = function () {
return this.options.id;
};
/**
* @returns HTMLElement
*/
MenuItem.prototype.getNode = function () {
return this.node;
};
MenuItem.prototype.createNode = function () {
this.node = document.createElement('a');
this.node.style.position = 'relative';
this.node.dataset.parentId = this.parentId;
this.node.kit = this.node.kit || {};
this.node.kit.menuItem = this;
};
/**
* @param content HTMLElement | string
* @returns {MenuItem}
*/
MenuItem.prototype.setLabel = function (content = '') {
this.options.label = content;
var wrap = this.getNodeScopeElementByClassName('kit-menu-item__label');
this.setContent(wrap, content);
return this;
};
/**
* @param href
* @returns {MenuItem}
*/
MenuItem.prototype.setHref = function (href = 'javascript:void(0);') {
this.options.href = href;
this.getNode().setAttribute('href', href);
return this;
};
/**
* @param content HTMLElement | string
* @returns {MenuItem}
*/
MenuItem.prototype.setIcon = function (content = '') {
this.options.icon = content;
var wrap = this.getNodeScopeElementByClassName('kit-menu-item__icon');
this.setContent(wrap, content);
return this;
};
/**
* @param sort int
* @returns {MenuItem}
*/
MenuItem.prototype.setSort = function (sort = 500) {
this.options.sort = Number(sort);
return this;
};
/**
* @param content HTMLElement | string
* @returns {MenuItem}
*/
MenuItem.prototype.setArrow = function (content = '') {
this.options.arrow = content;
var wrap = this.getNodeScopeElementByClassName('kit-menu-item__arrow');
this.setContent(wrap, content);
return this;
};
MenuItem.prototype.setId = function (id = '') {
if(id) {
this.options.id = id;
}
this.node.id = this.options.id;
return this;
};
MenuItem.prototype.setVisible = function (visible = true) {
this.options.visible = visible;
this.node.style.display = this.options.visible?'':'none';
return this;
};
/**
* @param {Menu} menu
* @return {MenuItem}
*/
MenuItem.prototype.setChild = function (menu) {
if(menu instanceof Menu) {
menu.setParent(this.node);
menu.init();
}
else if( Array.isArray(menu) ) {
var menuClass;
menuClass = new Menu();
menu.forEach(function (item, i) {
if(typeof item === 'object') {
menuClass.addItem(item);
}
});
menuClass.setParent(this.node);
menuClass.init();
}
return this;
};
MenuItem.prototype.addClass = function (addClass = '') {
if(addClass) {
this.node.classList.add(addClass);
}
return this;
};
/**
* @param options Object
* @return {MenuItem}
*/
MenuItem.prototype.setOptions = function (options = {}) {
/**
* @type {kitMenuItemOptions}
*/
this.options = Object.assign({}, this.options, options);
return this;
};
/**
* @return {MenuItem}
*/
MenuItem.prototype.render = function () {
this.setLabel(this.options.label);
this.setHref(this.options.href);
this.setId(this.options.id);
this.addClass(MENU_ITEM_CLASS);
this.addClass(this.options.class);
this.setChild(this.options.child);
this.setIcon(this.options.icon);
this.setArrow(this.options.arrow);
this.setVisible(this.options.visible);
this.setSort(this.options.sort);
return this;
};
MenuItem.prototype.init = function () {
if(this.bInit) {
return this;
}
this.render();
this.node.removeEventListener('click', this, false);
this.node.removeEventListener('dblclick', this, false);
this.node.addEventListener('click', this, false);
this.node.addEventListener('dblclick', this, false);
this.bInit = true;
return this;
};
MenuItem.prototype.reInit = function () {
this.render();
this.node.removeEventListener('click', this, false);
this.node.removeEventListener('dblclick', this, false);
this.node.addEventListener('click', this, false);
this.node.addEventListener('dblclick', this, false);
return this;
};
MenuItem.prototype.remove = function () {
this.node.removeEventListener('click', this, false);
this.node.removeEventListener('dblclick', this, false);
var id = this.options.id;
//console.log('remove item' + id);
setTimeout((function(parentId, id){
return function() {
if(MenuList.hasOwnProperty(parentId) && MenuList[parentId].itemList.hasOwnProperty(id)) {
delete MenuList[parentId].itemList[id];
MenuList[parentId].init();
}
};
this.createNode();
})(this.parentId, this.options.id),0);
};
/**
* @param className
* @returns HTMLElement
*/
MenuItem.prototype.getNodeScopeElementByClassName = function(className = ''){
var wrap;
this.node && (wrap = this.node.querySelector(':scope > .' + className));
if(!wrap) {
wrap = document.createElement('div');
wrap.classList.add(className);
this.node.append(wrap);
}
return wrap;
};
/**
* @param event {MouseEvent}
* @param menuItem {MenuItem}
* @param menu {Menu}
*/
MenuItem.prototype.defaultHoverHandler = function (event, menuItem, menu) {
console.log('hover: ' + menuItem.options.label);
};
/**
* @param event {MouseEvent}
* @param menuItem {MenuItem}
* @param menu {Menu}
*/
MenuItem.prototype.defaultClickHandler = function (event, menuItem, menu) {
console.log('click: ' + menuItem.options.label);
menu.hide();
};
/**
* @param event {MouseEvent}
* @param menuItem {MenuItem}
* @param menu {Menu}
*/
MenuItem.prototype.defaultDblClickHandler = function (event, menuItem, menu) {
console.log('dblclick: ' + menuItem.options.label);
};
MenuItem.prototype.getMenu = function () {
return MenuList[this.parentId];
};
MenuItem.prototype.getParent = function () {
if(MenuList[this.parentId] instanceof Menu) {
return MenuList[this.parentId].node;
}
return null;
};
MenuItem.prototype.getId = function () {
return this.options.id;
};
/**
* @returns HTMLElement
*/
MenuItem.prototype.getNode = function () {
return this.node;
};
MenuItem.prototype.createNode = function () {
this.node = document.createElement('a');
this.node.style.position = 'relative';
this.node.dataset.parentId = this.parentId;
this.node.kit = this.node.kit || {};
this.node.kit.menuItem = this;
};
/**
* @param content HTMLElement | string
* @returns {MenuItem}
*/
MenuItem.prototype.setLabel = function (content = '') {
this.options.label = content;
var wrap = this.getNodeScopeElementByClassName('kit-menu-item__label');
this.setContent(wrap, content);
return this;
};
/**
* @param href
* @returns {MenuItem}
*/
MenuItem.prototype.setHref = function (href = 'javascript:void(0);') {
this.options.href = href;
this.getNode().setAttribute('href', href);
return this;
};
/**
* @param content HTMLElement | string
* @returns {MenuItem}
*/
MenuItem.prototype.setIcon = function (content = '') {
this.options.icon = content;
var wrap = this.getNodeScopeElementByClassName('kit-menu-item__icon');
this.setContent(wrap, content);
return this;
};
/**
* @param sort int
* @returns {MenuItem}
*/
MenuItem.prototype.setSort = function (sort = 500) {
this.options.sort = Number(sort);
return this;
};
/**
* @param content HTMLElement | string
* @returns {MenuItem}
*/
MenuItem.prototype.setArrow = function (content = '') {
this.options.arrow = content;
var wrap = this.getNodeScopeElementByClassName('kit-menu-item__arrow');
this.setContent(wrap, content);
return this;
};
MenuItem.prototype.setId = function (id = '') {
if(id) {
this.options.id = id;
}
this.node.id = this.options.id;
return this;
};
MenuItem.prototype.setVisible = function (visible = true) {
this.options.visible = visible;
this.node.style.display = this.options.visible?'':'none';
return this;
};
/**
* @param {Menu} menu
* @return {MenuItem}
*/
MenuItem.prototype.setChild = function (menu) {
if(menu instanceof Menu) {
menu.setParent(this.node);
menu.init();
}
else if( Array.isArray(menu) ) {
var menuClass;
menuClass = new Menu();
menu.forEach(function (item, i) {
if(typeof item === 'object') {
menuClass.addItem(item);
}
});
menuClass.setParent(this.node);
menuClass.init();
}
return this;
};
MenuItem.prototype.addClass = function (addClass = '') {
if(addClass) {
this.node.classList.add(addClass);
}
return this;
};
/**
* @param options Object
* @return {MenuItem}
*/
MenuItem.prototype.setOptions = function (options = {}) {
this.options = Object.assign({}, this.options, options);
return this;
};
/**
* @return {MenuItem}
*/
MenuItem.prototype.render = function () {
this.setLabel(this.options.label);
this.setHref(this.options.href);
this.setId(this.options.id);
this.addClass(MENU_ITEM_CLASS);
this.addClass(this.options.class);
this.setChild(this.options.child);
this.setIcon(this.options.icon);
this.setArrow(this.options.arrow);
this.setVisible(this.options.visible);
this.setSort(this.options.sort);
return this;
};
MenuItem.prototype.init = function () {
if(this.bInit) {
return this;
}
this.render();
this.node.removeEventListener('click', this, false);
this.node.removeEventListener('dblclick', this, false);
this.node.addEventListener('click', this, false);
this.node.addEventListener('dblclick', this, false);
this.bInit = true;
return this;
};
MenuItem.prototype.reInit = function () {
this.render();
this.node.removeEventListener('click', this, false);
this.node.removeEventListener('dblclick', this, false);
this.node.addEventListener('click', this, false);
this.node.addEventListener('dblclick', this, false);
return this;
};
MenuItem.prototype.remove = function () {
this.node.removeEventListener('click', this, false);
this.node.removeEventListener('dblclick', this, false);
var id = this.options.id;
//console.log('remove item' + id);
setTimeout((function(parentId, id){
return function() {
if(MenuList.hasOwnProperty(parentId) && MenuList[parentId].itemList.hasOwnProperty(id)) {
delete MenuList[parentId].itemList[id];
MenuList[parentId].init();
}
};
})(this.parentId, this.options.id),0);
};
/**
* @param className
* @returns HTMLElement
*/
MenuItem.prototype.getNodeScopeElementByClassName = function(className = ''){
var wrap;
this.node && (wrap = this.node.querySelector(':scope > .' + className));
if(!wrap) {
wrap = document.createElement('div');
wrap.classList.add(className);
this.node.append(wrap);
}
return wrap;
};
/**
* @param node HTMLElement
* @param content HTMLElement | string
* @param isText boolean
* @return boolean
*/
MenuItem.prototype.setContent = function(node, content, isText = false){
if(!(node instanceof Element)) {
return false;
}
while (node.firstChild) {
node.removeChild(node.firstChild);
}
if((content instanceof Element) || isText) {
node.append(content);
} else {
node.innerHTML = content;
}
return true;
};
return MenuItem;
}();
Menu.prototype.MenuItem = MenuItemClass;
/**
* @param node HTMLElement
* @param content HTMLElement | string
* @param isText boolean
* @return boolean
*/
MenuItem.prototype.setContent = function(node, content, isText = false){
if(!(node instanceof Element)) {
return false;
}
while (node.firstChild) {
node.removeChild(node.firstChild);
}
if((content instanceof Element) || isText) {
node.append(content);
} else {
node.innerHTML = content;
}
return true;
};
var getInstance = function(options = {}, items = []) {
@@ -786,6 +789,7 @@ __PluginManager__.register('menu',function(module, exports, require, global) {
* @memberOf kit
*/
var menu = {
MenuItem: MenuItem,
Class: Menu,
getInstance: getInstance,
reInitAllMenu: reInitAllMenu,