From 822006c54680495e1222962a5e76f6e0bb9b99e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Tue, 29 Jul 2014 16:22:46 +0200 Subject: [PATCH] Now we can edit and delete attachments --- app/coffee/modules/common/attachments.coffee | 60 ++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/app/coffee/modules/common/attachments.coffee b/app/coffee/modules/common/attachments.coffee index 09e38a98..c3b8fd53 100644 --- a/app/coffee/modules/common/attachments.coffee +++ b/app/coffee/modules/common/attachments.coffee @@ -76,7 +76,7 @@ module.directive("tgAttachments", [AttachmentsDirective]) ## Attachment Directive ############################################################################# -AttachmentDirective = ($log) -> +AttachmentDirective = ($log, $repo, $rs, $confirm) -> singleAttachment = _.template("""
@@ -98,10 +98,13 @@ AttachmentDirective = ($log) ->
(<%- size %>) - +
- + checked<% } %> />
@@ -109,6 +112,8 @@ AttachmentDirective = ($log) -> """) # TODO: i18n link = ($scope, $el, $attrs, $model) -> + $ctrl = $el.controller() + render = (attachment, isEditable=false) -> ctx = { id: attachment.id @@ -123,6 +128,7 @@ AttachmentDirective = ($log) -> html = singleAttachmentEditable(ctx) else html = singleAttachment(ctx) + $el.html(html) if attachment.is_deprecated @@ -140,6 +146,51 @@ AttachmentDirective = ($log) -> attachment = $scope.$eval($attrs.tgAttachment) render(attachment) + ## Actions (on view mode) + $el.on "click", "a.settings.icon-edit", (event) -> + event.preventDefault() + render(attachment, true) + + $el.on "click", "a.settings.icon-delete", (event) -> + event.preventDefault() + + title = "Delete attachment" # i18n + subtitle = "the attachment '#{attachment.name}'" + + onSuccess = -> + $ctrl.loadAttachments(attachment.object_id) + $confirm.notify("success", null, "We've deleted #{subtitle}.") #TODO: i18in + + onError = -> + $confirm.notify("error", null, "We have not been able to delete #{subtitle}.") #TODO: i18in + + $confirm.ask(title, subtitle).then -> + $repo.remove(attachment).then(onSuccess, onError) + + ## Actions (on edit mode) + $el.on "click", "a.editable.icon-delete", (event) -> + event.preventDefault() + render(attachment) + + $el.on "click", "a.editable.icon-floppy", (event) -> + newDescription = $el.find("input[name='description']").val() + newIsDeprecated = $el.find("input[name='is-deprecated']").prop("checked") + + if newDescription != attachment.description + attachment.setAttr("description", newDescription) + if newIsDeprecated != attachment.is_deprecated + attachment.setAttr("is_deprecated", newIsDeprecated) + + onSuccess = -> + $ctrl.loadAttachments(attachment.object_id) + $confirm.notify("success") + + onError = -> + $confirm.notify("error") + + if attachment.isModified() + $repo.save(attachment).then(onSuccess, onError) + ## On destroy $scope.$on "$destroy", -> $el.off() @@ -149,4 +200,5 @@ AttachmentDirective = ($log) -> require: "ngModel" } -module.directive("tgAttachment", ["$log", AttachmentDirective]) +module.directive("tgAttachment", ["$log", "$tgRepo", "$tgResources", "$tgConfirm", + AttachmentDirective])