2021-02-20 17:31:36 +08:00
|
|
|
var open_menu = null;
|
2021-01-11 06:57:55 +08:00
|
|
|
|
|
|
|
function handleMenuOverflow(node) {
|
|
|
|
node = node.get(0);
|
|
|
|
if (!node) return;
|
2022-03-20 23:07:32 +08:00
|
|
|
function offset(amount) {
|
2021-01-11 06:57:55 +08:00
|
|
|
let top = parseInt(node.style.top);
|
|
|
|
let offset = top - $(node).offset().top;
|
|
|
|
top = Math.clamp(
|
2022-03-20 23:07:32 +08:00
|
|
|
top + amount,
|
2021-01-11 06:57:55 +08:00
|
|
|
window.innerHeight - node.clientHeight + offset,
|
|
|
|
offset + 26
|
|
|
|
);
|
|
|
|
node.style.top = `${top}px`;
|
2022-03-20 23:07:32 +08:00
|
|
|
}
|
|
|
|
if (Blockbench.isTouch) {
|
|
|
|
node.addEventListener('touchstart', e1 => {
|
|
|
|
e1.stopPropagation();
|
|
|
|
convertTouchEvent(e1);
|
|
|
|
let last_y = e1.clientY;
|
|
|
|
let move = e2 => {
|
|
|
|
convertTouchEvent(e2);
|
|
|
|
offset(e2.clientY - last_y);
|
|
|
|
last_y = e2.clientY;
|
|
|
|
}
|
|
|
|
let stop = e2 => {
|
|
|
|
document.removeEventListener('touchmove', move);
|
|
|
|
document.removeEventListener('touchend', stop);
|
|
|
|
}
|
|
|
|
document.addEventListener('touchmove', move);
|
|
|
|
document.addEventListener('touchend', stop);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
node.addEventListener('wheel', e => {
|
|
|
|
e.stopPropagation();
|
|
|
|
offset(-e.deltaY);
|
2021-01-11 06:57:55 +08:00
|
|
|
})
|
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
class Menu {
|
2022-02-28 03:03:07 +08:00
|
|
|
constructor(id, structure, options) {
|
|
|
|
if (typeof id !== 'string') {
|
|
|
|
options = structure;
|
|
|
|
structure = id;
|
|
|
|
}
|
2021-12-10 19:05:54 +08:00
|
|
|
this.id = typeof id == 'string' ? id : '';
|
2019-07-18 00:02:07 +08:00
|
|
|
this.children = [];
|
2022-09-23 23:47:37 +08:00
|
|
|
this.node = document.createElement('ul');
|
|
|
|
this.node.classList.add('contextMenu');
|
2022-02-28 03:03:07 +08:00
|
|
|
this.structure = structure;
|
|
|
|
this.options = options || {};
|
2022-05-08 01:35:41 +08:00
|
|
|
this.onOpen = this.options.onOpen;
|
|
|
|
this.onClose = this.options.onClose;
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
2022-02-01 05:39:46 +08:00
|
|
|
hover(node, event, expand) {
|
2019-07-18 00:02:07 +08:00
|
|
|
if (event) event.stopPropagation()
|
|
|
|
$(open_menu.node).find('li.focused').removeClass('focused')
|
|
|
|
$(open_menu.node).find('li.opened').removeClass('opened')
|
|
|
|
var obj = $(node)
|
|
|
|
obj.addClass('focused')
|
2022-02-01 05:39:46 +08:00
|
|
|
obj.parents('li.parent, li.hybrid_parent').addClass('opened')
|
2019-07-18 00:02:07 +08:00
|
|
|
|
2022-02-01 05:39:46 +08:00
|
|
|
if (obj.hasClass('parent') || (expand && obj.hasClass('hybrid_parent'))) {
|
2019-07-18 00:02:07 +08:00
|
|
|
var childlist = obj.find('> ul.contextMenu.sub')
|
|
|
|
|
2022-02-01 05:39:46 +08:00
|
|
|
if (expand) obj.addClass('opened');
|
|
|
|
|
2019-07-18 00:02:07 +08:00
|
|
|
var p_width = obj.outerWidth()
|
|
|
|
childlist.css('left', p_width + 'px')
|
|
|
|
var el_width = childlist.width()
|
|
|
|
var offset = childlist.offset()
|
|
|
|
var el_height = childlist.height()
|
|
|
|
|
2021-01-31 07:12:53 +08:00
|
|
|
if (offset.left + el_width > window.innerWidth) {
|
2019-07-18 00:02:07 +08:00
|
|
|
if (Blockbench.isMobile) {
|
|
|
|
childlist.css('visibility', 'hidden');
|
|
|
|
setTimeout(() => {
|
|
|
|
childlist.css('left', 0);
|
|
|
|
childlist.css('visibility', 'visible');
|
|
|
|
}, 100);
|
|
|
|
} else {
|
|
|
|
childlist.css('left', -el_width + 'px')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 06:57:55 +08:00
|
|
|
let window_height = window.innerHeight - 26;
|
|
|
|
|
|
|
|
if (el_height > window_height) {
|
|
|
|
childlist.css('margin-top', '0').css('top', '0')
|
|
|
|
childlist.css('top', (-childlist.offset().top + 26) + 'px')
|
|
|
|
handleMenuOverflow(childlist);
|
2020-07-18 03:49:23 +08:00
|
|
|
|
2021-01-11 06:57:55 +08:00
|
|
|
} else if (offset.top + el_height > window_height) {
|
|
|
|
childlist.css('margin-top', 26-childlist.height() + 'px')
|
2020-07-18 03:49:23 +08:00
|
|
|
if (childlist.offset().top < 26) {
|
|
|
|
childlist.offset({top: 26})
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 20:57:07 +08:00
|
|
|
}
|
|
|
|
reveal(path) {
|
|
|
|
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
keyNavigate(e) {
|
|
|
|
var scope = this;
|
|
|
|
var used;
|
|
|
|
var obj = $(this.node)
|
|
|
|
if (e.which >= 37 && e.which <= 40) {
|
|
|
|
|
2021-03-02 02:38:10 +08:00
|
|
|
let is_menu_bar = scope.type === 'bar_menu' && e.which%2;
|
|
|
|
if (obj.find('li.focused').length || is_menu_bar) {
|
2019-07-18 00:02:07 +08:00
|
|
|
var old = obj.find('li.focused'), next;
|
|
|
|
switch (e.which) {
|
|
|
|
case 37: next = old.parent('ul').parent('li'); break;//<
|
|
|
|
case 38: next = old.prevAll('li:not(.menu_separator)').first(); break;//UP
|
|
|
|
case 39: next = old.find('ul li:first-child'); break;//>
|
|
|
|
case 40: next = old.nextAll('li:not(.menu_separator)').first(); break;//DOWN
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!next.length && e.which%2 == 0) {
|
|
|
|
var siblings = old.siblings('li:not(.menu_separator)')
|
|
|
|
if (e.which === 38) {
|
|
|
|
next = siblings.last()
|
|
|
|
} else {
|
|
|
|
next = siblings.first()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (next && next.length) {
|
|
|
|
old.removeClass('focused')
|
|
|
|
scope.hover(next.get(0))
|
2021-03-02 02:38:10 +08:00
|
|
|
} else if (is_menu_bar) {
|
2019-07-18 00:02:07 +08:00
|
|
|
var index = MenuBar.keys.indexOf(scope.id)
|
|
|
|
index += (e.which == 39 ? 1 : -1)
|
|
|
|
if (index < 0) {
|
|
|
|
index = MenuBar.keys.length-1
|
|
|
|
} else if (index >= MenuBar.keys.length) {
|
|
|
|
index = 0;
|
|
|
|
}
|
2020-04-26 02:25:07 +08:00
|
|
|
MenuBar.menus[MenuBar.keys[index]].open()
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
obj.find('> li:first-child').addClass('focused')
|
|
|
|
}
|
|
|
|
used = true;
|
|
|
|
} else if (Keybinds.extra.confirm.keybind.isTriggered(e)) {
|
|
|
|
obj.find('li.focused').click()
|
2022-08-15 20:57:07 +08:00
|
|
|
if (scope && !this.options.keep_open) {
|
2022-09-23 23:47:37 +08:00
|
|
|
//scope.hide()
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
used = true;
|
|
|
|
} else if (Keybinds.extra.cancel.keybind.isTriggered(e)) {
|
|
|
|
scope.hide()
|
|
|
|
used = true;
|
|
|
|
}
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
open(position, context) {
|
2022-05-08 01:35:41 +08:00
|
|
|
if (this.onOpen) this.onOpen(position, context);
|
2019-07-18 00:02:07 +08:00
|
|
|
|
2020-01-24 01:53:36 +08:00
|
|
|
if (position && position.changedTouches) {
|
2019-12-16 03:04:31 +08:00
|
|
|
convertTouchEvent(position);
|
|
|
|
}
|
2023-03-11 06:17:25 +08:00
|
|
|
let last_context = context;
|
2019-07-18 00:02:07 +08:00
|
|
|
var scope = this;
|
|
|
|
var ctxmenu = $(this.node)
|
|
|
|
if (open_menu) {
|
|
|
|
open_menu.hide()
|
|
|
|
}
|
|
|
|
$('body').append(ctxmenu)
|
|
|
|
|
|
|
|
ctxmenu.children().detach()
|
|
|
|
|
2022-08-23 02:04:59 +08:00
|
|
|
function createChildList(object, node, list) {
|
|
|
|
if (!list && typeof object.children == 'function') {
|
|
|
|
list = object.children(context)
|
|
|
|
} else if (!list) {
|
|
|
|
list = object.children
|
2020-04-26 02:25:07 +08:00
|
|
|
}
|
2022-02-01 05:39:46 +08:00
|
|
|
node.find('ul.contextMenu.sub').detach();
|
2020-04-26 02:25:07 +08:00
|
|
|
if (list.length) {
|
|
|
|
var childlist = $('<ul class="contextMenu sub"></ul>')
|
2022-02-28 03:03:07 +08:00
|
|
|
|
|
|
|
populateList(list, childlist, object.searchable);
|
|
|
|
|
2022-10-02 23:32:04 +08:00
|
|
|
if ((typeof object.click == 'function' || object instanceof Tool) && (object instanceof Action == false || object.side_menu)) {
|
2022-08-23 02:04:59 +08:00
|
|
|
if (node.find('> .menu_more_button').length == 0) {
|
|
|
|
node.addClass('hybrid_parent');
|
|
|
|
let more_button = Interface.createElement('div', {class: 'menu_more_button'}, Blockbench.getIconNode('more_horiz'));
|
|
|
|
node.append(more_button);
|
2023-05-28 02:53:54 +08:00
|
|
|
more_button.addEventListener('mouseenter', e => {
|
2022-08-23 02:04:59 +08:00
|
|
|
scope.hover(node.get(0), e, true);
|
|
|
|
})
|
2023-05-28 02:53:54 +08:00
|
|
|
more_button.addEventListener('mouseleave', e => {
|
|
|
|
if (node.is(':hover') && !childlist.is(':hover')) {
|
|
|
|
scope.hover(node, e);
|
|
|
|
}
|
|
|
|
})
|
2022-08-23 02:04:59 +08:00
|
|
|
}
|
2022-02-01 05:39:46 +08:00
|
|
|
} else {
|
|
|
|
node.addClass('parent');
|
|
|
|
}
|
|
|
|
node.append(childlist)
|
2020-04-26 02:25:07 +08:00
|
|
|
return childlist.children().length;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2022-02-28 03:03:07 +08:00
|
|
|
function populateList(list, menu_node, searchable) {
|
|
|
|
|
|
|
|
if (searchable) {
|
2023-02-18 17:18:50 +08:00
|
|
|
let input = Interface.createElement('input', {type: 'text', placeholder: tl('generic.search'), inputmode: 'search'});
|
2022-02-28 03:03:07 +08:00
|
|
|
let search_button = Interface.createElement('div', {}, Blockbench.getIconNode('search'));
|
|
|
|
let search_bar = Interface.createElement('li', {class: 'menu_search_bar'}, [input, search_button]);
|
|
|
|
menu_node.append(search_bar);
|
2022-03-12 22:09:09 +08:00
|
|
|
menu_node.append(Interface.createElement('li', {class: 'menu_separator'}));
|
2022-02-28 03:03:07 +08:00
|
|
|
|
|
|
|
let object_list = [];
|
|
|
|
list.forEach(function(s2, i) {
|
|
|
|
let jq_node = getEntry(s2, menu_node);
|
|
|
|
if (!jq_node) return;
|
|
|
|
object_list.push({
|
|
|
|
object: s2,
|
|
|
|
node: jq_node[0] || jq_node,
|
|
|
|
id: s2.id,
|
|
|
|
name: s2.name,
|
|
|
|
description: s2.description,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
search_button.onclick = (e) => {
|
|
|
|
input.value = '';
|
2022-03-11 04:24:33 +08:00
|
|
|
input.oninput(e);
|
2022-02-28 03:03:07 +08:00
|
|
|
}
|
|
|
|
input.oninput = (e) => {
|
|
|
|
let search_term = input.value.toUpperCase();
|
|
|
|
search_button.firstElementChild.replaceWith(Blockbench.getIconNode(search_term ? 'clear' : 'search'));
|
|
|
|
|
|
|
|
object_list.forEach(item => {
|
|
|
|
$(item.node).detach();
|
|
|
|
})
|
|
|
|
object_list.forEach(item => {
|
|
|
|
if (
|
|
|
|
typeof item.object == 'string' ||
|
|
|
|
item.object.always_show ||
|
|
|
|
(item.id && item.id.toUpperCase().includes(search_term)) ||
|
|
|
|
(item.name && item.name.toUpperCase().includes(search_term)) ||
|
|
|
|
(item.description && item.description.toUpperCase().includes(search_term))
|
|
|
|
) {
|
|
|
|
menu_node.append(item.node);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-05-28 04:56:21 +08:00
|
|
|
if (menu_node == ctxmenu) {
|
|
|
|
input.focus();
|
|
|
|
}
|
2022-02-28 03:03:07 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
list.forEach((object) => {
|
|
|
|
getEntry(object, menu_node);
|
|
|
|
})
|
|
|
|
}
|
2023-05-17 01:14:08 +08:00
|
|
|
let nodes = menu_node.children();
|
|
|
|
if (nodes.length && nodes.last().hasClass('menu_separator')) {
|
|
|
|
nodes.last().remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nodes.toArray().find(node => node.classList.contains('parent') || node.classList.contains('hybrid_parent'))) {
|
|
|
|
menu_node.addClass('scrollable');
|
2022-02-28 03:03:07 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-26 02:25:07 +08:00
|
|
|
|
2019-07-18 00:02:07 +08:00
|
|
|
function getEntry(s, parent) {
|
|
|
|
|
2023-03-11 06:17:25 +08:00
|
|
|
if (s.context) {
|
|
|
|
last_context = context;
|
|
|
|
context = s.context;
|
|
|
|
}
|
|
|
|
let scope_context = context;
|
2019-07-18 00:02:07 +08:00
|
|
|
var entry;
|
|
|
|
if (s === '_') {
|
|
|
|
entry = new MenuSeparator().menu_node
|
|
|
|
var last = parent.children().last()
|
|
|
|
if (last.length && !last.hasClass('menu_separator')) {
|
|
|
|
parent.append(entry)
|
|
|
|
}
|
2022-02-28 03:03:07 +08:00
|
|
|
return entry;
|
2020-09-27 16:01:39 +08:00
|
|
|
}
|
|
|
|
if (typeof s == 'string' && BarItems[s]) {
|
|
|
|
s = BarItems[s];
|
|
|
|
}
|
2023-03-11 06:17:25 +08:00
|
|
|
if (!Condition(s.condition, scope_context)) return;
|
2020-09-27 16:01:39 +08:00
|
|
|
|
|
|
|
if (s instanceof Action) {
|
|
|
|
|
|
|
|
entry = $(s.menu_node)
|
|
|
|
|
2021-03-02 02:38:10 +08:00
|
|
|
entry.removeClass('focused')
|
2020-09-27 16:01:39 +08:00
|
|
|
entry.off('click')
|
|
|
|
entry.off('mouseenter mousedown')
|
|
|
|
entry.on('mouseenter mousedown', function(e) {
|
|
|
|
if (this == e.target) {
|
|
|
|
scope.hover(this, e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
//Submenu
|
|
|
|
if (typeof s.children == 'function' || typeof s.children == 'object') {
|
|
|
|
createChildList(s, entry)
|
|
|
|
} else {
|
2022-10-14 04:52:04 +08:00
|
|
|
entry.on('click', (e) => {
|
|
|
|
if (!(e.target == entry[0] || e.target.parentElement == entry[0])) return;
|
|
|
|
s.trigger(e)
|
|
|
|
});
|
2023-02-22 05:56:44 +08:00
|
|
|
if (s.side_menu instanceof Menu) {
|
2023-03-11 06:17:25 +08:00
|
|
|
let content_list = typeof s.side_menu.structure == 'function' ? s.side_menu.structure(scope_context) : s.side_menu.structure;
|
2022-09-01 04:45:19 +08:00
|
|
|
createChildList(s, entry, content_list);
|
2023-02-22 05:56:44 +08:00
|
|
|
|
|
|
|
} else if (s.side_menu instanceof Dialog) {
|
|
|
|
createChildList(s, entry, [
|
|
|
|
{
|
|
|
|
name: 'menu.options',
|
|
|
|
icon: 'web_asset',
|
|
|
|
click() {
|
|
|
|
s.side_menu.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
2022-08-23 02:04:59 +08:00
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
2021-05-23 02:19:41 +08:00
|
|
|
|
2020-09-27 16:01:39 +08:00
|
|
|
parent.append(entry)
|
|
|
|
|
|
|
|
} else if (s instanceof BarSelect) {
|
|
|
|
|
|
|
|
if (typeof s.icon === 'function') {
|
2023-03-11 06:17:25 +08:00
|
|
|
var icon = Blockbench.getIconNode(s.icon(scope_context), s.color)
|
2020-09-27 16:01:39 +08:00
|
|
|
} else {
|
|
|
|
var icon = Blockbench.getIconNode(s.icon, s.color)
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
2022-03-02 04:59:40 +08:00
|
|
|
entry = $(Interface.createElement('li', {title: s.description && tl(s.description), menu_item: s.id}, Interface.createElement('span', {}, tl(s.name))));
|
2020-09-27 16:01:39 +08:00
|
|
|
entry.prepend(icon)
|
2019-07-18 00:02:07 +08:00
|
|
|
|
2020-09-27 16:01:39 +08:00
|
|
|
//Submenu
|
|
|
|
var children = [];
|
|
|
|
for (var key in s.options) {
|
|
|
|
|
|
|
|
let val = s.options[key];
|
|
|
|
if (val) {
|
|
|
|
(function() {
|
|
|
|
var save_key = key;
|
|
|
|
children.push({
|
|
|
|
name: s.getNameFor(key),
|
|
|
|
id: key,
|
|
|
|
icon: val.icon || ((s.value == save_key) ? 'far.fa-dot-circle' : 'far.fa-circle'),
|
|
|
|
condition: val.condition,
|
|
|
|
click: (e) => {
|
|
|
|
s.set(save_key);
|
|
|
|
if (s.onChange) {
|
|
|
|
s.onChange(s, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})()
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
2020-09-27 16:01:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let child_count = createChildList({children}, entry)
|
|
|
|
|
|
|
|
if (child_count !== 0 || typeof s.click === 'function') {
|
2019-07-18 00:02:07 +08:00
|
|
|
parent.append(entry)
|
|
|
|
}
|
2020-09-27 16:01:39 +08:00
|
|
|
entry.mouseenter(function(e) {
|
|
|
|
scope.hover(this, e)
|
|
|
|
})
|
|
|
|
|
2023-02-22 05:56:44 +08:00
|
|
|
/*} else if (s instanceof NumSlider) {
|
|
|
|
|
|
|
|
let icon = Blockbench.getIconNode(s.icon, s.color);
|
|
|
|
let numeric_input = new Interface.CustomElements.NumericInput(s.id, {
|
|
|
|
value: s.get(),
|
|
|
|
min: s.settings?.min, max: s.settings?.max,
|
|
|
|
onChange(value) {
|
|
|
|
if (typeof s.onBefore === 'function') {
|
|
|
|
s.onBefore()
|
|
|
|
}
|
|
|
|
s.change(() => value);
|
|
|
|
if (typeof s.onAfter === 'function') {
|
|
|
|
s.onAfter()
|
|
|
|
}
|
|
|
|
s.update();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
entry = Interface.createElement('li', {title: s.description && tl(s.description), menu_item: s.id}, [
|
|
|
|
Interface.createElement('span', {}, tl(s.name)),
|
|
|
|
numeric_input.node
|
|
|
|
]);
|
|
|
|
entry.prepend(icon);
|
|
|
|
|
|
|
|
parent.append(entry);
|
|
|
|
|
|
|
|
$(entry).mouseenter(function(e) {
|
|
|
|
scope.hover(this, e)
|
|
|
|
})
|
|
|
|
*/
|
2022-08-15 20:57:07 +08:00
|
|
|
} else if (s instanceof HTMLElement) {
|
|
|
|
parent.append(s);
|
|
|
|
|
2019-07-18 00:02:07 +08:00
|
|
|
} else if (typeof s === 'object') {
|
2020-04-30 05:35:47 +08:00
|
|
|
|
2020-09-27 16:01:39 +08:00
|
|
|
let child_count;
|
|
|
|
if (typeof s.icon === 'function') {
|
2023-03-11 06:17:25 +08:00
|
|
|
var icon = Blockbench.getIconNode(s.icon(scope_context), s.color)
|
2020-09-27 16:01:39 +08:00
|
|
|
} else {
|
|
|
|
var icon = Blockbench.getIconNode(s.icon, s.color)
|
|
|
|
}
|
2022-03-02 04:59:40 +08:00
|
|
|
entry = $(Interface.createElement('li', {title: s.description && tl(s.description), menu_item: s.id}, Interface.createElement('span', {}, tl(s.name))));
|
2021-12-09 01:43:58 +08:00
|
|
|
entry.prepend(icon);
|
|
|
|
if (s.keybind) {
|
|
|
|
let label = document.createElement('label');
|
|
|
|
label.classList.add('keybinding_label')
|
|
|
|
label.innerText = s.keybind || '';
|
|
|
|
entry.append(label);
|
|
|
|
}
|
2020-09-27 16:01:39 +08:00
|
|
|
if (typeof s.click === 'function') {
|
2022-02-01 05:39:46 +08:00
|
|
|
entry.on('click', e => {
|
2020-09-27 16:01:39 +08:00
|
|
|
if (e.target == entry.get(0)) {
|
2023-03-11 06:17:25 +08:00
|
|
|
s.click(scope_context, e)
|
2020-09-27 16:01:39 +08:00
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
})
|
|
|
|
}
|
2020-09-27 16:01:39 +08:00
|
|
|
//Submenu
|
|
|
|
if (typeof s.children == 'function' || typeof s.children == 'object') {
|
2022-02-01 05:39:46 +08:00
|
|
|
child_count = createChildList(s, entry);
|
2020-09-27 16:01:39 +08:00
|
|
|
}
|
|
|
|
if (child_count !== 0 || typeof s.click === 'function') {
|
|
|
|
parent.append(entry)
|
|
|
|
}
|
|
|
|
entry.mouseenter(function(e) {
|
|
|
|
scope.hover(this, e)
|
|
|
|
})
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
2021-05-23 02:19:41 +08:00
|
|
|
//Highlight
|
|
|
|
if (scope.highlight_action == s && entry) {
|
|
|
|
let obj = entry;
|
|
|
|
while (obj[0] && obj[0].nodeName == 'LI') {
|
|
|
|
obj.addClass('highlighted');
|
|
|
|
obj = obj.parent().parent();
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 06:17:25 +08:00
|
|
|
if (s.context && last_context != context) context = last_context;
|
2022-02-28 03:03:07 +08:00
|
|
|
return entry;
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
|
2022-09-01 04:45:19 +08:00
|
|
|
let content_list = typeof this.structure == 'function' ? this.structure(context) : this.structure;
|
|
|
|
populateList(content_list, ctxmenu, this.options.searchable);
|
2019-07-18 00:02:07 +08:00
|
|
|
|
|
|
|
var el_width = ctxmenu.width()
|
|
|
|
var el_height = ctxmenu.height()
|
2021-01-11 06:57:55 +08:00
|
|
|
let window_height = window.innerHeight - 26;
|
2019-07-18 00:02:07 +08:00
|
|
|
|
|
|
|
if (position && position.clientX !== undefined) {
|
|
|
|
var offset_left = position.clientX
|
|
|
|
var offset_top = position.clientY+1
|
2020-04-26 02:25:07 +08:00
|
|
|
|
|
|
|
} else if (position == document.body) {
|
|
|
|
var offset_left = (document.body.clientWidth-el_width)/2
|
|
|
|
var offset_top = (document.body.clientHeight-el_height)/2
|
|
|
|
|
2021-10-08 17:28:41 +08:00
|
|
|
} else if (position == 'mouse') {
|
|
|
|
var offset_left = mouse_pos.x;
|
|
|
|
var offset_top = mouse_pos.y;
|
|
|
|
|
2019-07-18 00:02:07 +08:00
|
|
|
} else {
|
|
|
|
if (!position && scope.type === 'bar_menu') {
|
|
|
|
position = scope.label
|
2020-09-30 23:29:13 +08:00
|
|
|
} else if (position && position.parentElement.classList.contains('tool')) {
|
|
|
|
position = position.parentElement;
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
var offset_left = $(position).offset().left;
|
2022-08-15 20:57:07 +08:00
|
|
|
var offset_top = $(position).offset().top + position.offsetHeight;
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
|
2021-01-31 07:12:53 +08:00
|
|
|
if (offset_left > window.innerWidth - el_width) {
|
2019-07-18 00:02:07 +08:00
|
|
|
offset_left -= el_width
|
2020-03-15 04:32:24 +08:00
|
|
|
if (position && position.clientWidth) offset_left += position.clientWidth;
|
2021-10-09 05:31:17 +08:00
|
|
|
if (offset_left < 0) offset_left = 0;
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
2023-05-17 01:14:08 +08:00
|
|
|
if (offset_top > window_height - el_height ) {
|
|
|
|
if (el_height < offset_top - 50) {
|
|
|
|
// Snap to element top
|
2023-02-05 02:48:32 +08:00
|
|
|
offset_top -= el_height;
|
|
|
|
if (position instanceof HTMLElement) {
|
|
|
|
offset_top -= position.clientHeight;
|
|
|
|
}
|
2023-05-17 01:14:08 +08:00
|
|
|
} else {
|
|
|
|
// Move up
|
|
|
|
offset_top = window_height - el_height;
|
2021-05-29 05:42:35 +08:00
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
2023-02-05 02:48:32 +08:00
|
|
|
offset_top = Math.max(offset_top, 26);
|
2019-07-18 00:02:07 +08:00
|
|
|
|
|
|
|
ctxmenu.css('left', offset_left+'px')
|
|
|
|
ctxmenu.css('top', offset_top +'px')
|
|
|
|
|
2020-07-18 03:49:23 +08:00
|
|
|
if (el_height > window_height) {
|
2021-01-11 06:57:55 +08:00
|
|
|
handleMenuOverflow(ctxmenu);
|
2020-07-18 03:49:23 +08:00
|
|
|
}
|
|
|
|
|
2022-09-23 23:47:37 +08:00
|
|
|
scope.node.onclick = (ev) => {
|
2021-05-29 05:42:35 +08:00
|
|
|
if (
|
2022-10-14 04:52:04 +08:00
|
|
|
ev.target.classList.contains('parent') ||
|
|
|
|
(ev.target.parentNode && ev.target.parentNode.classList.contains('parent')) ||
|
2022-02-28 03:03:07 +08:00
|
|
|
ev.target.classList.contains('menu_search_bar') ||
|
|
|
|
(ev.target.parentNode && ev.target.parentNode.classList.contains('menu_search_bar'))
|
2021-05-29 05:42:35 +08:00
|
|
|
) {} else {
|
2022-08-15 20:57:07 +08:00
|
|
|
if (this.options.keep_open) {
|
|
|
|
this.hide()
|
|
|
|
this.open(position, context);
|
|
|
|
} else {
|
|
|
|
this.hide()
|
|
|
|
}
|
2021-05-29 05:42:35 +08:00
|
|
|
}
|
2022-09-23 23:47:37 +08:00
|
|
|
}
|
2021-05-29 05:42:35 +08:00
|
|
|
|
2019-07-18 00:02:07 +08:00
|
|
|
if (scope.type === 'bar_menu') {
|
|
|
|
MenuBar.open = scope
|
|
|
|
$(scope.label).addClass('opened')
|
|
|
|
}
|
|
|
|
open_menu = scope;
|
|
|
|
return scope;
|
|
|
|
}
|
2023-03-09 07:02:52 +08:00
|
|
|
show(...args) {
|
|
|
|
return this.open(...args);
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
hide() {
|
2022-05-08 01:35:41 +08:00
|
|
|
if (this.onClose) this.onClose();
|
2021-05-23 02:19:41 +08:00
|
|
|
$(this.node).find('li.highlighted').removeClass('highlighted');
|
2019-07-18 00:02:07 +08:00
|
|
|
$(this.node).detach()
|
2021-02-20 17:31:36 +08:00
|
|
|
open_menu = null;
|
2019-07-18 00:02:07 +08:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
conditionMet() {
|
2021-01-27 05:46:17 +08:00
|
|
|
return Condition(this.condition);
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|
|
|
|
addAction(action, path) {
|
2022-09-01 04:45:19 +08:00
|
|
|
if (this.structure instanceof Array == false) return;
|
2022-03-02 04:59:40 +08:00
|
|
|
if (path === undefined) path = '';
|
|
|
|
if (typeof path !== 'string') path = path.toString();
|
2019-07-18 00:02:07 +08:00
|
|
|
var track = path.split('.')
|
|
|
|
|
|
|
|
function traverse(arr, layer) {
|
|
|
|
if (track.length === layer || track[layer] === '' || !isNaN(parseInt(track[layer]))) {
|
|
|
|
var index = arr.length;
|
|
|
|
if (track[layer] !== '' && track.length !== layer) {
|
|
|
|
index = parseInt(track[layer])
|
|
|
|
}
|
|
|
|
arr.splice(index, 0, action)
|
|
|
|
} else {
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
var item = arr[i]
|
2023-04-19 00:41:44 +08:00
|
|
|
if (item.children instanceof Array && item.id === track[layer] && layer < 20) {
|
2019-07-18 00:02:07 +08:00
|
|
|
traverse(item.children, layer+1)
|
|
|
|
i = 1000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
traverse(this.structure, 0)
|
|
|
|
if (action && action.menus) {
|
|
|
|
action.menus.push({menu: this, path});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
removeAction(path) {
|
2022-09-01 04:45:19 +08:00
|
|
|
if (this.structure instanceof Array == false) return;
|
2019-07-18 00:02:07 +08:00
|
|
|
var scope = this;
|
2021-10-13 19:12:55 +08:00
|
|
|
if (path instanceof Action) {
|
|
|
|
let action = path;
|
|
|
|
this.structure.remove(action);
|
|
|
|
this.structure.remove(action.id);
|
|
|
|
action.menus.remove(this);
|
|
|
|
}
|
|
|
|
if (path === undefined) path = '';
|
|
|
|
if (typeof path == 'string') path = path.split('.');
|
2019-07-18 00:02:07 +08:00
|
|
|
|
|
|
|
function traverse(arr, layer) {
|
|
|
|
if (!isNaN(parseInt(path[layer]))) {
|
|
|
|
result = arr[parseInt(path[layer])]
|
|
|
|
|
|
|
|
} else if (typeof path[layer] === 'string') {
|
|
|
|
var i = arr.length-1;
|
|
|
|
while (i >= 0) {
|
|
|
|
var item = arr[i]
|
|
|
|
if (item.id === path[layer] && layer < 20) {
|
|
|
|
if (layer === path.length-1) {
|
|
|
|
var action = arr.splice(i, 1)[0]
|
|
|
|
if (action instanceof Action) {
|
|
|
|
for (var i = action.menus.length-1; i >= 0; i--) {
|
|
|
|
if (action.menus[i].menu == scope) {
|
|
|
|
action.menus.splice(i, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (item.children) {
|
|
|
|
traverse(item.children, layer+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
traverse(this.structure, 0)
|
|
|
|
}
|
2020-03-05 03:56:17 +08:00
|
|
|
deleteItem(rm_item) {
|
|
|
|
var scope = this;
|
|
|
|
|
|
|
|
function traverse(arr, layer) {
|
|
|
|
arr.forEachReverse((item, i) => {
|
|
|
|
if (item === rm_item || item === rm_item.id) {
|
|
|
|
arr.splice(i, 1)
|
|
|
|
} else if (item && item.children instanceof Array) {
|
|
|
|
traverse(item.children)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
traverse(this.structure, 0)
|
|
|
|
rm_item.menus.remove(scope)
|
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
}
|