gitlab-org--gitlab-foss/app/assets/javascripts/todos.js.coffee
Jacob Schatz fa570525db Adds small AJAX optimistic functionality to todos.
Fixes #13656
A good first step and boring solution.
2016-03-17 12:20:14 +00:00

48 lines
No EOL
1.3 KiB
CoffeeScript

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