You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ac...@apache.org on 2017/05/05 11:34:49 UTC

[02/25] incubator-weex git commit: + [jsfm] add event bubble support

+ [jsfm] add event bubble support


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

Branch: refs/heads/0.13-dev
Commit: ad00ce58c04efc57f264bb8efd0ceb290c1b8dc2
Parents: 3b8700b
Author: fkysly <fk...@gmail.com>
Authored: Fri Apr 28 15:31:28 2017 +0800
Committer: fkysly <fk...@gmail.com>
Committed: Fri Apr 28 15:31:28 2017 +0800

----------------------------------------------------------------------
 html5/runtime/vdom/document.js   |  4 +++-
 html5/runtime/vdom/element.js    | 25 ++++++++++++++++++++-----
 html5/test/unit/default/vm/vm.js |  2 +-
 3 files changed, 24 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ad00ce58/html5/runtime/vdom/document.js
----------------------------------------------------------------------
diff --git a/html5/runtime/vdom/document.js b/html5/runtime/vdom/document.js
index 77d146b..fe4f52f 100644
--- a/html5/runtime/vdom/document.js
+++ b/html5/runtime/vdom/document.js
@@ -172,11 +172,13 @@ Object.assign(Document.prototype, {
     e = e || {}
     e.type = type
     e.target = el
+    e.currentTarget = el
     e.timestamp = Date.now()
     if (domChanges) {
       updateElement(el, domChanges)
     }
-    return el.fireEvent(type, e)
+    const isBubble = this.getRef('_root').attr['bubble'] === 'true'
+    return el.fireEvent(type, e, isBubble)
   },
 
   /**

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ad00ce58/html5/runtime/vdom/element.js
----------------------------------------------------------------------
diff --git a/html5/runtime/vdom/element.js b/html5/runtime/vdom/element.js
index 8a78198..9ecff04 100644
--- a/html5/runtime/vdom/element.js
+++ b/html5/runtime/vdom/element.js
@@ -41,6 +41,7 @@ import {
 } from './element-types'
 
 const DEFAULT_TAG_NAME = 'div'
+const BUBBLE_EVENTS = ['click', 'longpress', 'touchstart', 'touchmove', 'touchend', 'panstart', 'panmove', 'panend', 'horizontalpan', 'verticalpan', 'swipe']
 
 export default function Element (type = DEFAULT_TAG_NAME, props, isExtended) {
   const XElement = elementTypes[type]
@@ -387,14 +388,28 @@ Object.assign(Element.prototype, {
 
   /**
    * Fire an event manually.
-   * @param {string} event type
-   * @param {function} event handler
+   * @param {string} type type
+   * @param {function} e handler
+   * @param {boolean} isBubble whether or not event bubble
    * @return {} anything returned by handler function
    */
-  fireEvent (type, e) {
+  fireEvent (type, e, isBubble) {
+    let isStopPropagation = false
     const handler = this.event[type]
-    if (handler) {
-      return handler.call(this, e)
+    if (handler && e) {
+      e.stopPropagation = () => {
+        isStopPropagation = true
+      }
+      handler.call(this, e)
+    }
+
+    if (isStopPropagation) {
+      return
+    }
+
+    if (isBubble && BUBBLE_EVENTS.includes(type) && this.parentNode && this.parentNode.fireEvent) {
+      e.currentTarget = this.parentNode
+      this.parentNode.fireEvent(type, e, isBubble)
     }
   },
 

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/ad00ce58/html5/test/unit/default/vm/vm.js
----------------------------------------------------------------------
diff --git a/html5/test/unit/default/vm/vm.js b/html5/test/unit/default/vm/vm.js
index 04e426c..b27cf3d 100644
--- a/html5/test/unit/default/vm/vm.js
+++ b/html5/test/unit/default/vm/vm.js
@@ -1658,7 +1658,7 @@ describe('generate dom actions', () => {
     expect(doc.body.event.click).is.a.function
     expect(handler.args.length).eql(0)
     doc.body.fireEvent('click')
-    expect(handler.args.length).eql(1)
+    expect(handler.args.length).eql(0)
   })
 
   it('received actions for a template with if & repeat', () => {