Skip to content

Commit

Permalink
Merge pull request youtube#302 from nicksay/nav-state-keys
Browse files Browse the repository at this point in the history
Use consistently named state keys for navigation event listeners.
  • Loading branch information
nicksay committed Feb 27, 2015
2 parents 2102d95 + bdde825 commit 2a535ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
45 changes: 26 additions & 19 deletions src/client/nav/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,25 @@ goog.require('spf.url');
* Initializes (enables) pushState navigation.
*/
spf.nav.init = function() {
// Initialize history management.
spf.history.init(spf.nav.handleHistory_, spf.nav.dispatchError_);
if (!spf.state.get(spf.state.Key.NAV_INIT) && document.addEventListener) {
document.addEventListener('click', spf.nav.handleClick_, false);
if (spf.config.get('experimental-prefetch-mousedown') &&
!spf.nav.isTouchCapablePlatform_()) {
document.addEventListener('mousedown', spf.nav.handleMouseDown_, false);
spf.state.set(spf.state.Key.PREFETCH_LISTENER, spf.nav.handleMouseDown_);
}
spf.state.set(spf.state.Key.NAV_INIT, true);
spf.state.set(spf.state.Key.NAV_INIT_TIME, spf.now());
spf.state.set(spf.state.Key.NAV_COUNTER, 0);
spf.state.set(spf.state.Key.NAV_LISTENER, spf.nav.handleClick_);
// If already initialized, or running in an unsupported environment, return.
if (spf.state.get(spf.state.Key.NAV_INIT) || !document.addEventListener) {
return;
}
// Set some basic state.
spf.state.set(spf.state.Key.NAV_INIT, true);
spf.state.set(spf.state.Key.NAV_INIT_TIME, spf.now());
spf.state.set(spf.state.Key.NAV_COUNTER, 0);
// Handle clicks for navigating when a spf-link element click happens.
document.addEventListener('click', spf.nav.handleClick_, false);
spf.state.set(spf.state.Key.NAV_CLICK_LISTENER, spf.nav.handleClick_);
// Handle mousedowns for prefetching when a spf-link element click starts.
if (spf.config.get('experimental-prefetch-mousedown') &&
!spf.nav.isTouchCapablePlatform_()) {
document.addEventListener('mousedown', spf.nav.handleMouseDown_, false);
spf.state.set(spf.state.Key.NAV_MOUSEDOWN_LISTENER,
spf.nav.handleMouseDown_);
}
};

Expand All @@ -57,18 +64,18 @@ spf.nav.dispose = function() {
spf.nav.cancel();
if (spf.state.get(spf.state.Key.NAV_INIT)) {
if (document.removeEventListener) {
document.removeEventListener('click', /** @type {function(Event)} */ (
spf.state.get(spf.state.Key.NAV_LISTENER)), false);
if (spf.config.get('experimental-prefetch-mousedown')) {
document.removeEventListener('mousedown',
/** @type {function(Event)} */ (
spf.state.get(spf.state.Key.PREFETCH_LISTENER)), false);
}
var handleClick = /** @type {function(Event)} */ (
spf.state.get(spf.state.Key.NAV_CLICK_LISTENER));
document.removeEventListener('click', handleClick, false);
var handleMouseDown = /** @type {function(Event)} */ (
spf.state.get(spf.state.Key.NAV_MOUSEDOWN_LISTENER));
document.removeEventListener('mousedown', handleMouseDown, false);
}
spf.state.set(spf.state.Key.NAV_CLICK_LISTENER, null);
spf.state.set(spf.state.Key.NAV_MOUSEDOWN_LISTENER, null);
spf.state.set(spf.state.Key.NAV_INIT, false);
spf.state.set(spf.state.Key.NAV_INIT_TIME, null);
spf.state.set(spf.state.Key.NAV_COUNTER, null);
spf.state.set(spf.state.Key.NAV_LISTENER, null);
}
spf.history.dispose();
};
Expand Down
4 changes: 2 additions & 2 deletions src/client/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ spf.state.Key = {
NAV_COUNTER: 'nav-counter',
NAV_INIT: 'nav-init',
NAV_INIT_TIME: 'nav-init-time',
NAV_LISTENER: 'nav-listener',
NAV_CLICK_LISTENER: 'nav-listener',
NAV_MOUSEDOWN_LISTENER: 'nav-mousedown-listener',
NAV_PREFETCHES: 'nav-prefetches',
NAV_PROMOTE: 'nav-promote',
NAV_PROMOTE_TIME: 'nav-promote-time',
NAV_REQUEST: 'nav-request',
PREFETCH_LISTENER: 'prefetch-listener',
PUBSUB_SUBS: 'ps-s',
RESOURCE_NAME: 'rsrc-n',
RESOURCE_PATHS_PREFIX: 'rsrc-p-',
Expand Down

0 comments on commit 2a535ed

Please sign in to comment.