Modular integrations UI: Move JS code to js resource file. ObjC -> Modular JS starts to work

This commit is contained in:
manuroe
2017-09-08 13:51:06 +02:00
parent b0a9836c36
commit 29997c1f68
2 changed files with 74 additions and 27 deletions
+63
View File
@@ -0,0 +1,63 @@
/*
Copyright 2017 Vector Creations Ltd
Licensed 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.
*/
window.riotIOS = {};
// Generic JS -> ObjC bridge
window.riotIOS.sendObjectMessageToObjC = function(parameters) {
var iframe = document.createElement('iframe');
iframe.setAttribute('src', 'js:' + JSON.stringify(parameters));
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
};
window.riotIOS.events = {};
// Listen to messages posted by Modular
window.riotIOS.onMessage = function(event) {
// Do not SPAM ObjC with event already managed
if (riotIOS.events[event.data._id]) {
return;
}
// Keep this event for future usage
riotIOS.events[event.data._id] = event;
riotIOS.sendObjectMessageToObjC({
'event.data': event.data,
});
};
window.addEventListener('message', riotIOS.onMessage, false);
// ObjC -> Modular JS bridge
window.riotIOS.sendResponse = function(eventId, res) {
// Retrieve the correspong JS event
var event = riotIOS.events[eventId];
console.log(eventId + ": " + event);
var data = JSON.parse(JSON.stringify(event.data));
data.response = res;
event.source.postMessage(data, event.origin);
// Mark this event as handled
riotIOS.events[eventId] = true;
}