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.

systemtagsinfoview.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2015
  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(OCA) {
  11. function modelToSelection(model) {
  12. var data = model.toJSON();
  13. if (!OC.isUserAdmin() && !data.canAssign) {
  14. data.locked = true;
  15. }
  16. return data;
  17. }
  18. /**
  19. * @class OCA.SystemTags.SystemTagsInfoView
  20. * @classdesc
  21. *
  22. * Displays a file's system tags
  23. *
  24. */
  25. var SystemTagsInfoView = OCA.Files.DetailFileInfoView.extend(
  26. /** @lends OCA.SystemTags.SystemTagsInfoView.prototype */ {
  27. _rendered: false,
  28. className: 'systemTagsInfoView hidden',
  29. /**
  30. * @type OC.SystemTags.SystemTagsInputField
  31. */
  32. _inputView: null,
  33. initialize: function(options) {
  34. var self = this;
  35. options = options || {};
  36. this._inputView = new OC.SystemTags.SystemTagsInputField({
  37. multiple: true,
  38. allowActions: true,
  39. allowCreate: true,
  40. isAdmin: OC.isUserAdmin(),
  41. initSelection: function(element, callback) {
  42. callback(self.selectedTagsCollection.map(modelToSelection));
  43. }
  44. });
  45. this.selectedTagsCollection = new OC.SystemTags.SystemTagsMappingCollection([], {objectType: 'files'});
  46. this._inputView.collection.on('change:name', this._onTagRenamedGlobally, this);
  47. this._inputView.collection.on('remove', this._onTagDeletedGlobally, this);
  48. this._inputView.on('select', this._onSelectTag, this);
  49. this._inputView.on('deselect', this._onDeselectTag, this);
  50. },
  51. /**
  52. * Event handler whenever a tag was selected
  53. */
  54. _onSelectTag: function(tag) {
  55. // create a mapping entry for this tag
  56. this.selectedTagsCollection.create(tag.toJSON());
  57. },
  58. /**
  59. * Event handler whenever a tag gets deselected.
  60. * Removes the selected tag from the mapping collection.
  61. *
  62. * @param {string} tagId tag id
  63. */
  64. _onDeselectTag: function(tagId) {
  65. this.selectedTagsCollection.get(tagId).destroy();
  66. },
  67. /**
  68. * Event handler whenever a tag was renamed globally.
  69. *
  70. * This will automatically adjust the tag mapping collection to
  71. * container the new name.
  72. *
  73. * @param {OC.Backbone.Model} changedTag tag model that has changed
  74. */
  75. _onTagRenamedGlobally: function(changedTag) {
  76. // also rename it in the selection, if applicable
  77. var selectedTagMapping = this.selectedTagsCollection.get(changedTag.id);
  78. if (selectedTagMapping) {
  79. selectedTagMapping.set(changedTag.toJSON());
  80. }
  81. },
  82. /**
  83. * Event handler whenever a tag was deleted globally.
  84. *
  85. * This will automatically adjust the tag mapping collection to
  86. * container the new name.
  87. *
  88. * @param {OC.Backbone.Model} tagId tag model that has changed
  89. */
  90. _onTagDeletedGlobally: function(tagId) {
  91. // also rename it in the selection, if applicable
  92. this.selectedTagsCollection.remove(tagId);
  93. },
  94. setFileInfo: function(fileInfo) {
  95. var self = this;
  96. if (!this._rendered) {
  97. this.render();
  98. }
  99. if (fileInfo) {
  100. this.selectedTagsCollection.setObjectId(fileInfo.id);
  101. this.selectedTagsCollection.fetch({
  102. success: function(collection) {
  103. collection.fetched = true;
  104. var appliedTags = collection.map(modelToSelection);
  105. self._inputView.setData(appliedTags);
  106. if (appliedTags.length !== 0) {
  107. self.show();
  108. } else {
  109. self.hide();
  110. }
  111. }
  112. });
  113. }
  114. this.hide();
  115. },
  116. /**
  117. * Renders this details view
  118. */
  119. render: function() {
  120. var self = this;
  121. this.$el.append(this._inputView.$el);
  122. this._inputView.render();
  123. },
  124. isVisible: function() {
  125. return !this.$el.hasClass('hidden');
  126. },
  127. show: function() {
  128. this.$el.removeClass('hidden');
  129. },
  130. hide: function() {
  131. this.$el.addClass('hidden');
  132. },
  133. openDropdown: function() {
  134. this.$el.find('.systemTagsInputField').select2('open');
  135. },
  136. remove: function() {
  137. this._inputView.remove();
  138. }
  139. });
  140. OCA.SystemTags.SystemTagsInfoView = SystemTagsInfoView;
  141. })(OCA);