admin.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  3. * @copyright Copyright (c) 2019 Gary Kim <gary@garykim.dev>
  4. *
  5. * @license AGPL-3.0-or-later
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. (function() {
  22. if (!OCA.SystemTags) {
  23. /**
  24. * @namespace
  25. */
  26. OCA.SystemTags = {};
  27. }
  28. OCA.SystemTags.Admin = {
  29. collection: null,
  30. init: function() {
  31. var self = this;
  32. this.collection = OC.SystemTags.collection;
  33. this.collection.fetch({
  34. success: function() {
  35. $('#systemtag').select2(_.extend(self.select2));
  36. $('#systemtag').parent().children('.select2-container').attr('aria-expanded', 'false')
  37. }
  38. });
  39. var self = this;
  40. $('#systemtag_name').on('keyup', function(e) {
  41. if (e.which === 13) {
  42. _.bind(self._onClickSubmit, self)();
  43. }
  44. });
  45. $('#systemtag_submit').on('click', _.bind(this._onClickSubmit, this));
  46. $('#systemtag_delete').on('click', _.bind(this._onClickDelete, this));
  47. $('#systemtag_reset').on('click', _.bind(this._onClickReset, this));
  48. $('#systemtag').select2(_.extend(self.select2)).on('select2-open', () => {
  49. $('.select2-container').attr('aria-expanded', 'true')
  50. });
  51. $('#systemtag').select2(_.extend(self.select2)).on('select2-close', () => {
  52. $('.select2-container').attr('aria-expanded', 'false')
  53. });
  54. },
  55. /**
  56. * Selecting a systemtag in select2
  57. *
  58. * @param {OC.SystemTags.SystemTagModel} tag
  59. */
  60. onSelectTag: function (tag) {
  61. var level = 0;
  62. if (tag.get('userVisible')) {
  63. level += 2;
  64. if (tag.get('userAssignable')) {
  65. level += 1;
  66. }
  67. }
  68. $('#systemtag_name').val(tag.get('name'));
  69. $('#systemtag_level').val(level);
  70. this._prepareForm(tag.get('id'));
  71. },
  72. /**
  73. * Clicking the "Create"/"Update" button
  74. */
  75. _onClickSubmit: function () {
  76. var level = parseInt($('#systemtag_level').val(), 10),
  77. tagId = $('#systemtags').attr('data-systemtag-id');
  78. var data = {
  79. name: $('#systemtag_name').val(),
  80. userVisible: level === 2 || level === 3,
  81. userAssignable: level === 3
  82. };
  83. if (!data.name) {
  84. OCP.Toast.error(t('systemtags_manager', 'Tag name is empty'));
  85. return;
  86. }
  87. if (tagId) {
  88. var model = this.collection.get(tagId);
  89. model.save(data);
  90. } else {
  91. this.collection.create(data);
  92. }
  93. this._onClickReset();
  94. },
  95. /**
  96. * Clicking the "Delete" button
  97. */
  98. _onClickDelete: function () {
  99. var tagId = $('#systemtags').attr('data-systemtag-id');
  100. var model = this.collection.get(tagId);
  101. model.destroy();
  102. this._onClickReset();
  103. },
  104. /**
  105. * Clicking the "Reset" button
  106. */
  107. _onClickReset: function () {
  108. $('#systemtag_name').val('');
  109. $('#systemtag_level').val(3);
  110. this._prepareForm(0);
  111. },
  112. /**
  113. * Prepare the form for create/update
  114. *
  115. * @param {number} tagId
  116. */
  117. _prepareForm: function (tagId) {
  118. if (tagId > 0) {
  119. $('#systemtags').attr('data-systemtag-id', tagId);
  120. $('#systemtag_delete').removeClass('hidden');
  121. $('#systemtag_submit span').text(t('systemtags_manager', 'Update'));
  122. $('#systemtag_create').addClass('hidden');
  123. } else {
  124. $('#systemtag').select2('val', '');
  125. $('#systemtags').attr('data-systemtag-id', '');
  126. $('#systemtag_delete').addClass('hidden');
  127. $('#systemtag_submit span').text(t('systemtags_manager', 'Create'));
  128. $('#systemtag_create').removeClass('hidden');
  129. }
  130. },
  131. /**
  132. * Select2 options for the SystemTag dropdown
  133. */
  134. select2: {
  135. allowClear: false,
  136. multiple: false,
  137. placeholder: t('systemtags_manager', 'Select tag …'),
  138. query: _.debounce(function(query) {
  139. query.callback({
  140. results: OCA.SystemTags.Admin.collection.filterByName(query.term)
  141. });
  142. }, 100, true),
  143. id: function(element) {
  144. return element;
  145. },
  146. initSelection: function(element, callback) {
  147. var selection = ($(element).val() || []).split('|').sort();
  148. callback(selection);
  149. },
  150. formatResult: function (tag) {
  151. return OC.SystemTags.getDescriptiveTag(tag);
  152. },
  153. formatSelection: function (tag) {
  154. OCA.SystemTags.Admin.onSelectTag(tag);
  155. return OC.SystemTags.getDescriptiveTag(tag);
  156. },
  157. escapeMarkup: function(m) {
  158. return m;
  159. },
  160. sortResults: function(results) {
  161. results.sort(function(a, b) {
  162. return OC.Util.naturalSortCompare(a.get('name'), b.get('name'));
  163. });
  164. return results;
  165. }
  166. }
  167. };
  168. })();
  169. window.addEventListener('DOMContentLoaded', function() {
  170. if (!window.TESTING) {
  171. OCA.SystemTags.Admin.init();
  172. }
  173. });