You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by so...@apache.org on 2017/06/16 04:05:56 UTC

[03/20] incubator-weex git commit: * [html5] add globalEvent API.

* [html5] add globalEvent API.


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

Branch: refs/heads/0.14-dev
Commit: cb398736a26b46c652fc66c1b9cee399ad8df4b9
Parents: 4b68aa1
Author: MrRaindrop <te...@gmail.com>
Authored: Tue Jun 6 14:47:05 2017 +0800
Committer: MrRaindrop <te...@gmail.com>
Committed: Tue Jun 6 14:47:05 2017 +0800

----------------------------------------------------------------------
 html5/render/vue/modules/globalEvent.js | 50 ++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/cb398736/html5/render/vue/modules/globalEvent.js
----------------------------------------------------------------------
diff --git a/html5/render/vue/modules/globalEvent.js b/html5/render/vue/modules/globalEvent.js
new file mode 100644
index 0000000..66b7969
--- /dev/null
+++ b/html5/render/vue/modules/globalEvent.js
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+/**
+ * globalEvent API:
+ * @doc http://weex.apache.org/cn/references/modules/globalevent.html
+ */
+// track varies kinds of events and listeners.
+const handlerTraker = {}
+
+export default {
+  /**
+   * addEventListener
+   * @param {string} evt - the event name to add a listener on.
+   */
+  addEventListener (evt, callback) {
+    // const cb = e => this.sender.performCallback(callbackId, e)
+    const cb = e => callback && callback(e)
+    if (!handlerTraker[evt]) {
+      handlerTraker[evt] = [cb]
+    }
+    else {
+      handlerTraker.push(cb)
+    }
+    document.addEventListener(evt, cb)
+  },
+
+  /**
+   * removeEventListener
+   * @param {string} evt - the event name to remove a listener from.
+   */
+  removeEventListener (evt) {
+    handlerTraker[evt].forEach(cb => document.removeEventListener(evt, cb))
+  }
+}