Index: branches/5.2.x/core/admin_templates/js/uploader/uploader.js =================================================================== --- branches/5.2.x/core/admin_templates/js/uploader/uploader.js (revision 16557) +++ branches/5.2.x/core/admin_templates/js/uploader/uploader.js (revision 16558) @@ -1,688 +1,688 @@ function Uploader(id, params) { this.id = id; this.instance = null; // normalize params if (isNaN(parseInt(params.multiple))) { // ensure that maximal file number is greater then zero params.multiple = 1; } params.allowedFilesize = this._normalizeFilesize(params.allowedFilesize); // set params to uploader this.ready = false; this.params = params; this._ensureDefaultValues(); this.files = []; this.files_count = 0; this.deleted = []; // because used outside this class this.deleteURL = params.deleteURL; this.enableUploadButton(); this._fixFileExtensions(); this._attachEventHandler(); var $me = this; if ( this.params.ajax ) { $(document).bind('FormManager.WindowManager.Ready', function ($e) { $me.init(); }); } else { $(document).ready(function() { $me.init(); }); } } /* ==== Private methods ==== */ Uploader.prototype._fixFileExtensions = function() { this.params.allowedFiletypes = this.params.allowedFiletypes.replace(/\*\./g, '').replace(/;/g, ','); }; Uploader.prototype._attachEventHandler = function() { var $me = this; $(document).bind('UploadsManager.Uploader.' + crc32(this.id), function ($e, $method) { $me[$method].apply($me, Array.prototype.slice.call(arguments, 2)); }); }; Uploader.prototype._ensureDefaultValues = function() { // Upload backend settings 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) { var $normalize_size = parseInt($file_size); if (isNaN($normalize_size)) { return $file_size; } // in kilobytes (flash doesn't recognize numbers, that are longer, then 9 digits) return $normalize_size / 1024; }; Uploader.prototype._prepareFiles = function() { var ids = '', names = ''; // process uploaded files for (var f = 0; f < this.files.length; f++) { if (isset(this.files[f].uploaded) && !isset(this.files[f].temp)) { continue; } ids += this.files[f].id + '|'; names += this.files[f].name + '|'; } ids = ids.replace(/\|$/, ''); names = names.replace(/\|$/, ''); document.getElementById(this.id+'[tmp_ids]').value = ids; document.getElementById(this.id+'[tmp_names]').value = names; document.getElementById(this.id+'[tmp_deleted]').value = this.deleted.join('|'); }; Uploader.prototype._formatSize = function (bytes) { var kb = Math.round(bytes / 1024); if (kb < 1024) { return kb + ' KB'; } var mb = Math.round(kb / 1024 * 100) / 100; return mb + ' MB'; }; /* ==== Public methods ==== */ Uploader.prototype.init = function() { var $me = this, $uploader_options = { - runtimes : 'flash,html4', // html5 + runtimes : 'html5,flash,html4', 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'; $uploader_options.init['Init'] = function(uploader) { $me.onReady(); }; $uploader_options.init['FilesAdded'] = function(uploader, files) { $.each(files, function (index, file) { $me.onFileQueued(file); }); $me.startUpload(); }; $uploader_options.init['FilesRemoved'] = function(uploader, files) { $.each(files, function (index, file) { if ( file.status != plupload.QUEUED ) { uploader.stop(); uploader.start(); } }); }; $uploader_options.init['Error'] = function(uploader, error) { $me.onError(error); }; $uploader_options.init['BeforeUpload'] = function(uploader, file) { return $me.onUploadFileStart(uploader, file); }; $uploader_options.init['UploadProgress'] = function(uploader, file) { $me.onUploadProgress(file); }; $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(); }; Uploader.prototype.refreshQueue = function($params) { if ( $params !== undefined ) { $.extend(true, this.params, $params); document.getElementById(this.id+'[upload]').value = this.params.names; document.getElementById(this.id+'[order]').value = this.params.names; } // 1. remove queue DIVs for files, that doesn't exist after upload was made var $new_file_ids = this.getFileIds(this.params.names); for (var $i = 0; $i < this.files.length; $i++) { if ( !in_array(this.files[$i].id, $new_file_ids) ) { this.updateQueueFile($i, true); } } this.files = []; this.files_count = 0; this.deleted = []; if (this.params.urls != '') { var urls = this.params.urls.split('|'), names = this.params.names.split('|'), sizes = this.params.sizes.split('|'); for (var i = 0; i < urls.length; i++) { var a_file = { // original properties from Uploader id : this.getUploadedFileId(names[i]), name : names[i], size: sizes[i], percent: 100, // custom properties url : urls[i], uploaded : 1 }; this.files_count++; this.files.push(a_file); } this.updateInfo(); } }; Uploader.prototype.getFileIds = function($file_names) { var $ret = []; if ( !$file_names.length ) { return $ret; } if ( !$.isArray($file_names) ) { $file_names = $file_names.split('|'); } for (var i = 0; i < $file_names.length; i++) { $ret.push(this.getUploadedFileId($file_names[i])) } return $ret; }; Uploader.prototype.getUploadedFileId = function($file_name) { return 'uploaded_' + crc32($file_name); }; Uploader.prototype.enableUploadButton = function() { // enable upload button, when plupload runtime is fully loaded $('#' + jq(this.id + '_browse_button')).prop('disabled', false).removeClass('button-disabled'); }; Uploader.prototype.renderBrowseButton = function() { this.instance.init(); }; Uploader.prototype.remove = function() { this.instance.destroy(); }; Uploader.prototype.isImage = function($filename) { this.removeTempExtension($filename).match(/\.([^.]*)$/); var $ext = RegExp.$1.toLowerCase(); return $ext.match(/^(bmp|gif|jpg|jpeg|png)$/); }; Uploader.prototype.getFileIcon = function($filename) { this.removeTempExtension($filename).match(/\.([^.]*)$/); var $ext = RegExp.$1.toLowerCase(), $ext_overrides = { 'doc': '^(docx|dotx|docm|dotm)$', 'xls': '^(xlsx|xltx|xlsm|xltm|xlam|xlsb)$', 'ppt': '^(pptx|potx|ppsx|ppam|pptm|potm|ppsm)$' }; $.each($ext_overrides, function ($new_ext, $expression) { var $regexp = new RegExp($expression); if ( $ext.match($regexp) ) { $ext = $new_ext; return false; } return true; }); var $icon = $ext.match(/^(ai|avi|bmp|cs|dll|doc|dot|exe|fla|gif|htm|html|jpg|js|mdb|mp3|pdf|ppt|rdp|swf|swt|txt|vsd|xls|xml|zip)$/) ? $ext : 'default.icon'; return this.IconPath + '/' + $icon + '.gif'; }; Uploader.prototype.removeTempExtension = function ($file) { return $file.replace(/(_[\d]+)?\.tmp$/, ''); }; Uploader.prototype.getQueueElement = function($file) { 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 $need_preview = (this.params.thumb_format.length > 0) && this.isImage($file.name); $ret += '
'; // add icon based on file type $ret += ''; // add filename + preview link $ret += ''; } else { // add icon based on file type $ret += ''; // add filename $ret += '