ozdenui 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +14 -0
- data/NOTICE +5 -0
- data/README.md +242 -0
- data/exe/ozdenui +17 -0
- data/lib/ozdenui/AGENTS.md +210 -0
- data/lib/ozdenui/COMPONENTS.md +3017 -0
- data/lib/ozdenui/app.rb +749 -0
- data/lib/ozdenui/command.rb +107 -0
- data/lib/ozdenui/components.rb +368 -0
- data/lib/ozdenui/dialogs.rb +212 -0
- data/lib/ozdenui/files.rb +124 -0
- data/lib/ozdenui/icon.png +0 -0
- data/lib/ozdenui/kit.json +1 -0
- data/lib/ozdenui/kit.rb +115 -0
- data/lib/ozdenui/look.rb +229 -0
- data/lib/ozdenui/objects.rb +652 -0
- data/lib/ozdenui/server.rb +270 -0
- data/lib/ozdenui/values.rb +273 -0
- data/lib/ozdenui/version.rb +15 -0
- data/lib/ozdenui/web/basic/LICENSE +14 -0
- data/lib/ozdenui/web/basic/basic.min.css +1 -0
- data/lib/ozdenui/web/basic/basic.min.js +78 -0
- data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
- data/lib/ozdenui/web/bridge.js +584 -0
- data/lib/ozdenui/web/comp/accordion.js +659 -0
- data/lib/ozdenui/web/comp/action-button/add.png +0 -0
- data/lib/ozdenui/web/comp/action-button.js +521 -0
- data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
- data/lib/ozdenui/web/comp/app-bar.js +237 -0
- data/lib/ozdenui/web/comp/badge.js +292 -0
- data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
- data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
- data/lib/ozdenui/web/comp/canvas.js +652 -0
- data/lib/ozdenui/web/comp/chat-view.js +827 -0
- data/lib/ozdenui/web/comp/check-box.js +490 -0
- data/lib/ozdenui/web/comp/code-editor.js +965 -0
- data/lib/ozdenui/web/comp/color-picker.js +636 -0
- data/lib/ozdenui/web/comp/context-menu.js +951 -0
- data/lib/ozdenui/web/comp/currency-field.js +209 -0
- data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/data-table.js +937 -0
- data/lib/ozdenui/web/comp/date-picker.js +1055 -0
- data/lib/ozdenui/web/comp/dialog.js +992 -0
- data/lib/ozdenui/web/comp/email-field.js +65 -0
- data/lib/ozdenui/web/comp/file-picker.js +920 -0
- data/lib/ozdenui/web/comp/filters.js +807 -0
- data/lib/ozdenui/web/comp/form.js +602 -0
- data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
- data/lib/ozdenui/web/comp/gauge.js +439 -0
- data/lib/ozdenui/web/comp/graph-box.js +3911 -0
- data/lib/ozdenui/web/comp/grid-group.js +185 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
- data/lib/ozdenui/web/comp/image-view.js +598 -0
- data/lib/ozdenui/web/comp/item-list.js +1740 -0
- data/lib/ozdenui/web/comp/json-editor.js +3221 -0
- data/lib/ozdenui/web/comp/json-view.js +597 -0
- data/lib/ozdenui/web/comp/label-editor.js +1140 -0
- data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
- data/lib/ozdenui/web/comp/left-menu.js +548 -0
- data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
- data/lib/ozdenui/web/comp/loading-screen.js +413 -0
- data/lib/ozdenui/web/comp/log-view.js +530 -0
- data/lib/ozdenui/web/comp/login-page.js +685 -0
- data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
- data/lib/ozdenui/web/comp/menu-bar.js +624 -0
- data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
- data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
- data/lib/ozdenui/web/comp/notice.js +229 -0
- data/lib/ozdenui/web/comp/number-field.js +128 -0
- data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
- data/lib/ozdenui/web/comp/page-control.js +762 -0
- data/lib/ozdenui/web/comp/page-header.js +191 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field.js +192 -0
- data/lib/ozdenui/web/comp/period-bar.js +436 -0
- data/lib/ozdenui/web/comp/phone-field.js +322 -0
- data/lib/ozdenui/web/comp/progress-bar.js +992 -0
- data/lib/ozdenui/web/comp/property-panel.js +3639 -0
- data/lib/ozdenui/web/comp/radio-button.js +527 -0
- data/lib/ozdenui/web/comp/range-slider.js +569 -0
- data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
- data/lib/ozdenui/web/comp/scroll-area.js +308 -0
- data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
- data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
- data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
- data/lib/ozdenui/web/comp/search-input.js +725 -0
- data/lib/ozdenui/web/comp/search-results.js +596 -0
- data/lib/ozdenui/web/comp/select-box.js +1862 -0
- data/lib/ozdenui/web/comp/settings-row.js +299 -0
- data/lib/ozdenui/web/comp/side-panel.js +616 -0
- data/lib/ozdenui/web/comp/slider-field.js +952 -0
- data/lib/ozdenui/web/comp/slider.js +838 -0
- data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
- data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-button.js +981 -0
- data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
- data/lib/ozdenui/web/comp/smart-table.js +1883 -0
- data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
- data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
- data/lib/ozdenui/web/comp/splitter.js +411 -0
- data/lib/ozdenui/web/comp/status-bar.js +408 -0
- data/lib/ozdenui/web/comp/step-bar.js +345 -0
- data/lib/ozdenui/web/comp/stepper.js +753 -0
- data/lib/ozdenui/web/comp/tabs.js +1366 -0
- data/lib/ozdenui/web/comp/tag-input.js +1107 -0
- data/lib/ozdenui/web/comp/tap-effect.js +170 -0
- data/lib/ozdenui/web/comp/text-area-field.js +233 -0
- data/lib/ozdenui/web/comp/text-field.js +1006 -0
- data/lib/ozdenui/web/comp/time-line.js +398 -0
- data/lib/ozdenui/web/comp/time-picker.js +1032 -0
- data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
- data/lib/ozdenui/web/comp/tiny-select.js +322 -0
- data/lib/ozdenui/web/comp/tiny-table.js +283 -0
- data/lib/ozdenui/web/comp/toast.js +891 -0
- data/lib/ozdenui/web/comp/toggle.js +757 -0
- data/lib/ozdenui/web/comp/tool-bar.js +760 -0
- data/lib/ozdenui/web/comp/tooltip.js +534 -0
- data/lib/ozdenui/web/comp/tree-view.js +1179 -0
- data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
- data/lib/ozdenui/web/comp/ui-standards.js +403 -0
- data/lib/ozdenui/web/comp/url-field.js +190 -0
- data/lib/ozdenui/web/comp/web-view.js +588 -0
- data/lib/ozdenui/web/components.js +123 -0
- data/lib/ozdenui/web/index.html +39 -0
- data/lib/ozdenui/window.rb +188 -0
- data/lib/ozdenui/window_cocoa.rb +221 -0
- data/lib/ozdenui.rb +32 -0
- metadata +200 -0
|
@@ -0,0 +1,951 @@
|
|
|
1
|
+
/* Bismillah */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
ContextMenu - v26.09
|
|
6
|
+
|
|
7
|
+
UI COMPONENT TEMPLATE
|
|
8
|
+
- A simple menu that opens at the mouse point. (Like the right click menu of Windows, but simpler.)
|
|
9
|
+
- Items can be only text, or icon + text.
|
|
10
|
+
- If at least one item has an icon, all texts start at the same place. (Items without icon get an empty icon space.)
|
|
11
|
+
- If no item has an icon, texts start near the left side.
|
|
12
|
+
- "-" in the item list adds a separator line.
|
|
13
|
+
- Closes on: item click, click outside, Escape, mouse wheel outside, page resize, window blur.
|
|
14
|
+
- Keyboard: ArrowUp/ArrowDown to move, Enter or Space to click, Escape to close.
|
|
15
|
+
- The menu is made on the page (over everything), not in the group where ContextMenu() is called. A menu with one
|
|
16
|
+
target is removed with it; a menu of a list, of attachTo() or of openAt() is removed by the page (or obj.own(menu)).
|
|
17
|
+
- Submenus: an item with items: [...] has a › at its end. Its menu opens at its side on
|
|
18
|
+
hover, a click, Enter or ArrowRight (at the left when there is no room). ArrowLeft or Escape closes only the
|
|
19
|
+
submenu. A click on an item of a submenu closes every menu and calls onClick(self, item, index) of the menu
|
|
20
|
+
(self: the menu of the click, item: the item of the submenu). Submenus can have submenus.
|
|
21
|
+
- For a menu of a program (MenuBar uses them): an item with shortcut has a soft text at its end (only
|
|
22
|
+
a text: the page runs it, or MenuBar), an item with checked (0 / 1) has a check mark in the icon place
|
|
23
|
+
(setItemChecked), and openBelow(obj) opens the menu under an object (over it when there is no room).
|
|
24
|
+
- Style packages: "light" (default; the old name "classic" works too), "dark", "minimal", "minimalDark". Select with styleName. (ContextMenu.styles)
|
|
25
|
+
|
|
26
|
+
ITEM: { text, key, iconFile, icon, items, shortcut, checked, enabled, data, onClick }
|
|
27
|
+
- A string is also accepted: "Copy" -> { text: "Copy" }
|
|
28
|
+
- "-" or { type: "separator" }: Separator line
|
|
29
|
+
- icon: an SVG text ("<svg ...>", your own code, not a user text). Drawn with the color of the text of the item
|
|
30
|
+
(use stroke="currentColor" or fill="currentColor"), so one icon works in every style (dark too). Its size: style.icon.
|
|
31
|
+
- iconFile: an image file (its colors are its own).
|
|
32
|
+
- items: the items of its submenu (the item itself is not clicked).
|
|
33
|
+
- shortcut: a soft text at the end of the row ("Ctrl+S").
|
|
34
|
+
- checked: 0 or 1: the item is a choice that can be on: a check mark when 1 (its place stays empty when 0). Over an icon.
|
|
35
|
+
- enabled: 0 -> Shown, but can not be clicked.
|
|
36
|
+
- onClick: function (self, item, index) {} -> Optional, only for this item.
|
|
37
|
+
|
|
38
|
+
USAGE:
|
|
39
|
+
const menu = ContextMenu({ items: ["Open", "-", { text: "Delete", key: "delete", iconFile: "delete.png" }] });
|
|
40
|
+
const menu2 = ContextMenu({ items: [{ text: "Copy to", icon: "<svg ...>", items: [{ text: "My colors", key: "mine" }] }] });
|
|
41
|
+
menu.attachTo(myBox); // Right click on myBox opens the menu.
|
|
42
|
+
menu.attachTo(myButton, "click"); // Left click on myButton opens the menu.
|
|
43
|
+
menu.openAt(event.clientX, event.clientY);
|
|
44
|
+
menu.openBelow(myButton); // Under the object, at its left edge.
|
|
45
|
+
menu.setItemChecked("wrap", 1);
|
|
46
|
+
|
|
47
|
+
Started Date: September 2026
|
|
48
|
+
Webpage: https://bug7a.github.io/js-components/
|
|
49
|
+
|
|
50
|
+
Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
51
|
+
Licensed under the Apache License, Version 2.0
|
|
52
|
+
SPDX-License-Identifier: Apache-2.0
|
|
53
|
+
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
"use strict";
|
|
57
|
+
|
|
58
|
+
// Default values:
|
|
59
|
+
const ContextMenuDefaults = {
|
|
60
|
+
key: "0",
|
|
61
|
+
items: [], // [{ text, key, iconFile, enabled, data, onClick }, "-", "Text"]
|
|
62
|
+
target: null, // An object (or a list of objects). The menu opens on it with the trigger. One object: the menu is removed with it.
|
|
63
|
+
trigger: "contextmenu", // "contextmenu": Right click, "click": Left click
|
|
64
|
+
minWidth: 180,
|
|
65
|
+
maxWidth: 320, // Long texts are cut with "...". 0: No limit.
|
|
66
|
+
enabled: 1,
|
|
67
|
+
ariaLabel: "Menu",
|
|
68
|
+
onClick: function (self, item, index) { }, // self.source: The object that opened the menu.
|
|
69
|
+
onOpen: function (self) { },
|
|
70
|
+
onClose: function (self) { },
|
|
71
|
+
onSideKey: function (self, direction) { }, // ArrowRight (1) / ArrowLeft (-1) that no submenu takes (MenuBar: the next menu)
|
|
72
|
+
parentMenu: null, // (inside) The menu of a submenu: it handles the keys and the clicks outside for it.
|
|
73
|
+
styleName: "light", // "light" ("classic": its old name), "dark", "minimal", "minimalDark" or a name added to ContextMenu.styles
|
|
74
|
+
style: { // Light style package (default)
|
|
75
|
+
menu: {
|
|
76
|
+
color: "white",
|
|
77
|
+
border: 1,
|
|
78
|
+
borderColor: Black(0.12),
|
|
79
|
+
round: 8,
|
|
80
|
+
padding: 4,
|
|
81
|
+
shadow: "0px 8px 24px " + Black(0.14),
|
|
82
|
+
zIndex: 1000,
|
|
83
|
+
},
|
|
84
|
+
item: {
|
|
85
|
+
height: 32,
|
|
86
|
+
fontSize: 14,
|
|
87
|
+
textColor: Black(0.85),
|
|
88
|
+
color: "transparent",
|
|
89
|
+
round: 5,
|
|
90
|
+
padding: 10, // Left and right space
|
|
91
|
+
gap: 10, // Space between icon and text
|
|
92
|
+
border: 0, // px: an edge around every item (its color: borderColor, itemHover.borderColor). 0: none
|
|
93
|
+
borderColor: "transparent",
|
|
94
|
+
},
|
|
95
|
+
itemHover: {
|
|
96
|
+
textColor: Black(0.9),
|
|
97
|
+
color: Black(0.06),
|
|
98
|
+
borderColor: "", // the edge of the item under the mouse (item.border). "": its own
|
|
99
|
+
},
|
|
100
|
+
disabled: {
|
|
101
|
+
textColor: Black(0.35),
|
|
102
|
+
opacity: 0.4, // Icon opacity
|
|
103
|
+
},
|
|
104
|
+
icon: {
|
|
105
|
+
width: 16,
|
|
106
|
+
height: 16,
|
|
107
|
+
opacity: 0.85, // An SVG icon (icon): a little softer than its text
|
|
108
|
+
},
|
|
109
|
+
submenu: {
|
|
110
|
+
arrowOpacity: 0.55, // The › at the end of an item with a submenu
|
|
111
|
+
overlap: 4, // px: a submenu covers the edge of its menu a little
|
|
112
|
+
},
|
|
113
|
+
shortcut: {
|
|
114
|
+
fontSize: 12,
|
|
115
|
+
opacity: 0.6, // It takes the color of the text of its item.
|
|
116
|
+
space: 24, // Space before it
|
|
117
|
+
},
|
|
118
|
+
separator: {
|
|
119
|
+
color: Black(0.1),
|
|
120
|
+
space: 4, // Space above and below the line
|
|
121
|
+
},
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const ContextMenu = function (params = {}) {
|
|
126
|
+
|
|
127
|
+
// Merge style package: params.style > ContextMenu.styles[styleName] > ContextMenuDefaults.style (light)
|
|
128
|
+
basic.applyStylePackage(params, ContextMenu.styles, ContextMenuDefaults);
|
|
129
|
+
|
|
130
|
+
// Merge params:
|
|
131
|
+
mergeIntoIfMissing(params, ContextMenuDefaults);
|
|
132
|
+
|
|
133
|
+
// Edit params, if needed:
|
|
134
|
+
const _ms = params.style.menu;
|
|
135
|
+
params.width = "auto";
|
|
136
|
+
params.height = "auto";
|
|
137
|
+
params.color = _ms.color;
|
|
138
|
+
params.border = _ms.border;
|
|
139
|
+
params.borderColor = _ms.borderColor;
|
|
140
|
+
params.round = _ms.round;
|
|
141
|
+
params.padding = _ms.padding;
|
|
142
|
+
params.zIndex = _ms.zIndex;
|
|
143
|
+
params.boxShadow = _ms.shadow;
|
|
144
|
+
|
|
145
|
+
// WHY: The menu must be over everything. Create it directly on the page,
|
|
146
|
+
// even if ContextMenu() is called inside a group.
|
|
147
|
+
const _previousContainer = getDefaultContainerBox();
|
|
148
|
+
setDefaultContainerBox(page);
|
|
149
|
+
|
|
150
|
+
// BOX: Component container (the menu panel)
|
|
151
|
+
let box = startObject(params);
|
|
152
|
+
|
|
153
|
+
// *** PRIVATE VARIABLES:
|
|
154
|
+
const _s = box.style;
|
|
155
|
+
let rows = []; // Created row objects (items and separators)
|
|
156
|
+
let attachedList = []; // [{ obj, trigger, removeEvent }]
|
|
157
|
+
let isOpen = 0;
|
|
158
|
+
let activeIndex = -1;
|
|
159
|
+
let skipOpenFor = null; // WHY: A click on the opener button (while open) must close the menu, not open it again.
|
|
160
|
+
let skipOpenTime = 0;
|
|
161
|
+
let openPageSize = ""; // Page size when the menu was opened.
|
|
162
|
+
const PAGE_SIDE_SPACE = 8; // Minimum space between the menu and the page sides
|
|
163
|
+
const OPENER_CLICK_TIME_LIMIT = 1000; // ms
|
|
164
|
+
const OPEN_MOTION = "opacity 0.12s, transform 0.12s";
|
|
165
|
+
const ARROW_ICON = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="M6.5 4l4 4-4 4"/></svg>';
|
|
166
|
+
const ARROW_WIDTH = 14;
|
|
167
|
+
const CHECK_ICON = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3.5 8.5l3 3 6-7"/></svg>';
|
|
168
|
+
let openSubmenuIndex = -1; // The item whose submenu is open (-1: none)
|
|
169
|
+
|
|
170
|
+
// *** PUBLIC VARIABLES:
|
|
171
|
+
// [var] Items: [{ text, key, iconFile, enabled, data, onClick } or { type: "separator" }]
|
|
172
|
+
box.items = [];
|
|
173
|
+
// [var] The object that opened the menu. (attachTo) null: Opened with openAt().
|
|
174
|
+
box.source = null;
|
|
175
|
+
|
|
176
|
+
// *** PRIVATE FUNCTIONS:
|
|
177
|
+
|
|
178
|
+
const toFlag = function (value) {
|
|
179
|
+
return basic.isOn(value) ? 1 : 0;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const normalizeItem = function (item) {
|
|
183
|
+
if (item === "-" || (item && item.type == "separator")) {
|
|
184
|
+
return { type: "separator" };
|
|
185
|
+
}
|
|
186
|
+
if (typeof item == "string" || typeof item == "number") {
|
|
187
|
+
return { type: "item", text: String(item), enabled: 1 };
|
|
188
|
+
}
|
|
189
|
+
const result = Object.assign({ text: "", enabled: 1 }, item);
|
|
190
|
+
result.type = "item";
|
|
191
|
+
result.enabled = toFlag(result.enabled);
|
|
192
|
+
return result;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const isSubmenuItem = function (item) {
|
|
196
|
+
return !!(item && item.type == "item" && Array.isArray(item.items) && item.items.length);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const isCheckItem = function (item) {
|
|
200
|
+
return (item.type == "item" && item.checked !== undefined && item.checked !== null);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const openSubmenu = function () {
|
|
204
|
+
const row = rows[openSubmenuIndex];
|
|
205
|
+
return (row && row.submenu && row.submenu.isOpen()) ? row.submenu : null;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// The menu and its open submenus
|
|
209
|
+
const isInsideMenu = function (element) {
|
|
210
|
+
if (!box || !element) return false;
|
|
211
|
+
if (box.elem.contains(element)) return true;
|
|
212
|
+
const submenu = openSubmenu();
|
|
213
|
+
return !!(submenu && submenu.containsElement(element));
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
// The first menu (a submenu closes it with every click on an item)
|
|
217
|
+
const rootMenu = function () {
|
|
218
|
+
let menu = box;
|
|
219
|
+
while (menu.parentMenu) menu = menu.parentMenu;
|
|
220
|
+
return menu;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const isClickableIndex = function (index) {
|
|
224
|
+
const item = box.items[index];
|
|
225
|
+
return !!(item && item.type == "item" && item.enabled == 1);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const setActive = function (index) {
|
|
229
|
+
|
|
230
|
+
if (activeIndex == index) return;
|
|
231
|
+
|
|
232
|
+
// WHY: Another item of this menu: its open submenu goes. (Not -1: the mouse may be going into the submenu.)
|
|
233
|
+
if (index >= 0 && index != openSubmenuIndex) box.closeSubmenu();
|
|
234
|
+
|
|
235
|
+
const oldRow = rows[activeIndex];
|
|
236
|
+
const keepsHover = (activeIndex == openSubmenuIndex && openSubmenu()); // The item of the open submenu stays painted.
|
|
237
|
+
if (oldRow && oldRow.label && !keepsHover) setRowHover(oldRow, 0);
|
|
238
|
+
|
|
239
|
+
activeIndex = index;
|
|
240
|
+
|
|
241
|
+
const newRow = rows[activeIndex];
|
|
242
|
+
if (newRow && newRow.label) setRowHover(newRow, 1);
|
|
243
|
+
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const setRowHover = function (row, isOver) {
|
|
247
|
+
const style = (isOver) ? _s.itemHover : _s.item;
|
|
248
|
+
const textColor = (row.item.enabled == 1) ? style.textColor : _s.disabled.textColor;
|
|
249
|
+
row.color = style.color;
|
|
250
|
+
if (_s.item.border) row.borderColor = (isOver && _s.itemHover.borderColor) ? _s.itemHover.borderColor : _s.item.borderColor;
|
|
251
|
+
row.label.textColor = textColor;
|
|
252
|
+
// WHY: An SVG icon and the › are drawn with currentColor: they take the color of the text.
|
|
253
|
+
if (row.icon && row.icon.isSvg) row.icon.textColor = textColor;
|
|
254
|
+
if (row.arrow) row.arrow.textColor = textColor;
|
|
255
|
+
if (row.lblShortcut) row.lblShortcut.textColor = textColor;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// Enabled/disabled view of a row. (Colors, cursor, icon opacity)
|
|
259
|
+
const updateRowEnabled = function (row) {
|
|
260
|
+
const enabled = (row.item.enabled == 1);
|
|
261
|
+
row.cursor = (enabled) ? "pointer" : "default";
|
|
262
|
+
row.aria = { disabled: (enabled) ? "false" : "true" };
|
|
263
|
+
if (row.icon) {
|
|
264
|
+
const normalOpacity = (row.icon.isSvg) ? _s.icon.opacity : 1;
|
|
265
|
+
const hasPicture = (row.item.iconFile || row.item.icon);
|
|
266
|
+
row.icon.opacity = (enabled || !hasPicture) ? normalOpacity : _s.disabled.opacity;
|
|
267
|
+
}
|
|
268
|
+
if (isSubmenuItem(row.item)) row.aria = { haspopup: "menu" };
|
|
269
|
+
setRowHover(row, 0);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// Move the active item with the keyboard. Skips separators and disabled items.
|
|
273
|
+
const moveActive = function (direction) {
|
|
274
|
+
|
|
275
|
+
const count = box.items.length;
|
|
276
|
+
if (count == 0) return;
|
|
277
|
+
|
|
278
|
+
// WHY: No active item: ArrowDown starts from the first item, ArrowUp from the last item.
|
|
279
|
+
let index = (activeIndex < 0 && direction < 0) ? count : activeIndex;
|
|
280
|
+
for (let i = 0; i < count; i++) {
|
|
281
|
+
index = (index + direction + count) % count;
|
|
282
|
+
if (isClickableIndex(index)) {
|
|
283
|
+
setActive(index);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const showCheckMark = function (row) {
|
|
291
|
+
const isChecked = basic.isOn(row.item.checked);
|
|
292
|
+
row.icon.text = (isChecked) ? CHECK_ICON : "";
|
|
293
|
+
row.aria = { checked: (isChecked) ? "true" : "false" };
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const createSeparator = function () {
|
|
297
|
+
|
|
298
|
+
// BOX: Line
|
|
299
|
+
const line = Box({
|
|
300
|
+
width: "auto",
|
|
301
|
+
height: 1,
|
|
302
|
+
color: _s.separator.color,
|
|
303
|
+
shrink: 0,
|
|
304
|
+
});
|
|
305
|
+
line.css.margin = _s.separator.space + "px " + _s.item.padding + "px";
|
|
306
|
+
line.role = "separator";
|
|
307
|
+
|
|
308
|
+
return line;
|
|
309
|
+
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const createItem = function (item, index, hasIcon) {
|
|
313
|
+
|
|
314
|
+
// GROUP: Row (icon space + label)
|
|
315
|
+
const row = HGroup({
|
|
316
|
+
width: "auto",
|
|
317
|
+
height: _s.item.height,
|
|
318
|
+
align: "left center",
|
|
319
|
+
gap: _s.item.gap,
|
|
320
|
+
padding: [_s.item.padding, 0],
|
|
321
|
+
round: _s.item.round,
|
|
322
|
+
});
|
|
323
|
+
if (_s.item.border) row.props({ border: _s.item.border, borderColor: _s.item.borderColor });
|
|
324
|
+
row.role = "menuitem";
|
|
325
|
+
row.item = item;
|
|
326
|
+
|
|
327
|
+
// ICON: Only if one of the items has an icon.
|
|
328
|
+
// WHY: Items without an icon get an empty space, so every text starts at the same place.
|
|
329
|
+
if (hasIcon) {
|
|
330
|
+
if (isCheckItem(item)) {
|
|
331
|
+
// LABEL: The check mark (the color of the text)
|
|
332
|
+
row.icon = Label({ width: _s.icon.width, height: _s.icon.height, shrink: 0 });
|
|
333
|
+
row.icon.elem.classList.add("context-menu-svg");
|
|
334
|
+
row.icon.isSvg = 1;
|
|
335
|
+
row.role = "menuitemcheckbox";
|
|
336
|
+
showCheckMark(row);
|
|
337
|
+
} else if (item.icon) {
|
|
338
|
+
// LABEL: An SVG icon (the color of the text)
|
|
339
|
+
row.icon = Label({ width: _s.icon.width, height: _s.icon.height, shrink: 0 });
|
|
340
|
+
row.icon.text = item.icon;
|
|
341
|
+
row.icon.elem.classList.add("context-menu-svg");
|
|
342
|
+
row.icon.isSvg = 1;
|
|
343
|
+
} else if (item.iconFile) {
|
|
344
|
+
row.icon = Icon({
|
|
345
|
+
width: _s.icon.width,
|
|
346
|
+
height: _s.icon.height,
|
|
347
|
+
shrink: 0,
|
|
348
|
+
});
|
|
349
|
+
row.icon.load(item.iconFile);
|
|
350
|
+
row.icon.elem.alt = "";
|
|
351
|
+
} else {
|
|
352
|
+
row.icon = Box({
|
|
353
|
+
width: _s.icon.width,
|
|
354
|
+
height: _s.icon.height,
|
|
355
|
+
color: "transparent",
|
|
356
|
+
shrink: 0,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// LABEL: Text
|
|
362
|
+
// WHY: plainText: Item texts must not be read as HTML. (Label.text uses innerHTML.)
|
|
363
|
+
row.label = Label({
|
|
364
|
+
plainText: item.text,
|
|
365
|
+
fontSize: _s.item.fontSize,
|
|
366
|
+
ellipsis: 1,
|
|
367
|
+
shrink: 1,
|
|
368
|
+
});
|
|
369
|
+
row.label.css.minWidth = "0";
|
|
370
|
+
row.label.grow = 1; // WHY: the › of a submenu goes to the end of the row
|
|
371
|
+
|
|
372
|
+
// LABEL: Its shortcut
|
|
373
|
+
if (item.shortcut) {
|
|
374
|
+
row.lblShortcut = Label({
|
|
375
|
+
plainText: item.shortcut,
|
|
376
|
+
fontSize: _s.shortcut.fontSize,
|
|
377
|
+
opacity: _s.shortcut.opacity,
|
|
378
|
+
shrink: 0,
|
|
379
|
+
css: { marginLeft: (_s.shortcut.space - _s.item.gap) + "px", whiteSpace: "nowrap" },
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// LABEL: › (the item opens a submenu)
|
|
384
|
+
if (isSubmenuItem(item)) {
|
|
385
|
+
row.arrow = Label({ width: ARROW_WIDTH, height: _s.icon.height, shrink: 0, opacity: _s.submenu.arrowOpacity });
|
|
386
|
+
row.arrow.text = ARROW_ICON;
|
|
387
|
+
row.arrow.elem.classList.add("context-menu-svg");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
endGroup();
|
|
391
|
+
|
|
392
|
+
updateRowEnabled(row);
|
|
393
|
+
|
|
394
|
+
row.on("mouseenter", function () {
|
|
395
|
+
setActive((item.enabled == 1) ? index : -1);
|
|
396
|
+
if (item.enabled == 1 && isSubmenuItem(item)) showSubmenu(index, 0);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
row.on("click", function () {
|
|
400
|
+
if (isSubmenuItem(item)) {
|
|
401
|
+
if (item.enabled == 1) showSubmenu(index, 0);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
box.clickItem(index);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
return row;
|
|
408
|
+
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
const render = function () {
|
|
412
|
+
|
|
413
|
+
removeSubmenus();
|
|
414
|
+
rows.forEach(function (obj) { obj.remove(); });
|
|
415
|
+
rows = [];
|
|
416
|
+
activeIndex = -1;
|
|
417
|
+
|
|
418
|
+
const hasIcon = box.items.some(function (item) { return item.type == "item" && (item.iconFile || item.icon || isCheckItem(item)); });
|
|
419
|
+
|
|
420
|
+
box.items.forEach(function (item, index) {
|
|
421
|
+
// WHY: One createIn per row: endGroup() (in createItem) sets the container to the last started group, not to box.content.
|
|
422
|
+
createIn(box.content, function () {
|
|
423
|
+
rows.push((item.type == "separator") ? createSeparator() : createItem(item, index, hasIcon));
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
// Put the menu at the point. If there is no space, open it to the left and/or up.
|
|
430
|
+
const positionMenu = function (x, y) {
|
|
431
|
+
|
|
432
|
+
const menuWidth = box.elem.offsetWidth;
|
|
433
|
+
const menuHeight = box.elem.offsetHeight;
|
|
434
|
+
|
|
435
|
+
let left = x;
|
|
436
|
+
let top = y;
|
|
437
|
+
let originX = "left";
|
|
438
|
+
let originY = "top";
|
|
439
|
+
|
|
440
|
+
if (left + menuWidth > page.width - PAGE_SIDE_SPACE) {
|
|
441
|
+
left = x - menuWidth;
|
|
442
|
+
originX = "right";
|
|
443
|
+
}
|
|
444
|
+
if (top + menuHeight > page.height - PAGE_SIDE_SPACE) {
|
|
445
|
+
top = y - menuHeight;
|
|
446
|
+
originY = "bottom";
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
left = Math.max(PAGE_SIDE_SPACE, Math.min(left, page.width - menuWidth - PAGE_SIDE_SPACE));
|
|
450
|
+
top = Math.max(PAGE_SIDE_SPACE, Math.min(top, page.height - menuHeight - PAGE_SIDE_SPACE));
|
|
451
|
+
|
|
452
|
+
box.left = left;
|
|
453
|
+
box.top = top;
|
|
454
|
+
box.css.transformOrigin = originX + " " + originY;
|
|
455
|
+
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// *** SUBMENUS: one ContextMenu for each item with items (made the first time it opens, on the page).
|
|
459
|
+
// Its keys and the clicks outside come through this menu (parentMenu), so only one menu reads a key.
|
|
460
|
+
|
|
461
|
+
const removeSubmenus = function () {
|
|
462
|
+
openSubmenuIndex = -1;
|
|
463
|
+
rows.forEach(function (row) {
|
|
464
|
+
if (row.submenu) row.submenu.remove();
|
|
465
|
+
row.submenu = null;
|
|
466
|
+
});
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
// withKeyboard: its first item is active (the keys go to it)
|
|
470
|
+
const showSubmenu = function (index, withKeyboard) {
|
|
471
|
+
const row = rows[index];
|
|
472
|
+
const item = box.items[index];
|
|
473
|
+
if (!row || !isSubmenuItem(item)) return;
|
|
474
|
+
if (openSubmenuIndex != index || !openSubmenu()) {
|
|
475
|
+
box.closeSubmenu();
|
|
476
|
+
if (!row.submenu) {
|
|
477
|
+
row.submenu = ContextMenu({
|
|
478
|
+
items: item.items,
|
|
479
|
+
styleName: box.styleName,
|
|
480
|
+
style: _s,
|
|
481
|
+
minWidth: box.minWidth,
|
|
482
|
+
maxWidth: box.maxWidth,
|
|
483
|
+
ariaLabel: item.text,
|
|
484
|
+
parentMenu: box,
|
|
485
|
+
// The click of an item of the submenu goes to the onClick of this menu (and so up to the first one).
|
|
486
|
+
onClick: function (self, subItem, subIndex) { box.onClick(self, subItem, subIndex); },
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
openSubmenuIndex = index;
|
|
490
|
+
setRowHover(row, 1);
|
|
491
|
+
row.submenu.openBeside(row.elem.getBoundingClientRect());
|
|
492
|
+
}
|
|
493
|
+
if (withKeyboard) row.submenu.activateFirst();
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// At the right of the item (at its left when there is no room), its first item at the height of the item.
|
|
497
|
+
const positionBeside = function (rect) {
|
|
498
|
+
|
|
499
|
+
const menuWidth = box.elem.offsetWidth;
|
|
500
|
+
const menuHeight = box.elem.offsetHeight;
|
|
501
|
+
const overlap = _s.submenu.overlap;
|
|
502
|
+
const firstItemSpace = _s.menu.padding + _s.menu.border;
|
|
503
|
+
|
|
504
|
+
let left = withPageZoom(rect.right) - overlap;
|
|
505
|
+
let originX = "left";
|
|
506
|
+
if (left + menuWidth > page.width - PAGE_SIDE_SPACE) {
|
|
507
|
+
left = withPageZoom(rect.left) - menuWidth + overlap;
|
|
508
|
+
originX = "right";
|
|
509
|
+
}
|
|
510
|
+
const top = withPageZoom(rect.top) - firstItemSpace;
|
|
511
|
+
|
|
512
|
+
box.left = Math.max(PAGE_SIDE_SPACE, Math.min(left, page.width - menuWidth - PAGE_SIDE_SPACE));
|
|
513
|
+
box.top = Math.max(PAGE_SIDE_SPACE, Math.min(top, page.height - menuHeight - PAGE_SIDE_SPACE));
|
|
514
|
+
box.css.transformOrigin = originX + " top";
|
|
515
|
+
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// Under the object at its left edge; over it when there is no room under it and there is room over it.
|
|
519
|
+
const positionBelow = function (rect) {
|
|
520
|
+
|
|
521
|
+
const menuWidth = box.elem.offsetWidth;
|
|
522
|
+
const menuHeight = box.elem.offsetHeight;
|
|
523
|
+
const left = withPageZoom(rect.left);
|
|
524
|
+
const bottom = withPageZoom(rect.bottom);
|
|
525
|
+
const top = withPageZoom(rect.top);
|
|
526
|
+
const hasRoomBelow = (bottom + menuHeight <= page.height - PAGE_SIDE_SPACE);
|
|
527
|
+
const hasRoomAbove = (top - menuHeight >= PAGE_SIDE_SPACE);
|
|
528
|
+
const opensAbove = (!hasRoomBelow && hasRoomAbove);
|
|
529
|
+
|
|
530
|
+
box.left = Math.max(PAGE_SIDE_SPACE, Math.min(left, page.width - menuWidth - PAGE_SIDE_SPACE));
|
|
531
|
+
box.top = (opensAbove) ? (top - menuHeight) : Math.max(PAGE_SIDE_SPACE, Math.min(bottom, page.height - menuHeight - PAGE_SIDE_SPACE));
|
|
532
|
+
box.css.transformOrigin = "left " + ((opensAbove) ? "bottom" : "top");
|
|
533
|
+
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
const onDocumentPointerDown = function (event) {
|
|
537
|
+
|
|
538
|
+
if (isInsideMenu(event.target)) return;
|
|
539
|
+
|
|
540
|
+
// Left click on the opener button: close only. (Do not open again with the click event.)
|
|
541
|
+
if (event.button === 0 && box.source && box.source.elem.contains(event.target)) {
|
|
542
|
+
const info = attachedList.find(function (a) { return a.obj == box.source; });
|
|
543
|
+
if (info && info.trigger == "click") {
|
|
544
|
+
skipOpenFor = box.source;
|
|
545
|
+
skipOpenTime = Date.now();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
box.close();
|
|
550
|
+
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
// The keys of the first menu (a document listener); a submenu gets them through handleKey().
|
|
554
|
+
const onDocumentKeyDown = function (event) {
|
|
555
|
+
|
|
556
|
+
// WHY: A submenu opened with the keys (or with an active item) reads the keys, not this menu.
|
|
557
|
+
const submenu = openSubmenu();
|
|
558
|
+
if (submenu && submenu.hasActive()) {
|
|
559
|
+
submenu.handleKey(event);
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const activeItem = box.items[activeIndex];
|
|
564
|
+
const isOpenKey = (event.key === "ArrowRight" || event.key === "Enter" || event.key === " ");
|
|
565
|
+
|
|
566
|
+
if (event.key === "Escape" || (event.key === "ArrowLeft" && box.parentMenu)) {
|
|
567
|
+
event.preventDefault();
|
|
568
|
+
// WHY: this Escape closes the menu only (a page listener after it would also clear its selection)
|
|
569
|
+
if (event.key === "Escape") event.stopPropagation();
|
|
570
|
+
// A submenu closes only itself; its item in the menu before stays active.
|
|
571
|
+
if (box.parentMenu) box.parentMenu.closeSubmenu();
|
|
572
|
+
else box.close();
|
|
573
|
+
} else if (isOpenKey && isSubmenuItem(activeItem) && activeItem.enabled == 1) {
|
|
574
|
+
event.preventDefault();
|
|
575
|
+
showSubmenu(activeIndex, 1);
|
|
576
|
+
} else if (event.key === "ArrowDown") {
|
|
577
|
+
event.preventDefault();
|
|
578
|
+
moveActive(1);
|
|
579
|
+
} else if (event.key === "ArrowUp") {
|
|
580
|
+
event.preventDefault();
|
|
581
|
+
moveActive(-1);
|
|
582
|
+
} else if (event.key === "Enter" || event.key === " ") {
|
|
583
|
+
if (activeIndex < 0) return;
|
|
584
|
+
event.preventDefault();
|
|
585
|
+
box.clickItem(activeIndex);
|
|
586
|
+
} else if (event.key === "Tab") {
|
|
587
|
+
rootMenu().close();
|
|
588
|
+
} else if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
|
|
589
|
+
// WHY: the side keys that no menu used go to the first menu (MenuBar: the next menu of the bar).
|
|
590
|
+
event.preventDefault();
|
|
591
|
+
const firstMenu = rootMenu();
|
|
592
|
+
firstMenu.onSideKey(firstMenu, (event.key === "ArrowRight") ? 1 : -1);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
const onDocumentWheel = function (event) {
|
|
598
|
+
if (!isInsideMenu(event.target)) box.close();
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
const onWindowBlur = function () {
|
|
602
|
+
box.close();
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
const onPageResize = function () {
|
|
606
|
+
// WHY: Close only if the size really changed. (Some browsers call resize without a change.)
|
|
607
|
+
if (isOpen && page.width + "x" + page.height != openPageSize) box.close();
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
// Events only needed while the menu is open.
|
|
611
|
+
const setOpenEvents = function (isOn) {
|
|
612
|
+
const method = (isOn) ? "addEventListener" : "removeEventListener";
|
|
613
|
+
// WHY: Capture phase: Some objects stop the event. The menu must still close.
|
|
614
|
+
document[method]("pointerdown", onDocumentPointerDown, true);
|
|
615
|
+
document[method]("keydown", onDocumentKeyDown, true);
|
|
616
|
+
document[method]("wheel", onDocumentWheel, true);
|
|
617
|
+
window[method]("blur", onWindowBlur);
|
|
618
|
+
if (isOn) page.onResize(onPageResize);
|
|
619
|
+
else page.remove_onResize(onPageResize);
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
// *** PUBLIC FUNCTIONS:
|
|
623
|
+
|
|
624
|
+
box.setItems = function (items) {
|
|
625
|
+
box.items = (items || []).map(normalizeItem);
|
|
626
|
+
render();
|
|
627
|
+
if (isOpen) box.close(); // WHY: The size changes. Open it again at the new point.
|
|
628
|
+
};
|
|
629
|
+
// USAGE: menu.setItems(["Open", "-", { text: "Delete", key: "delete" }])
|
|
630
|
+
|
|
631
|
+
box.getItems = function () {
|
|
632
|
+
return box.items.slice();
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
box.getItemByKey = function (key) {
|
|
636
|
+
return box.items.find(function (item) { return item.key == key; }) || null;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
box.setItemEnabled = function (key, enabled) {
|
|
640
|
+
const item = box.getItemByKey(key);
|
|
641
|
+
if (!item) return;
|
|
642
|
+
item.enabled = toFlag(enabled);
|
|
643
|
+
// WHY: Update only this row, not render() all of them.
|
|
644
|
+
const index = box.items.indexOf(item);
|
|
645
|
+
if (item.enabled != 1 && activeIndex == index) setActive(-1);
|
|
646
|
+
updateRowEnabled(rows[index]);
|
|
647
|
+
if (activeIndex == index) setRowHover(rows[index], 1);
|
|
648
|
+
};
|
|
649
|
+
// USAGE: menu.setItemEnabled("paste", 0)
|
|
650
|
+
|
|
651
|
+
box.setItemChecked = function (key, checked) {
|
|
652
|
+
const item = box.getItemByKey(key);
|
|
653
|
+
if (!item || !isCheckItem(item)) return;
|
|
654
|
+
item.checked = toFlag(checked);
|
|
655
|
+
showCheckMark(rows[box.items.indexOf(item)]);
|
|
656
|
+
};
|
|
657
|
+
// USAGE: menu.setItemChecked("wrap", 1)
|
|
658
|
+
|
|
659
|
+
// Shows the menu: place() puts it. (A submenu has no events of its own: its first menu has them.)
|
|
660
|
+
const showMenu = function (place) {
|
|
661
|
+
|
|
662
|
+
isOpen = 1;
|
|
663
|
+
openPageSize = page.width + "x" + page.height;
|
|
664
|
+
|
|
665
|
+
// WHY: a menu of a row in a Dialog or a SidePanel opens over them (under them it is invisible but holds
|
|
666
|
+
// the keys); a submenu over its menu
|
|
667
|
+
box.zIndex = basic.zIndexAbove(box.parentMenu || box.source, _ms.zIndex);
|
|
668
|
+
|
|
669
|
+
// Show hidden (opacity 0) first, so the size can be measured.
|
|
670
|
+
box.setMotion("none");
|
|
671
|
+
box.opacity = 0;
|
|
672
|
+
box.css.transform = "scale(0.97)";
|
|
673
|
+
box.visible = 1;
|
|
674
|
+
|
|
675
|
+
place();
|
|
676
|
+
|
|
677
|
+
// Small open animation
|
|
678
|
+
requestAnimationFrame(function () {
|
|
679
|
+
if (!box || !isOpen) return;
|
|
680
|
+
box.setMotion(OPEN_MOTION);
|
|
681
|
+
box.opacity = 1;
|
|
682
|
+
box.css.transform = "scale(1)";
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
if (!box.parentMenu) {
|
|
686
|
+
box.elem.focus({ preventScroll: true });
|
|
687
|
+
setOpenEvents(1);
|
|
688
|
+
}
|
|
689
|
+
box.onOpen(box);
|
|
690
|
+
|
|
691
|
+
};
|
|
692
|
+
|
|
693
|
+
// x, y: Mouse point on the screen. (event.clientX, event.clientY)
|
|
694
|
+
box.openAt = function (x, y, source = null) {
|
|
695
|
+
|
|
696
|
+
if (box.enabled != 1 || box.items.length == 0) return;
|
|
697
|
+
if (isOpen) box.close();
|
|
698
|
+
|
|
699
|
+
box.source = source;
|
|
700
|
+
showMenu(function () { positionMenu(withPageZoom(x), withPageZoom(y)); });
|
|
701
|
+
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
// Open at the mouse point of an event.
|
|
705
|
+
box.openWithEvent = function (event, source = null) {
|
|
706
|
+
box.openAt(event.clientX, event.clientY, source);
|
|
707
|
+
};
|
|
708
|
+
// USAGE: myBox.on("contextmenu", function (self, event) { event.preventDefault(); menu.openWithEvent(event, self); });
|
|
709
|
+
|
|
710
|
+
// Open under the object. (It is the source of the menu.)
|
|
711
|
+
box.openBelow = function (obj) {
|
|
712
|
+
|
|
713
|
+
if (box.enabled != 1 || box.items.length == 0) return;
|
|
714
|
+
if (isOpen) box.close();
|
|
715
|
+
|
|
716
|
+
box.source = obj;
|
|
717
|
+
showMenu(function () { positionBelow(obj.elem.getBoundingClientRect()); });
|
|
718
|
+
|
|
719
|
+
};
|
|
720
|
+
// USAGE: menu.openBelow(myButton)
|
|
721
|
+
|
|
722
|
+
box.close = function () {
|
|
723
|
+
|
|
724
|
+
if (!isOpen) return;
|
|
725
|
+
isOpen = 0;
|
|
726
|
+
|
|
727
|
+
box.closeSubmenu();
|
|
728
|
+
if (!box.parentMenu) setOpenEvents(0);
|
|
729
|
+
setActive(-1);
|
|
730
|
+
box.setMotion("none");
|
|
731
|
+
box.visible = 0;
|
|
732
|
+
|
|
733
|
+
box.onClose(box);
|
|
734
|
+
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
box.isOpen = function () {
|
|
738
|
+
return isOpen;
|
|
739
|
+
};
|
|
740
|
+
|
|
741
|
+
// Same as a user click on the item.
|
|
742
|
+
box.clickItem = function (index) {
|
|
743
|
+
|
|
744
|
+
if (!isClickableIndex(index)) return;
|
|
745
|
+
|
|
746
|
+
const item = box.items[index];
|
|
747
|
+
|
|
748
|
+
// An item with a submenu is not clicked: it opens its submenu.
|
|
749
|
+
if (isSubmenuItem(item)) {
|
|
750
|
+
showSubmenu(index, 1);
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// WHY: Close first (every menu, with the menus before a submenu). onClick can open a dialog or another menu.
|
|
755
|
+
rootMenu().close();
|
|
756
|
+
|
|
757
|
+
if (typeof item.onClick === "function") item.onClick(box, item, index);
|
|
758
|
+
box.onClick(box, item, index);
|
|
759
|
+
|
|
760
|
+
};
|
|
761
|
+
|
|
762
|
+
// Opens the menu on the object with the trigger. Returns a function to remove it.
|
|
763
|
+
box.attachTo = function (obj, trigger) {
|
|
764
|
+
|
|
765
|
+
trigger = trigger || box.trigger;
|
|
766
|
+
|
|
767
|
+
const removeEvent = obj.on(trigger, function (self, event) {
|
|
768
|
+
|
|
769
|
+
event.preventDefault(); // WHY: Do not show the browser menu.
|
|
770
|
+
|
|
771
|
+
// WHY: Time limit: If the mouse was released outside the button, there was no click to skip.
|
|
772
|
+
const skip = (skipOpenFor == obj && Date.now() - skipOpenTime < OPENER_CLICK_TIME_LIMIT);
|
|
773
|
+
skipOpenFor = null;
|
|
774
|
+
if (skip) return;
|
|
775
|
+
|
|
776
|
+
if (box.enabled != 1) return;
|
|
777
|
+
|
|
778
|
+
box.openWithEvent(event, obj);
|
|
779
|
+
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
attachedList.push({ obj: obj, trigger: trigger, removeEvent: removeEvent });
|
|
783
|
+
|
|
784
|
+
return function () {
|
|
785
|
+
box.detach(obj);
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
};
|
|
789
|
+
// USAGE: menu.attachTo(fileRow) or menu.attachTo(moreButton, "click")
|
|
790
|
+
|
|
791
|
+
box.detach = function (obj) {
|
|
792
|
+
attachedList = attachedList.filter(function (info) {
|
|
793
|
+
if (info.obj != obj) return true;
|
|
794
|
+
if (typeof info.removeEvent === "function") info.removeEvent();
|
|
795
|
+
return false;
|
|
796
|
+
});
|
|
797
|
+
};
|
|
798
|
+
|
|
799
|
+
box.setEnabled = function (enabled) {
|
|
800
|
+
box.enabled = toFlag(enabled);
|
|
801
|
+
if (box.enabled != 1) box.close();
|
|
802
|
+
};
|
|
803
|
+
// USAGE: get: menu.enabled, set: menu.setEnabled(0)
|
|
804
|
+
|
|
805
|
+
box.refresh = render;
|
|
806
|
+
|
|
807
|
+
// (inside: used by the submenus)
|
|
808
|
+
|
|
809
|
+
box.closeSubmenu = function () {
|
|
810
|
+
const submenu = openSubmenu();
|
|
811
|
+
const row = rows[openSubmenuIndex];
|
|
812
|
+
const index = openSubmenuIndex;
|
|
813
|
+
openSubmenuIndex = -1;
|
|
814
|
+
if (submenu) submenu.close();
|
|
815
|
+
if (row && row.label && activeIndex != index) setRowHover(row, 0);
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
// A submenu opens at the side of the item of its menu before. (rect: the item on the screen)
|
|
819
|
+
box.openBeside = function (rect) {
|
|
820
|
+
if (box.items.length == 0) return;
|
|
821
|
+
if (isOpen) box.close();
|
|
822
|
+
box.source = (box.parentMenu) ? box.parentMenu.source : null; // WHY: onClick reads self.source
|
|
823
|
+
showMenu(function () { positionBeside(rect); });
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
box.containsElement = isInsideMenu;
|
|
827
|
+
box.handleKey = onDocumentKeyDown;
|
|
828
|
+
box.hasActive = function () { return (activeIndex >= 0) ? 1 : 0; };
|
|
829
|
+
box.activateFirst = function () {
|
|
830
|
+
setActive(-1);
|
|
831
|
+
moveActive(1);
|
|
832
|
+
};
|
|
833
|
+
|
|
834
|
+
// WHY: box.superRemove is overwritten by a component that extends this one, so the local copy is called below.
|
|
835
|
+
const superRemove = box.remove;
|
|
836
|
+
box.superRemove = superRemove;
|
|
837
|
+
box.remove = function () {
|
|
838
|
+
|
|
839
|
+
if (!box) return; // WHY: remove() can be called twice (also by the parent's remove()).
|
|
840
|
+
box.close();
|
|
841
|
+
attachedList.slice().forEach(function (info) { box.detach(info.obj); });
|
|
842
|
+
removeSubmenus(); // WHY: they are on the page, not in this box
|
|
843
|
+
|
|
844
|
+
superRemove.call(box); // NOTE: basic.js remove(). It cleans all the events and the objects inside.
|
|
845
|
+
box = null;
|
|
846
|
+
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
// *** OBJECT VIEW:
|
|
850
|
+
|
|
851
|
+
// An SVG icon fills its place.
|
|
852
|
+
basic.addCss("context-menu-svg", ".context-menu-svg { display: flex; align-items: center; justify-content: center; } .context-menu-svg svg { display: block; width: 100%; height: 100%; }");
|
|
853
|
+
|
|
854
|
+
box.css = {
|
|
855
|
+
position: "fixed",
|
|
856
|
+
boxSizing: "border-box",
|
|
857
|
+
minWidth: box.minWidth + "px",
|
|
858
|
+
outline: "none",
|
|
859
|
+
userSelect: "none",
|
|
860
|
+
};
|
|
861
|
+
if (box.maxWidth > 0) box.css.maxWidth = box.maxWidth + "px";
|
|
862
|
+
box.tabIndex = -1;
|
|
863
|
+
box.role = "menu";
|
|
864
|
+
box.aria = { label: box.ariaLabel };
|
|
865
|
+
|
|
866
|
+
// GROUP: Rows (created by render)
|
|
867
|
+
box.content = VGroup({
|
|
868
|
+
width: "auto",
|
|
869
|
+
height: "auto",
|
|
870
|
+
align: "left top",
|
|
871
|
+
gap: 0,
|
|
872
|
+
position: "relative", // WHY: Component size is "auto". A relative group makes the container wrap it.
|
|
873
|
+
});
|
|
874
|
+
// WHY: Rows fill the menu width, so the hover color is as wide as the menu.
|
|
875
|
+
box.content.css.alignItems = "stretch";
|
|
876
|
+
|
|
877
|
+
endGroup();
|
|
878
|
+
|
|
879
|
+
// *** OBJECT INIT CODE:
|
|
880
|
+
|
|
881
|
+
// WHY: Keep the focus in the menu when an item is pressed.
|
|
882
|
+
box.on("mousedown", function (self, event) { event.preventDefault(); });
|
|
883
|
+
box.on("contextmenu", function (self, event) { event.preventDefault(); });
|
|
884
|
+
box.on("mouseleave", function () { setActive(-1); });
|
|
885
|
+
|
|
886
|
+
box.setEnabled(box.enabled);
|
|
887
|
+
box.setItems(params.items); // WHY: Copy the array. The default [] must not be shared between components.
|
|
888
|
+
box.visible = 0;
|
|
889
|
+
|
|
890
|
+
if (box.target) {
|
|
891
|
+
const targets = Array.isArray(box.target) ? box.target : [box.target];
|
|
892
|
+
targets.forEach(function (obj) { box.attachTo(obj, box.trigger); });
|
|
893
|
+
// WHY: the menu is on the page, not in the group of its target, so it stayed when the target was removed (a
|
|
894
|
+
// page that draws its window again left one at every drawing). A list keeps it: one of its rows can go.
|
|
895
|
+
if (!Array.isArray(box.target)) box.target.own(box);
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
endObject(box);
|
|
899
|
+
|
|
900
|
+
setDefaultContainerBox(_previousContainer);
|
|
901
|
+
|
|
902
|
+
return box;
|
|
903
|
+
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
// *** STATIC FUNCTIONS:
|
|
907
|
+
|
|
908
|
+
// For user data in your own Label.text. (Kept for old code: same as basic.escapeHtml.)
|
|
909
|
+
ContextMenu.escapeHtml = basic.escapeHtml;
|
|
910
|
+
|
|
911
|
+
// *** STYLE PACKAGES:
|
|
912
|
+
// USAGE: ContextMenu({ styleName: "dark" })
|
|
913
|
+
// USAGE: ContextMenu({ styleName: "dark", style: { menu: { round: 4 } } }) // Change only some keys.
|
|
914
|
+
// USAGE: ContextMenu({ style: ContextMenu.styles.dark }) // The same as styleName: "dark". (The package is copied, never changed.)
|
|
915
|
+
// NOTE: A new package needs only the keys that differ from the default (classic) style.
|
|
916
|
+
// NOTE: An iconFile is an image: give light icons to the items of a dark menu. An SVG icon (icon) takes the text color.
|
|
917
|
+
// USAGE: ContextMenu.styles.myStyle = { menu: { round: 4 } };
|
|
918
|
+
ContextMenu.styles = {
|
|
919
|
+
|
|
920
|
+
// White, dark text, a soft line.
|
|
921
|
+
light: ContextMenuDefaults.style,
|
|
922
|
+
|
|
923
|
+
// Dark theme, same colors as SelectBox "dark".
|
|
924
|
+
dark: {
|
|
925
|
+
menu: { color: "#232322", borderColor: "rgba(255, 255, 255, 0.12)", shadow: "0px 8px 24px rgba(0, 0, 0, 0.5)" },
|
|
926
|
+
item: { textColor: "rgba(255, 255, 255, 0.85)" },
|
|
927
|
+
itemHover: { textColor: "rgba(255, 255, 255, 0.95)", color: "rgba(255, 255, 255, 0.08)" },
|
|
928
|
+
disabled: { textColor: "rgba(255, 255, 255, 0.35)" },
|
|
929
|
+
separator: { color: "rgba(255, 255, 255, 0.10)" },
|
|
930
|
+
},
|
|
931
|
+
|
|
932
|
+
};
|
|
933
|
+
ContextMenu.styles.classic = ContextMenu.styles.light; // the old name of light: the pages made with it keep their look
|
|
934
|
+
|
|
935
|
+
// The minimal theme (_designs/minimal-theme.htm): on the ground of the window with a line and no shadow, 28 px items
|
|
936
|
+
// with a 13 px text, a line around the item under the mouse. minimalDark: the same on a dark window.
|
|
937
|
+
ContextMenu.createMinimalStyle = function (colors) {
|
|
938
|
+
return {
|
|
939
|
+
menu: { color: colors.ground, border: 1, borderColor: colors.line, round: 4, padding: 4, shadow: "none" },
|
|
940
|
+
item: { height: 28, fontSize: 13, textColor: colors.text, color: "transparent", round: 3, padding: 8, gap: 8, border: 1, borderColor: "transparent" },
|
|
941
|
+
itemHover: { textColor: colors.text, color: "transparent", borderColor: colors.lineHover },
|
|
942
|
+
disabled: { textColor: colors.faintText, opacity: 0.4 },
|
|
943
|
+
icon: { width: 14, height: 14, opacity: 0.85 },
|
|
944
|
+
shortcut: { fontSize: 12, opacity: 0.5, space: 20 },
|
|
945
|
+
separator: { color: colors.line, space: 4 },
|
|
946
|
+
};
|
|
947
|
+
};
|
|
948
|
+
ContextMenu.MINIMAL_COLORS = { ground: "#FFFFFF", text: Black(0.85), faintText: Black(0.54), line: Black(0.2), lineHover: Black(0.45) };
|
|
949
|
+
ContextMenu.styles.minimal = ContextMenu.createMinimalStyle(ContextMenu.MINIMAL_COLORS);
|
|
950
|
+
ContextMenu.MINIMAL_DARK_COLORS = { ground: "#1A1A19", text: White(0.88), faintText: White(0.48), line: White(0.18), lineHover: White(0.42) };
|
|
951
|
+
ContextMenu.styles.minimalDark = ContextMenu.createMinimalStyle(ContextMenu.MINIMAL_DARK_COLORS);
|