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

systemtagsfilelist.js « src « systemtags « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4821f140222532ba4ec71dfcb6bfa7a610bc0995 (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
/**
 * Copyright (c) 2016 Vincent Petry <pvince81@owncloud.com>
 *
 * @author Joas Schilling <coding@schilljs.com>
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 * @author Vincent Petry <vincent@nextcloud.com>
 *
 * @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/>.
 *
 */

(function() {
	/**
	 * @class OCA.SystemTags.FileList
	 * @augments OCA.Files.FileList
	 *
	 * @classdesc SystemTags file list.
	 * Contains a list of files filtered by system tags.
	 *
	 * @param {object} $el container element with existing markup for the #controls and a table
	 * @param {Array} [options] map of options, see other parameters
	 * @param {Array.<string>} [options.systemTagIds] array of system tag ids to
	 * filter by
	 */
	const FileList = function($el, options) {
		this.initialize($el, options)
	}
	FileList.prototype = _.extend(
		{},
		OCA.Files.FileList.prototype,
		/** @lends OCA.SystemTags.FileList.prototype */ {
			id: 'systemtagsfilter',
			appName: t('systemtags', 'Tagged files'),

			/**
			 * Array of system tag ids to filter by
			 *
			 * @type Array.<string>
			 */
			_systemTagIds: [],
			_lastUsedTags: [],

			_clientSideSort: true,
			_allowSelection: false,

			_filterField: null,

			/**
			 * @private
			 * @param {object} $el container element
			 * @param {object} [options] map of options, see other parameters
			 */
			initialize($el, options) {
				OCA.Files.FileList.prototype.initialize.apply(this, arguments)
				if (this.initialized) {
					return
				}

				if (options && options.systemTagIds) {
					this._systemTagIds = options.systemTagIds
				}

				OC.Plugins.attach('OCA.SystemTags.FileList', this)

				const $controls = this.$el.find('#controls').empty()

				_.defer(_.bind(this._getLastUsedTags, this))
				this._initFilterField($controls)
			},

			destroy() {
				this.$filterField.remove()

				OCA.Files.FileList.prototype.destroy.apply(this, arguments)
			},

			_getLastUsedTags() {
				const self = this
				$.ajax({
					type: 'GET',
					url: OC.generateUrl('/apps/systemtags/lastused'),
					success(response) {
						self._lastUsedTags = response
					},
				})
			},

			_initFilterField($container) {
				const self = this
				this.$filterField = $('<input type="hidden" name="tags"/>')
				$container.append(this.$filterField)
				this.$filterField.select2({
					placeholder: t('systemtags', 'Select tags to filter by'),
					allowClear: false,
					multiple: true,
					toggleSelect: true,
					separator: ',',
					query: _.bind(this._queryTagsAutocomplete, this),

					id(tag) {
						return tag.id
					},

					initSelection(element, callback) {
						const val = $(element)
							.val()
							.trim()
						if (val) {
							const tagIds = val.split(',')
							const tags = []

							OC.SystemTags.collection.fetch({
								success() {
									_.each(tagIds, function(tagId) {
										const tag = OC.SystemTags.collection.get(
											tagId
										)
										if (!_.isUndefined(tag)) {
											tags.push(tag.toJSON())
										}
									})

									callback(tags)
								},
							})
						} else {
							// eslint-disable-next-line node/no-callback-literal
							callback([])
						}
					},

					formatResult(tag) {
						return OC.SystemTags.getDescriptiveTag(tag)
					},

					formatSelection(tag) {
						return OC.SystemTags.getDescriptiveTag(tag)[0]
							.outerHTML
					},

					sortResults(results) {
						results.sort(function(a, b) {
							const aLastUsed = self._lastUsedTags.indexOf(a.id)
							const bLastUsed = self._lastUsedTags.indexOf(b.id)

							if (aLastUsed !== bLastUsed) {
								if (bLastUsed === -1) {
									return -1
								}
								if (aLastUsed === -1) {
									return 1
								}
								return aLastUsed < bLastUsed ? -1 : 1
							}

							// Both not found
							return OC.Util.naturalSortCompare(a.name, b.name)
						})
						return results
					},

					escapeMarkup(m) {
						// prevent double markup escape
						return m
					},
					formatNoMatches() {
						return t('systemtags', 'No tags found')
					},
				})
				this.$filterField.on(
					'change',
					_.bind(this._onTagsChanged, this)
				)
				return this.$filterField
			},

			/**
			 * Autocomplete function for dropdown results
			 *
			 * @param {object} query select2 query object
			 */
			_queryTagsAutocomplete(query) {
				OC.SystemTags.collection.fetch({
					success() {
						const results = OC.SystemTags.collection.filterByName(
							query.term
						)

						query.callback({
							results: _.invoke(results, 'toJSON'),
						})
					},
				})
			},

			/**
			 * Event handler for when the URL changed
			 *
			 * @param {Event} e the urlchanged event
			 */
			_onUrlChanged(e) {
				if (e.dir) {
					const tags = _.filter(e.dir.split('/'), function(val) {
						return val.trim() !== ''
					})
					this.$filterField.select2('val', tags || [])
					this._systemTagIds = tags
					this.reload()
				}
			},

			_onTagsChanged(ev) {
				const val = $(ev.target)
					.val()
					.trim()
				if (val !== '') {
					this._systemTagIds = val.split(',')
				} else {
					this._systemTagIds = []
				}

				this.$el.trigger(
					$.Event('changeDirectory', {
						dir: this._systemTagIds.join('/'),
					})
				)
				this.reload()
			},

			updateEmptyContent() {
				const dir = this.getCurrentDirectory()
				if (dir === '/') {
					// root has special permissions
					if (!this._systemTagIds.length) {
						// no tags selected
						this.$el
							.find('#emptycontent')
							.html(
								'<div class="icon-systemtags"></div>'
									+ '<h2>'
									+ t(
										'systemtags',
										'Please select tags to filter by'
									)
									+ '</h2>'
							)
					} else {
						// tags selected but no results
						this.$el
							.find('#emptycontent')
							.html(
								'<div class="icon-systemtags"></div>'
									+ '<h2>'
									+ t(
										'systemtags',
										'No files found for the selected tags'
									)
									+ '</h2>'
							)
					}
					this.$el
						.find('#emptycontent')
						.toggleClass('hidden', !this.isEmpty)
					this.$el
						.find('#filestable thead th')
						.toggleClass('hidden', this.isEmpty)
				} else {
					OCA.Files.FileList.prototype.updateEmptyContent.apply(
						this,
						arguments
					)
				}
			},

			getDirectoryPermissions() {
				return OC.PERMISSION_READ | OC.PERMISSION_DELETE
			},

			updateStorageStatistics() {
				// no op because it doesn't have
				// storage info like free space / used space
			},

			reload() {
				// there is only root
				this._setCurrentDir('/', false)

				if (!this._systemTagIds.length) {
					// don't reload
					this.updateEmptyContent()
					this.setFiles([])
					return $.Deferred().resolve()
				}

				this._selectedFiles = {}
				this._selectionSummary.clear()
				if (this._currentFileModel) {
					this._currentFileModel.off()
				}
				this._currentFileModel = null
				this.$el.find('.select-all').prop('checked', false)
				this.showMask()
				this._reloadCall = this.filesClient.getFilteredFiles(
					{
						systemTagIds: this._systemTagIds,
					},
					{
						properties: this._getWebdavProperties(),
					}
				)
				if (this._detailsView) {
					// close sidebar
					this._updateDetailsView(null)
				}
				const callBack = this.reloadCallback.bind(this)
				return this._reloadCall.then(callBack, callBack)
			},

			reloadCallback(status, result) {
				if (result) {
					// prepend empty dir info because original handler
					result.unshift({})
				}

				return OCA.Files.FileList.prototype.reloadCallback.call(
					this,
					status,
					result
				)
			},
		}
	)

	OCA.SystemTags.FileList = FileList
})()