var MediaController = function(params) { this.init(params); }; MediaController.prototype = { top_cat_id: false, cat: false, cat_id: false, categories: false, s3_upload_url: false, aws_access_key_id: false, policy: false, signature: false, selected_media: false, uploader: false, init: function(params) { var that = this; for (var i in params) this[i] = params[i]; $("#uploader") .empty() .append($('
') .append($('').attr('href', '#').html('Upload').click(function(e) { e.preventDefault(); that.toggle_uploader(); })) .append(' | ') .append($('').attr('href', '#').html('Select All').click(function(e) { e.preventDefault(); that.select_all_media(); })) ) .append($('').attr('id', 'the_uploader')); that.refresh(); }, refresh: function() { var that = this; that.refresh_categories(); that.refresh_media(); that.print_controls(); }, toggle_uploader: function() { var that = this; if (that.uploader) { $("#the_uploader").slideUp(400, function() { $("#the_uploader").plupload('destroy'); that.uploader = false; }); } else { $("#the_uploader").hide(); that.uploader = $("#the_uploader").plupload({ runtimes: 'html5,flash,silverlight', url: that.s3_upload_url, multipart: true, multipart_params: { key: that.cat_id + '_${filename}', // use filename as a key Filename: that.cat_id + '_${filename}', // adding this to keep consistency across the runtimes acl: 'public-read', //'Content-Type': 'image/jpeg', AWSAccessKeyId: that.aws_access_key_id, policy: that.policy, signature: that.signature }, file_data_name: 'file', // optional, but better be specified directly filters: { max_file_size: '10mb', // Maximum file size mime_types: [{ title: "Upload files", extensions: "jpg,jpeg,png,gif,tif,tiff,pdf,doc,docx,odt,odp,ods,ppt,pptx,xls,xlsx,zip,tgz,csv,txt" }] // Specify what files to browse for }, flash_swf_url: '../../js/Moxie.swf', // Flash settings silverlight_xap_url: '../../js/Moxie.xap', // Silverlight settings init: { BeforeUpload: function(up, file) { $.ajax({ url: '/admin/media/pre-upload', type: 'post', data: { media_category_id: that.cat_id, name: file.name }, success: function(resp) {}, async: false }); controller.refresh(); }, FileUploaded: function(ip, file) { that.refresh(); }, UploadComplete: function(up, files) { that.refresh(); if (that.uploader) { $("#the_uploader").slideUp(400, function() { $("#the_uploader").plupload('destroy'); that.uploader = false; }); } } } }); $("#the_uploader").slideDown(); } }, refresh_categories: function(after) { var that = this; $.ajax({ url: '/admin/media-categories/flat-tree', type: 'get', async: false, success: function(resp) { that.categories = resp; that.print_categories(); if (after) after(); } }); }, refresh_media: function() { var that = this; $.ajax({ url: '/admin/media/json', type: 'get', async: false, data: { media_category_id: that.cat_id }, success: function(resp) { that.cat = resp; that.cat_id = that.cat.id; that.selected_media = []; that.print_media(); } }); }, print_categories: function() { var that = this; var ul = $('Deleting...
"); $.ajax({ url: '/admin/media/bulk', type: 'delete', data: { ids: that.selected_media }, success: function(resp) { that.refresh_categories(); that.refresh_media(); that.print_controls(); } }); }, edit_media_description: function(media_id) { var that = this; caboose_modal_url('/admin/media/' + media_id + '/description'); //var div = $('').attr('id', 'media_' + media_id + '_description'); //$('#media').append(div); //new ModelBinder({ // name: 'Media', // id: media_id, // update_url: '/admin/media/' + media_id, // authenticity_token: that.authenticity_token, // attributes: [ // { // name: 'description', nice_name: 'Description', type: 'textarea', value: '', width: 400, height: 100, fixed_placeholder: true, // after_update: function() { $('#media_' + media_id + '_description_container').remove(); }, // after_cancel: function() { $('#media_' + media_id + '_description_container').remove(); } // } // ] //}); //var options = { // iframe: true, // innerWidth: 200, // innerHeight: 50, // scrolling: false, // transition: 'fade', // closeButton: false, // onComplete: caboose_fix_colorbox, // opacity: 0.50 //}; //setTimeout(function() { $('#media_' + media_id + '_description_container').colorbox(options); }, 2000); }, //============================================================================ select_category: function(cat_id) { var that = this; that.cat_id = cat_id; that.print_categories(); that.refresh_media(); }, add_category: function(name) { var that = this; if (!name) { var div = $('').addClass('note warning') .append('New Category Name: ') .append($('').attr('type', 'text').attr('id', 'new_cat_name')).append(" ") .append($('').attr('type', 'button').val('Add').click(function() { that.add_category($('#new_cat_name').val()); })).append(" ") .append($('').attr('type', 'button').val('Cancel').click(function() { $('#new_cat_message').empty(); })); $('#new_cat_message').empty().append(div); return; } $('#new_cat_message').empty().html("Adding category...
"); $.ajax({ url: '/admin/media-categories', type: 'post', data: { parent_id: that.cat.id, name: name }, success: function(resp) { if (resp.error) $('#new_cat_message').empty().html("" + resp.error + "
"); if (resp.refresh) that.refresh_categories(); } }); }, delete_category: function(cat_id, confirm) { var that = this; if (!confirm) { var div = $('').addClass('note warning') .append('Are you sure? ') .append($('').attr('type', 'button').val('Yes').click(function() { that.delete_category(cat_id, true); })).append(" ") .append($('').attr('type', 'button').val('No' ).click(function() { that.refresh_categories(); that.print_controls() })); $('#delete').empty().append(div); return; } $('#delete').empty().html("Deleting...
"); $.ajax({ url: '/admin/media-categories/' + cat_id, type: 'delete', success: function(resp) { that.refresh_categories(function() { var exists = false; $.each(that.categories, function(i, cat) { if (cat.id == cat_id) { exists = true; return false; }}); if (!exists) that.select_category(that.categories[0].id); else that.refresh_media(); that.print_controls(); }); } }); }, };