From 2627a03e29db0c8adef5aa9bff08955ef0eb7ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Thu, 16 Oct 2014 22:14:47 +0200 Subject: [PATCH] Refactor tg-us-estimation. Fuck Yeah!!! :clap: :metal: :clap: :metal: --- app/coffee/modules/userstories/detail.coffee | 55 ++++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/app/coffee/modules/userstories/detail.coffee b/app/coffee/modules/userstories/detail.coffee index 99d18f21..e8e21932 100644 --- a/app/coffee/modules/userstories/detail.coffee +++ b/app/coffee/modules/userstories/detail.coffee @@ -263,7 +263,7 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) -> total <% _.each(roles, function(role) { %> -
  • +
  • <%- role.points %> <%- role.name %>
  • <% }); %> @@ -289,6 +289,11 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) -> link = ($scope, $el, $attrs, $model) -> saveAfterModify = $attrs.saveAfterModify or false + isEditable = -> + if $model.$modelValue.id + return $scope.project.my_permissions.indexOf("modify_us") != -1 + return $scope.project.my_permissions.indexOf("add_us") != -1 + render = (us) -> totalPoints = us.total_points or 0 computableRoles = _.filter($scope.project.roles, "computable") @@ -301,7 +306,12 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) -> role.points = if pointObj? and pointObj.name? then pointObj.name else "?" return role - html = mainTemplate({totalPoints: totalPoints, roles: roles}) + ctx = { + totalPoints: totalPoints + roles: roles + editable: isEditable() + } + html = mainTemplate(ctx) $el.html(html) renderPoints = (target, us, roleId) -> @@ -337,6 +347,8 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) -> $el.on "click", ".total.clickable", (event) -> event.preventDefault() event.stopPropagation() + return if not isEditable() + target = angular.element(event.currentTarget) roleId = target.data("role-id") @@ -349,6 +361,7 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) -> $el.on "click", ".point", (event) -> event.preventDefault() event.stopPropagation() + return if not isEditable() target = angular.element(event.currentTarget) roleId = target.data("role-id") @@ -356,28 +369,24 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) -> $el.find(".popover").popover().close() - us = $model.$modelValue + us = angular.copy($model.$modelValue) + us.points[roleId] = pointId + us.total_points = calculateTotalPoints(us) + $model.$setViewValue(us) - points = _.clone(us.points, true) - points[roleId] = pointId - - # NOTE: your past self wants that you make a refactor of this - $scope.$apply -> - us.points = points - us.total_points = calculateTotalPoints(us) - - if saveAfterModify - onSuccess = -> - $repo.refresh(us).then -> - render(us) - $rootScope.$broadcast("history:reload") - onError = -> - $repo.refresh(us).then -> - render(us) - $confirm.notify("error") - $repo.save(us).then(onSuccess, onError) - else - render(us) + if saveAfterModify + # Edit in the detail page + onSuccess = -> + $confirm.notify("success") + $rootScope.$broadcast("history:reload") + onError = -> + $confirm.notify("error") + us.revert() + $model.$setViewValue(us) + $repo.save($model.$modelValue).then(onSuccess, onError) + else + # Create or eedit in the lightbox + render($model.$modelValue) $scope.$watch $attrs.ngModel, (us) -> render(us) if us