todos.js.coffee 2.55 KB
Newer Older
1
class @Todos
2 3 4 5 6 7 8
  constructor: (opts = {}) ->
    {
      @el = $('.js-todos-options')
    } = opts

    @perPage = @el.data('perPage')

9
    @clearListeners()
10
    @initBtnListeners()
11 12 13

  clearListeners: ->
    $('.done-todo').off('click')
14
    $('.js-todos-mark-all').off('click')
Annabel Dunstone's avatar
Annabel Dunstone committed
15
    $('.todo').off('click')
16

17 18
  initBtnListeners: ->
    $('.done-todo').on('click', @doneClicked)
19
    $('.js-todos-mark-all').on('click', @allDoneClicked)
20
    $('.todo').on('click', @goToTodoUrl)
21 22

  doneClicked: (e) =>
23 24
    e.preventDefault()
    e.stopImmediatePropagation()
25 26 27 28

    $this = $(e.currentTarget)
    $this.disable()

29 30
    $.ajax
      type: 'POST'
31
      url: $this.attr('href')
32 33
      dataType: 'json'
      data: '_method': 'delete'
34
      success: (data) =>
35
        @redirectIfNeeded data.count
36 37
        @clearDone $this.closest('li')
        @updateBadges data
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  allDoneClicked: (e) =>
    e.preventDefault()
    e.stopImmediatePropagation()

    $this = $(e.currentTarget)
    $this.disable()

    $.ajax
      type: 'POST'
      url: $this.attr('href')
      dataType: 'json'
      data: '_method': 'delete'
      success: (data) =>
        $this.remove()
        $('.js-todos-list').remove()
        @updateBadges data

  clearDone: ($row) ->
57 58
    $ul = $row.closest('ul')
    $row.remove()
59

60
    if not $ul.find('li').length
61
      $ul.parents('.panel').remove()
62 63 64 65

  updateBadges: (data) ->
    $('.todos-pending .badge, .todos-pending-count').text data.count
    $('.todos-done .badge').text data.done_count
66

67 68
  getTotalPages: ->
    @el.data('totalPages')
69 70

  getCurrentPage: ->
71 72 73 74 75
    @el.data('currentPage')

  getTodosPerPage: ->
    @el.data('perPage')

76
  redirectIfNeeded: (total) ->
77
    currPages = @getTotalPages()
78 79 80
    currPage = @getCurrentPage()

    # Refresh if no remaining Todos
81
    if not total
82 83 84 85
      location.reload()
      return

    # Do nothing if no pagination
86
    return if not currPages
87

Alfredo Sumaran's avatar
Alfredo Sumaran committed
88 89 90
    newPages = Math.ceil(total / @getTodosPerPage())
    url = location.href # Includes query strings

Alfredo Sumaran's avatar
typo  
Alfredo Sumaran committed
91
    # If new total of pages is different than we have now
92
    if newPages isnt currPages
Alfredo Sumaran's avatar
typo  
Alfredo Sumaran committed
93
      # Redirect to previous page if there's one available
94
      if currPages > 1 and currPage is currPages
95 96 97
        pageParams =
          page: currPages - 1
        url = gl.utils.mergeUrlParams(pageParams, url)
98

99
      Turbolinks.visit(url)
100

101 102
  goToTodoUrl: (e)->
    todoLink = $(this).data('url')
103 104
    return unless todoLink

105 106
    # Allow Meta-Click or Mouse3-click to open in a new tab
    if e.metaKey or e.which is 2
107 108 109 110
      e.preventDefault()
      window.open(todoLink,'_blank')
    else
      Turbolinks.visit(todoLink)