From 58923eaa8114968c4ce575dff4c3d9dab416cac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 29 Aug 2014 14:01:11 +0200 Subject: [PATCH] Adding loading service to allow set a spinner on button during request --- app/coffee/modules/common/lightboxes.coffee | 16 +++------ app/coffee/modules/common/loading.coffee | 36 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 app/coffee/modules/common/loading.coffee diff --git a/app/coffee/modules/common/lightboxes.coffee b/app/coffee/modules/common/lightboxes.coffee index 8f607af4..554a89ea 100644 --- a/app/coffee/modules/common/lightboxes.coffee +++ b/app/coffee/modules/common/lightboxes.coffee @@ -146,7 +146,7 @@ module.directive("tgLbBlock", ["lightboxService", BlockLightboxDirective]) ## Create/Edit Userstory Lightbox Directive ############################################################################# -CreateEditUserstoryDirective = ($repo, $model, $rs, $rootScope, lightboxService) -> +CreateEditUserstoryDirective = ($repo, $model, $rs, $rootScope, lightboxService, $loading) -> link = ($scope, $el, attrs) -> isNew = true @@ -202,32 +202,25 @@ CreateEditUserstoryDirective = ($repo, $model, $rs, $rootScope, lightboxService) form = $el.find("form").checksley() target = angular.element(event.currentTarget) - loading = "" #Create spinner item - finish = target.text() #Save current text - if not form.validate() return + $loading.start(target) if isNew - target.addClass('loading').html(loading) # Add item - promise = $repo.create("userstories", $scope.us) broadcastEvent = "usform:new:success" else - target.addClass('loading').html(loading) # Add item promise = $repo.save($scope.us) broadcastEvent = "usform:edit:success" promise.then (data) -> - target.removeClass('loading').html(finish) # Add item - + $loading.finish(target) lightboxService.close($el) $rootScope.$broadcast(broadcastEvent, data) promise.then null, (data) -> - target.removeClass('loading').html(finish) # Add item - + $loading.finish(target) form.setErrors(data) if data._error_message $confirm.notify("error", data._error_message) @@ -260,6 +253,7 @@ module.directive("tgLbCreateEditUserstory", [ "$tgResources", "$rootScope", "lightboxService", + "loadingService", CreateEditUserstoryDirective ]) diff --git a/app/coffee/modules/common/loading.coffee b/app/coffee/modules/common/loading.coffee new file mode 100644 index 00000000..c6551901 --- /dev/null +++ b/app/coffee/modules/common/loading.coffee @@ -0,0 +1,36 @@ +### +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino Garcia +# Copyright (C) 2014 David Barragán Merino +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# File: modules/lightboxes.coffee +### + +module = angular.module("taigaCommon") + +class LoadingService extends taiga.Service + start: (target) -> + target.data('loading-old-content', target.html()) + target.addClass('loading') + target.html("") + + finish: (target) -> + oldContent = target.data('loading-old-content') + target.data('loading-old-content', null) + target.html(oldContent) + target.removeClass('loading') + +module.service("loadingService", LoadingService)