Browse Source

Enable `curly` rule.

pull/379/head
XhmikosR 11 years ago
parent
commit
1e88a8ad5c
  1. 14
      docs/assets/js/docs.js
  2. 52
      docs/assets/js/fingerblast.js
  3. 1
      js/.jshintrc
  4. 14
      js/modals.js
  5. 22
      js/popovers.js
  6. 117
      js/push.js
  7. 24
      js/segmented-controllers.js
  8. 22
      js/sliders.js
  9. 41
      js/toggles.js

14
docs/assets/js/docs.js

@ -53,7 +53,9 @@ $(function() {
calculateScroll(); calculateScroll();
calculateToggle(); calculateToggle();
if (!eventListeners) addEventListeners(); if (!eventListeners) {
addEventListeners();
}
}; };
var addEventListeners = function () { var addEventListeners = function () {
@ -119,7 +121,9 @@ $(function() {
var calculateScroll = function() { var calculateScroll = function() {
// if small screen don't worry about this // if small screen don't worry about this
if (windowWidth <= 768) return; if (windowWidth <= 768) {
return;
}
// Save scrollTop value // Save scrollTop value
var contentSectionItem; var contentSectionItem;
@ -136,12 +140,14 @@ $(function() {
// Injection of components into device // Injection of components into device
for (var l = contentSection.length; l--;) { for (var l = contentSection.length; l--;) {
if ((topCache[l] - currentTop) < windowHeight) { if ((topCache[l] - currentTop) < windowHeight) {
if (currentActive === l) return; if (currentActive === l) {
return;
}
currentActive = l; currentActive = l;
bod.find('.component.active').removeClass('active'); bod.find('.component.active').removeClass('active');
contentSectionItem = $(contentSection[l]); contentSectionItem = $(contentSection[l]);
contentSectionItem.addClass('active'); contentSectionItem.addClass('active');
if(contentSectionItem.attr('id')) { if (contentSectionItem.attr('id')) {
device.attr('id', contentSectionItem.attr('id') + 'InDevice'); device.attr('id', contentSectionItem.attr('id') + 'InDevice');
} else { } else {
device.attr('id', ''); device.attr('id', '');

52
docs/assets/js/fingerblast.js

@ -34,7 +34,9 @@ FingerBlast.prototype = {
return ancestor !== element && ancestor.contains(element); return ancestor !== element && ancestor.contains(element);
} else { } else {
for (descendants = ancestor.getElementsByTagName('*'), index = 0; descendant = descendants[index++];) { for (descendants = ancestor.getElementsByTagName('*'), index = 0; descendant = descendants[index++];) {
if (descendant === element) return true; if (descendant === element) {
return true;
}
} }
return false; return false;
} }
@ -42,19 +44,25 @@ FingerBlast.prototype = {
this.element.addEventListener('mouseover', function (e) { this.element.addEventListener('mouseover', function (e) {
var target = e.relatedTarget; var target = e.relatedTarget;
if (target !== this && !contains(target, this)) activate(); if (target !== this && !contains(target, this)) {
activate();
}
}); });
this.element.addEventListener('mouseout', function (e) { this.element.addEventListener('mouseout', function (e) {
var target = e.relatedTarget; var target = e.relatedTarget;
if (target !== this && !contains(target, this)) deactivate(e); if (target !== this && !contains(target, this)) {
deactivate(e);
}
}); });
}, },
activate: function () { activate: function () {
'use strict'; 'use strict';
if (this.active) return; if (this.active) {
return;
}
this.element.addEventListener('mousedown', (this.touchStart = this.touchStart.bind(this)), true); this.element.addEventListener('mousedown', (this.touchStart = this.touchStart.bind(this)), true);
this.element.addEventListener('mousemove', (this.touchMove = this.touchMove.bind(this)), true); this.element.addEventListener('mousemove', (this.touchMove = this.touchMove.bind(this)), true);
this.element.addEventListener('mouseup', (this.touchEnd = this.touchEnd.bind(this)), true); this.element.addEventListener('mouseup', (this.touchEnd = this.touchEnd.bind(this)), true);
@ -66,7 +74,9 @@ FingerBlast.prototype = {
'use strict'; 'use strict';
this.active = false; this.active = false;
if (this.mouseIsDown) this.touchEnd(e); if (this.mouseIsDown) {
this.touchEnd(e);
}
this.element.removeEventListener('mousedown', this.touchStart, true); this.element.removeEventListener('mousedown', this.touchStart, true);
this.element.removeEventListener('mousemove', this.touchMove, true); this.element.removeEventListener('mousemove', this.touchMove, true);
this.element.removeEventListener('mouseup', this.touchEnd, true); this.element.removeEventListener('mouseup', this.touchEnd, true);
@ -76,7 +86,9 @@ FingerBlast.prototype = {
click: function (e) { click: function (e) {
'use strict'; 'use strict';
if (e.synthetic) return; if (e.synthetic) {
return;
}
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
}, },
@ -84,7 +96,9 @@ FingerBlast.prototype = {
touchStart: function (e) { touchStart: function (e) {
'use strict'; 'use strict';
if (e.synthetic || /input|textarea/.test(e.target.tagName.toLowerCase())) return; if (e.synthetic || /input|textarea/.test(e.target.tagName.toLowerCase())) {
return;
}
this.mouseIsDown = true; this.mouseIsDown = true;
@ -97,20 +111,26 @@ FingerBlast.prototype = {
touchMove: function (e) { touchMove: function (e) {
'use strict'; 'use strict';
if (e.synthetic) return; if (e.synthetic) {
return;
}
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
this.move(e.clientX, e.clientY); this.move(e.clientX, e.clientY);
if (this.mouseIsDown) this.fireTouchEvents('touchmove', e); if (this.mouseIsDown) {
this.fireTouchEvents('touchmove', e);
}
}, },
touchEnd: function (e) { touchEnd: function (e) {
'use strict'; 'use strict';
if (e.synthetic) return; if (e.synthetic) {
return;
}
this.mouseIsDown = false; this.mouseIsDown = false;
@ -119,7 +139,9 @@ FingerBlast.prototype = {
this.fireTouchEvents('touchend', e); this.fireTouchEvents('touchend', e);
if (!this.target) return; if (!this.target) {
return;
}
// Mobile Safari moves all the mouse events to fire after the touchend event. // Mobile Safari moves all the mouse events to fire after the touchend event.
this.target.dispatchEvent(this.createMouseEvent('mouseover', e)); this.target.dispatchEvent(this.createMouseEvent('mouseover', e));
@ -133,7 +155,9 @@ FingerBlast.prototype = {
var events = []; var events = [];
var gestures = []; var gestures = [];
if (!this.target) return; if (!this.target) {
return;
}
// Convert 'ontouch*' properties and attributes to listeners. // Convert 'ontouch*' properties and attributes to listeners.
var onEventName = 'on' + eventName; var onEventName = 'on' + eventName;
@ -172,7 +196,9 @@ FingerBlast.prototype = {
this.startAngle = angle; this.startAngle = angle;
} }
if (eventName === 'touchend') gestureName = 'gestureend'; if (eventName === 'touchend') {
gestureName = 'gestureend';
}
events.forEach(function(event) { events.forEach(function(event) {
var gesture = this.createMouseEvent.call(event._finger, gestureName, event); var gesture = this.createMouseEvent.call(event._finger, gestureName, event);

1
js/.jshintrc

@ -2,6 +2,7 @@
"boss" : true, "boss" : true,
"browser" : true, "browser" : true,
"camelcase" : true, "camelcase" : true,
"curly" : true,
"devel" : true, "devel" : true,
"eqeqeq" : true, "eqeqeq" : true,
"eqnull" : true, "eqnull" : true,

14
js/modals.js

@ -10,19 +10,27 @@
var findModals = function (target) { var findModals = function (target) {
var i, modals = document.querySelectorAll('a'); var i, modals = document.querySelectorAll('a');
for (; target && target !== document; target = target.parentNode) { 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 getModal = function (event) {
var modalToggle = findModals(event.target); 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) { window.addEventListener('touchend', function (event) {
var modal = getModal(event); var modal = getModal(event);
if (modal) { if (modal) {
if (modal && modal.classList.contains('modal')) modal.classList.toggle('active'); if (modal && modal.classList.contains('modal')) {
modal.classList.toggle('active');
}
event.preventDefault(); // prevents rewriting url (apps can still use hash values in url) event.preventDefault(); // prevents rewriting url (apps can still use hash values in url)
} }
}); });

22
js/popovers.js

@ -12,7 +12,11 @@
var findPopovers = function (target) { var findPopovers = function (target) {
var i, popovers = document.querySelectorAll('a'); var i, popovers = document.querySelectorAll('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;
}
}
} }
}; };
@ -38,7 +42,9 @@
var getPopover = function (e) { var getPopover = function (e) {
var anchor = findPopovers(e.target); var anchor = findPopovers(e.target);
if (!anchor || !anchor.hash || (anchor.hash.indexOf('/') > 0)) return; if (!anchor || !anchor.hash || (anchor.hash.indexOf('/') > 0)) {
return;
}
try { try {
popover = document.querySelector(anchor.hash); popover = document.querySelector(anchor.hash);
@ -47,9 +53,13 @@
popover = null; popover = null;
} }
if (popover == null) return; if (popover == null) {
return;
}
if (!popover || !popover.classList.contains('popover')) return; if (!popover || !popover.classList.contains('popover')) {
return;
}
return popover; return popover;
}; };
@ -57,7 +67,9 @@
var showHidePopover = function (e) { var showHidePopover = function (e) {
var popover = getPopover(e); var popover = getPopover(e);
if (!popover) return; if (!popover) {
return;
}
popover.style.display = 'block'; popover.style.display = 'block';
popover.offsetHeight; popover.offsetHeight;

117
js/push.js

@ -35,7 +35,9 @@
var cacheReplace = function (data, updates) { var cacheReplace = function (data, updates) {
PUSH.id = data.id; PUSH.id = data.id;
if (updates) data = getCached(data.id); if (updates) {
data = getCached(data.id);
}
cacheMapping[data.id] = JSON.stringify(data); cacheMapping[data.id] = JSON.stringify(data);
window.history.replaceState(data.id, data.title, data.url); window.history.replaceState(data.id, data.title, data.url);
domCache[data.id] = document.body.cloneNode(true); domCache[data.id] = document.body.cloneNode(true);
@ -49,8 +51,12 @@
cacheBackStack.push(id); cacheBackStack.push(id);
while (cacheForwardStack.length) delete cacheMapping[cacheForwardStack.shift()]; while (cacheForwardStack.length) {
while (cacheBackStack.length > maxCacheLength) delete cacheMapping[cacheBackStack.shift()]; delete cacheMapping[cacheForwardStack.shift()];
}
while (cacheBackStack.length > maxCacheLength) {
delete cacheMapping[cacheBackStack.shift()];
}
window.history.pushState(null, '', cacheMapping[PUSH.id].url); window.history.pushState(null, '', cacheMapping[PUSH.id].url);
@ -65,7 +71,9 @@
var pushStack = forward ? cacheBackStack : cacheForwardStack; var pushStack = forward ? cacheBackStack : cacheForwardStack;
var popStack = forward ? cacheForwardStack : cacheBackStack; var popStack = forward ? cacheForwardStack : cacheBackStack;
if (PUSH.id) pushStack.push(PUSH.id); if (PUSH.id) {
pushStack.push(PUSH.id);
}
popStack.pop(); popStack.pop();
cacheMapping.cacheForwardStack = JSON.stringify(cacheForwardStack); cacheMapping.cacheForwardStack = JSON.stringify(cacheForwardStack);
@ -90,7 +98,7 @@
|| !target.hash && /#/.test(target.href) || !target.hash && /#/.test(target.href)
|| target.hash && target.href.replace(target.hash, '') === location.href.replace(location.hash, '') || target.hash && target.href.replace(target.hash, '') === location.href.replace(location.hash, '')
|| target.getAttribute('data-ignore') === 'push' || target.getAttribute('data-ignore') === 'push'
) return; ) { return; }
return target; return target;
}; };
@ -102,7 +110,9 @@
var touchend = function (e) { var touchend = function (e) {
var target = getTarget(e); var target = getTarget(e);
if (!target) return; if (!target) {
return;
}
e.preventDefault(); e.preventDefault();
@ -125,7 +135,9 @@
var transitionFromObj; var transitionFromObj;
var id = e.state; var id = e.state;
if (!id || !cacheMapping[id]) return; if (!id || !cacheMapping[id]) {
return;
}
direction = PUSH.id < id ? 'forward' : 'back'; direction = PUSH.id < id ? 'forward' : 'back';
@ -134,7 +146,9 @@
activeObj = getCached(id); activeObj = getCached(id);
activeDom = domCache[id]; activeDom = domCache[id];
if (activeObj.title) document.title = activeObj.title; if (activeObj.title) {
document.title = activeObj.title;
}
if (direction === 'back') { if (direction === 'back') {
transitionFrom = JSON.parse(direction === 'back' ? cacheMapping.cacheForwardStack : cacheMapping.cacheBackStack); transitionFrom = JSON.parse(direction === 'back' ? cacheMapping.cacheForwardStack : cacheMapping.cacheBackStack);
@ -143,7 +157,9 @@
transitionFromObj = activeObj; transitionFromObj = activeObj;
} }
if (direction === 'back' && !transitionFromObj.id) return PUSH.id = id; if (direction === 'back' && !transitionFromObj.id) {
return PUSH.id = id;
}
transition = direction === 'back' ? transitionMap[transitionFromObj.transition] : transitionFromObj.transition; transition = direction === 'back' ? transitionMap[transitionFromObj.transition] : transitionFromObj.transition;
@ -162,8 +178,11 @@
activeObj = extendWithDom(activeObj, '.content', activeDom.cloneNode(true)); activeObj = extendWithDom(activeObj, '.content', activeDom.cloneNode(true));
for (key in bars) { for (key in bars) {
barElement = document.querySelector(bars[key]); barElement = document.querySelector(bars[key]);
if (activeObj[key]) swapContent(activeObj[key], barElement); if (activeObj[key]) {
else if (barElement) barElement.parentNode.removeChild(barElement); swapContent(activeObj[key], barElement);
} else if (barElement) {
barElement.parentNode.removeChild(barElement);
}
} }
} }
@ -202,8 +221,12 @@
xhr.setRequestHeader('X-PUSH', 'true'); xhr.setRequestHeader('X-PUSH', 'true');
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (options._timeout) clearTimeout(options._timeout); if (options._timeout) {
if (xhr.readyState === 4) xhr.status === 200 ? success(xhr, options) : failure(options.url); clearTimeout(options._timeout);
}
if (xhr.readyState === 4) {
xhr.status === 200 ? success(xhr, options) : failure(options.url);
}
}; };
if (!PUSH.id) { if (!PUSH.id) {
@ -222,7 +245,9 @@
xhr.send(); xhr.send();
if (xhr.readyState && !options.ignorePush) cachePush(); if (xhr.readyState && !options.ignorePush) {
cachePush();
}
}; };
@ -234,15 +259,22 @@
var barElement; var barElement;
var data = parseXHR(xhr, options); var data = parseXHR(xhr, options);
if (!data.contents) return locationReplace(options.url); if (!data.contents) {
return locationReplace(options.url);
}
if (data.title) document.title = data.title; if (data.title) {
document.title = data.title;
}
if (options.transition) { if (options.transition) {
for (key in bars) { for (key in bars) {
barElement = document.querySelector(bars[key]); barElement = document.querySelector(bars[key]);
if (data[key]) swapContent(data[key], barElement); if (data[key]) {
else if (barElement) barElement.parentNode.removeChild(barElement); swapContent(data[key], barElement);
} else if (barElement) {
barElement.parentNode.removeChild(barElement);
}
} }
} }
@ -257,8 +289,12 @@
triggerStateChange(); triggerStateChange();
}); });
if (!options.ignorePush && window._gaq) _gaq.push(['_trackPageview']); // google analytics if (!options.ignorePush && window._gaq) {
if (!options.hash) return; _gaq.push(['_trackPageview']); // google analytics
}
if (!options.hash) {
return;
}
}; };
var failure = function (url) { var failure = function (url) {
@ -275,9 +311,13 @@
var swapDirection; var swapDirection;
if (!transition) { if (!transition) {
if (container) container.innerHTML = swap.innerHTML; if (container) {
else if (swap.classList.contains('content')) document.body.appendChild(swap); container.innerHTML = swap.innerHTML;
else document.body.insertBefore(swap, document.querySelector('.content')); } else if (swap.classList.contains('content')) {
document.body.appendChild(swap);
} else {
document.body.insertBefore(swap, document.querySelector('.content'));
}
} else { } else {
enter = /in$/.test(transition); enter = /in$/.test(transition);
@ -296,7 +336,9 @@
container.parentNode.insertBefore(swap, container); container.parentNode.insertBefore(swap, container);
} }
if (!transition) complete && complete(); if (!transition) {
complete && complete();
}
if (transition === 'fade') { if (transition === 'fade') {
container.offsetWidth; // force reflow container.offsetWidth; // force reflow
@ -348,7 +390,11 @@
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;
}
}
} }
}; };
@ -361,11 +407,15 @@
var i; var i;
var result = {}; var result = {};
for (i in obj) result[i] = obj[i]; for (i in obj) {
result[i] = obj[i];
}
Object.keys(bars).forEach(function (key) { Object.keys(bars).forEach(function (key) {
var el = dom.querySelector(bars[key]); var el = dom.querySelector(bars[key]);
if (el) el.parentNode.removeChild(el); if (el) {
el.parentNode.removeChild(el);
}
result[key] = el; result[key] = el;
}); });
@ -382,7 +432,9 @@
data.url = options.url; data.url = options.url;
if (!responseText) return data; if (!responseText) {
return data;
}
if (/<html/i.test(responseText)) { if (/<html/i.test(responseText)) {
head = document.createElement('div'); head = document.createElement('div');
@ -397,8 +449,11 @@
data.title = head.querySelector('title'); data.title = head.querySelector('title');
data.title = data.title && data.title.innerText.trim(); data.title = data.title && data.title.innerText.trim();
if (options.transition) data = extendWithDom(data, '.content', body); if (options.transition) {
else data.contents = body; data = extendWithDom(data, '.content', body);
} else {
data.contents = body;
}
return data; return data;
}; };
@ -410,7 +465,7 @@
window.addEventListener('touchstart', function () { isScrolling = false; }); window.addEventListener('touchstart', function () { isScrolling = false; });
window.addEventListener('touchmove', function () { isScrolling = true; }); 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);
window.PUSH = PUSH; window.PUSH = PUSH;

24
js/segmented-controllers.js

@ -10,7 +10,11 @@
var getTarget = function (target) { var getTarget = function (target) {
var i, segmentedControls = document.querySelectorAll('.segmented-control .control-item'); var i, segmentedControls = document.querySelectorAll('.segmented-control .control-item');
for (; target && target !== document; target = target.parentNode) { for (; target && target !== document; target = target.parentNode) {
for (i = segmentedControls.length; i--;) { if (segmentedControls[i] === target) return target; } for (i = segmentedControls.length; i--;) {
if (segmentedControls[i] === target) {
return target;
}
}
} }
}; };
@ -22,19 +26,27 @@
var className = 'active'; var className = 'active';
var classSelector = '.' + className; var classSelector = '.' + className;
if (!targetTab) return; if (!targetTab) {
return;
}
activeTab = targetTab.parentNode.querySelector(classSelector); activeTab = targetTab.parentNode.querySelector(classSelector);
if (activeTab) activeTab.classList.remove(className); if (activeTab) {
activeTab.classList.remove(className);
}
targetTab.classList.add(className); targetTab.classList.add(className);
if (!targetTab.hash) return; if (!targetTab.hash) {
return;
}
targetBody = document.querySelector(targetTab.hash); targetBody = document.querySelector(targetTab.hash);
if (!targetBody) return; if (!targetBody) {
return;
}
activeBodies = targetBody.parentNode.querySelectorAll(classSelector); activeBodies = targetBody.parentNode.querySelectorAll(classSelector);
@ -45,5 +57,5 @@
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();} });
}(); }();

22
js/sliders.js

@ -25,7 +25,11 @@
var getSlider = function (target) { var getSlider = function (target) {
var i, sliders = document.querySelectorAll('.slider > .slide-group'); var i, sliders = document.querySelectorAll('.slider > .slide-group');
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;
}
}
} }
}; };
@ -46,7 +50,9 @@
var onTouchStart = function (e) { var onTouchStart = function (e) {
slider = getSlider(e.target); slider = getSlider(e.target);
if (!slider) return; if (!slider) {
return;
}
var firstItem = slider.querySelector('.slide'); var firstItem = slider.querySelector('.slide');
@ -67,7 +73,9 @@
}; };
var onTouchMove = function (e) { var onTouchMove = function (e) {
if (e.touches.length > 1 || !slider) return; // Exit if a pinch || no slider if (e.touches.length > 1 || !slider) {
return; // Exit if a pinch || no slider
}
deltaX = e.touches[0].pageX - pageX; deltaX = e.touches[0].pageX - pageX;
deltaY = e.touches[0].pageY - pageY; deltaY = e.touches[0].pageY - pageY;
@ -78,7 +86,9 @@
isScrolling = Math.abs(deltaY) > Math.abs(deltaX); isScrolling = Math.abs(deltaY) > Math.abs(deltaX);
} }
if (isScrolling) return; if (isScrolling) {
return;
}
offsetX = (deltaX / resistance) + getScroll(); offsetX = (deltaX / resistance) + getScroll();
@ -91,7 +101,9 @@
}; };
var onTouchEnd = function (e) { var onTouchEnd = function (e) {
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

41
js/toggles.js

@ -15,7 +15,11 @@
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;
}
}
} }
}; };
@ -24,7 +28,9 @@
toggle = findToggle(e.target); toggle = findToggle(e.target);
if (!toggle) return; if (!toggle) {
return;
}
var handle = toggle.querySelector('.toggle-handle'); var handle = toggle.querySelector('.toggle-handle');
var toggleWidth = toggle.clientWidth; var toggleWidth = toggle.clientWidth;
@ -38,9 +44,13 @@
window.addEventListener('touchmove', function (e) { window.addEventListener('touchmove', function (e) {
e = e.originalEvent || e; e = e.originalEvent || e;
if (e.touches.length > 1) return; // Exit if a pinch if (e.touches.length > 1) {
return; // Exit if a pinch
}
if (!toggle) return; if (!toggle) {
return;
}
var handle = toggle.querySelector('.toggle-handle'); var handle = toggle.querySelector('.toggle-handle');
var current = e.touches[0]; var current = e.touches[0];
@ -51,12 +61,18 @@
touchMove = true; touchMove = true;
distanceX = current.pageX - start.pageX; 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(); e.preventDefault();
if (distanceX < 0) return handle.style.webkitTransform = 'translate3d(0,0,0)'; if (distanceX < 0) {
if (distanceX > offset) return handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,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)'; handle.style.webkitTransform = 'translate3d(' + distanceX + 'px,0,0)';
@ -64,7 +80,9 @@
}); });
window.addEventListener('touchend', function (e) { window.addEventListener('touchend', function (e) {
if (!toggle) return; if (!toggle) {
return;
}
var handle = toggle.querySelector('.toggle-handle'); var handle = toggle.querySelector('.toggle-handle');
var toggleWidth = toggle.clientWidth; var toggleWidth = toggle.clientWidth;
@ -72,8 +90,11 @@
var offset = (toggleWidth - handleWidth); var offset = (toggleWidth - handleWidth);
var slideOn = (!touchMove && !toggle.classList.contains('active')) || (touchMove && (distanceX > (toggleWidth/2 - handleWidth/2))); var slideOn = (!touchMove && !toggle.classList.contains('active')) || (touchMove && (distanceX > (toggleWidth/2 - handleWidth/2)));
if (slideOn) handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)'; if (slideOn) {
else handle.style.webkitTransform = 'translate3d(0,0,0)'; handle.style.webkitTransform = 'translate3d(' + offset + 'px,0,0)';
} else {
handle.style.webkitTransform = 'translate3d(0,0,0)';
}
toggle.classList[slideOn ? 'add' : 'remove']('active'); toggle.classList[slideOn ? 'add' : 'remove']('active');

Loading…
Cancel
Save