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 = $('