sidebarCustomFields.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. var 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. let settings = {};
  45. switch (this.type.get()) {
  46. case 'dropdown':
  47. let dropdownItems = this.getDropdownItems().filter(item => !!item.name.trim());
  48. settings.dropdownItems = dropdownItems;
  49. break;
  50. }
  51. return settings;
  52. },
  53. events() {
  54. return [{
  55. 'change .js-field-type'(evt) {
  56. const value = evt.target.value;
  57. this.type.set(value);
  58. },
  59. 'keydown .js-dropdown-item.last'(evt) {
  60. if (evt.target.value.trim() && evt.keyCode === 13) {
  61. let items = this.getDropdownItems();
  62. this.dropdownItems.set(items);
  63. evt.target.value = '';
  64. }
  65. },
  66. 'click .js-field-show-on-card'(evt) {
  67. let $target = $(evt.target);
  68. if(!$target.hasClass('js-field-show-on-card')){
  69. $target = $target.parent();
  70. }
  71. $target.find('.materialCheckBox').toggleClass('is-checked');
  72. $target.toggleClass('is-checked');
  73. },
  74. 'click .primary'(evt) {
  75. evt.preventDefault();
  76. const data = {
  77. boardId: Session.get('currentBoard'),
  78. name: this.find('.js-field-name').value.trim(),
  79. type: this.type.get(),
  80. settings: this.getSettings(),
  81. showOnCard: this.find('.js-field-show-on-card.is-checked') != null
  82. }
  83. // insert or update
  84. if (!this.data()._id) {
  85. CustomFields.insert(data);
  86. } else {
  87. CustomFields.update(this.data()._id, {$set: data});
  88. }
  89. Popup.back();
  90. },
  91. 'click .js-delete-custom-field': Popup.afterConfirm('deleteCustomField', function() {
  92. const customFieldId = this._id;
  93. CustomFields.remove(customFieldId);
  94. Popup.close();
  95. }),
  96. }];
  97. },
  98. });
  99. CreateCustomFieldPopup.register('createCustomFieldPopup');
  100. (class extends CreateCustomFieldPopup {
  101. template() {
  102. return 'createCustomFieldPopup';
  103. }
  104. }).register('editCustomFieldPopup');
  105. /*Template.deleteCustomFieldPopup.events({
  106. 'submit'(evt) {
  107. const customFieldId = this._id;
  108. CustomFields.remove(customFieldId);
  109. Popup.close();
  110. }
  111. });*/