From e43845784d48c840d02fe95c76636ef415cba085 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Mon, 26 May 2014 14:13:04 -0500 Subject: [PATCH 01/34] Converting all tabs to spaces Indentation should be consistent. The dominant usage is two spaces, so I've converted all tab characters to spaces. --- google.fastbutton.js | 60 ++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index 6cfc1bf..7bb5135 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -1,49 +1,49 @@ (function() { - /** + /** * From: http://code.this.com/mobile/articles/fast_buttons.html - * Also see: http://stackoverflow.com/questions/6300136/trying-to-implement-googles-fast-button + * Also see: http://stackoverflow.com/questions/6300136/trying-to-implement-googles-fast-button */ - + /** For IE8 and earlier compatibility: https://developer.mozilla.org/en/DOM/element.addEventListener */ function addListener(el, type, listener, useCapture) { - if (el.addEventListener) { + if (el.addEventListener) { el.addEventListener(type, listener, useCapture); - return { - destroy: function() { el.removeEventListener(type, listener, useCapture); } + return { + destroy: function() { el.removeEventListener(type, listener, useCapture); } }; - } else { + } else { // see: http://stackoverflow.com/questions/5198845/javascript-this-losing-context-in-ie var handler = function(e) { listener.handleEvent(window.event, listener); } el.attachEvent('on' + type, handler); - - return { + + return { destroy: function() { el.detachEvent('on' + type, handler); } }; - } + } } - + var isTouch = "ontouchstart" in window; /* Construct the FastButton with a reference to the element and click handler. */ this.FastButton = function(element, handler, useCapture) { - // collect functions to call to cleanup events + // collect functions to call to cleanup events this.events = []; this.touchEvents = []; this.element = element; this.handler = handler; this.useCapture = useCapture; - if (isTouch) - this.events.push(addListener(element, 'touchstart', this, this.useCapture)); + if (isTouch) + this.events.push(addListener(element, 'touchstart', this, this.useCapture)); this.events.push(addListener(element, 'click', this, this.useCapture)); }; - + /* Remove event handling when no longer needed for this button */ this.FastButton.prototype.destroy = function() { for (i = this.events.length - 1; i >= 0; i -= 1) - this.events[i].destroy(); + this.events[i].destroy(); this.events = this.touchEvents = this.element = this.handler = this.fastButton = null; }; - + /* acts as an event dispatcher */ this.FastButton.prototype.handleEvent = function(event) { switch (event.type) { @@ -53,7 +53,7 @@ case 'click': this.onClick(event); break; } }; - + /* Save a reference to the touchstart coordinate and start listening to touchmove and touchend events. Calling stopPropagation guarantees that other behaviors don’t get a chance to handle the same click event. This is executed at the beginning of touch. */ @@ -64,44 +64,44 @@ this.startX = event.touches[0].clientX; this.startY = event.touches[0].clientY; }; - + /* When /if touchmove event is invoked, check if the user has dragged past the threshold of 10px. */ this.FastButton.prototype.onTouchMove = function(event) { if (Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) { this.reset(); //if he did, then cancel the touch event } }; - + /* Invoke the actual click handler and prevent ghost clicks if this was a touchend event. */ this.FastButton.prototype.onClick = function(event) { event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true); this.reset(); // Use .call to call the method so that we have the correct "this": https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call var result = this.handler.call(this.element, event); - if (event.type == 'touchend') - clickbuster.preventGhostClick(this.startX, this.startY); + if (event.type == 'touchend') + clickbuster.preventGhostClick(this.startX, this.startY); return result; }; - + this.FastButton.prototype.reset = function() { - for (i = this.touchEvents.length - 1; i >= 0; i -= 1) - this.touchEvents[i].destroy(); + for (i = this.touchEvents.length - 1; i >= 0; i -= 1) + this.touchEvents[i].destroy(); this.touchEvents = []; }; - + this.clickbuster = function() {} - + /* Call preventGhostClick to bust all click events that happen within 25px of the provided x, y coordinates in the next 2.5s. */ this.clickbuster.preventGhostClick = function(x, y) { clickbuster.coordinates.push(x, y); window.setTimeout(clickbuster.pop, 2500); }; - + this.clickbuster.pop = function() { clickbuster.coordinates.splice(0, 2); }; - + /* If we catch a click event inside the given radius and time threshold then we call stopPropagation and preventDefault. Calling preventDefault will stop links from being activated. */ @@ -115,7 +115,7 @@ } } }; - + if (isTouch) { // Don't need to use our custom addListener function since we only bust clicks on touch devices document.addEventListener('click', clickbuster.onClick, true); From a5ba6abdf34f932a4250f7b1e68f253a4679e91f Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Mon, 26 May 2014 14:16:16 -0500 Subject: [PATCH 02/34] Adding 'use strict' Strict mode ensures consistent behavior with newer browsers. Its useful in development to keep issues in check. Compilers/minifiers will remove it eventually for production. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode --- google.fastbutton.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/google.fastbutton.js b/google.fastbutton.js index 7bb5135..f428f18 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -1,4 +1,6 @@ (function() { + 'use strict'; + /** * From: http://code.this.com/mobile/articles/fast_buttons.html * Also see: http://stackoverflow.com/questions/6300136/trying-to-implement-googles-fast-button From 596d9af815cbb06341a6752dded1510e1f425a3f Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Mon, 26 May 2014 14:19:30 -0500 Subject: [PATCH 03/34] Reformatting comments I've reformatted the comments to a simpler, consistent format for use with Docco to create a simple documentation. --- google.fastbutton.js | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index f428f18..2d56820 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -1,12 +1,13 @@ (function() { 'use strict'; - /** - * From: http://code.this.com/mobile/articles/fast_buttons.html - * Also see: http://stackoverflow.com/questions/6300136/trying-to-implement-googles-fast-button - */ + // From: + // http://code.this.com/mobile/articles/fast_buttons.html + // + // Also see: + // http://stackoverflow.com/questions/6300136/trying-to-implement-googles-fast-button - /** For IE8 and earlier compatibility: https://developer.mozilla.org/en/DOM/element.addEventListener */ + // For IE8 and earlier compatibility: https://developer.mozilla.org/en/DOM/element.addEventListener function addListener(el, type, listener, useCapture) { if (el.addEventListener) { el.addEventListener(type, listener, useCapture); @@ -26,7 +27,7 @@ var isTouch = "ontouchstart" in window; - /* Construct the FastButton with a reference to the element and click handler. */ + // Construct the FastButton with a reference to the element and click handler. this.FastButton = function(element, handler, useCapture) { // collect functions to call to cleanup events this.events = []; @@ -39,14 +40,14 @@ this.events.push(addListener(element, 'click', this, this.useCapture)); }; - /* Remove event handling when no longer needed for this button */ + // Remove event handling when no longer needed for this button this.FastButton.prototype.destroy = function() { for (i = this.events.length - 1; i >= 0; i -= 1) this.events[i].destroy(); this.events = this.touchEvents = this.element = this.handler = this.fastButton = null; }; - /* acts as an event dispatcher */ + // acts as an event dispatcher this.FastButton.prototype.handleEvent = function(event) { switch (event.type) { case 'touchstart': this.onTouchStart(event); break; @@ -56,9 +57,10 @@ } }; - /* Save a reference to the touchstart coordinate and start listening to touchmove and - touchend events. Calling stopPropagation guarantees that other behaviors don’t get a - chance to handle the same click event. This is executed at the beginning of touch. */ + // Save a reference to the touchstart coordinate and start listening + // to touchmove and touchend events. Calling stopPropagation guarantees + // that other behaviors don’t get a chance to handle the same click event. + // This is executed at the beginning of touch. this.FastButton.prototype.onTouchStart = function(event) { event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true); this.touchEvents.push(addListener(this.element, 'touchend', this, this.useCapture)); @@ -67,14 +69,16 @@ this.startY = event.touches[0].clientY; }; - /* When /if touchmove event is invoked, check if the user has dragged past the threshold of 10px. */ + // When/if touchmove event is invoked, check if the user has dragged past + // the threshold of 10px. this.FastButton.prototype.onTouchMove = function(event) { if (Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) { this.reset(); //if he did, then cancel the touch event } }; - /* Invoke the actual click handler and prevent ghost clicks if this was a touchend event. */ + // Invoke the actual click handler and prevent ghost clicks if + // this was a touchend event. this.FastButton.prototype.onClick = function(event) { event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true); this.reset(); @@ -93,8 +97,8 @@ this.clickbuster = function() {} - /* Call preventGhostClick to bust all click events that happen within 25px of - the provided x, y coordinates in the next 2.5s. */ + // Call preventGhostClick to bust all click events that happen within + // 25px of the provided x, y coordinates in the next 2.5s. this.clickbuster.preventGhostClick = function(x, y) { clickbuster.coordinates.push(x, y); window.setTimeout(clickbuster.pop, 2500); @@ -104,9 +108,9 @@ clickbuster.coordinates.splice(0, 2); }; - /* If we catch a click event inside the given radius and time threshold then we call - stopPropagation and preventDefault. Calling preventDefault will stop links - from being activated. */ + // If we catch a click event inside the given radius and time threshold + // then we call stopPropagation and preventDefault. Calling preventDefault + // will stop links from being activated. this.clickbuster.onClick = function(event) { for (var i = 0; i < clickbuster.coordinates.length; i += 2) { var x = clickbuster.coordinates[i]; From 9ca7e4e022fbd5469968c0ea16bf070a20b4e4b7 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:10:33 -0500 Subject: [PATCH 04/34] Consistent quote usage --- google.fastbutton.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index 2d56820..c3d50c8 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -25,7 +25,7 @@ } } - var isTouch = "ontouchstart" in window; + var isTouch = 'ontouchstart' in window; // Construct the FastButton with a reference to the element and click handler. this.FastButton = function(element, handler, useCapture) { From 604496ea3fd488f04706dcf94a1e0fa97f08d4ed Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:11:20 -0500 Subject: [PATCH 05/34] Fixing comment length --- google.fastbutton.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index c3d50c8..b3bdba5 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -7,7 +7,8 @@ // Also see: // http://stackoverflow.com/questions/6300136/trying-to-implement-googles-fast-button - // For IE8 and earlier compatibility: https://developer.mozilla.org/en/DOM/element.addEventListener + // For IE8 and earlier compatibility: + // https://developer.mozilla.org/en/DOM/element.addEventListener function addListener(el, type, listener, useCapture) { if (el.addEventListener) { el.addEventListener(type, listener, useCapture); @@ -15,7 +16,8 @@ destroy: function() { el.removeEventListener(type, listener, useCapture); } }; } else { - // see: http://stackoverflow.com/questions/5198845/javascript-this-losing-context-in-ie + // see: + // http://stackoverflow.com/questions/5198845/javascript-this-losing-context-in-ie var handler = function(e) { listener.handleEvent(window.event, listener); } el.attachEvent('on' + type, handler); @@ -123,7 +125,8 @@ }; if (isTouch) { - // Don't need to use our custom addListener function since we only bust clicks on touch devices + // Don't need to use our custom addListener function + // since we only bust clicks on touch devices document.addEventListener('click', clickbuster.onClick, true); clickbuster.coordinates = []; } From aed77db9be00ad532e7ac75511a32f838a39e7fe Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:13:08 -0500 Subject: [PATCH 06/34] Simplifying handler --- google.fastbutton.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index b3bdba5..a9efdd8 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -18,7 +18,10 @@ } else { // see: // http://stackoverflow.com/questions/5198845/javascript-this-losing-context-in-ie - var handler = function(e) { listener.handleEvent(window.event, listener); } + var handler = function() { + listener.handleEvent(window.event, listener); + }; + el.attachEvent('on' + type, handler); return { From bee119bb05a29fc82b50046a2af569f3948d418b Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:13:55 -0500 Subject: [PATCH 07/34] Avoiding inline control structures --- google.fastbutton.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index a9efdd8..292a786 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -40,15 +40,17 @@ this.element = element; this.handler = handler; this.useCapture = useCapture; - if (isTouch) + if (isTouch) { this.events.push(addListener(element, 'touchstart', this, this.useCapture)); + } this.events.push(addListener(element, 'click', this, this.useCapture)); }; // Remove event handling when no longer needed for this button this.FastButton.prototype.destroy = function() { - for (i = this.events.length - 1; i >= 0; i -= 1) + for (i = this.events.length - 1; i >= 0; i -= 1) { this.events[i].destroy(); + } this.events = this.touchEvents = this.element = this.handler = this.fastButton = null; }; @@ -89,14 +91,16 @@ this.reset(); // Use .call to call the method so that we have the correct "this": https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call var result = this.handler.call(this.element, event); - if (event.type == 'touchend') + if (event.type == 'touchend') { clickbuster.preventGhostClick(this.startX, this.startY); + } return result; }; this.FastButton.prototype.reset = function() { - for (i = this.touchEvents.length - 1; i >= 0; i -= 1) + for (i = this.touchEvents.length - 1; i >= 0; i -= 1) { this.touchEvents[i].destroy(); + } this.touchEvents = []; }; From d795d99c2d1cc39896150c1b078ca30b02b19c8c Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:15:21 -0500 Subject: [PATCH 08/34] Fixing undeclared variables --- google.fastbutton.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index 292a786..f3efa55 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -48,7 +48,7 @@ // Remove event handling when no longer needed for this button this.FastButton.prototype.destroy = function() { - for (i = this.events.length - 1; i >= 0; i -= 1) { + for (var i = this.events.length - 1; i >= 0; i -= 1) { this.events[i].destroy(); } this.events = this.touchEvents = this.element = this.handler = this.fastButton = null; @@ -98,7 +98,7 @@ }; this.FastButton.prototype.reset = function() { - for (i = this.touchEvents.length - 1; i >= 0; i -= 1) { + for (var i = this.touchEvents.length - 1; i >= 0; i -= 1) { this.touchEvents[i].destroy(); } this.touchEvents = []; From 2e6e8356441a5e61586d30de7b18b347b5319a7f Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:16:33 -0500 Subject: [PATCH 09/34] Changing ternary to proper conditional --- google.fastbutton.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index f3efa55..b0dafbd 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -69,9 +69,17 @@ // that other behaviors don’t get a chance to handle the same click event. // This is executed at the beginning of touch. this.FastButton.prototype.onTouchStart = function(event) { - event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true); - this.touchEvents.push(addListener(this.element, 'touchend', this, this.useCapture)); - this.touchEvents.push(addListener(document.body, 'touchmove', this, this.useCapture)); + if (event.stopPropagation) { + event.stopPropagation(); + } else { + event.cancelBubble = true; + } + this.touchEvents.push( + addListener(this.element, 'touchend', this, this.useCapture) + ); + this.touchEvents.push( + addListener(document.body, 'touchmove', this, this.useCapture) + ); this.startX = event.touches[0].clientX; this.startY = event.touches[0].clientY; }; @@ -87,7 +95,11 @@ // Invoke the actual click handler and prevent ghost clicks if // this was a touchend event. this.FastButton.prototype.onClick = function(event) { - event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true); + if (event.stopPropagation) { + event.stopPropagation(); + } else { + event.cancelBubble = true; + } this.reset(); // Use .call to call the method so that we have the correct "this": https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call var result = this.handler.call(this.element, event); From 54c9dc0440e4a206d93d5f5012518c51bc3d5a85 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:19:11 -0500 Subject: [PATCH 10/34] Fixing line length Lines should ideally be less than 85 characters --- google.fastbutton.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index b0dafbd..1328e21 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -87,7 +87,8 @@ // When/if touchmove event is invoked, check if the user has dragged past // the threshold of 10px. this.FastButton.prototype.onTouchMove = function(event) { - if (Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) { + if (Math.abs(event.touches[0].clientX - this.startX) > 10 || + Math.abs(event.touches[0].clientY - this.startY) > 10) { this.reset(); //if he did, then cancel the touch event } }; @@ -101,7 +102,8 @@ event.cancelBubble = true; } this.reset(); - // Use .call to call the method so that we have the correct "this": https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call + // Use .call to call the method so that we have the correct "this": + // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call var result = this.handler.call(this.element, event); if (event.type == 'touchend') { clickbuster.preventGhostClick(this.startX, this.startY); From eeaf5ff8358f0f30606a62f44568aaeecb7f8563 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:19:59 -0500 Subject: [PATCH 11/34] Using strict equality for string comparison It doesn't matter immensely, but creates consistency with other comparisons. --- google.fastbutton.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index 1328e21..5d06bee 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -105,7 +105,7 @@ // Use .call to call the method so that we have the correct "this": // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call var result = this.handler.call(this.element, event); - if (event.type == 'touchend') { + if (event.type === 'touchend') { clickbuster.preventGhostClick(this.startX, this.startY); } return result; From ecb719644484c7aee9bd0c85394e68f454370966 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:26:47 -0500 Subject: [PATCH 12/34] Refactor click buster to be a proper object but scoped to closure --- google.fastbutton.js | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index 5d06bee..e73098a 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -118,29 +118,29 @@ this.touchEvents = []; }; - this.clickbuster = function() {} - - // Call preventGhostClick to bust all click events that happen within - // 25px of the provided x, y coordinates in the next 2.5s. - this.clickbuster.preventGhostClick = function(x, y) { - clickbuster.coordinates.push(x, y); - window.setTimeout(clickbuster.pop, 2500); - }; - - this.clickbuster.pop = function() { - clickbuster.coordinates.splice(0, 2); - }; - - // If we catch a click event inside the given radius and time threshold - // then we call stopPropagation and preventDefault. Calling preventDefault - // will stop links from being activated. - this.clickbuster.onClick = function(event) { - for (var i = 0; i < clickbuster.coordinates.length; i += 2) { - var x = clickbuster.coordinates[i]; - var y = clickbuster.coordinates[i + 1]; - if (Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) { - event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true); - event.preventDefault ? event.preventDefault() : (event.returnValue=false); + var clickbuster = { + // Call preventGhostClick to bust all click events that happen within + // 25px of the provided x, y coordinates in the next 2.5s. + preventGhostClick: function (x, y) { + clickbuster.coordinates.push(x, y); + window.setTimeout(clickbuster.pop, 2500); + }, + + pop: function () { + clickbuster.coordinates.splice(0, 2); + }, + + // If we catch a click event inside the given radius and time threshold + // then we call stopPropagation and preventDefault. Calling preventDefault + // will stop links from being activated. + onClick: function (event) { + for (var i = 0; i < clickbuster.coordinates.length; i += 2) { + var x = clickbuster.coordinates[i]; + var y = clickbuster.coordinates[i + 1]; + if (Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) { + event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true); + event.preventDefault ? event.preventDefault() : (event.returnValue=false); + } } } }; From 2abb0c66baf6501e921c5e7d537692d173188eaf Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 28 May 2014 13:28:22 -0500 Subject: [PATCH 13/34] Using conditional instead of ternary --- google.fastbutton.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index e73098a..3e84a4e 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -138,8 +138,16 @@ var x = clickbuster.coordinates[i]; var y = clickbuster.coordinates[i + 1]; if (Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) { - event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true); - event.preventDefault ? event.preventDefault() : (event.returnValue=false); + if (event.stopPropagation) { + event.stopPropagation(); + } else { + event.cancelBubble = true; + } + if (event.preventDefault) { + event.preventDefault(); + } else { + event.returnValue = false; + } } } } From da0bce469c6a36c24d9f2ec3ea57e18124bcc5de Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 13:34:01 -0500 Subject: [PATCH 14/34] Fixing case statement --- google.fastbutton.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index 3e84a4e..b483574 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -57,10 +57,18 @@ // acts as an event dispatcher this.FastButton.prototype.handleEvent = function(event) { switch (event.type) { - case 'touchstart': this.onTouchStart(event); break; - case 'touchmove': this.onTouchMove(event); break; - case 'touchend': this.onClick(event); break; - case 'click': this.onClick(event); break; + case 'touchstart': + this.onTouchStart(event); + break; + case 'touchmove': + this.onTouchMove(event); + break; + case 'touchend': + this.onClick(event); + break; + case 'click': + this.onClick(event); + break; } }; From fea29bbcb555669bae45b676d56079fc68595137 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 13:35:20 -0500 Subject: [PATCH 15/34] Line length --- google.fastbutton.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index b483574..c6679ff 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -13,7 +13,9 @@ if (el.addEventListener) { el.addEventListener(type, listener, useCapture); return { - destroy: function() { el.removeEventListener(type, listener, useCapture); } + destroy: function() { + el.removeEventListener(type, listener, useCapture); + } }; } else { // see: @@ -41,7 +43,9 @@ this.handler = handler; this.useCapture = useCapture; if (isTouch) { - this.events.push(addListener(element, 'touchstart', this, this.useCapture)); + this.events.push( + addListener(element, 'touchstart', this, this.useCapture) + ); } this.events.push(addListener(element, 'click', this, this.useCapture)); }; @@ -51,7 +55,11 @@ for (var i = this.events.length - 1; i >= 0; i -= 1) { this.events[i].destroy(); } - this.events = this.touchEvents = this.element = this.handler = this.fastButton = null; + this.events = + this.touchEvents = + this.element = + this.handler = + this.fastButton = null; }; // acts as an event dispatcher @@ -145,7 +153,8 @@ for (var i = 0; i < clickbuster.coordinates.length; i += 2) { var x = clickbuster.coordinates[i]; var y = clickbuster.coordinates[i + 1]; - if (Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) { + if (Math.abs(event.clientX - x) < 25 && + Math.abs(event.clientY - y) < 25) { if (event.stopPropagation) { event.stopPropagation(); } else { From 8cc90b563df43f308fea3b3fe5aee10bc731ed6a Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 13:45:18 -0500 Subject: [PATCH 16/34] Attaching fast click to window --- google.fastbutton.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index c6679ff..92302ee 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -35,7 +35,7 @@ var isTouch = 'ontouchstart' in window; // Construct the FastButton with a reference to the element and click handler. - this.FastButton = function(element, handler, useCapture) { + window.FastButton = function (element, handler, useCapture) { // collect functions to call to cleanup events this.events = []; this.touchEvents = []; @@ -51,19 +51,18 @@ }; // Remove event handling when no longer needed for this button - this.FastButton.prototype.destroy = function() { + window.FastButton.prototype.destroy = function() { for (var i = this.events.length - 1; i >= 0; i -= 1) { this.events[i].destroy(); } this.events = this.touchEvents = this.element = - this.handler = - this.fastButton = null; + this.handler = null; }; // acts as an event dispatcher - this.FastButton.prototype.handleEvent = function(event) { + window.FastButton.prototype.handleEvent = function(event) { switch (event.type) { case 'touchstart': this.onTouchStart(event); @@ -84,7 +83,7 @@ // to touchmove and touchend events. Calling stopPropagation guarantees // that other behaviors don’t get a chance to handle the same click event. // This is executed at the beginning of touch. - this.FastButton.prototype.onTouchStart = function(event) { + window.FastButton.prototype.onTouchStart = function(event) { if (event.stopPropagation) { event.stopPropagation(); } else { @@ -102,7 +101,7 @@ // When/if touchmove event is invoked, check if the user has dragged past // the threshold of 10px. - this.FastButton.prototype.onTouchMove = function(event) { + window.FastButton.prototype.onTouchMove = function(event) { if (Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) { this.reset(); //if he did, then cancel the touch event @@ -111,7 +110,7 @@ // Invoke the actual click handler and prevent ghost clicks if // this was a touchend event. - this.FastButton.prototype.onClick = function(event) { + window.FastButton.prototype.onClick = function(event) { if (event.stopPropagation) { event.stopPropagation(); } else { @@ -127,7 +126,7 @@ return result; }; - this.FastButton.prototype.reset = function() { + window.FastButton.prototype.reset = function() { for (var i = this.touchEvents.length - 1; i >= 0; i -= 1) { this.touchEvents[i].destroy(); } From ef2210f6b7fcb13955170b1bc806c7365dd05ffb Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 13:48:05 -0500 Subject: [PATCH 17/34] Fixing indentation on jQuery implementation --- jquery.google.fastbutton.js | 50 ++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/jquery.google.fastbutton.js b/jquery.google.fastbutton.js index e89bb52..5708a3c 100644 --- a/jquery.google.fastbutton.js +++ b/jquery.google.fastbutton.js @@ -1,30 +1,30 @@ (function($) { $.event.special.fastClick = { - setup: function () { - $(this).data('fastClick', new FastButton(this, $.event.special.fastClick.handler)); - }, - teardown: function () { - $(this).data('fastClick').destroy(); - $(this).removeData('fastClick'); - }, - handler: function (e) { - // convert native event to jquery event - e = $.event.fix(e); - e.type = 'fastClick'; - - /* - event.handle is deprecated and removed as of version 1.9 - use event.dispatch instead, - $.event.handle.apply(this, arguments); - */ - $.event.dispatch.apply(this, arguments); - } - }; + setup: function () { + $(this).data('fastClick', new FastButton(this, $.event.special.fastClick.handler)); + }, + teardown: function () { + $(this).data('fastClick').destroy(); + $(this).removeData('fastClick'); + }, + handler: function (e) { + // convert native event to jquery event + e = $.event.fix(e); + e.type = 'fastClick'; - $.fn.fastClick = function(fn) { - return $(this).each(function() { - return fn ? $(this).bind("fastClick", fn) : $(this).trigger("fastClick"); - }); - }; + /* + event.handle is deprecated and removed as of version 1.9 + use event.dispatch instead, + $.event.handle.apply(this, arguments); + */ + $.event.dispatch.apply(this, arguments); + } + }; + + $.fn.fastClick = function(fn) { + return $(this).each(function() { + return fn ? $(this).bind("fastClick", fn) : $(this).trigger("fastClick"); + }); + }; }(jQuery)); From cc41e93eaa66e644070a464abd2f31bf71e01d36 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 13:48:58 -0500 Subject: [PATCH 18/34] Adding "use strict" to jQuery implementation --- jquery.google.fastbutton.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jquery.google.fastbutton.js b/jquery.google.fastbutton.js index 5708a3c..18c5ce4 100644 --- a/jquery.google.fastbutton.js +++ b/jquery.google.fastbutton.js @@ -1,4 +1,6 @@ (function($) { + 'use strict'; + $.event.special.fastClick = { setup: function () { $(this).data('fastClick', new FastButton(this, $.event.special.fastClick.handler)); From 0045a46dea88193e33cf68dd4a24bc98e2aad417 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 13:49:23 -0500 Subject: [PATCH 19/34] Fixing line length --- jquery.google.fastbutton.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jquery.google.fastbutton.js b/jquery.google.fastbutton.js index 18c5ce4..ea643c3 100644 --- a/jquery.google.fastbutton.js +++ b/jquery.google.fastbutton.js @@ -3,7 +3,10 @@ $.event.special.fastClick = { setup: function () { - $(this).data('fastClick', new FastButton(this, $.event.special.fastClick.handler)); + $(this).data( + 'fastClick', + new window.FastButton(this, $.event.special.fastClick.handler) + ); }, teardown: function () { $(this).data('fastClick').destroy(); From 1fd9db1f91bd35407e6edd6b8c74b69f4b27185d Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 13:49:56 -0500 Subject: [PATCH 20/34] Consistent quotes in jQuery implementation --- jquery.google.fastbutton.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.google.fastbutton.js b/jquery.google.fastbutton.js index ea643c3..e86e4b9 100644 --- a/jquery.google.fastbutton.js +++ b/jquery.google.fastbutton.js @@ -28,7 +28,7 @@ $.fn.fastClick = function(fn) { return $(this).each(function() { - return fn ? $(this).bind("fastClick", fn) : $(this).trigger("fastClick"); + return fn ? $(this).bind('fastClick', fn) : $(this).trigger('fastClick'); }); }; }(jQuery)); From 017c9bcd9aa5bfb90cee70f047b684edceda48de Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 14:02:15 -0500 Subject: [PATCH 21/34] Adding "use strict" to xui implementation --- xui.google.fastbutton.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xui.google.fastbutton.js b/xui.google.fastbutton.js index 2dda484..7177e6e 100644 --- a/xui.google.fastbutton.js +++ b/xui.google.fastbutton.js @@ -1,5 +1,7 @@ /** Integrate google.fastbutton.js with XUI */ (function(x$) { + "use strict"; + x$.fn.fastClick = function(handler, useCapture) { return this.each(function(element, index, xui) { element.fastButton = new FastButton(xui[index], handler, useCapture); From 049b3b2e17f1a773c8ddda1d958e734b14acd8d9 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 14:02:34 -0500 Subject: [PATCH 22/34] Various lint improvements for xui implementation --- xui.google.fastbutton.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/xui.google.fastbutton.js b/xui.google.fastbutton.js index 7177e6e..5f0eb53 100644 --- a/xui.google.fastbutton.js +++ b/xui.google.fastbutton.js @@ -4,15 +4,15 @@ x$.fn.fastClick = function(handler, useCapture) { return this.each(function(element, index, xui) { - element.fastButton = new FastButton(xui[index], handler, useCapture); + element.fastButton = new window.FastButton(xui[index], handler, useCapture); }); - } - - x$.fn.unFastClick = function() { - return this.each(function(element, index, xui) { + }; + + x$.fn.unFastClick = function() { + return this.each(function (element) { element.fastButton.destroy(); element.fastButton = null; }); - } + }; }(xui)); From 9393dd6195923f0c67fc3eeef44a5d4636d5b8d2 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 14:25:00 -0500 Subject: [PATCH 23/34] JSHint Testing --- .gitignore | 1 + .travis.yml | 3 +++ Gruntfile.js | 1 + package.json | 23 +++++++++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Gruntfile.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ccbe46 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b0366aa --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - 0.9 diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..95b7fb9 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1 @@ +grunt.registerTask('travis', 'jshint'); diff --git a/package.json b/package.json new file mode 100644 index 0000000..f49405d --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "fast-button", + "version": "0.0.0", + "description": "An implementation of [Google's FastButton javascript](http://code.google.com/mobile/articles/fast_buttons.html), to avoid the 300ms touch delay on Android and iOS devices. Code forked from: http://stackoverflow.com/questions/6300136/trying-to-implement-googles-fast-button", + "main": "Gruntfile.js", + "scripts": { + "test": "grunt travis" + }, + "repository": { + "type": "git", + "url": "https://alex%40someoddpilot.com@github.com/alexsomeoddpilot/google-fastbutton.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/alexsomeoddpilot/google-fastbutton/issues" + }, + "homepage": "https://github.com/alexsomeoddpilot/google-fastbutton", + "devDependencies": { + "grunt": "^0.4.5", + "jshint": "^2.5.1" + } +} From 0b35d7b8f41f15c29a9754daf87b641943a993b2 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 14:30:59 -0500 Subject: [PATCH 24/34] Updated travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b0366aa..1932865 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: node_js node_js: - - 0.9 + - "0.11" From 3bfa4f2c16a1c9f14fbc6f8fbf27d012cac4ac77 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 14:38:55 -0500 Subject: [PATCH 25/34] Improved Gruntfile --- Gruntfile.js | 20 +++++++++++++++++++- package.json | 3 ++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 95b7fb9..6a9a4db 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1 +1,19 @@ -grunt.registerTask('travis', 'jshint'); +module.exports = function(grunt) { + grunt.initConfig({ + jshint: { + with_overrides: { + files: { + src: [ + 'google.fastbutton.js', + 'jquery.google.fastbutton.js', + 'xui.google.fastbutton.js' + ] + } + } + } + }); + + grunt.loadNpmTasks('grunt-contrib-jshint'); + + grunt.registerTask('travis', 'jshint'); +}; diff --git a/package.json b/package.json index f49405d..bca46c3 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "homepage": "https://github.com/alexsomeoddpilot/google-fastbutton", "devDependencies": { "grunt": "^0.4.5", - "jshint": "^2.5.1" + "jshint": "^2.5.1", + "grunt-contrib-jshint": "^0.10.0" } } From 38623157396049679150cebdefcccc3c948b822e Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 14:48:35 -0500 Subject: [PATCH 26/34] Still attempting travis --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1932865..666f7aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,5 @@ language: node_js node_js: - - "0.11" + - "0.8" +before_script: + - npm install -g grunt-cli From 26aef35ed718e533f4985b4fa038e9acbd81c0c7 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 14:48:35 -0500 Subject: [PATCH 27/34] Still attempting travis --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1932865..97a5adb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,5 @@ language: node_js node_js: - "0.11" +before_script: + - npm install -g grunt-cli From 564225d0268d630ae5d348d096709b22da65d273 Mon Sep 17 00:00:00 2001 From: alexsomeoddpilot Date: Wed, 28 May 2014 14:58:15 -0500 Subject: [PATCH 28/34] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 54dcc4e..35eeed5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Google FastButton +[![Build Status](https://travis-ci.org/alexsomeoddpilot/google-fastbutton.svg?branch=master)](https://travis-ci.org/alexsomeoddpilot/google-fastbutton) + An implementation of [Google's FastButton javascript](http://code.google.com/mobile/articles/fast_buttons.html), to avoid the 300ms touch delay on Android and iOS devices. Code forked from: http://stackoverflow.com/questions/6300136/trying-to-implement-googles-fast-button - Doesn't break in IE6-8, Chrome 17, Firefox 11, or Windows Phone 7.5 From 22548732bdfe3360cc42142aa749957ebb1d6a43 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Sat, 31 May 2014 14:26:08 -0500 Subject: [PATCH 29/34] Bower config --- bower.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 bower.json diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..d0c0b18 --- /dev/null +++ b/bower.json @@ -0,0 +1,16 @@ +{ + "name": "fast-button", + "version": "0.0.0", + "homepage": "https://github.com/alexsomeoddpilot/google-fastbutton", + "authors": [ + "Alex Robertson " + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} From 8c8018a858a40316fc11e503ec0f1fd8a72c4e2e Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Sat, 31 May 2014 15:57:01 -0500 Subject: [PATCH 30/34] Bower --- .gitignore | 2 ++ bower.json | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2ccbe46..25588d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /node_modules/ + +/bower_components/ diff --git a/bower.json b/bower.json index d0c0b18..ce35503 100644 --- a/bower.json +++ b/bower.json @@ -12,5 +12,9 @@ "bower_components", "test", "tests" - ] + ], + "devDependencies": { + "qunit": "~1.14.0", + "blanket": "~1.1.5" + } } From 324539c80b9c27fcd3eee7452493d8fa24d72086 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Sat, 31 May 2014 17:29:41 -0500 Subject: [PATCH 31/34] qunit testing --- google.fastbutton.js | 4 ++++ tests/index.html | 19 +++++++++++++++++++ tests/tests.js | 12 ++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/index.html create mode 100644 tests/tests.js diff --git a/google.fastbutton.js b/google.fastbutton.js index 92302ee..cb524cb 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -1,3 +1,7 @@ +/* jshint +quotmark: single + */ + (function() { 'use strict'; diff --git a/tests/index.html b/tests/index.html new file mode 100644 index 0000000..264beb6 --- /dev/null +++ b/tests/index.html @@ -0,0 +1,19 @@ + + + + + Tests + + + + + Button + +
+ + + + + + + diff --git a/tests/tests.js b/tests/tests.js new file mode 100644 index 0000000..a3e339e --- /dev/null +++ b/tests/tests.js @@ -0,0 +1,12 @@ +var testButton = new FastButton( + document.getElementById("button"), + function handler() { + var window.globalVar = true; + } +); + +test("element", function() { + equal(testButton.element, document.getElementById("button")); + + equal(window.globalVar); +}); From b0ce0a5d7a7716dee60546e7e6783cb34b2d63f6 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Sat, 31 May 2014 17:37:42 -0500 Subject: [PATCH 32/34] real qunit --- .travis.yml | 2 ++ Gruntfile.js | 11 ++++++++++- google.fastbutton.js | 4 ++++ package.json | 6 ++++-- tests/index.html | 19 +++++++++++++++++++ tests/tests.js | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tests/index.html create mode 100644 tests/tests.js diff --git a/.travis.yml b/.travis.yml index 97a5adb..6f26aac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,5 @@ node_js: - "0.11" before_script: - npm install -g grunt-cli + - npm install -g bower + - bower install diff --git a/Gruntfile.js b/Gruntfile.js index 6a9a4db..cd0c434 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -10,10 +10,19 @@ module.exports = function(grunt) { ] } } + }, + + qunit: { + all: ['tests/*.html'] } }); + grunt.loadNpmTasks('grunt-contrib-qunit'); + grunt.loadNpmTasks('grunt-contrib-jshint'); - grunt.registerTask('travis', 'jshint'); + grunt.registerTask('travis', [ + 'jshint', + 'qunit' + ]); }; diff --git a/google.fastbutton.js b/google.fastbutton.js index 92302ee..cb524cb 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -1,3 +1,7 @@ +/* jshint +quotmark: single + */ + (function() { 'use strict'; diff --git a/package.json b/package.json index bca46c3..6af16c2 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,10 @@ }, "homepage": "https://github.com/alexsomeoddpilot/google-fastbutton", "devDependencies": { + "bower": "^1.3.3", "grunt": "^0.4.5", - "jshint": "^2.5.1", - "grunt-contrib-jshint": "^0.10.0" + "grunt-contrib-jshint": "^0.10.0", + "grunt-contrib-qunit": "^0.4.0", + "jshint": "^2.5.1" } } diff --git a/tests/index.html b/tests/index.html new file mode 100644 index 0000000..264beb6 --- /dev/null +++ b/tests/index.html @@ -0,0 +1,19 @@ + + + + + Tests + + + + + Button + +
+ + + + + + + diff --git a/tests/tests.js b/tests/tests.js new file mode 100644 index 0000000..eefc496 --- /dev/null +++ b/tests/tests.js @@ -0,0 +1,32 @@ +(function (test, equal) { + "use strict"; + + test("element", function() { + var button = document.getElementById("button"); + + var handler = function handler() { + window.globalVar = true; + }; + + var testButton = new window.FastButton(button, handler); + + // equal(testButton.events.length, 1); + equal(testButton.element, button); + equal(testButton.handler, handler); + + var event = document.createEvent("HTMLEvents"); + event.initEvent("click", true, false); + button.dispatchEvent(event); + + equal(window.globalVar, true); + + testButton.destroy(); + + equal(testButton.element, null); + equal(testButton.events, null); + equal(testButton.handler, null); + equal(testButton.touchEvents, null); + }); + +}(window.test, window.equal)); + From d9676b768d8d771d0ffefea7a04a98ea729bf411 Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Mon, 22 Dec 2014 15:30:30 +0000 Subject: [PATCH 33/34] Add UMD boilerplate --- google.fastbutton.js | 35 +++++++++++++++++++++++++---------- jquery.google.fastbutton.js | 15 ++++++++++++--- xui.google.fastbutton.js | 14 ++++++++++++-- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/google.fastbutton.js b/google.fastbutton.js index cb524cb..7191bd5 100644 --- a/google.fastbutton.js +++ b/google.fastbutton.js @@ -2,7 +2,20 @@ quotmark: single */ -(function() { +(function (root, factory) { + if (typeof exports === 'object') { + // CommonJS + module.exports = factory(); + } else if (typeof define === 'function' && define.amd) { + // AMD + define(function() { + return (root.FastButton = factory()); + }); + } else { + // Global variable + root.FastButton = factory(); + } +}(this, function() { 'use strict'; // From: @@ -39,7 +52,7 @@ quotmark: single var isTouch = 'ontouchstart' in window; // Construct the FastButton with a reference to the element and click handler. - window.FastButton = function (element, handler, useCapture) { + function FastButton(element, handler, useCapture) { // collect functions to call to cleanup events this.events = []; this.touchEvents = []; @@ -52,10 +65,10 @@ quotmark: single ); } this.events.push(addListener(element, 'click', this, this.useCapture)); - }; + } // Remove event handling when no longer needed for this button - window.FastButton.prototype.destroy = function() { + FastButton.prototype.destroy = function() { for (var i = this.events.length - 1; i >= 0; i -= 1) { this.events[i].destroy(); } @@ -66,7 +79,7 @@ quotmark: single }; // acts as an event dispatcher - window.FastButton.prototype.handleEvent = function(event) { + FastButton.prototype.handleEvent = function(event) { switch (event.type) { case 'touchstart': this.onTouchStart(event); @@ -87,7 +100,7 @@ quotmark: single // to touchmove and touchend events. Calling stopPropagation guarantees // that other behaviors don’t get a chance to handle the same click event. // This is executed at the beginning of touch. - window.FastButton.prototype.onTouchStart = function(event) { + FastButton.prototype.onTouchStart = function(event) { if (event.stopPropagation) { event.stopPropagation(); } else { @@ -105,7 +118,7 @@ quotmark: single // When/if touchmove event is invoked, check if the user has dragged past // the threshold of 10px. - window.FastButton.prototype.onTouchMove = function(event) { + FastButton.prototype.onTouchMove = function(event) { if (Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) { this.reset(); //if he did, then cancel the touch event @@ -114,7 +127,7 @@ quotmark: single // Invoke the actual click handler and prevent ghost clicks if // this was a touchend event. - window.FastButton.prototype.onClick = function(event) { + FastButton.prototype.onClick = function(event) { if (event.stopPropagation) { event.stopPropagation(); } else { @@ -130,7 +143,7 @@ quotmark: single return result; }; - window.FastButton.prototype.reset = function() { + FastButton.prototype.reset = function() { for (var i = this.touchEvents.length - 1; i >= 0; i -= 1) { this.touchEvents[i].destroy(); } @@ -179,4 +192,6 @@ quotmark: single document.addEventListener('click', clickbuster.onClick, true); clickbuster.coordinates = []; } -})(this); + + return FastButton; +})); diff --git a/jquery.google.fastbutton.js b/jquery.google.fastbutton.js index e86e4b9..9835e23 100644 --- a/jquery.google.fastbutton.js +++ b/jquery.google.fastbutton.js @@ -1,4 +1,14 @@ -(function($) { +(function (factory) { + if (typeof exports === 'object') { + // CommonJS + module.exports = factory(require('jquery')); + } else if (typeof define === 'function' && define.amd) { + // AMD + define(['jquery'], factory); + } else { + factory(jQuery); + } +}(function($) { 'use strict'; $.event.special.fastClick = { @@ -31,5 +41,4 @@ return fn ? $(this).bind('fastClick', fn) : $(this).trigger('fastClick'); }); }; -}(jQuery)); - +})); diff --git a/xui.google.fastbutton.js b/xui.google.fastbutton.js index 5f0eb53..92d99cf 100644 --- a/xui.google.fastbutton.js +++ b/xui.google.fastbutton.js @@ -1,5 +1,15 @@ /** Integrate google.fastbutton.js with XUI */ -(function(x$) { +(function (factory) { + if (typeof exports === 'object') { + // CommonJS + module.exports = factory(require('xui')); + } else if (typeof define === 'function' && define.amd) { + // AMD + define(['xui'], factory); + } else { + factory(xui); + } +}(function(x$) { "use strict"; x$.fn.fastClick = function(handler, useCapture) { @@ -14,5 +24,5 @@ element.fastButton = null; }); }; -}(xui)); +})); From fef6f8d977e071d186a8484c19f3fecfe3496e4e Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Wed, 4 Feb 2015 08:46:38 -0600 Subject: [PATCH 34/34] Updating dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6af16c2..970fa46 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "devDependencies": { "bower": "^1.3.3", "grunt": "^0.4.5", - "grunt-contrib-jshint": "^0.10.0", - "grunt-contrib-qunit": "^0.4.0", + "grunt-contrib-jshint": "^0.11.0", + "grunt-contrib-qunit": "^0.5.2", "jshint": "^2.5.1" } }