@hprint/core 0.0.1-alpha.3 → 0.0.1

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.
@@ -1,277 +1,277 @@
1
- import './styles/contextMenu.css';
2
-
3
- class ContextMenu {
4
- constructor(container, items) {
5
- this.container = container;
6
- this.dom = null;
7
- this.shown = false;
8
- this.root = true;
9
- this.parent = null;
10
- this.submenus = [];
11
- this.items = items;
12
-
13
- this._onclick = (e) => {
14
- if (
15
- this.dom &&
16
- e.target != this.dom &&
17
- e.target.parentElement != this.dom &&
18
- !e.target.classList.contains('item') &&
19
- !e.target.parentElement.classList.contains('item')
20
- ) {
21
- this.hideAll();
22
- }
23
- };
24
-
25
- this._oncontextmenu = (e) => {
26
- e.preventDefault();
27
- if (
28
- e.target != this.dom &&
29
- e.target.parentElement != this.dom &&
30
- !e.target.classList.contains('item') &&
31
- !e.target.parentElement.classList.contains('item')
32
- ) {
33
- this.hideAll();
34
- this.show(e.clientX, e.clientY);
35
- }
36
- };
37
-
38
- this._oncontextmenu_keydown = (e) => {
39
- if (e.keyCode != 93) return;
40
- e.preventDefault();
41
-
42
- this.hideAll();
43
- this.show(e.clientX, e.clientY);
44
- };
45
-
46
- this._onblur = () => {
47
- this.hideAll();
48
- };
49
- }
50
-
51
- getMenuDom() {
52
- const menu = document.createElement('div');
53
- menu.classList.add('context');
54
-
55
- for (const item of this.items) {
56
- menu.appendChild(this.itemToDomEl(item));
57
- }
58
-
59
- return menu;
60
- }
61
-
62
- itemToDomEl(data) {
63
- const item = document.createElement('div');
64
-
65
- if (data === null) {
66
- item.classList = 'separator';
67
- return item;
68
- }
69
-
70
- if (
71
- data.hasOwnProperty('color') &&
72
- /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(
73
- data.color.toString()
74
- )
75
- ) {
76
- item.style.cssText = `color: ${data.color}`;
77
- }
78
-
79
- item.classList.add('item');
80
-
81
- const label = document.createElement('span');
82
- label.classList = 'label';
83
- label.innerText = data.hasOwnProperty('text')
84
- ? data['text'].toString()
85
- : '';
86
- item.appendChild(label);
87
-
88
- if (data.hasOwnProperty('disabled') && data['disabled']) {
89
- item.classList.add('disabled');
90
- } else {
91
- item.classList.add('enabled');
92
- }
93
-
94
- const hotkey = document.createElement('span');
95
- hotkey.classList = 'hotkey';
96
- hotkey.innerText = data.hasOwnProperty('hotkey')
97
- ? data['hotkey'].toString()
98
- : '';
99
- item.appendChild(hotkey);
100
-
101
- if (
102
- data.hasOwnProperty('subitems') &&
103
- Array.isArray(data['subitems']) &&
104
- data['subitems'].length > 0
105
- ) {
106
- const menu = new ContextMenu(this.container, data['subitems']);
107
- menu.root = false;
108
- menu.parent = this;
109
-
110
- const openSubItems = () => {
111
- if (data.hasOwnProperty('disabled') && data['disabled'] == true)
112
- return;
113
-
114
- this.hideSubMenus();
115
-
116
- const x =
117
- this.dom.offsetLeft +
118
- this.dom.clientWidth +
119
- item.offsetLeft;
120
- const y = this.dom.offsetTop + item.offsetTop;
121
-
122
- if (!menu.shown) {
123
- menu.show(x, y);
124
- } else {
125
- menu.hide();
126
- }
127
- };
128
-
129
- this.submenus.push(menu);
130
-
131
- item.classList.add('has-subitems');
132
- item.addEventListener('click', openSubItems);
133
- item.addEventListener('mousemove', openSubItems);
134
- } else if (
135
- data.hasOwnProperty('submenu') &&
136
- data['submenu'] instanceof ContextMenu
137
- ) {
138
- const menu = data['submenu'];
139
- menu.root = false;
140
- menu.parent = this;
141
-
142
- const openSubItems = () => {
143
- if (data.hasOwnProperty('disabled') && data['disabled'] == true)
144
- return;
145
-
146
- this.hideSubMenus();
147
-
148
- const x =
149
- this.dom.offsetLeft +
150
- this.dom.clientWidth +
151
- item.offsetLeft;
152
- const y = this.dom.offsetTop + item.offsetTop;
153
-
154
- if (!menu.shown) {
155
- menu.show(x, y);
156
- } else {
157
- menu.hide();
158
- }
159
- };
160
-
161
- this.submenus.push(menu);
162
-
163
- item.classList.add('has-subitems');
164
- item.addEventListener('click', openSubItems);
165
- item.addEventListener('mousemove', openSubItems);
166
- } else {
167
- item.addEventListener('click', () => {
168
- this.hideSubMenus();
169
-
170
- if (item.classList.contains('disabled')) return;
171
-
172
- if (
173
- data.hasOwnProperty('onclick') &&
174
- typeof data['onclick'] === 'function'
175
- ) {
176
- const event = {
177
- handled: false,
178
- item: item,
179
- label: label,
180
- hotkey: hotkey,
181
- items: this.items,
182
- data: data,
183
- };
184
-
185
- data['onclick'](event);
186
-
187
- if (!event.handled) {
188
- this.hide();
189
- }
190
- } else {
191
- this.hide();
192
- }
193
- });
194
-
195
- item.addEventListener('mousemove', () => {
196
- this.hideSubMenus();
197
- });
198
- }
199
-
200
- return item;
201
- }
202
-
203
- hideAll() {
204
- if (this.root && !this.parent) {
205
- if (this.shown) {
206
- this.hideSubMenus();
207
-
208
- this.shown = false;
209
- this.container.removeChild(this.dom);
210
-
211
- if (this.parent && this.parent.shown) {
212
- this.parent.hide();
213
- }
214
- }
215
-
216
- return;
217
- }
218
-
219
- this.parent.hide();
220
- }
221
-
222
- hide() {
223
- if (this.dom && this.shown) {
224
- this.shown = false;
225
- this.hideSubMenus();
226
- this.container.removeChild(this.dom);
227
-
228
- if (this.parent && this.parent.shown) {
229
- this.parent.hide();
230
- }
231
- }
232
- }
233
-
234
- hideSubMenus() {
235
- for (const menu of this.submenus) {
236
- if (menu.shown) {
237
- menu.shown = false;
238
- menu.container.removeChild(menu.dom);
239
- }
240
- menu.hideSubMenus();
241
- }
242
- }
243
-
244
- show(x, y) {
245
- this.dom = this.getMenuDom();
246
-
247
- this.dom.style.left = `${x}px`;
248
- this.dom.style.top = `${y}px`;
249
-
250
- this.shown = true;
251
- this.container.appendChild(this.dom);
252
- }
253
-
254
- install() {
255
- this.container.addEventListener('contextmenu', this._oncontextmenu);
256
- this.container.addEventListener('keydown', this._oncontextmenu_keydown);
257
- this.container.addEventListener('click', this._onclick);
258
- window.addEventListener('blur', this._onblur);
259
- }
260
-
261
- setData(data) {
262
- this.items = data;
263
- }
264
-
265
- uninstall() {
266
- this.dom = null;
267
- // this.container.removeEventListener('contextmenu', this._oncontextmenu);
268
- this.container.removeEventListener(
269
- 'keydown',
270
- this._oncontextmenu_keydown
271
- );
272
- this.container.removeEventListener('click', this._onclick);
273
- window.removeEventListener('blur', this._onblur);
274
- }
275
- }
276
-
277
- export default ContextMenu;
1
+ import './styles/contextMenu.css';
2
+
3
+ class ContextMenu {
4
+ constructor(container, items) {
5
+ this.container = container;
6
+ this.dom = null;
7
+ this.shown = false;
8
+ this.root = true;
9
+ this.parent = null;
10
+ this.submenus = [];
11
+ this.items = items;
12
+
13
+ this._onclick = (e) => {
14
+ if (
15
+ this.dom &&
16
+ e.target != this.dom &&
17
+ e.target.parentElement != this.dom &&
18
+ !e.target.classList.contains('item') &&
19
+ !e.target.parentElement.classList.contains('item')
20
+ ) {
21
+ this.hideAll();
22
+ }
23
+ };
24
+
25
+ this._oncontextmenu = (e) => {
26
+ e.preventDefault();
27
+ if (
28
+ e.target != this.dom &&
29
+ e.target.parentElement != this.dom &&
30
+ !e.target.classList.contains('item') &&
31
+ !e.target.parentElement.classList.contains('item')
32
+ ) {
33
+ this.hideAll();
34
+ this.show(e.clientX, e.clientY);
35
+ }
36
+ };
37
+
38
+ this._oncontextmenu_keydown = (e) => {
39
+ if (e.keyCode != 93) return;
40
+ e.preventDefault();
41
+
42
+ this.hideAll();
43
+ this.show(e.clientX, e.clientY);
44
+ };
45
+
46
+ this._onblur = () => {
47
+ this.hideAll();
48
+ };
49
+ }
50
+
51
+ getMenuDom() {
52
+ const menu = document.createElement('div');
53
+ menu.classList.add('context');
54
+
55
+ for (const item of this.items) {
56
+ menu.appendChild(this.itemToDomEl(item));
57
+ }
58
+
59
+ return menu;
60
+ }
61
+
62
+ itemToDomEl(data) {
63
+ const item = document.createElement('div');
64
+
65
+ if (data === null) {
66
+ item.classList = 'separator';
67
+ return item;
68
+ }
69
+
70
+ if (
71
+ data.hasOwnProperty('color') &&
72
+ /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(
73
+ data.color.toString()
74
+ )
75
+ ) {
76
+ item.style.cssText = `color: ${data.color}`;
77
+ }
78
+
79
+ item.classList.add('item');
80
+
81
+ const label = document.createElement('span');
82
+ label.classList = 'label';
83
+ label.innerText = data.hasOwnProperty('text')
84
+ ? data['text'].toString()
85
+ : '';
86
+ item.appendChild(label);
87
+
88
+ if (data.hasOwnProperty('disabled') && data['disabled']) {
89
+ item.classList.add('disabled');
90
+ } else {
91
+ item.classList.add('enabled');
92
+ }
93
+
94
+ const hotkey = document.createElement('span');
95
+ hotkey.classList = 'hotkey';
96
+ hotkey.innerText = data.hasOwnProperty('hotkey')
97
+ ? data['hotkey'].toString()
98
+ : '';
99
+ item.appendChild(hotkey);
100
+
101
+ if (
102
+ data.hasOwnProperty('subitems') &&
103
+ Array.isArray(data['subitems']) &&
104
+ data['subitems'].length > 0
105
+ ) {
106
+ const menu = new ContextMenu(this.container, data['subitems']);
107
+ menu.root = false;
108
+ menu.parent = this;
109
+
110
+ const openSubItems = () => {
111
+ if (data.hasOwnProperty('disabled') && data['disabled'] == true)
112
+ return;
113
+
114
+ this.hideSubMenus();
115
+
116
+ const x =
117
+ this.dom.offsetLeft +
118
+ this.dom.clientWidth +
119
+ item.offsetLeft;
120
+ const y = this.dom.offsetTop + item.offsetTop;
121
+
122
+ if (!menu.shown) {
123
+ menu.show(x, y);
124
+ } else {
125
+ menu.hide();
126
+ }
127
+ };
128
+
129
+ this.submenus.push(menu);
130
+
131
+ item.classList.add('has-subitems');
132
+ item.addEventListener('click', openSubItems);
133
+ item.addEventListener('mousemove', openSubItems);
134
+ } else if (
135
+ data.hasOwnProperty('submenu') &&
136
+ data['submenu'] instanceof ContextMenu
137
+ ) {
138
+ const menu = data['submenu'];
139
+ menu.root = false;
140
+ menu.parent = this;
141
+
142
+ const openSubItems = () => {
143
+ if (data.hasOwnProperty('disabled') && data['disabled'] == true)
144
+ return;
145
+
146
+ this.hideSubMenus();
147
+
148
+ const x =
149
+ this.dom.offsetLeft +
150
+ this.dom.clientWidth +
151
+ item.offsetLeft;
152
+ const y = this.dom.offsetTop + item.offsetTop;
153
+
154
+ if (!menu.shown) {
155
+ menu.show(x, y);
156
+ } else {
157
+ menu.hide();
158
+ }
159
+ };
160
+
161
+ this.submenus.push(menu);
162
+
163
+ item.classList.add('has-subitems');
164
+ item.addEventListener('click', openSubItems);
165
+ item.addEventListener('mousemove', openSubItems);
166
+ } else {
167
+ item.addEventListener('click', () => {
168
+ this.hideSubMenus();
169
+
170
+ if (item.classList.contains('disabled')) return;
171
+
172
+ if (
173
+ data.hasOwnProperty('onclick') &&
174
+ typeof data['onclick'] === 'function'
175
+ ) {
176
+ const event = {
177
+ handled: false,
178
+ item: item,
179
+ label: label,
180
+ hotkey: hotkey,
181
+ items: this.items,
182
+ data: data,
183
+ };
184
+
185
+ data['onclick'](event);
186
+
187
+ if (!event.handled) {
188
+ this.hide();
189
+ }
190
+ } else {
191
+ this.hide();
192
+ }
193
+ });
194
+
195
+ item.addEventListener('mousemove', () => {
196
+ this.hideSubMenus();
197
+ });
198
+ }
199
+
200
+ return item;
201
+ }
202
+
203
+ hideAll() {
204
+ if (this.root && !this.parent) {
205
+ if (this.shown) {
206
+ this.hideSubMenus();
207
+
208
+ this.shown = false;
209
+ this.container.removeChild(this.dom);
210
+
211
+ if (this.parent && this.parent.shown) {
212
+ this.parent.hide();
213
+ }
214
+ }
215
+
216
+ return;
217
+ }
218
+
219
+ this.parent.hide();
220
+ }
221
+
222
+ hide() {
223
+ if (this.dom && this.shown) {
224
+ this.shown = false;
225
+ this.hideSubMenus();
226
+ this.container.removeChild(this.dom);
227
+
228
+ if (this.parent && this.parent.shown) {
229
+ this.parent.hide();
230
+ }
231
+ }
232
+ }
233
+
234
+ hideSubMenus() {
235
+ for (const menu of this.submenus) {
236
+ if (menu.shown) {
237
+ menu.shown = false;
238
+ menu.container.removeChild(menu.dom);
239
+ }
240
+ menu.hideSubMenus();
241
+ }
242
+ }
243
+
244
+ show(x, y) {
245
+ this.dom = this.getMenuDom();
246
+
247
+ this.dom.style.left = `${x}px`;
248
+ this.dom.style.top = `${y}px`;
249
+
250
+ this.shown = true;
251
+ this.container.appendChild(this.dom);
252
+ }
253
+
254
+ install() {
255
+ this.container.addEventListener('contextmenu', this._oncontextmenu);
256
+ this.container.addEventListener('keydown', this._oncontextmenu_keydown);
257
+ this.container.addEventListener('click', this._onclick);
258
+ window.addEventListener('blur', this._onblur);
259
+ }
260
+
261
+ setData(data) {
262
+ this.items = data;
263
+ }
264
+
265
+ uninstall() {
266
+ this.dom = null;
267
+ // this.container.removeEventListener('contextmenu', this._oncontextmenu);
268
+ this.container.removeEventListener(
269
+ 'keydown',
270
+ this._oncontextmenu_keydown
271
+ );
272
+ this.container.removeEventListener('click', this._onclick);
273
+ window.removeEventListener('blur', this._onblur);
274
+ }
275
+ }
276
+
277
+ export default ContextMenu;