sidebarCustomFields.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. BlazeComponent.extendComponent({
  2. customFields() {
  3. return CustomFields.find({
  4. boardId: 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 .primary'(evt) {
  76. evt.preventDefault();
  77. const data = {
  78. boardId: Session.get('currentBoard'),
  79. name: this.find('.js-field-name').value.trim(),
  80. type: this.type.get(),
  81. settings: this.getSettings(),
  82. showOnCard: this.find('.js-field-show-on-card.is-checked') !== null,
  83. };
  84. // insert or update
  85. if (!this.data()._id) {
  86. CustomFields.insert(data);
  87. } else {
  88. CustomFields.update(this.data()._id, {$set: data});
  89. }
  90. Popup.back();
  91. },
  92. 'click .js-delete-custom-field': Popup.afterConfirm('deleteCustomField', function() {
  93. const customFieldId = this._id;
  94. CustomFields.remove(customFieldId);
  95. Popup.close();
  96. }),
  97. }];
  98. },
  99. });
  100. CreateCustomFieldPopup.register('createCustomFieldPopup');
  101. (class extends CreateCustomFieldPopup {
  102. template() {
  103. return 'createCustomFieldPopup';
  104. }
  105. }).register('editCustomFieldPopup');
  106. /*Template.deleteCustomFieldPopup.events({
  107. 'submit'(evt) {
  108. const customFieldId = this._id;
  109. CustomFields.remove(customFieldId);
  110. Popup.close();
  111. }
  112. });*/