todos.js 4.2 KB
Newer Older
J
Jacopo 已提交
1
/* eslint-disable class-methods-use-this, no-new, func-names, no-unneeded-ternary, object-shorthand, quote-props, no-param-reassign, max-len */
2 3
/* global UsersSelect */

4
((global) => {
B
Bryce Johnson 已提交
5
  class Todos {
J
Jacopo 已提交
6
    constructor() {
7
      this.initFilters();
J
Jacopo 已提交
8 9 10 11
      this.bindEvents();

      this.cleanupWrapper = this.cleanup.bind(this);
      document.addEventListener('beforeunload', this.cleanupWrapper);
F
Fatih Acet 已提交
12 13
    }

J
Jacopo 已提交
14 15 16
    cleanup() {
      this.unbindEvents();
      document.removeEventListener('beforeunload', this.cleanupWrapper);
B
Bryce Johnson 已提交
17
    }
F
Fatih Acet 已提交
18

J
Jacopo 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
    unbindEvents() {
      $('.js-done-todo, .js-undo-todo').off('click', this.updateStateClickedWrapper);
      $('.js-todos-mark-all').off('click', this.allDoneClickedWrapper);
      $('.todo').off('click', this.goToTodoUrl);
    }

    bindEvents() {
      this.updateStateClickedWrapper = this.updateStateClicked.bind(this);
      this.allDoneClickedWrapper = this.allDoneClicked.bind(this);

      $('.js-done-todo, .js-undo-todo').on('click', this.updateStateClickedWrapper);
      $('.js-todos-mark-all').on('click', this.allDoneClickedWrapper);
      $('.todo').on('click', this.goToTodoUrl);
B
Bryce Johnson 已提交
32
    }
F
Fatih Acet 已提交
33

B
Bryce Johnson 已提交
34
    initFilters() {
35
      new UsersSelect();
L
Luke Bennett 已提交
36
      this.initFilterDropdown($('.js-project-search'), 'project_id', ['text']);
37 38
      this.initFilterDropdown($('.js-type-search'), 'type');
      this.initFilterDropdown($('.js-action-search'), 'action_id');
39 40 41

      $('form.filter-form').on('submit', function (event) {
        event.preventDefault();
J
Jacopo 已提交
42
        gl.utils.visitUrl(`${this.action}&${$(this).serialize()}`);
43
      });
B
Bryce Johnson 已提交
44
    }
45

B
Bryce Johnson 已提交
46
    initFilterDropdown($dropdown, fieldName, searchFields) {
47
      $dropdown.glDropdown({
B
Bryce Johnson 已提交
48
        fieldName,
49
        selectable: true,
L
Luke Bennett 已提交
50 51
        filterable: searchFields ? true : false,
        search: { fields: searchFields },
52
        data: $dropdown.data('data'),
J
Jacopo 已提交
53
        clicked: function () {
54
          return $dropdown.closest('form.filter-form').submit();
J
Jacopo 已提交
55
        },
56
      });
B
Bryce Johnson 已提交
57
    }
58

J
Jacopo 已提交
59
    updateStateClicked(e) {
F
Fatih Acet 已提交
60
      e.preventDefault();
J
Jacopo 已提交
61 62 63 64
      const target = e.target;
      target.setAttribute('disabled', '');
      target.classList.add('disabled');
      $.ajax({
F
Fatih Acet 已提交
65
        type: 'POST',
J
Jacopo 已提交
66
        url: target.getAttribute('href'),
F
Fatih Acet 已提交
67 68
        dataType: 'json',
        data: {
J
Jacopo 已提交
69
          '_method': target.getAttribute('data-method'),
F
Fatih Acet 已提交
70
        },
71
        success: (data) => {
J
Jacopo 已提交
72 73 74
          this.updateState(target);
          this.updateBadges(data);
        },
F
Fatih Acet 已提交
75
      });
B
Bryce Johnson 已提交
76
    }
F
Fatih Acet 已提交
77

B
Bryce Johnson 已提交
78
    allDoneClicked(e) {
F
Fatih Acet 已提交
79
      e.preventDefault();
80
      const $target = $(e.currentTarget);
B
Bryce Johnson 已提交
81
      $target.disable();
J
Jacopo 已提交
82
      $.ajax({
F
Fatih Acet 已提交
83
        type: 'POST',
B
Bryce Johnson 已提交
84
        url: $target.attr('href'),
F
Fatih Acet 已提交
85 86
        dataType: 'json',
        data: {
J
Jacopo 已提交
87
          '_method': 'delete',
F
Fatih Acet 已提交
88
        },
89
        success: (data) => {
B
Bryce Johnson 已提交
90
          $target.remove();
91
          $('.js-todos-all').html('<div class="nothing-here-block">You\'re all done!</div>');
J
Jacopo 已提交
92 93
          this.updateBadges(data);
        },
F
Fatih Acet 已提交
94
      });
B
Bryce Johnson 已提交
95
    }
F
Fatih Acet 已提交
96

J
Jacopo 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    updateState(target) {
      const row = target.closest('li');
      const restoreBtn = row.querySelector('.js-undo-todo');
      const doneBtn = row.querySelector('.js-done-todo');

      target.removeAttribute('disabled');
      target.classList.remove('disabled');
      target.classList.add('hidden');

      if (target === doneBtn) {
        row.classList.add('done-reversible');
        restoreBtn.classList.remove('hidden');
      } else {
        row.classList.remove('done-reversible');
        doneBtn.classList.remove('hidden');
F
Fatih Acet 已提交
112
      }
B
Bryce Johnson 已提交
113
    }
F
Fatih Acet 已提交
114

B
Bryce Johnson 已提交
115
    updateBadges(data) {
C
Clement Ho 已提交
116 117
      $(document).trigger('todo:toggle', data.count);
      $('.todos-pending .badge').text(data.count);
J
Jacopo 已提交
118
      $('.todos-done .badge').text(data.done_count);
B
Bryce Johnson 已提交
119
    }
F
Fatih Acet 已提交
120

B
Bryce Johnson 已提交
121
    goToTodoUrl(e) {
122
      const todoLink = this.dataset.url;
K
Kushal Pandya 已提交
123

F
Fatih Acet 已提交
124 125 126
      if (!todoLink) {
        return;
      }
K
Kushal Pandya 已提交
127

128
      if (gl.utils.isMetaClick(e)) {
129 130
        const windowTarget = '_blank';
        const selected = e.target;
F
Fatih Acet 已提交
131
        e.preventDefault();
132 133 134

        if (selected.tagName === 'IMG') {
          const avatarUrl = selected.parentElement.getAttribute('href');
J
Jacopo 已提交
135
          window.open(avatarUrl, windowTarget);
K
Kushal Pandya 已提交
136
        } else {
J
Jacopo 已提交
137
          window.open(todoLink, windowTarget);
K
Kushal Pandya 已提交
138
        }
F
Fatih Acet 已提交
139
      } else {
J
Jacopo 已提交
140
        gl.utils.visitUrl(todoLink);
F
Fatih Acet 已提交
141
      }
B
Bryce Johnson 已提交
142 143
    }
  }
F
Fatih Acet 已提交
144

B
Bryce Johnson 已提交
145 146
  global.Todos = Todos;
})(window.gl || (window.gl = {}));