Fixed problems with touch devices; Small refactoring.

This commit is contained in:
miripiruni 2013-01-20 00:47:54 +04:00
parent c4edbb3ee9
commit ce37304006
1 changed files with 48 additions and 37 deletions

View File

@ -104,12 +104,12 @@ window.shower = (function(window, document, undefined) {
}; };
/** /**
* Get containing slide id. * Get slide id from HTML element.
* @private * @private
* @param {domElem} el * @param {domElem} el
* @returns {String} * @returns {String}
*/ */
shower._getContainingSlideId = function(el) { shower._getSlideIdByEl = function(el) {
while ('BODY' !== el.nodeName && 'HTML' !== el.nodeName) { while ('BODY' !== el.nodeName && 'HTML' !== el.nodeName) {
if (el.classList.contains('slide')) { if (el.classList.contains('slide')) {
return el.id; return el.id;
@ -122,28 +122,33 @@ window.shower = (function(window, document, undefined) {
}; };
/** /**
* Dispatch single slide mode. * For touch devices: check if click on links...
* @TODO: Renaming needed? Or just some handlers rewriting? *
* @TODO: add support for textareas/inputs/...
*
* @private * @private
* @param {domElem} e * @param {domElem} e
* @returns {Undefined} * @returns {Boolean}
*/ */
shower._dispatchSingleSlideMode = function(e) { shower._checkInteractiveElement = function(e) {
// Process links
// @TODO: presentation links support
if ('A' === e.target.nodeName) { if ('A' === e.target.nodeName) {
e.preventDefault(); return true;
} else {
window.open(e.target.getAttribute('href'));
return false; return false;
} }
};
var slideId = shower._getContainingSlideId(e.target), /**
i = slideList.length - 1, * Get slide number by slideId
* @param {Number} slideId (HTML id or position in slideList)
* @returns {Number}
*/
shower.getSlideNumber = function(slideId) {
var i = slideList.length - 1,
slideNumber; slideNumber;
if (slideId === '') { if (slideId === '') {
return false; slideNumber = 0;
} }
for (; i >= 0; --i) { for (; i >= 0; --i) {
@ -153,16 +158,10 @@ window.shower = (function(window, document, undefined) {
} }
} }
shower.go(slideNumber); return slideNumber;
if (shower.isListMode()) {
e.preventDefault();
shower.enterSlideMode();
}
return true;
}; };
/** /**
* Show next slide. If slide is last returns false, otherwise return slide * Show next slide. If slide is last returns false, otherwise return slide
* number which been shown. * number which been shown.
@ -690,25 +689,37 @@ window.shower = (function(window, document, undefined) {
} }
}, false); }, false);
document.addEventListener('click', shower._dispatchSingleSlideMode, false); document.addEventListener('click', function(e) {
document.addEventListener('touchend', shower._dispatchSingleSlideMode, false); if (shower.isListMode() && shower._getSlideIdByEl(e.target)) {
e.preventDefault();
document.addEventListener('touchstart', function(e) { shower.go(shower.getSlideNumber(shower._getSlideIdByEl(e.target)));
if ( ! shower.isListMode()) { shower.enterSlideMode();
var currentSlideNumber = shower.getCurrentSlideNumber(),
x = e.touches[0].pageX;
if (x > window.innerWidth / 2) {
currentSlideNumber++;
} else {
currentSlideNumber--;
}
shower.go(currentSlideNumber);
} }
}, false); }, false);
document.addEventListener('touchstart', function(e) {
if (shower._getSlideIdByEl(e.target)) {
if (shower.isSlideMode() && ! shower._checkInteractiveElement(e)) {
var x = e.touches[0].pageX;
if (x > window.innerWidth / 2) {
shower.next();
} else {
shower.previous();
}
}
if (shower.isListMode()) {
shower.go(shower.getSlideNumber(shower._getSlideIdByEl(e.target)));
shower.enterSlideMode();
}
}
}, false);
document.addEventListener('touchmove', function(e) { document.addEventListener('touchmove', function(e) {
if ( ! shower.isListMode()) { if (shower.isSlideMode()) {
e.preventDefault(); e.preventDefault();
} }
}, false); }, false);