### # 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/backlog/lightboxes.coffee ### taiga = @.taiga bindOnce = @.taiga.bindOnce debounce = @.taiga.debounce module = angular.module("taigaBacklog") ############################################################################# ## Creare/Edit Sprint Lightbox Directive ############################################################################# CreateEditSprint = ($repo, $confirm, $rs, $rootscope, lightboxService, $loading) -> link = ($scope, $el, attrs) -> hasErrors = false createSprint = true $scope.sprint = { project: null name: null estimated_start: null estimated_finish: null } submit = (event) -> form = $el.find("form").checksley() if not form.validate() hasErrors = true $el.find(".last-sprint-name").addClass("disappear") return hasErrors = false newSprint = angular.copy($scope.sprint) if createSprint newSprint.estimated_start = moment(newSprint.estimated_start).format("YYYY-MM-DD") newSprint.estimated_finish = moment(newSprint.estimated_finish).format("YYYY-MM-DD") promise = $repo.create("milestones", newSprint) else newSprint.setAttr("estimated_start", moment(newSprint.estimated_start).format("YYYY-MM-DD")) newSprint.setAttr("estimated_finish", moment(newSprint.estimated_finish).format("YYYY-MM-DD")) promise = $repo.save(newSprint) target = angular.element(event.currentTarget) $loading.start(target) promise.then (data) -> $loading.finish(target) $scope.sprintsCounter += 1 if createSprint lightboxService.close($el) $rootscope.$broadcast("sprintform:create:success", data) promise.then null, (data) -> $loading.finish(target) form.setErrors(data) if data._error_message $confirm.notify("light-error", data._error_message) else if data.__all__ $confirm.notify("light-error", data.__all__[0]) remove = -> #TODO: i18n title = "Delete sprint" subtitle = $scope.sprint.name $confirm.ask(title, subtitle).then (finish) => onSuccess = -> finish() $scope.milestonesCounter -= 1 lightboxService.close($el) $rootscope.$broadcast("sprintform:remove:success") onError = -> finish(false) $confirm.notify("error") $repo.remove($scope.sprint).then(onSuccess, onError) $scope.$on "sprintform:create", (event, projectId) -> createSprint = true $scope.sprint.project = projectId $scope.sprint.name = null $scope.sprint.slug = null lastSprint = $scope.sprints[0] estimatedStart = moment() if $scope.sprint.estimated_start estimatedStart = moment($scope.sprint.estimated_start) else if lastSprint? estimatedStart = moment(lastSprint.estimated_finish) $scope.sprint.estimated_start = estimatedStart.format("DD MMM YYYY") estimatedFinish = moment().add(2, "weeks") if $scope.sprint.estimated_finish estimatedFinish = moment($scope.sprint.estimated_finish) else if lastSprint? estimatedFinish = moment(lastSprint.estimated_finish).add(2, "weeks") $scope.sprint.estimated_finish = estimatedFinish.format("DD MMM YYYY") lastSprintNameDom = $el.find(".last-sprint-name") if lastSprint?.name? lastSprintNameDom.html(" last sprint is #{lastSprint.name} ;-) ") $el.find(".delete-sprint").hide() $el.find(".title").text("New sprint") #TODO i18n $el.find(".button-green").text("Create") #TODO i18n lightboxService.open($el) $el.find(".sprint-name").focus() $scope.$on "sprintform:edit", (ctx, sprint) -> createSprint = false $scope.$apply -> $scope.sprint = sprint $scope.sprint.estimated_start = moment($scope.sprint.estimated_start).format("DD MMM YYYY") $scope.sprint.estimated_finish = moment($scope.sprint.estimated_finish).format("DD MMM YYYY") $el.find(".delete-sprint").show() $el.find(".title").text("Edit sprint") #TODO i18n $el.find(".button-green").text("Save") #TODO i18n lightboxService.open($el) $el.find(".sprint-name").focus().select() $el.find(".last-sprint-name").addClass("disappear") $el.on "keyup", ".sprint-name", (event) -> if $el.find(".sprint-name").val().length > 0 or hasErrors $el.find(".last-sprint-name").addClass("disappear") else $el.find(".last-sprint-name").removeClass("disappear") $el.on "click", ".button-green", debounce 2000, (event) -> event.preventDefault() submit(event) $el.on "click", ".delete-sprint .icon-delete", (event) -> event.preventDefault() remove() $scope.$on "$destroy", -> $el.off() return {link: link} module.directive("tgLbCreateEditSprint", [ "$tgRepo", "$tgConfirm", "$tgResources", "$rootScope", "lightboxService" "$tgLoading" CreateEditSprint ])