Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TaskDragContainer.vue « components « src - github.com/nextcloud/tasks.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d51e48e416fba046c33f66515934fc727dc639ac (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
<!--
Nextcloud - Tasks

@author Raimund Schlüßler
@copyright 2018 Raimund Schlüßler <raimund.schluessler@mailbox.org>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.

This library 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 library.  If not, see <http://www.gnu.org/licenses/>.

-->

<template>
	<draggable tag="ol"
		:list="['']"
		:set-data="setDragData"
		v-bind="{group: 'tasks', swapThreshold: 0.30, delay: 500, delayOnTouchOnly: true, touchStartThreshold: 3, disabled: disabled, filter: '.readOnly'}"
		:move="onMove"
		@add="onAdd"
		@end="onEnd">
		<TaskBody v-for="task in sortedTasks"
			:key="task.key"
			:task="task"
			:collection-string="collectionString" />
	</draggable>
</template>

<script>
import { sort } from '../store/storeHelper'

import draggable from 'vuedraggable'
import { mapGetters, mapActions, mapMutations } from 'vuex'

export default {
	name: 'TaskDragContainer',
	components: {
		/**
		 * We asynchronously import here, because we have a circular dependency
		 * between TaskDragContainer and TaskBody which otherwise cannot be resolved.
		 * See https://vuejs.org/v2/guide/components-edge-cases.html#Circular-References-Between-Components
		 *
		 * We load it "eager", because the TaskBody will always be required.
		 *
		 * @returns {Object} The TaskBody component
		 */
		TaskBody: () => import(/* webpackMode: "eager" */ './TaskBody'),
		draggable,
	},
	props: {
		tasks: {
			type: Array,
			default: () => [],
		},
		disabled: {
			type: Boolean,
			default: false,
		},
		collectionString: {
			type: String,
			default: null,
		},
	},
	computed: {
		...mapGetters({
			getCalendar: 'getCalendarById',
			getTask: 'getTaskByUri',
			sortOrder: 'sortOrder',
			sortDirection: 'sortDirection',
		}),

		sortedTasks() {
			return sort([...this.tasks], this.sortOrder, this.sortDirection)
		},
	},
	methods: {
		...mapActions([
			'moveTask',
			'setPriority',
			'setPercentComplete',
			'setDate',
			'setSortOrder',
		]),

		...mapMutations({
			commitSortOrder: 'setSortOrder',
		}),

		setDragData: (dataTransfer) => {
			// We do nothing here, this just prevents
			// vue.draggable from setting data on the
			// dataTransfer object.
		},

		adjustSortOrder(task, newIndex, oldIndex = -1) {
			// Only change the sort order if we sort manually
			if (this.sortOrder !== 'manual') {
				return
			}
			// If the tasks array has no entry, we don't need to sort.
			if (this.sortedTasks.length === 0) {
				return
			}
			// If the task is inserted at its current position, don't sort.
			if (newIndex === oldIndex) {
				return
			}

			// Get a copy of the sorted tasks array
			const sortedTasks = [...this.sortedTasks]

			// In case the task to move is already in the array, move it to the new position
			if (oldIndex > -1) {
				sortedTasks.splice(newIndex, 0, sortedTasks.splice(oldIndex, 1)[0])
			// Otherwise insert it
			} else {
				sortedTasks.splice(newIndex, 0, task)
			}
			// Get the new sort order for the moved task and apply it.
			// We just do that to minimize the number of other tasks to be changed.
			let newSortOrder
			if (newIndex + 1 < sortedTasks.length) {
				newSortOrder = sortedTasks[newIndex + 1].sortOrder - Math.pow(-1, +this.sortDirection)
			} else {
				newSortOrder = sortedTasks[newIndex - 1].sortOrder + Math.pow(-1, +this.sortDirection)
			}
			if (newSortOrder < 0) {
				newSortOrder = 0
			}
			// If we moved the task from a different list, don't schedule a request to the server,
			// this will be done afterwards.
			const newOrder = { task: sortedTasks[newIndex], order: newSortOrder }
			if (oldIndex > -1) {
				this.setSortOrder(newOrder)
			} else {
				this.commitSortOrder(newOrder)
			}

			// Check the sort orders to be strictly monotonous
			if (this.sortDirection) {
				sortedTasks.reverse()
			}
			let currentIndex = 1
			while (currentIndex < sortedTasks.length) {
				if (sortedTasks[currentIndex].sortOrder <= sortedTasks[currentIndex - 1].sortOrder) {
					const order = { task: sortedTasks[currentIndex], order: sortedTasks[currentIndex - 1].sortOrder + 1 }
					if (sortedTasks[currentIndex] === task) {
						this.commitSortOrder(order)
					} else {
						this.setSortOrder(order)
					}
				}
				currentIndex++
			}
		},

		/**
		 * Called when a task is dropped.
		 * We only handle sorting tasks here.
		 *
		 * @param {Object} $event The event which caused the drop
		 */
		onEnd($event) {
			// Don't do anything if the tasks are not sorted but moved.
			if ($event.to !== $event.from) {
				return
			}
			/**
			 * We have to adjust the sortOrder property of the tasks
			 * to achieve the desired sort order.
			 */
			this.adjustSortOrder(null, $event.newIndex, $event.oldIndex)
		},

		/**
		 * Called when a task is dropped.
		 * We handle changing the parent task, calendar or collection here
		 * and also have to sort a task to the correct position
		 * in case of manual sort order.
		 *
		 * @param {Object} $event The event which caused the drop
		 */
		onAdd($event) {
			let task
			// The task to move
			const taskAttribute = $event.item.attributes['task-id']
			if (taskAttribute) {
				task = this.getTask(taskAttribute.value)
			}
			/**
			 * We have to adjust the sortOrder property of the tasks
			 * to achieve the desired sort order.
			 */
			this.adjustSortOrder(task, $event.newIndex, -1)
			// Move the task to a new calendar or parent.
			this.prepareMoving(task, $event)
			this.prepareCollecting(task, $event)
			$event.stopPropagation()
		},

		/**
		 * Called when a task is moved.
		 * We check here if drop onto the target is allowed.
		 *
		 * Dropping tasks with class not PUBLIC onto calendars shared with me
		 * is forbidden.
		 *
		 * @param {Object} $event The event which caused the move
		 * @returns {Boolean} If the drop is allowed
		 */
		onMove($event) {
			// The task to move
			const taskAttribute = $event.dragged.attributes['task-id']
			if (taskAttribute) {
				const task = this.getTask(taskAttribute.value)
				if (task.class === 'PUBLIC') {
					return true
				}
				let calendar
				const calendarAttribute = $event.to.attributes['calendar-id']
				if (calendarAttribute) {
					calendar = this.getCalendar(calendarAttribute.value)
				}
				if (!calendar) {
					const parentAttribute = $event.to.attributes['task-id']
					if (parentAttribute) {
						const parent = this.getTask(parentAttribute.value)
						// If we move to a parent task, the calendar has to be the parents calendar.
						calendar = parent.calendar
					}
				}
				if (calendar && calendar.isSharedWithMe) {
					return false
				}
			}
			return true
		},

		/**
		 * Function to move a task to a new calendar or parent
		 *
		 * @param {Task} task The task to change
		 * @param {Object} $event The event which caused the move
		 */
		prepareMoving(task, $event) {
			let parent, calendar
			// The new calendar --> make the moved task a root task
			const calendarAttribute = $event.to.attributes['calendar-id']
			if (calendarAttribute) {
				calendar = this.getCalendar(calendarAttribute.value)
			}
			// The new parent task --> make the moved task a subtask
			const parentAttribute = $event.to.attributes['task-id']
			if (parentAttribute) {
				parent = this.getTask(parentAttribute.value)
				// If we move to a parent task, the calendar has to be the parents calendar.
				calendar = parent.calendar
			}
			// If no calendar is given (e.g. in week collection), the calendar is unchanged.
			if (!calendar) {
				calendar = task.calendar
			}
			// Move the task to the appropriate calendar and parent.
			this.moveTask({ task, calendar, parent })
		},

		/**
		 * Function to add a task to a collection.
		 *
		 * @param {Task} task The task to change
		 * @param {Object} $event The event which caused the change
		 */
		prepareCollecting(task, $event) {
			// The new collection --> make the moved task a member of this collection
			// This is necessary for the collections {starred, today, completed, uncompleted and week}
			const collectionAttribute = $event.to.attributes['collection-id']
			if (collectionAttribute) {
				let collectionId = collectionAttribute.value
				// Split the collectionId in case we deal with 'week-x'
				collectionId = collectionId.split('-')
				switch (collectionId[0]) {
				case 'starred':
					this.setPriority({ task, priority: 1 })
					break
				case 'completed':
					this.setPercentComplete({ task, complete: 100 })
					break
				case 'uncompleted':
					if (task.completed) {
						this.setPercentComplete({ task, complete: 0 })
					}
					break
				case 'today':
					this.setDate({ task, day: 0 })
					break
				case 'week':
					this.setDate({ task, day: collectionId[1] })
					break
				}
			}
		},
	},
}
</script>