update Message code

This commit is contained in:
rolux 2013-02-24 16:27:37 +05:30
parent c1f9e9d7a3
commit e6bc3dcdbb
3 changed files with 70 additions and 41 deletions

View file

@ -289,7 +289,7 @@ Ox.load.UI = function(options, callback) {
Ox.$window = Ox.UI.$window = $(window); Ox.$window = Ox.UI.$window = $(window);
Ox.$parent = { Ox.$parent = {
postMessage: function(event, message) { postMessage: function(event, message) {
Ox.Message.post(event, message); Ox.Message.post(Ox.$parent, event, message);
return this; return this;
}, },
onMessage: function() { onMessage: function() {

View file

@ -93,32 +93,8 @@ Ox.Element = function(options, self) {
that.on('load', function() { that.on('load', function() {
Ox.Message.post(that, 'init', {id: that.oxid}); Ox.Message.post(that, 'init', {id: that.oxid});
}); });
that.onMessage = function() {
var callback;
if (Ox.isObject(arguments[0])) {
Ox.forEach(arguments[0], function(callback, event) {
Ox.Message.bind(arguments[0], function(event_, data, oxid) {
if (event_ == event && oxid == that.oxid) {
callback(data);
}
});
});
} else {
callback = arguments[0];
Ox.Message.bind(function(event, data, oxid) {
if (that.oxid == oxid) {
callback(event, data);
}
});
}
return that;
};
that.postMessage = function(event, data) {
Ox.Message.post(that, event, data);
return that;
};
} }
setTooltip(); setTooltip();
function mousedown(e) { function mousedown(e) {
@ -368,6 +344,42 @@ Ox.Element = function(options, self) {
return that; return that;
}; };
/*@
onMessage <f> Adds message handlers (if the element is an iframe)
(callback) -> <o> This element
Adds a catch-all handler
(event, callback) -> <o> This element
Adds a handler for a single event
({event: callback, ...}) -> <o> This element
Adds handlers for multiple events
callback <f> Callback function
data <o> event data (key/value pairs)
event <s> Event name
@*/
that.onMessage = function() {
// FIXME: Implement catch-all handler
var callback;
if (self.options.element == '<iframe>') {
if (Ox.isObject(arguments[0])) {
Ox.forEach(arguments[0], function(callback, event) {
Ox.Message.bind(arguments[0], function(event_, data, oxid) {
if (event_ == event && oxid == that.oxid) {
callback(data);
}
});
});
} else {
callback = arguments[0];
Ox.Message.bind(function(event, data, oxid) {
if (that.oxid == oxid) {
callback(event, data);
}
});
}
}
return that;
};
/*@ /*@
options <f> Gets or sets the options of an element object options <f> Gets or sets the options of an element object
() -> <o> All options () -> <o> All options
@ -385,6 +397,19 @@ Ox.Element = function(options, self) {
return Ox.getset(self.options, arguments, update, that); return Ox.getset(self.options, arguments, update, that);
}; };
/*@
postMessage <f> Sends a message (if the element is an iframe)
(event, data) -> This element
event <s> Event name
data <o> Event data
@*/
that.postMessage = function(event, data) {
if (self.options.element == '<iframe>') {
Ox.Message.post(that, event, data);
return that;
}
};
/*@ /*@
removeElement <f> Removes an element object and its event handler removeElement <f> Removes an element object and its event handler
() -> <o> This element () -> <o> This element

View file

@ -23,30 +23,34 @@ Ox.Message = (function() {
} }
}); });
/*@
.bind <f> Adds message handler
(callback) -> <o> Ox.Message
callback <f> Callback function
@*/
that.bind = function(callback) { that.bind = function(callback) {
callbacks.push(callback); callbacks.push(callback);
return that;
}; };
that.post = function(/*[target, ]event, data */) { /*@
var target, data, event; .post <f> Sends a message
if (arguments.length == 2) { (target, event, data) -> <o> Ox.Message
target = window.parent; target <o|s> Ox.$parent or iframe Ox.Element or element id
event = arguments[0]; event <s> Event name
data = arguments[1]; data <o> Event data
} else { @*/
target = ( that.post = function(target, event, data) {
Ox.isElement(arguments[0][0]) target = target == Ox.$parent ? window.parent
? arguments[0][0] ? Ox.isElement(target[0]) ? target[0]
: $('#' + arguments[0])[0] : $('#' + target)[0];
).contentWindow;
event = arguments[1];
data = arguments[2];
}
target.postMessage(JSON.stringify({ target.postMessage(JSON.stringify({
event: event, event: event,
data: data, data: data,
oxid: window.oxid oxid: window.oxid
}), '*'); }), '*');
return that;
}; };
return that; return that;