You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by da...@apache.org on 2017/08/17 03:59:30 UTC

[13/43] incubator-weex git commit: * [jsfm] refactor element-types

* [jsfm] refactor element-types


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/e9ad371a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/e9ad371a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/e9ad371a

Branch: refs/heads/0.16-dev
Commit: e9ad371a3ce8eb5c7c4175b9d4fe117b2bdfecdc
Parents: 2f7921e
Author: Hanks <zh...@gmail.com>
Authored: Mon Aug 7 16:37:30 2017 +0800
Committer: Hanks <zh...@gmail.com>
Committed: Mon Aug 7 16:37:30 2017 +0800

----------------------------------------------------------------------
 html5/runtime/api/init.js           |  2 +-
 html5/runtime/vdom/WeexElement.js   | 84 ++++++++++++++++++++++++++++++++
 html5/runtime/vdom/element-types.js | 72 ---------------------------
 html5/runtime/vdom/element.js       |  4 +-
 html5/runtime/vdom/index.js         |  8 +--
 html5/test/unit/vdom/index.js       |  8 +--
 6 files changed, 96 insertions(+), 82 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/e9ad371a/html5/runtime/api/init.js
----------------------------------------------------------------------
diff --git a/html5/runtime/api/init.js b/html5/runtime/api/init.js
index cc770af..d9a877f 100644
--- a/html5/runtime/api/init.js
+++ b/html5/runtime/api/init.js
@@ -17,7 +17,7 @@
  * under the License.
  */
 import { init as initTaskHandler } from '../bridge/TaskCenter'
-import { registerElement } from '../vdom/element-types'
+import { registerElement } from '../vdom/WeexElement'
 import { services, register, unregister } from './service'
 
 let frameworks

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/e9ad371a/html5/runtime/vdom/WeexElement.js
----------------------------------------------------------------------
diff --git a/html5/runtime/vdom/WeexElement.js b/html5/runtime/vdom/WeexElement.js
new file mode 100644
index 0000000..455e69e
--- /dev/null
+++ b/html5/runtime/vdom/WeexElement.js
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { getTaskCenter } from './operation'
+
+let Element
+
+export function setElement (El) {
+  Element = El
+}
+
+/**
+ * A map which stores all type of elements.
+ * @type {Object}
+ */
+const registeredElements = {}
+
+/**
+ * Register an extended element type with component methods.
+ * @param  {string} type    component type
+ * @param  {array}  methods a list of method names
+ */
+export function registerElement (type, methods) {
+  // Skip when no special component methods.
+  if (!methods || !methods.length) {
+    return
+  }
+
+  // Init constructor.
+  class WeexElement extends Element {}
+
+  // Add methods to prototype.
+  methods.forEach(methodName => {
+    WeexElement.prototype[methodName] = function (...args) {
+      const taskCenter = getTaskCenter(this.docId)
+      if (taskCenter) {
+        return taskCenter.send('component', {
+          ref: this.ref,
+          component: type,
+          method: methodName
+        }, args)
+      }
+    }
+  })
+
+  // Add to element type map.
+  registeredElements[type] = WeexElement
+}
+
+export function unregisterElement (type) {
+  delete registeredElements[type]
+}
+
+export function getWeexElement (type) {
+  return registeredElements[type]
+}
+
+export function isWeexElement (type) {
+  return !!registeredElements[type]
+}
+
+/**
+ * Clear all element types. Only for testing.
+ */
+export function clearWeexElements () {
+  for (const type in registeredElements) {
+    unregisterElement(type)
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/e9ad371a/html5/runtime/vdom/element-types.js
----------------------------------------------------------------------
diff --git a/html5/runtime/vdom/element-types.js b/html5/runtime/vdom/element-types.js
deleted file mode 100644
index 4eb6bae..0000000
--- a/html5/runtime/vdom/element-types.js
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-import { getTaskCenter } from './operation'
-
-let Element
-
-export function setElement (El) {
-  Element = El
-}
-
-/**
- * A map which stores all type of elements.
- * @type {Object}
- */
-export const elementTypes = {}
-
-/**
- * Register an extended element type with component methods.
- * @param  {string} type    component type
- * @param  {array}  methods a list of method names
- */
-export function registerElement (type, methods) {
-  // Skip when no special component methods.
-  if (!methods || !methods.length) {
-    return
-  }
-
-  // Init constructor.
-  class WeexElement extends Element {}
-
-  // Add methods to prototype.
-  methods.forEach(methodName => {
-    WeexElement.prototype[methodName] = function (...args) {
-      const taskCenter = getTaskCenter(this.docId)
-      if (taskCenter) {
-        return taskCenter.send('component', {
-          ref: this.ref,
-          component: type,
-          method: methodName
-        }, args)
-      }
-    }
-  })
-
-  // Add to element type map.
-  elementTypes[type] = WeexElement
-}
-
-/**
- * Clear all element types. Only for testing.
- */
-export function clearElementTypes () {
-  for (const type in elementTypes) {
-    delete elementTypes[type]
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/e9ad371a/html5/runtime/vdom/element.js
----------------------------------------------------------------------
diff --git a/html5/runtime/vdom/element.js b/html5/runtime/vdom/element.js
index d0115a5..dcb487b 100644
--- a/html5/runtime/vdom/element.js
+++ b/html5/runtime/vdom/element.js
@@ -29,7 +29,7 @@ import {
   removeIndex
 } from './operation'
 import { uniqueId } from '../utils'
-import { elementTypes, setElement } from './element-types'
+import { getWeexElement, setElement } from './WeexElement'
 import { filterDirective } from './directive'
 
 const DEFAULT_TAG_NAME = 'div'
@@ -47,7 +47,7 @@ export default class Element extends Node {
   constructor (type = DEFAULT_TAG_NAME, props, isExtended) {
     super()
 
-    const WeexElement = elementTypes[type]
+    const WeexElement = getWeexElement(type)
     if (WeexElement && !isExtended) {
       return new WeexElement(props)
     }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/e9ad371a/html5/runtime/vdom/index.js
----------------------------------------------------------------------
diff --git a/html5/runtime/vdom/index.js b/html5/runtime/vdom/index.js
index 2c979e8..2947f5e 100644
--- a/html5/runtime/vdom/index.js
+++ b/html5/runtime/vdom/index.js
@@ -16,16 +16,18 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 import Node from './Node'
 import Element from './Element'
 import Comment from './Comment'
 import Document from './Document'
 
 export {
-  elementTypes,
   registerElement,
-  clearElementTypes
-} from './element-types'
+  unregisterElement,
+  isWeexElement,
+  clearWeexElements
+} from './WeexElement'
 
 export {
   Document,

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/e9ad371a/html5/test/unit/vdom/index.js
----------------------------------------------------------------------
diff --git a/html5/test/unit/vdom/index.js b/html5/test/unit/vdom/index.js
index be85d84..01a55c6 100644
--- a/html5/test/unit/vdom/index.js
+++ b/html5/test/unit/vdom/index.js
@@ -26,9 +26,9 @@ import {
   Document,
   Element,
   Comment,
-  elementTypes,
+  isWeexElement,
   registerElement,
-  clearElementTypes
+  clearWeexElements
 } from '../../../runtime/vdom'
 
 describe('document constructor', () => {
@@ -51,11 +51,11 @@ describe('component methods management', () => {
   })
 
   after(() => {
-    clearElementTypes()
+    clearWeexElements()
   })
 
   it('has registered element types', () => {
-    expect(Object.keys(elementTypes)).eql(['x'])
+    expect(isWeexElement('x')).to.be.true
   })
 
   it('will call component method', () => {