// Allows users to upload files via AJAX as attachments for a given block. // $(function () { // Return the authenticity token for any JS function that needs to do AJAX. // Since AJAX posts will fail if you don't attach this as defined here: http://michaelhendrickx.com/201012_jquery-ajax-with-rails-authenticity-token.html $.cms.auth_token = function () { return $('meta[name=csrf-token]').attr('content'); }; $.cms.AttachmentManager = { upload:function (file) { var assetName = $('#asset_types').val() , attachableClass = $('#asset_attachable_class').val() , attachableIDField = $('#asset_attachable_id') , attachableID = attachableIDField.val() , file = $('#asset_add_file') , clone = file.clone() , form = $('
') , fields = ''; fields += ''; fields += ''; fields += ''; fields += ''; fields += ''; $('body').append(form); form.append(fields); form.append(file); attachableIDField.after(clone); var inp = $(''); clone.after($('') .css({position:'static', margin:'5px 0 0 5px', 'verticalAlign':'middle'}) .attr({'id':'file-asset-uploader', 'src':'<%= asset_path 'cms/file-uploading.gif' %>'})); clone.replaceWith(inp); form.submit(); $('div.buttons').hide(); }, // @param [Integer] id The id of the attachment to delete. destroy:function (id) { if (confirm("Are you sure want to delete this attachment?")) { $.post('/cms/attachments/' + id, {_method:'delete', authenticity_token:$.cms.auth_token()}, function (attachment_id) { console.log(attachment_id); $("#attachment_" + attachment_id).hide(); if ($("#assets_table > table tr:visible").length <= 2) { $("#assets_table > table").hide(); } }, 'script'); } return false; } } $('#asset_types').selectbox({width:'445px'}); $('#asset_types').change(function () { if ($(this).val().indexOf('Select') != 0) { $('#asset_add').show(); } else { $('#asset_add').hide(); } }); // After an attachment is uploaded, copy the values into the main attachment table. $('#asset_add_uploader').load(function () { var response = $(this).contents(); if (response.find('tr').html()) { var asset = $(response).find('tr').clone(); var id = $(response).find("#asset-id").html(); var asset_ids = $('#attachment_manager_ids_list'); $(asset_ids).val($(asset_ids).val() + id + ","); $('#file-asset-uploader').remove(); $('#assets_table > table').append(asset).show(); $('div.buttons').show(); } }); });