CRM-14378 - Cleanup attachment js
[civicrm-core.git] / templates / CRM / Form / attachmentjs.tpl
index 046d0e89b8a6f947d12df012f993ddc41a4863b0..a3e44e60b112919a072f6fdc135ce1efecb26199 100644 (file)
@@ -1,27 +1,23 @@
 <script type="text/javascript">
 {literal}
-  function hideStatusAttachment( divName ) {
-    cj( divName ).hide( );
-  }
-
-  function showDeleteAttachment(fileName, postURLData, fileID, divName, divFile) {
-    var confirmMsg = '<div>{/literal}{ts escape="js"}Are you sure you want to delete attachment: {/ts}{literal}' + fileName + '&nbsp; <a href="#" onclick="deleteAttachment( \'' + postURLData + '\',' + fileID + ',\'' + divName + '\', \'' + divFile + '\' ); return false;" style="text-decoration: underline;">{/literal}</div><div>{ts escape='js'}Yes{/ts}{literal}</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="hideStatusAttachment( \'' + divName + '\' ); return false;" style="text-decoration: underline;">{/literal}{ts escape='js'}No{/ts}{literal}</a></div>';
-    cj(divName).show().html(confirmMsg);
-  }
-
-  function deleteAttachment(postURLData, fileID, divName, divFile) {
-    var postUrl = {/literal}"{crmURL p='civicrm/file/delete' h=0 }"{literal};
-    cj.ajax({
-      type: "POST",
-      data:  postURLData,
-      url: postUrl,
-      success: function (html) {
-        var resourceBase   = {/literal}"{$config->resourceBase}"{literal};
-        var successMsg = '{/literal}{ts escape="js"}The selected attachment has been deleted.{/ts}{literal}';
-        cj(divFile + ',' + divName).hide();
-        CRM.alert(successMsg, '{/literal}{ts escape="js"}Removed{/ts}{literal}', 'success');
-      }
+  cj(function($) {
+    $('a.delete-attachment').click(function() {
+      var $el = $(this),
+        $row = $el.closest('.crm-attachment-wrapper'),
+        msg = '{/literal}{ts escape="js" 1="%1"}This will immediately delete the file %1. This action cannot be undone.{/ts}{literal}';
+      CRM.confirm(null, {
+        title: $el.attr('title'),
+        message: ts(msg, {1: '<em>' + $el.data('filename') + '</em>'})
+      }).on('crmConfirmYes', function() {
+        var postUrl = {/literal}"{crmURL p='civicrm/file/delete' h=0 }"{literal};
+        var request = $.post(postUrl, $el.data('args'));
+        CRM.status({success: '{/literal}{ts escape="js"}Removed{/ts}{literal}'}, request);
+        request.done(function() {
+          $row.remove();
+        });
+      });
+      return false;
     });
-  }
+  });
 {/literal}
 </script>