// $Id: jw_player.js, v 0.1, 2009/07/28 12:11:24, skilip Exp $; (function ($) { /** * */ function SWFU(id, settings) { var ref = {}; ref.settings = {}; ref.ajax_settings = {}; ref.queue = {}; ref.stats = {}; ref.instance = {}; ref.upload_stack_length = 0; ref.max_queue_size = 0; ref.upload_stack = {}; ref.upload_stack_obj; ref.upload_button_obj; ref.upload_stack_size = 0; ref.wrapper_obj; ref.wrapper_id; ref.num_elements; ref.key_pressed; ref.message_wrapper_obj; ref.messages_timeout; /** * */ ref.init = function() { ref.settings = settings; ref.upload_button_obj = $('#' + ref.settings.upload_button_id); ref.instance = {name:settings.file_post_name}; ref.ajax_settings = { type:"post", url:ref.settings.upload_url, data:{ op:'init', file_path:ref.settings.post_params.file_path, instance:ref.toJson(ref.instance), instance_settings:ref.settings.post_params.instance_settings }, success:function(result) { ref.ajaxResponse(result); } }; ref.prepareSWFButton(); // Get the instance data by an AJAX request in order to let other modules change the callbacks and elements for this instance (using hook_swfupload); $.ajax(ref.ajax_settings); }; /** * Prepares the swfupload button. */ ref.prepareSWFButton = function() { // Create a copy of the button to get it's dimensions. // If we'd use the original button, we could end up with dimensions equal to 0px when the button is inside a hidden fieldset. var tmp_button = ref.upload_button_obj.clone().css({'position':'absolute'}).prependTo('body'); // Set the dimensions of the swf so it matches exactly the dimensions of the upload button // swfupload.swf will be placed exactly over the upload button ref.settings.button_width = (tmp_button.find('.left').width() + tmp_button.find('.center').width() + tmp_button.find('.right').width()); ref.settings.button_height = tmp_button.find('.center').height(); tmp_button.remove(); // Add the other button settings to the settings object ref.settings.button_placeholder_id = ref.settings.file_post_name + '-swfwrapper'; ref.settings.button_window_mode = SWFUpload.WINDOW_MODE.TRANSPARENT; ref.settings.button_cursor = SWFUpload.CURSOR.HAND; }; /** * Creates a hidden input field which will contain a JSON formatted string containing all uploaded files */ ref.createStackObj = function() { var upload_stack_value = settings.custom_settings.upload_stack_value; ref.max_queue_size = settings.custom_settings.max_queue_size; ref.upload_stack_obj = $('').attr('name', ref.instance.name).val(upload_stack_value).prependTo(ref.upload_button_obj); ref.upload_stack = jQuery.parseJSON(upload_stack_value); ref.upload_stack_length = ref.objectLength(ref.upload_stack); }; /** * */ ref.newSWFUpload = function() { ref.swfu = new SWFUpload(ref.settings); }; /** * */ ref.ajaxResponse = function(result) { var result = jQuery.parseJSON(result); switch (result.op) { case 'init': ref.instance = result.instance; ref.num_elements = ref.objectLength(ref.instance.elements); $.each(result.instance.callbacks, function(setting, callback) { ref.settings[setting] = eval(callback); }); ref.newSWFUpload(); ref.settings.init_complete_handler(result); break; }; ref.addEventHandlers(result.op); }; /** * Custom function for when the initialization is complete * This event handler is defined in swfupload.module as an instance callback function */ ref.initComplete = function(result) { ref.createWrapper(result.instance.name); ref.createStackObj(); ref.addStoredFiles(); // Enable the upload button if the current stack is smaller than the allowed stack size, // or when there's no limit at all. if ((ref.settings.file_upload_limit && (ref.upload_stack_length < ref.settings.file_upload_limit)) || ref.settings.file_upload_limit === 0) { ref.upload_button_obj.removeClass('disabled').css({opacity:1}); } else { ref.upload_button_obj.addClass('disabled').css({opacity:0.4}); }; }; /** * This will process all file elements stored in the upload stack. * The upload represents all files submitted in the upload form. * For all files in the stack, a file element will be added to the wrapper using ref.addFileItem(). */ ref.addStoredFiles = function() { for(var i in ref.upload_stack) { if (ref.upload_stack[i] == 0) { break; }; ref.upload_stack[i].id = i; ref.upload_stack[i].fid = i; ref.upload_stack[i].extension = ref.getExtension(ref.upload_stack[i].filename); ref.addFileItem(ref.upload_stack[i]); // Adjust the bytes in the stack. ref.upload_stack_size += parseInt(ref.upload_stack[i].filesize); }; ref.addEventHandlers('drag_enable'); }; /** * Places the wrapper markup above the upload button * Depending on what type isset by the instance, a table or a list element is created. */ ref.createWrapper = function(field_name) { var use_header = false; var element; if (ref.num_elements > 1 && ref.instance.type == 'table') { // First we'll check if we need to create a header for (var name in ref.instance.elements) { if (ref.instance.elements[name].title) { use_header = true; }; }; ref.wrapper_id = 'swfupload_file_wrapper-' + field_name.replace(/[\[\]]/g, '-'); ref.wrapper_obj = $('').attr({'id': ref.wrapper_id, 'class':'swfupload'}); if (use_header) { ref.wrapper_obj.append($('').append(ref.tableRow(true))); }; ref.wrapper_obj.append($('').append(ref.tableRow())); ref.upload_button_obj.before(ref.wrapper_obj); if (!Drupal.settings.tableDrag) { Drupal.settings.tableDrag = {}; }; Drupal.settings.tableDrag[ref.wrapper_id] = {}; }; }; /** * Creates or changes a tablerow * @param header Boolean Wheter or not the tablerow should contain th's. If sety to false, td's will be generated. * @param file Object A completed file object * - If this is not set, a row is created including the progressbar, which replaces the td's with contains_progressbar set to true. * - If file is set, the progressbar will be replaced with the appropriate td's */ ref.tableRow = function(header, file) { var counter = 0; var colspan = 0; var fid = (file) ? file.fid : 0; var progress_td_counter = 0; var element, colum, content, input, progress_td, elem_value, value; var tr = (file) ? $('#' + file.fid) : $(''); var wrapper = $('
').addClass('wrapper'); var left_span = $('
').addClass('left').html(' '); var center_span = $('
').addClass('center'); var right_span = $('
').addClass('right').html(' '); // A tablerow will be created containing all elements defined in ref.instance.elements. // If file is set, all elements will be skipped exept the ones with 'contains_progressbar' // If file isn't set, this tablerow will be hidden. for (var name in ref.instance.elements) { counter++; element = ref.instance.elements[name]; if (file) { if(!element.contains_progressbar) { // The current td doesn't have to be replaced. // We only need to replace fid of the id and name of the input field tr.find('#edit-' + name + '_0').attr({'name':name +'_' + fid, 'id':'edit-' + name + '_' + fid}); continue; }; } else { if (!header && element.contains_progressbar) { if (!progress_td) { progress_td = $('
').addClass('progress').append($('
').addClass('sfwupload-list-progressbar').append($('
').addClass('sfwupload-list-progressbar-status')).append($('
').addClass('sfwupload-list-progressbar-glow'))).appendTo(tr); }; progress_td_counter++; continue; }; }; column = $((header ? '
' : '')); content = wrapper.clone().appendTo(column); input = $((element.type == 'textarea' ? '