Browse Source

Crazy plugin name refactor to make it work in IE

http://stackoverflow.com/questions/25140723/constructor-name-is-undefine
d-in-internet-explorer
Artur Paikin 9 years ago
parent
commit
93f8fcdc7f
3 changed files with 34 additions and 13 deletions
  1. 5 3
      src/core/Core.js
  2. 22 7
      src/core/Utils.js
  3. 7 3
      src/plugins/Plugin.js

+ 5 - 3
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
       }

+ 22 - 7
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,

+ 7 - 3
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