summaryrefslogtreecommitdiffstats
path: root/client/components/lists/listHeader.js
blob: 9ae2c1fe236ec95574be38847f9ad5fbc542253c (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
BlazeComponent.extendComponent({
  editTitle(evt) {
    evt.preventDefault();
    const newTitle = this.childComponents('inlinedForm')[0].getValue().trim();
    const list = this.currentData();
    if (newTitle) {
      list.rename(newTitle.trim());
    }
  },

  isWipLimitEnabled() {
    const limit = this.currentData().wipLimit
    return limit.enabled && limit.value > 0;
  },

  isWatching() {
    const list = this.currentData();
    return list.findWatcher(Meteor.userId());
  },

  limitToShowCardsCount() {
    return Meteor.user().getLimitToShowCardsCount();
  },

  showCardsCountForList(count) {
    return count > this.limitToShowCardsCount();
  },

  events() {
    return [{
      'click .js-open-list-menu': Popup.open('listAction'),
      'click .js-add-card' () {
        const listDom = document.getElementById(`js-list-${this.currentData()._id}`);
        const listComponent = BlazeComponent.getComponentForElement(listDom);
        listComponent.openForm({
          position: 'top',
        });
      },
      submit: this.editTitle,
    }];
  },
}).register('listHeader');

Template.listActionPopup.helpers({
  isWipLimitEnabled() {
    return Lists.findOne(this.data()._id, { 'wipLimit.enabled': 1 }).wipLimit.enabled;
  },
  isWatching() {
    return this.findWatcher(Meteor.userId());
  },
});

Template.listActionPopup.events({
  'click .js-list-subscribe' () {},
  'click .js-select-cards' () {
    const cardIds = this.allCards().map((card) => card._id);
    MultiSelection.add(cardIds);
    Popup.close();
  },
  'click .js-toggle-watch-list' () {
    const currentList = this;
    const level = currentList.findWatcher(Meteor.userId()) ? null : 'watching';
    Meteor.call('watch', 'list', currentList._id, level, (err, ret) => {
      if (!err && ret) Popup.close();
    });
  },
  'click .js-close-list' (evt) {
    evt.preventDefault();
    this.archive();
    Popup.close();
  },
  'click .js-set-wip-limit': Popup.open('setWipLimit'),
  'click .js-more': Popup.open('listMore'),
});

Template.setWipLimitPopup.helpers({
  one() {
    //console.log(this)
    //console.log(Template.instance())
  }
});

BlazeComponent.extendComponent({
  onCreated() {
    this.wipEnabled = new ReactiveVar(Template.currentData().wipLimit.enabled);
  },

  toggleWipEnabled() {
    const list = Lists.findOne(this.data()._id);
    list.wipLimit.enabled ? list.setWipLimitDisabled() : list.setWipLimitEnabled()
  },

  isWipLimitEnabled() {
    return Lists.findOne(this.data()._id, { 'wipLimit.enabled': 1 }).wipLimit.enabled;
  },
  events() {
    return [{
      'click .js-enable-wip-limit'(_, instance) {
        //By default wipLimit.enabled is false or undefined. First click will always be to enable wip limiting
        this.wipEnabled.set(!this.wipEnabled.get());
        //console.log(Template.parentData(2))
        //Template.parentData(2).data.toggleWipLimit(!Template.currentData().wipLimit.enabled); //If wipLimit.enabled is not yet definied, the negation of "undefined" is "true"
        this.toggleWipEnabled()
      },
      'click .wip-limit-apply'(_, instance) {
        const list = Template.currentData();
        const limit = Template.instance().$('.wip-limit-value').val();

        if(limit < list.allCards().count()){
          Template.instance().$('.wip-limit-error').click();
        } else {
          list.setWipLimit(limit);
        }
      },
      'click .wip-limit-error': Popup.open('wipLimitError'),
    }];
  },
}).register('setWipLimitPopup');


/*
Template.setWipLimitPopup.helpers({
  isWipLimitEnabled(instance) {
    console.log(this);
    console.log(Template.currentData());
    console.log(instance);
    return Template.currentData().wipLimit.enabled;
  },
});

Template.setWipLimitPopup.events({
  'click .js-enable-wip-limit'(_, instance) {
    //By default wipLimit.enabled is false or undefined. First click will always be to enable wip limiting
    instance.wipEnabled.set(!instance.wipEnabled.get())
  //  list.toggleWipLimit(!list.wipLimit.enabled); //If wipLimit.enabled is not yet definied, the negation of "undefined" is "true"
  },
  'click .wip-limit-apply'(_, instance) {
    const limit = instance.$('.wip-limit-value').val();
    if(limit < this.allCards().count()){
      instance.$('.wip-limit-error').click(); //open popup with invisible button click
      return;
    }
    this.setWipLimit(limit);
  },
  'click .wip-limit-error': Popup.open('wipLimitError'),
});*/

Template.listMorePopup.events({
  'click .js-delete': Popup.afterConfirm('listDelete', function () {
    Popup.close();
    this.allCards().map((card) => Cards.remove(card._id));
    Lists.remove(this._id);
    Utils.goBoardId(this.boardId);
  }),
});