Add spinner to iocaine button and refactor a little the directive

stable
Jesús Espino 2014-10-16 19:24:26 +02:00 committed by David Barragán Merino
parent 02c0b38bff
commit 752f14584a
1 changed files with 19 additions and 12 deletions

View File

@ -262,7 +262,7 @@ TaskStatusButtonDirective = ($rootScope, $repo, $confirm) ->
module.directive("tgTaskStatusButton", ["$rootScope", "$tgRepo", "$tgConfirm", TaskStatusButtonDirective]) module.directive("tgTaskStatusButton", ["$rootScope", "$tgRepo", "$tgConfirm", TaskStatusButtonDirective])
TaskIsIocaineButtonDirective = ($rootscope, $tgrepo, $confirm) -> TaskIsIocaineButtonDirective = ($rootscope, $tgrepo, $confirm, $loading) ->
template = _.template(""" template = _.template("""
<fieldset title="Feeling a bit overwhelmed by a task? Make sure others know about it by clicking on Iocaine when editing a task. It's possible to become immune to this (fictional) deadly poison by consuming small amounts over time just as it's possible to get better at what you do by occasionally taking on extra challenges!"> <fieldset title="Feeling a bit overwhelmed by a task? Make sure others know about it by clicking on Iocaine when editing a task. It's possible to become immune to this (fictional) deadly poison by consuming small amounts over time just as it's possible to get better at what you do by occasionally taking on extra challenges!">
<label for="is-iocaine" class="clickable button button-gray is-iocaine">Iocaine</label> <label for="is-iocaine" class="clickable button button-gray is-iocaine">Iocaine</label>
@ -271,37 +271,44 @@ TaskIsIocaineButtonDirective = ($rootscope, $tgrepo, $confirm) ->
""") """)
link = ($scope, $el, $attrs, $model) -> link = ($scope, $el, $attrs, $model) ->
render = _.once (us) -> render = _.once (task) ->
$el.html(template()) $el.html(template())
refresh = (us) -> refresh = (task) ->
if us?.is_iocaine if task?.is_iocaine
$el.find('.is-iocaine').addClass('active') $el.find('.is-iocaine').addClass('active')
else else
$el.find('.is-iocaine').removeClass('active') $el.find('.is-iocaine').removeClass('active')
$scope.$watch $attrs.ngModel, (us) -> $scope.$watch $attrs.ngModel, (task) ->
return if not us return if not task
render(us) render(task)
refresh(us) refresh(task)
$scope.$on "$destroy", -> $scope.$on "$destroy", ->
$el.off() $el.off()
$el.on "click", ".is-iocaine", (event) -> $el.on "click", ".is-iocaine", (event) ->
us = $model.$modelValue.clone() task = $model.$modelValue.clone()
us.is_iocaine = not us.is_iocaine task.is_iocaine = not task.is_iocaine
$model.$setViewValue(us) $model.$setViewValue(task)
$loading.start($el.find('label'))
promise = $tgrepo.save($model.$modelValue) promise = $tgrepo.save($model.$modelValue)
promise.then -> promise.then ->
$confirm.notify("success")
$rootscope.$broadcast("history:reload") $rootscope.$broadcast("history:reload")
promise.then null, -> promise.then null, ->
$confirm.notify("error") $confirm.notify("error")
promise.finally ->
$loading.finish($el.find('label'))
return { return {
link: link link: link
restrict: "EA" restrict: "EA"
require: "ngModel" require: "ngModel"
} }
module.directive("tgTaskIsIocaineButton", ["$rootScope", "$tgRepo", "$tgConfirm", TaskIsIocaineButtonDirective]) module.directive("tgTaskIsIocaineButton", ["$rootScope", "$tgRepo", "$tgConfirm", "$tgLoading", TaskIsIocaineButtonDirective])