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.

systemtagsplugin.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2014 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. /* global Handlebars */
  11. (function (OCA) {
  12. _.extend(OC.Files.Client, {
  13. PROPERTY_SYSTEM_TAGS: '{' + OC.Files.Client.NS_NEXTCLOUD + '}system-tags',
  14. });
  15. OCA.Files = OCA.Files || {};
  16. /**
  17. * Extends the file actions and file list to add system tags inline
  18. *
  19. * @namespace OCA.Files.SystemTagsPlugin
  20. */
  21. OCA.Files.SystemTagsPlugin = {
  22. name: 'SystemTags',
  23. allowedLists: [
  24. 'files',
  25. 'favorites',
  26. 'shares.self',
  27. 'shares.others',
  28. 'shares.link'
  29. ],
  30. _buildTagSpan: function(tag, isMore = false) {
  31. var $tag = $('<li class="system-tags__tag"></li>');
  32. $tag.text(tag).addClass(isMore ? 'system-tags__tag--more' : '');
  33. return $tag;
  34. },
  35. _buildTagsUI: function(tags) {
  36. $systemTags = $('<ul class="system-tags"></ul>');
  37. if (tags.length === 1) {
  38. $systemTags.attr('aria-label', t('files', 'This file has the tag {tag}', { tag: tags[0] }));
  39. } else if (tags.length > 1) {
  40. var firstTags = tags.slice(0, -1).join(', ');
  41. var lastTag = tags[tags.length - 1];
  42. $systemTags.attr('aria-label', t('files', 'This file has the tags {firstTags} and {lastTag}', { firstTags, lastTag }));
  43. }
  44. if (tags.length > 0) {
  45. $systemTags.append(this._buildTagSpan(tags[0]));
  46. }
  47. // More tags than the one we're showing
  48. if (tags.length > 1) {
  49. $moreTag = this._buildTagSpan('+' + (tags.length - 1), true)
  50. $moreTag.attr('title', tags.slice(1).join(', '));
  51. $systemTags.append($moreTag);
  52. }
  53. return $systemTags;
  54. },
  55. _extendFileList: function(fileList) {
  56. var self = this;
  57. // extend row prototype
  58. var oldCreateRow = fileList._createRow;
  59. fileList._createRow = function(fileData) {
  60. var $tr = oldCreateRow.apply(this, arguments);
  61. var systemTags = fileData.systemTags || [];
  62. // Update tr data list
  63. $tr.attr('data-systemTags', systemTags.join('|'));
  64. // No tags, no need to do anything
  65. if (systemTags.length === 0) {
  66. return $tr;
  67. }
  68. // Build tags ui and inject
  69. $systemTags = self._buildTagsUI.apply(self, [systemTags])
  70. $systemTags.insertAfter($tr.find('td.filename .nametext'));
  71. return $tr;
  72. };
  73. var oldElementToFile = fileList.elementToFile;
  74. fileList.elementToFile = function ($el) {
  75. var fileInfo = oldElementToFile.apply(this, arguments);
  76. var systemTags = $el.attr('data-systemTags');
  77. fileInfo.systemTags = systemTags?.split?.('|') || [];
  78. return fileInfo;
  79. };
  80. var oldGetWebdavProperties = fileList._getWebdavProperties;
  81. fileList._getWebdavProperties = function () {
  82. var props = oldGetWebdavProperties.apply(this, arguments);
  83. props.push(OC.Files.Client.PROPERTY_SYSTEM_TAGS);
  84. return props;
  85. };
  86. fileList.filesClient.addFileInfoParser(function (response) {
  87. var data = {};
  88. var props = response.propStat[0].properties;
  89. var systemTags = props[OC.Files.Client.PROPERTY_SYSTEM_TAGS] || [];
  90. if (systemTags && systemTags.length) {
  91. data.systemTags = systemTags
  92. .filter(xmlvalue => xmlvalue.namespaceURI === OC.Files.Client.NS_NEXTCLOUD && xmlvalue.nodeName.split(':')[1] === 'system-tag')
  93. .map(xmlvalue => xmlvalue.textContent || xmlvalue.text);
  94. }
  95. return data;
  96. });
  97. },
  98. attach: function(fileList) {
  99. if (this.allowedLists.indexOf(fileList.id) < 0) {
  100. return;
  101. }
  102. this._extendFileList(fileList);
  103. },
  104. };
  105. })
  106. (OCA);
  107. OC.Plugins.register('OCA.Files.FileList', OCA.Files.SystemTagsPlugin);