BigW Consortium Gitlab

todos.js.es6 4.22 KB
Newer Older
1
((global) => {
Bryce Johnson committed
2 3

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

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

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

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

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

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

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

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

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

Bryce Johnson committed
99
    updateBadges(data) {
Fatih Acet committed
100 101
      $('.todos-pending .badge, .todos-pending-count').text(data.count);
      return $('.todos-done .badge').text(data.done_count);
Bryce Johnson committed
102
    }
Fatih Acet committed
103

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

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

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

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

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

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

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

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

Bryce Johnson committed
160 161
  global.Todos = Todos;
})(window.gl || (window.gl = {}));