/** * EGroupware eTemplate2 - JS Number object * * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @package etemplate * @subpackage api * @link http://www.egroupware.org * @author Nathan Gray * @copyright Nathan Gray 2011 * @version $Id$ */ "use strict"; /*egw:uses et2_core_inputWidget; phpgwapi.jquery.jquery.html5_upload; */ /** * Class which implements file upload * * @augments et2_inputWidget */ var et2_file = et2_inputWidget.extend( { attributes: { "multiple": { "name": "Multiple files", "type": "boolean", "default": false, "description": "Allow the user to select more than one file to upload at a time. Subject to browser support." }, "max_file_size": { "name": "Maximum file size", "type": "integer", "default":"8388608", "description": "Largest file accepted, in bytes. Subject to server limitations. 8Mb = 8388608" }, "mime": { "name": "Allowed file types", "type": "string", "default": et2_no_init, "description": "Mime type (eg: image/png) or regex (eg: /^text\//i) for allowed file types" }, "blur": { "name": "Placeholder", "type": "string", "default": "", "description": "This text get displayed if an input-field is empty and does not have the input-focus (blur). It can be used to show a default value or a kind of help-text." }, "progress": { "name": "Progress node", "type": "string", "default": et2_no_init, "description": "The ID of an alternate node (div) to display progress and results." }, "onStart": { "name": "Start event handler", "type": "any", "default": et2_no_init, "description": "A (js) function called when an upload starts. Return true to continue with upload, false to cancel." }, "onFinish": { "name": "Finish event handler", "type": "any", "default": et2_no_init, "description": "A (js) function called when all files to be uploaded are finished." } }, asyncOptions: {}, /** * Constructor * * @memberOf et2_file */ init: function() { this._super.apply(this, arguments); this.node = null; this.input = null; this.progress = null; if(!this.options.id) { console.warn("File widget needs an ID. Used 'file_widget'."); this.options.id = "file_widget"; } // Legacy - id ending in [] means multiple if(this.options.id.substr(-2) == "[]") { this.options.multiple = true; } // Set up the URL to have the request ID & the widget ID var instance = this.getInstanceManager(); var self = this; this.asyncOptions = jQuery.extend({ // Callbacks onStart: function(event, file_count) { // Hide any previous errors self.hideMessage(); return self.onStart(event, file_count); }, onFinish: function(event, file_count) { self.onFinish(event, file_count); // Fire legacy change action when done self.change(self.input); }, onStartOne: function(event, file_name, index, file_count) { // Here 'this' is the input if(self.checkMime(this.files[index])) { return self.createStatus(event,file_name, index,file_count); } else { // Wrong mime type - show in the list of files return self.createStatus( self.egw().lang("File is of wrong type (%1 != %2)!", this.files[index].type, self.options.mime), file_name ); } }, onFinishOne: function(event, response, name, number, total) { return self.finishUpload(event,response,name,number,total);}, onProgress: function(event, progress, name, number, total) { return self.onProgress(event,progress,name,number,total);}, onError: function(event, name, error) { return self.onError(event,name,error);}, sendBoundary: window.FormData || jQuery.browser.mozilla, beforeSend: function(form) { return self.beforeSend(form);}, url: egw_json_request.prototype._assembleAjaxUrl("etemplate_widget_file::ajax_upload::etemplate"), autoclear: !(this.options.onchange) },this.asyncOptions); this.asyncOptions.fieldName = this.options.id; this.createInputWidget(); }, destroy: function() { this._super.apply(this, arguments); this.node = null; this.input = null; this.progress = null; }, createInputWidget: function() { this.node = $j(document.createElement("div")).addClass("et2_file"); this.input = $j(document.createElement("input")) .attr("type", "file").attr("placeholder", this.options.blur) .appendTo(this.node); // Check for File interface, should fall back to normal form submit if missing if(typeof File != "undefined" && typeof (new XMLHttpRequest()).upload != "undefined") { this.input.html5_upload(this.asyncOptions); } else { // This may be a problem submitting via ajax } this.progress = this.options.progress ? $j(document.getElementById(this.options.progress)) : $j(document.createElement("div")).appendTo(this.node); this.progress.addClass("progress"); if(this.options.multiple) { this.input.attr("multiple","multiple"); } this.setDOMNode(this.node[0]); }, attachToDOM: function() { this._super.apply(this, arguments); // Override parent's behaviour to fire legacy change when finished if (this.onchange) { this.input.unbind("change.et2_inputWidget"); } }, getValue: function() { var value = this.options.value ? this.options.value : this.input.val(); return value; }, /** * You can't actually set value, but clearing works */ set_value: function(value) { if(!value || typeof value == "undefined") { value = {}; } if(jQuery.isEmptyObject(value)) { this.options.value = {}; this.progress.empty(); // Reset the HTML element this.input.wrap('