gitlab-org--gitlab-foss/app/assets/javascripts/todos.js.coffee

111 lines
2.5 KiB
CoffeeScript
Raw Normal View History

class @Todos
2016-04-01 17:55:45 +00:00
constructor: (opts = {}) ->
{
@el = $('.js-todos-options')
} = opts
@perPage = @el.data('perPage')
@clearListeners()
@initBtnListeners()
clearListeners: ->
$('.done-todo').off('click')
2016-03-17 13:08:59 +00:00
$('.js-todos-mark-all').off('click')
2016-03-30 17:28:36 +00:00
$('.todo').off('click')
initBtnListeners: ->
$('.done-todo').on('click', @doneClicked)
2016-03-17 13:08:59 +00:00
$('.js-todos-mark-all').on('click', @allDoneClicked)
2016-03-30 14:17:03 +00:00
$('.todo').on('click', @goToTodoUrl)
doneClicked: (e) =>
e.preventDefault()
e.stopImmediatePropagation()
$this = $(e.currentTarget)
$this.disable()
$.ajax
type: 'POST'
url: $this.attr('href')
dataType: 'json'
data: '_method': 'delete'
success: (data) =>
2016-03-28 22:01:44 +00:00
@redirectIfNeeded data.count
2016-03-17 13:08:59 +00:00
@clearDone $this.closest('li')
@updateBadges data
2016-03-17 13:08:59 +00:00
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) ->
$ul = $row.closest('ul')
$row.remove()
if not $ul.find('li').length
$ul.parents('.panel').remove()
2016-03-17 13:08:59 +00:00
updateBadges: (data) ->
$('.todos-pending .badge, .todos-pending-count').text data.count
$('.todos-done .badge').text data.done_count
2016-03-30 14:17:03 +00:00
2016-04-01 17:55:45 +00:00
getTotalPages: ->
@el.data('totalPages')
2016-03-28 22:01:44 +00:00
getCurrentPage: ->
2016-04-01 17:55:45 +00:00
@el.data('currentPage')
getTodosPerPage: ->
@el.data('perPage')
2016-03-28 22:01:44 +00:00
redirectIfNeeded: (total) ->
2016-04-01 17:55:45 +00:00
currPages = @getTotalPages()
2016-03-28 22:01:44 +00:00
currPage = @getCurrentPage()
# Refresh if no remaining Todos
2016-04-01 17:55:45 +00:00
if not total
2016-03-28 22:01:44 +00:00
location.reload()
return
# Do nothing if no pagination
2016-04-01 17:55:45 +00:00
return if not currPages
2016-03-28 22:01:44 +00:00
2016-04-13 15:54:51 +00:00
newPages = Math.ceil(total / @getTodosPerPage())
url = location.href # Includes query strings
2016-03-28 22:05:04 +00:00
# If new total of pages is different than we have now
2016-03-28 22:01:44 +00:00
if newPages isnt currPages
2016-03-28 22:05:04 +00:00
# Redirect to previous page if there's one available
2016-03-28 22:01:44 +00:00
if currPages > 1 and currPage is currPages
pageParams =
page: currPages - 1
url = gl.utils.mergeUrlParams(pageParams, url)
2016-03-28 22:01:44 +00:00
2016-04-18 21:28:30 +00:00
Turbolinks.visit(url)
2016-03-28 22:01:44 +00:00
2016-04-08 16:46:32 +00:00
goToTodoUrl: (e)->
todoLink = $(this).data('url')
return unless todoLink
# Allow Meta-Click or Mouse3-click to open in a new tab
if e.metaKey or e.which is 2
2016-04-08 16:46:32 +00:00
e.preventDefault()
window.open(todoLink,'_blank')
else
Turbolinks.visit(todoLink)