sidebarCustomFields.js 4.5 KB

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