Index: branches/5.2.x/core/admin_templates/js/uploader/uploader.js =================================================================== diff -u -N -r15858 -r16416 --- branches/5.2.x/core/admin_templates/js/uploader/uploader.js (.../uploader.js) (revision 15858) +++ branches/5.2.x/core/admin_templates/js/uploader/uploader.js (.../uploader.js) (revision 16416) @@ -1,9 +1,6 @@ -// this js class name is hardcoded in flash object :( -var SWFUpload = function () {}; -SWFUpload.instances = {}; - function Uploader(id, params) { this.id = id; + this.instance = null; // normalize params if (isNaN(parseInt(params.multiple))) { @@ -14,9 +11,7 @@ params.allowedFilesize = this._normalizeFilesize(params.allowedFilesize); // set params to uploader - this._eventQueue = []; - this.uploadCancelled = false; - this.flashReady = false; + this.ready = false; this.params = params; this._ensureDefaultValues(); @@ -29,6 +24,7 @@ this.deleteURL = params.deleteURL; this.enableUploadButton(); + this._fixFileExtensions(); this._attachEventHandler(); var $me = this; @@ -46,6 +42,10 @@ } /* ==== Private methods ==== */ +Uploader.prototype._fixFileExtensions = function() { + this.params.allowedFiletypes = this.params.allowedFiletypes.replace(/\*\./g, '').replace(/;/g, ','); +}; + Uploader.prototype._attachEventHandler = function() { var $me = this; @@ -56,46 +56,31 @@ Uploader.prototype._ensureDefaultValues = function() { // Upload backend settings - var $defaults = { - baseUrl : '', - uploadURL : '', - deleteURL : '', - previewURL : '', - useQueryString : false, - requeueOnError : false, - httpSuccess : '', - filePostName : 'Filedata', - allowedFiletypes : '*.*', - allowedFiletypesDescription : 'All Files', - allowedFilesize : 0, // Default zero means "unlimited" - multiple : 0, - field : '', - thumb_format: '', - urls : '', - names : '', - sizes : '', - fileQueueLimit : 0, - buttonImageURL : '', - buttonWidth : 1, - buttonHeight : 1, - buttonText : '', - buttonTextTopPadding : 0, - buttonTextLeftPadding : 0, - buttonTextStyle : 'color: #000000; font-size: 16pt;', - buttonAction : parseInt(this.params.multiple) == 1 ? -100 : -110, // SELECT_FILE : -100, SELECT_FILES : -110 - buttonDisabled : true, //false, - buttonCursor : -1, // ARROW : -1, HAND : -2 - wmode : 'transparent', // "window", "transparent", "opaque" - buttonPlaceholderId: false, - ajax: false - }; - for (var $param_name in $defaults) { - if (this.params[$param_name] == null) { -// console.log('setting default value [', $defaults[$param_name], '] for missing parameter [', $param_name, '] instead of [', this.params[$param_name], ']'); - this.params[$param_name] = $defaults[$param_name]; + var $me = this, + $defaults = { + baseUrl : '', + uploadURL : '', + deleteURL : '', + previewURL : '', + allowedFiletypes : '*.*', + allowedFiletypesDescription : 'All Files', + allowedFilesize : 0, // Default zero means "unlimited" + multiple : 0, + field : '', + thumb_format: '', + urls : '', + names : '', + sizes : '', + ajax: false + }; + + $.each($defaults, function ($param_name, $param_value) { + if ($me.params[$param_name] == null) { +// console.log('setting default value [', $param_value, '] for missing parameter [', $param_name, '] instead of [', $me.params[$param_name], ']'); + $me.params[$param_name] = $param_value; } - } + }); }; Uploader.prototype._normalizeFilesize = function($file_size) { @@ -109,8 +94,8 @@ }; Uploader.prototype._prepareFiles = function() { - var ids = ''; - var names = ''; + var ids = '', + names = ''; // process uploaded files for (var f = 0; f < this.files.length; f++) { @@ -122,8 +107,8 @@ names += this.files[f].name + '|'; } - ids = ids.replace(/\|$/, '', ids); - names = names.replace(/\|$/, '', names); + ids = ids.replace(/\|$/, ''); + names = names.replace(/\|$/, ''); document.getElementById(this.id+'[tmp_ids]').value = ids; document.getElementById(this.id+'[tmp_names]').value = names; @@ -142,71 +127,78 @@ return mb + ' MB'; }; -Uploader.prototype._executeNextEvent = function () { - var f = this._eventQueue ? this._eventQueue.shift() : null; - if (typeof(f) === 'function') { - f.apply(this); - } -}; - /* ==== Public methods ==== */ Uploader.prototype.init = function() { + var $me = this, + $uploader_options = { + runtimes : 'flash,html4', // html5 + chunk_size: '1mb', + browse_button : this.id + '_browse_button', + container: this.id + '_container', + url : this.params.uploadURL, + flash_swf_url : this.params.baseUrl + '/Moxie.swf', + multi_selection: this.params.multiple > 1, + filters : {}, + init: {} + }; + + if ( this.params.allowedFilesize > 0 ) { + $uploader_options.filters.max_file_size = this.params.allowedFilesize + 'kb'; + } + + if ( this.params.allowedFiletypes != '*' ) { + $uploader_options.filters.mime_types = [ + {title : this.params.allowedFiletypesDescription, extensions : this.params.allowedFiletypes} + ]; + } + this.IconPath = this.params.IconPath ? this.params.IconPath : '../admin_templates/img/browser/icons'; - // initialize flash object - this.flash_id = UploadsManager._nextFlashId(); + $uploader_options.init['Init'] = function(uploader) { + $me.onReady(); + }; - // add callbacks for every event, because none of callbacks will work in other case (see swfupload documentation) - SWFUpload.instances[this.flash_id] = this; - SWFUpload.instances[this.flash_id].flashReady = function () { UploadsManager.onFlashReady(this.id); }; - SWFUpload.instances[this.flash_id].fileDialogStart = UploadsManager.onHandleEverything; - SWFUpload.instances[this.flash_id].fileQueued = UploadsManager.onFileQueued; - SWFUpload.instances[this.flash_id].fileQueueError = UploadsManager.onFileQueueError; - SWFUpload.instances[this.flash_id].fileDialogComplete = UploadsManager.onHandleEverything; + $uploader_options.init['FilesAdded'] = function(uploader, files) { + $.each(files, function (index, file) { + $me.onFileQueued(file); + }); - SWFUpload.instances[this.flash_id].uploadStart = UploadsManager.onUploadStart; - SWFUpload.instances[this.flash_id].uploadProgress = UploadsManager.onUploadProgress; - SWFUpload.instances[this.flash_id].uploadError = UploadsManager.onUploadError; - SWFUpload.instances[this.flash_id].uploadSuccess = UploadsManager.onUploadSuccess; - SWFUpload.instances[this.flash_id].uploadComplete = UploadsManager.onUploadComplete; - SWFUpload.instances[this.flash_id].debug = UploadsManager.onDebug; + $me.startUpload(); + }; - this.swf = new SWFObject(this.params.baseUrl + '/swfupload.swf', this.flash_id, this.params.buttonWidth, this.params.buttonHeight, '9', '#FFFFFF'); - this.swf.setAttribute('style', ''); - this.swf.addParam('wmode', encodeURIComponent(this.params.wmode)); + $uploader_options.init['FilesRemoved'] = function(uploader, files) { + $.each(files, function (index, file) { + if ( file.status != plupload.QUEUED ) { + uploader.stop(); + uploader.start(); + } + }); + }; - this.swf.addVariable('movieName', encodeURIComponent(this.flash_id)); - this.swf.addVariable('fileUploadLimit', 0); - this.swf.addVariable('fileQueueLimit', encodeURIComponent(this.params.fileQueueLimit)); - this.swf.addVariable('fileSizeLimit', encodeURIComponent(this.params.allowedFilesize)); // in kilobytes - this.swf.addVariable('fileTypes', encodeURIComponent(this.params.allowedFiletypes)); - this.swf.addVariable('fileTypesDescription', encodeURIComponent(this.params.allowedFiletypesDescription)); - this.swf.addVariable('uploadURL', encodeURIComponent(this.params.uploadURL)); + $uploader_options.init['Error'] = function(uploader, error) { + $me.onError(error); + }; - // upload button appearance - this.swf.addVariable('buttonImageURL', encodeURIComponent(this.params.buttonImageURL)); - this.swf.addVariable('buttonWidth', encodeURIComponent(this.params.buttonWidth)); - this.swf.addVariable('buttonHeight', encodeURIComponent(this.params.buttonHeight)); - this.swf.addVariable('buttonText', encodeURIComponent(this.params.buttonText)); - this.swf.addVariable('buttonTextTopPadding', encodeURIComponent(this.params.buttonTextTopPadding)); - this.swf.addVariable('buttonTextLeftPadding', encodeURIComponent(this.params.buttonTextLeftPadding)); - this.swf.addVariable('buttonTextStyle', encodeURIComponent(this.params.buttonTextStyle)); - this.swf.addVariable('buttonAction', encodeURIComponent(this.params.buttonAction)); - this.swf.addVariable('buttonDisabled', encodeURIComponent(this.params.buttonDisabled)); - this.swf.addVariable('buttonCursor', encodeURIComponent(this.params.buttonCursor)); + $uploader_options.init['BeforeUpload'] = function(uploader, file) { + return $me.onUploadFileStart(uploader, file); + }; - if (UploadsManager._debugMode) { - this.swf.addVariable('debugEnabled', encodeURIComponent('true')); // flash var - } + $uploader_options.init['UploadProgress'] = function(uploader, file) { + $me.onUploadProgress(file); + }; - var $me = this; + $uploader_options.init['FileUploaded'] = function(uploader, file, response) { + $me.onUploadFinished(file, response); + }; + this.instance = new plupload.Uploader($uploader_options); + Application.setHook( 'm:OnAfterFormInit', function () { $me.renderBrowseButton(); } - ) + ); this.refreshQueue(); }; @@ -239,12 +231,15 @@ for (var i = 0; i < urls.length; i++) { var a_file = { + // original properties from Uploader id : this.getUploadedFileId(names[i]), name : names[i], - url : urls[i], size: sizes[i], - uploaded : 1, - progress: 100 + percent: 100, + + // custom properties + url : urls[i], + uploaded : 1 }; this.files_count++; @@ -278,67 +273,18 @@ }; Uploader.prototype.enableUploadButton = function() { - var $me = this; - - // enable upload button, when flash is fully loaded - this.queueEvent( - function() { - setTimeout( - function () { - $me.callFlash('SetButtonDisabled', [false]); - }, 0 - ) - } - ); + // enable upload button, when plupload runtime is fully loaded + $('#' + jq(this.id + '_browse_button')).prop('disabled', false).removeClass('button-disabled'); }; Uploader.prototype.renderBrowseButton = function() { - var holder = document.getElementById(this.params.buttonPlaceholderId); - this.swf.write(holder); - - this.flash = document.getElementById(this.flash_id); + this.instance.init(); }; Uploader.prototype.remove = function() { - var id = this.params.buttonPlaceholderId; - - var obj = document.getElementById(id); - - if (obj/* && obj.nodeName == "OBJECT"*/) { - var u = navigator.userAgent.toLowerCase(); - var p = navigator.platform.toLowerCase(); - var windows = p ? /win/.test(p) : /win/.test(u); - var $me = this; - - if (document.all && windows) { - obj.style.display = "none"; - (function(){ - if (obj.readyState == 4) { - $me.removeObjectInIE(id); - } - else { - setTimeout(arguments.callee, 10); - } - })(); - } - else { - obj.parentNode.removeChild(obj); - } - } + this.instance.destroy(); }; -Uploader.prototype.removeObjectInIE = function(id) { - var obj = document.getElementById(id); - if (obj) { - for (var i in obj) { - if (typeof obj[i] == 'function') { - obj[i] = null; - } - } - obj.parentNode.removeChild(obj); - } -}; - Uploader.prototype.isImage = function($filename) { this.removeTempExtension($filename).match(/\.([^.]*)$/); @@ -379,10 +325,11 @@ }; Uploader.prototype.getQueueElement = function($file) { - var $ret = ''; - var $icon_image = this.getFileIcon($file.name); - var $file_label = this.removeTempExtension($file.name) + ' (' + this._formatSize($file.size) + ')'; - var $need_preview = false; + var $me = this, + $ret = '', + $icon_image = this.getFileIcon($file.name), + $file_label = this.removeTempExtension($file.name) + ' (' + this._formatSize($file.size) + ')', + $need_preview = false; if (isset($file.uploaded)) { // add deletion checkbox @@ -393,7 +340,7 @@ $ret += '