Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active October 9, 2016 18:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersevenrud/ff8b7c48b6a3cf9c823f51c7ff02705f to your computer and use it in GitHub Desktop.
Save andersevenrud/ff8b7c48b6a3cf9c823f51c7ff02705f to your computer and use it in GitHub Desktop.
OS.js Iframe Application bi-directional API script
/*!
* OS.js - JavaScript Cloud/Web Desktop Platform
*
* Copyright (c) 2011-2015, Anders Evenrud <andersevenrud@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Anders Evenrud <andersevenrud@gmail.com>
* @licence Simplified BSD License
*/
(function() {
'use strict';
var COUNT = 0;
window.OSjs = (function() {
var origin = {}; // Data that connects your iframe wirh OS.js app
var listeners = {}; // The list of event listeners
var callbacks = {};
//////////////////////////////////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////////////////////////////////
/**
* Clones given object
*/
function cloneo(o) {
return JSON.parse(JSON.stringify(o));
}
//////////////////////////////////////////////////////////////////////////
// Private methods
//////////////////////////////////////////////////////////////////////////
/**
* onload window event handler
*/
function _onload(ev) {
_emit('load', [ev]);
}
/**
* Parse a recieved event
*/
function _onmessage(ev) {
if ( ev && ev.data ) {
var data = cloneo(ev.data);
if ( data.message === 'Window::init' ) {
origin.wid = data.wid;
origin.pid = data.pid;
Object.freeze(origin);
_emit('init', [origin]);
} else if ( data.message === 'Window::destroy' ) {
_emit('destroy');
} else {
if ( data.message ) {
if ( typeof data.message === 'object' ) {
if ( typeof data.message.id === 'number' && data.message.method && typeof callbacks[data.message.id] === 'function' ) {
callbacks[data.message.id](data.message.error, data.message.result);
delete callbacks[data.message.id];
}
} else {
var msg = data.message;
delete data.message;
_emit('message:' + msg, data);
}
} else {
_emit('data', data);
}
}
}
}
/**
* Emits a bound event
*/
function _emit(type, args) {
if ( listeners[type] ) {
listeners[type].forEach(function(listener) {
listener.apply(null, args || []);
});
}
}
//////////////////////////////////////////////////////////////////////////
// Main
//////////////////////////////////////////////////////////////////////////
window.addEventListener('load', _onload);
window.addEventListener('message', _onmessage, false);
//////////////////////////////////////////////////////////////////////////
// Public methods
//////////////////////////////////////////////////////////////////////////
var exports = {
/**
* Bind an event
*/
on: function(type, cb) {
if ( !listeners[type] ) {
listeners[type] = [];
}
listeners[type].push(cb);
},
/**
* Make a callback-type request to your OS.js app.
*/
request: function(method, args, cb) {
callbacks[COUNT] = cb;
exports.send({
id: COUNT,
method: method,
args: args
});
COUNT++;
},
/**
* Send a message to OS.js app.
* Is recieved by `Application.onPostMessage`
*/
send: function(data) {
if ( origin.pid ) {
top.postMessage({
wid: origin.wid,
pid: origin.pid,
message: data
}, '*');
}
}
};
return exports;
})();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment