update Message code
This commit is contained in:
parent
c1f9e9d7a3
commit
e6bc3dcdbb
3 changed files with 70 additions and 41 deletions
|
@ -289,7 +289,7 @@ Ox.load.UI = function(options, callback) {
|
|||
Ox.$window = Ox.UI.$window = $(window);
|
||||
Ox.$parent = {
|
||||
postMessage: function(event, message) {
|
||||
Ox.Message.post(event, message);
|
||||
Ox.Message.post(Ox.$parent, event, message);
|
||||
return this;
|
||||
},
|
||||
onMessage: function() {
|
||||
|
|
|
@ -93,32 +93,8 @@ Ox.Element = function(options, self) {
|
|||
that.on('load', function() {
|
||||
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();
|
||||
|
||||
function mousedown(e) {
|
||||
|
@ -368,6 +344,42 @@ Ox.Element = function(options, self) {
|
|||
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
|
||||
() -> <o> All options
|
||||
|
@ -385,6 +397,19 @@ Ox.Element = function(options, self) {
|
|||
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
|
||||
() -> <o> This element
|
||||
|
|
|
@ -23,30 +23,34 @@ Ox.Message = (function() {
|
|||
}
|
||||
});
|
||||
|
||||
/*@
|
||||
.bind <f> Adds message handler
|
||||
(callback) -> <o> Ox.Message
|
||||
callback <f> Callback function
|
||||
|
||||
@*/
|
||||
that.bind = function(callback) {
|
||||
callbacks.push(callback);
|
||||
return that;
|
||||
};
|
||||
|
||||
that.post = function(/*[target, ]event, data */) {
|
||||
var target, data, event;
|
||||
if (arguments.length == 2) {
|
||||
target = window.parent;
|
||||
event = arguments[0];
|
||||
data = arguments[1];
|
||||
} else {
|
||||
target = (
|
||||
Ox.isElement(arguments[0][0])
|
||||
? arguments[0][0]
|
||||
: $('#' + arguments[0])[0]
|
||||
).contentWindow;
|
||||
event = arguments[1];
|
||||
data = arguments[2];
|
||||
}
|
||||
/*@
|
||||
.post <f> Sends a message
|
||||
(target, event, data) -> <o> Ox.Message
|
||||
target <o|s> Ox.$parent or iframe Ox.Element or element id
|
||||
event <s> Event name
|
||||
data <o> Event data
|
||||
@*/
|
||||
that.post = function(target, event, data) {
|
||||
target = target == Ox.$parent ? window.parent
|
||||
? Ox.isElement(target[0]) ? target[0]
|
||||
: $('#' + target)[0];
|
||||
target.postMessage(JSON.stringify({
|
||||
event: event,
|
||||
data: data,
|
||||
oxid: window.oxid
|
||||
}), '*');
|
||||
return that;
|
||||
};
|
||||
|
||||
return that;
|
||||
|
|
Loading…
Reference in a new issue