oxjs/source/Ox.UI/js/Core/Message.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2013-02-24 09:36:08 +00:00
/*@
Ox.Message <o> Message controller
@*/
Ox.Message = (function() {
var that = {},
callbacks = [];
window.addEventListener('message', function(e) {
var data = {};
try {
data = JSON.parse(e.data);
} catch(e) {}
if (data.event == 'init') {
window.oxid = data.data.id;
} else if (data.event) {
callbacks.forEach(function(callback) {
callback(data.event, data.data, data.oxid);
});
} else {
2013-02-24 09:36:08 +00:00
Ox.Log('Core', 'unknown message', e.data);
}
});
that.bind = function(callback) {
callbacks.push(callback);
2013-02-24 09:36:08 +00:00
};
that.post = function(/*[target, ]event, data */) {
var target, data, event;
if (arguments.length == 2) {
target = window.parent;
event = arguments[0];
data = arguments[1];
} else {
2013-02-24 09:36:08 +00:00
target = (
Ox.isElement(arguments[0][0])
? arguments[0][0]
: $('#' + arguments[0])[0]
).contentWindow;
event = arguments[1];
data = arguments[2];
}
target.postMessage(JSON.stringify({
event: event,
data: data,
oxid: window.oxid
}), '*');
2013-02-24 09:36:08 +00:00
};
return that;
2013-02-24 09:36:08 +00:00
})();