new event controller
This commit is contained in:
parent
dfa0a9b8eb
commit
32f85e42e4
1 changed files with 137 additions and 120 deletions
|
@ -1,157 +1,174 @@
|
|||
'use strict';
|
||||
|
||||
/*@
|
||||
Ox.Event <o> Basic event handler
|
||||
Ox.Event <o> Used internally for events
|
||||
@*/
|
||||
|
||||
// FIXME: unused
|
||||
|
||||
Ox.Event = (function() {
|
||||
|
||||
var that = {};
|
||||
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();
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/*@
|
||||
bind <f> Binds a callback to an event
|
||||
(event, callback) -> <o> The event handler
|
||||
({event: callback, ...}) -> <o> The event handler
|
||||
.bind <f> Adds an event handler
|
||||
(callback) -> <o> Ox.Event
|
||||
(self, callback) -> <o> Ox.Event
|
||||
(self, event, callback) -> <o> Ox.Event
|
||||
(self, {event: callback, ...}) -> <o> Ox.Event
|
||||
self <o> The element's shared private object
|
||||
callback <f> Callback function
|
||||
data <*> Event data
|
||||
data <o> Event data
|
||||
event <s> Event name
|
||||
element <o> Element
|
||||
event <s> Event name
|
||||
Event names can be namespaced, like `'click.foo'`
|
||||
@*/
|
||||
that.bind = function(self) {
|
||||
var args = Ox.slice(arguments, 1), once;
|
||||
if (Ox.isBoolean(Ox.last(args))) {
|
||||
once = args.pop();
|
||||
}
|
||||
if (Ox.isFunction(args[0])) {
|
||||
args = {'': args[0]};
|
||||
}
|
||||
Ox.forEach(Ox.makeObject(args), function(callback, event) {
|
||||
if (once) {
|
||||
callback.once = true;
|
||||
*/
|
||||
that.bind = function() {
|
||||
var args = Ox.toArray(arguments), once, self;
|
||||
if (args.length == 1) {
|
||||
eventHandlers.push(args[0])
|
||||
} else {
|
||||
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 = {};
|
||||
}
|
||||
self.eventHandlers = self.eventHandlers || {};
|
||||
self.eventHandlers[event] = (
|
||||
self.eventHandlers[event] || []
|
||||
).concat(callback);
|
||||
});
|
||||
Ox.forEach(args, function(handler, event) {
|
||||
handler.once = once;
|
||||
self.eventHandlers[event] = (
|
||||
self.eventHandlers[event] || []
|
||||
).concat(handler);
|
||||
});
|
||||
}
|
||||
return that;
|
||||
};
|
||||
|
||||
/*@
|
||||
.bindOnce <f> Adds an event handler that fires once
|
||||
(self, callback) -> <o> Ox.Event
|
||||
(self, event, callback) -> <o> Ox.Event
|
||||
(self, {event: callback, ...}) -> <o> Ox.Event
|
||||
self <o> The element's shared private object
|
||||
callback <f> Callback function
|
||||
data <o> Event data
|
||||
event <s> Event name
|
||||
element <o> Element
|
||||
event <s> Event name
|
||||
Event names can be namespaced, like `'click.foo'`
|
||||
*/
|
||||
that.bindOnce = function() {
|
||||
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);
|
||||
return that;
|
||||
};
|
||||
|
||||
/*@
|
||||
.trigger <f> Triggers an event
|
||||
(self, event) -> <o> Ox.Event
|
||||
(self, event, data) -> <o> Ox.Event
|
||||
(self, {event: data, ...}) -> Ox.Event
|
||||
self <o> The element's shared private object
|
||||
event <s> Event name
|
||||
data <o> Event data
|
||||
*/
|
||||
that.trigger = function(self) {
|
||||
if (!self.eventHandlers) {
|
||||
return;
|
||||
}
|
||||
Ox.forEach(Ox.makeObject(
|
||||
Ox.slice(arguments, 1, -1)
|
||||
), function(data, event) {
|
||||
triggered = event.split('.');
|
||||
triggered.map(function(v, i) {
|
||||
return triggered.slice(0, i + 1).join('.');
|
||||
}).forEach(function(triggered) {
|
||||
Ox.forEach(
|
||||
Ox.extend(
|
||||
{'': self.eventHandlers[''] || []},
|
||||
triggered, self.eventHandlers[triggered] || []
|
||||
),
|
||||
function(handlers, handled) {
|
||||
var element = this;
|
||||
if (self.eventHandlers) {
|
||||
Ox.forEach(Ox.makeObject(
|
||||
Ox.slice(arguments, 1)
|
||||
), function(data, event) {
|
||||
var triggered = event.split('.');
|
||||
triggered.map(function(v, i) {
|
||||
return triggered.slice(0, i + 1).join('.');
|
||||
}).concat('*').forEach(function(triggered) {
|
||||
var handlers = self.eventHandlers[triggered];
|
||||
handlers && handlers.forEach(function(handler) {
|
||||
handler.once && that.unbind(self, triggered, handler);
|
||||
handler(data || {}, event, Ox.last(arguments).oxid);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
return that;
|
||||
};
|
||||
|
||||
that.unbind = function(self) {
|
||||
if (!self.eventHandlers) {
|
||||
return;
|
||||
}
|
||||
var args = Ox.slice(arguments, 1);
|
||||
if (args.length == 0) {
|
||||
delete self.eventHandlers;
|
||||
} else {
|
||||
if (Ox.isFunction(args[0])) {
|
||||
args = {'': args[0]};
|
||||
}
|
||||
Ox.forEach(Ox.makeObject(args), function(unbound, event) {
|
||||
if (!unbound) {
|
||||
delete self.eventHandlers[event];
|
||||
} else {
|
||||
self.eventHandlers[event].forEach(function(bound, i) {
|
||||
if (bound == unbound) {
|
||||
self.eventHandlers[event].splice(i, 1);
|
||||
}
|
||||
handler.call(element, data || {}, event);
|
||||
});
|
||||
}
|
||||
});
|
||||
eventHandlers.forEach(function(handler) {
|
||||
handler.call(element, data || {}, event, element);
|
||||
});
|
||||
});
|
||||
}
|
||||
return that;
|
||||
};
|
||||
|
||||
/*@
|
||||
bindOnce <f> Binds a callback to an event, once
|
||||
(event, callback) -> <o> The event handler
|
||||
({event: callback, ...}) -> <o> The event handler
|
||||
.unbind <f> Removes an event handler
|
||||
(callback) -> <o> Ox.Event
|
||||
(self) -> <o> Ox.Event
|
||||
(self, callback) -> <o> Ox.Event
|
||||
(self, event) -> <o> Ox.Event
|
||||
(self, event, callback) -> <o> Ox.Event
|
||||
(self, {event: callback, ...}) -> <o> Ox.Event
|
||||
self <o> The element's shared private object
|
||||
callback <f> Callback function
|
||||
data <*> Event data
|
||||
event <s> Event name
|
||||
Event names can be namespaced, like `'click.foo'`
|
||||
@*/
|
||||
that.bindOnce = function() {
|
||||
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
|
||||
self.$eventHandler.one('ox_' + event, function(event, data) {
|
||||
callback(data.value);
|
||||
});
|
||||
});
|
||||
return that;
|
||||
};
|
||||
|
||||
/*@
|
||||
trigger <function> Triggers an event
|
||||
(event) -> <o> The event handler
|
||||
(event, data) -> <o> The event handler
|
||||
({event: data, ...}) -> <o> The event handler
|
||||
event <s> Event name
|
||||
data <*> Event data
|
||||
@*/
|
||||
that.trigger = function() {
|
||||
Ox.forEach(Ox.makeObject(arguments), function(data, event) {
|
||||
Ox.Log('Core', 'Ox.Event.trigger', event, data)
|
||||
self.$eventHandler.trigger('ox_' + event, {value: data});
|
||||
});
|
||||
return that;
|
||||
};
|
||||
|
||||
/*@
|
||||
unbind <f> Unbinds all callbacks from an event
|
||||
To unbind a specific handler, use namespaced events, like
|
||||
`bind('click.foo', callback)`, and then `unbind('click.foo')`.
|
||||
() -> <o> The event handler
|
||||
Unbinds all events
|
||||
(event) -> <o> The event handler
|
||||
Unbinds one event
|
||||
(event, event, ...) -> <o> The event handler
|
||||
Unbinds multiple events
|
||||
([event, event, ...]) -> <o> The event handler
|
||||
Unbinds multiple events
|
||||
event <s> Event name
|
||||
@*/
|
||||
*/
|
||||
that.unbind = function() {
|
||||
if (arguments.length == 0) {
|
||||
self.$eventHandler.off();
|
||||
} else {
|
||||
Ox.toArray(arguments).forEach(function(event) {
|
||||
self.$eventHandler.off('ox_' + event);
|
||||
var args = Ox.slice(arguments), self;
|
||||
if (Ox.isFunction(args[0])) {
|
||||
eventHandlers.forEach(function(handler) {
|
||||
handler === args[0] && eventHandlers.splice(i, 1);
|
||||
});
|
||||
} else if ((self = args.shift()).eventHandlers) {
|
||||
if (args.length == 0) {
|
||||
delete self.eventHandlers;
|
||||
} else {
|
||||
if (Ox.isFunction(args[0])) {
|
||||
args = {'*': args[0]};
|
||||
}
|
||||
Ox.forEach(Ox.makeObject(args), function(unbind, event) {
|
||||
if (Ox.isUndefined(unbind)) {
|
||||
delete self.eventHandlers[event];
|
||||
} else {
|
||||
self.eventHandlers[event].forEach(function(handler, i) {
|
||||
handler === unbind && self.eventHandlers[event].splice(i, 1);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return that;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue