summaryrefslogtreecommitdiffstats
path: root/src/components/Properties/PropertyMultipleText.vue
blob: 1f489cfd281f36f0cec22c99044095b3be75e0bb (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
<!--
  - @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  -
  - @author John Molakvoæ <skjnldsv@protonmail.com>
  - @author Richard Steinmetz <richard@steinmetz.cloud>
  -
  - @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>
	<div v-if="propModel" class="property property--multiple-text">
		<!-- title if first element -->
		<PropertyTitle v-if="isFirstProperty && propModel.icon"
			:property="property"
			:is-multiple="isMultiple"
			:is-read-only="isReadOnly"
			:bus="bus"
			:icon="propModel.icon"
			:readable-name="propModel.readableName">
			<template #actions>
				<!-- props actions -->
				<PropertyActions v-if="!showActionsInFirstRow && !isReadOnly"
					class="property__actions"
					:actions="actions"
					:property-component="self"
					@delete="deleteProperty" />
			</template>
		</PropertyTitle>

		<div v-if="showActionsInFirstRow" class="property__row">
			<div class="property__label">
				<!-- read-only type because NcMultiselect can't be styled properly -->
				<span v-if="isReadOnly && propModel.options">
					{{ (localType && localType.name) || '' }}
				</span>

				<!-- type selector -->
				<NcSelect v-else-if="!isReadOnly && propModel.options"
					v-model="localType"
					:options="options"
					:placeholder="t('contacts', 'Select type')"
					:taggable="true"
					tag-placeholder="create"
					track-by="id"
					label="name"
					@option:created="createLabel"
					@input="updateType" />

				<!-- if we do not support any type on our model but one is set anyway -->
				<span v-else-if="selectType">
					{{ selectType.name }}
				</span>

				<!-- no options, and showing the first input of an unstructured value?
				then let's put an empty space (or the name again if no title is present) -->
				<span v-else-if="!property.isStructuredValue">
					{{ isFirstProperty ? '' : propModel.readableName }}
				</span>
			</div>

			<div class="property__value">
				<!-- show the first input if not a structured value -->
				<input v-if="!property.isStructuredValue"
					v-model.trim="localValue[0]"
					:readonly="isReadOnly"
					type="text"
					@input="updateValue">
			</div>

			<!-- props actions -->
			<div class="property__actions">
				<PropertyActions v-if="showActionsInFirstRow && !isReadOnly"
					:actions="actions"
					:property-component="this"
					@delete="deleteProperty" />
			</div>
		</div>

		<!-- force order based on model -->
		<template v-if="propModel.displayOrder && propModel.readableValues">
			<div v-for="index in propModel.displayOrder"
				:key="index"
				class="property__row">
				<template v-if="(isReadOnly && localValue[index]) || !isReadOnly">
					<div class="property__label">
						<span>{{ propModel.readableValues[index] }}</span>
					</div>
					<div class="property__value">
						<input v-model.trim="localValue[index]"
							:readonly="isReadOnly"
							type="text"
							@input="updateValue">
					</div>
					<div class="property__actions" />
				</template>
			</div>
		</template>

		<!-- no order enforced: just iterate on all the values -->
		<template v-else>
			<div v-for="(value, index) in filteredValue"
				:key="index"
				class="property__row">
				<template v-if="(isReadOnly && filteredValue[index]) || !isReadOnly">
					<div class="property__label" />
					<div class="property__value">
						<input v-model.trim="filteredValue[index]"
							:readonly="isReadOnly"
							type="text"
							@input="updateValue">
					</div>
					<div class="property__actions" />
				</template>
			</div>
		</template>
	</div>
</template>

<script>
import { NcSelect } from '@nextcloud/vue'
import PropertyMixin from '../../mixins/PropertyMixin.js'
import PropertyTitle from './PropertyTitle.vue'
import PropertyActions from './PropertyActions.vue'

export default {
	name: 'PropertyMultipleText',

	components: {
		NcSelect,
		PropertyTitle,
		PropertyActions,
	},

	mixins: [PropertyMixin],

	props: {
		value: {
			type: [Array, Object],
			default: () => [],
			required: true,
		},
	},

	computed: {
		self() {
			// It isn't possible to use "this" in a template slot so it needs to be aliased
			// Ref https://stackoverflow.com/a/69485484
			return this
		},

		filteredValue() {
			return this.localValue.filter((value, index) => index > 0)
		},

		/**
		 * Show the actions menu in the first row (instead of the title).
		 * This is true for all props that either have a type select or a fixed/unknown type.
		 * Otherwise, show the actions menu next to the title to prevent an empty row with just an
		 * actions menu.
		 *
		 * @return {boolean}
		 */
		showActionsInFirstRow() {
			return !!this.propModel.options
				|| !!this.selectType
				|| !this.property.isStructuredValue
		},
	},
}

</script>

<style lang="scss" scoped>
.property {
	&__label {
		&--read-only {
			// Prevent jumping of the label when changing edit/view mode
			// FIXME: drop forced height if NcMultiselect is migrated to NcSelect and can be
			//        properly styled as read-only
			height: 42px;
			line-height: 42px;
		}
	}
}

// Mobile tweaks
@media (max-width: 600px) {
	.property__value {
		width: 125px;
	}
}
</style>