⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ext.ux.uploadpanel.js

📁 java + Ext 实现文件上传功能
💻 JS
📖 第 1 页 / 共 2 页
字号:
		// relay uploader events		this.relayEvents(this.uploader, [			 'beforeallstart'			,'allfinished'			,'progress'		]);		// install event handlers		this.on({			 beforeallstart:{scope:this, fn:function() {			 	this.uploading = true;				this.updateButtons();			}}			,allfinished:{scope:this, fn:function() {				this.uploading = false;				this.updateButtons();			}}			,progress:{fn:this.onProgress.createDelegate(this)}		});	} // eo function initComponent	// }}}	// {{{	/**	 * onRender override, saves references to buttons	 * @private	 */	,onRender:function() {		// call parent		Ext.ux.UploadPanel.superclass.onRender.apply(this, arguments);		// save useful references		var tb = 'tbar' === this.buttonsAt ? this.getTopToolbar() : this.getBottomToolbar();		this.addBtn = Ext.getCmp(tb.items.first().id);		this.uploadBtn = Ext.getCmp(tb.items.itemAt(1).id);		this.removeAllBtn = Ext.getCmp(tb.items.last().id);	} // eo function onRender	// }}}	// added methods	// {{{	/**	 * called by XTemplate to get qtip depending on state	 * @private	 * @param {Object} values XTemplate values	 */	,getQtip:function(values) {		var qtip = '';		switch(values.state) {			case 'queued':				qtip = String.format(this.fileQueuedText, values.fileName);				qtip += '<br>' + this.clickRemoveText;			break;			case 'uploading':				qtip = String.format(this.fileUploadingText, values.fileName);				qtip += '<br>' + values.pctComplete + '% done';				qtip += '<br>' + this.clickStopText;			break;			case 'done':				qtip = String.format(this.fileDoneText, values.fileName);				qtip += '<br>' + this.clickRemoveText;			break;			case 'failed':				qtip = String.format(this.fileFailedText, values.fileName);				qtip += '<br>' + this.errorText + ':' + values.error;				qtip += '<br>' + this.clickRemoveText;			break;			case 'stopped':				qtip = String.format(this.fileStoppedText, values.fileName);				qtip += '<br>' + this.clickRemoveText;			break;		}		return qtip;	} // eo function getQtip	// }}}	// {{{	/**	 * get file name	 * @private	 * @param {Ext.Element} inp Input element containing the full file path	 * @return {String}	 */	,getFileName:function(inp) {		return inp.getValue().split(/[\/\\]/).pop();	} // eo function getFileName	// }}}	// {{{	/**	 * get file path (excluding the file name)	 * @private	 * @param {Ext.Element} inp Input element containing the full file path	 * @return {String}	 */	,getFilePath:function(inp) {		return inp.getValue().replace(/[^\/\\]+$/,'');	} // eo function getFilePath	// }}}	// {{{	/**	 * returns file class based on name extension	 * @private	 * @param {String} name File name to get class of	 * @return {String} class to use for file type icon	 */	,getFileCls: function(name) {		var atmp = name.split('.');		if(1 === atmp.length) {			return this.fileCls;		}		else {			return this.fileCls + '-' + atmp.pop().toLowerCase();		}	}	// }}}	// {{{	/**	 * called when file is added - adds file to store	 * @private	 * @param {Ext.ux.BrowseButton}	 */	,onAddFile:function(bb) {		if(true !== this.eventsSuspended && false === this.fireEvent('beforefileadd', this, bb.getInputFile())) {			return;		}		var inp = bb.detachInputFile();		inp.addClass('x-hidden');		var fileName = this.getFileName(inp);		// create new record and add it to store		var rec = new this.store.recordType({			 input:inp			,fileName:fileName			,filePath:this.getFilePath(inp)			,shortName: Ext.util.Format.ellipsis(fileName, this.maxLength)			,fileCls:this.getFileCls(fileName)			,state:'queued'		}, inp.id);		rec.commit();		this.store.add(rec);		this.syncShadow();		this.uploadBtn.enable();		this.removeAllBtn.enable();		if(true !== this.eventsSuspended) {			this.fireEvent('fileadd', this, this.store, rec);		}	} // eo onAddFile	// }}}	// {{{	/**	 * destroys child components	 * @private	 */	,onDestroy:function() {		// destroy uploader		if(this.uploader) {			this.uploader.stopAll();			this.uploader.purgeListeners();			this.uploader = null;		}		// destroy view		if(this.view) {			this.view.purgeListeners();			this.view.destroy();			this.view = null;		}		// destroy store		if(this.store) {			this.store.purgeListeners();			this.store.destroy();			this.store = null;		}	} // eo function onDestroy	// }}}	// {{{	/**	 * progress event handler	 * @private	 * @param {Ext.ux.FileUploader} uploader	 * @param {Object} data progress data	 * @param {Ext.data.Record} record	 */	,onProgress:function(uploader, data, record) {		var bytesTotal, bytesUploaded, pctComplete, state, idx, item, width, pgWidth;		if(record) {			state = record.get('state');			bytesTotal = record.get('bytesTotal') || 1;			bytesUploaded = record.get('bytesUploaded') || 0;			if('uploading' === state) {				pctComplete = Math.round(1000 * bytesUploaded/bytesTotal) / 10;			}			else if('done' === 'state') {				pctComplete = 100;			}			else {				pctComplete = 0;			}			record.set('pctComplete', pctComplete);			idx = this.store.indexOf(record);			item = Ext.get(this.view.getNode(idx));			if(item) {				width = item.getWidth();				item.applyStyles({'background-position':width * pctComplete / 100 + 'px'});			}		}	} // eo function onProgress	// }}}	// {{{	/**	 * called when file remove icon is clicked - performs the remove	 * @private	 * @param {Ext.data.Record}	 */	,onRemoveFile:function(record) {		if(true !== this.eventsSuspended && false === this.fireEvent('beforefileremove', this, this.store, record)) {			return;		}		// remove DOM elements		var inp = record.get('input');		var wrap = inp.up('em');		inp.remove();		if(wrap) {			wrap.remove();		}		// remove record from store		this.store.remove(record);		var count = this.store.getCount();		this.uploadBtn.setDisabled(!count);		this.removeAllBtn.setDisabled(!count);		if(true !== this.eventsSuspended) {			this.fireEvent('fileremove', this, this.store);			this.syncShadow();		}	} // eo function onRemoveFile	// }}}	// {{{	/**	 * Remove All/Stop All button click handler	 * @private	 */	,onRemoveAllClick:function(btn) {		if(true === this.uploading) {			this.stopAll();		}		else {			this.removeAll();		}	} // eo function onRemoveAllClick	,stopAll:function() {		this.uploader.stopAll();	} // eo function stopAll	// }}}	// {{{	/**	 * DataView click handler	 * @private	 */	,onViewClick:function(view, index, node, e) {		var t = e.getTarget('div:any(.ux-up-icon-queued|.ux-up-icon-failed|.ux-up-icon-done|.ux-up-icon-stopped)');		if(t) {			this.onRemoveFile(this.store.getAt(index));		}		t = e.getTarget('div.ux-up-icon-uploading');		if(t) {			this.uploader.stopUpload(this.store.getAt(index));		}	} // eo function onViewClick	// }}}	// {{{	/**	 * tells uploader to upload	 * @private	 */	,onUpload:function() {		if(true !== this.eventsSuspended && false === this.fireEvent('beforeupload', this)) {			return false;		}		this.uploader.upload();	} // eo function onUpload	// }}}	// {{{	/**	 * url setter	 */	,setUrl:function(url) {		this.url = url;		this.uploader.setUrl(url);	} // eo function setUrl	// }}}	// {{{	/**	 * path setter	 */	,setPath:function(path) {		this.uploader.setPath(path);	} // eo function setPath	// }}}	// {{{	/**	 * Updates buttons states depending on uploading state	 * @private	 */	,updateButtons:function() {		if(true === this.uploading) {			this.addBtn.disable();			this.uploadBtn.disable();			this.removeAllBtn.setIconClass(this.stopIconCls);			this.removeAllBtn.getEl().child(this.removeAllBtn.buttonSelector).dom[this.removeAllBtn.tooltipType] = this.stopAllText;		}		else {			this.addBtn.enable();			this.uploadBtn.enable();			this.removeAllBtn.setIconClass(this.removeAllIconCls);			this.removeAllBtn.getEl().child(this.removeAllBtn.buttonSelector).dom[this.removeAllBtn.tooltipType] = this.removeAllText;		}	} // eo function updateButtons	// }}}	// {{{	/**	 * Removes all files from store and destroys file inputs	 */	,removeAll:function() {		var suspendState = this.eventsSuspended;		if(false !== this.eventsSuspended && false === this.fireEvent('beforequeueclear', this, this.store)) {			return false;		}		this.suspendEvents();		this.store.each(this.onRemoveFile, this);		this.eventsSuspended = suspendState;		if(true !== this.eventsSuspended) {			this.fireEvent('queueclear', this, this.store);		}		this.syncShadow();	} // eo function removeAll	// }}}	// {{{	/**	 * synchronize context menu shadow if we're in contextmenu	 * @private	 */	,syncShadow:function() {		if(this.contextmenu && this.contextmenu.shadow) {			this.contextmenu.getEl().shadow.show(this.contextmenu.getEl());		}	} // eo function syncShadow	// }}}}); // eo extend// register xtypeExt.reg('uploadpanel', Ext.ux.UploadPanel);// eof

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -