Ox.$: normalize mousewheel event names

This commit is contained in:
rolux 2013-12-04 23:43:07 +01:00
parent 6cb7269ce1
commit 9ea8e0352d

View file

@ -28,11 +28,30 @@ Ox.$ <f> Generic HTML element, mimics jQuery
true
@*/
Ox.$ = Ox.element = function(value) {
var element = !Ox.isString(value) ? value // window, document or element
: value[0] == '<' ? document.createElement(value.slice(1, -1))
: value[0] == '#' ? document.getElementById(value.slice(1))
: value[0] == '.' ? document.getElementsByClassName(value.slice(1))[0]
: document.getElementsByTagName(value)[0];
: value[0] == '<' ? document.createElement(value.slice(1, -1))
: value[0] == '#' ? document.getElementById(value.slice(1))
: value[0] == '.' ? document.getElementsByClassName(value.slice(1))[0]
: document.getElementsByTagName(value)[0],
mousewheelEvents = ['wheel', 'mousewheel'],
originalMousewheelEvents = 'onwheel' in document ? ['wheel']
: ['mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'];
function normalizeEvents(args) {
var ret = {};
Ox.forEach(Ox.makeObject(args), function(callback, event) {
if (Ox.contains(mousewheelEvents, event)) {
originalMousewheelEvents.forEach(function(event) {
ret[event] = callback;
});
} else {
ret[event] = callback;
}
});
return ret;
}
return element ? {
//@ 0 <h> The DOM element itself
0: element,
@ -225,7 +244,7 @@ Ox.$ = Ox.element = function(value) {
@*/
off: function(event, callback) {
var that = this;
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
Ox.forEach(normalizeEvents(arguments), function(callback, event) {
if (callback) {
that[0].removeEventListener(event, callback, false);
} else {
@ -244,7 +263,7 @@ Ox.$ = Ox.element = function(value) {
@*/
on: function() {
var that = this;
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
Ox.forEach(normalizeEvents(arguments), function(callback, event) {
that[0].addEventListener(event, callback, false);
});
return this;
@ -260,7 +279,7 @@ Ox.$ = Ox.element = function(value) {
one: function(events) {
var args = Ox.slice(arguments),
that = this;
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
Ox.forEach(normalizeEvents(arguments), function(callback, event) {
that.on(event, function fn() {
that.off(event, fn);
return callback.apply(that, args);
@ -422,6 +441,7 @@ Ox.$ = Ox.element = function(value) {
return this[0].offsetWidth;
}
} : null;
};
/*@