summaryrefslogtreecommitdiffstats
path: root/src/components/EntityPicker/EntityPicker.vue
blob: a60ee85ea2a6df96f5c211092eda1d707527df7a (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
<!--
  - @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="full"
		@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
					v-model="searchQuery"
					class="entity-picker__search-input"
					type="search"
					:placeholder="t('contacts', 'Search {types}', {types: searchPlaceholderTypes})"
					@change="onSearch">
			</div>

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

			<!-- TODO: find better wording/icon -->
			<EmptyContent v-if="loading" icon="">
				{{ t('contacts', 'Loading …') }}
			</EmptyContent>

			<!-- Searched & picked entities -->
			<div v-else-if="searchSet.length > 0 && availableEntities.length > 0" class="entity-picker__options">
				<!-- For each type we show title + list -->
				<div v-for="type in availableEntities" :key="type.id" class="entity-picker__option">
					<!-- Show content if we have something to show -->
					<h4 v-if="!isSingleType && type.dataSet.length > 0" class="entity-picker__option-caption">
						{{ t('contacts', 'Add {type}', {type: type.label.toLowerCase()}) }}
					</h4>

					<EntitySearchResult v-for="entity in type.dataSet"
						:key="entity.key || `entity-${entity.type}-${entity.id}`"
						:selection="selection"
						v-bind="entity"
						@click="onToggle(entity)" />
				</div>
			</div>

			<EmptyContent v-else-if="searchQuery" icon="icon-search">
				{{ t('contacts', 'No results') }}
			</EmptyContent>

			<div class="entity-picker__navigation">
				<button
					class="navigation__button-left"
					@click="onCancel">
					{{ t('contacts', 'Cancel') }}
				</button>
				<button
					class="navigation__button-right primary"
					@click="onSubmit">
					{{ t('contacts', 'Add to group') }}
				</button>
			</div>
		</div>
	</modal>
</template>

<script>
import Modal from '@nextcloud/vue/dist/Components/Modal'
import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
import EntityBubble from './EntityBubble'
import EntitySearchResult from './EntitySearchResult'

export default {
	name: 'EntityPicker',

	components: {
		EmptyContent,
		EntityBubble,
		EntitySearchResult,
		Modal,
	},

	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,
		},

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

	data() {
		return {
			searchQuery: '',
			selection: {},
		}
	},

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

		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
		 * @returns {Object[]}
		 */
		searchSet() {
			if (this.searchQuery && this.searchQuery.trim !== '') {
				return this.dataSet.filter(entity => {
					return entity.label.indexOf(this.searchQuery) > -1
				})
			}
			return this.dataSet
		},

		/**
		 * Returns available entities grouped by type(s) if any
		 * @returns {Array[]}
		 */
		availableEntities() {
			// If only one type, return the full set directly
			if (this.isSingleType) {
				return [{
					id: this.dataTypes[0].id,
					label: this.dataTypes[0].label,
					dataSet: this.searchSet,
				}]
			}

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

	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.selection))
		},

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

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

		/**
		 * Add entity from selection
		 * @param {Object} entity the entity to add
		 */
		onPick(entity) {
			this.$set(this.selection, 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.selection) {
				this.onDelete(entity)
			} else {
				this.onPick(entity)
			}
		},
	},

}

</script>

<style lang="scss" scoped>

// Dialog variables
$dialog-margin: 20px;
$dialog-width: 300px;
$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: ($clickable-area - $icon-size) / 2;

.entity-picker {
	position: relative;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	/** This next 2 rules are pretty hacky, with the modal component somehow
	the margin applied to the content is added to the total modal width,
	so here we subtract it to the width and height of the content.
	*/
	width: $dialog-width - $dialog-margin * 2;
	height: $dialog-height - $dialog-margin * 2;
	margin: $dialog-margin;
	max-height: calc(100vh - $dialog-margin * 2 - 10px);

	&__search {
		position: relative;
		display: flex;
		align-items: center;
		&-input {
			width: 100%;
			height: $clickable-area - $entity-spacing !important;
			padding-left: $clickable-area;
			font-size: 16px;
			line-height: $clickable-area - $entity-spacing;
			margin: $entity-spacing 0;
		}
		&-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;
		// 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);
	}

	&__options {
		margin: $entity-spacing 0;
		overflow-y: auto;
	}
	&__option {
		&-caption {
			padding-left: 10px;
			list-style-type: none;
			user-select: none;
			white-space: nowrap;
			text-overflow: ellipsis;
			pointer-events: none;
			color: var(--color-primary);
			box-shadow: none !important;
			line-height: $clickable-area;

			&:not(:first-child) {
				margin-top: $clickable-area / 2;
			}
		}
	}

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

/** 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>