2013-02-24 09:36:08 +00:00
|
|
|
/*@
|
|
|
|
Ox.Message <o> Message controller
|
|
|
|
@*/
|
|
|
|
|
2013-02-24 07:37:20 +00:00
|
|
|
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);
|
2013-02-24 07:37:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
that.bind = function(callback) {
|
|
|
|
callbacks.push(callback);
|
2013-02-24 09:36:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
that.post = function(/*[target, ]event, data */) {
|
2013-02-24 07:37:20 +00:00
|
|
|
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;
|
2013-02-24 07:37:20 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-02-24 07:37:20 +00:00
|
|
|
return that;
|
2013-02-24 09:36:08 +00:00
|
|
|
|
2013-02-24 07:37:20 +00:00
|
|
|
})();
|