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';
|
'use strict';
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
Ox.Event <o> Basic event handler
|
Ox.Event <o> Used internally for events
|
||||||
@*/
|
@*/
|
||||||
|
|
||||||
// FIXME: unused
|
|
||||||
|
|
||||||
Ox.Event = (function() {
|
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
|
.bind <f> Adds an event handler
|
||||||
(event, callback) -> <o> The event handler
|
(callback) -> <o> Ox.Event
|
||||||
({event: callback, ...}) -> <o> The event handler
|
(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
|
callback <f> Callback function
|
||||||
data <*> Event data
|
data <o> Event data
|
||||||
|
event <s> Event name
|
||||||
|
element <o> Element
|
||||||
event <s> Event name
|
event <s> Event name
|
||||||
Event names can be namespaced, like `'click.foo'`
|
Event names can be namespaced, like `'click.foo'`
|
||||||
@*/
|
*/
|
||||||
that.bind = function(self) {
|
that.bind = function() {
|
||||||
var args = Ox.slice(arguments, 1), once;
|
var args = Ox.toArray(arguments), once, self;
|
||||||
if (Ox.isBoolean(Ox.last(args))) {
|
if (args.length == 1) {
|
||||||
once = args.pop();
|
eventHandlers.push(args[0])
|
||||||
}
|
} else {
|
||||||
if (Ox.isFunction(args[0])) {
|
self = args.shift();
|
||||||
args = {'': args[0]};
|
once = Ox.isBoolean(Ox.last(args)) ? args.pop() : false;
|
||||||
}
|
args = Ox.isFunction(args[0]) ? {'*': args[0]} : Ox.makeObject(args);
|
||||||
Ox.forEach(Ox.makeObject(args), function(callback, event) {
|
if (Ox.len(args) && !self.eventHandlers) {
|
||||||
if (once) {
|
self.eventHandlers = {};
|
||||||
callback.once = true;
|
|
||||||
}
|
}
|
||||||
self.eventHandlers = self.eventHandlers || {};
|
Ox.forEach(args, function(handler, event) {
|
||||||
self.eventHandlers[event] = (
|
handler.once = once;
|
||||||
self.eventHandlers[event] || []
|
self.eventHandlers[event] = (
|
||||||
).concat(callback);
|
self.eventHandlers[event] || []
|
||||||
});
|
).concat(handler);
|
||||||
|
});
|
||||||
|
}
|
||||||
return that;
|
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() {
|
that.bindOnce = function() {
|
||||||
return that.bind.apply(null, Ox.slice(arguments).concat(true));
|
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) {
|
that.trigger = function(self) {
|
||||||
if (!self.eventHandlers) {
|
var element = this;
|
||||||
return;
|
if (self.eventHandlers) {
|
||||||
}
|
Ox.forEach(Ox.makeObject(
|
||||||
Ox.forEach(Ox.makeObject(
|
Ox.slice(arguments, 1)
|
||||||
Ox.slice(arguments, 1, -1)
|
), function(data, event) {
|
||||||
), function(data, event) {
|
var triggered = event.split('.');
|
||||||
triggered = event.split('.');
|
triggered.map(function(v, i) {
|
||||||
triggered.map(function(v, i) {
|
return triggered.slice(0, i + 1).join('.');
|
||||||
return triggered.slice(0, i + 1).join('.');
|
}).concat('*').forEach(function(triggered) {
|
||||||
}).forEach(function(triggered) {
|
var handlers = self.eventHandlers[triggered];
|
||||||
Ox.forEach(
|
handlers && handlers.forEach(function(handler) {
|
||||||
Ox.extend(
|
|
||||||
{'': self.eventHandlers[''] || []},
|
|
||||||
triggered, self.eventHandlers[triggered] || []
|
|
||||||
),
|
|
||||||
function(handlers, handled) {
|
|
||||||
handler.once && that.unbind(self, triggered, handler);
|
handler.once && that.unbind(self, triggered, handler);
|
||||||
handler(data || {}, event, Ox.last(arguments).oxid);
|
handler.call(element, data || {}, event);
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
eventHandlers.forEach(function(handler) {
|
||||||
|
handler.call(element, data || {}, event, element);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return that;
|
return that;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
bindOnce <f> Binds a callback to an event, once
|
.unbind <f> Removes an event handler
|
||||||
(event, callback) -> <o> The event handler
|
(callback) -> <o> Ox.Event
|
||||||
({event: callback, ...}) -> <o> The event handler
|
(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
|
callback <f> Callback function
|
||||||
data <*> Event data
|
|
||||||
event <s> Event name
|
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() {
|
that.unbind = function() {
|
||||||
if (arguments.length == 0) {
|
var args = Ox.slice(arguments), self;
|
||||||
self.$eventHandler.off();
|
if (Ox.isFunction(args[0])) {
|
||||||
} else {
|
eventHandlers.forEach(function(handler) {
|
||||||
Ox.toArray(arguments).forEach(function(event) {
|
handler === args[0] && eventHandlers.splice(i, 1);
|
||||||
self.$eventHandler.off('ox_' + event);
|
|
||||||
});
|
});
|
||||||
|
} 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;
|
return that;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue