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

collections.js « store « src - github.com/nextcloud/tasks.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de73ab7d36c94cda33d227aadb27085c45587d69 (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
/**
 * 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/>.
 *
 */
'use strict'

import Requests from '../services/requests.js'
import { isTaskInList, searchSubTasks } from './storeHelper.js'

import { generateUrl } from '@nextcloud/router'

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
	collections: [],
}

const getters = {

	/**
	 * Returns the count of tasks in a colllection
	 *
	 * Tasks have to
	 * - belong to a collection
	 * - be uncompleted
	 *
	 * @param {object} state The store data
	 * @param {object} getters The store getters
	 * @param {object} rootState The store root state
	 * @return {number} Count of tasks in the collection
	 */
	getCollectionCount: (state, getters, rootState) =>
		/**
		 * @param {string} collectionId The id of the collection in question
		 * @return {number} Count of tasks in the collection
		 */
		(collectionId) => {
			let count = 0
			getters.getTaskCalendars.forEach(calendar => {
				let tasks = Object.values(calendar.tasks).filter(task => {
					return isTaskInList(task, collectionId, false)
				})
				if (rootState.tasks.searchQuery) {
					tasks = tasks.filter(task => {
						if (task.matches(rootState.tasks.searchQuery)) {
							return true
						}
						// We also have to show tasks for which one sub(sub...)task matches.
						return searchSubTasks(task, rootState.tasks.searchQuery)
					})
				}
				count += tasks.length
			})
			return count
		},
}

const mutations = {
	/**
	 * Stores all available collections in the state
	 *
	 * @param {object} state The store data
	 * @param {object} payload The collections payload
	 */
	setCollections(state, payload) {
		state.collections = payload.collections
	},

	/**
	 * Sets the visibility of a collection
	 *
	 * @param {object} state The store data
	 * @param {object} newCollection The collection to update
	 */
	setVisibility(state, newCollection) {
		const collection = state.collections.find(search => search.id === newCollection.id)
		Vue.set(collection, 'show', newCollection.show)
	},
}

const actions = {
	/**
	 * Requests all collections from the server
	 *
	 * @param {object} context The store object
	 * @param {object} context.commit The store mutations
	 * @return {Promise}
	 */
	loadCollections({ commit }) {
		return new Promise(function(resolve) {
			Requests.get(generateUrl('apps/tasks/collections'))
				.then(response => {
					commit('setCollections', {
						collections: response.data.data.collections,
					})
					resolve()
				})
		})
	},

	/**
	 * Writes the visibility of a collection to the server
	 *
	 * @param {object} context The store mutations
	 * @param {object} collection The collection to change
	 * @return {Promise}
	 */
	setVisibility(context, collection) {
		context.commit('setVisibility', collection)
		return new Promise(function() {
			Requests.post(generateUrl('apps/tasks/collection/{id}/visibility/{show}', collection), {})
		})
	},
}

export default { state, getters, mutations, actions }