UNPKG

uppy

Version:

Almost as cute as a Puppy :dog:

119 lines (91 loc) 3.33 kB
'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var yo = require('yo-yo'); var nanoraf = require('nanoraf'); var _require = require('../core/Utils'), findDOMElement = _require.findDOMElement; var getFormData = require('get-form-data' /** * 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(core, opts) { _classCallCheck(this, Plugin); this.core = core; this.opts = opts || {}; // clear everything inside the target selector // this.opts.replaceTargetContent = this.opts.replaceTargetContent !== undefined ? this.opts.replaceTargetContent : true 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.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 // Set up nanoraf. );this.updateUI = nanoraf(function (state) { _this.el = yo.update(_this.el, _this.render(state)); }); if (targetElement) { this.core.log('Installing ' + callerPluginName + ' to a DOM element' // attempt to extract meta from form element );if (this.opts.getMetaFromForm && targetElement.nodeName === 'FORM') { var formMeta = getFormData(targetElement); this.core.setMeta(formMeta); } // clear everything inside the target container if (this.opts.replaceTargetContent) { targetElement.innerHTML = ''; } this.el = plugin.render(this.core.state); targetElement.appendChild(this.el); return targetElement; } else { // TODO: is instantiating the plugin really the way to roll // just to get the plugin name? var Target = target; var targetPluginName = new Target().id; this.core.log('Installing ' + callerPluginName + ' to ' + targetPluginName); var targetPlugin = this.core.getPlugin(targetPluginName); var selectorTarget = targetPlugin.addTarget(plugin); return selectorTarget; } }; Plugin.prototype.unmount = function unmount() { if (this.el && this.el.parentNode) { this.el.parentNode.removeChild(this.el); } }; Plugin.prototype.install = function install() { return; }; Plugin.prototype.uninstall = function uninstall() { this.unmount(); }; return Plugin; }(); //# sourceMappingURL=Plugin.js.map