Skip to content

Commit

Permalink
plugin accepts callback as an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
GerManson committed Jun 26, 2011
1 parent 0814c80 commit 8ace22c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 59 deletions.
4 changes: 3 additions & 1 deletion js/application.js
@@ -1,5 +1,7 @@
$(document).ready(function() {

$(this).gShake();
$(this).gShake(function() {
alert("function loaded from document")
});

});
127 changes: 69 additions & 58 deletions lib/gShake.js
Expand Up @@ -2,90 +2,101 @@

var settings = {
'sensibility': 20,
'timeout': 0
'timeout': 0,
callback: null,
};

var coords = {
'x': null,
'y': null,
'z': null
};

var lastTime = new Date();

var methods = {
init: function(options) {
alert("plugin started");
//alert(methods.test());

return this.each(function() {
// If options exist, lets merge them
// with our default settings
if (options) {
$.extend(settings, options);
}
// If options exist, lets merge them
// with our default settings
if (options) {
$.extend(settings, options);
}

if (('ondevicemotion' in window)) {
window.addEventListener('devicemotion', methods.devicemotion, false);
if (('ondevicemotion' in window)) {
window.addEventListener('devicemotion', methods.devicemotion, false);
}
});
},

initWithCallback: function(callback) {
settings.callback = callback;
methods.init.apply(this, arguments);
},

shakeEventDidOcurr: function() {
if (typeof settings.callback === 'function') {
settings.callback();
}
});
},

shakeEventDidOcurr: function() {
alert ("shaked!");
},
else {
alert("shaked detected!");
}
},

stop: function() {
if (('ondevicemotion' in window)) {
window.removeEventListener('devicemotion', this, false);
}
stop: function() {
if (('ondevicemotion' in window)) {
window.removeEventListener('devicemotion', this, false);
}

lastTime = new Date();
coords.x = null;
coords.y = null;
coords.z = null;
},
lastTime = new Date();
coords.x = null;
coords.y = null;
coords.z = null;
},

devicemotion: function(e) {
var current = e.accelerationIncludingGravity;
devicemotion: function(e) {
var current = e.accelerationIncludingGravity;

if((coords.x !== null) || (coords.y !== null) || (coords.z !== null)) {
if((coords.x !== null) || (coords.y !== null) || (coords.z !== null)) {

var deltaX = Math.abs(coords.x - current.x),
deltaY = Math.abs(coords.y - current.y),
deltaZ = Math.abs(coords.z - current.z);
var deltaX = Math.abs(coords.x - current.x),
deltaY = Math.abs(coords.y - current.y),
deltaZ = Math.abs(coords.z - current.z);

if(((deltaX > settings.sensibility) && (deltaY > settings.sensibility)) ||
((deltaX > settings.sensibility) && (deltaZ > settings.sensibility)) ||
((deltaY > settings.sensibility) && (deltaZ > settings.sensibility))) {
if(((deltaX > settings.sensibility) && (deltaY > settings.sensibility)) ||
((deltaX > settings.sensibility) && (deltaZ > settings.sensibility)) ||
((deltaY > settings.sensibility) && (deltaZ > settings.sensibility))) {

//calculate time in milliseconds since last shake registered
var currentTime = new Date(),
timeDifference = currentTime.getTime() - lastTime.getTime();
//calculate time in milliseconds since last shake registered
var currentTime = new Date(),
timeDifference = currentTime.getTime() - lastTime.getTime();

if (timeDifference > 300) {
methods.shakeEventDidOcurr();
lastTime = new Date();
}
}
}
if (timeDifference > 300) {
methods.shakeEventDidOcurr();
lastTime = new Date();
}
}
}

coords.x = current.x;
coords.y = current.y;
coords.z = current.z;
}
coords.x = current.x;
coords.y = current.y;
coords.z = current.z;
}
};
// end methods

$.fn.gShake = function(method) {

if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
}
else {
$.error('Method ' + method + ' does not exist on jQuery.gShake');
}

if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else if (typeof method === 'function') {
return methods.initWithCallback.apply(this, arguments);
}
else {
$.error('Method ' + method + ' does not exist on jQuery.gShake');
}

};
})(jQuery);

0 comments on commit 8ace22c

Please sign in to comment.