From 372e19cf3fbad3e60ac5c5731df166e24b3a8c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Sun, 3 Aug 2014 12:07:48 +0200 Subject: [PATCH] Refactor attachments --- app/coffee/modules/common/attachments.coffee | 33 +++++++++++++------- app/coffee/modules/controllerMixins.coffee | 19 +++++++++-- app/partials/views/modules/attachments.jade | 1 - 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/coffee/modules/common/attachments.coffee b/app/coffee/modules/common/attachments.coffee index df6bf149..94ac90d0 100644 --- a/app/coffee/modules/common/attachments.coffee +++ b/app/coffee/modules/common/attachments.coffee @@ -105,18 +105,21 @@ AttachmentsDirective = ($repo, $rs) -> ## Add Attachments ########### $el.on "click", "a.add-attach", -> + event.preventDefault() angular.element("input.add-attach").trigger("click") $el.on "change", "input.add-attach", -> files = _.map(event.target.files, (x) -> x) return if files.length < 1 + # Add files to uploadingFiles array $scope.$apply => if not $scope.uploadingFiles or $scope.uploadingFiles.length == 0 $scope.uploadingFiles = files else $scope.uploadingFiles = scope.uploadingFiles.concat(files) + # Upload new files urlName = $ctrl.attachmentsUrlName projectId = $scope.projectId objectId = $model.$modelValue.id @@ -125,13 +128,15 @@ AttachmentsDirective = ($repo, $rs) -> promise = $rs.attachments.create(urlName, projectId, objectId, file) promise.then (data) -> - $scope.uploadingFiles = _.without($scope.uploadingFiles, file) data.isCreatedRightNow = true - $scope.attachments[$scope.attachments.length] = data - $scope.attachmentsCount = $scope.attachments.length + + index = $scope.uploadingFiles.indexOf(file) + $scope.uploadingFiles.splice(index, 1) + $ctrl.onCreateAttachment(data) promise.then null, (data) -> - $scope.uploadingFiles = _.without($scope.uploadingFiles, file) + index = $scope.uploadingFiles.indexOf(file) + $scope.uploadingFiles.splice(index, 1) $confirm.notify("error", null, "We have not been able to upload '#{file.name}'.") #TODO: i18in ########### @@ -164,7 +169,7 @@ AttachmentDirective = ($log, $repo, $confirm) -> <%- size %>
- + <% if (isDeprecated){ %> (deprecated) <% } %> <%- description %>
@@ -220,9 +225,10 @@ AttachmentDirective = ($log, $repo, $confirm) -> if attachment.is_deprecated $el.addClass("deprecated") - $el.find(".deprecated-file").removeClass('hidden') if $scope.showDeprecatedAttachments $el.removeClass("hidden") + else + $el.addClass("hidden") else $el.removeClass("deprecated") $el.removeClass("hidden") @@ -251,7 +257,7 @@ AttachmentDirective = ($log, $repo, $confirm) -> subtitle = "the attachment '#{attachment.name}'" #TODO: i18in onSuccess = -> - $ctrl.loadAttachments(attachment.object_id) + $ctrl.onDeleteAttachment(attachment) $confirm.notify("success", null, "We've deleted #{subtitle}.") #TODO: i18in onError = -> @@ -263,11 +269,9 @@ AttachmentDirective = ($log, $repo, $confirm) -> ########### ## Actions (on edit mode) ########### - $el.on "click", "a.editable-settings.icon-delete", (event) -> - event.preventDefault() - render(attachment) - $el.on "click", "a.editable-settings.icon-floppy", (event) -> + event.preventDefault() + newDescription = $el.find("input[name='description']").val() newIsDeprecated = $el.find("input[name='is-deprecated']").prop("checked") @@ -277,7 +281,8 @@ AttachmentDirective = ($log, $repo, $confirm) -> attachment.is_deprecated = newIsDeprecated onSuccess = -> - $ctrl.loadAttachments(attachment.object_id) + $ctrl.onEditAttachment(attachment) + render(attachment) $confirm.notify("success") onError = -> @@ -285,6 +290,10 @@ AttachmentDirective = ($log, $repo, $confirm) -> $repo.save(attachment).then(onSuccess, onError) + $el.on "click", "a.editable-settings.icon-delete", (event) -> + event.preventDefault() + render(attachment) + ########### ## On destroy ########### diff --git a/app/coffee/modules/controllerMixins.coffee b/app/coffee/modules/controllerMixins.coffee index 3b95cdbc..e704f398 100644 --- a/app/coffee/modules/controllerMixins.coffee +++ b/app/coffee/modules/controllerMixins.coffee @@ -116,8 +116,23 @@ class AttachmentsMixin return @rs.attachments.list(@.attachmentsUrlName, objectId).then (attachments) => @scope.attachments = _.sortBy(attachments, "order") - @scope.attachmentsCount = @scope.attachments.length - @scope.deprecatedAttachmentsCount = _.filter(@scope.attachments, is_deprecated: true).length + @.updateAttachmentsCounters() return attachments + updateAttachmentsCounters: -> + @scope.attachmentsCount = @scope.attachments.length + @scope.deprecatedAttachmentsCount = _.filter(@scope.attachments, is_deprecated: true).length + + onCreateAttachment: (attachment) -> + @scope.attachments[@scope.attachments.length] = attachment + @.updateAttachmentsCounters() + + onEditAttachment: (attachment) -> + @.updateAttachmentsCounters() + + onDeleteAttachment: (attachment) -> + index = @scope.attachments.indexOf(attachment) + @scope.attachments.splice(index, 1) + @.updateAttachmentsCounters() + taiga.AttachmentsMixin = AttachmentsMixin diff --git a/app/partials/views/modules/attachments.jade b/app/partials/views/modules/attachments.jade index af3f88e6..2fe6a51e 100644 --- a/app/partials/views/modules/attachments.jade +++ b/app/partials/views/modules/attachments.jade @@ -25,7 +25,6 @@ section.attachments(tg-attachments, ng-model=attachModel, ng-if="#{attachModel}. div.attachment-comments span(ng-bind="file.progressMessage") div.percentage(ng-style="{'width': file.progressPercent}") - div.attachment-settings a.hidden.more-attachments(href="", title="show deprecated atachments") span.text + show deprecated atachments