Skip to content

Commit

Permalink
Adds DOM event fns on() off() trigger()
Browse files Browse the repository at this point in the history
  • Loading branch information
stephband committed Oct 27, 2016
1 parent 80071a7 commit ad6d799
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/js/jquery-*
.testswarm
.testswarm
archived/
120 changes: 85 additions & 35 deletions js/jquery.event.move.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// jquery.event.move
//
// 1.3.6
// 1.3.7
//
// Stephen Band
//
Expand Down Expand Up @@ -32,23 +32,13 @@
}
})(function(jQuery, undefined){

var // Number of pixels a pressed pointer travels before movestart
// event is fired.
threshold = 6,

add = jQuery.event.add,

remove = jQuery.event.remove,

// Just sugar, so we can have arguments in the same order as
// add and remove.
trigger = function(node, type, data) {
jQuery.event.trigger(type, data, node);
},
// Number of pixels a pressed pointer travels before movestart
// event is fired.
var threshold = 6;

// Shim for requestAnimationFrame, falling back to timer. See:
// see http://paulirish.com/2011/requestanimationframe-for-smart-animating/
requestFrame = (function(){
// Shim for requestAnimationFrame, falling back to timer. See:
// see http://paulirish.com/2011/requestanimationframe-for-smart-animating/
var requestFrame = (function(){
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
Expand All @@ -61,27 +51,82 @@
}, 25);
}
);
})(),
ignoreTags = {
})();

var ignoreTags = {
textarea: true,
input: true,
select: true,
button: true
},
mouseevents = {
move: 'mousemove',
};

var mouseevents = {
move: 'mousemove',
cancel: 'mouseup dragstart',
end: 'mouseup'
},
touchevents = {
move: 'touchmove',
end: 'mouseup'
};

var touchevents = {
move: 'touchmove',
cancel: 'touchend',
end: 'touchend'
end: 'touchend'
};

var rspaces = /\s+/;


// DOM Events

var eventOptions = { bubbles: true };

var eventsSymbol = Symbol();

function createEvent(type) {
return new CustomEvent(type, eventOptions);
}

function getEvents(node) {
return node[eventsSymbol] || (node[eventsSymbol] = {});
}

function on(node, types, fn, data, selector) {
types = types.split(rspaces);

var events = getEvents(node);
var handlers, type;

function handler(e) { fn(e, data); }

for (type of types) {
handlers = events[type] || (events[type] = []);
handlers.push([fn, handler]);
node.addEventListener(type, handler);
}
}

function off(node, types, fn, selector) {
types = types.split(rspaces);

var type, handlers, i;

for (type of types) {
handlers = events[type] || (events[type] = []);
i = handlers.length;
while (i--) {
if (handlers[i][0] === fn) {
node.removeEventListener(type, handlers[i][1]);
}
}
}
}

function trigger(node, type) {
// Don't cache events. It prevents you from triggering an event of a
// given type from inside the handler of another event of that type.
var event = createEvent(type);
node.dispatchEvent();
}


// Constructors

Expand Down Expand Up @@ -131,7 +176,9 @@


// Functions


function noop() {}

function returnTrue() {
return true;
}
Expand Down Expand Up @@ -307,6 +354,9 @@
// Create a movestart object with some special properties that
// are passed only to the movestart handlers.
template.type = 'movestart';
template.altKey = e.altKey;
template.ctrlKey = e.ctrlKey;
template.shiftKey = e.shiftKey;
template.distX = distX;
template.distY = distY;
template.deltaX = distX;
Expand Down Expand Up @@ -544,23 +594,23 @@
setup: function() {
// Bind a noop to movestart. Why? It's the movestart
// setup that decides whether other move events are fired.
add(this, 'movestart.move', jQuery.noop);
add(this, 'movestart.move', noop);
},

teardown: function() {
remove(this, 'movestart.move', jQuery.noop);
remove(this, 'movestart.move', noop);
}
};

jQuery.event.special.moveend = {
setup: function() {
// Bind a noop to movestart. Why? It's the movestart
// setup that decides whether other move events are fired.
add(this, 'movestart.moveend', jQuery.noop);
add(this, 'movestart.moveend', noop);
},

teardown: function() {
remove(this, 'movestart.moveend', jQuery.noop);
remove(this, 'movestart.moveend', noop);
}
};

Expand Down

0 comments on commit ad6d799

Please sign in to comment.