Browse Source

beautified js code

pull/179/head
Simon Waldherr 12 years ago
parent
commit
fdabe57db6
  1. 74
      dist/ratchet.js
  2. 37
      lib/js/push.js
  3. 8
      lib/js/segmented-controllers.js
  4. 11
      lib/js/sliders.js
  5. 15
      lib/js/toggles.js

74
dist/ratchet.js vendored

@ -284,8 +284,7 @@
barElement = document.querySelector(bars[key]); barElement = document.querySelector(bars[key]);
if (activeObj[key]) { if (activeObj[key]) {
swapContent(activeObj[key], barElement); swapContent(activeObj[key], barElement);
} } else if (barElement) {
else if (barElement) {
barElement.parentNode.removeChild(barElement); barElement.parentNode.removeChild(barElement);
} }
} }
@ -294,8 +293,7 @@
swapContent( swapContent(
(activeObj.contents || activeDom).cloneNode(true), (activeObj.contents || activeDom).cloneNode(true),
document.querySelector('.content'), document.querySelector('.content'),
transition transition);
);
PUSH.id = id; PUSH.id = id;
@ -344,7 +342,9 @@
} }
if (options.timeout) { if (options.timeout) {
options._timeout = setTimeout(function () { xhr.abort('timeout'); }, options.timeout); options._timeout = setTimeout(function () {
xhr.abort('timeout');
}, options.timeout);
} }
xhr.send(); xhr.send();
@ -398,11 +398,13 @@
var swapContent = function (swap, container, transition, complete) { var swapContent = function (swap, container, transition, complete) {
"use strict"; "use strict";
function fadeContainerEnd() { function fadeContainerEnd() {
container.removeEventListener('webkitTransitionEnd', fadeContainerEnd); container.removeEventListener('webkitTransitionEnd', fadeContainerEnd);
swap.classList.add('in'); swap.classList.add('in');
swap.addEventListener('webkitTransitionEnd', fadeSwapEnd); swap.addEventListener('webkitTransitionEnd', fadeSwapEnd);
} }
function fadeSwapEnd() { function fadeSwapEnd() {
swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd); swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd);
container.parentNode.removeChild(container); container.parentNode.removeChild(container);
@ -410,6 +412,7 @@
swap.classList.remove('in'); swap.classList.remove('in');
complete && complete(); complete && complete();
} }
function slideEnd() { function slideEnd() {
swap.removeEventListener('webkitTransitionEnd', slideEnd); swap.removeEventListener('webkitTransitionEnd', slideEnd);
swap.classList.remove('slide'); swap.classList.remove('slide');
@ -465,7 +468,9 @@
var triggerStateChange = function () { var triggerStateChange = function () {
var e = new CustomEvent('push', { var e = new CustomEvent('push', {
detail: { state: getCached(PUSH.id) }, detail: {
state: getCached(PUSH.id)
},
bubbles: true, bubbles: true,
cancelable: true cancelable: true
}); });
@ -476,7 +481,9 @@
var findTarget = function (target) { var findTarget = function (target) {
var i, toggles = document.querySelectorAll('a'); var i, toggles = document.querySelectorAll('a');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = toggles.length; i--;) { if (toggles[i] === target) return target; } for (i = toggles.length; i--;) {
if (toggles[i] === target) return target;
}
} }
}; };
@ -486,7 +493,9 @@
}; };
var parseURL = function (url) { var parseURL = function (url) {
var a = document.createElement('a'); a.href = url; return a; var a = document.createElement('a');
a.href = url;
return a;
}; };
var extendWithDom = function (obj, fragment, dom) { var extendWithDom = function (obj, fragment, dom) {
@ -546,10 +555,16 @@
// Attach PUSH event handlers // Attach PUSH event handlers
// ========================== // ==========================
window.addEventListener('touchstart', function () { isScrolling = false; }); window.addEventListener('touchstart', function () {
window.addEventListener('touchmove', function () { isScrolling = true; }) isScrolling = false;
});
window.addEventListener('touchmove', function () {
isScrolling = true;
})
window.addEventListener('touchend', touchend); window.addEventListener('touchend', touchend);
window.addEventListener('click', function (e) { if (getTarget(e)) e.preventDefault(); }); window.addEventListener('click', function (e) {
if (getTarget(e)) e.preventDefault();
});
window.addEventListener('popstate', popstate); window.addEventListener('popstate', popstate);
}(); }();
@ -564,7 +579,9 @@
var getTarget = function (target) { var getTarget = function (target) {
var i, popovers = document.querySelectorAll('.segmented-controller li a'); var i, popovers = document.querySelectorAll('.segmented-controller li a');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = popovers.length; i--;) { if (popovers[i] === target) return target; } for (i = popovers.length; i--;) {
if (popovers[i] === target) return target;
}
} }
}; };
@ -609,8 +626,11 @@
targetBody.classList.add(className); targetBody.classList.add(className);
}); });
window.addEventListener('click', function (e) { if (getTarget(e.target)) e.preventDefault(); }); window.addEventListener('click', function (e) {
}();/* ---------------------------------- if (getTarget(e.target)) e.preventDefault();
});
}();
/* ----------------------------------
* SLIDER v1.0.0 * SLIDER v1.0.0
* Licensed under The MIT License * Licensed under The MIT License
* Adapted from Brad Birdsall's swipe * Adapted from Brad Birdsall's swipe
@ -636,7 +656,9 @@
var getSlider = function (target) { var getSlider = function (target) {
var i, sliders = document.querySelectorAll('.slider ul'); var i, sliders = document.querySelectorAll('.slider ul');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = sliders.length; i--;) { if (sliders[i] === target) return target; } for (i = sliders.length; i--;) {
if (sliders[i] === target) return target;
}
} }
} }
@ -702,8 +724,7 @@
if (!slider || isScrolling) return; if (!slider || isScrolling) return;
setSlideNumber( setSlideNumber(
(+new Date) - startTime < 1000 && Math.abs(deltaX) > 15 ? (deltaX < 0 ? -1 : 1) : 0 (+new Date) - startTime < 1000 && Math.abs(deltaX) > 15 ? (deltaX < 0 ? -1 : 1) : 0);
);
offsetX = slideNumber * sliderWidth; offsetX = slideNumber * sliderWidth;
@ -711,7 +732,9 @@
slider.style.webkitTransform = 'translate3d(' + offsetX + 'px,0,0)'; slider.style.webkitTransform = 'translate3d(' + offsetX + 'px,0,0)';
e = new CustomEvent('slide', { e = new CustomEvent('slide', {
detail: { slideNumber: Math.abs(slideNumber) }, detail: {
slideNumber: Math.abs(slideNumber)
},
bubbles: true, bubbles: true,
cancelable: true cancelable: true
}); });
@ -731,7 +754,7 @@
* ---------------------------------- */ * ---------------------------------- */
!function () { !function () {
"use strict";
var start = {}, var start = {},
touchMove = false, touchMove = false,
distanceX = false, distanceX = false,
@ -740,7 +763,9 @@
var findToggle = function (target) { var findToggle = function (target) {
var i, toggles = document.querySelectorAll('.toggle'); var i, toggles = document.querySelectorAll('.toggle');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = toggles.length; i--;) { if (toggles[i] === target) return target; } for (i = toggles.length; i--;) {
if (toggles[i] === target) return target;
}
} }
} }
@ -758,7 +783,10 @@
handleWidth = handle.offsetWidth, handleWidth = handle.offsetWidth,
offset = toggle.classList.contains('active') ? (toggleWidth - handleWidth) : 0; offset = toggle.classList.contains('active') ? (toggleWidth - handleWidth) : 0;
start = { pageX : e.touches[0].pageX - offset, pageY : e.touches[0].pageY }; start = {
pageX: e.touches[0].pageX - offset,
pageY: e.touches[0].pageY
};
touchMove = false; touchMove = false;
// todo: probably should be moved to the css // todo: probably should be moved to the css
@ -817,7 +845,9 @@
toggle.classList[slideOn ? 'add' : 'remove']('active'); toggle.classList[slideOn ? 'add' : 'remove']('active');
e = new CustomEvent('toggle', { e = new CustomEvent('toggle', {
detail: { isActive: slideOn }, detail: {
isActive: slideOn
},
bubbles: true, bubbles: true,
cancelable: true cancelable: true
}); });

37
lib/js/push.js

@ -165,8 +165,7 @@
barElement = document.querySelector(bars[key]); barElement = document.querySelector(bars[key]);
if (activeObj[key]) { if (activeObj[key]) {
swapContent(activeObj[key], barElement); swapContent(activeObj[key], barElement);
} } else if (barElement) {
else if (barElement) {
barElement.parentNode.removeChild(barElement); barElement.parentNode.removeChild(barElement);
} }
} }
@ -175,8 +174,7 @@
swapContent( swapContent(
(activeObj.contents || activeDom).cloneNode(true), (activeObj.contents || activeDom).cloneNode(true),
document.querySelector('.content'), document.querySelector('.content'),
transition transition);
);
PUSH.id = id; PUSH.id = id;
@ -225,7 +223,9 @@
} }
if (options.timeout) { if (options.timeout) {
options._timeout = setTimeout(function () { xhr.abort('timeout'); }, options.timeout); options._timeout = setTimeout(function () {
xhr.abort('timeout');
}, options.timeout);
} }
xhr.send(); xhr.send();
@ -279,11 +279,13 @@
var swapContent = function (swap, container, transition, complete) { var swapContent = function (swap, container, transition, complete) {
"use strict"; "use strict";
function fadeContainerEnd() { function fadeContainerEnd() {
container.removeEventListener('webkitTransitionEnd', fadeContainerEnd); container.removeEventListener('webkitTransitionEnd', fadeContainerEnd);
swap.classList.add('in'); swap.classList.add('in');
swap.addEventListener('webkitTransitionEnd', fadeSwapEnd); swap.addEventListener('webkitTransitionEnd', fadeSwapEnd);
} }
function fadeSwapEnd() { function fadeSwapEnd() {
swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd); swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd);
container.parentNode.removeChild(container); container.parentNode.removeChild(container);
@ -291,6 +293,7 @@
swap.classList.remove('in'); swap.classList.remove('in');
complete && complete(); complete && complete();
} }
function slideEnd() { function slideEnd() {
swap.removeEventListener('webkitTransitionEnd', slideEnd); swap.removeEventListener('webkitTransitionEnd', slideEnd);
swap.classList.remove('slide'); swap.classList.remove('slide');
@ -346,7 +349,9 @@
var triggerStateChange = function () { var triggerStateChange = function () {
var e = new CustomEvent('push', { var e = new CustomEvent('push', {
detail: { state: getCached(PUSH.id) }, detail: {
state: getCached(PUSH.id)
},
bubbles: true, bubbles: true,
cancelable: true cancelable: true
}); });
@ -357,7 +362,9 @@
var findTarget = function (target) { var findTarget = function (target) {
var i, toggles = document.querySelectorAll('a'); var i, toggles = document.querySelectorAll('a');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = toggles.length; i--;) { if (toggles[i] === target) return target; } for (i = toggles.length; i--;) {
if (toggles[i] === target) return target;
}
} }
}; };
@ -367,7 +374,9 @@
}; };
var parseURL = function (url) { var parseURL = function (url) {
var a = document.createElement('a'); a.href = url; return a; var a = document.createElement('a');
a.href = url;
return a;
}; };
var extendWithDom = function (obj, fragment, dom) { var extendWithDom = function (obj, fragment, dom) {
@ -427,10 +436,16 @@
// Attach PUSH event handlers // Attach PUSH event handlers
// ========================== // ==========================
window.addEventListener('touchstart', function () { isScrolling = false; }); window.addEventListener('touchstart', function () {
window.addEventListener('touchmove', function () { isScrolling = true; }) isScrolling = false;
});
window.addEventListener('touchmove', function () {
isScrolling = true;
})
window.addEventListener('touchend', touchend); window.addEventListener('touchend', touchend);
window.addEventListener('click', function (e) { if (getTarget(e)) e.preventDefault(); }); window.addEventListener('click', function (e) {
if (getTarget(e)) e.preventDefault();
});
window.addEventListener('popstate', popstate); window.addEventListener('popstate', popstate);
}(); }();

8
lib/js/segmented-controllers.js

@ -9,7 +9,9 @@
var getTarget = function (target) { var getTarget = function (target) {
var i, popovers = document.querySelectorAll('.segmented-controller li a'); var i, popovers = document.querySelectorAll('.segmented-controller li a');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = popovers.length; i--;) { if (popovers[i] === target) return target; } for (i = popovers.length; i--;) {
if (popovers[i] === target) return target;
}
} }
}; };
@ -54,5 +56,7 @@
targetBody.classList.add(className); targetBody.classList.add(className);
}); });
window.addEventListener('click', function (e) { if (getTarget(e.target)) e.preventDefault(); }); window.addEventListener('click', function (e) {
if (getTarget(e.target)) e.preventDefault();
});
}(); }();

11
lib/js/sliders.js

@ -24,7 +24,9 @@
var getSlider = function (target) { var getSlider = function (target) {
var i, sliders = document.querySelectorAll('.slider ul'); var i, sliders = document.querySelectorAll('.slider ul');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = sliders.length; i--;) { if (sliders[i] === target) return target; } for (i = sliders.length; i--;) {
if (sliders[i] === target) return target;
}
} }
} }
@ -90,8 +92,7 @@
if (!slider || isScrolling) return; if (!slider || isScrolling) return;
setSlideNumber( setSlideNumber(
(+new Date) - startTime < 1000 && Math.abs(deltaX) > 15 ? (deltaX < 0 ? -1 : 1) : 0 (+new Date) - startTime < 1000 && Math.abs(deltaX) > 15 ? (deltaX < 0 ? -1 : 1) : 0);
);
offsetX = slideNumber * sliderWidth; offsetX = slideNumber * sliderWidth;
@ -99,7 +100,9 @@
slider.style.webkitTransform = 'translate3d(' + offsetX + 'px,0,0)'; slider.style.webkitTransform = 'translate3d(' + offsetX + 'px,0,0)';
e = new CustomEvent('slide', { e = new CustomEvent('slide', {
detail: { slideNumber: Math.abs(slideNumber) }, detail: {
slideNumber: Math.abs(slideNumber)
},
bubbles: true, bubbles: true,
cancelable: true cancelable: true
}); });

15
lib/js/toggles.js

@ -5,7 +5,7 @@
* ---------------------------------- */ * ---------------------------------- */
!function () { !function () {
"use strict";
var start = {}, var start = {},
touchMove = false, touchMove = false,
distanceX = false, distanceX = false,
@ -14,7 +14,9 @@
var findToggle = function (target) { var findToggle = function (target) {
var i, toggles = document.querySelectorAll('.toggle'); var i, toggles = document.querySelectorAll('.toggle');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = toggles.length; i--;) { if (toggles[i] === target) return target; } for (i = toggles.length; i--;) {
if (toggles[i] === target) return target;
}
} }
} }
@ -32,7 +34,10 @@
handleWidth = handle.offsetWidth, handleWidth = handle.offsetWidth,
offset = toggle.classList.contains('active') ? (toggleWidth - handleWidth) : 0; offset = toggle.classList.contains('active') ? (toggleWidth - handleWidth) : 0;
start = { pageX : e.touches[0].pageX - offset, pageY : e.touches[0].pageY }; start = {
pageX: e.touches[0].pageX - offset,
pageY: e.touches[0].pageY
};
touchMove = false; touchMove = false;
// todo: probably should be moved to the css // todo: probably should be moved to the css
@ -91,7 +96,9 @@
toggle.classList[slideOn ? 'add' : 'remove']('active'); toggle.classList[slideOn ? 'add' : 'remove']('active');
e = new CustomEvent('toggle', { e = new CustomEvent('toggle', {
detail: { isActive: slideOn }, detail: {
isActive: slideOn
},
bubbles: true, bubbles: true,
cancelable: true cancelable: true
}); });

Loading…
Cancel
Save