Browse Source

more valid (jshint) code

pull/179/head
Simon Waldherr 12 years ago
parent
commit
c0d00ce2ca
  1. 390
      dist/ratchet.js
  2. 17
      lib/js/modals.js
  3. 32
      lib/js/popovers.js
  4. 210
      lib/js/push.js
  5. 39
      lib/js/segmented-controllers.js
  6. 28
      lib/js/sliders.js
  7. 59
      lib/js/toggles.js

390
dist/ratchet.js vendored

@ -13,37 +13,51 @@
* ---------------------------------- */
!function () {
"use strict";
var findModals = function (target) {
var i;
var modals = document.querySelectorAll('a');
for (; target && target !== document; target = target.parentNode) {
for (i = modals.length; i--;) { if (modals[i] === target) return target; }
for (i = modals.length; i--;) {
if (modals[i] === target) {
return target;
}
}
}
};
var getModal = function (event) {
var modalToggle = findModals(event.target);
if (modalToggle && modalToggle.hash) return document.querySelector(modalToggle.hash);
if (modalToggle && modalToggle.hash) {
return document.querySelector(modalToggle.hash);
}
};
window.addEventListener('touchend', function (event) {
var modal = getModal(event);
if (modal) modal.classList.toggle('active');
if (modal) {
modal.classList.toggle('active');
}
});
}();/* ----------------------------------
}();
/* ----------------------------------
* POPOVER v1.0.0
* Licensed under The MIT License
* http://opensource.org/licenses/MIT
* ---------------------------------- */
!function () {
"use strict";
var popover;
var findPopovers = function (target) {
var i, popovers = document.querySelectorAll('a');
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;
}
}
}
};
@ -51,7 +65,7 @@
document.body.removeChild(backdrop);
popover.style.display = 'none';
popover.removeEventListener('webkitTransitionEnd', onPopoverHidden);
}
};
var backdrop = function () {
var element = document.createElement('div');
@ -69,28 +83,38 @@
var getPopover = function (e) {
var anchor = findPopovers(e.target);
if (!anchor || !anchor.hash) return;
if (!anchor || !anchor.hash) {
return;
}
popover = document.querySelector(anchor.hash);
if (!popover || !popover.classList.contains('popover')) return;
if (!popover || !popover.classList.contains('popover')) {
return;
}
return popover;
}
};
window.addEventListener('touchend', function (e) {
var popover = getPopover(e);
if (!popover) return;
if (!popover) {
return;
}
popover.style.display = 'block';
popover.offsetHeight;
//popover.offsetHeight;
popover.classList.add('visible');
popover.parentNode.appendChild(backdrop);
});
window.addEventListener('click', function (e) { if (getPopover(e)) e.preventDefault(); });
window.addEventListener('click', function (e) {
if (getPopover(e)) {
e.preventDefault();
}
});
}();
/* ----------------------------------
@ -101,47 +125,51 @@
* ---------------------------------- */
!function () {
"use strict";
var noop = function () {};
// Pushstate cacheing
// ==================
var isScrolling;
var maxCacheLength = 20;
var cacheMapping = sessionStorage;
var domCache = {};
var transitionMap = {
var isScrolling,
maxCacheLength = 20,
cacheMapping = sessionStorage,
domCache = {},
transitionMap = {
'slide-in' : 'slide-out',
'slide-out' : 'slide-in',
'fade' : 'fade'
};
var bars = {
},
bars = {
bartab : '.bar-tab',
bartitle : '.bar-title',
barfooter : '.bar-footer',
barheadersecondary : '.bar-header-secondary'
}
};
var cacheReplace = function (data, updates) {
PUSH.id = data.id;
if (updates) data = getCached(data.id);
if (updates) {
data = getCached(data.id);
}
cacheMapping[data.id] = JSON.stringify(data);
window.history.replaceState(data.id, data.title, data.url);
domCache[data.id] = document.body.cloneNode(true);
};
var cachePush = function () {
var id = PUSH.id;
var cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]');
var cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]');
var id = PUSH.id,
cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]'),
cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]');
cacheBackStack.push(id);
while (cacheForwardStack.length) delete cacheMapping[cacheForwardStack.shift()];
while (cacheBackStack.length > maxCacheLength) delete cacheMapping[cacheBackStack.shift()];
while (cacheForwardStack.length) {
delete cacheMapping[cacheForwardStack.shift()];
}
while (cacheBackStack.length > maxCacheLength) {
delete cacheMapping[cacheBackStack.shift()];
}
window.history.pushState(null, '', cacheMapping[PUSH.id].url);
@ -150,13 +178,15 @@
};
var cachePop = function (id, direction) {
var forward = direction == 'forward';
var cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]');
var cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]');
var pushStack = forward ? cacheBackStack : cacheForwardStack;
var popStack = forward ? cacheForwardStack : cacheBackStack;
if (PUSH.id) pushStack.push(PUSH.id);
var forward = direction === 'forward',
cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]'),
cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]'),
pushStack = forward ? cacheBackStack : cacheForwardStack,
popStack = forward ? cacheForwardStack : cacheBackStack;
if (PUSH.id) {
pushStack.push(PUSH.id);
}
popStack.pop();
cacheMapping.cacheForwardStack = JSON.stringify(cacheForwardStack);
@ -170,18 +200,9 @@
var getTarget = function (e) {
var target = findTarget(e.target);
if (
! target
|| e.which > 1
|| e.metaKey
|| e.ctrlKey
|| isScrolling
|| location.protocol !== target.protocol
|| location.host !== target.host
|| !target.hash && /#/.test(target.href)
|| target.hash && target.href.replace(target.hash, '') === location.href.replace(location.hash, '')
|| target.getAttribute('data-ignore') == 'push'
) return;
if (!target || (e.which > 1) || e.metaKey || e.ctrlKey || isScrolling || (location.protocol !== target.protocol) || (location.host !== target.host) || (!target.hash && /#/.test(target.href)) || (target.hash && target.href.replace(target.hash, '') === location.href.replace(location.hash, '')) || (target.getAttribute('data-ignore') === 'push')) {
return;
}
return target;
};
@ -193,7 +214,9 @@
var touchend = function (e) {
var target = getTarget(e);
if (!target) return;
if (!target) {
return;
}
e.preventDefault();
@ -206,37 +229,43 @@
};
var popstate = function (e) {
var key;
var barElement;
var activeObj;
var activeDom;
var direction;
var transition;
var transitionFrom;
var transitionFromObj;
var id = e.state;
if (!id || !cacheMapping[id]) return;
var key,
barElement,
activeObj,
activeDom,
direction,
transition,
transitionFrom,
transitionFromObj,
id = e.state;
if (!id || !cacheMapping[id]) {
return;
}
direction = PUSH.id < id ? 'forward' : 'back';
direction = (PUSH.id < id) ? 'forward' : 'back';
cachePop(id, direction);
activeObj = getCached(id);
activeDom = domCache[id];
if (activeObj.title) document.title = activeObj.title;
if (activeObj.title) {
document.title = activeObj.title;
}
if (direction == 'back') {
transitionFrom = JSON.parse(direction == 'back' ? cacheMapping.cacheForwardStack : cacheMapping.cacheBackStack);
if (direction === 'back') {
transitionFrom = JSON.parse((direction === 'back') ? cacheMapping.cacheForwardStack : cacheMapping.cacheBackStack);
transitionFromObj = getCached(transitionFrom[transitionFrom.length - 1]);
} else {
transitionFromObj = activeObj;
}
if (direction == 'back' && !transitionFromObj.id) return PUSH.id = id;
if (direction === 'back' && !transitionFromObj.id) {
return id;
}
transition = direction == 'back' ? transitionMap[transitionFromObj.transition] : transitionFromObj.transition;
transition = (direction === 'back') ? transitionMap[transitionFromObj.transition] : transitionFromObj.transition;
if (!activeDom) {
return PUSH({
@ -252,9 +281,13 @@
if (transitionFromObj.transition) {
activeObj = extendWithDom(activeObj, '.content', activeDom.cloneNode(true));
for (key in bars) {
barElement = document.querySelector(bars[key])
if (activeObj[key]) swapContent(activeObj[key], barElement);
else if (barElement) barElement.parentNode.removeChild(barElement);
barElement = document.querySelector(bars[key]);
if (activeObj[key]) {
swapContent(activeObj[key], barElement);
}
else if (barElement) {
barElement.parentNode.removeChild(barElement);
}
}
}
@ -274,9 +307,9 @@
// =======================
var PUSH = function (options) {
var key;
var data = {};
var xhr = PUSH.xhr;
var key,
data = {},
xhr = PUSH.xhr;
options.container = options.container || options.transition ? document.querySelector('.content') : document.body;
@ -295,7 +328,9 @@
xhr.onreadystatechange = function () {
if (options._timeout) clearTimeout(options._timeout);
if (xhr.readyState == 4) xhr.status == 200 ? success(xhr, options) : failure(options.url);
if (xhr.readyState === 4) {
(xhr.status === 200) ? success(xhr, options) : failure(options.url);
}
};
if (!PUSH.id) {
@ -362,9 +397,29 @@
// ============
var swapContent = function (swap, container, transition, complete) {
var enter;
var containerDirection;
var swapDirection;
"use strict";
function fadeContainerEnd() {
container.removeEventListener('webkitTransitionEnd', fadeContainerEnd);
swap.classList.add('in');
swap.addEventListener('webkitTransitionEnd', fadeSwapEnd);
}
function fadeSwapEnd () {
swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd);
container.parentNode.removeChild(container);
swap.classList.remove('fade');
swap.classList.remove('in');
complete && complete();
}
function slideEnd() {
swap.removeEventListener('webkitTransitionEnd', slideEnd);
swap.classList.remove('slide');
swap.classList.remove(swapDirection);
container.parentNode.removeChild(container);
complete && complete();
}
var enter,
containerDirection,
swapDirection;
if (!transition) {
if (container) container.innerHTML = swap.innerHTML;
@ -373,7 +428,7 @@
} else {
enter = /in$/.test(transition);
if (transition == 'fade') {
if (transition === 'fade') {
container.classList.add('in');
container.classList.add('fade');
swap.classList.add('fade');
@ -388,25 +443,14 @@
container.parentNode.insertBefore(swap, container);
}
if (!transition) complete && complete();
if (!transition) {
complete && complete();
}
if (transition == 'fade') {
if (transition === 'fade') {
container.offsetWidth; // force reflow
container.classList.remove('in');
container.addEventListener('webkitTransitionEnd', fadeContainerEnd);
function fadeContainerEnd() {
container.removeEventListener('webkitTransitionEnd', fadeContainerEnd);
swap.classList.add('in');
swap.addEventListener('webkitTransitionEnd', fadeSwapEnd);
}
function fadeSwapEnd () {
swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd);
container.parentNode.removeChild(container);
swap.classList.remove('fade');
swap.classList.remove('in');
complete && complete();
}
}
if (/slide/.test(transition)) {
@ -416,14 +460,6 @@
container.classList.add(containerDirection);
swap.classList.remove(swapDirection);
swap.addEventListener('webkitTransitionEnd', slideEnd);
function slideEnd() {
swap.removeEventListener('webkitTransitionEnd', slideEnd);
swap.classList.remove('slide');
swap.classList.remove(swapDirection);
container.parentNode.removeChild(container);
complete && complete();
}
}
};
@ -457,7 +493,9 @@
var i;
var result = {};
for (i in obj) result[i] = obj[i];
for (i = 0; i < obj.length; i += 1) {
result[i] = obj[i];
}
Object.keys(bars).forEach(function (key) {
var el = dom.querySelector(bars[key]);
@ -471,14 +509,16 @@
};
var parseXHR = function (xhr, options) {
var head;
var body;
var data = {};
var responseText = xhr.responseText;
var head,
body,
data = {},
responseText = xhr.responseText;
data.url = options.url;
if (!responseText) return data;
if (!responseText) {
return data;
}
if (/<html/i.test(responseText)) {
head = document.createElement('div');
@ -493,8 +533,11 @@
data.title = head.querySelector('title');
data.title = data.title && data.title.innerText.trim();
if (options.transition) data = extendWithDom(data, '.content', body);
else data.contents = body;
if (options.transition) {
data = extendWithDom(data, '.content', body);
} else {
data.contents = body;
}
return data;
};
@ -509,13 +552,15 @@
window.addEventListener('click', function (e) { if (getTarget(e)) e.preventDefault(); });
window.addEventListener('popstate', popstate);
}();/* ----------------------------------
}();
/* ----------------------------------
* TABS v1.0.0
* Licensed under The MIT License
* http://opensource.org/licenses/MIT
* ---------------------------------- */
!function () {
"use strict";
var getTarget = function (target) {
var i, popovers = document.querySelectorAll('.segmented-controller li a');
for (; target && target !== document; target = target.parentNode) {
@ -524,34 +569,44 @@
};
window.addEventListener("touchend", function (e) {
var activeTab;
var activeBody;
var targetBody;
var targetTab;
var className = 'active';
var classSelector = '.' + className;
var targetAnchor = getTarget(e.target);
if (!targetAnchor) return;
var activeTab,
activeBody,
targetBody,
targetTab,
className = 'active',
classSelector = '.' + className,
targetAnchor = getTarget(e.target);
if (!targetAnchor) {
return;
}
targetTab = targetAnchor.parentNode;
activeTab = targetTab.parentNode.querySelector(classSelector);
if (activeTab) activeTab.classList.remove(className);
if (activeTab) {
activeTab.classList.remove(className);
}
targetTab.classList.add(className);
if (!targetAnchor.hash) return;
if (!targetAnchor.hash) {
return;
}
targetBody = document.querySelector(targetAnchor.hash);
if (!targetBody) return;
if (!targetBody) {
return;
}
activeBody = targetBody.parentNode.querySelector(classSelector);
if (activeBody) activeBody.classList.remove(className);
if (activeBody) {
activeBody.classList.remove(className);
}
targetBody.classList.add(className)
targetBody.classList.add(className);
});
window.addEventListener('click', function (e) { if (getTarget(e.target)) e.preventDefault(); });
@ -563,20 +618,20 @@
* ---------------------------------- */
!function () {
var pageX;
var pageY;
var slider;
var deltaX;
var deltaY;
var offsetX;
var lastSlide;
var startTime;
var resistance;
var sliderWidth;
var slideNumber;
var isScrolling;
var scrollableArea;
"use strict";
var pageX,
pageY,
slider,
deltaX,
deltaY,
offsetX,
lastSlide,
startTime,
resistance,
sliderWidth,
slideNumber,
isScrolling,
scrollableArea;
var getSlider = function (target) {
var i, sliders = document.querySelectorAll('.slider ul');
@ -677,10 +732,10 @@
!function () {
var start = {};
var touchMove = false;
var distanceX = false;
var toggle = false;
var start = {},
touchMove = false,
distanceX = false,
toggle = false;
var findToggle = function (target) {
var i, toggles = document.querySelectorAll('.toggle');
@ -694,12 +749,14 @@
toggle = findToggle(e.target);
if (!toggle) return;
if (!toggle) {
return;
}
var handle = toggle.querySelector('.toggle-handle');
var toggleWidth = toggle.offsetWidth;
var handleWidth = handle.offsetWidth;
var offset = toggle.classList.contains('active') ? toggleWidth - handleWidth : 0;
var handle = toggle.querySelector('.toggle-handle'),
toggleWidth = toggle.offsetWidth,
handleWidth = handle.offsetWidth,
offset = toggle.classList.contains('active') ? (toggleWidth - handleWidth) : 0;
start = { pageX : e.touches[0].pageX - offset, pageY : e.touches[0].pageY };
touchMove = false;
@ -715,21 +772,27 @@
if (!toggle) return;
var handle = toggle.querySelector('.toggle-handle');
var current = e.touches[0];
var toggleWidth = toggle.offsetWidth;
var handleWidth = handle.offsetWidth;
var offset = toggleWidth - handleWidth;
var handle = toggle.querySelector('.toggle-handle'),
current = e.touches[0],
toggleWidth = toggle.offsetWidth,
handleWidth = handle.offsetWidth,
offset = toggleWidth - handleWidth;
touchMove = true;
distanceX = current.pageX - start.pageX;
if (Math.abs(distanceX) < Math.abs(current.pageY - start.pageY)) return;
if (Math.abs(distanceX) < Math.abs(current.pageY - start.pageY)) {
return;
}
e.preventDefault();
if (distanceX < 0) return handle.style.webkitTransform = 'translate3d(0,0,0)';
if (distanceX > offset) return handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
if (distanceX < 0) {
return handle.style.webkitTransform = 'translate3d(0,0,0)';
}
if (distanceX > offset) {
return handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
}
handle.style.webkitTransform = 'translate3d(' + distanceX + 'px,0,0)';
@ -739,14 +802,17 @@
window.addEventListener('touchend', function (e) {
if (!toggle) return;
var handle = toggle.querySelector('.toggle-handle');
var toggleWidth = toggle.offsetWidth;
var handleWidth = handle.offsetWidth;
var offset = toggleWidth - handleWidth;
var slideOn = (!touchMove && !toggle.classList.contains('active')) || (touchMove && (distanceX > (toggleWidth/2 - handleWidth/2)));
var handle = toggle.querySelector('.toggle-handle'),
toggleWidth = toggle.offsetWidth,
handleWidth = handle.offsetWidth,
offset = toggleWidth - handleWidth,
slideOn = (!touchMove && !toggle.classList.contains('active')) || (touchMove && (distanceX > (toggleWidth/2 - handleWidth/2)));
if (slideOn) handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
else handle.style.webkitTransform = 'translate3d(0,0,0)';
if (slideOn) {
handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
} else {
handle.style.webkitTransform = 'translate3d(0,0,0)';
}
toggle.classList[slideOn ? 'add' : 'remove']('active');

17
lib/js/modals.js

@ -5,21 +5,30 @@
* ---------------------------------- */
!function () {
"use strict";
var findModals = function (target) {
var i;
var modals = document.querySelectorAll('a');
for (; target && target !== document; target = target.parentNode) {
for (i = modals.length; i--;) { if (modals[i] === target) return target; }
for (i = modals.length; i--;) {
if (modals[i] === target) {
return target;
}
}
}
};
var getModal = function (event) {
var modalToggle = findModals(event.target);
if (modalToggle && modalToggle.hash) return document.querySelector(modalToggle.hash);
if (modalToggle && modalToggle.hash) {
return document.querySelector(modalToggle.hash);
}
};
window.addEventListener('touchend', function (event) {
var modal = getModal(event);
if (modal) modal.classList.toggle('active');
if (modal) {
modal.classList.toggle('active');
}
});
}();
}();

32
lib/js/popovers.js

@ -5,13 +5,17 @@
* ---------------------------------- */
!function () {
"use strict";
var popover;
var findPopovers = function (target) {
var i, popovers = document.querySelectorAll('a');
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;
}
}
}
};
@ -19,7 +23,7 @@
document.body.removeChild(backdrop);
popover.style.display = 'none';
popover.removeEventListener('webkitTransitionEnd', onPopoverHidden);
}
};
var backdrop = function () {
var element = document.createElement('div');
@ -37,27 +41,37 @@
var getPopover = function (e) {
var anchor = findPopovers(e.target);
if (!anchor || !anchor.hash) return;
if (!anchor || !anchor.hash) {
return;
}
popover = document.querySelector(anchor.hash);
if (!popover || !popover.classList.contains('popover')) return;
if (!popover || !popover.classList.contains('popover')) {
return;
}
return popover;
}
};
window.addEventListener('touchend', function (e) {
var popover = getPopover(e);
if (!popover) return;
if (!popover) {
return;
}
popover.style.display = 'block';
popover.offsetHeight;
//popover.offsetHeight;
popover.classList.add('visible');
popover.parentNode.appendChild(backdrop);
});
window.addEventListener('click', function (e) { if (getPopover(e)) e.preventDefault(); });
window.addEventListener('click', function (e) {
if (getPopover(e)) {
e.preventDefault();
}
});
}();

210
lib/js/push.js

@ -6,24 +6,22 @@
* ---------------------------------- */
!function () {
"use strict";
var noop = function () {};
// Pushstate cacheing
// ==================
var isScrolling;
var maxCacheLength = 20;
var cacheMapping = sessionStorage;
var domCache = {};
var transitionMap = {
var isScrolling,
maxCacheLength = 20,
cacheMapping = sessionStorage,
domCache = {},
transitionMap = {
'slide-in' : 'slide-out',
'slide-out' : 'slide-in',
'fade' : 'fade'
};
var bars = {
},
bars = {
bartab : '.bar-tab',
bartitle : '.bar-title',
barfooter : '.bar-footer',
@ -32,22 +30,27 @@
var cacheReplace = function (data, updates) {
PUSH.id = data.id;
if (updates) data = getCached(data.id);
if (updates) {
data = getCached(data.id);
}
cacheMapping[data.id] = JSON.stringify(data);
window.history.replaceState(data.id, data.title, data.url);
domCache[data.id] = document.body.cloneNode(true);
};
var cachePush = function () {
var id = PUSH.id;
var cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]');
var cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]');
var id = PUSH.id,
cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]'),
cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]');
cacheBackStack.push(id);
while (cacheForwardStack.length) delete cacheMapping[cacheForwardStack.shift()];
while (cacheBackStack.length > maxCacheLength) delete cacheMapping[cacheBackStack.shift()];
while (cacheForwardStack.length) {
delete cacheMapping[cacheForwardStack.shift()];
}
while (cacheBackStack.length > maxCacheLength) {
delete cacheMapping[cacheBackStack.shift()];
}
window.history.pushState(null, '', cacheMapping[PUSH.id].url);
@ -56,13 +59,15 @@
};
var cachePop = function (id, direction) {
var forward = direction == 'forward';
var cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]');
var cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]');
var pushStack = forward ? cacheBackStack : cacheForwardStack;
var popStack = forward ? cacheForwardStack : cacheBackStack;
if (PUSH.id) pushStack.push(PUSH.id);
var forward = direction === 'forward',
cacheForwardStack = JSON.parse(cacheMapping.cacheForwardStack || '[]'),
cacheBackStack = JSON.parse(cacheMapping.cacheBackStack || '[]'),
pushStack = forward ? cacheBackStack : cacheForwardStack,
popStack = forward ? cacheForwardStack : cacheBackStack;
if (PUSH.id) {
pushStack.push(PUSH.id);
}
popStack.pop();
cacheMapping.cacheForwardStack = JSON.stringify(cacheForwardStack);
@ -76,18 +81,9 @@
var getTarget = function (e) {
var target = findTarget(e.target);
if (
! target
|| e.which > 1
|| e.metaKey
|| e.ctrlKey
|| isScrolling
|| location.protocol !== target.protocol
|| location.host !== target.host
|| !target.hash && /#/.test(target.href)
|| target.hash && target.href.replace(target.hash, '') === location.href.replace(location.hash, '')
|| target.getAttribute('data-ignore') == 'push'
) return;
if (!target || (e.which > 1) || e.metaKey || e.ctrlKey || isScrolling || (location.protocol !== target.protocol) || (location.host !== target.host) || (!target.hash && /#/.test(target.href)) || (target.hash && target.href.replace(target.hash, '') === location.href.replace(location.hash, '')) || (target.getAttribute('data-ignore') === 'push')) {
return;
}
return target;
};
@ -99,7 +95,9 @@
var touchend = function (e) {
var target = getTarget(e);
if (!target) return;
if (!target) {
return;
}
e.preventDefault();
@ -112,37 +110,43 @@
};
var popstate = function (e) {
var key;
var barElement;
var activeObj;
var activeDom;
var direction;
var transition;
var transitionFrom;
var transitionFromObj;
var id = e.state;
if (!id || !cacheMapping[id]) return;
var key,
barElement,
activeObj,
activeDom,
direction,
transition,
transitionFrom,
transitionFromObj,
id = e.state;
if (!id || !cacheMapping[id]) {
return;
}
direction = PUSH.id < id ? 'forward' : 'back';
direction = (PUSH.id < id) ? 'forward' : 'back';
cachePop(id, direction);
activeObj = getCached(id);
activeDom = domCache[id];
if (activeObj.title) document.title = activeObj.title;
if (activeObj.title) {
document.title = activeObj.title;
}
if (direction == 'back') {
transitionFrom = JSON.parse(direction == 'back' ? cacheMapping.cacheForwardStack : cacheMapping.cacheBackStack);
if (direction === 'back') {
transitionFrom = JSON.parse((direction === 'back') ? cacheMapping.cacheForwardStack : cacheMapping.cacheBackStack);
transitionFromObj = getCached(transitionFrom[transitionFrom.length - 1]);
} else {
transitionFromObj = activeObj;
}
if (direction == 'back' && !transitionFromObj.id) return PUSH.id = id;
if (direction === 'back' && !transitionFromObj.id) {
return id;
}
transition = direction == 'back' ? transitionMap[transitionFromObj.transition] : transitionFromObj.transition;
transition = (direction === 'back') ? transitionMap[transitionFromObj.transition] : transitionFromObj.transition;
if (!activeDom) {
return PUSH({
@ -158,9 +162,13 @@
if (transitionFromObj.transition) {
activeObj = extendWithDom(activeObj, '.content', activeDom.cloneNode(true));
for (key in bars) {
barElement = document.querySelector(bars[key])
if (activeObj[key]) swapContent(activeObj[key], barElement);
else if (barElement) barElement.parentNode.removeChild(barElement);
barElement = document.querySelector(bars[key]);
if (activeObj[key]) {
swapContent(activeObj[key], barElement);
}
else if (barElement) {
barElement.parentNode.removeChild(barElement);
}
}
}
@ -180,9 +188,9 @@
// =======================
var PUSH = function (options) {
var key;
var data = {};
var xhr = PUSH.xhr;
var key,
data = {},
xhr = PUSH.xhr;
options.container = options.container || options.transition ? document.querySelector('.content') : document.body;
@ -201,7 +209,9 @@
xhr.onreadystatechange = function () {
if (options._timeout) clearTimeout(options._timeout);
if (xhr.readyState == 4) xhr.status == 200 ? success(xhr, options) : failure(options.url);
if (xhr.readyState === 4) {
(xhr.status === 200) ? success(xhr, options) : failure(options.url);
}
};
if (!PUSH.id) {
@ -268,9 +278,29 @@
// ============
var swapContent = function (swap, container, transition, complete) {
var enter;
var containerDirection;
var swapDirection;
"use strict";
function fadeContainerEnd() {
container.removeEventListener('webkitTransitionEnd', fadeContainerEnd);
swap.classList.add('in');
swap.addEventListener('webkitTransitionEnd', fadeSwapEnd);
}
function fadeSwapEnd () {
swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd);
container.parentNode.removeChild(container);
swap.classList.remove('fade');
swap.classList.remove('in');
complete && complete();
}
function slideEnd() {
swap.removeEventListener('webkitTransitionEnd', slideEnd);
swap.classList.remove('slide');
swap.classList.remove(swapDirection);
container.parentNode.removeChild(container);
complete && complete();
}
var enter,
containerDirection,
swapDirection;
if (!transition) {
if (container) container.innerHTML = swap.innerHTML;
@ -279,7 +309,7 @@
} else {
enter = /in$/.test(transition);
if (transition == 'fade') {
if (transition === 'fade') {
container.classList.add('in');
container.classList.add('fade');
swap.classList.add('fade');
@ -294,25 +324,14 @@
container.parentNode.insertBefore(swap, container);
}
if (!transition) complete && complete();
if (!transition) {
complete && complete();
}
if (transition == 'fade') {
if (transition === 'fade') {
container.offsetWidth; // force reflow
container.classList.remove('in');
container.addEventListener('webkitTransitionEnd', fadeContainerEnd);
function fadeContainerEnd() {
container.removeEventListener('webkitTransitionEnd', fadeContainerEnd);
swap.classList.add('in');
swap.addEventListener('webkitTransitionEnd', fadeSwapEnd);
}
function fadeSwapEnd () {
swap.removeEventListener('webkitTransitionEnd', fadeSwapEnd);
container.parentNode.removeChild(container);
swap.classList.remove('fade');
swap.classList.remove('in');
complete && complete();
}
}
if (/slide/.test(transition)) {
@ -322,14 +341,6 @@
container.classList.add(containerDirection);
swap.classList.remove(swapDirection);
swap.addEventListener('webkitTransitionEnd', slideEnd);
function slideEnd() {
swap.removeEventListener('webkitTransitionEnd', slideEnd);
swap.classList.remove('slide');
swap.classList.remove(swapDirection);
container.parentNode.removeChild(container);
complete && complete();
}
}
};
@ -363,7 +374,9 @@
var i;
var result = {};
for (i in obj) result[i] = obj[i];
for (i = 0; i < obj.length; i += 1) {
result[i] = obj[i];
}
Object.keys(bars).forEach(function (key) {
var el = dom.querySelector(bars[key]);
@ -377,14 +390,16 @@
};
var parseXHR = function (xhr, options) {
var head;
var body;
var data = {};
var responseText = xhr.responseText;
var head,
body,
data = {},
responseText = xhr.responseText;
data.url = options.url;
if (!responseText) return data;
if (!responseText) {
return data;
}
if (/<html/i.test(responseText)) {
head = document.createElement('div');
@ -399,8 +414,11 @@
data.title = head.querySelector('title');
data.title = data.title && data.title.innerText.trim();
if (options.transition) data = extendWithDom(data, '.content', body);
else data.contents = body;
if (options.transition) {
data = extendWithDom(data, '.content', body);
} else {
data.contents = body;
}
return data;
};

39
lib/js/segmented-controllers.js

@ -5,6 +5,7 @@
* ---------------------------------- */
!function () {
"use strict";
var getTarget = function (target) {
var i, popovers = document.querySelectorAll('.segmented-controller li a');
for (; target && target !== document; target = target.parentNode) {
@ -13,34 +14,44 @@
};
window.addEventListener("touchend", function (e) {
var activeTab;
var activeBody;
var targetBody;
var targetTab;
var className = 'active';
var classSelector = '.' + className;
var targetAnchor = getTarget(e.target);
if (!targetAnchor) return;
var activeTab,
activeBody,
targetBody,
targetTab,
className = 'active',
classSelector = '.' + className,
targetAnchor = getTarget(e.target);
if (!targetAnchor) {
return;
}
targetTab = targetAnchor.parentNode;
activeTab = targetTab.parentNode.querySelector(classSelector);
if (activeTab) activeTab.classList.remove(className);
if (activeTab) {
activeTab.classList.remove(className);
}
targetTab.classList.add(className);
if (!targetAnchor.hash) return;
if (!targetAnchor.hash) {
return;
}
targetBody = document.querySelector(targetAnchor.hash);
if (!targetBody) return;
if (!targetBody) {
return;
}
activeBody = targetBody.parentNode.querySelector(classSelector);
if (activeBody) activeBody.classList.remove(className);
if (activeBody) {
activeBody.classList.remove(className);
}
targetBody.classList.add(className)
targetBody.classList.add(className);
});
window.addEventListener('click', function (e) { if (getTarget(e.target)) e.preventDefault(); });

28
lib/js/sliders.js

@ -6,20 +6,20 @@
* ---------------------------------- */
!function () {
var pageX;
var pageY;
var slider;
var deltaX;
var deltaY;
var offsetX;
var lastSlide;
var startTime;
var resistance;
var sliderWidth;
var slideNumber;
var isScrolling;
var scrollableArea;
"use strict";
var pageX,
pageY,
slider,
deltaX,
deltaY,
offsetX,
lastSlide,
startTime,
resistance,
sliderWidth,
slideNumber,
isScrolling,
scrollableArea;
var getSlider = function (target) {
var i, sliders = document.querySelectorAll('.slider ul');

59
lib/js/toggles.js

@ -6,10 +6,10 @@
!function () {
var start = {};
var touchMove = false;
var distanceX = false;
var toggle = false;
var start = {},
touchMove = false,
distanceX = false,
toggle = false;
var findToggle = function (target) {
var i, toggles = document.querySelectorAll('.toggle');
@ -23,12 +23,14 @@
toggle = findToggle(e.target);
if (!toggle) return;
if (!toggle) {
return;
}
var handle = toggle.querySelector('.toggle-handle');
var toggleWidth = toggle.offsetWidth;
var handleWidth = handle.offsetWidth;
var offset = toggle.classList.contains('active') ? toggleWidth - handleWidth : 0;
var handle = toggle.querySelector('.toggle-handle'),
toggleWidth = toggle.offsetWidth,
handleWidth = handle.offsetWidth,
offset = toggle.classList.contains('active') ? (toggleWidth - handleWidth) : 0;
start = { pageX : e.touches[0].pageX - offset, pageY : e.touches[0].pageY };
touchMove = false;
@ -44,21 +46,27 @@
if (!toggle) return;
var handle = toggle.querySelector('.toggle-handle');
var current = e.touches[0];
var toggleWidth = toggle.offsetWidth;
var handleWidth = handle.offsetWidth;
var offset = toggleWidth - handleWidth;
var handle = toggle.querySelector('.toggle-handle'),
current = e.touches[0],
toggleWidth = toggle.offsetWidth,
handleWidth = handle.offsetWidth,
offset = toggleWidth - handleWidth;
touchMove = true;
distanceX = current.pageX - start.pageX;
if (Math.abs(distanceX) < Math.abs(current.pageY - start.pageY)) return;
if (Math.abs(distanceX) < Math.abs(current.pageY - start.pageY)) {
return;
}
e.preventDefault();
if (distanceX < 0) return handle.style.webkitTransform = 'translate3d(0,0,0)';
if (distanceX > offset) return handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
if (distanceX < 0) {
return handle.style.webkitTransform = 'translate3d(0,0,0)';
}
if (distanceX > offset) {
return handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
}
handle.style.webkitTransform = 'translate3d(' + distanceX + 'px,0,0)';
@ -68,14 +76,17 @@
window.addEventListener('touchend', function (e) {
if (!toggle) return;
var handle = toggle.querySelector('.toggle-handle');
var toggleWidth = toggle.offsetWidth;
var handleWidth = handle.offsetWidth;
var offset = toggleWidth - handleWidth;
var slideOn = (!touchMove && !toggle.classList.contains('active')) || (touchMove && (distanceX > (toggleWidth/2 - handleWidth/2)));
var handle = toggle.querySelector('.toggle-handle'),
toggleWidth = toggle.offsetWidth,
handleWidth = handle.offsetWidth,
offset = toggleWidth - handleWidth,
slideOn = (!touchMove && !toggle.classList.contains('active')) || (touchMove && (distanceX > (toggleWidth/2 - handleWidth/2)));
if (slideOn) handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
else handle.style.webkitTransform = 'translate3d(0,0,0)';
if (slideOn) {
handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
} else {
handle.style.webkitTransform = 'translate3d(0,0,0)';
}
toggle.classList[slideOn ? 'add' : 'remove']('active');

Loading…
Cancel
Save