Refactor tg-us-estimation. Fuck Yeah!!! 👏 🤘 👏 🤘

stable
David Barragán Merino 2014-10-16 22:14:47 +02:00
parent 8032b37773
commit 2627a03e29
1 changed files with 32 additions and 23 deletions

View File

@ -263,7 +263,7 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) ->
<span class="role">total</span> <span class="role">total</span>
</li> </li>
<% _.each(roles, function(role) { %> <% _.each(roles, function(role) { %>
<li class="total clickable" data-role-id="<%- role.id %>"> <li class="total <% if(editable){ %>clickable<% } %>" data-role-id="<%- role.id %>">
<span class="points"><%- role.points %></span> <span class="points"><%- role.points %></span>
<span class="role"><%- role.name %></span></li> <span class="role"><%- role.name %></span></li>
<% }); %> <% }); %>
@ -289,6 +289,11 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) ->
link = ($scope, $el, $attrs, $model) -> link = ($scope, $el, $attrs, $model) ->
saveAfterModify = $attrs.saveAfterModify or false 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) -> render = (us) ->
totalPoints = us.total_points or 0 totalPoints = us.total_points or 0
computableRoles = _.filter($scope.project.roles, "computable") 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 "?" role.points = if pointObj? and pointObj.name? then pointObj.name else "?"
return role return role
html = mainTemplate({totalPoints: totalPoints, roles: roles}) ctx = {
totalPoints: totalPoints
roles: roles
editable: isEditable()
}
html = mainTemplate(ctx)
$el.html(html) $el.html(html)
renderPoints = (target, us, roleId) -> renderPoints = (target, us, roleId) ->
@ -337,6 +347,8 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) ->
$el.on "click", ".total.clickable", (event) -> $el.on "click", ".total.clickable", (event) ->
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
return if not isEditable()
target = angular.element(event.currentTarget) target = angular.element(event.currentTarget)
roleId = target.data("role-id") roleId = target.data("role-id")
@ -349,6 +361,7 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) ->
$el.on "click", ".point", (event) -> $el.on "click", ".point", (event) ->
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
return if not isEditable()
target = angular.element(event.currentTarget) target = angular.element(event.currentTarget)
roleId = target.data("role-id") roleId = target.data("role-id")
@ -356,28 +369,24 @@ UsEstimationDirective = ($rootScope, $repo, $confirm) ->
$el.find(".popover").popover().close() $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) if saveAfterModify
points[roleId] = pointId # Edit in the detail page
onSuccess = ->
# NOTE: your past self wants that you make a refactor of this $confirm.notify("success")
$scope.$apply -> $rootScope.$broadcast("history:reload")
us.points = points onError = ->
us.total_points = calculateTotalPoints(us) $confirm.notify("error")
us.revert()
if saveAfterModify $model.$setViewValue(us)
onSuccess = -> $repo.save($model.$modelValue).then(onSuccess, onError)
$repo.refresh(us).then -> else
render(us) # Create or eedit in the lightbox
$rootScope.$broadcast("history:reload") render($model.$modelValue)
onError = ->
$repo.refresh(us).then ->
render(us)
$confirm.notify("error")
$repo.save(us).then(onSuccess, onError)
else
render(us)
$scope.$watch $attrs.ngModel, (us) -> $scope.$watch $attrs.ngModel, (us) ->
render(us) if us render(us) if us