@cloudron/pankow 3.2.16 → 3.2.18

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.
@@ -0,0 +1,314 @@
1
+ <script setup>
2
+
3
+ import { nextTick, useTemplateRef, computed, ref, onUnmounted } from 'vue';
4
+
5
+ import MenuItem from './MenuItem.vue';
6
+ import MenuItemLink from './MenuItemLink.vue';
7
+ import TextInput from './TextInput.vue';
8
+
9
+ function getViewport() {
10
+ const win = window,
11
+ d = document,
12
+ e = d.documentElement,
13
+ g = d.getElementsByTagName('body')[0],
14
+ w = win.innerWidth || e.clientWidth || g.clientWidth,
15
+ h = win.innerHeight || e.clientHeight || g.clientHeight;
16
+
17
+ return {
18
+ width: w,
19
+ height: h
20
+ };
21
+ }
22
+
23
+ function getHiddenElementSize(element) {
24
+ if (element) {
25
+ const originalVisibility = element.style.visibility;
26
+ const originalDisplay = element.style.display;
27
+
28
+ element.style.visibility = 'hidden';
29
+ element.style.display = 'block';
30
+
31
+ element.clientHeight; // force reflow
32
+
33
+ const height = element.offsetHeight;
34
+ const width = element.offsetWidth;
35
+
36
+ element.style.display = originalDisplay;
37
+ element.style.visibility = originalVisibility;
38
+
39
+ return { height, width };
40
+ }
41
+ return { height: 0, width: 0 };
42
+ }
43
+
44
+
45
+ function getActiveElement(children) {
46
+ for (const child of children) {
47
+ if (child === document.activeElement) return child;
48
+ }
49
+ return null;
50
+ }
51
+
52
+ // outsideClickListener: null,
53
+ const container = useTemplateRef('container');
54
+ const itemElements = useTemplateRef('itemElements');
55
+
56
+ const emit = defineEmits([ 'close' ]);
57
+
58
+ const props = defineProps({
59
+ icon: String,
60
+ model: Array,
61
+ closeOnActivation: {
62
+ type: Boolean,
63
+ default: true,
64
+ },
65
+ searchThreshold: {
66
+ type: Number,
67
+ default: Infinity,
68
+ }
69
+ });
70
+
71
+ const openEventTimeStamp = ref(0);
72
+ const forElement = ref(null);
73
+ const isOpen = ref(false);
74
+ let pageX = 0;
75
+ let targetBottom = 0;
76
+ let targetTop = 0;
77
+ const offsetY = ref(0);
78
+ const rollUp = ref(false);
79
+ const searchString = ref('');
80
+ const emptyItem = ref({
81
+ label: '',
82
+ disabled: true,
83
+ });
84
+
85
+ const hasIcons = computed(() => {
86
+ return !!props.model.find((item) => !!item.icon);
87
+ });
88
+
89
+ const visibleItems = computed(() => {
90
+ if (!searchString.value) return props.model;
91
+ const s = searchString.value.toLowerCase();
92
+ return props.model.filter((item) => item.label.toLowerCase().indexOf(s) !== -1);
93
+ });
94
+
95
+ // select menu item by .label
96
+ function select(elem) {
97
+ let next = container.value.children[0];
98
+ while (true) {
99
+ if (!next) return;
100
+
101
+ if (next.innerText !== elem.label) {
102
+ next = next.nextElementSibling;
103
+ continue;
104
+ }
105
+
106
+ next.focus();
107
+ break;
108
+ }
109
+ }
110
+
111
+ let search = '';
112
+ let debounceTimer = null;
113
+ function onKeyDown(event) {
114
+ // only handle alphanumerics
115
+ if (!/^[a-zA-Z0-9]$/.test(event.key)) return;
116
+
117
+ const key = event.key.toLowerCase();
118
+
119
+ if (debounceTimer) clearTimeout(debounceTimer);
120
+ debounceTimer = setTimeout(() => search = '', 500);
121
+
122
+ search += key;
123
+
124
+ const found = visibleItems.value.find(elem => {
125
+ if (!elem.label) return false;
126
+ return elem.label.toLowerCase().indexOf(search) === 0;
127
+ });
128
+
129
+ if (found) select(found)
130
+ }
131
+
132
+ function onItemActivated(item) {
133
+ if (item.action instanceof Function) item.action(item);
134
+ if (props.closeOnActivation) close();
135
+ }
136
+
137
+ function blurEventHandler() {
138
+ close();
139
+ }
140
+
141
+ async function open(event, element = null) {
142
+ isOpen.value = true;
143
+ pageX = element ? element.getBoundingClientRect().left : event.pageX;
144
+ targetBottom = element ? element.getBoundingClientRect().bottom : event.pageY;
145
+ targetTop = element ? element.getBoundingClientRect().top : event.pageY;
146
+ offsetY.value = element ? (element.getBoundingClientRect().height + 2) : 0; // offset in case we roll up
147
+ forElement.value = element;
148
+
149
+ if (!container.value) return;
150
+
151
+ if (element) container.value.style.minWidth = element.getBoundingClientRect().width + 'px';
152
+
153
+ await nextTick();
154
+
155
+ position();
156
+
157
+ openEventTimeStamp.value = event.timeStamp;
158
+
159
+ event.preventDefault();
160
+
161
+ // if opened from a key action, immediately select first
162
+ if (event.type === 'keydown' && event.key === 'ArrowUp') selectUp();
163
+ else if (event.type === 'keydown') selectDown();
164
+ else container.value.focus();
165
+
166
+ setTimeout(() => {
167
+ window.document.addEventListener('click', blurEventHandler);
168
+ }, 0);
169
+ }
170
+
171
+ function selectUp() {
172
+ if (!container.value.children[0]) return;
173
+
174
+ const active = getActiveElement(container.value.children);
175
+
176
+ if (!active) return container.value.lastElementChild.focus();
177
+
178
+ let prev = active;
179
+ while (true) {
180
+ prev = prev.previousElementSibling;
181
+ if (!prev) return;
182
+
183
+ if (prev.getAttribute('separator') === 'true') continue;
184
+ else if (prev.classList.contains('pankow-menu-item-disabled')) continue;
185
+
186
+ prev.focus();
187
+ break;
188
+ }
189
+ }
190
+
191
+ function selectDown() {
192
+ if (!container.value.children[0]) return;
193
+
194
+ const active = getActiveElement(container.value.children);
195
+
196
+ if (!active) return container.value.firstElementChild.focus();
197
+
198
+ let next = active;
199
+ while (true) {
200
+ next = next.nextElementSibling;
201
+ if (!next) return;
202
+
203
+ if (next.getAttribute('separator') === 'true') continue;
204
+ else if (next.classList.contains('pankow-menu-item-disabled')) continue;
205
+
206
+ next.focus();
207
+ break;
208
+ }
209
+ }
210
+
211
+ function close() {
212
+ window.document.removeEventListener('click', blurEventHandler);
213
+ isOpen.value = false;
214
+ container.value.style.maxHeight = 'unset';
215
+ emit('close');
216
+ }
217
+
218
+ function position() {
219
+ if (!container.value) return;
220
+
221
+ const size = getHiddenElementSize(container.value);
222
+
223
+ let left = pageX;
224
+ let top = targetBottom + 1;
225
+ let width = container.value.offsetParent ? container.value.offsetWidth : size.width;
226
+ let height = container.value.offsetParent ? container.value.offsetHeight : size.height;
227
+ let viewport = getViewport();
228
+
229
+ let bottom = viewport.height - targetTop + 1;
230
+
231
+ //flip
232
+ if (left + width - document.body.scrollLeft > viewport.width) {
233
+ // if this is like a dropdown right align instead of flip
234
+ if (forElement.value) {
235
+ left = forElement.value.getBoundingClientRect().left + (forElement.value.getBoundingClientRect().width - width);
236
+ } else {
237
+ left -= width;
238
+ }
239
+ }
240
+
241
+ //flip
242
+ if (top + height - document.body.scrollTop > viewport.height) {
243
+ if (top - document.body.scrollTop > viewport.height/2) {
244
+ rollUp.value = true;
245
+ } else {
246
+ rollUp.value = false;
247
+ }
248
+ } else {
249
+ rollUp.value = false;
250
+ }
251
+
252
+ //fit
253
+ if (left < document.body.scrollLeft) {
254
+ left = document.body.scrollLeft;
255
+ }
256
+
257
+ container.value.style.left = left + 'px';
258
+ if (rollUp.value) {
259
+ container.value.style.top = 'unset';
260
+ container.value.style.bottom = bottom + 'px';
261
+ container.value.style.maxHeight = viewport.height - bottom + 'px';
262
+ container.value.style.height = 'auto';
263
+ } else {
264
+ container.value.style.top = top + 'px';
265
+ container.value.style.bottom = 'unset';
266
+ container.value.style.maxHeight = viewport.height - top + 'px';
267
+ }
268
+ }
269
+
270
+ onUnmounted(() => {
271
+ window.document.removeEventListener('click', blurEventHandler);
272
+ });
273
+
274
+ defineExpose({
275
+ isOpen,
276
+ open,
277
+ close,
278
+ });
279
+
280
+ </script>
281
+
282
+ <template>
283
+ <teleport to="#app">
284
+ <Transition :name="rollUp ? 'pankow-roll-up' : 'pankow-roll-down'">
285
+ <div class="pankow-menu" v-show="isOpen" ref="container" tabindex="0" @keydown.up.stop="selectUp()" @keydown.down.stop="selectDown()" @keydown.esc.stop="close()" @keydown="onKeyDown">
286
+ <TextInput placeholder="Filter ..." @keydown.up.stop="selectUp()" @keydown.down.stop="selectDown()" @keydown.stop @keydown.esc.stop="close()" @click.stop style="border: 0; padding: 8px 12px;" v-model="searchString" v-if="searchThreshold < model.length"/>
287
+ <component v-for="item in visibleItems" ref="itemElements" :is="item.type || (item.href ? MenuItemLink : MenuItem)" @activated="onItemActivated(item)" :item="item" :has-icons="hasIcons" />
288
+ <MenuItem v-if="model.length === 0" :item="emptyItem"/>
289
+ </div>
290
+ </Transition>
291
+ </teleport>
292
+ </template>
293
+
294
+ <style>
295
+
296
+ .pankow-menu {
297
+ position: fixed;
298
+ box-shadow: var(--pankow-menu-shadow);
299
+ border-radius: var(--pankow-border-radius);
300
+ background-color: var(--pankow-input-background-color);
301
+ min-width: 120px;
302
+ z-index: 3001;
303
+ color: var(--pankow-color-dark);
304
+ overflow: auto;
305
+ outline: none;
306
+ }
307
+
308
+ @media (prefers-color-scheme: dark) {
309
+ .pankow-menu {
310
+ color: var(--pankow-color-light-dark);
311
+ }
312
+ }
313
+
314
+ </style>
@@ -0,0 +1,319 @@
1
+ <script setup>
2
+
3
+ import { nextTick, useTemplateRef, computed, ref } from 'vue';
4
+
5
+ import MenuItem from './MenuItem.vue';
6
+ import MenuItemLink from './MenuItemLink.vue';
7
+ import TextInput from './TextInput.vue';
8
+
9
+ function getViewport() {
10
+ const win = window,
11
+ d = document,
12
+ e = d.documentElement,
13
+ g = d.getElementsByTagName('body')[0],
14
+ w = win.innerWidth || e.clientWidth || g.clientWidth,
15
+ h = win.innerHeight || e.clientHeight || g.clientHeight;
16
+
17
+ return {
18
+ width: w,
19
+ height: h
20
+ };
21
+ }
22
+
23
+ function getHiddenElementSize(element) {
24
+ if (element) {
25
+ const originalVisibility = element.style.visibility;
26
+ const originalDisplay = element.style.display;
27
+
28
+ element.style.visibility = 'hidden';
29
+ element.style.display = 'block';
30
+
31
+ element.clientHeight; // force reflow
32
+
33
+ const height = element.offsetHeight;
34
+ const width = element.offsetWidth;
35
+
36
+ element.style.display = originalDisplay;
37
+ element.style.visibility = originalVisibility;
38
+
39
+ return { height, width };
40
+ }
41
+ return { height: 0, width: 0 };
42
+ }
43
+
44
+
45
+ function getActiveElement(children) {
46
+ for (const child of children) {
47
+ if (child === document.activeElement) return child;
48
+ }
49
+ return null;
50
+ }
51
+
52
+ // outsideClickListener: null,
53
+ const container = useTemplateRef('container');
54
+ const itemElements = useTemplateRef('itemElements');
55
+
56
+ const emit = defineEmits([ 'close' ]);
57
+
58
+ const props = defineProps({
59
+ icon: String,
60
+ model: Array,
61
+ closeOnActivation: {
62
+ type: Boolean,
63
+ default: true,
64
+ },
65
+ searchThreshold: {
66
+ type: Number,
67
+ default: Infinity,
68
+ }
69
+ });
70
+
71
+ const openEventTimeStamp = ref(0);
72
+ const forElement = ref(null);
73
+ const isOpen = ref(false);
74
+ let pageX = 0;
75
+ let targetBottom = 0;
76
+ let targetTop = 0;
77
+ const offsetY = ref(0);
78
+ const rollUp = ref(false);
79
+ const searchString = ref('');
80
+ const emptyItem = ref({
81
+ label: '',
82
+ disabled: true,
83
+ });
84
+
85
+ const hasIcons = computed(() => {
86
+ return !!props.model.find((item) => !!item.icon);
87
+ });
88
+
89
+ const visibleItems = computed(() => {
90
+ if (!searchString.value) return props.model;
91
+ const s = searchString.value.toLowerCase();
92
+ return props.model.filter((item) => item.label.toLowerCase().indexOf(s) !== -1);
93
+ });
94
+
95
+ // select menu item by .label
96
+ function select(elem) {
97
+ let next = container.value.children[0];
98
+ while (true) {
99
+ if (!next) return;
100
+
101
+ if (next.innerText !== elem.label) {
102
+ next = next.nextElementSibling;
103
+ continue;
104
+ }
105
+
106
+ next.focus();
107
+ break;
108
+ }
109
+ }
110
+
111
+ let search = '';
112
+ let debounceTimer = null;
113
+ function onKeyDown(event) {
114
+ // only handle alphanumerics
115
+ if (!/^[a-zA-Z0-9]$/.test(event.key)) return;
116
+
117
+ const key = event.key.toLowerCase();
118
+
119
+ if (debounceTimer) clearTimeout(debounceTimer);
120
+ debounceTimer = setTimeout(() => search = '', 500);
121
+
122
+ search += key;
123
+
124
+ const found = visibleItems.value.find(elem => {
125
+ if (!elem.label) return false;
126
+ return elem.label.toLowerCase().indexOf(search) === 0;
127
+ });
128
+
129
+ if (found) select(found)
130
+ }
131
+
132
+ function onItemActivated(item) {
133
+ if (item.action instanceof Function) item.action(item);
134
+ if (props.closeOnActivation) close();
135
+ }
136
+
137
+ async function open(event, element = null) {
138
+ isOpen.value = true;
139
+ pageX = element ? element.getBoundingClientRect().left : event.pageX;
140
+ targetBottom = element ? element.getBoundingClientRect().bottom : event.pageY;
141
+ targetTop = element ? element.getBoundingClientRect().top : event.pageY;
142
+ offsetY.value = element ? (element.getBoundingClientRect().height + 2) : 0; // offset in case we roll up
143
+ forElement.value = element;
144
+
145
+ if (!container.value) return;
146
+
147
+ if (element) container.value.style.minWidth = element.getBoundingClientRect().width + 'px';
148
+
149
+ await nextTick();
150
+
151
+ position();
152
+
153
+ openEventTimeStamp.value = event.timeStamp;
154
+
155
+ event.preventDefault();
156
+
157
+ // if opened from a key action, immediately select first
158
+ if (event.type === 'keydown' && event.key === 'ArrowUp') selectUp();
159
+ else if (event.type === 'keydown') selectDown();
160
+ else container.value.focus();
161
+ }
162
+
163
+ function selectUp() {
164
+ if (!container.value.children[0]) return;
165
+
166
+ const active = getActiveElement(container.value.children);
167
+
168
+ if (!active) return container.value.lastElementChild.focus();
169
+
170
+ let prev = active;
171
+ while (true) {
172
+ prev = prev.previousElementSibling;
173
+ if (!prev) return;
174
+
175
+ if (prev.getAttribute('separator') === 'true') continue;
176
+ else if (prev.classList.contains('pankow-menu-item-disabled')) continue;
177
+
178
+ prev.focus();
179
+ break;
180
+ }
181
+ }
182
+
183
+ function selectDown() {
184
+ if (!container.value.children[0]) return;
185
+
186
+ const active = getActiveElement(container.value.children);
187
+
188
+ if (!active) return container.value.firstElementChild.focus();
189
+
190
+ let next = active;
191
+ while (true) {
192
+ next = next.nextElementSibling;
193
+ if (!next) return;
194
+
195
+ if (next.getAttribute('separator') === 'true') continue;
196
+ else if (next.classList.contains('pankow-menu-item-disabled')) continue;
197
+
198
+ next.focus();
199
+ break;
200
+ }
201
+ }
202
+
203
+ function onBackdrop(event) {
204
+ close();
205
+ event.preventDefault();
206
+ }
207
+
208
+ function close() {
209
+ isOpen.value = false;
210
+ container.value.style.maxHeight = 'unset';
211
+ container.value.style.bottom = 'unset';
212
+ container.value.style.top = 'unset';
213
+ emit('close');
214
+ }
215
+
216
+ function position() {
217
+ if (!container.value) return;
218
+
219
+ const size = getHiddenElementSize(container.value);
220
+
221
+ let left = pageX;
222
+ let top = targetBottom + 1;
223
+ let width = container.value.offsetParent ? container.value.offsetWidth : size.width;
224
+ let height = container.value.offsetParent ? container.value.offsetHeight : size.height;
225
+ let viewport = getViewport();
226
+
227
+ let bottom = viewport.height - targetTop + 1;
228
+
229
+ //flip
230
+ if (left + width - document.body.scrollLeft > viewport.width) {
231
+ // if this is like a dropdown right align instead of flip
232
+ if (forElement.value) {
233
+ left = forElement.value.getBoundingClientRect().left + (forElement.value.getBoundingClientRect().width - width);
234
+ } else {
235
+ left -= width;
236
+ }
237
+ }
238
+
239
+ //flip
240
+ if (top + height - document.body.scrollTop > viewport.height) {
241
+ if (top - document.body.scrollTop > viewport.height/2) {
242
+ rollUp.value = true;
243
+ } else {
244
+ rollUp.value = false;
245
+ }
246
+ } else {
247
+ rollUp.value = false;
248
+ }
249
+
250
+ //fit
251
+ if (left < document.body.scrollLeft) {
252
+ left = document.body.scrollLeft;
253
+ }
254
+
255
+ container.value.style.left = left + 'px';
256
+ if (rollUp.value) {
257
+ container.value.style.top = 'unset';
258
+ container.value.style.bottom = bottom + 'px';
259
+ container.value.style.maxHeight = viewport.height - bottom + 'px';
260
+ container.value.style.height = 'auto';
261
+ } else {
262
+ container.value.style.top = top + 'px';
263
+ container.value.style.bottom = 'unset';
264
+ container.value.style.maxHeight = viewport.height - top + 'px';
265
+ }
266
+ }
267
+
268
+ defineExpose({
269
+ open,
270
+ close,
271
+ });
272
+
273
+ </script>
274
+
275
+ <template>
276
+ <teleport to="#app">
277
+ <div class="pankow-menu-backdrop" @click="onBackdrop($event)" @contextmenu="onBackdrop($event)" v-show="isOpen"></div>
278
+ <Transition :name="rollUp ? 'pankow-roll-up' : 'pankow-roll-down'">
279
+ <div class="pankow-menu" v-show="isOpen" ref="container" tabindex="0" @keydown.up.stop="selectUp()" @keydown.down.stop="selectDown()" @keydown.esc.stop="close()" @keydown="onKeyDown">
280
+ <TextInput placeholder="Filter ..." style="border: 0; padding: 8px 12px;" v-model="searchString" v-if="searchThreshold < model.length"/>
281
+ <component v-for="item in visibleItems" ref="itemElements" :is="item.type || (item.href ? MenuItemLink : MenuItem)" @activated="onItemActivated(item)" :item="item" :has-icons="hasIcons" />
282
+ <MenuItem v-if="model.length === 0" :item="emptyItem"/>
283
+ </div>
284
+ </Transition>
285
+ </teleport>
286
+ </template>
287
+
288
+ <style>
289
+
290
+ .pankow-menu-backdrop {
291
+ position: fixed;
292
+ height: 100%;
293
+ width: 100%;
294
+ left: 0px;
295
+ top: 0px;
296
+ z-index: 3001;
297
+ background-color: transparent;
298
+ }
299
+
300
+ .pankow-menu {
301
+ position: fixed;
302
+ box-shadow: var(--pankow-menu-shadow);
303
+ border-radius: var(--pankow-border-radius);
304
+ background-color: var(--pankow-input-background-color);
305
+ min-width: 120px;
306
+ z-index: 3001;
307
+ color: var(--pankow-color-dark);
308
+ overflow: auto;
309
+ /*max-height: 100%;*/
310
+ outline: none;
311
+ }
312
+
313
+ @media (prefers-color-scheme: dark) {
314
+ .pankow-menu {
315
+ color: var(--pankow-color-light-dark);
316
+ }
317
+ }
318
+
319
+ </style>