2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-09-23 10:43:57 +00:00
|
|
|
/*@
|
2012-06-30 09:24:03 +00:00
|
|
|
Ox.Event <o> Used internally for events
|
2011-09-23 10:43:57 +00:00
|
|
|
@*/
|
|
|
|
|
|
|
|
Ox.Event = (function() {
|
|
|
|
|
2012-06-30 09:24:03 +00:00
|
|
|
var eventHandlers = [],
|
|
|
|
that = {};
|
|
|
|
|
|
|
|
function log(data, event, self) {
|
|
|
|
var element = this,
|
|
|
|
handlers = self.eventHandlers ? self.eventHandlers[event] : [];
|
|
|
|
if (!Ox.contains([
|
|
|
|
'mousedown', 'mouserepeat', 'anyclick', 'singleclick', 'doubleclick',
|
|
|
|
'dragstart', 'drag', 'dragenter', 'dragleave', 'dragpause', 'dragend',
|
|
|
|
'draganddropstart', 'draganddrop', 'draganddropenter', 'draganddropleave', 'draganddropend',
|
|
|
|
'playing', 'position', 'progress'
|
|
|
|
], event)) {
|
|
|
|
try {
|
|
|
|
data = JSON.stringify(data)
|
|
|
|
} catch(e) {}
|
|
|
|
Ox.print(
|
|
|
|
'EVENT',
|
|
|
|
element.oxid,
|
|
|
|
'"' + element[0].className.split(' ').filter(function(className) {
|
|
|
|
return /^Ox/.test(className);
|
|
|
|
}).map(function(className) {
|
|
|
|
return className.replace(/^Ox/, '');
|
|
|
|
}).join(' ') + '"',
|
|
|
|
event,
|
|
|
|
data,
|
|
|
|
handlers.length,
|
|
|
|
handlers.map(function(handler) {
|
|
|
|
return handler.toString().split('\n').shift();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-09-23 10:43:57 +00:00
|
|
|
|
|
|
|
/*@
|
2012-06-30 10:29:06 +00:00
|
|
|
.bind <f> Adds event handler(s)
|
2012-06-30 09:24:03 +00:00
|
|
|
(callback) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Adds a global event handler
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, callback) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Adds a catch-all handler
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, event, callback) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Adds a handler for a single event
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, {event: callback, ...}) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Adds handlers for multiple events
|
2012-06-30 09:24:03 +00:00
|
|
|
self <o> The element's shared private object
|
2011-09-23 10:43:57 +00:00
|
|
|
callback <f> Callback function
|
2012-06-30 09:24:03 +00:00
|
|
|
data <o> Event data
|
|
|
|
event <s> Event name
|
|
|
|
element <o> Element
|
2011-09-23 10:43:57 +00:00
|
|
|
event <s> Event name
|
2012-06-02 11:06:44 +00:00
|
|
|
Event names can be namespaced, like `'click.foo'`
|
2012-06-29 12:24:06 +00:00
|
|
|
*/
|
2012-06-30 09:24:03 +00:00
|
|
|
that.bind = function() {
|
|
|
|
var args = Ox.toArray(arguments), once, self;
|
|
|
|
if (args.length == 1) {
|
|
|
|
eventHandlers.push(args[0])
|
2012-06-29 12:24:06 +00:00
|
|
|
} else {
|
2012-06-30 09:24:03 +00:00
|
|
|
self = args.shift();
|
|
|
|
once = Ox.isBoolean(Ox.last(args)) ? args.pop() : false;
|
|
|
|
args = Ox.isFunction(args[0]) ? {'*': args[0]} : Ox.makeObject(args);
|
|
|
|
if (Ox.len(args) && !self.eventHandlers) {
|
|
|
|
self.eventHandlers = {};
|
2012-06-29 12:24:06 +00:00
|
|
|
}
|
2012-06-30 10:15:27 +00:00
|
|
|
Ox.forEach(args, function(callback, event) {
|
2012-06-30 09:24:03 +00:00
|
|
|
self.eventHandlers[event] = (
|
|
|
|
self.eventHandlers[event] || []
|
2012-06-30 10:15:27 +00:00
|
|
|
).concat({callback: callback, once: once});
|
2012-06-29 12:24:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-09-23 10:43:57 +00:00
|
|
|
/*@
|
2012-06-30 10:29:06 +00:00
|
|
|
.bindOnce <f> Adds event handler(s) that run(s) only once
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, callback) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Adds a catch-all handler
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, event, callback) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Adds an event handler for a single event
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, {event: callback, ...}) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Adds event handlers for multiple events
|
2012-06-30 09:24:03 +00:00
|
|
|
self <o> The element's shared private object
|
2011-09-23 10:43:57 +00:00
|
|
|
callback <f> Callback function
|
2012-06-30 09:24:03 +00:00
|
|
|
data <o> Event data
|
|
|
|
event <s> Event name
|
|
|
|
element <o> Element
|
2011-09-23 10:43:57 +00:00
|
|
|
event <s> Event name
|
2012-06-30 09:24:03 +00:00
|
|
|
Event names can be namespaced, like `'click.foo'`
|
|
|
|
*/
|
2011-09-23 10:43:57 +00:00
|
|
|
that.bindOnce = function() {
|
2012-06-30 09:24:03 +00:00
|
|
|
return that.bind.apply(null, Ox.slice(arguments).concat(true));
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
.log <f> Turns event logging on or off
|
|
|
|
(enabled) -> <o> Ox.Event
|
|
|
|
enabled <b> Enables (`true`) or disables (`false`) event logging
|
|
|
|
*/
|
|
|
|
that.log = function(enabled) {
|
|
|
|
that[enabled ? 'bind' : 'unbind'](log);
|
2011-09-23 10:43:57 +00:00
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-06-30 09:24:03 +00:00
|
|
|
.trigger <f> Triggers an event
|
|
|
|
(self, event) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Triggers an event
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, event, data) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Triggers an event with data
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, {event: data, ...}) -> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Triggers multiple events with data
|
2012-06-30 09:24:03 +00:00
|
|
|
self <o> The element's shared private object
|
2011-09-23 10:43:57 +00:00
|
|
|
event <s> Event name
|
2012-06-30 09:24:03 +00:00
|
|
|
data <o> Event data
|
|
|
|
*/
|
|
|
|
that.trigger = function(self) {
|
2012-06-30 15:05:35 +00:00
|
|
|
var args = arguments, element = this;
|
2012-06-30 16:11:29 +00:00
|
|
|
if (self.eventHandlers) {
|
2012-06-30 15:05:35 +00:00
|
|
|
Ox.forEach(Ox.makeObject(Ox.slice(args, 1)), function(data, event) {
|
2012-06-30 09:24:03 +00:00
|
|
|
var triggered = event.split('.');
|
2012-06-30 15:05:35 +00:00
|
|
|
eventHandlers.forEach(function(callback) {
|
|
|
|
callback.call(element, data || {}, event, element);
|
|
|
|
});
|
2012-06-30 09:24:03 +00:00
|
|
|
triggered.map(function(v, i) {
|
|
|
|
return triggered.slice(0, i + 1).join('.');
|
|
|
|
}).concat('*').forEach(function(triggered) {
|
|
|
|
var handlers = self.eventHandlers[triggered];
|
2012-06-30 10:15:27 +00:00
|
|
|
handlers && handlers.forEach(function(handler, i) {
|
|
|
|
handler.once && handlers.splice(i, 1);
|
|
|
|
handler.callback.call(element, data || {}, event);
|
2012-06-30 09:24:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-06-30 16:11:29 +00:00
|
|
|
}
|
2011-09-23 10:43:57 +00:00
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-06-30 09:24:03 +00:00
|
|
|
.unbind <f> Removes an event handler
|
2012-06-30 10:15:27 +00:00
|
|
|
() -> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes all global handlers
|
2012-06-30 09:24:03 +00:00
|
|
|
(callback) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes a global handler
|
2012-06-30 09:24:03 +00:00
|
|
|
(self) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes all handlers
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, callback) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes a specific catch-all handler
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, event) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Remove all handlers for a single event
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, event, callback) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes a specific handler for a single event
|
2012-06-30 09:24:03 +00:00
|
|
|
(self, {event: callback, ...}) -> <o> Ox.Event
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes specific event handlers for multiple events
|
2012-06-30 09:24:03 +00:00
|
|
|
self <o> The element's shared private object
|
|
|
|
callback <f> Callback function
|
2011-09-23 10:43:57 +00:00
|
|
|
event <s> Event name
|
2012-06-30 09:24:03 +00:00
|
|
|
*/
|
2011-09-23 10:43:57 +00:00
|
|
|
that.unbind = function() {
|
2012-06-30 09:24:03 +00:00
|
|
|
var args = Ox.slice(arguments), self;
|
2012-06-30 10:15:27 +00:00
|
|
|
if (args.length == 0) {
|
|
|
|
eventHandlers = [];
|
|
|
|
} else if (Ox.isFunction(args[0])) {
|
2012-06-30 15:05:35 +00:00
|
|
|
eventHandlers.forEach(function(handler, i) {
|
2012-06-30 09:24:03 +00:00
|
|
|
handler === args[0] && eventHandlers.splice(i, 1);
|
2011-09-23 10:43:57 +00:00
|
|
|
});
|
2012-06-30 09:24:03 +00:00
|
|
|
} else if ((self = args.shift()).eventHandlers) {
|
|
|
|
if (args.length == 0) {
|
|
|
|
delete self.eventHandlers;
|
|
|
|
} else {
|
|
|
|
if (Ox.isFunction(args[0])) {
|
|
|
|
args = {'*': args[0]};
|
|
|
|
}
|
2012-06-30 10:15:27 +00:00
|
|
|
Ox.forEach(Ox.makeObject(args), function(callback, event) {
|
|
|
|
if (Ox.isUndefined(callback)) {
|
2012-06-30 09:24:03 +00:00
|
|
|
delete self.eventHandlers[event];
|
|
|
|
} else {
|
|
|
|
self.eventHandlers[event].forEach(function(handler, i) {
|
2012-06-30 10:15:27 +00:00
|
|
|
if (handler.callback === callback) {
|
|
|
|
self.eventHandlers[event].splice(i, 1);
|
|
|
|
}
|
2012-06-30 09:24:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-09-23 10:43:57 +00:00
|
|
|
}
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
2012-05-21 19:23:16 +00:00
|
|
|
}());
|