summaryrefslogtreecommitdiffstats
path: root/src/components/EntityPicker/EntityPicker.vue
blob: 391e45e4d8d3010b0b240c140a03be3aca752f11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<!--
  - @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@pm.me>
  -
  - @author Marco Ambrosini <marcoambrosini@pm.me>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<template>
	<Modal size="normal"
		@close="onCancel">
		<!-- Wrapper for content & navigation -->
		<div class="entity-picker">
			<!-- Search -->
			<div class="entity-picker__search">
				<div class="entity-picker__search-icon icon-search" />
				<input ref="input"
					v-model="searchQuery"
					:placeholder="t('contacts', 'Search {types}', {types: searchPlaceholderTypes})"
					class="entity-picker__search-input"
					type="search"
					@input="onSearch">
			</div>

			<!-- Loading -->
			<EmptyContent v-if="loading" :name="t('contacts', 'Loading …')">
				<template #icon>
					<IconLoading :size="20" />
				</template>
			</EmptyContent>

			<template v-else>
				<!-- Picked entities -->
				<transition-group v-if="Object.keys(selectionSet).length > 0"
					name="zoom"
					tag="ul"
					class="entity-picker__selection">
					<EntityBubble v-for="entity in selectionSet"
						:key="entity.key || `entity-${entity.type}-${entity.id}`"
						v-bind="entity"
						@delete="onDelete(entity)" />
				</transition-group>

				<!-- No recommendations -->
				<EmptyContent v-if="dataSet.length === 0" :name="t('contacts', 'Search for people to add')">
					<template #icon>
						<IconSearch :size="20" />
					</template>
				</EmptyContent>

				<!-- Searched & picked entities -->
				<VirtualList v-else-if="searchSet.length > 0 && availableEntities.length > 0"
					class="entity-picker__options"
					data-key="id"
					:data-sources="availableEntities"
					:data-component="EntitySearchResult"
					:estimate-size="44"
					:extra-props="{ selection: selectionSet, onClick }" />

				<EmptyContent v-else-if="searchQuery" :name="t('contacts', 'No results')">
					<template #icon>
						<IconSearch :size="20" />
					</template>
				</EmptyContent>

				<div class="entity-picker__navigation">
					<button :disabled="loading"
						class="navigation__button-left"
						@click="onCancel">
						{{ t('contacts', 'Cancel') }}
					</button>
					<button :disabled="isEmptySelection || loading"
						class="navigation__button-right primary"
						@click="onSubmit">
						{{ confirmLabel }}
					</button>
				</div>
			</template>
		</div>
	</modal>
</template>

<script>
import debounce from 'debounce'
import VirtualList from 'vue-virtual-scroll-list'
import {
	NcEmptyContent as EmptyContent,
	NcLoadingIcon as IconLoading,
	NcModal as Modal,
} from '@nextcloud/vue'
import IconSearch from 'vue-material-design-icons/Magnify.vue'
import EntityBubble from './EntityBubble.vue'
import EntitySearchResult from './EntitySearchResult.vue'

export default {
	name: 'EntityPicker',

	components: {
		EmptyContent,
		EntityBubble,
		IconSearch,
		IconLoading,
		Modal,
		VirtualList,
	},

	props: {
		loading: {
			type: Boolean,
			default: false,
		},

		/**
		 * The types of data within dataSet
		 * Array of objects. id must match dataSet entity type
		 */
		dataTypes: {
			type: Array,
			required: true,
			validator: types => {
				const invalidTypes = types.filter(type => !type.id && !type.label)
				if (invalidTypes.length > 0) {
					console.error('The following types MUST have a proper id and label key', invalidTypes)
					return false
				}
				return true
			},
		},

		/**
		 * The data to be used
		 */
		dataSet: {
			type: Array,
			required: true,
			validator: data => {
				data.forEach(source => {
					if (!source.id || !source.label) {
						console.error('The following source MUST have a proper id and label key', source)
					}
				})
				return true
			},
		},

		/**
		 * The sorting key for the dataSet
		 */
		sort: {
			type: String,
			default: 'label',
		},

		/**
		 * Confirm button text
		 */
		confirmLabel: {
			type: String,
			default: t('contacts', 'Add to group'),
		},

		/**
		 * The input will also filter the dataSet based on the label.
		 * If you are using the search event to inject a different dataSet, you can disable this
		 */
		internalSearch: {
			type: Boolean,
			default: true,
		},

		/**
		 * Override the local management of selection
		 * You MUST use a sync modifier or the selection will be locked
		 */
		selection: {
			type: Object,
			default: null,
		},
	},

	data() {
		return {
			searchQuery: '',
			localSelection: {},
			EntitySearchResult,
		}
	},

	computed: {
		/**
		 * If the selection is set externally, let's use it
		 */
		selectionSet: {
			get() {
				if (this.selection !== null) {
					return this.selection
				}
				return this.localSelection
			},
			set(selection) {
				if (this.selection !== null) {
					this.$emit('update:selection', selection)
				}
				this.localSelection = selection
			},
		},

		/**
		 * Are we handling a single entity type ?
		 *
		 * @return {boolean}
		 */
		isSingleType() {
			return !(this.dataTypes.length > 1)
		},

		/**
		 * Is the current selection empty
		 *
		 * @return {boolean}
		 */
		isEmptySelection() {
			return Object.keys(this.selectionSet).length === 0
		},

		/**
		 * Formatted search input placeholder based on
		 * available types
		 *
		 * @return {string}
		 */
		searchPlaceholderTypes() {
			const types = this.dataTypes
				.map(type => type.label)
				.join(', ')
			return `${types}…`
		},

		/**
		 * Available data based on current search if query
		 * is valid, returns default full data et otherwise
		 *
		 * @return {object[]}
		 */
		searchSet() {
			// If internal search is enabled and we have a search query, filter data set
			if (this.internalSearch && this.searchQuery && this.searchQuery.trim !== '') {
				return this.dataSet.filter(entity => {
					return entity.label.toLowerCase().indexOf(this.searchQuery.toLowerCase()) > -1
				})
			}
			return this.dataSet
		},

		/**
		 * Returns available entities grouped by type(s) if any
		 *
		 * @return {object[]}
		 */
		availableEntities() {
			// If only one type, return the full set directly
			if (this.isSingleType) {
				return this.searchSet
			}

			// Else group by types
			return this.dataTypes.map(type => {
				const dataSet = this.searchSet.filter(entity => entity.type === type.id)
				const dataList = [
					{
						id: type.id,
						label: type.label,
						heading: true,
					},
					...dataSet,
				]

				// If no results, hide the type
				if (dataSet.length === 0) {
					return []
				}

				return dataList
			}).flat()
		},
	},

	mounted() {
		this.$nextTick(() => {
			this.$refs.input.focus()
			this.$refs.input.select()
		})
	},

	methods: {
		onCancel() {
			/**
			 * Emitted when the user closed or cancelled
			 */
			this.$emit('close')
		},
		onSubmit() {
			/**
			 * Emitted when user submit the form
			 *
			 * @type {Array} the selected entities
			 */
			this.$emit('submit', Object.values(this.selectionSet))
		},

		onSearch: debounce(function() {
			/**
			 * Emitted when search change
			 *
			 * @type {string} the search query
			 */
			this.$emit('search', this.searchQuery)
		}, 200),

		/**
		 * Remove entity from selection
		 *
		 * @param {object} entity the entity to remove
		 */
		onDelete(entity) {
			this.$delete(this.selectionSet, entity.id, entity)
			console.debug('Removing entity from selection', entity)
		},

		/**
		 * Add/remove entity from selection
		 *
		 * @param {object} entity the entity to add
		 */
		onClick(entity) {
			if (entity.id in this.selectionSet) {
				this.$delete(this.selectionSet, entity.id)
				console.debug('Removed entity to selection', entity)
				return
			}

			this.$set(this.selectionSet, entity.id, entity)
			console.debug('Added entity to selection', entity)
		},

		/**
		 * Toggle entity from selection
		 *
		 * @param {object} entity the entity to add/remove
		 */
		onToggle(entity) {
			if (entity.id in this.selectionSet) {
				this.onDelete(entity)
			} else {
				this.onPick(entity)
			}
		},
	},

}

</script>

<style lang="scss" scoped>
@use "sass:math";

// Dialog variables
$dialog-padding: 20px;
$dialog-height: 480px;
$entity-spacing: 4px;

// https://uxplanet.org/7-rules-for-mobile-ui-button-design-e9cf2ea54556
// recommended is 48px
// 44px is what we choose and have very good visual-to-usability ratio
$clickable-area: 44px;

// background icon size
// also used for the scss icon font
$icon-size: 16px;

// icon padding for a $clickable-area width and a $icon-size icon
// ( 44px - 16px ) / 2
$icon-margin: math.div($clickable-area - $icon-size, 2);

.entity-picker {
	position: relative;
	display: flex;
	flex-direction: column;
	min-height: $dialog-height;
	height: 100%;
	padding: $dialog-padding;
	box-sizing: border-box;

	&__search {
		position: relative;
		display: flex;
		align-items: center;
		&-input {
			width: 100%;
			height: $clickable-area - $entity-spacing !important;
			margin: $entity-spacing 0;
			padding-left: $clickable-area !important;
			font-size: 16px;
			line-height: $clickable-area - $entity-spacing;
		}
		&-icon {
			position: absolute;
			width: $clickable-area;
			height: $clickable-area;
		}
	}

	&__selection {
		display: flex;
		overflow-y: auto;
		align-content: flex-start;
		flex: 1 0 auto;
		flex-wrap: wrap;
		justify-content: flex-start;
		// half a line height to know there is more lines
		max-height: 6.5em;
		padding: $entity-spacing 0;
		border-bottom: 1px solid var(--color-background-darker);
		background: var(--color-main-background);

		// Allows 2 per line
		.entity-picker__bubble {
			max-width: calc(50% - #{$entity-spacing});
			margin-right: $entity-spacing;
			margin-bottom: $entity-spacing;
		}
	}

	&__options {
		overflow-y: auto;
		flex: 1 1 100%;
		margin: $entity-spacing 0;
	}

	&__navigation {
		z-index: 1;
		display: flex;
		// define our base width, no shrinkage
		flex: 0 0;
		justify-content: space-between;
		// Same as above
		width: 100%;
		box-shadow: 0 -10px 5px var(--color-main-background);
		&__button-right {
			margin-left: auto;
		}
	}
}

// Properly center Entity Picker empty content
::v-deep .empty-content {
	margin: auto 0 !important;
}

/** Size full in the modal component doesn't have border radius, this adds
it back */
::v-deep .modal-container {
	border-radius: var(--border-radius-large) !important;
}

</style>

<style lang="scss" scoped>
.zoom-enter-active {
	animation: zoom-in var(--animation-quick);
}

.zoom-leave-active {
	animation: zoom-in var(--animation-quick) reverse;

	will-change: transform;
}

@keyframes zoom-in {
	0% {
		transform: scale(0);
	}
	100% {
		transform: scale(1);
	}
}

</style>