From 93f8fcdc7f56e10102254d413f86ef370f2cddd0 Mon Sep 17 00:00:00 2001 From: Artur Paikin Date: Wed, 4 May 2016 00:14:20 -0400 Subject: [PATCH] Crazy plugin name refactor to make it work in IE http://stackoverflow.com/questions/25140723/constructor-name-is-undefine d-in-internet-explorer --- src/core/Core.js | 8 +++++--- src/core/Utils.js | 29 ++++++++++++++++++++++------- src/plugins/Plugin.js | 10 +++++++--- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/core/Core.js b/src/core/Core.js index e9bdf5554..f0baae33d 100644 --- a/src/core/Core.js +++ b/src/core/Core.js @@ -187,9 +187,10 @@ export default class Core { use (Plugin, opts) { // Instantiate const plugin = new Plugin(this, opts) + const pluginName = Utils.getFnName(plugin.constructor) this.plugins[plugin.type] = this.plugins[plugin.type] || [] - if (!plugin.id) { + if (!pluginName) { throw new Error('Your plugin must have a name') } @@ -197,7 +198,7 @@ export default class Core { throw new Error('Your plugin must have a type') } - let existsPluginAlready = this.getPlugin(plugin.id) + let existsPluginAlready = this.getPlugin(pluginName) if (existsPluginAlready) { let msg = `Already found a plugin named '${existsPluginAlready.name}'. Tried to use: '${plugin.constructor.name}'. @@ -221,7 +222,8 @@ export default class Core { getPlugin (name) { let foundPlugin = false this.iteratePlugins((plugin) => { - if (plugin.constructor.name === name) { + const pluginName = Utils.getFnName(plugin.constructor) + if (pluginName === name) { foundPlugin = plugin return false } diff --git a/src/core/Utils.js b/src/core/Utils.js index 7048256da..49ea78f24 100644 --- a/src/core/Utils.js +++ b/src/core/Utils.js @@ -32,12 +32,12 @@ function promiseWaterfall (methods) { * @param {requestCallback} cb * @return {String} */ -function addListenerMulti (el, events, cb) { - const eventsArray = events.split(' ') - for (let event in eventsArray) { - el.addEventListener(eventsArray[event], cb, false) - } -} +// function addListenerMulti (el, events, cb) { +// const eventsArray = events.split(' ') +// for (let event in eventsArray) { +// el.addEventListener(eventsArray[event], cb, false) +// } +// } /** * Shallow flatten nested arrays. @@ -103,10 +103,25 @@ function extend (...objs) { return Object.assign.apply(this, [{}].concat(objs)) } +/** + * Takes function or class, returns its name. + * Because IE doesn’t support `constructor.name`. + * https://gist.github.com/dfkaye/6384439, http://stackoverflow.com/a/15714445 + * + * @param {Object} fn — function + * + */ +function getFnName (fn) { + var f = typeof fn === 'function' + var s = f && ((fn.name && ['', fn.name]) || fn.toString().match(/function ([^\(]+)/)) + return (!f && 'not a function') || (s && s[1] || 'anonymous') +} + export default { promiseWaterfall, generateFileID, - addListenerMulti, + getFnName, + // addListenerMulti, every, flatten, groupBy, diff --git a/src/plugins/Plugin.js b/src/plugins/Plugin.js index 5b305f37e..b03bbf2e5 100644 --- a/src/plugins/Plugin.js +++ b/src/plugins/Plugin.js @@ -1,4 +1,5 @@ import yo from 'yo-yo' +import Utils from '../core/Utils' /** * Boilerplate that all Plugins share - and should not be used @@ -36,8 +37,10 @@ export default class Plugin { * */ getTarget (target, caller, el, render) { + const callerPluginName = Utils.getFnName(caller.constructor) + if (typeof target === 'string') { - this.core.log(`Installing ${caller.name} to ${target}`) + this.core.log(`Installing ${callerPluginName} to ${target}`) // clear everything inside the target selector // if (replaceTargetContent) { @@ -47,8 +50,9 @@ export default class Plugin { return target } else { - this.core.log(`Installing ${caller.name} to ${target.name}`) - let targetPlugin = this.core.getPlugin(target.name) + const targetPluginName = Utils.getFnName(target) + this.core.log(`Installing ${callerPluginName} to ${targetPluginName}`) + let targetPlugin = this.core.getPlugin(targetPluginName) let selectorTarget = targetPlugin.addTarget(caller, render) return selectorTarget