📄 ext.ux.fileuploader.js
字号:
// vim: ts=4:sw=4:nu:fdc=4:nospell/** * Ext.ux.FileUploader * * @author Ing. Jozef Sakáloš * @version $Id: Ext.ux.FileUploader.js 83 2008-03-21 12:54:35Z jozo $ * @date 15. March 2008 * * @license Ext.ux.FileUploader is licensed under the terms of * the Open Source LGPL 3.0 license. Commercial use is permitted to the extent * that the code/component(s) do NOT become part of another Open Source or Commercially * licensed development library or toolkit without explicit permission. * * License details: http://www.gnu.org/licenses/lgpl.html *//*global Ext *//** * @class Ext.ux.FileUploader * @extends Ext.util.Observable * @constructor */Ext.ux.FileUploader = function(config) { Ext.apply(this, config); // call parent Ext.ux.FileUploader.superclass.constructor.apply(this, arguments); // add events // {{{ this.addEvents( /** * @event beforeallstart * Fires before an upload (of all files) is started. Return false to cancel the event. * @param {Ext.ux.FileUploader} this */ 'beforeallstart' /** * @event allfinished * Fires after upload (of all files) is finished * @param {Ext.ux.FileUploader} this */ ,'allfinished' /** * @event beforefilestart * Fires before the file upload is started. Return false to cancel the event. * Fires only when singleUpload = false * @param {Ext.ux.FileUploader} this * @param {Ext.data.Record} record upload of which is being started */ ,'beforefilestart' /** * @event filefinished * Fires when file finished uploading. * Fires only when singleUpload = false * @param {Ext.ux.FileUploader} this * @param {Ext.data.Record} record upload of which has finished */ ,'filefinished' /** * @event progress * Fires when progress has been updated * @param {Ext.ux.FileUploader} this * @param {Object} data Progress data object * @param {Ext.data.Record} record Only if singleUpload = false */ ,'progress' ); // }}}}; // eo constructorExt.extend(Ext.ux.FileUploader, Ext.util.Observable, { // configuration options // {{{ /** * @cfg {Object} baseParams baseParams are sent to server in each request. */ baseParams:{cmd:'upload',dir:'.'} /** * @cfg {Boolean} concurrent true to start all requests upon upload start, false to start * the next request only if previous one has been completed (or failed). Applicable only if * singleUpload = false */ ,concurrent:true /** * @cfg {Boolean} enableProgress true to enable querying server for progress information */ ,enableProgress:true /** * @cfg {String} jsonErrorText Text to use for json error */ ,jsonErrorText:'Cannot decode JSON object' /** * @cfg {Number} Maximum client file size in bytes */ ,maxFileSize:524288 /** * @cfg {String} progressIdName Name to give hidden field for upload progress identificator */ ,progressIdName:'UPLOAD_IDENTIFIER' /** * @cfg {Number} progressInterval How often (in ms) is progress requested from server */ ,progressInterval:2000 /** * @cfg {String} progressUrl URL to request upload progress from */ ,progressUrl:'progress.php' /** * @cfg {Object} progressMap Mapping of received progress fields to store progress fields */ ,progressMap:{ bytes_total:'bytesTotal' ,bytes_uploaded:'bytesUploaded' ,est_sec:'estSec' ,files_uploaded:'filesUploaded' ,speed_average:'speedAverage' ,speed_last:'speedLast' ,time_last:'timeLast' ,time_start:'timeStart' } /** * @cfg {Boolean} singleUpload true to upload files in one form, false to upload one by one */ ,singleUpload:false /** * @cfg {Ext.data.Store} store Mandatory. Store that holds files to upload */ /** * @cfg {String} unknownErrorText Text to use for unknow error */ ,unknownErrorText:'Unknown error' /** * @cfg {String} url Mandatory. URL to upload to */ // }}} // private // {{{ /** * uploads in progress count * @private */ ,upCount:0 // }}} // methods // {{{ /** * creates form to use for upload. * @private * @return {Ext.Element} form */ ,createForm:function(record) { var progressId = parseInt(Math.random() * 1e10, 10); var form = Ext.getBody().createChild({ tag:'form' ,action:this.url ,method:'post' ,cls:'x-hidden' ,id:Ext.id() ,cn:[{ tag:'input' ,type:'hidden' ,name:'APC_UPLOAD_PROGRESS' ,value:progressId },{ tag:'input' ,type:'hidden' ,name:this.progressIdName ,value:progressId },{ tag:'input' ,type:'hidden' ,name:'MAX_FILE_SIZE' ,value:this.maxFileSize }] }); if(record) { record.set('form', form); record.set('progressId', progressId); } else { this.progressId = progressId; } return form; } // eo function createForm // }}} // {{{ ,deleteForm:function(form, record) { form.remove(); if(record) { record.set('form', null); } } // eo function deleteForm // }}} // {{{ /** * Fires event(s) on upload finish/error * @private */ ,fireFinishEvents:function(options) { if(true !== this.eventsSuspended && !this.singleUpload) { this.fireEvent('filefinished', this, options && options.record); } if(true !== this.eventsSuspended && 0 === this.upCount) { this.stopProgress(); this.fireEvent('allfinished', this); } } // eo function fireFinishEvents // }}} // {{{ /** * Geg the iframe identified by record * @private * @param {Ext.data.Record} record * @return {Ext.Element} iframe or null if not found */ ,getIframe:function(record) { var iframe = null; var form = record.get('form'); if(form && form.dom && form.dom.target) { iframe = Ext.get(form.dom.target); } return iframe; } // eo function getIframe // }}} // {{{ /** * returns options for Ajax upload request * @private * @param {Ext.data.Record} record * @param {Object} params params to add */ ,getOptions:function(record, params) { var o = { url:this.url ,method:'post' ,isUpload:true ,scope:this ,callback:this.uploadCallback ,record:record ,params:this.getParams(record, params) }; return o; } // eo function getOptions // }}} // {{{ /** * get params to use for request * @private * @return {Object} params */ ,getParams:function(record, params) { var p = {path:this.path}; Ext.apply(p, this.baseParams || {}, params || {}); return p; } // }}} // {{{ /** * processes success response * @private * @param {Object} options options the request was called with * @param {Object} response request response object * @param {Object} o decoded response.responseText */ ,processSuccess:function(options, response, o) { var record = false; // all files uploadded ok if(this.singleUpload) { this.store.each(function(r) { r.set('state', 'done'); r.set('error', ''); r.commit(); }); } else { record = options.record; record.set('state', 'done'); record.set('error', ''); record.commit(); } this.deleteForm(options.form, record); } // eo processSuccess // }}} // {{{ /** * processes failure response * @private * @param {Object} options options the request was called with * @param {Object} response request response object * @param {String/Object} error Error text or JSON decoded object. Optional. */ ,processFailure:function(options, response, error) { var record = options.record; var records; // singleUpload - all files uploaded in one form if(this.singleUpload) { // some files may have been successful records = this.store.queryBy(function(r){return 'done' !== r.get('state');}); records.each(function(record) { var e = error.errors ? error.errors[record.id] : this.unknownErrorText; if(e) { record.set('state', 'failed'); record.set('error', e); Ext.getBody().appendChild(record.get('input')); } else { record.set('state', 'done'); record.set('error', ''); } record.commit(); }, this); this.deleteForm(options.form);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -