uppy
Version:
Extensible JavaScript file upload widget with support for drag&drop, resumable uploads, previews, restrictions, file processing/encoding, remote providers like Instagram, Dropbox, Google Drive, S3 and more :dog:
145 lines (111 loc) • 4.52 kB
JavaScript
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var preact = require('preact');
var _require = require('../core/Utils'),
findDOMElement = _require.findDOMElement;
/**
* Boilerplate that all Plugins share - and should not be used
* directly. It also shows which methods final plugins should implement/override,
* this deciding on structure.
*
* @param {object} main Uppy core object
* @param {object} object with plugin options
* @return {array | string} files or success/fail message
*/
module.exports = function () {
function Plugin(uppy, opts) {
_classCallCheck(this, Plugin);
this.uppy = uppy;
this.opts = opts || {};
this.update = this.update.bind(this);
this.mount = this.mount.bind(this);
this.install = this.install.bind(this);
this.uninstall = this.uninstall.bind(this);
}
Plugin.prototype.getPluginState = function getPluginState() {
return this.uppy.state.plugins[this.id];
};
Plugin.prototype.setPluginState = function setPluginState(update) {
var plugins = _extends({}, this.uppy.state.plugins);
plugins[this.id] = _extends({}, plugins[this.id], update);
this.uppy.setState({
plugins: plugins
});
};
Plugin.prototype.update = function update(state) {
if (typeof this.el === 'undefined') {
return;
}
if (this.updateUI) {
this.updateUI(state);
}
};
/**
* Check if supplied `target` is a DOM element or an `object`.
* If it’s an object — target is a plugin, and we search `plugins`
* for a plugin with same name and return its target.
*
* @param {String|Object} target
*
*/
Plugin.prototype.mount = function mount(target, plugin) {
var _this = this;
var callerPluginName = plugin.id;
var targetElement = findDOMElement(target);
if (targetElement) {
this.updateUI = function (state) {
_this.el = preact.render(_this.render(state), targetElement, _this.el);
};
this.uppy.log('Installing ' + callerPluginName + ' to a DOM element');
// clear everything inside the target container
if (this.opts.replaceTargetContent) {
targetElement.innerHTML = '';
}
this.el = preact.render(this.render(this.uppy.state), targetElement);
return this.el;
}
var targetPlugin = void 0;
if ((typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object' && target instanceof Plugin) {
// Targeting a plugin *instance*
targetPlugin = target;
} else if (typeof target === 'function') {
// Targeting a plugin type
var Target = target;
// Find the target plugin instance.
this.uppy.iteratePlugins(function (plugin) {
if (plugin instanceof Target) {
targetPlugin = plugin;
return false;
}
});
}
if (targetPlugin) {
var targetPluginName = targetPlugin.id;
this.uppy.log('Installing ' + callerPluginName + ' to ' + targetPluginName);
this.el = targetPlugin.addTarget(plugin);
return this.el;
}
this.uppy.log('Not installing ' + callerPluginName);
throw new Error('Invalid target option given to ' + callerPluginName);
};
Plugin.prototype.render = function render(state) {
throw new Error('Extend the render method to add your plugin to a DOM element');
};
Plugin.prototype.addTarget = function addTarget(plugin) {
throw new Error('Extend the addTarget method to add your plugin to another plugin\'s target');
};
Plugin.prototype.unmount = function unmount() {
if (this.el && this.el.parentNode) {
this.el.parentNode.removeChild(this.el);
}
// this.target = null
};
Plugin.prototype.install = function install() {};
Plugin.prototype.uninstall = function uninstall() {
this.unmount();
};
return Plugin;
}();
//# sourceMappingURL=Plugin.js.map