Comparing version 0.2.0 to 0.3.1
@@ -44,2 +44,3 @@ 'use strict'; | ||
var initialState = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
@@ -50,3 +51,3 @@ _classCallCheck(this, App); | ||
this.state = initialState; | ||
this.router = new _router3.default(this); | ||
this.router = new _router3.default(this, options); | ||
@@ -85,3 +86,3 @@ var screens = this.SCREENS; | ||
if (updateHash) { | ||
window.history.replaceState(null, null, '#' + this.state.$fragment); | ||
this.router.replaceHash(this.state.$fragment); | ||
} | ||
@@ -88,0 +89,0 @@ } |
@@ -21,2 +21,4 @@ 'use strict'; | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
_classCallCheck(this, Router); | ||
@@ -44,3 +46,3 @@ | ||
var navigateToHash = function navigateToHash() { | ||
return _this.navigate(window.location.hash.replace(/^#*/, '')); | ||
return _this.navigate(window.location.hash); | ||
}; | ||
@@ -50,5 +52,7 @@ window.addEventListener('popstate', function () { | ||
}); | ||
var origReplaceState = window.history.replaceState; | ||
window.history.replaceState = function () { | ||
origReplaceState.apply(this, arguments); | ||
var historyMethod = options.historyMethod || 'pushState'; | ||
var origChangeState = window.history[historyMethod]; | ||
this.changeState = window.history[historyMethod] = function () { | ||
origChangeState.apply(window.history, arguments); | ||
navigateToHash(); | ||
@@ -65,2 +69,3 @@ }; | ||
fragment = stripHash(fragment); | ||
if (fragment === this.app.state.$fragment && !Object.keys(state).length) { | ||
@@ -118,2 +123,10 @@ return; | ||
} | ||
}, { | ||
key: 'replaceHash', | ||
value: function replaceHash(fragment) { | ||
fragment = stripHash(fragment); | ||
if (fragment !== stripHash(window.location.hash)) { | ||
this.changeState(null, null, '#' + fragment); | ||
} | ||
} | ||
}]); | ||
@@ -124,2 +137,7 @@ | ||
exports.default = Router; | ||
exports.default = Router; | ||
function stripHash(fragment) { | ||
return fragment.replace(/^#*/, ''); | ||
} |
@@ -12,6 +12,6 @@ import create from 'virtual-dom/create-element'; | ||
export default class App { | ||
constructor(elID, initialState={}) { | ||
constructor(elID, initialState={}, options={}) { | ||
this.el = document.getElementById(elID); | ||
this.state = initialState; | ||
this.router = new Router(this); | ||
this.router = new Router(this, options); | ||
@@ -49,5 +49,5 @@ const screens = this.SCREENS; | ||
if (updateHash) { | ||
window.history.replaceState(null, null, `#${this.state.$fragment}`); | ||
this.router.replaceHash(this.state.$fragment); | ||
} | ||
} | ||
} |
// just the necessary bits of Backbone router+history | ||
export default class Router { | ||
constructor(app) { | ||
constructor(app, options={}) { | ||
this.app = app; | ||
@@ -25,7 +25,9 @@ const routeDefs = this.app.ROUTES; | ||
const navigateToHash = () => this.navigate(window.location.hash.replace(/^#*/, '')); | ||
const navigateToHash = () => this.navigate(window.location.hash); | ||
window.addEventListener('popstate', () => navigateToHash()); | ||
const origReplaceState = window.history.replaceState; | ||
window.history.replaceState = function() { | ||
origReplaceState.apply(this, arguments); | ||
const historyMethod = options.historyMethod || 'pushState'; | ||
const origChangeState = window.history[historyMethod]; | ||
this.changeState = window.history[historyMethod] = function() { | ||
origChangeState.apply(window.history, arguments); | ||
navigateToHash(); | ||
@@ -36,2 +38,3 @@ }; | ||
navigate(fragment, state={}) { | ||
fragment = stripHash(fragment); | ||
if (fragment === this.app.state.$fragment && !Object.keys(state).length) { | ||
@@ -60,2 +63,13 @@ return; | ||
} | ||
replaceHash(fragment) { | ||
fragment = stripHash(fragment); | ||
if (fragment !== stripHash(window.location.hash)) { | ||
this.changeState(null, null, `#${fragment}`); | ||
} | ||
} | ||
} | ||
function stripHash(fragment) { | ||
return fragment.replace(/^#*/, ''); | ||
} |
{ | ||
"name": "panel", | ||
"version": "0.2.0", | ||
"version": "0.3.1", | ||
"description": "virtual-dom view management and routing", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17203
407