summaryrefslogtreecommitdiffstats
path: root/client/components/sidebar/sidebarCustomFields.js
blob: e56d744eab1c2d33f1949a660afafe2ffd4aa934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
BlazeComponent.extendComponent({

  customFields() {
    return CustomFields.find({
      boardId: Session.get('currentBoard'),
    });
  },

  events() {
    return [{
      'click .js-open-create-custom-field': Popup.open('createCustomField'),
      'click .js-edit-custom-field': Popup.open('editCustomField'),
    }];
  },

}).register('customFieldsSidebar');

const CreateCustomFieldPopup = BlazeComponent.extendComponent({

  _types: ['text', 'number', 'date', 'dropdown'],

  onCreated() {
    this.type = new ReactiveVar((this.data().type) ? this.data().type : this._types[0]);
    this.dropdownItems = new ReactiveVar((this.data().settings && this.data().settings.dropdownItems) ? this.data().settings.dropdownItems : []);
  },

  types() {
    const currentType = this.data().type;
    return this._types.
      map((type) => {return {
        value: type,
        name: TAPi18n.__(`custom-field-${type}`),
        selected: type === currentType,
      };});
  },

  isTypeNotSelected(type) {
    return this.type.get() !== type;
  },

  getDropdownItems() {
    const items = this.dropdownItems.get();
    Array.from(this.findAll('.js-field-settings-dropdown input')).forEach((el, index) => {
      //console.log('each item!', index, el.value);
      if (!items[index]) items[index] = {
        _id: Random.id(6),
      };
      items[index].name = el.value.trim();
    });
    return items;
  },

  getSettings() {
    const settings = {};
    switch (this.type.get()) {
    case 'dropdown': {
      const dropdownItems = this.getDropdownItems().filter((item) => !!item.name.trim());
      settings.dropdownItems = dropdownItems;
      break;
    }
    }
    return settings;
  },

  events() {
    return [{
      'change .js-field-type'(evt) {
        const value = evt.target.value;
        this.type.set(value);
      },
      'keydown .js-dropdown-item.last'(evt) {
        if (evt.target.value.trim() && evt.keyCode === 13) {
          const items = this.getDropdownItems();
          this.dropdownItems.set(items);
          evt.target.value = '';
        }
      },
      'click .js-field-show-on-card'(evt) {
        let $target = $(evt.target);
        if(!$target.hasClass('js-field-show-on-card')){
          $target = $target.parent();
        }
        $target.find('.materialCheckBox').toggleClass('is-checked');
        $target.toggleClass('is-checked');
      },
      'click .primary'(evt) {
        evt.preventDefault();

        const data = {
          boardId: Session.get('currentBoard'),
          name: this.find('.js-field-name').value.trim(),
          type: this.type.get(),
          settings: this.getSettings(),
          showOnCard: this.find('.js-field-show-on-card.is-checked') !== null,
        };

        // insert or update
        if (!this.data()._id) {
          CustomFields.insert(data);
        } else {
          CustomFields.update(this.data()._id, {$set: data});
        }

        Popup.back();
      },
      'click .js-delete-custom-field': Popup.afterConfirm('deleteCustomField', function() {
        const customFieldId = this._id;
        CustomFields.remove(customFieldId);
        Popup.close();
      }),
    }];
  },

});
CreateCustomFieldPopup.register('createCustomFieldPopup');

(class extends CreateCustomFieldPopup {

  template() {
    return 'createCustomFieldPopup';
  }

}).register('editCustomFieldPopup');

/*Template.deleteCustomFieldPopup.events({
  'submit'(evt) {
    const customFieldId = this._id;
    CustomFields.remove(customFieldId);
    Popup.close();
  }
});*/