summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/todos.js.coffee
blob: b68c143b4bbf8e1b96f3825796a958aa7d1147b8 (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
class @Todos
  _this = null;
  constructor: (@name) ->
    _this = @
    @initBtnListeners()
    
  initBtnListeners: ->
    $('.done-todo').on('click', @doneClicked)
    
  doneClicked: (e) ->
    $this = $(this)
    doneURL = $this.attr('href')
    e.preventDefault()
    e.stopImmediatePropagation()
    $spinner = $('<i></i>').addClass('fa fa-spinner fa-spin')
    $this.addClass("disabled")
    $this.append($spinner)
    $.ajax
      type: 'POST'
      url: doneURL
      dataType: 'json'
      data: '_method': 'delete'
      error: (data, textStatus, jqXHR) ->
        new Flash('Unable to update your todos.', 'alert')
        _this.clearDone($this.closest('li'))
        return

      success: (data, textStatus, jqXHR) ->
        new Flash(data.notice, 'success')
        _this.clearDone($this.closest('li'))
        return

  clearDone: ($row) ->
    $ul = $row.closest('ul')
    $row.remove()
    if not $ul.find('li').length
      Turbolinks.visit(location.href)
    else
      $pendingBadge = $('.todos-pending .badge')
      $pendingBadge.text parseInt($pendingBadge.text()) - 1

      $doneBadge = $('.todos-done .badge')
      $doneBadge.text parseInt($doneBadge.text()) + 1

      $mainTodosPendingBadge = $('.todos-pending-count')
      $mainTodosPendingBadge.text parseInt($mainTodosPendingBadge.text()) - 1
    return