Refactor attachments

stable
David Barragán Merino 2014-08-03 12:07:48 +02:00
parent 0b950981f2
commit 372e19cf3f
3 changed files with 38 additions and 15 deletions

View File

@ -105,18 +105,21 @@ AttachmentsDirective = ($repo, $rs) ->
## Add Attachments ## Add Attachments
########### ###########
$el.on "click", "a.add-attach", -> $el.on "click", "a.add-attach", ->
event.preventDefault()
angular.element("input.add-attach").trigger("click") angular.element("input.add-attach").trigger("click")
$el.on "change", "input.add-attach", -> $el.on "change", "input.add-attach", ->
files = _.map(event.target.files, (x) -> x) files = _.map(event.target.files, (x) -> x)
return if files.length < 1 return if files.length < 1
# Add files to uploadingFiles array
$scope.$apply => $scope.$apply =>
if not $scope.uploadingFiles or $scope.uploadingFiles.length == 0 if not $scope.uploadingFiles or $scope.uploadingFiles.length == 0
$scope.uploadingFiles = files $scope.uploadingFiles = files
else else
$scope.uploadingFiles = scope.uploadingFiles.concat(files) $scope.uploadingFiles = scope.uploadingFiles.concat(files)
# Upload new files
urlName = $ctrl.attachmentsUrlName urlName = $ctrl.attachmentsUrlName
projectId = $scope.projectId projectId = $scope.projectId
objectId = $model.$modelValue.id objectId = $model.$modelValue.id
@ -125,13 +128,15 @@ AttachmentsDirective = ($repo, $rs) ->
promise = $rs.attachments.create(urlName, projectId, objectId, file) promise = $rs.attachments.create(urlName, projectId, objectId, file)
promise.then (data) -> promise.then (data) ->
$scope.uploadingFiles = _.without($scope.uploadingFiles, file)
data.isCreatedRightNow = true 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) -> 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 $confirm.notify("error", null, "We have not been able to upload '#{file.name}'.") #TODO: i18in
########### ###########
@ -164,7 +169,7 @@ AttachmentDirective = ($log, $repo, $confirm) ->
<span><%- size %></span> <span><%- size %></span>
</div> </div>
<div class="attachment-comments"> <div class="attachment-comments">
<span class="deprecated-file hidden">(deprecated)</span> <% if (isDeprecated){ %> <span class="deprecated-file">(deprecated)</span> <% } %>
<span><%- description %></span> <span><%- description %></span>
</div> </div>
<div class="attachment-settings"> <div class="attachment-settings">
@ -220,9 +225,10 @@ AttachmentDirective = ($log, $repo, $confirm) ->
if attachment.is_deprecated if attachment.is_deprecated
$el.addClass("deprecated") $el.addClass("deprecated")
$el.find(".deprecated-file").removeClass('hidden')
if $scope.showDeprecatedAttachments if $scope.showDeprecatedAttachments
$el.removeClass("hidden") $el.removeClass("hidden")
else
$el.addClass("hidden")
else else
$el.removeClass("deprecated") $el.removeClass("deprecated")
$el.removeClass("hidden") $el.removeClass("hidden")
@ -251,7 +257,7 @@ AttachmentDirective = ($log, $repo, $confirm) ->
subtitle = "the attachment '#{attachment.name}'" #TODO: i18in subtitle = "the attachment '#{attachment.name}'" #TODO: i18in
onSuccess = -> onSuccess = ->
$ctrl.loadAttachments(attachment.object_id) $ctrl.onDeleteAttachment(attachment)
$confirm.notify("success", null, "We've deleted #{subtitle}.") #TODO: i18in $confirm.notify("success", null, "We've deleted #{subtitle}.") #TODO: i18in
onError = -> onError = ->
@ -263,11 +269,9 @@ AttachmentDirective = ($log, $repo, $confirm) ->
########### ###########
## Actions (on edit mode) ## 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) -> $el.on "click", "a.editable-settings.icon-floppy", (event) ->
event.preventDefault()
newDescription = $el.find("input[name='description']").val() newDescription = $el.find("input[name='description']").val()
newIsDeprecated = $el.find("input[name='is-deprecated']").prop("checked") newIsDeprecated = $el.find("input[name='is-deprecated']").prop("checked")
@ -277,7 +281,8 @@ AttachmentDirective = ($log, $repo, $confirm) ->
attachment.is_deprecated = newIsDeprecated attachment.is_deprecated = newIsDeprecated
onSuccess = -> onSuccess = ->
$ctrl.loadAttachments(attachment.object_id) $ctrl.onEditAttachment(attachment)
render(attachment)
$confirm.notify("success") $confirm.notify("success")
onError = -> onError = ->
@ -285,6 +290,10 @@ AttachmentDirective = ($log, $repo, $confirm) ->
$repo.save(attachment).then(onSuccess, onError) $repo.save(attachment).then(onSuccess, onError)
$el.on "click", "a.editable-settings.icon-delete", (event) ->
event.preventDefault()
render(attachment)
########### ###########
## On destroy ## On destroy
########### ###########

View File

@ -116,8 +116,23 @@ class AttachmentsMixin
return @rs.attachments.list(@.attachmentsUrlName, objectId).then (attachments) => return @rs.attachments.list(@.attachmentsUrlName, objectId).then (attachments) =>
@scope.attachments = _.sortBy(attachments, "order") @scope.attachments = _.sortBy(attachments, "order")
@scope.attachmentsCount = @scope.attachments.length @.updateAttachmentsCounters()
@scope.deprecatedAttachmentsCount = _.filter(@scope.attachments, is_deprecated: true).length
return attachments 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 taiga.AttachmentsMixin = AttachmentsMixin

View File

@ -25,7 +25,6 @@ section.attachments(tg-attachments, ng-model=attachModel, ng-if="#{attachModel}.
div.attachment-comments div.attachment-comments
span(ng-bind="file.progressMessage") span(ng-bind="file.progressMessage")
div.percentage(ng-style="{'width': file.progressPercent}") div.percentage(ng-style="{'width': file.progressPercent}")
div.attachment-settings
a.hidden.more-attachments(href="", title="show deprecated atachments") a.hidden.more-attachments(href="", title="show deprecated atachments")
span.text + show deprecated atachments span.text + show deprecated atachments