You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

systemtagsfilelist.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2016 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function() {
  11. /**
  12. * @class OCA.SystemTags.FileList
  13. * @augments OCA.Files.FileList
  14. *
  15. * @classdesc SystemTags file list.
  16. * Contains a list of files filtered by system tags.
  17. *
  18. * @param $el container element with existing markup for the #controls
  19. * and a table
  20. * @param [options] map of options, see other parameters
  21. * @param {Array.<string>} [options.systemTagIds] array of system tag ids to
  22. * filter by
  23. */
  24. var FileList = function($el, options) {
  25. this.initialize($el, options);
  26. };
  27. FileList.prototype = _.extend({}, OCA.Files.FileList.prototype,
  28. /** @lends OCA.SystemTags.FileList.prototype */ {
  29. id: 'systemtagsfilter',
  30. appName: t('systemtags', 'Tagged files'),
  31. /**
  32. * Array of system tag ids to filter by
  33. *
  34. * @type Array.<string>
  35. */
  36. _systemTagIds: [],
  37. _lastUsedTags: [],
  38. _clientSideSort: true,
  39. _allowSelection: false,
  40. _filterField: null,
  41. /**
  42. * @private
  43. */
  44. initialize: function($el, options) {
  45. OCA.Files.FileList.prototype.initialize.apply(this, arguments);
  46. if (this.initialized) {
  47. return;
  48. }
  49. if (options && options.systemTagIds) {
  50. this._systemTagIds = options.systemTagIds;
  51. }
  52. OC.Plugins.attach('OCA.SystemTags.FileList', this);
  53. var $controls = this.$el.find('#controls').empty();
  54. _.defer(_.bind(this._getLastUsedTags, this));
  55. this._initFilterField($controls);
  56. },
  57. destroy: function() {
  58. this.$filterField.remove();
  59. OCA.Files.FileList.prototype.destroy.apply(this, arguments);
  60. },
  61. _getLastUsedTags: function() {
  62. var self = this;
  63. $.ajax({
  64. type: 'GET',
  65. url: OC.generateUrl('/apps/systemtags/lastused'),
  66. success: function (response) {
  67. self._lastUsedTags = response;
  68. }
  69. });
  70. },
  71. _initFilterField: function($container) {
  72. var self = this;
  73. this.$filterField = $('<input type="hidden" name="tags"/>');
  74. $container.append(this.$filterField);
  75. this.$filterField.select2({
  76. placeholder: t('systemtags', 'Select tags to filter by'),
  77. allowClear: false,
  78. multiple: true,
  79. toggleSelect: true,
  80. separator: ',',
  81. query: _.bind(this._queryTagsAutocomplete, this),
  82. id: function(tag) {
  83. return tag.id;
  84. },
  85. initSelection: function(element, callback) {
  86. var val = $(element).val().trim();
  87. if (val) {
  88. var tagIds = val.split(','),
  89. tags = [];
  90. OC.SystemTags.collection.fetch({
  91. success: function() {
  92. _.each(tagIds, function(tagId) {
  93. var tag = OC.SystemTags.collection.get(tagId);
  94. if (!_.isUndefined(tag)) {
  95. tags.push(tag.toJSON());
  96. }
  97. });
  98. callback(tags);
  99. }
  100. });
  101. } else {
  102. callback([]);
  103. }
  104. },
  105. formatResult: function (tag) {
  106. return OC.SystemTags.getDescriptiveTag(tag);
  107. },
  108. formatSelection: function (tag) {
  109. return OC.SystemTags.getDescriptiveTag(tag)[0].outerHTML;
  110. },
  111. sortResults: function(results) {
  112. results.sort(function(a, b) {
  113. var aLastUsed = self._lastUsedTags.indexOf(a.id);
  114. var bLastUsed = self._lastUsedTags.indexOf(b.id);
  115. if (aLastUsed !== bLastUsed) {
  116. if (bLastUsed === -1) {
  117. return -1;
  118. }
  119. if (aLastUsed === -1) {
  120. return 1;
  121. }
  122. return aLastUsed < bLastUsed ? -1 : 1;
  123. }
  124. // Both not found
  125. return OC.Util.naturalSortCompare(a.name, b.name);
  126. });
  127. return results;
  128. },
  129. escapeMarkup: function(m) {
  130. // prevent double markup escape
  131. return m;
  132. },
  133. formatNoMatches: function() {
  134. return t('systemtags', 'No tags found');
  135. }
  136. });
  137. this.$filterField.on('change', _.bind(this._onTagsChanged, this));
  138. return this.$filterField;
  139. },
  140. /**
  141. * Autocomplete function for dropdown results
  142. *
  143. * @param {Object} query select2 query object
  144. */
  145. _queryTagsAutocomplete: function(query) {
  146. OC.SystemTags.collection.fetch({
  147. success: function() {
  148. var results = OC.SystemTags.collection.filterByName(query.term);
  149. query.callback({
  150. results: _.invoke(results, 'toJSON')
  151. });
  152. }
  153. });
  154. },
  155. /**
  156. * Event handler for when the URL changed
  157. */
  158. _onUrlChanged: function(e) {
  159. if (e.dir) {
  160. var tags = _.filter(e.dir.split('/'), function(val) { return val.trim() !== ''; });
  161. this.$filterField.select2('val', tags || []);
  162. this._systemTagIds = tags;
  163. this.reload();
  164. }
  165. },
  166. _onTagsChanged: function(ev) {
  167. var val = $(ev.target).val().trim();
  168. if (val !== '') {
  169. this._systemTagIds = val.split(',');
  170. } else {
  171. this._systemTagIds = [];
  172. }
  173. this.$el.trigger(jQuery.Event('changeDirectory', {
  174. dir: this._systemTagIds.join('/')
  175. }));
  176. this.reload();
  177. },
  178. updateEmptyContent: function() {
  179. var dir = this.getCurrentDirectory();
  180. if (dir === '/') {
  181. // root has special permissions
  182. if (!this._systemTagIds.length) {
  183. // no tags selected
  184. this.$el.find('#emptycontent').html('<div class="icon-systemtags"></div>' +
  185. '<h2>' + t('systemtags', 'Please select tags to filter by') + '</h2>');
  186. } else {
  187. // tags selected but no results
  188. this.$el.find('#emptycontent').html('<div class="icon-systemtags"></div>' +
  189. '<h2>' + t('systemtags', 'No files found for the selected tags') + '</h2>');
  190. }
  191. this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty);
  192. this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty);
  193. }
  194. else {
  195. OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments);
  196. }
  197. },
  198. getDirectoryPermissions: function() {
  199. return OC.PERMISSION_READ | OC.PERMISSION_DELETE;
  200. },
  201. updateStorageStatistics: function() {
  202. // no op because it doesn't have
  203. // storage info like free space / used space
  204. },
  205. reload: function() {
  206. // there is only root
  207. this._setCurrentDir('/', false);
  208. if (!this._systemTagIds.length) {
  209. // don't reload
  210. this.updateEmptyContent();
  211. this.setFiles([]);
  212. return $.Deferred().resolve();
  213. }
  214. this._selectedFiles = {};
  215. this._selectionSummary.clear();
  216. if (this._currentFileModel) {
  217. this._currentFileModel.off();
  218. }
  219. this._currentFileModel = null;
  220. this.$el.find('.select-all').prop('checked', false);
  221. this.showMask();
  222. this._reloadCall = this.filesClient.getFilteredFiles(
  223. {
  224. systemTagIds: this._systemTagIds
  225. },
  226. {
  227. properties: this._getWebdavProperties()
  228. }
  229. );
  230. if (this._detailsView) {
  231. // close sidebar
  232. this._updateDetailsView(null);
  233. }
  234. var callBack = this.reloadCallback.bind(this);
  235. return this._reloadCall.then(callBack, callBack);
  236. },
  237. reloadCallback: function(status, result) {
  238. if (result) {
  239. // prepend empty dir info because original handler
  240. result.unshift({});
  241. }
  242. return OCA.Files.FileList.prototype.reloadCallback.call(this, status, result);
  243. }
  244. });
  245. OCA.SystemTags.FileList = FileList;
  246. })();