浏览代码

Move isDOMElement helper to core/Utils.

René Kooi 8 年之前
父节点
当前提交
5da7d3f0d7
共有 2 个文件被更改,包括 32 次插入19 次删除
  1. 27 1
      src/core/Utils.js
  2. 5 18
      src/plugins/Plugin.js

+ 27 - 1
src/core/Utils.js

@@ -399,6 +399,31 @@ function prettyETA (seconds) {
 //   }
 //   }
 // }
 // }
 
 
+/**
+ * Check if an object is a DOM element. Duck-typing based on `nodeType`.
+ *
+ * @param {*} obj
+ */
+function isDOMElement (obj) {
+  return obj && typeof obj === 'object' && obj.nodeType === Node.ELEMENT_NODE
+}
+
+/**
+ * Find a DOM element.
+ *
+ * @param {Node|string} element
+ * @return {Node|null}
+ */
+function findDOMElement (element) {
+  if (typeof element === 'string') {
+    return document.querySelector(element)
+  }
+
+  if (typeof element === 'object' && isDOMElement(element)) {
+    return element
+  }
+}
+
 module.exports = {
 module.exports = {
   generateFileID,
   generateFileID,
   toArray,
   toArray,
@@ -423,5 +448,6 @@ module.exports = {
   // makeWorker,
   // makeWorker,
   // makeCachingFunction,
   // makeCachingFunction,
   copyToClipboard,
   copyToClipboard,
-  prettyETA
+  prettyETA,
+  findDOMElement
 }
 }

+ 5 - 18
src/plugins/Plugin.js

@@ -1,14 +1,6 @@
 const yo = require('yo-yo')
 const yo = require('yo-yo')
 // const nanoraf = require('nanoraf')
 // const nanoraf = require('nanoraf')
-
-/**
- * Check if an object is a DOM element. Duck-typing based on `nodeType`.
- *
- * @param {*} obj
- */
-function isDOMElement (obj) {
-  return obj && typeof obj === 'object' && obj.nodeType === Node.ELEMENT_NODE
-}
+const { findDOMElement } = require('../core/Utils')
 
 
 /**
 /**
  * Boilerplate that all Plugins share - and should not be used
  * Boilerplate that all Plugins share - and should not be used
@@ -67,7 +59,7 @@ module.exports = class Plugin {
   }
   }
 
 
   /**
   /**
-   * Check if supplied `target` is a `string` or an `object`.
+   * Check if supplied `target` is a DOM element or an `object`.
    * If it’s an object — target is a plugin, and we search `plugins`
    * If it’s an object — target is a plugin, and we search `plugins`
    * for a plugin with same name and return its target.
    * for a plugin with same name and return its target.
    *
    *
@@ -77,16 +69,11 @@ module.exports = class Plugin {
   mount (target, plugin) {
   mount (target, plugin) {
     const callerPluginName = plugin.id
     const callerPluginName = plugin.id
 
 
-    let targetElement
-    if (isDOMElement(target)) {
-      this.core.log(`Installing ${callerPluginName} to a DOM element`)
-      targetElement = target
-    } else if (typeof target === 'string') {
-      this.core.log(`Installing ${callerPluginName} to ${target}`)
-      targetElement = document.querySelector(target)
-    }
+    const targetElement = findDOMElement(target)
 
 
     if (targetElement) {
     if (targetElement) {
+      this.core.log(`Installing ${callerPluginName} to a DOM element`)
+
       // clear everything inside the target container
       // clear everything inside the target container
       if (this.opts.replaceTargetContent) {
       if (this.opts.replaceTargetContent) {
         targetElement.innerHTML = ''
         targetElement.innerHTML = ''