sidebarCustomFields.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. BlazeComponent.extendComponent({
  2. customFields() {
  3. return CustomFields.find({
  4. boardIds: { $in: [Session.get('currentBoard')] },
  5. });
  6. },
  7. events() {
  8. return [
  9. {
  10. 'click .js-open-create-custom-field': Popup.open('createCustomField'),
  11. 'click .js-edit-custom-field': Popup.open('editCustomField'),
  12. },
  13. ];
  14. },
  15. }).register('customFieldsSidebar');
  16. const CreateCustomFieldPopup = BlazeComponent.extendComponent({
  17. _types: ['text', 'number', 'date', 'dropdown'],
  18. onCreated() {
  19. this.type = new ReactiveVar(
  20. this.data().type ? this.data().type : this._types[0],
  21. );
  22. this.dropdownItems = new ReactiveVar(
  23. this.data().settings && this.data().settings.dropdownItems
  24. ? this.data().settings.dropdownItems
  25. : [],
  26. );
  27. },
  28. types() {
  29. const currentType = this.data().type;
  30. return this._types.map(type => {
  31. return {
  32. value: type,
  33. name: TAPi18n.__(`custom-field-${type}`),
  34. selected: type === currentType,
  35. };
  36. });
  37. },
  38. isTypeNotSelected(type) {
  39. return this.type.get() !== type;
  40. },
  41. getDropdownItems() {
  42. const items = this.dropdownItems.get();
  43. Array.from(this.findAll('.js-field-settings-dropdown input')).forEach(
  44. (el, index) => {
  45. //console.log('each item!', index, el.value);
  46. if (!items[index])
  47. items[index] = {
  48. _id: Random.id(6),
  49. };
  50. items[index].name = el.value.trim();
  51. },
  52. );
  53. return items;
  54. },
  55. getSettings() {
  56. const settings = {};
  57. switch (this.type.get()) {
  58. case 'dropdown': {
  59. const dropdownItems = this.getDropdownItems().filter(
  60. item => !!item.name.trim(),
  61. );
  62. settings.dropdownItems = dropdownItems;
  63. break;
  64. }
  65. }
  66. return settings;
  67. },
  68. events() {
  69. return [
  70. {
  71. 'change .js-field-type'(evt) {
  72. const value = evt.target.value;
  73. this.type.set(value);
  74. },
  75. 'keydown .js-dropdown-item.last'(evt) {
  76. if (evt.target.value.trim() && evt.keyCode === 13) {
  77. const items = this.getDropdownItems();
  78. this.dropdownItems.set(items);
  79. evt.target.value = '';
  80. }
  81. },
  82. 'click .js-field-show-on-card'(evt) {
  83. let $target = $(evt.target);
  84. if (!$target.hasClass('js-field-show-on-card')) {
  85. $target = $target.parent();
  86. }
  87. $target.find('.materialCheckBox').toggleClass('is-checked');
  88. $target.toggleClass('is-checked');
  89. },
  90. 'click .js-field-automatically-on-card'(evt) {
  91. let $target = $(evt.target);
  92. if (!$target.hasClass('js-field-automatically-on-card')) {
  93. $target = $target.parent();
  94. }
  95. $target.find('.materialCheckBox').toggleClass('is-checked');
  96. $target.toggleClass('is-checked');
  97. },
  98. 'click .js-field-showLabel-on-card'(evt) {
  99. let $target = $(evt.target);
  100. if (!$target.hasClass('js-field-showLabel-on-card')) {
  101. $target = $target.parent();
  102. }
  103. $target.find('.materialCheckBox').toggleClass('is-checked');
  104. $target.toggleClass('is-checked');
  105. },
  106. 'click .primary'(evt) {
  107. evt.preventDefault();
  108. const data = {
  109. name: this.find('.js-field-name').value.trim(),
  110. type: this.type.get(),
  111. settings: this.getSettings(),
  112. showOnCard: this.find('.js-field-show-on-card.is-checked') !== null,
  113. showLabelOnMiniCard:
  114. this.find('.js-field-showLabel-on-card.is-checked') !== null,
  115. automaticallyOnCard:
  116. this.find('.js-field-automatically-on-card.is-checked') !== null,
  117. };
  118. // insert or update
  119. if (!this.data()._id) {
  120. data.boardIds = [Session.get('currentBoard')];
  121. CustomFields.insert(data);
  122. } else {
  123. CustomFields.update(this.data()._id, { $set: data });
  124. }
  125. Popup.back();
  126. },
  127. 'click .js-delete-custom-field': Popup.afterConfirm(
  128. 'deleteCustomField',
  129. function() {
  130. const customField = CustomFields.findOne(this._id);
  131. if (customField.boardIds.length > 1) {
  132. CustomFields.update(customField._id, {
  133. $pull: {
  134. boardIds: Session.get('currentBoard'),
  135. },
  136. });
  137. } else {
  138. CustomFields.remove(customField._id);
  139. }
  140. Popup.close();
  141. },
  142. ),
  143. },
  144. ];
  145. },
  146. });
  147. CreateCustomFieldPopup.register('createCustomFieldPopup');
  148. (class extends CreateCustomFieldPopup {
  149. template() {
  150. return 'createCustomFieldPopup';
  151. }
  152. }.register('editCustomFieldPopup'));
  153. /*Template.deleteCustomFieldPopup.events({
  154. 'submit'(evt) {
  155. const customFieldId = this._id;
  156. CustomFields.remove(customFieldId);
  157. Popup.close();
  158. }
  159. });*/