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 = $('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(); $("#sort-btn").text("Sort"); }, 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(); }); } }); }, media_with_id: function(media_id) { var that = this; var m = false; if (that.media.length > 0) { $.each(that.media, function(i, m2) { if (parseInt(m2.id) == parseInt(media_id)) { m = m2; return false; } }); } return m; }, download_image: function(media_id) { var that = this; url = that.download_url(media_id); window.open(url); }, download_url: function(media_id) { var that = this; var m = that.media_with_id(media_id); var url = ''; if ( m.image_urls ) url = m.image_urls.original_url; else url = m.file_url; return url; }, //============================================================================ // Aviary //============================================================================ edit_image: function(media_id) { var that = this; var m = that.media_with_id(media_id); if (that.feather_editor != false) { that.edit_image_helper(m); return; } that.feather_editor = new Aviary.Feather({ apiKey: '933414ee42934e8b81d8cd2226a5a13b', theme: 'light', enableCORS: false, maxSize: 1600, onSave: function(media_id, new_url) { return that.update_image(media_id, new_url); }, onLoad: function() { that.edit_image_helper(m); } }); }, edit_image_helper: function(m) { console.log(m.image_urls.original_url); var that = this; that.feather_editor.launch({ image: 'image-' + m.id, url: m.image_urls.original_url }); }, update_image: function(image_id, new_url) { var that = this; var media_id = parseInt(image_id.replace('image-', '')); that.feather_editor.showWaitIndicator(); $.ajax({ url: '/admin/media/' + media_id, type: 'put', data: { image_url: new_url }, success: function(resp) { if(resp.error) { alert("Error saving image:\n\n" + resp.error); } if(resp.success) { that.feather_editor.hideWaitIndicator(); var src = $('#image-' + media_id).attr('src').split('?')[0]; $('#image-' + media_id).attr('src', src + '?' + Date.now()); that.feather_editor.close(); that.refresh_media(); } } }); return false; } };