"use strict"; /** * 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$ */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); /*egw:uses et2_core_inputWidget; phpgwapi.Resumable.resumable; */ var et2_core_inputWidget_1 = require("./et2_core_inputWidget"); var et2_core_widget_1 = require("./et2_core_widget"); var et2_core_inheritance_1 = require("./et2_core_inheritance"); /** * Class which implements file upload * * @augments et2_inputWidget */ var et2_file = /** @class */ (function (_super) { __extends(et2_file, _super); /** * Constructor * * @memberOf et2_file */ function et2_file(_parent, _attrs, _child) { var _this = // Call the inherited constructor _super.call(this, _parent, _attrs, et2_core_inheritance_1.ClassWithAttributes.extendAttributes(et2_file._attributes, _child || {})) || this; _this.asyncOptions = {}; _this.input = null; _this.progress = null; _this.span = null; _this.node = null; _this.input = null; _this.progress = null; _this.span = null; // Contains all submit buttons need to be disabled during upload process _this.disabled_buttons = jQuery("input[type='submit'], button"); if (!_this.options.value) _this.options.value = {}; 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; } // If ID ends in /, it's a directory - allow multiple else if (_this.options.id.substr(-1) === "/") { _this.options.multiple = true; _attrs.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) { return self.onStart(event, file_count); }, onFinish: function (event, file_count) { self.onFinish.apply(self, [event, file_count]); }, onStartOne: function (event, file_name, index, file_count) { }, 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); }, beforeSend: function (form) { return self.beforeSend(form); }, chunkSize: _this.options.chunk_size || 1024 * 1024, target: egw.ajaxUrl("EGroupware\\Api\\Etemplate\\Widget\\File::ajax_upload"), query: function (file) { return self.beforeSend(file); }, // Disable checking for already uploaded chunks testChunks: false }, _this.asyncOptions); _this.asyncOptions.fieldName = _this.options.id; _this.createInputWidget(); _this.set_readonly(_this.options.readonly); return _this; } et2_file.prototype.destroy = function () { _super.prototype.destroy.call(this); this.set_drop_target(null); this.node = null; this.input = null; this.span = null; this.progress = null; }; et2_file.prototype.createInputWidget = function () { this.node = jQuery(document.createElement("div")).addClass("et2_file"); this.span = jQuery(document.createElement("span")) .addClass('et2_file_span et2_button') .appendTo(this.node); if (this.options.label != '') this.span.addClass('et2_button_text'); var span = this.span; this.input = jQuery(document.createElement("input")) .attr("type", "file").attr("placeholder", this.options.blur) .addClass("et2_file_upload") .appendTo(this.node) .hover(function () { jQuery(span) .toggleClass('et2_file_spanHover'); }) .on({ mousedown: function () { jQuery(span).addClass('et2_file_spanActive'); }, mouseup: function () { jQuery(span).removeClass('et2_file_spanActive'); } }); if (this.options.accept) this.input.attr('accept', this.options.accept); var self = this; // trigger native input upload file if (!this.options.readonly) this.span.click(function () { self.input.click(); }); // Check for File interface, should fall back to normal form submit if missing if (typeof File != "undefined" && typeof (new XMLHttpRequest()).upload != "undefined") { this.resumable = new Resumable(this.asyncOptions); this.resumable.assignBrowse(this.input); this.resumable.on('fileAdded', jQuery.proxy(this._fileAdded, this)); this.resumable.on('fileProgress', jQuery.proxy(this._fileProgress, this)); this.resumable.on('fileSuccess', jQuery.proxy(this.finishUpload, this)); this.resumable.on('complete', jQuery.proxy(this.onFinish, this)); } else { // This may be a problem submitting via ajax } if (this.options.progress) { var widget = this.getRoot().getWidgetById(this.options.progress); if (widget) { //may be not available at createInputWidget time this.progress = jQuery(widget.getDOMNode()); } } if (!this.progress) { this.progress = jQuery(document.createElement("div")).appendTo(this.node); } this.progress.addClass("progress"); if (this.options.multiple) { this.input.attr("multiple", "multiple"); } this.setDOMNode(this.node[0]); }; /** * Set a widget or DOM node as a HTML5 file drop target * * @param {string} new_target widget ID or DOM node ID to be used as a new target */ et2_file.prototype.set_drop_target = function (new_target) { // Cancel old drop target if (this.options.drop_target) { var widget_1 = this.getRoot().getWidgetById(this.options.drop_target); var drop_target_1 = widget_1 && widget_1.getDOMNode() || document.getElementById(this.options.drop_target); if (drop_target_1) { this.resumable.unAssignDrop(drop_target_1); } } this.options.drop_target = new_target; if (!this.options.drop_target) return; // Set up new drop target var widget = this.getRoot().getWidgetById(this.options.drop_target); var drop_target = widget && widget.getDOMNode() || document.getElementById(this.options.drop_target); if (drop_target) { this.resumable.assignDrop([drop_target]); } else { this.egw().debug("warn", "Did not find file drop target %s", this.options.drop_target); } }; et2_file.prototype.attachToDOM = function () { var res = _super.prototype.attachToDOM.call(this); // Override parent's change, file widget will fire change when finished uploading this.input.unbind("change.et2_inputWidget"); return res; }; et2_file.prototype.getValue = function () { return this.options.value ? this.options.value : this.input.val(); }; /** * Set the value of the file widget. * * If you pass a FileList or list of files, it will trigger the async upload * * @param {FileList|File[]|false} value List of files to be uploaded, or false to reset. * @param {Event} event Most browsers require the user to initiate file transfers in some way. * Pass the event in, if you have it. */ et2_file.prototype.set_value = function (value, event) { if (!value || typeof value == "undefined") { value = {}; } if (jQuery.isEmptyObject(value)) { this.options.value = {}; if (this.resumable.progress() == 1) this.progress.empty(); // Reset the HTML element this.input.wrap('