1.Added methods: shower.isSlideMode() & shower.toggleMode(); 2. All with hash and body.classList are incapsulated into shower.enterSlideMode() and shower.enterListMode() (no more copypaste code in eventListener).

This commit is contained in:
miripiruni 2013-01-18 21:01:10 +04:00
parent 031f4b4fda
commit c4edbb3ee9
1 changed files with 73 additions and 70 deletions

143
shower.js
View File

@ -135,27 +135,32 @@ window.shower = (function(window, document, undefined) {
e.preventDefault(); e.preventDefault();
window.open(e.target.getAttribute('href')); window.open(e.target.getAttribute('href'));
return; return false;
} }
var slideId = shower._getContainingSlideId(e.target); var slideId = shower._getContainingSlideId(e.target),
i = slideList.length - 1,
slideNumber;
if ('' !== slideId && shower.isListMode()) { if (slideId === '') {
return false;
}
for (; i >= 0; --i) {
if (slideId === slideList[i].id) {
slideNumber = i;
break;
}
}
shower.go(slideNumber);
if (shower.isListMode()) {
e.preventDefault(); e.preventDefault();
// NOTE: we should update hash to get things work properly
url.hash = '#' + slideId;
if (isHistoryApiSupported) {
history.replaceState(null, null, url.pathname + '?full#' + slideId);
}
shower.enterSlideMode(); shower.enterSlideMode();
shower.updateProgress(shower.getCurrentSlideNumber());
shower.updateCurrentAndPassedSlides(shower.getCurrentSlideNumber());
shower.runSlideshowIfPresented(shower.getCurrentSlideNumber());
} }
return; return true;
}; };
/** /**
@ -246,9 +251,17 @@ window.shower = (function(window, document, undefined) {
* @returns {Number|Boolean} * @returns {Number|Boolean}
*/ */
shower.enterSlideMode = function(callback) { shower.enterSlideMode = function(callback) {
// check if it's already in slide mode... var currentSlideNumber = shower.getCurrentSlideNumber();
if (body.classList.contains('full')) {
return false; if (currentSlideNumber === -1) {
currentSlideNumber = 0;
}
shower.go(currentSlideNumber);
shower.showPresenterNotes(currentSlideNumber);
if (shower.isListMode() && isHistoryApiSupported) {
history.pushState(null, null, url.pathname + '?full' + shower.getSlideHash(currentSlideNumber));
} }
body.classList.remove('list'); body.classList.remove('list');
@ -258,7 +271,9 @@ window.shower = (function(window, document, undefined) {
console.clear(); console.clear();
} }
shower.showPresenterNotes(shower.getCurrentSlideNumber()); shower.updateProgress(currentSlideNumber);
shower.updateCurrentAndPassedSlides(currentSlideNumber);
shower.runSlideshowIfPresented(currentSlideNumber);
if (typeof(callback) === "function") { if (typeof(callback) === "function") {
callback(); callback();
@ -273,14 +288,17 @@ window.shower = (function(window, document, undefined) {
* @returns {Number} * @returns {Number}
*/ */
shower.enterListMode = function(callback) { shower.enterListMode = function(callback) {
// check if it's already in list mode... var currentSlideNumber = shower.getCurrentSlideNumber();
if (body.classList.contains('list')) {
return false;
}
body.classList.remove('full'); body.classList.remove('full');
body.classList.add('list'); body.classList.add('list');
if (shower.isSlideMode() && isHistoryApiSupported) {
history.pushState(null, null, url.pathname + shower.getSlideHash(currentSlideNumber));
}
shower.scrollToSlide(currentSlideNumber);
if (typeof(callback) === "function") { if (typeof(callback) === "function") {
callback(); callback();
} }
@ -288,7 +306,23 @@ window.shower = (function(window, document, undefined) {
return shower._applyTransform('none'); return shower._applyTransform('none');
}; };
// @TODO: add method shower.toggleMode() /**
* Toggle Mode: Slide and List
* @param {Function} callback
*/
shower.toggleMode = function(callback) {
if (shower.isListMode()) {
shower.enterSlideMode();
} else {
shower.enterListMode();
}
if (typeof(callback) === "function") {
callback();
}
return true;
};
/** /**
* Get current slide number. Starts from zero. Warning: when in url you have * Get current slide number. Starts from zero. Warning: when in url you have
@ -324,7 +358,7 @@ window.shower = (function(window, document, undefined) {
throw new Error('Gimme slide number as Number, baby!'); throw new Error('Gimme slide number as Number, baby!');
} }
if ( ! shower.isListMode()) { if (shower.isSlideMode()) {
throw new Error('You can\'t scroll to because you in slide mode. Please, switch to list mode.'); throw new Error('You can\'t scroll to because you in slide mode. Please, switch to list mode.');
} }
@ -345,13 +379,21 @@ window.shower = (function(window, document, undefined) {
}; };
/** /**
* Chech if it's list mode. * Check if it's List mode.
* @returns {Boolean} * @returns {Boolean}
*/ */
shower.isListMode = function() { shower.isListMode = function() {
return isHistoryApiSupported ? ! /^full.*/.test(url.search.substr(1)) : body.classList.contains('list'); return isHistoryApiSupported ? ! /^full.*/.test(url.search.substr(1)) : body.classList.contains('list');
}; };
/**
* Check if it's Slide mode.
* @returns {Boolean}
*/
shower.isSlideMode = function() {
return isHistoryApiSupported ? /^full.*/.test(url.search.substr(1)) : body.classList.contains('full');
};
/** /**
* Update progress bar. * Update progress bar.
* @param {Number} slideNumber slide number (sic!) * @param {Number} slideNumber slide number (sic!)
@ -459,7 +501,7 @@ window.shower = (function(window, document, undefined) {
url.hash = shower.getSlideHash(slideNumber); url.hash = shower.getSlideHash(slideNumber);
if ( ! shower.isListMode()) { if (shower.isSlideMode()) {
shower.updateProgress(slideNumber); shower.updateProgress(slideNumber);
shower.updateCurrentAndPassedSlides(slideNumber); shower.updateCurrentAndPassedSlides(slideNumber);
shower.showPresenterNotes(slideNumber); shower.showPresenterNotes(slideNumber);
@ -534,32 +576,21 @@ window.shower = (function(window, document, undefined) {
// Event handlers // Event handlers
window.addEventListener('DOMContentLoaded', function() { window.addEventListener('DOMContentLoaded', function() {
if ( ! shower.isListMode()) { if (shower.isSlideMode()) {
// "?full" is present without slide hash, so we should display first slide
if (-1 === shower.getCurrentSlideNumber()) {
if (isHistoryApiSupported) {
history.replaceState(null, null, url.pathname + '?full' + shower.getSlideHash(0));
}
}
shower.enterSlideMode(); shower.enterSlideMode();
shower.updateProgress(shower.getCurrentSlideNumber());
shower.updateCurrentAndPassedSlides(shower.getCurrentSlideNumber());
shower.runSlideshowIfPresented(shower.getCurrentSlideNumber());
} }
}, false); }, false);
window.addEventListener('popstate', function(e) { window.addEventListener('popstate', function(e) {
if (shower.isListMode()) { if (shower.isListMode()) {
shower.enterListMode(); shower.enterListMode();
shower.scrollToSlide(shower.getCurrentSlideNumber());
} else { } else {
shower.enterSlideMode(); shower.enterSlideMode();
} }
}, false); }, false);
window.addEventListener('resize', function(e) { window.addEventListener('resize', function(e) {
if ( ! shower.isListMode()) { if (shower.isSlideMode()) {
shower._applyTransform(shower._getTransform()); shower._applyTransform(shower._getTransform());
} }
}, false); }, false);
@ -579,48 +610,23 @@ window.shower = (function(window, document, undefined) {
var slideNumber = e.shiftKey ? currentSlideNumber : 0; var slideNumber = e.shiftKey ? currentSlideNumber : 0;
shower.go(slideNumber); shower.go(slideNumber);
if (isHistoryApiSupported) {
history.pushState(null, null, url.pathname + '?full' + shower.getSlideHash(slideNumber));
}
shower.enterSlideMode(); shower.enterSlideMode();
shower.updateProgress(slideNumber);
shower.updateCurrentAndPassedSlides(slideNumber);
shower.runSlideshowIfPresented(slideNumber);
} else { } else {
if (isHistoryApiSupported) {
history.pushState(null, null, url.pathname + shower.getSlideHash(currentSlideNumber));
}
shower.enterListMode(); shower.enterListMode();
shower.scrollToSlide(currentSlideNumber);
} }
break; break;
case 13: // Enter case 13: // Enter
if (shower.isListMode() && -1 !== currentSlideNumber) { if (shower.isListMode() && -1 !== currentSlideNumber) {
e.preventDefault(); e.preventDefault();
if (isHistoryApiSupported) {
history.pushState(null, null, url.pathname + '?full' + shower.getSlideHash(currentSlideNumber));
}
shower.enterSlideMode(); shower.enterSlideMode();
shower.updateProgress(currentSlideNumber);
shower.updateCurrentAndPassedSlides(currentSlideNumber);
shower.runSlideshowIfPresented(currentSlideNumber);
} }
break; break;
case 27: // Esc case 27: // Esc
if ( ! shower.isListMode()) { if (shower.isSlideMode()) {
e.preventDefault(); e.preventDefault();
if (isHistoryApiSupported) {
history.pushState(null, null, url.pathname + shower.getSlideHash(currentSlideNumber));
}
shower.enterListMode(); shower.enterListMode();
shower.scrollToSlide(currentSlideNumber);
} }
break; break;
@ -640,7 +646,7 @@ window.shower = (function(window, document, undefined) {
case 74: // j case 74: // j
e.preventDefault(); e.preventDefault();
if ( ! shower.isListMode()) { if (shower.isSlideMode()) {
// Inner navigation is "completed" if current slide have // Inner navigation is "completed" if current slide have
// no inner navigation or inner navigation is fully shown // no inner navigation or inner navigation is fully shown
innerNavigationCompleted = !slideList[currentSlideNumber].hasInnerNavigation || innerNavigationCompleted = !slideList[currentSlideNumber].hasInnerNavigation ||
@ -657,7 +663,7 @@ window.shower = (function(window, document, undefined) {
currentSlideNumber++; currentSlideNumber++;
shower.go(currentSlideNumber); shower.go(currentSlideNumber);
// We must run slideshow only in full mode // We must run slideshow only in full mode
if ( ! shower.isListMode()) { if (shower.isSlideMode()) {
shower.runSlideshowIfPresented(currentSlideNumber); shower.runSlideshowIfPresented(currentSlideNumber);
} }
} }
@ -665,20 +671,17 @@ window.shower = (function(window, document, undefined) {
case 36: // Home case 36: // Home
e.preventDefault(); e.preventDefault();
shower.first(); shower.first();
break; break;
case 35: // End case 35: // End
e.preventDefault(); e.preventDefault();
shower.last(); shower.last();
break; break;
case 9: // Tab = +1; Shift + Tab = -1 case 9: // Tab = +1; Shift + Tab = -1
case 32: // Space = +1; Shift + Space = -1 case 32: // Space = +1; Shift + Space = -1
e.preventDefault(); e.preventDefault();
shower[e.shiftKey ? 'previous' : 'next'](); shower[e.shiftKey ? 'previous' : 'next']();
break; break;