Skip to content

Loading…

Move function declarations outside blocks #806

Closed
wants to merge 1 commit into from

2 participants

@nelsonmenezes

Function declarations should not be within blocks; they won't be hoisted (if following the spec; some browsers do it anyway). This breaks on Spidermonkey (Firefox) if on a touch-enabled device (e.g. a laptop with a touch screen). This is the problem mentioned on #754.

For reference: http://statichtml.com/2011/spidermonkey-function-hoisting.html

@jeffikus jeffikus closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 12, 2013
  1. @nelsonmenezes
This page is out of date. Refresh to see the latest.
Showing with 126 additions and 126 deletions.
  1. +126 −126 jquery.flexslider.js
View
252 jquery.flexslider.js
@@ -393,79 +393,156 @@
localY = 0,
accDx = 0;
- if(!msGesture){
- el.addEventListener('touchstart', onTouchStart, false);
+ function onTouchStart(e) {
+ if (slider.animating) {
+ e.preventDefault();
+ } else if ( ( window.navigator.msPointerEnabled ) || e.touches.length === 1 ) {
+ slider.pause();
+ // CAROUSEL:
+ cwidth = (vertical) ? slider.h : slider. w;
+ startT = Number(new Date());
+ // CAROUSEL:
+
+ // Local vars for X and Y points.
+ localX = e.touches[0].pageX;
+ localY = e.touches[0].pageY;
+
+ offset = (carousel && reverse && slider.animatingTo === slider.last) ? 0 :
+ (carousel && reverse) ? slider.limit - (((slider.itemW + slider.vars.itemMargin) * slider.move) * slider.animatingTo) :
+ (carousel && slider.currentSlide === slider.last) ? slider.limit :
+ (carousel) ? ((slider.itemW + slider.vars.itemMargin) * slider.move) * slider.currentSlide :
+ (reverse) ? (slider.last - slider.currentSlide + slider.cloneOffset) * cwidth : (slider.currentSlide + slider.cloneOffset) * cwidth;
+ startX = (vertical) ? localY : localX;
+ startY = (vertical) ? localX : localY;
+
+ el.addEventListener('touchmove', onTouchMove, false);
+ el.addEventListener('touchend', onTouchEnd, false);
+ }
+ }
+
+ function onTouchMove(e) {
+ // Local vars for X and Y points.
+
+ localX = e.touches[0].pageX;
+ localY = e.touches[0].pageY;
+
+ dx = (vertical) ? startX - localY : startX - localX;
+ scrolling = (vertical) ? (Math.abs(dx) < Math.abs(localX - startY)) : (Math.abs(dx) < Math.abs(localY - startY));
+
+ var fxms = 500;
- function onTouchStart(e) {
- if (slider.animating) {
+ if ( ! scrolling || Number( new Date() ) - startT > fxms ) {
+ e.preventDefault();
+ if (!fade && slider.transitions) {
+ if (!slider.vars.animationLoop) {
+ dx = dx/((slider.currentSlide === 0 && dx < 0 || slider.currentSlide === slider.last && dx > 0) ? (Math.abs(dx)/cwidth+2) : 1);
+ }
+ slider.setProps(offset + dx, "setTouch");
+ }
+ }
+ }
+
+ function onTouchEnd(e) {
+ // finish the touch by undoing the touch session
+ el.removeEventListener('touchmove', onTouchMove, false);
+
+ if (slider.animatingTo === slider.currentSlide && !scrolling && !(dx === null)) {
+ var updateDx = (reverse) ? -dx : dx,
+ target = (updateDx > 0) ? slider.getTarget('next') : slider.getTarget('prev');
+
+ if (slider.canAdvance(target) && (Number(new Date()) - startT < 550 && Math.abs(updateDx) > 50 || Math.abs(updateDx) > cwidth/2)) {
+ slider.flexAnimate(target, slider.vars.pauseOnAction);
+ } else {
+ if (!fade) slider.flexAnimate(slider.currentSlide, slider.vars.pauseOnAction, true);
+ }
+ }
+ el.removeEventListener('touchend', onTouchEnd, false);
+
+ startX = null;
+ startY = null;
+ dx = null;
+ offset = null;
+ }
+
+ function onMSPointerDown(e){
+ e.stopPropagation();
+ if (slider.animating) {
e.preventDefault();
- } else if ( ( window.navigator.msPointerEnabled ) || e.touches.length === 1 ) {
+ }else{
slider.pause();
- // CAROUSEL:
+ el._gesture.addPointer(e.pointerId);
+ accDx = 0;
cwidth = (vertical) ? slider.h : slider. w;
startT = Number(new Date());
// CAROUSEL:
- // Local vars for X and Y points.
- localX = e.touches[0].pageX;
- localY = e.touches[0].pageY;
-
offset = (carousel && reverse && slider.animatingTo === slider.last) ? 0 :
- (carousel && reverse) ? slider.limit - (((slider.itemW + slider.vars.itemMargin) * slider.move) * slider.animatingTo) :
- (carousel && slider.currentSlide === slider.last) ? slider.limit :
- (carousel) ? ((slider.itemW + slider.vars.itemMargin) * slider.move) * slider.currentSlide :
- (reverse) ? (slider.last - slider.currentSlide + slider.cloneOffset) * cwidth : (slider.currentSlide + slider.cloneOffset) * cwidth;
- startX = (vertical) ? localY : localX;
- startY = (vertical) ? localX : localY;
-
- el.addEventListener('touchmove', onTouchMove, false);
- el.addEventListener('touchend', onTouchEnd, false);
- }
+ (carousel && reverse) ? slider.limit - (((slider.itemW + slider.vars.itemMargin) * slider.move) * slider.animatingTo) :
+ (carousel && slider.currentSlide === slider.last) ? slider.limit :
+ (carousel) ? ((slider.itemW + slider.vars.itemMargin) * slider.move) * slider.currentSlide :
+ (reverse) ? (slider.last - slider.currentSlide + slider.cloneOffset) * cwidth : (slider.currentSlide + slider.cloneOffset) * cwidth;
}
+ }
- function onTouchMove(e) {
- // Local vars for X and Y points.
+ function onMSGestureChange(e) {
+ e.stopPropagation();
+ var slider = e.target._slider;
+ if(!slider){
+ return;
+ }
+ var transX = -e.translationX,
+ transY = -e.translationY;
- localX = e.touches[0].pageX;
- localY = e.touches[0].pageY;
+ //Accumulate translations.
+ accDx = accDx + ((vertical) ? transY : transX);
+ dx = accDx;
+ scrolling = (vertical) ? (Math.abs(accDx) < Math.abs(-transX)) : (Math.abs(accDx) < Math.abs(-transY));
- dx = (vertical) ? startX - localY : startX - localX;
- scrolling = (vertical) ? (Math.abs(dx) < Math.abs(localX - startY)) : (Math.abs(dx) < Math.abs(localY - startY));
+ if(e.detail === e.MSGESTURE_FLAG_INERTIA){
+ setImmediate(function (){
+ el._gesture.stop();
+ });
- var fxms = 500;
+ return;
+ }
- if ( ! scrolling || Number( new Date() ) - startT > fxms ) {
+ if (!scrolling || Number(new Date()) - startT > 500) {
e.preventDefault();
if (!fade && slider.transitions) {
- if (!slider.vars.animationLoop) {
- dx = dx/((slider.currentSlide === 0 && dx < 0 || slider.currentSlide === slider.last && dx > 0) ? (Math.abs(dx)/cwidth+2) : 1);
- }
- slider.setProps(offset + dx, "setTouch");
+ if (!slider.vars.animationLoop) {
+ dx = accDx / ((slider.currentSlide === 0 && accDx < 0 || slider.currentSlide === slider.last && accDx > 0) ? (Math.abs(accDx) / cwidth + 2) : 1);
+ }
+ slider.setProps(offset + dx, "setTouch");
}
- }
}
+ }
- function onTouchEnd(e) {
- // finish the touch by undoing the touch session
- el.removeEventListener('touchmove', onTouchMove, false);
-
- if (slider.animatingTo === slider.currentSlide && !scrolling && !(dx === null)) {
+ function onMSGestureEnd(e) {
+ e.stopPropagation();
+ var slider = e.target._slider;
+ if(!slider){
+ return;
+ }
+ if (slider.animatingTo === slider.currentSlide && !scrolling && !(dx === null)) {
var updateDx = (reverse) ? -dx : dx,
target = (updateDx > 0) ? slider.getTarget('next') : slider.getTarget('prev');
if (slider.canAdvance(target) && (Number(new Date()) - startT < 550 && Math.abs(updateDx) > 50 || Math.abs(updateDx) > cwidth/2)) {
- slider.flexAnimate(target, slider.vars.pauseOnAction);
+ slider.flexAnimate(target, slider.vars.pauseOnAction);
} else {
- if (!fade) slider.flexAnimate(slider.currentSlide, slider.vars.pauseOnAction, true);
+ if (!fade) slider.flexAnimate(slider.currentSlide, slider.vars.pauseOnAction, true);
}
- }
- el.removeEventListener('touchend', onTouchEnd, false);
-
- startX = null;
- startY = null;
- dx = null;
- offset = null;
}
+
+ startX = null;
+ startY = null;
+ dx = null;
+ offset = null;
+ accDx = 0;
+ }
+
+ if(!msGesture){
+ el.addEventListener('touchstart', onTouchStart, false);
}else{
el.style.msTouchAction = "none";
el._gesture = new MSGesture();
@@ -474,83 +551,6 @@
el._slider = slider;
el.addEventListener("MSGestureChange", onMSGestureChange, false);
el.addEventListener("MSGestureEnd", onMSGestureEnd, false);
-
- function onMSPointerDown(e){
- e.stopPropagation();
- if (slider.animating) {
- e.preventDefault();
- }else{
- slider.pause();
- el._gesture.addPointer(e.pointerId);
- accDx = 0;
- cwidth = (vertical) ? slider.h : slider. w;
- startT = Number(new Date());
- // CAROUSEL:
-
- offset = (carousel && reverse && slider.animatingTo === slider.last) ? 0 :
- (carousel && reverse) ? slider.limit - (((slider.itemW + slider.vars.itemMargin) * slider.move) * slider.animatingTo) :
- (carousel && slider.currentSlide === slider.last) ? slider.limit :
- (carousel) ? ((slider.itemW + slider.vars.itemMargin) * slider.move) * slider.currentSlide :
- (reverse) ? (slider.last - slider.currentSlide + slider.cloneOffset) * cwidth : (slider.currentSlide + slider.cloneOffset) * cwidth;
- }
- }
-
- function onMSGestureChange(e) {
- e.stopPropagation();
- var slider = e.target._slider;
- if(!slider){
- return;
- }
- var transX = -e.translationX,
- transY = -e.translationY;
-
- //Accumulate translations.
- accDx = accDx + ((vertical) ? transY : transX);
- dx = accDx;
- scrolling = (vertical) ? (Math.abs(accDx) < Math.abs(-transX)) : (Math.abs(accDx) < Math.abs(-transY));
-
- if(e.detail === e.MSGESTURE_FLAG_INERTIA){
- setImmediate(function (){
- el._gesture.stop();
- });
-
- return;
- }
-
- if (!scrolling || Number(new Date()) - startT > 500) {
- e.preventDefault();
- if (!fade && slider.transitions) {
- if (!slider.vars.animationLoop) {
- dx = accDx / ((slider.currentSlide === 0 && accDx < 0 || slider.currentSlide === slider.last && accDx > 0) ? (Math.abs(accDx) / cwidth + 2) : 1);
- }
- slider.setProps(offset + dx, "setTouch");
- }
- }
- }
-
- function onMSGestureEnd(e) {
- e.stopPropagation();
- var slider = e.target._slider;
- if(!slider){
- return;
- }
- if (slider.animatingTo === slider.currentSlide && !scrolling && !(dx === null)) {
- var updateDx = (reverse) ? -dx : dx,
- target = (updateDx > 0) ? slider.getTarget('next') : slider.getTarget('prev');
-
- if (slider.canAdvance(target) && (Number(new Date()) - startT < 550 && Math.abs(updateDx) > 50 || Math.abs(updateDx) > cwidth/2)) {
- slider.flexAnimate(target, slider.vars.pauseOnAction);
- } else {
- if (!fade) slider.flexAnimate(slider.currentSlide, slider.vars.pauseOnAction, true);
- }
- }
-
- startX = null;
- startY = null;
- dx = null;
- offset = null;
- accDx = 0;
- }
}
},
resize: function() {
@@ -599,7 +599,7 @@
if ('hidden' in document) return 'hidden';
for (var i = 0; i < prefixes.length; i++) {
- if ((prefixes[i] + 'Hidden') in document)
+ if ((prefixes[i] + 'Hidden') in document)
methods.pauseInvisible.visProp = prefixes[i] + 'Hidden';
}
if (methods.pauseInvisible.visProp) {
@@ -614,7 +614,7 @@
else (slider.vars.initDelay > 0) ? setTimeout(slider.play, slider.vars.initDelay) : slider.play(); //Didn't init before: simply init or wait for it
}
});
- }
+ }
},
isHidden: function() {
return document[methods.pauseInvisible.visProp] || false;
Something went wrong with that request. Please try again.