Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Version 1.1.2
  • Loading branch information
Brandon committed Sep 3, 2016
1 parent 46a9786 commit ae5ff95
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 116 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets" : ["es2015"]
}
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@

# node
node_modules
npm-debug.log
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -42,6 +42,9 @@ Goes backward or forward to reach a tag.

# Changes

### 1.1.2
- Fix babel compiling

### 1.1.1
- Added deleteTag method

Expand Down
285 changes: 171 additions & 114 deletions index.js
@@ -1,140 +1,197 @@
const SESSIONSTORAGE_PREFIX = 'BackForwardHistory_';
'use strict';

class BackForwardHistory {
constructor() {
this.loadStateFromSession();
}

listenTo(browserHistory) {
this.browserHistory = browserHistory;
browserHistory.listen(this.onHistoryEvent.bind(this));
}

get firstLocationKey() {
return this.historyStack[0].key;
}

get currentLocationKey() {
return this.historyStack[this.currentIndex].key;
}
Object.defineProperty(exports, "__esModule", {
value: true
});

canGoBack(n = 1) {
return this.currentIndex >= n;
}

canGoForward(n = 1) {
return this.currentIndex < this.historyStack.length - n;
}

get currentlyOnFirstPage() {
return this.currentLocationKey == this.firstLocationKey;
}
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; }; }();

get currentlyOnLastPage() {
return !this.canGoForward();
}
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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')) || {};
}
var SESSIONSTORAGE_PREFIX = 'BackForwardHistory_';

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));
}
var BackForwardHistory = function () {
function BackForwardHistory() {
_classCallCheck(this, BackForwardHistory);

resetState() {
this.historyStack = [];
this.currentIndex = 0;
this.tags = {};
this.saveStateToSession();
this.loadStateFromSession();
}

onHistoryEvent({ pathname, key, action }) {
if (this.historyStack.length === 0) {
this.historyStack.push({ key, pathname });
return;
_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];

if (action === 'PUSH') {
if (!this.currentlyOnLastPage) {
this.historyStack = this.historyStack.splice(0, this.currentIndex + 1);
}

this.historyStack.push({ key, pathname });
this.currentIndex++;
return this.currentIndex >= n;
}
}, {
key: 'canGoForward',
value: function canGoForward() {
var n = arguments.length <= 0 || arguments[0] === undefined ? 1 : arguments[0];

if (action === 'POP') {
const potentialIndex = this.getIndexForKey(key);
if (potentialIndex === -1) {
// User hit go on url bar. Reset history stack.
this.resetState();
this.historyStack = [{ key, pathname }];
}

this.currentIndex = this.getIndexForKey(key);
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;
}

this.saveStateToSession();
}

getIndexForKey(key) {
return this.historyStack.findIndex(i => i.key === key);
}

getIndexForTag(tag) {
return this.getIndexForKey(this.tags[tag]);
}

getKeyForTag(tag) {
return this.tags[tag];
}

getCurrentPathname(n = 0) {
return this.historyStack[this.currentIndex + n].pathname;
}
if (action === 'PUSH') {
if (!this.currentlyOnLastPage) {
this.historyStack = this.historyStack.splice(0, this.currentIndex + 1);
}

getCurrentLocationTitle(n = 0) {
return this.historyStack[this.currentIndex + n].title;
}
this.historyStack.push({ key: key, pathname: pathname });
this.currentIndex++;
}

getTitleForKey(key) {
return this.historyStack[this.getIndexForKey(key)].title;
}
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 }];
}

setTitleForKey(key, title) {
this.historyStack[this.getIndexForKey(key)].title = title;
}
this.currentIndex = this.getIndexForKey(key);
}

setTitleForCurrentLocation(title, n = 0) {
this.historyStack[this.currentIndex + n].title = title;
}
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];

setTagForKey(key, tag) {
this.tags[tag] = key;
}
return this.historyStack[this.currentIndex + n].pathname;
}
}, {
key: 'getCurrentLocationTitle',
value: function getCurrentLocationTitle() {
var n = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];

deleteTagForKey(tag) {
delete this.tags[tag];
}
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;
}
}, {
key: 'setTitleForCurrentLocation',
value: function setTitleForCurrentLocation(title) {
var n = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];

isCurrentlyOnTag(tag) {
return this.currentLocationKey == this.tags[tag];
}
this.historyStack[this.currentIndex + n].title = title;
}
}, {
key: 'setTagForKey',
value: function setTagForKey(key, tag) {
this.tags[tag] = key;
}
}, {
key: 'deleteTagForKey',
value: function deleteTagForKey(tag) {
delete this.tags[tag];
}
}, {
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);
}

goToLocationWithTag(tag, n = 0) {
const 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();
}
}]);

this.browserHistory.go(indexOfTag - this.currentIndex + n);
}
}
return BackForwardHistory;
}();

export default new BackForwardHistory();
exports.default = new BackForwardHistory();

5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "back-forward-history",
"version": "1.1.1",
"version": "1.1.2",
"description": "This is an addon to the history JavaScript library. It enables tracking of back and forward locations.",
"main": "index.js",
"scripts": {
Expand All @@ -19,6 +19,7 @@
"react-router"
],
"devDependencies": {
"babel-cli": "^6.14.0"
"babel-cli": "^6.14.0",
"babel-preset-es2015": "^6.14.0"
}
}

0 comments on commit ae5ff95

Please sign in to comment.