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.

admin.js 4.4KB

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