diff --git a/js/application.js b/js/application.js index 03c5509..917692f 100644 --- a/js/application.js +++ b/js/application.js @@ -1,5 +1,7 @@ $(document).ready(function() { - $(this).gShake(); + $(this).gShake(function() { + alert("function loaded from document") + }); }); \ No newline at end of file diff --git a/lib/gShake.js b/lib/gShake.js index deac64d..1a5e85e 100644 --- a/lib/gShake.js +++ b/lib/gShake.js @@ -2,7 +2,8 @@ var settings = { 'sensibility': 20, - 'timeout': 0 + 'timeout': 0, + callback: null, }; var coords = { @@ -10,82 +11,92 @@ '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); \ No newline at end of file