123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- ;(function($, plupload) {
- var uploaders = {};
- function _(str) {
- return plupload.translate(str) || str;
- }
- function renderUI(id, target) {
-
- target.contents().each(function(i, node) {
- node = $(node);
- if (!node.is('.plupload')) {
- node.remove();
- }
- });
- target.prepend(
- '<div class="plupload_wrapper plupload_scroll">' +
- '<div id="' + id + '_container" class="plupload_container">' +
- '<div class="plupload">' +
- '<div class="plupload_header">' +
- '<div class="plupload_header_content">' +
- '<div class="plupload_header_title">' + _('Select files') + '</div>' +
- '<div class="plupload_header_text">' + _('Add files to the upload queue and click the start button.') + '</div>' +
- '</div>' +
- '</div>' +
- '<div class="plupload_content">' +
- '<div class="plupload_filelist_header">' +
- '<div class="plupload_file_name">' + _('Filename') + '</div>' +
- '<div class="plupload_file_action"> </div>' +
- '<div class="plupload_file_status"><span>' + _('Status') + '</span></div>' +
- '<div class="plupload_file_size">' + _('Size') + '</div>' +
- '<div class="plupload_clearer"> </div>' +
- '</div>' +
- '<ul id="' + id + '_filelist" class="plupload_filelist"></ul>' +
- '<div class="plupload_filelist_footer">' +
- '<div class="plupload_file_name">' +
- '<div class="plupload_buttons">' +
- '<a href="#" class="plupload_button plupload_add" id="' + id + '_browse">' + _('Add Files') + '</a>' +
- '<a href="#" class="plupload_button plupload_start">' + _('Start Upload') + '</a>' +
- '</div>' +
- '<span class="plupload_upload_status"></span>' +
- '</div>' +
- '<div class="plupload_file_action"></div>' +
- '<div class="plupload_file_status"><span class="plupload_total_status">0%</span></div>' +
- '<div class="plupload_file_size"><span class="plupload_total_file_size">0 b</span></div>' +
- '<div class="plupload_progress">' +
- '<div class="plupload_progress_container">' +
- '<div class="plupload_progress_bar"></div>' +
- '</div>' +
- '</div>' +
- '<div class="plupload_clearer"> </div>' +
- '</div>' +
- '</div>' +
- '</div>' +
- '</div>' +
- '<input type="hidden" id="' + id + '_count" name="' + id + '_count" value="0" />' +
- '</div>'
- );
- }
- $.fn.pluploadQueue = function(settings) {
- if (settings) {
- this.each(function() {
- var uploader, target, id, contents_bak;
- target = $(this);
- id = target.attr('id');
- if (!id) {
- id = plupload.guid();
- target.attr('id', id);
- }
- contents_bak = target.html();
- renderUI(id, target);
- settings = $.extend({
- dragdrop : true,
- browse_button : id + '_browse',
- container : id
- }, settings);
-
- if (settings.dragdrop) {
- settings.drop_element = id + '_filelist';
- }
- uploader = new plupload.Uploader(settings);
- uploaders[id] = uploader;
- function handleStatus(file) {
- var actionClass;
- if (file.status == plupload.DONE) {
- actionClass = 'plupload_done';
- }
- if (file.status == plupload.FAILED) {
- actionClass = 'plupload_failed';
- }
- if (file.status == plupload.QUEUED) {
- actionClass = 'plupload_delete';
- }
- if (file.status == plupload.UPLOADING) {
- actionClass = 'plupload_uploading';
- }
- var icon = $('#' + file.id).attr('class', actionClass).find('a').css('display', 'block');
- if (file.hint) {
- icon.attr('title', file.hint);
- }
- }
- function updateTotalProgress() {
- $('span.plupload_total_status', target).html(uploader.total.percent + '%');
- $('div.plupload_progress_bar', target).css('width', uploader.total.percent + '%');
- $('span.plupload_upload_status', target).html(
- plupload.sprintf(_('Uploaded %d/%d files'), uploader.total.uploaded, uploader.files.length)
- );
- }
- function updateList() {
- var fileList = $('ul.plupload_filelist', target).html(''), inputCount = 0, inputHTML;
- $.each(uploader.files, function(i, file) {
- inputHTML = '';
- if (file.status == plupload.DONE) {
- if (file.target_name) {
- inputHTML += '<input type="hidden" name="' + id + '_' + inputCount + '_tmpname" value="' + plupload.xmlEncode(file.target_name) + '" />';
- }
- inputHTML += '<input type="hidden" name="' + id + '_' + inputCount + '_name" value="' + plupload.xmlEncode(file.name) + '" />';
- inputHTML += '<input type="hidden" name="' + id + '_' + inputCount + '_status" value="' + (file.status == plupload.DONE ? 'done' : 'failed') + '" />';
- inputCount++;
- $('#' + id + '_count').val(inputCount);
- }
- fileList.append(
- '<li id="' + file.id + '">' +
- '<div class="plupload_file_name"><span>' + file.name + '</span></div>' +
- '<div class="plupload_file_action"><a href="#"></a></div>' +
- '<div class="plupload_file_status">' + file.percent + '%</div>' +
- '<div class="plupload_file_size">' + plupload.formatSize(file.size) + '</div>' +
- '<div class="plupload_clearer"> </div>' +
- inputHTML +
- '</li>'
- );
- handleStatus(file);
- $('#' + file.id + '.plupload_delete a').click(function(e) {
- $('#' + file.id).remove();
- uploader.removeFile(file);
- e.preventDefault();
- });
- });
- $('span.plupload_total_file_size', target).html(plupload.formatSize(uploader.total.size));
- if (uploader.total.queued === 0) {
- $('span.plupload_add_text', target).html(_('Add Files'));
- } else {
- $('span.plupload_add_text', target).html(plupload.sprintf(_('%d files queued'), uploader.total.queued));
- }
- $('a.plupload_start', target).toggleClass('plupload_disabled', uploader.files.length == (uploader.total.uploaded + uploader.total.failed));
-
- fileList[0].scrollTop = fileList[0].scrollHeight;
- updateTotalProgress();
-
- if (!uploader.files.length && uploader.features.dragdrop && uploader.settings.dragdrop) {
- $('#' + id + '_filelist').append('<li class="plupload_droptext">' + _("Drag files here.") + '</li>');
- }
- }
- function destroy() {
- delete uploaders[id];
- uploader.destroy();
- target.html(contents_bak);
- uploader = target = contents_bak = null;
- }
- uploader.bind("UploadFile", function(up, file) {
- $('#' + file.id).addClass('plupload_current_file');
- });
- uploader.bind('Init', function(up, res) {
-
- if (!settings.unique_names && settings.rename) {
- target.on('click', '#' + id + '_filelist div.plupload_file_name span', function(e) {
- var targetSpan = $(e.target), file, parts, name, ext = "";
- var fileContainer = targetSpan.closest('li');
- if (!fileContainer.hasClass('plupload_delete')) {
- return;
- }
-
- file = up.getFile(targetSpan.parents('li')[0].id);
- name = file.name;
- parts = /^(.+)(\.[^.]+)$/.exec(name);
- if (parts) {
- name = parts[1];
- ext = parts[2];
- }
-
- targetSpan.hide().after('<input type="text" />');
- targetSpan.next().val(name).focus().blur(function() {
- targetSpan.show().next().remove();
- }).keydown(function(e) {
- var targetInput = $(this);
- if (e.keyCode == 13) {
- e.preventDefault();
-
- file.name = targetInput.val() + ext;
- targetSpan.html(file.name);
- targetInput.blur();
- }
- });
- });
- }
- $('#' + id + '_container').attr('title', 'Using runtime: ' + res.runtime);
- $('a.plupload_start', target).click(function(e) {
- if (!$(this).hasClass('plupload_disabled')) {
- uploader.start();
- }
- e.preventDefault();
- });
- $('a.plupload_stop', target).click(function(e) {
- e.preventDefault();
- uploader.stop();
- });
- $('a.plupload_start', target).addClass('plupload_disabled');
- });
- uploader.bind("Error", function(up, err) {
- var file = err.file, message;
- if (file) {
- message = err.message;
- if (err.details) {
- message += " (" + err.details + ")";
- }
- if (err.code == plupload.FILE_SIZE_ERROR) {
- alert(_("Error: File too large:") + " " + file.name);
- }
- if (err.code == plupload.FILE_EXTENSION_ERROR) {
- alert(_("Error: Invalid file extension:") + " " + file.name);
- }
- file.hint = message;
- $('#' + file.id).attr('class', 'plupload_failed').find('a').css('display', 'block').attr('title', message);
- }
- if (err.code === plupload.INIT_ERROR) {
- setTimeout(function() {
- destroy();
- }, 1);
- }
- });
- uploader.bind("PostInit", function(up) {
-
- if (up.settings.dragdrop && up.features.dragdrop) {
- $('#' + id + '_filelist').append('<li class="plupload_droptext">' + _("Drag files here.") + '</li>');
- }
- });
- uploader.init();
- uploader.bind('StateChanged', function() {
- if (uploader.state === plupload.STARTED) {
- $('li.plupload_delete a,div.plupload_buttons', target).hide();
- uploader.disableBrowse(true);
- $('span.plupload_upload_status,div.plupload_progress,a.plupload_stop', target).css('display', 'block');
- $('span.plupload_upload_status', target).html('Uploaded ' + uploader.total.uploaded + '/' + uploader.files.length + ' files');
- if (settings.multiple_queues) {
- $('span.plupload_total_status,span.plupload_total_file_size', target).show();
- }
- } else {
- updateList();
- $('a.plupload_stop,div.plupload_progress', target).hide();
- $('a.plupload_delete', target).css('display', 'block');
- if (settings.multiple_queues && uploader.total.uploaded + uploader.total.failed == uploader.files.length) {
- $(".plupload_buttons,.plupload_upload_status", target).css("display", "inline");
- uploader.disableBrowse(false);
- $(".plupload_start", target).addClass("plupload_disabled");
- $('span.plupload_total_status,span.plupload_total_file_size', target).hide();
- }
- }
- });
- uploader.bind('FilesAdded', updateList);
- uploader.bind('FilesRemoved', function() {
-
-
-
- var scrollTop = $('#' + id + '_filelist').scrollTop();
- updateList();
- $('#' + id + '_filelist').scrollTop(scrollTop);
- });
- uploader.bind('FileUploaded', function(up, file) {
- handleStatus(file);
- });
- uploader.bind("UploadProgress", function(up, file) {
-
- $('#' + file.id + ' div.plupload_file_status', target).html(file.percent + '%');
- handleStatus(file);
- updateTotalProgress();
- });
-
- if (settings.setup) {
- settings.setup(uploader);
- }
- });
- return this;
- } else {
-
- return uploaders[$(this[0]).attr('id')];
- }
- };
- })(jQuery, plupload);
|