Skip to content

Improve event dispatcher + add event trigger #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Same as https://github.com/jakiestfu/Snap.js + improve event dispatcher to allow the "on" method to register multiple events a la jQuery (e.g, snap.on('open close', function(event, trigger) {}) will listen to both the "open" and "close" events) + added the trigger source ('button', 'tag', or 'drag') to the event callback. All the modification all backward compatible.

# Snap.js
A Library for creating beautiful mobile shelfs in Javascript

Expand Down
33 changes: 26 additions & 7 deletions snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@
},
dispatchEvent: function(type) {
if (typeof eventList[type] === 'function') {
return eventList[type].call();
return eventList[type].call(type);
}
// Event namespaces
var namespace = type.indexOf('.');
if (typeof eventList[type.substr(0, namespace)] === 'function') {
return eventList[type.substr(0, namespace)](type.substr(0, namespace),
type.substr(namespace + 1));
}
},
vendor: function(){
Expand Down Expand Up @@ -420,7 +426,7 @@

// Tap Close
if (cache.dragWatchers.current === 0 && translated !== 0 && settings.tapToClose) {
utils.dispatchEvent('close');
utils.dispatchEvent('close.tap');
utils.events.prevent(e);
action.translate.easeTo(0);
cache.isDragging = false;
Expand All @@ -433,11 +439,16 @@
// Halfway, Flicking, or Too Far Out
if ((cache.simpleStates.halfway || cache.simpleStates.hyperExtending || cache.simpleStates.flick)) {
if (cache.simpleStates.flick && cache.simpleStates.towards === 'left') { // Flicking Closed
utils.dispatchEvent('close.drag');
action.translate.easeTo(0);
} else if (
(cache.simpleStates.flick && cache.simpleStates.towards === 'right') || // Flicking Open OR
(cache.simpleStates.halfway || cache.simpleStates.hyperExtending) // At least halfway open OR hyperextending
) {
// Only throw an event in the drawer was previously closed
if (cache.translation === 0) {
utils.dispatchEvent('open.drag');
}
action.translate.easeTo(settings.maxPosition); // Open Left
}
} else {
Expand All @@ -448,11 +459,13 @@
// Halfway, Flicking, or Too Far Out
if ((cache.simpleStates.halfway || cache.simpleStates.hyperExtending || cache.simpleStates.flick)) {
if (cache.simpleStates.flick && cache.simpleStates.towards === 'right') { // Flicking Closed
utils.dispatchEvent('close.drag');
action.translate.easeTo(0);
} else if (
(cache.simpleStates.flick && cache.simpleStates.towards === 'left') || // Flicking Open OR
(cache.simpleStates.halfway || cache.simpleStates.hyperExtending) // At least halfway open OR hyperextending
) {
utils.dispatchEvent('open.drag');
action.translate.easeTo(settings.minPosition); // Open Right
}
} else {
Expand All @@ -476,7 +489,7 @@
* Public
*/
this.open = function(side) {
utils.dispatchEvent('open');
utils.dispatchEvent('open.button');
utils.klass.remove(doc.body, 'snapjs-expand-left');
utils.klass.remove(doc.body, 'snapjs-expand-right');

Expand All @@ -495,7 +508,7 @@
}
};
this.close = function() {
utils.dispatchEvent('close');
utils.dispatchEvent('close.button');
action.translate.easeTo(0);
};
this.expand = function(side){
Expand All @@ -515,12 +528,18 @@
};

this.on = function(evt, fn) {
eventList[evt] = fn;
var events = evt.split(' ');
for (var i = 0; i < events.length; i++) {
eventList[events[i]] = fn;
}
return this;
};
this.off = function(evt) {
if (eventList[evt]) {
eventList[evt] = false;
var events = evt.split(' ');
for (var i = 0; i < events.length; i++) {
if (eventList[evt]) {
eventList[evt] = false;
}
}
};

Expand Down