Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
BackForwardHistory/index.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
201 lines (178 sloc)
6.3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
Object.defineProperty(exports, "__esModule", { | |
value: true | |
}); | |
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
var SESSIONSTORAGE_PREFIX = 'BackForwardHistory_'; | |
var BackForwardHistory = function () { | |
function BackForwardHistory() { | |
_classCallCheck(this, BackForwardHistory); | |
this.loadStateFromSession(); | |
} | |
_createClass(BackForwardHistory, [{ | |
key: 'listenTo', | |
value: function listenTo(browserHistory) { | |
this.browserHistory = browserHistory; | |
browserHistory.listen(this.onHistoryEvent.bind(this)); | |
} | |
}, { | |
key: 'canGoBack', | |
value: function canGoBack() { | |
var n = arguments.length <= 0 || arguments[0] === undefined ? 1 : arguments[0]; | |
return this.currentIndex >= n; | |
} | |
}, { | |
key: 'canGoForward', | |
value: function canGoForward() { | |
var n = arguments.length <= 0 || arguments[0] === undefined ? 1 : arguments[0]; | |
return this.currentIndex < this.historyStack.length - n; | |
} | |
}, { | |
key: 'loadStateFromSession', | |
value: function loadStateFromSession() { | |
this.historyStack = JSON.parse(sessionStorage.getItem(SESSIONSTORAGE_PREFIX + 'historyStack')) || []; | |
this.currentIndex = sessionStorage.getItem(SESSIONSTORAGE_PREFIX + 'currentIndex') || 0; | |
this.tags = JSON.parse(sessionStorage.getItem(SESSIONSTORAGE_PREFIX + 'tags')) || {}; | |
} | |
}, { | |
key: 'saveStateToSession', | |
value: function saveStateToSession() { | |
sessionStorage.setItem(SESSIONSTORAGE_PREFIX + 'historyStack', JSON.stringify(this.historyStack)); | |
sessionStorage.setItem(SESSIONSTORAGE_PREFIX + 'currentIndex', this.currentIndex); | |
sessionStorage.setItem(SESSIONSTORAGE_PREFIX + 'tags', JSON.stringify(this.tags)); | |
} | |
}, { | |
key: 'resetState', | |
value: function resetState() { | |
this.historyStack = []; | |
this.currentIndex = 0; | |
this.tags = {}; | |
this.saveStateToSession(); | |
} | |
}, { | |
key: 'onHistoryEvent', | |
value: function onHistoryEvent(_ref) { | |
var pathname = _ref.pathname; | |
var key = _ref.key; | |
var action = _ref.action; | |
if (this.historyStack.length === 0) { | |
this.historyStack.push({ key: key, pathname: pathname }); | |
return; | |
} | |
if (action === 'PUSH') { | |
if (!this.currentlyOnLastPage) { | |
this.historyStack = this.historyStack.splice(0, this.currentIndex + 1); | |
} | |
this.historyStack.push({ key: key, pathname: pathname }); | |
this.currentIndex++; | |
} | |
if (action === 'POP') { | |
var potentialIndex = this.getIndexForKey(key); | |
if (potentialIndex === -1) { | |
// User hit go on url bar. Reset history stack. | |
this.resetState(); | |
this.historyStack = [{ key: key, pathname: pathname }]; | |
} | |
this.currentIndex = this.getIndexForKey(key); | |
} | |
this.saveStateToSession(); | |
} | |
}, { | |
key: 'getIndexForKey', | |
value: function getIndexForKey(key) { | |
return this.historyStack.findIndex(function (i) { | |
return i.key === key; | |
}); | |
} | |
}, { | |
key: 'getIndexForTag', | |
value: function getIndexForTag(tag) { | |
return this.getIndexForKey(this.tags[tag]); | |
} | |
}, { | |
key: 'getKeyForTag', | |
value: function getKeyForTag(tag) { | |
return this.tags[tag]; | |
} | |
}, { | |
key: 'getCurrentPathname', | |
value: function getCurrentPathname() { | |
var n = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0]; | |
return this.historyStack[this.currentIndex + n].pathname; | |
} | |
}, { | |
key: 'getCurrentLocationTitle', | |
value: function getCurrentLocationTitle() { | |
var n = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0]; | |
return this.historyStack[this.currentIndex + n].title; | |
} | |
}, { | |
key: 'getTitleForKey', | |
value: function getTitleForKey(key) { | |
return this.historyStack[this.getIndexForKey(key)].title; | |
} | |
}, { | |
key: 'setTitleForKey', | |
value: function setTitleForKey(key, title) { | |
this.historyStack[this.getIndexForKey(key)].title = title; | |
this.saveStateToSession(); | |
} | |
}, { | |
key: 'setTitleForCurrentLocation', | |
value: function setTitleForCurrentLocation(title) { | |
var n = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; | |
this.historyStack[this.currentIndex + n].title = title; | |
this.saveStateToSession(); | |
} | |
}, { | |
key: 'setTagForKey', | |
value: function setTagForKey(key, tag) { | |
this.tags[tag] = key; | |
this.saveStateToSession(); | |
} | |
}, { | |
key: 'deleteTag', | |
value: function deleteTag(tag) { | |
delete this.tags[tag]; | |
this.saveStateToSession(); | |
} | |
}, { | |
key: 'isCurrentlyOnTag', | |
value: function isCurrentlyOnTag(tag) { | |
return this.currentLocationKey == this.tags[tag]; | |
} | |
}, { | |
key: 'goToLocationWithTag', | |
value: function goToLocationWithTag(tag) { | |
var n = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; | |
var indexOfTag = this.getIndexForTag(tag); | |
if (indexOfTag === -1) { | |
throw new Error('No index found for tag: ' + tag); | |
} | |
this.browserHistory.go(indexOfTag - this.currentIndex + n); | |
} | |
}, { | |
key: 'firstLocationKey', | |
get: function get() { | |
return this.historyStack[0].key; | |
} | |
}, { | |
key: 'currentLocationKey', | |
get: function get() { | |
return this.historyStack[this.currentIndex].key; | |
} | |
}, { | |
key: 'currentlyOnFirstPage', | |
get: function get() { | |
return this.currentLocationKey == this.firstLocationKey; | |
} | |
}, { | |
key: 'currentlyOnLastPage', | |
get: function get() { | |
return !this.canGoForward(); | |
} | |
}]); | |
return BackForwardHistory; | |
}(); | |
exports.default = new BackForwardHistory(); |