BigW Consortium Gitlab

todos.js.es6 4.28 KB
Newer Older
1
/* eslint-disable */
2
((global) => {
Bryce Johnson committed
3 4

  class Todos {
Bryce Johnson committed
5
    constructor({ el } = {}) {
Bryce Johnson committed
6 7
      this.allDoneClicked = this.allDoneClicked.bind(this);
      this.doneClicked = this.doneClicked.bind(this);
8
      this.el = el || $('.js-todos-options');
9
      this.perPage = this.el.data('perPage');
Fatih Acet committed
10 11
      this.clearListeners();
      this.initBtnListeners();
12
      this.initFilters();
Fatih Acet committed
13 14
    }

Bryce Johnson committed
15
    clearListeners() {
Fatih Acet committed
16 17 18
      $('.done-todo').off('click');
      $('.js-todos-mark-all').off('click');
      return $('.todo').off('click');
Bryce Johnson committed
19
    }
Fatih Acet committed
20

Bryce Johnson committed
21
    initBtnListeners() {
Fatih Acet committed
22 23 24
      $('.done-todo').on('click', this.doneClicked);
      $('.js-todos-mark-all').on('click', this.allDoneClicked);
      return $('.todo').on('click', this.goToTodoUrl);
Bryce Johnson committed
25
    }
Fatih Acet committed
26

Bryce Johnson committed
27
    initFilters() {
28
      new UsersSelect();
Luke Bennett committed
29
      this.initFilterDropdown($('.js-project-search'), 'project_id', ['text']);
30 31
      this.initFilterDropdown($('.js-type-search'), 'type');
      this.initFilterDropdown($('.js-action-search'), 'action_id');
32 33 34 35 36

      $('form.filter-form').on('submit', function (event) {
        event.preventDefault();
        Turbolinks.visit(this.action + '&' + $(this).serialize());
      });
Bryce Johnson committed
37
    }
38

Bryce Johnson committed
39
    initFilterDropdown($dropdown, fieldName, searchFields) {
40
      $dropdown.glDropdown({
Bryce Johnson committed
41
        fieldName,
42
        selectable: true,
Luke Bennett committed
43 44
        filterable: searchFields ? true : false,
        search: { fields: searchFields },
45
        data: $dropdown.data('data'),
46
        clicked: function() {
47
          return $dropdown.closest('form.filter-form').submit();
48
        }
49
      })
Bryce Johnson committed
50
    }
51

Bryce Johnson committed
52
    doneClicked(e) {
Fatih Acet committed
53 54
      e.preventDefault();
      e.stopImmediatePropagation();
Bryce Johnson committed
55 56
      const $target = $(e.currentTarget);
      $target.disable();
Fatih Acet committed
57 58
      return $.ajax({
        type: 'POST',
Bryce Johnson committed
59
        url: $target.attr('href'),
Fatih Acet committed
60 61 62 63
        dataType: 'json',
        data: {
          '_method': 'delete'
        },
64
        success: (data) => {
Bryce Johnson committed
65 66 67 68
          this.redirectIfNeeded(data.count);
          this.clearDone($target.closest('li'));
          return this.updateBadges(data);
        }
Fatih Acet committed
69
      });
Bryce Johnson committed
70
    }
Fatih Acet committed
71

Bryce Johnson committed
72
    allDoneClicked(e) {
Fatih Acet committed
73 74
      e.preventDefault();
      e.stopImmediatePropagation();
Bryce Johnson committed
75 76
      $target = $(e.currentTarget);
      $target.disable();
Fatih Acet committed
77 78
      return $.ajax({
        type: 'POST',
Bryce Johnson committed
79
        url: $target.attr('href'),
Fatih Acet committed
80 81 82 83
        dataType: 'json',
        data: {
          '_method': 'delete'
        },
84
        success: (data) => {
Bryce Johnson committed
85 86 87 88
          $target.remove();
          $('.prepend-top-default').html('<div class="nothing-here-block">You\'re all done!</div>');
          return this.updateBadges(data);
        }
Fatih Acet committed
89
      });
Bryce Johnson committed
90
    }
Fatih Acet committed
91

Bryce Johnson committed
92 93
    clearDone($row) {
      const $ul = $row.closest('ul');
Fatih Acet committed
94 95 96 97
      $row.remove();
      if (!$ul.find('li').length) {
        return $ul.parents('.panel').remove();
      }
Bryce Johnson committed
98
    }
Fatih Acet committed
99

Bryce Johnson committed
100
    updateBadges(data) {
Clement Ho committed
101 102
      $(document).trigger('todo:toggle', data.count);
      $('.todos-pending .badge').text(data.count);
Fatih Acet committed
103
      return $('.todos-done .badge').text(data.done_count);
Bryce Johnson committed
104
    }
Fatih Acet committed
105

Bryce Johnson committed
106
    getTotalPages() {
Fatih Acet committed
107
      return this.el.data('totalPages');
Bryce Johnson committed
108
    }
Fatih Acet committed
109

Bryce Johnson committed
110
    getCurrentPage() {
Fatih Acet committed
111
      return this.el.data('currentPage');
Bryce Johnson committed
112
    }
Fatih Acet committed
113

Bryce Johnson committed
114
    getTodosPerPage() {
Fatih Acet committed
115
      return this.el.data('perPage');
Bryce Johnson committed
116
    }
Fatih Acet committed
117

Bryce Johnson committed
118
    redirectIfNeeded(total) {
119 120
      const currPages = this.getTotalPages();
      const currPage = this.getCurrentPage();
Bryce Johnson committed
121

122
      // Refresh if no remaining Todos
Fatih Acet committed
123
      if (!total) {
Bryce Johnson committed
124
        window.location.reload();
Fatih Acet committed
125 126
        return;
      }
127
      // Do nothing if no pagination
Fatih Acet committed
128 129 130
      if (!currPages) {
        return;
      }
Bryce Johnson committed
131 132 133 134

      const newPages = Math.ceil(total / this.getTodosPerPage());
      let url = location.href;

Fatih Acet committed
135
      if (newPages !== currPages) {
136
        // Redirect to previous page if there's one available
Fatih Acet committed
137
        if (currPages > 1 && currPage === currPages) {
Bryce Johnson committed
138
          const pageParams = {
Fatih Acet committed
139 140 141 142 143 144
            page: currPages - 1
          };
          url = gl.utils.mergeUrlParams(pageParams, url);
        }
        return Turbolinks.visit(url);
      }
Bryce Johnson committed
145
    }
Fatih Acet committed
146

Bryce Johnson committed
147 148
    goToTodoUrl(e) {
      const todoLink = $(this).data('url');
Fatih Acet committed
149 150 151
      if (!todoLink) {
        return;
      }
152
      // Allow Meta-Click or Mouse3-click to open in a new tab
Fatih Acet committed
153 154 155 156 157 158
      if (e.metaKey || e.which === 2) {
        e.preventDefault();
        return window.open(todoLink, '_blank');
      } else {
        return Turbolinks.visit(todoLink);
      }
Bryce Johnson committed
159 160
    }
  }
Fatih Acet committed
161

Bryce Johnson committed
162 163
  global.Todos = Todos;
})(window.gl || (window.gl = {}));