todos.js 5.6 KB
Newer Older
F
Fatih Acet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(function() {
  var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  this.Todos = (function() {
    function Todos(opts) {
      var ref;
      if (opts == null) {
        opts = {};
      }
      this.allDoneClicked = bind(this.allDoneClicked, this);
      this.doneClicked = bind(this.doneClicked, this);
      this.el = (ref = opts.el) != null ? ref : $('.js-todos-options');
      this.perPage = this.el.data('perPage');
      this.clearListeners();
      this.initBtnListeners();
16
      this.initFilters();
F
Fatih Acet 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30
    }

    Todos.prototype.clearListeners = function() {
      $('.done-todo').off('click');
      $('.js-todos-mark-all').off('click');
      return $('.todo').off('click');
    };

    Todos.prototype.initBtnListeners = function() {
      $('.done-todo').on('click', this.doneClicked);
      $('.js-todos-mark-all').on('click', this.allDoneClicked);
      return $('.todo').on('click', this.goToTodoUrl);
    };

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
    Todos.prototype.initFilters = function() {
      new UsersSelect();
      this.initProjectFilterDropdown();
      this.initTypeFilterDropdown();
      this.initActionFilterDropdown();

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

    Todos.prototype.initProjectFilterDropdown = function() {
      $projectDropdown = $('.js-project-search');
      $projectDropdown.glDropdown({
        filterable: true,
        selectable: true,
        fieldName: 'project_id',
        data: $projectDropdown.data('data'),
        clicked: function() {
          if ($projectDropdown.hasClass('js-filter-submit')) {
            return $projectDropdown.closest('form.filter-form').submit();
          }
        }
      });
    };

    Todos.prototype.initTypeFilterDropdown = function() {
      $typeDropdown = $('.js-type-search');
      $typeDropdown.glDropdown({
        selectable: true,
        fieldName: 'type',
        data: $typeDropdown.data('data'),
        clicked: function() {
          if ($typeDropdown.hasClass('js-filter-submit')) {
            return $typeDropdown.closest('form.filter-form').submit();
          }
        }
      });
    };

    Todos.prototype.initActionFilterDropdown = function() {
      $actionDropdown = $('.js-action-search');
      $actionDropdown.glDropdown({
        selectable: true,
        fieldName: 'action_id',
        data: $actionDropdown.data('data'),
        clicked: function() {
          if ($actionDropdown.hasClass('js-filter-submit')) {
            return $actionDropdown.closest('form.filter-form').submit();
          }
        }
      });
    };

F
Fatih Acet 已提交
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
    Todos.prototype.doneClicked = function(e) {
      var $this;
      e.preventDefault();
      e.stopImmediatePropagation();
      $this = $(e.currentTarget);
      $this.disable();
      return $.ajax({
        type: 'POST',
        url: $this.attr('href'),
        dataType: 'json',
        data: {
          '_method': 'delete'
        },
        success: (function(_this) {
          return function(data) {
            _this.redirectIfNeeded(data.count);
            _this.clearDone($this.closest('li'));
            return _this.updateBadges(data);
          };
        })(this)
      });
    };

    Todos.prototype.allDoneClicked = function(e) {
      var $this;
      e.preventDefault();
      e.stopImmediatePropagation();
      $this = $(e.currentTarget);
      $this.disable();
      return $.ajax({
        type: 'POST',
        url: $this.attr('href'),
        dataType: 'json',
        data: {
          '_method': 'delete'
        },
        success: (function(_this) {
          return function(data) {
            $this.remove();
125
            $('.prepend-top-default').html('<div class="nothing-here-block">You\'re all done!</div>');
F
Fatih Acet 已提交
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            return _this.updateBadges(data);
          };
        })(this)
      });
    };

    Todos.prototype.clearDone = function($row) {
      var $ul;
      $ul = $row.closest('ul');
      $row.remove();
      if (!$ul.find('li').length) {
        return $ul.parents('.panel').remove();
      }
    };

    Todos.prototype.updateBadges = function(data) {
      $('.todos-pending .badge, .todos-pending-count').text(data.count);
      return $('.todos-done .badge').text(data.done_count);
    };

    Todos.prototype.getTotalPages = function() {
      return this.el.data('totalPages');
    };

    Todos.prototype.getCurrentPage = function() {
      return this.el.data('currentPage');
    };

    Todos.prototype.getTodosPerPage = function() {
      return this.el.data('perPage');
    };

    Todos.prototype.redirectIfNeeded = function(total) {
      var currPage, currPages, newPages, pageParams, url;
      currPages = this.getTotalPages();
      currPage = this.getCurrentPage();
      if (!total) {
        location.reload();
        return;
      }
      if (!currPages) {
        return;
      }
      newPages = Math.ceil(total / this.getTodosPerPage());
      url = location.href;
      if (newPages !== currPages) {
        if (currPages > 1 && currPage === currPages) {
          pageParams = {
            page: currPages - 1
          };
          url = gl.utils.mergeUrlParams(pageParams, url);
        }
        return Turbolinks.visit(url);
      }
    };

    Todos.prototype.goToTodoUrl = function(e) {
      var todoLink;
      todoLink = $(this).data('url');
      if (!todoLink) {
        return;
      }
      if (e.metaKey || e.which === 2) {
        e.preventDefault();
        return window.open(todoLink, '_blank');
      } else {
        return Turbolinks.visit(todoLink);
      }
    };

    return Todos;

  })();

}).call(this);