var MediaController = function(params) { this.init(params); }; MediaController.prototype = { top_cat_id: false, cat: false, cat_id: false, categories: false, media: false, s3_upload_url: false, aws_access_key_id: false, policy: false, signature: false, selected_media: false, uploader: false, refresh_unprocessed_images: true, allow_edit: false, feather_editor: false, last_upload_processed: false, ajax_count: 0, ajax_limit: 20, init: function(params) { var that = this; for (var i in params) this[i] = params[i]; $("#uploader") .empty() .append($('

') .append($('').addClass('caboose-btn').attr('href', '#').html('Upload').click(function(e) { e.preventDefault(); that.toggle_uploader(); })) .append($('').addClass('caboose-btn').attr('href', '#').html('Select All').click(function(e) { e.preventDefault(); that.select_all_media(); })) .append($('').addClass('caboose-btn').attr('href', '#').attr('id', 'sort-btn').html('Sort').click(function(e) { e.preventDefault(); that.sort_media(); })) ) .append($('

').attr('id', 'the_uploader')); that.refresh(); }, refresh: function() { var that = this; that.refresh_categories(); that.refresh_media(); that.print_controls(); }, refresh_categories: function(after) { var that = this; $.ajax({ url: '/admin/media-categories/flat-tree', type: 'get', async: false, success: function(resp) { that.categories = resp; if (!that.cat_id) that.cat_id = that.categories[0].id; 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.media = resp; that.selected_media = []; that.print_media(); } }); }, change_sort_order: function(list) { var that = this; // console.log(list.toString()); $.ajax({ url: '/admin/media-categories/' + that.cat_id + '/sort-order', type: 'put', data: { sort: list }, success: function(resp) { console.log("success"); } }); }, sort_media: function() { var that = this; var btn = $("#sort-btn"); if ( btn.text() == "Sort" ) { $("li.media").draggable("disable"); $("#sort-btn").text("Done Sorting"); $("li.media").css("cursor","move"); $("#media ul").sortable({ update: function(event, ui) { var index = ui.item.index(); var start = ui.item.data('start'); var media_id = ui.item.attr("id").replace("media",""); var sort_list = []; for(var i=1;i <= $("#media ul li").length; i++) { // console.log(i); var m = $('#media ul li:nth-child(' + i + ')').attr('id').replace("media",""); // if (m) { sort_list.push(m); // } } that.change_sort_order(sort_list); } }); $("#media ul").sortable('enable'); $("#media ul li").css("border-color","#98FF97"); } else if ( btn.text() == "Done Sorting") { $("#media ul").sortable("disable"); $("li.media").draggable("enable"); $("li.media").css("cursor","default"); $("#sort-btn").text("Sort"); $("#media ul li").css("border-color","#666"); } }, 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': '', 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: '100mb', // Maximum file size mime_types: [{ title: "Upload files", extensions: "jpg,jpeg,png,gif,tif,tiff,mp3,wav,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) { //up.settings.multipart_params["Content-Type"] = file.type; $.ajax({ url: '/admin/media/pre-upload', type: 'post', data: { media_category_id: that.cat_id, name: file.name, file_type: file.type }, success: function(resp) {}, async: false }); controller.refresh(); }, FileUploaded: function(ip, file) { that.refresh(); }, UploadComplete: function(up, files) { that.refresh(); that.ajax_count = 0; if (that.uploader) { $("#the_uploader").slideUp(400, function() { $("#the_uploader").plupload('destroy'); that.uploader = false; }); } } } }); $("#the_uploader").slideDown(); } }, check_processing_status: function() { var that = this; if (!that.last_upload_processed) that.last_upload_processed = new Date(); if ( that.ajax_count < that.ajax_limit ) { $.ajax({ url: '/admin/media/last-upload-processed', type: 'get', success: function(resp) { that.ajax_count += 1; var d = Date.parse(resp['last_upload_processed']); if (d > that.last_upload_processed) that.refresh_media(); else setTimeout(function() { that.check_processing_status(); }, 3000); that.last_upload_processed = d; } }); } }, print_categories: function() { var that = this; var ul = $('