From 145887e5af208abe3e915bb84e64118b892ca1fc Mon Sep 17 00:00:00 2001 From: Greg Davis Date: Sun, 5 Jul 2015 23:45:03 -0600 Subject: [PATCH 1/4] First commit of sample API, rewritten to match spec --- API/epubsc_api.js | 304 +++++++++++++ API/tests/css/status.css | 65 +++ API/tests/epubdoc.html | 94 ++++ API/tests/parent.html | 153 +++++++ API/tests/parent2.html | 101 +++++ API/tests/parentRS.html | 470 ++++++++++++++++++++ API/tests/parentScale.html | 192 +++++++++ API/tests/supportJS/focus.js | 15 + API/tests/supportJS/pause.js | 5 + API/tests/supportJS/profiler.js | 26 ++ API/tests/supportJS/timer.js | 45 ++ API/tests/supportJS/tracker.js | 16 + API/tests/userAgent.html | 229 ++++++++++ API/tests/wapievent/monitorEvents.js | 567 +++++++++++++++++++++++++ API/tests/wapievent/subscribeEvents.js | 299 +++++++++++++ API/tests/widgets/childEventA.html | 214 ++++++++++ API/tests/widgets/composition.htm | 47 ++ API/tests/widgets/composition2.htm | 48 +++ API/tests/widgets/composition3.htm | 62 +++ API/tests/widgets/minigesture.htm | 51 +++ API/tests/widgets/miniwidget.htm | 25 ++ API/tests/widgets/miniwidget2.htm | 63 +++ 22 files changed, 3091 insertions(+) create mode 100755 API/epubsc_api.js create mode 100644 API/tests/css/status.css create mode 100644 API/tests/epubdoc.html create mode 100644 API/tests/parent.html create mode 100644 API/tests/parent2.html create mode 100644 API/tests/parentRS.html create mode 100644 API/tests/parentScale.html create mode 100644 API/tests/supportJS/focus.js create mode 100644 API/tests/supportJS/pause.js create mode 100644 API/tests/supportJS/profiler.js create mode 100644 API/tests/supportJS/timer.js create mode 100644 API/tests/supportJS/tracker.js create mode 100644 API/tests/userAgent.html create mode 100644 API/tests/wapievent/monitorEvents.js create mode 100644 API/tests/wapievent/subscribeEvents.js create mode 100644 API/tests/widgets/childEventA.html create mode 100644 API/tests/widgets/composition.htm create mode 100644 API/tests/widgets/composition2.htm create mode 100644 API/tests/widgets/composition3.htm create mode 100644 API/tests/widgets/minigesture.htm create mode 100644 API/tests/widgets/miniwidget.htm create mode 100644 API/tests/widgets/miniwidget2.htm diff --git a/API/epubsc_api.js b/API/epubsc_api.js new file mode 100755 index 0000000..ade4bd7 --- /dev/null +++ b/API/epubsc_api.js @@ -0,0 +1,304 @@ +/* + A sample implementation of the IDPF scripted + component prototocol. + + authors: + Greg Davis -- Pearson PLC + +*/ + +var epubsc = (function(){ + 'use strict'; + + var methods = { + initialize : function(){ + // set up the message listener here, needs to be done for widgets and pages + window.addEventListener("message", subpub.messageHandler) + + // assign an id here + this.widgetID = subpub.genUuid() + + // setup the publishable browser event stack + subpub.publishableBrowserEvents.forEach(function(eventName) { + // Setup the event listener here - listen for bubble phase, then publish the event when it fires + document.addEventListener(eventName, function(e){ + epubsc.publish("epubsc_event", new epubsc.BrowserEvent(e)) + }) + }) + + // fire off the loading event + this.publish("epubsc_load", "loading") + + // set up the ready event + if(window.parent != window){ + window.addEventListener("DOMContentLoaded", function(){ + epubsc.publish("epubsc_ready", "ready") + }, false) + } + + // set up the unload event + if(window.parent != window){ + window.addEventListener("unload", function(){ + epubsc.send(window.parent, "epubsc_unload", document.URL) + }) + } + }, + + // sub-pub mechanisms here + // message constructor + Message : function(method, topic, message, bubbles){ + // this is a constructor, so when it's invoked as "new" this will refer + // only to the created object, not the whole PSO object + this.type = "epubsc_message" + this.method = method + this.timestamp = +new Date() // date string + this.id = subpub.genUuid() // unique id for the message + this.widgetId = this.widgetID // unique id for the widget during this session + + // make the payload + this.topic = topic + this.topicData = message + + // not sure what this does, investigate... + this.widgetPath_ = []; + }, + + BrowserEvent : function(e){ + /* TODO: Decide if assigning undefined properties is bad. It means that + * this.hasOwnProperty(X) will be true even if e.hasOwnProperty(X) is + * false. + */ + this.type = e.type + this.screenX = e.screenX + this.screenY = e.screenY + this.button = e.button + + this.keyCode = e.keyCode + this.charCode = e.charCode + this.ctrlKey = e.ctrlKey + this.altKey = e.altKey + this.shiftKey = e.shiftKey + this.metaKey = e.metaKey + + /* TODO: Test this. */ + if (e.touches) { + /* TODO: Decide if we want to rely on Array.prototype.map. If not, + * we should provide an implementation. + */ + this.touches = e.touches.map(function (coord) { + return { screenX: coord.screenX, screenY: coord.screenY } + }) + } + + this.state = e.state + this.defaultPrevented = e.defaultPrevented + }, + + subscriptions : {}, //object to store the subscriptions per window + + subscribers : {}, //object to store the subscribers to broadcast to + + requests : {}, //request objects stored until done + + subscribe : function(topic, handler){ + // subscribe to messages on certain topics + // only the local instance cares about the subscription + console.log("subscribing to: "+topic) + + // attach the handler here to be fired on receipt of the message + if(topic instanceof Array){ + for(var key in topic){ + var Topic = topic[key] + pushSub(Topic, handler) + } + } + else { + pushSub(topic, handler) + } + + function pushSub(topic, handler){ + if(epubsc.subscriptions[topic] && epubsc.subscriptions[topic].handlers){ + epubsc.subscriptions[topic].handlers.push(handler) + } else { + epubsc.subscriptions[topic] = {} + epubsc.subscriptions[topic].handlers = [] + epubsc.subscriptions[topic].handlers.push(handler) + } + } + }, + + unsubscribe : function(topic, handler){ + console.log("unsubscribing from: "+topic) + console.log(topic) + // unsubscribe from the locally assigned handler + for(var key in epubsc.subscriptions[topic].handlers){ + var Handler = epubsc.subscriptions[topic].handlers[key] + if(Handler == handler){ + delete epubsc.subscriptions[topic].handlers[key] + } + } + }, + + publish : function(topic, message){ + // publish messages to listeners + var payload = new this.Message("epubsc_publish", topic, message), + isEvent = subpub.isEvent(topic) + + // stringify if the browser doesn't support objects + if(subpub.postmessage_usestring()){ + payload = JSON.stringify(payload) + } + + // publish message to window.parent and any iframes in the window + if(window.parent != window){ + window.parent.postMessage(payload, "*") + } + + var iframes = document.getElementsByTagName("iframe") + for(var i = 0; i < iframes.length; i++){ + var iframe = iframes[i] + iframe.contentWindow.postMessage(payload, "*") + } + }, + + send : function(target, topic, message){ + // send a targeted message, this is used internally for rebroadcasting + // using send prevents the message from going recursive by going back up + var payload = new epubsc.Message("epubsc_publish", topic, message) + // stringify if the browser doesn't support objects + if(subpub.postmessage_usestring()){ + payload = JSON.stringify(payload) + } + target.postMessage(payload, "*") + } + } + + // the internal methods for subpub + var subpub = { + messageHandler : function(e){ + // respond to messages here + if(e.data == undefined) e.data = e.originalEvent.data + // check to see if it's a string, then parse it (to support older systems that use JSON (< IE 9)) + if(typeof e.data == "string") { e.data = JSON.parse(e.data) } + + // get the topic, then fire the position in + // descriptions that holds the handler + if(e.data.type == "epubsc_message") { + switch(e.data.method) { + case "epubsc_publish" : + subpub.publishHandler(e) + break + default : + console.log("epubsc: unknown method type") + break + } + } + + }, + + /** + * An array of all publishable events, used in init to setup the stack. + */ + publishableBrowserEvents : [ + "click", "dblclick", "mousedown", "mouseup", "mousemove", + "keydown", "keypress", "keyup", + "touchstart", "touchend", "touchmove", "touchcancel", + "pointerdown", "pointerup", "pointercancel", "pointermove" + ], + + isEvent : function(topic){ + var isEvent = false + for(var key in subpub.publishableBrowserEvents){ + var Event = subpub.publishableBrowserEvents[key] + if(topic == Event){ + isEvent = true + } + } + return isEvent + }, + + publishHandler : function(e){ + var topic = e.data.topic + + // fire off the local handler here + if(epubsc.subscriptions[topic]){ + for(var key in epubsc.subscriptions[topic].handlers){ + var Handler = epubsc.subscriptions[topic].handlers[key] + Handler(e) + } + } + + // rebroadcast to everyone + subpub.rebroadcast(e) + }, + rebroadcast : function(e){ + // blind rebroadcasting of all publishes + var iframes = document.getElementsByTagName("iframe") + for(var i=0;i + + + Events + + + + + + + + + +
src
+
No status
+
blurred
+
This area swallows keyup events
+ + + +
RS directives:
+ + + + + + + diff --git a/API/tests/parent.html b/API/tests/parent.html new file mode 100644 index 0000000..09b8810 --- /dev/null +++ b/API/tests/parent.html @@ -0,0 +1,153 @@ + + + + Parent + + + + + + + + + + +
src
+
No status
+ + + + + + + +
+
+ + + + +
+
+ + +
+
+ + + + + diff --git a/API/tests/parent2.html b/API/tests/parent2.html new file mode 100644 index 0000000..a5297d8 --- /dev/null +++ b/API/tests/parent2.html @@ -0,0 +1,101 @@ + + + + Parent2 + + + + + + + + + + +
src
+
No status
+ + + + +
+
+ + +
+
+ + +
+
+ + + + + + + diff --git a/API/tests/parentRS.html b/API/tests/parentRS.html new file mode 100644 index 0000000..3ff5bee --- /dev/null +++ b/API/tests/parentRS.html @@ -0,0 +1,470 @@ + + + + Events + + + + + + + + + + + +
src
+
No status
+
blurred
+ +
+
+ + +
+
+
+ + + + + + +
+
+ + +
+
+ + + + +
+
+ +
+ + +
+ +
+
+ Event received: +
+ +
+ ALL MOUSE: + 0 +
+ +
+ + click: + 0 +
+ +
+ + dblclick: + 0 +
+ +
+ + mousedown: + 0 +
+ +
+ + mouseup: + 0 +
+ +
+ + mouseover: + 0 +
+ +
+ + mousemove: + 0 +
+ +
+ + mouseout: + 0 +
+ +
+ ALL KEY: + 0 +
+ +
+ + keydown: + 0 +
+ +
+ + keypress: + 0 +
+ +
+ + keyup: + 0 +
+ +
+ + resize: + 0 +
+ +
+ + scroll: + 0 +
+ +
+ + focus: + 0 +
+ +
+ + blur: + 0 +
+ +
+ + focusin: + 0 +
+ +
+ + focusout: + 0 +
+ +
+ + DOMActivate: + 0 +
+ +
+ ALL TOUCH: + 0 +
+ +
+ + touchstart: + 0 +
+ +
+ + touchend: + 0 +
+ +
+ + touchmove: + 0 +
+ +
+ + touchenter: + 0 +
+ +
+ + touchleave: + 0 +
+ +
+ + touchcancel: + 0 +
+ +
+ ALL POINTER: + 0 +
+ +
+ + pointerdown: + 0 +
+ +
+ + pointerup: + 0 +
+ +
+ + pointercancel: + 0 +
+ +
+ + pointermove: + 0 +
+ +
+ + pointerover: + 0 +
+ +
+ + pointerout: + 0 +
+ +
+ + pointerenter: + 0 +
+ +
+ + pointerleave: + 0 +
+ +
+ + gotpointercapture: + 0 +
+ +
+ + lostpointercapture: + 0 +
+
+ +
+
+
+
+ + + + + + + + + + + + + + diff --git a/API/tests/parentScale.html b/API/tests/parentScale.html new file mode 100644 index 0000000..e9e912b --- /dev/null +++ b/API/tests/parentScale.html @@ -0,0 +1,192 @@ + + + + Parent2 + + + + + + + + + +
src
+
No status
+ +
+ +
+ +
+
+ + +
+
+ + + diff --git a/API/tests/supportJS/focus.js b/API/tests/supportJS/focus.js new file mode 100644 index 0000000..4f5d333 --- /dev/null +++ b/API/tests/supportJS/focus.js @@ -0,0 +1,15 @@ +window.addEventListener("focus", function (e) { + var focus = document.getElementById("focus_status"); + if (focus) + { + focus.innerHTML = "focused"; + } +}); + +window.addEventListener("blur", function (e) { + var focus = document.getElementById("focus_status"); + if (focus) + { + focus.innerHTML = "blurred"; + } +}); \ No newline at end of file diff --git a/API/tests/supportJS/pause.js b/API/tests/supportJS/pause.js new file mode 100644 index 0000000..b9a71a9 --- /dev/null +++ b/API/tests/supportJS/pause.js @@ -0,0 +1,5 @@ +/** + * Sample code. + */ + +epubsc.subscribe("epubsc_pause", handlePause); diff --git a/API/tests/supportJS/profiler.js b/API/tests/supportJS/profiler.js new file mode 100644 index 0000000..2c19bc4 --- /dev/null +++ b/API/tests/supportJS/profiler.js @@ -0,0 +1,26 @@ +epubsc.subscribe("profile", function (msg) { + var status = document.getElementById("status"); + if (status) + { + status.innerHTML = "Topic: [profiling] 15 seconds of profiling remain."; + + var count = eventMonitor.getEventCount(); + eventMonitor.stopCounters(); + window.console.profile(); + var timeLeft = 15; + var interval = setInterval(function () { + timeLeft -= 1; + status.innerHTML = "Topic: [profiling] " + timeLeft + " seconds of profiling remain."; + }, 1000); + + setTimeout(function () { + window.console.profileEnd(); + window.clearInterval(interval); + count = eventMonitor.getEventCount() - count; + eventMonitor.startCounters(); + alert("Profiling done: " + count); + status.innerHTML = "Topic: "; + }, 15000); + } +}); + diff --git a/API/tests/supportJS/timer.js b/API/tests/supportJS/timer.js new file mode 100644 index 0000000..6788727 --- /dev/null +++ b/API/tests/supportJS/timer.js @@ -0,0 +1,45 @@ +/* + * Simple sample code to toggle state of timers. + */ +var counter = 0; +var timer = document.getElementById("timer"); +var source = document.getElementById("source"); +var timerid = null; +var interval = 100; + +function handlePause(msg) { + if(typeof msg == "object"){ + // normalize the message + msg = msg.data.topicData + } + var e = document.getElementById("status"); + + if (msg === "pause") + { + e.innerHTML = "Topic: [paused]"; + + if (timer && timerid !== null) + { + clearInterval(timerid); + timerid = null; + } + } + else + { + e.innerHTML = "Topic: [running]"; + if (timer && timerid == null) + { + timerid = window.setInterval(function () { + timer.innerHTML = counter; + counter += interval; + }, interval); + } + } +} + +source.innerHTML = window.document.URL + ":" + epubsc.widgetID; +handlePause({ + data : { + topicData : "resume" + } +}); \ No newline at end of file diff --git a/API/tests/supportJS/tracker.js b/API/tests/supportJS/tracker.js new file mode 100644 index 0000000..24b61d2 --- /dev/null +++ b/API/tests/supportJS/tracker.js @@ -0,0 +1,16 @@ +window.addEventListener("mousemove", function (e) { + var tracker = document.getElementById("mousetrackerdata"); + if (tracker) + { + tracker.innerHTML = e.screenX + ", " + e.screenY; + } +}); + +window.addEventListener("touchmove", function (e) { + var tracker = document.getElementById("mousetrackerdata"); + if (tracker) + { + tracker.innerHTML = "touchmove: " + e.timeStamp; + } + e.preventDefault(); +}); diff --git a/API/tests/userAgent.html b/API/tests/userAgent.html new file mode 100644 index 0000000..eb389bb --- /dev/null +++ b/API/tests/userAgent.html @@ -0,0 +1,229 @@ + + + + + goog.userAgent + + + + + + + + +

goog.userAgent

+ +
+ +
+
+ + + +
+
+ +
+ + + +
+
+ + + + + + diff --git a/API/tests/wapievent/monitorEvents.js b/API/tests/wapievent/monitorEvents.js new file mode 100644 index 0000000..58f2ac9 --- /dev/null +++ b/API/tests/wapievent/monitorEvents.js @@ -0,0 +1,567 @@ +(function () { + + var eventMonitor_ = {}; + + var dispatch = true; + + var eventCounters_ = { + "click": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["click"].count += 1; + eventCounters_["click"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new MouseEvent("click", msg)); + } + } + }, + "dblclick": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["dblclick"].count += 1; + eventCounters_["dblclick"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new MouseEvent("dblclick", msg)); + } + } + }, + "mousedown": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["mousedown"].count += 1; + eventCounters_["mousedown"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new MouseEvent("mousedown", msg)); + } + } + }, + "mouseup": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["mouseup"].count += 1; + eventCounters_["mouseup"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new MouseEvent("mouseup", msg)); + } + } + }, + "mouseover": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["mouseover"].count += 1; + eventCounters_["mouseover"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new MouseEvent("mouseover", msg)); + } + } + }, + "mousemove": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["mousemove"].count += 1; + eventCounters_["mousemove"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new MouseEvent("mousemove", msg)); + } + } + }, + "mouseout": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["mouseout"].count += 1; + eventCounters_["mouseout"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new MouseEvent("mousedown", msg)); + } + } + }, + "keydown": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["keydown"].count += 1; + eventCounters_["keydown"].updated = false; + var keyboardEvent = new KeyboardEvent("keydown", msg); + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(keyboardEvent); + } + } + }, + "keypress": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["keypress"].count += 1; + eventCounters_["keypress"].updated = false; + var keyboardEvent = new KeyboardEvent("keypress", msg); + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(keyboardEvent); + } + } + }, + "keyup": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["keyup"].count += 1; + eventCounters_["keyup"].updated = false; + var keyboardEvent = new KeyboardEvent("keyup", msg); + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(keyboardEvent); + } + + } + }, + "resize": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["resize"].count += 1; + eventCounters_["resize"].updated = false; + } + }, + "scroll": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["scroll"].count += 1; + eventCounters_["scroll"].updated = false; + } + }, + "focus": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["focus"].count += 1; + eventCounters_["focus"].updated = false; + } + }, + + "blur": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["blur"].count += 1; + eventCounters_["blur"].updated = false; + } + }, + "focusin": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["focusin"].count += 1; + eventCounters_["focusin"].updated = false; + } + }, + "focusout": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["focusout"].count += 1; + eventCounters_["focusout"].updated = false; + } + }, + "DOMActivate": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["DOMActivate"].count += 1; + eventCounters_["DOMActivate"].updated = false; + } + }, + "touchstart": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["touchstart"].count += 1; + eventCounters_["touchstart"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new CustomEvent("touchstart", msg)); + } + + } + }, + "touchend": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["touchend"].count += 1; + eventCounters_["touchend"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new CustomEvent("touchend", msg)); + } + + } + }, + "touchmove": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["touchmove"].count += 1; + eventCounters_["touchmove"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new CustomEvent("touchmove", msg)); + } + + } + }, + "touchenter": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["touchenter"].count += 1; + eventCounters_["touchenter"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new CustomEvent("touchenter", msg)); + } + } + }, + "touchleave": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["touchleave"].count += 1; + eventCounters_["touchleave"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new CustomEvent("touchleave", msg)); + } + + } + }, + "touchcancel": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["touchcancel"].count += 1; + eventCounters_["touchcancel"].updated = false; + if (dispatch && !msg.defaultPrevented) + { + window.dispatchEvent(new CustomEvent("touchcancel", msg)); + } + } + }, + "pointerdown": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["pointerdown"].count += 1; + eventCounters_["pointerdown"].updated = false; + } + }, + "pointerup": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["pointerup"].count += 1; + eventCounters_["pointerup"].updated = false; + } + }, + "pointercancel": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["pointercancel"].count += 1; + eventCounters_["pointercancel"].updated = false; + } + }, + "pointermove": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["pointermove"].count += 1; + eventCounters_["pointermove"].updated = false; + } + }, + "pointerover": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["pointerover"].count += 1; + eventCounters_["pointerover"].updated = false; + } + }, + "pointerout": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["pointerout"].count += 1; + eventCounters_["pointerout"].updated = false; + } + }, + "pointerenter": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["pointerenter"].count += 1; + eventCounters_["pointerenter"].updated = false; + } + }, + "pointerleave": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["pointerleave"].count += 1; + eventCounters_["pointerleave"].updated = false; + } + }, + "gotpointercapture": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["gotpointercapture"].count += 1; + eventCounters_["gotpointercapture"].updated = false; + } + }, + "lostpointercapture": { + count: 0, + updated: true, + handler: function (msg) { + if (print_eventinfo) + { + printEventInfo(msg); + } + eventCounters_["lostpointercapture"].count += 1; + eventCounters_["lostpointercapture"].updated = false; + } + } + }; + + function subscribe(e) { + for (var key in e) + { + if (e.hasOwnProperty(key)) + { + wapi.subscribe(key, e[key].handler); + } + } + } + + function unsubscribe(e) { + for (var key in e) + { + if (e.hasOwnProperty(key)) + { + wapi.unsubscribe(key, e[key].handler); + } + } + } + + + var interval = null; + var interval2 = null; + + window.eventMonitor = {}; + + eventMonitor.eventCounters = eventCounters_; + + eventMonitor.startCounters = function () { + // updates counters + interval = window.setInterval(function () { + for (var key in eventCounters_) + { + if (eventCounters_.hasOwnProperty(key) && eventCounters_[key].updated === false) + { + var e = document.getElementById(key); + e.innerHTML = "" + eventCounters_[key].count + ""; + eventCounters_[key].updated = true; + } + } + }, 50); + + + // clears red color from recently updated counters + interval2 = window.setInterval(function () { + for (var key in eventCounters_) + { + if (eventCounters_.hasOwnProperty(key)) + { + var e = document.getElementById(key); + e.innerHTML = "" + eventCounters_[key].count + ""; + } + } + }, 500); + } + + eventMonitor.stopCounters = function () { + if (interval) + { + window.clearInterval(interval); + interval = null; + } + + if (interval2) + { + window.clearInterval(interval2); + interval2 = null; + } + } + + eventMonitor.startCounters(); + + eventMonitor.getEventCount = function () { + var count = 0; + for (var key in eventCounters_) + { + if (eventCounters_.hasOwnProperty(key)) + { + count += eventCounters_[key].count; + } + } + return count; + }; + + eventMonitor.toggleDispatching = function () { + dispatch = !dispatch; + }; + +})(); \ No newline at end of file diff --git a/API/tests/wapievent/subscribeEvents.js b/API/tests/wapievent/subscribeEvents.js new file mode 100644 index 0000000..e95c269 --- /dev/null +++ b/API/tests/wapievent/subscribeEvents.js @@ -0,0 +1,299 @@ +(function () { + + document.getElementById("sub_allmouse").addEventListener("click", function (e) { + var mouseSubscribe = [ + "click", "dblclick", "mousedown", "mouseup", + "mouseover", "mousemove", "mouseout" + ]; + if (e.srcElement.checked) + { + epubsc.publish("sysEventSubscribe", mouseSubscribe); + for (var i = 0; i < mouseSubscribe.length; i++) + { + document.getElementById("sub_" + mouseSubscribe[i]).checked = true; + epubsc.subscribe(mouseSubscribe[i], eventMonitor.eventCounters[mouseSubscribe[i]].handler); + } + } + else + { + epubsc.publish("sysEventUnsubscribe", mouseSubscribe); + for (var i = 0; i < mouseSubscribe.length; i++) + { + document.getElementById("sub_" + mouseSubscribe[i]).checked = false; + epubsc.unsubscribe(mouseSubscribe[i], eventMonitor.eventCounters[mouseSubscribe[i]].handler); + } + } + }, false); + + document.getElementById("sub_allkey").addEventListener("click", function (e) { + var keySubscribe = ["keydown", "keypress", "keyup"]; + if (e.srcElement.checked) + { + epubsc.publish("sysEventSubscribe", keySubscribe); + for (var i = 0; i < keySubscribe.length; i++) + { + document.getElementById("sub_" + keySubscribe[i]).checked = true; + epubsc.subscribe(keySubscribe[i], eventMonitor.eventCounters[keySubscribe[i]].handler); + } + } + else + { + epubsc.publish("sysEventUnsubscribe", keySubscribe); + for (var i = 0; i < keySubscribe.length; i++) + { + document.getElementById("sub_" + keySubscribe[i]).checked = false; + epubsc.unsubscribe(keySubscribe[i], eventMonitor.eventCounters[keySubscribe[i]].handler); + } + } + }, false); + + document.getElementById("sub_alltouch").addEventListener("click", function (e) { + var touchSubscribe = ["touchstart", "touchend", "touchmove", "touchenter", "touchleave", "touchcancel"]; + if (e.srcElement.checked) + { + epubsc.publish("sysEventSubscribe", touchSubscribe); + for (var i = 0; i < touchSubscribe.length; i++) + { + document.getElementById("sub_" + touchSubscribe[i]).checked = true; + epubsc.subscribe(touchSubscribe[i], eventMonitor.eventCounters[touchSubscribe[i]].handler); + } + } + else + { + epubsc.publish("sysEventUnsubscribe", touchSubscribe); + for (var i = 0; i < touchSubscribe.length; i++) + { + document.getElementById("sub_" + touchSubscribe[i]).checked = false; + epubsc.unsubscribe(touchSubscribe[i], eventMonitor.eventCounters[touchSubscribe[i]].handler); + } + } + }, false); + + document.getElementById("sub_allpointer").addEventListener("click", function (e) { + var pointerSubscribe = [ + "pointerdown", "pointerup", "pointercancel", "pointermove", + "pointerover", "pointerout", "pointerenter", "pointerleave", + "gotpointercapture", "lostpointercapture" + ]; + if (e.srcElement.checked) + { + epubsc.publish("sysEventSubscribe", pointerSubscribe); + for (var i = 0; i < pointerSubscribe.length; i++) + { + document.getElementById("sub_" + pointerSubscribe[i]).checked = true; + epubsc.subscribe(pointerSubscribe[i], eventMonitor.eventCounters[pointerSubscribe[i]].handler); + } + } + else + { + epubsc.publish("sysEventUnsubscribe", pointerSubscribe); + for (var i = 0; i < pointerSubscribe.length; i++) + { + document.getElementById("sub_" + pointerSubscribe[i]).checked = false; + epubsc.unsubscribe(pointerSubscribe[i], eventMonitor.eventCounters[pointerSubscribe[i]].handler); + } + } + }, false); + + + // TODO: recreate this to listen for all event types + function subscribe_(eventType) { + epubsc.publish("sysEventSubscribe", eventType); + epubsc.subscribe(eventType, eventMonitor.eventCounters[eventType].handler); + } + + function unsubscribe_(eventType) { + epubsc.publish("sysEventUnsubscribe", eventType); + epubsc.unsubscribe(eventType, eventMonitor.eventCounters[eventType].handler); + } + + + document.getElementById("sub_click").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("click") : unsubscribe_("click"); + }, false); + + + document.getElementById("sub_dblclick").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("dblclick") : unsubscribe_("dblclick"); + }, false); + + + document.getElementById("sub_mousedown").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("mousedown") : unsubscribe_("mousedown"); + }, false); + + + document.getElementById("sub_mouseup").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("mouseup") : unsubscribe_("mouseup"); + }, false); + + + document.getElementById("sub_mouseover").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("mouseover") : unsubscribe_("mouseover"); + }, false); + + + document.getElementById("sub_mousemove").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("mousemove") : unsubscribe_("mousemove"); + }, false); + + + document.getElementById("sub_mouseout").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("mouseout") : unsubscribe_("mouseout"); + }, false); + + + document.getElementById("sub_keydown").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("keydown") : unsubscribe_("keydown"); + }, false); + + + document.getElementById("sub_keypress").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("keypress") : unsubscribe_("keypress"); + }, false); + + + document.getElementById("sub_keyup").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("keyup") : unsubscribe_("keyup"); + }, false); + + + document.getElementById("sub_resize").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("resize") : unsubscribe_("resize"); + }, false); + + + document.getElementById("sub_scroll").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("scroll") : unsubscribe_("scroll"); + }, false); + + + document.getElementById("sub_focus").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("focus") : unsubscribe_("focus"); + }, false); + + + document.getElementById("sub_blur").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("blur") : unsubscribe_("blur"); + }, false); + + + document.getElementById("sub_focusin").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("focusin") : unsubscribe_("focusin"); + }, false); + + + document.getElementById("sub_focusout").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("focusout") : unsubscribe_("focusout"); + }, false); + + + document.getElementById("sub_DOMActivate").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("DOMActivate") : unsubscribe_("DOMActivate"); + }, false); + + + document.getElementById("sub_touchstart").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("touchstart") : unsubscribe_("touchstart"); + }, false); + + + document.getElementById("sub_touchend").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("touchend") : unsubscribe_("touchend"); + }, false); + + + document.getElementById("sub_touchmove").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("touchmove") : unsubscribe_("touchmove"); + }, false); + + + document.getElementById("sub_touchenter").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("touchenter") : unsubscribe_("touchenter"); + }, false); + + + document.getElementById("sub_touchleave").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("touchleave") : unsubscribe_("touchleave"); + }, false); + + + document.getElementById("sub_touchcancel").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("touchcancel") : unsubscribe_("touchcancel"); + }, false); + + + document.getElementById("sub_pointerdown").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("pointerdown") : unsubscribe_("pointerdown"); + }, false); + + + document.getElementById("sub_pointerup").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("pointerup") : unsubscribe_("pointerup"); + }, false); + + + document.getElementById("sub_pointercancel").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("pointercancel") : unsubscribe_("pointercancel"); + }, false); + + + document.getElementById("sub_pointermove").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("pointermove") : unsubscribe_("pointermove"); + }, false); + + + document.getElementById("sub_pointerover").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("pointerover") : unsubscribe_("pointerover"); + }, false); + + + document.getElementById("sub_pointerout").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("pointerout") : unsubscribe_("pointerout"); + }, false); + + + document.getElementById("sub_pointerenter").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("pointerenter") : unsubscribe_("pointerenter"); + }, false); + + + document.getElementById("sub_pointerleave").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("pointerleave") : unsubscribe_("pointerleave"); + }, false); + + + document.getElementById("sub_gotpointercapture").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("gotpointercapture") : unsubscribe_("gotpointercapture"); + }, false); + + + document.getElementById("sub_lostpointercapture").addEventListener("click", function (e) { + e.srcElement.checked === true ? subscribe_("lostpointercapture") : unsubscribe_("lostpointercapture"); + }, false); + + + window.print_eventinfo = true; + + document.getElementById("print_eventinfo").addEventListener("click", function (e) { + print_eventinfo = e.srcElement.checked; + if (!print_eventinfo) + { + var eventinfo = document.getElementById("eventinfo"); + eventinfo.innerHTML = ""; + } + }, false); + + window.printEventInfo = function (msg) { + var eventinfo = document.getElementById("eventinfo"); + eventinfo.innerHTML = ""; + for (key in msg) + { + if (msg.hasOwnProperty(key)) + { + eventinfo.innerHTML += "
" + key + ":" + msg[key] + "
"; + } + } + }; + +})(); \ No newline at end of file diff --git a/API/tests/widgets/childEventA.html b/API/tests/widgets/childEventA.html new file mode 100644 index 0000000..ade8bcd --- /dev/null +++ b/API/tests/widgets/childEventA.html @@ -0,0 +1,214 @@ + + + + Mini Widget + + + + + + + +
src
+
No status
+
blurred
+
+
This area sucks up click & mouseMove events (e.g calls preventDefault + on the click & mouseMove). +
+
(Chrome) 15 seconds of profiling. + +
+ +
+ +
+ +
+
+
+ + + + + + + diff --git a/API/tests/widgets/composition.htm b/API/tests/widgets/composition.htm new file mode 100644 index 0000000..c5f0158 --- /dev/null +++ b/API/tests/widgets/composition.htm @@ -0,0 +1,47 @@ + + + + Composition + + + + + + +
src
+
No status
+ + + + +
+ + + + diff --git a/API/tests/widgets/composition2.htm b/API/tests/widgets/composition2.htm new file mode 100644 index 0000000..64f6838 --- /dev/null +++ b/API/tests/widgets/composition2.htm @@ -0,0 +1,48 @@ + + + + Composition + + + + + + +
src
+
No status
+ + + + + +
+ + + + diff --git a/API/tests/widgets/composition3.htm b/API/tests/widgets/composition3.htm new file mode 100644 index 0000000..40f691a --- /dev/null +++ b/API/tests/widgets/composition3.htm @@ -0,0 +1,62 @@ + + + + Composition + + + + + + +
src
+
No status
+ + + + + +
+
+ + + + + diff --git a/API/tests/widgets/minigesture.htm b/API/tests/widgets/minigesture.htm new file mode 100644 index 0000000..a3bf0ec --- /dev/null +++ b/API/tests/widgets/minigesture.htm @@ -0,0 +1,51 @@ + + + + Mini Widget + + + + + + +
src
+
No status
+
+ +
+ + +
+ + + + + diff --git a/API/tests/widgets/miniwidget.htm b/API/tests/widgets/miniwidget.htm new file mode 100644 index 0000000..1a358cb --- /dev/null +++ b/API/tests/widgets/miniwidget.htm @@ -0,0 +1,25 @@ + + + + Mini Widget + + + + + + + +
src
+
No status
+
+ + + + diff --git a/API/tests/widgets/miniwidget2.htm b/API/tests/widgets/miniwidget2.htm new file mode 100644 index 0000000..2a950cf --- /dev/null +++ b/API/tests/widgets/miniwidget2.htm @@ -0,0 +1,63 @@ + + + + Mini Widget + + + + + + +
src
+
No status
+
+
+
+ + +
+ + + + + + From df790df2fbbce1693fb130ed1569a46f5ffc354e Mon Sep 17 00:00:00 2001 From: Greg Davis Date: Mon, 6 Jul 2015 00:05:13 -0600 Subject: [PATCH 2/4] update to include Darryl's complete list of UI events --- API/epubsc_api.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/API/epubsc_api.js b/API/epubsc_api.js index ade4bd7..0e3ce3a 100755 --- a/API/epubsc_api.js +++ b/API/epubsc_api.js @@ -200,10 +200,12 @@ var epubsc = (function(){ * An array of all publishable events, used in init to setup the stack. */ publishableBrowserEvents : [ - "click", "dblclick", "mousedown", "mouseup", "mousemove", - "keydown", "keypress", "keyup", + "load", "unload", "abort", "error", "select", "resize", "scroll", + "blur", "focus", "focusin", "focusout", + "click", "dblclick", "mousedown", "mouseenter", "mouseleave", "mousemove", "mouseout", "mouseover", "mouseup", + "wheel", "beforeinput", "input", "keydown", "keyup", "compositionstart", "compositionupdate", "compositionend", "touchstart", "touchend", "touchmove", "touchcancel", - "pointerdown", "pointerup", "pointercancel", "pointermove" + "pointerover", "pointerenter", "pointerdown", "pointermove", "pointerup", "pointercancel", "pointerout", "pointerleave", "gotpointercapture", "lostpointercapture" ], isEvent : function(topic){ From 679bd2192011dea94ad79bb1375bb5e67bde0e1e Mon Sep 17 00:00:00 2001 From: Greg Davis Date: Wed, 8 Jul 2015 15:32:54 -0600 Subject: [PATCH 3/4] Add in semicolons (I hate semicolons in JS) and fix bug on widget ID in messages. --- API/epubsc_api.js | 177 +++++++++++++++++++++++----------------------- 1 file changed, 87 insertions(+), 90 deletions(-) diff --git a/API/epubsc_api.js b/API/epubsc_api.js index 0e3ce3a..8cd0bc9 100755 --- a/API/epubsc_api.js +++ b/API/epubsc_api.js @@ -13,34 +13,34 @@ var epubsc = (function(){ var methods = { initialize : function(){ // set up the message listener here, needs to be done for widgets and pages - window.addEventListener("message", subpub.messageHandler) + window.addEventListener("message", subpub.messageHandler); // assign an id here - this.widgetID = subpub.genUuid() + this.widgetID = subpub.genUuid(); // setup the publishable browser event stack subpub.publishableBrowserEvents.forEach(function(eventName) { // Setup the event listener here - listen for bubble phase, then publish the event when it fires document.addEventListener(eventName, function(e){ - epubsc.publish("epubsc_event", new epubsc.BrowserEvent(e)) - }) - }) + epubsc.publish("epubsc_event", new epubsc.BrowserEvent(e)); + }); + }); // fire off the loading event - this.publish("epubsc_load", "loading") + this.publish("epubsc_load", "loading"); // set up the ready event if(window.parent != window){ window.addEventListener("DOMContentLoaded", function(){ - epubsc.publish("epubsc_ready", "ready") - }, false) + epubsc.publish("epubsc_ready", "ready"); + }, false); } // set up the unload event if(window.parent != window){ window.addEventListener("unload", function(){ - epubsc.send(window.parent, "epubsc_unload", document.URL) - }) + epubsc.send(window.parent, "epubsc_unload", document.URL); + }); } }, @@ -49,18 +49,15 @@ var epubsc = (function(){ Message : function(method, topic, message, bubbles){ // this is a constructor, so when it's invoked as "new" this will refer // only to the created object, not the whole PSO object - this.type = "epubsc_message" - this.method = method - this.timestamp = +new Date() // date string - this.id = subpub.genUuid() // unique id for the message - this.widgetId = this.widgetID // unique id for the widget during this session + this.type = "epubsc_message"; + this.method = method; + this.timestamp = +new Date(); // date string + this.id = subpub.genUuid(); // unique id for the message + this.widgetId = methods.widgetID; // unique id for the widget during this session // make the payload - this.topic = topic - this.topicData = message - - // not sure what this does, investigate... - this.widgetPath_ = []; + this.topic = topic; + this.topicData = message; }, BrowserEvent : function(e){ @@ -68,17 +65,17 @@ var epubsc = (function(){ * this.hasOwnProperty(X) will be true even if e.hasOwnProperty(X) is * false. */ - this.type = e.type - this.screenX = e.screenX - this.screenY = e.screenY - this.button = e.button - - this.keyCode = e.keyCode - this.charCode = e.charCode - this.ctrlKey = e.ctrlKey - this.altKey = e.altKey - this.shiftKey = e.shiftKey - this.metaKey = e.metaKey + this.type = e.type; + this.screenX = e.screenX; + this.screenY = e.screenY; + this.button = e.button; + + this.keyCode = e.keyCode; + this.charCode = e.charCode; + this.ctrlKey = e.ctrlKey; + this.altKey = e.altKey; + this.shiftKey = e.shiftKey; + this.metaKey = e.metaKey; /* TODO: Test this. */ if (e.touches) { @@ -86,12 +83,12 @@ var epubsc = (function(){ * we should provide an implementation. */ this.touches = e.touches.map(function (coord) { - return { screenX: coord.screenX, screenY: coord.screenY } - }) + return { screenX: coord.screenX, screenY: coord.screenY }; + }); } - this.state = e.state - this.defaultPrevented = e.defaultPrevented + this.state = e.state; + this.defaultPrevented = e.defaultPrevented; }, subscriptions : {}, //object to store the subscriptions per window @@ -103,38 +100,38 @@ var epubsc = (function(){ subscribe : function(topic, handler){ // subscribe to messages on certain topics // only the local instance cares about the subscription - console.log("subscribing to: "+topic) + console.log("subscribing to: "+topic); // attach the handler here to be fired on receipt of the message if(topic instanceof Array){ for(var key in topic){ - var Topic = topic[key] - pushSub(Topic, handler) + var Topic = topic[key]; + pushSub(Topic, handler); } } else { - pushSub(topic, handler) + pushSub(topic, handler); } function pushSub(topic, handler){ if(epubsc.subscriptions[topic] && epubsc.subscriptions[topic].handlers){ - epubsc.subscriptions[topic].handlers.push(handler) + epubsc.subscriptions[topic].handlers.push(handler); } else { - epubsc.subscriptions[topic] = {} - epubsc.subscriptions[topic].handlers = [] - epubsc.subscriptions[topic].handlers.push(handler) + epubsc.subscriptions[topic] = {}; + epubsc.subscriptions[topic].handlers = []; + epubsc.subscriptions[topic].handlers.push(handler); } } }, unsubscribe : function(topic, handler){ - console.log("unsubscribing from: "+topic) - console.log(topic) + console.log("unsubscribing from: "+topic); + console.log(topic); // unsubscribe from the locally assigned handler for(var key in epubsc.subscriptions[topic].handlers){ - var Handler = epubsc.subscriptions[topic].handlers[key] + var Handler = epubsc.subscriptions[topic].handlers[key]; if(Handler == handler){ - delete epubsc.subscriptions[topic].handlers[key] + delete epubsc.subscriptions[topic].handlers[key]; } } }, @@ -142,34 +139,34 @@ var epubsc = (function(){ publish : function(topic, message){ // publish messages to listeners var payload = new this.Message("epubsc_publish", topic, message), - isEvent = subpub.isEvent(topic) + isEvent = subpub.isEvent(topic); // stringify if the browser doesn't support objects if(subpub.postmessage_usestring()){ - payload = JSON.stringify(payload) + payload = JSON.stringify(payload); } // publish message to window.parent and any iframes in the window if(window.parent != window){ - window.parent.postMessage(payload, "*") + window.parent.postMessage(payload, "*"); } - var iframes = document.getElementsByTagName("iframe") + var iframes = document.getElementsByTagName("iframe"); for(var i = 0; i < iframes.length; i++){ - var iframe = iframes[i] - iframe.contentWindow.postMessage(payload, "*") + var iframe = iframes[i]; + iframe.contentWindow.postMessage(payload, "*"); } }, send : function(target, topic, message){ // send a targeted message, this is used internally for rebroadcasting // using send prevents the message from going recursive by going back up - var payload = new epubsc.Message("epubsc_publish", topic, message) + var payload = new epubsc.Message("epubsc_publish", topic, message); // stringify if the browser doesn't support objects if(subpub.postmessage_usestring()){ - payload = JSON.stringify(payload) + payload = JSON.stringify(payload); } - target.postMessage(payload, "*") + target.postMessage(payload, "*"); } } @@ -177,20 +174,20 @@ var epubsc = (function(){ var subpub = { messageHandler : function(e){ // respond to messages here - if(e.data == undefined) e.data = e.originalEvent.data + if(e.data == undefined) e.data = e.originalEvent.data; // check to see if it's a string, then parse it (to support older systems that use JSON (< IE 9)) - if(typeof e.data == "string") { e.data = JSON.parse(e.data) } + if(typeof e.data == "string") { e.data = JSON.parse(e.data); } // get the topic, then fire the position in // descriptions that holds the handler if(e.data.type == "epubsc_message") { switch(e.data.method) { case "epubsc_publish" : - subpub.publishHandler(e) - break + subpub.publishHandler(e); + break; default : - console.log("epubsc: unknown method type") - break + console.log("epubsc: unknown method type"); + break; } } @@ -211,74 +208,74 @@ var epubsc = (function(){ isEvent : function(topic){ var isEvent = false for(var key in subpub.publishableBrowserEvents){ - var Event = subpub.publishableBrowserEvents[key] + var Event = subpub.publishableBrowserEvents[key]; if(topic == Event){ - isEvent = true + isEvent = true; } } - return isEvent + return isEvent; }, publishHandler : function(e){ - var topic = e.data.topic + var topic = e.data.topic; // fire off the local handler here if(epubsc.subscriptions[topic]){ for(var key in epubsc.subscriptions[topic].handlers){ - var Handler = epubsc.subscriptions[topic].handlers[key] - Handler(e) + var Handler = epubsc.subscriptions[topic].handlers[key]; + Handler(e); } } // rebroadcast to everyone - subpub.rebroadcast(e) + subpub.rebroadcast(e); }, rebroadcast : function(e){ // blind rebroadcasting of all publishes - var iframes = document.getElementsByTagName("iframe") + var iframes = document.getElementsByTagName("iframe"); for(var i=0;i Date: Tue, 14 Jul 2015 11:38:12 -0600 Subject: [PATCH 4/4] bugfix from Ric Wright on rebroadcast double message --- API/epubsc_api.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/API/epubsc_api.js b/API/epubsc_api.js index 8cd0bc9..310094e 100755 --- a/API/epubsc_api.js +++ b/API/epubsc_api.js @@ -241,7 +241,9 @@ var epubsc = (function(){ } // TODO: get the publishes up to parent, but only if they come from children. // DO NOT re-publish to parent, will cause recursion. - if(window.parent != e.source) epubsc.send(window.parent, e.data.topic, e.data.topicData); + if(window.parent != e.source && window.parent != window){ + epubsc.send(window.parent, e.data.topic, e.data.topicData); + } }, genUuid : function(){ var d = new Date().getTime();