diff --git a/.gitignore b/.gitignore index ff1a80c..1af7233 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ node_modules npm-debug.log .DS_Store +build /test/browser/browserified-bundle.js diff --git a/lib/browser.js b/lib/browser.js index 0814545..1f75553 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -6,6 +6,7 @@ * */ +import historyPolyfill from 'html5-history-api'; import { Router as BaseRouter } from './tarantino/router'; var dloc = document.location; @@ -16,6 +17,31 @@ function dlocHashEmpty() { return dloc.hash === '' || dloc.hash === '#'; } +function applyUrlLogicGates(url, i, v, val) { + // Remove and insert: + if (typeof i === 'number' && typeof v === 'number' && typeof val === 'string') { + if (i < url.length) { + url.splice(i, v, val); + } + } + // Remove: + else if (typeof i === 'number' && typeof v === 'number') { + if (i < url.length) { + url.splice(i, v); + } + } + // Replace: + else if (typeof i === 'number' && typeof v === 'string') { + url[i] = v; + } + // Do nothing: + else { + url = [i]; + } + + return url; +} + var listener = { mode: 'modern', hash: dloc.hash, @@ -30,12 +56,13 @@ var listener = { }, fire: function () { - if (this.mode === 'modern') { - this.history === true ? window.onpopstate() : window.onhashchange(); - } - else { - this.onHashChanged(); - } + if (this.mode === 'modern' && this.history) + return window.onpopstate(); + + if (this.mode === 'modern' && !this.history) + return window.onhashchange(); + + return this.onHashChanged(); }, init: function (fn, history) { @@ -125,6 +152,13 @@ var listener = { return this; }, + replaceHash: function (s) { + historyPolyfill.replaceState({}, document.title, s); + this.fire(); + + return this; + }, + writeFrame: function (s) { // IE support... var f = document.getElementById('state-frame'); @@ -215,33 +249,19 @@ Router.prototype.explode = function () { }; Router.prototype.setRoute = function (i, v, val) { - var url = this.explode(); - - // Remove and insert: - if (typeof i === 'number' && typeof v === 'number' && typeof val === 'string') { - if (i < url.length) { - url.splice(i, v, val); - } - } - // Remove: - else if (typeof i === 'number' && typeof v === 'number') { - if (i < url.length) { - url.splice(i, v); - } - } - // Replace: - else if (typeof i === 'number' && typeof v === 'string') { - url[i] = v; - } - // Do nothing: - else { - url = [i]; - } - + var url = applyUrlLogicGates(this.explode(), i, v , val); listener.setHash(url.join('/')); + return url; }; +Router.prototype.replaceRoute = function(i, v, val) { + var url = applyUrlLogicGates(this.explode(), i, v , val); + listener.replaceHash(url.join('/')); + + return url; +} + // // ### function insertEx(method, path, route, parent) // #### @method {string} Method to insert the specific `route`. diff --git a/package.json b/package.json index 68e07c6..49de230 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "request": "^2.79.0", "rollup": "^0.36.4", "rollup-plugin-commonjs": "^5.0.5", + "rollup-plugin-node-resolve": "^2.0.0", "rollup-plugin-uglify": "^1.0.1", "uglify-js": "^2.7.4", "vows": "^0.8.1" @@ -48,5 +49,8 @@ "test-hash": "opn ./test/browser/routes-harness.html", "preversion": "npm run -s build", "version": "git add -A build" + }, + "dependencies": { + "html5-history-api": "^4.2.7" } } diff --git a/rollup.config.js b/rollup.config.js index e9ee17d..c404a4f 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,4 +1,5 @@ var commonjs = require('rollup-plugin-commonjs'); +var nodeResolve = require('rollup-plugin-node-resolve'); module.exports = { entry: './lib/browser.js', @@ -8,5 +9,9 @@ module.exports = { moduleName: 'tarantino', plugins: [ commonjs(), + nodeResolve({ + jsnext: true, + main: true + }), ], };