Skip to content
This repository has been archived by the owner on Dec 19, 2017. It is now read-only.

Commit

Permalink
Merge branch 'master' into greenkeeper-karma-tap-3.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyWebb committed Sep 9, 2016
2 parents 28f2bbf + 2d7afc1 commit 21928cc
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 67 deletions.
47 changes: 31 additions & 16 deletions dist/ko-component-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

var path = location.pathname + location.search + location.hash;
var state = (e.state || {})[this.ctx.config.depth + this.ctx.pathname()];
var state = (e.state || {})[(0, _utils.normalizePath)(this.ctx.config.depth + this.ctx.pathname())];

if (this.dispatch({ path: path, state: state })) {
e.preventDefault();
Expand Down Expand Up @@ -414,7 +414,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

if (!query && querystring) {
query = _qs2.default.parse(querystring)[_this2.config.depth + pathname];
query = _qs2.default.parse(querystring)[(0, _utils.normalizePath)(_this2.config.depth + pathname)];
}

var canonicalPath = Context.getCanonicalPath(_this2.getBase().replace(/\/$/, ''), pathname, childPath, _this2.query.getFullQueryString(query, pathname), hash);
Expand Down Expand Up @@ -1183,7 +1183,7 @@ return /******/ (function(modules) { // webpackBootstrap

var query = this;
var ctx = this.ctx;
var guid = this.ctx.config.depth + ctx.pathname();
var guid = (0, _utils.normalizePath)(ctx.config.depth + ctx.pathname());

if (!cache[guid]) {
cache[guid] = {};
Expand Down Expand Up @@ -1242,7 +1242,7 @@ return /******/ (function(modules) { // webpackBootstrap
var asObservable = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
var pathname = arguments.length <= 1 || arguments[1] === undefined ? this.ctx.pathname() : arguments[1];

var guid = this.ctx.config.depth + pathname;
var guid = (0, _utils.normalizePath)(this.ctx.config.depth + pathname);
return asObservable ? _knockout2.default.pureComputed({
read: function read() {
trigger();
Expand Down Expand Up @@ -1272,7 +1272,7 @@ return /******/ (function(modules) { // webpackBootstrap
if (typeof pathname !== 'string') {
pathname = this.ctx.pathname();
}
var guid = this.ctx.config.depth + pathname;
var guid = (0, _utils.normalizePath)(this.ctx.config.depth + pathname);
for (var pn in cache[guid]) {
var p = cache[guid][pn];
this.get(pn)(p.defaultVal);
Expand All @@ -1282,7 +1282,7 @@ return /******/ (function(modules) { // webpackBootstrap
key: 'reload',
value: function reload() {
var force = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
var guid = arguments.length <= 1 || arguments[1] === undefined ? this.ctx.config.depth + this.ctx.pathname() : arguments[1];
var guid = arguments.length <= 1 || arguments[1] === undefined ? (0, _utils.normalizePath)(this.ctx.config.depth + this.ctx.pathname()) : arguments[1];

if (!this.ctx.config.persistQuery || force) {
for (var p in qsParams[guid]) {
Expand Down Expand Up @@ -1310,7 +1310,7 @@ return /******/ (function(modules) { // webpackBootstrap
var query = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var pathname = arguments.length <= 1 || arguments[1] === undefined ? this.ctx.pathname() : arguments[1];

var guid = this.ctx.config.depth + pathname;
var guid = (0, _utils.normalizePath)(this.ctx.config.depth + pathname);

if ((0, _utils.deepEquals)(qsParams[guid], query)) {
return;
Expand All @@ -1323,7 +1323,7 @@ return /******/ (function(modules) { // webpackBootstrap
key: 'updateFromString',
value: function updateFromString(str, pathname) {
if (pathname) {
var guid = this.ctx.config.depth + pathname;
var guid = (0, _utils.normalizePath)(this.ctx.config.depth + pathname);
(0, _utils.merge)(qsParams, _defineProperty({}, guid, _qs2.default.parse(str)[guid]), false);
} else {
(0, _utils.merge)(qsParams, _qs2.default.parse(str), false);
Expand All @@ -1337,7 +1337,7 @@ return /******/ (function(modules) { // webpackBootstrap
var workingParams = qsParams;

if (query) {
(0, _utils.merge)(workingParams, _defineProperty({}, this.ctx.config.depth + pathname, query), false);
(0, _utils.merge)(workingParams, _defineProperty({}, (0, _utils.normalizePath)(this.ctx.config.depth + pathname), query), false);
}

for (var id in workingParams) {
Expand Down Expand Up @@ -1396,6 +1396,7 @@ return /******/ (function(modules) { // webpackBootstrap
exports.isArray = isArray;
exports.mapKeys = mapKeys;
exports.merge = merge;
exports.normalizePath = normalizePath;

var _knockout = __webpack_require__(1);

Expand Down Expand Up @@ -1627,6 +1628,19 @@ return /******/ (function(modules) { // webpackBootstrap
extend(dest, src, createAsObservable, false);
}

function normalizePath(path) {
if (path.length === 0) {
return '/';
}
if (path[0] !== '/') {
path = ''.concat('/', path);
}
if (path[path.length - 1] === '/') {
path = path.substr(0, path.length - 1);
}
return path;
}

function fromJS(obj, parentIsArray) {
var obs = void 0;

Expand Down Expand Up @@ -1675,19 +1689,20 @@ return /******/ (function(modules) { // webpackBootstrap

var state = _knockout2.default.pureComputed({
read: function read() {
var guid = (0, _utils.normalizePath)(ctx.config.depth + ctx.pathname());
trigger();
return history.state ? history.state[ctx.config.depth + ctx.pathname()] : {};
return history.state ? history.state[guid] : {};
},
write: function write(v) {
if (v) {
var s = history.state || {};
var key = ctx.config.depth + ctx.pathname();
var guid = (0, _utils.normalizePath)(ctx.config.depth + ctx.pathname());

if (!(0, _utils.deepEquals)(v, history.state ? history.state[ctx.config.depth + ctx.pathname()] : {})) {
if (s[key]) {
delete s[key];
if (!(0, _utils.deepEquals)(v, history.state ? history.state[guid] : {})) {
if (s[guid]) {
delete s[guid];
}
s[key] = v;
s[guid] = v;
history.replaceState(s, document.title);
trigger(!trigger());
}
Expand All @@ -1699,7 +1714,7 @@ return /******/ (function(modules) { // webpackBootstrap

state.reload = function () {
var force = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
var guid = arguments.length <= 1 || arguments[1] === undefined ? ctx.config.depth + ctx.pathname() : arguments[1];
var guid = arguments.length <= 1 || arguments[1] === undefined ? (0, _utils.normalizePath)(ctx.config.depth + ctx.pathname()) : arguments[1];

if (!ctx.config.persistState || force) {
if (history.state && history.state[guid]) {
Expand Down
3 changes: 2 additions & 1 deletion dist/ko-component-router.min.js

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions example/dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/dist/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var Context = function () {
}

if (!query && querystring) {
query = _qs2.default.parse(querystring)[_this2.config.depth + pathname];
query = _qs2.default.parse(querystring)[(0, _utils.normalizePath)(_this2.config.depth + pathname)];
}

var canonicalPath = Context.getCanonicalPath(_this2.getBase().replace(/\/$/, ''), pathname, childPath, _this2.query.getFullQueryString(query, pathname), hash);
Expand Down
14 changes: 7 additions & 7 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var Query = function () {

var query = this;
var ctx = this.ctx;
var guid = this.ctx.config.depth + ctx.pathname();
var guid = (0, _utils.normalizePath)(ctx.config.depth + ctx.pathname());

if (!cache[guid]) {
cache[guid] = {};
Expand Down Expand Up @@ -113,7 +113,7 @@ var Query = function () {
var asObservable = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
var pathname = arguments.length <= 1 || arguments[1] === undefined ? this.ctx.pathname() : arguments[1];

var guid = this.ctx.config.depth + pathname;
var guid = (0, _utils.normalizePath)(this.ctx.config.depth + pathname);
return asObservable ? _knockout2.default.pureComputed({
read: function read() {
trigger();
Expand Down Expand Up @@ -143,7 +143,7 @@ var Query = function () {
if (typeof pathname !== 'string') {
pathname = this.ctx.pathname();
}
var guid = this.ctx.config.depth + pathname;
var guid = (0, _utils.normalizePath)(this.ctx.config.depth + pathname);
for (var pn in cache[guid]) {
var p = cache[guid][pn];
this.get(pn)(p.defaultVal);
Expand All @@ -153,7 +153,7 @@ var Query = function () {
key: 'reload',
value: function reload() {
var force = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
var guid = arguments.length <= 1 || arguments[1] === undefined ? this.ctx.config.depth + this.ctx.pathname() : arguments[1];
var guid = arguments.length <= 1 || arguments[1] === undefined ? (0, _utils.normalizePath)(this.ctx.config.depth + this.ctx.pathname()) : arguments[1];

if (!this.ctx.config.persistQuery || force) {
for (var p in qsParams[guid]) {
Expand Down Expand Up @@ -181,7 +181,7 @@ var Query = function () {
var query = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var pathname = arguments.length <= 1 || arguments[1] === undefined ? this.ctx.pathname() : arguments[1];

var guid = this.ctx.config.depth + pathname;
var guid = (0, _utils.normalizePath)(this.ctx.config.depth + pathname);

if ((0, _utils.deepEquals)(qsParams[guid], query)) {
return;
Expand All @@ -194,7 +194,7 @@ var Query = function () {
key: 'updateFromString',
value: function updateFromString(str, pathname) {
if (pathname) {
var guid = this.ctx.config.depth + pathname;
var guid = (0, _utils.normalizePath)(this.ctx.config.depth + pathname);
(0, _utils.merge)(qsParams, _defineProperty({}, guid, _qs2.default.parse(str)[guid]), false);
} else {
(0, _utils.merge)(qsParams, _qs2.default.parse(str), false);
Expand All @@ -208,7 +208,7 @@ var Query = function () {
var workingParams = qsParams;

if (query) {
(0, _utils.merge)(workingParams, _defineProperty({}, this.ctx.config.depth + pathname, query), false);
(0, _utils.merge)(workingParams, _defineProperty({}, (0, _utils.normalizePath)(this.ctx.config.depth + pathname), query), false);
}

for (var id in workingParams) {
Expand Down
2 changes: 1 addition & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var Router = function () {
}

var path = location.pathname + location.search + location.hash;
var state = (e.state || {})[this.ctx.config.depth + this.ctx.pathname()];
var state = (e.state || {})[(0, _utils.normalizePath)(this.ctx.config.depth + this.ctx.pathname())];

if (this.dispatch({ path: path, state: state })) {
e.preventDefault();
Expand Down
15 changes: 8 additions & 7 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ function factory(ctx) {

var state = _knockout2.default.pureComputed({
read: function read() {
var guid = (0, _utils.normalizePath)(ctx.config.depth + ctx.pathname());
trigger();
return history.state ? history.state[ctx.config.depth + ctx.pathname()] : {};
return history.state ? history.state[guid] : {};
},
write: function write(v) {
if (v) {
var s = history.state || {};
var key = ctx.config.depth + ctx.pathname();
var guid = (0, _utils.normalizePath)(ctx.config.depth + ctx.pathname());

if (!(0, _utils.deepEquals)(v, history.state ? history.state[ctx.config.depth + ctx.pathname()] : {})) {
if (s[key]) {
delete s[key];
if (!(0, _utils.deepEquals)(v, history.state ? history.state[guid] : {})) {
if (s[guid]) {
delete s[guid];
}
s[key] = v;
s[guid] = v;
history.replaceState(s, document.title);
trigger(!trigger());
}
Expand All @@ -42,7 +43,7 @@ function factory(ctx) {

state.reload = function () {
var force = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
var guid = arguments.length <= 1 || arguments[1] === undefined ? ctx.config.depth + ctx.pathname() : arguments[1];
var guid = arguments.length <= 1 || arguments[1] === undefined ? (0, _utils.normalizePath)(ctx.config.depth + ctx.pathname()) : arguments[1];

if (!ctx.config.persistState || force) {
if (history.state && history.state[guid]) {
Expand Down
14 changes: 14 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports.isPlainObject = isPlainObject;
exports.isArray = isArray;
exports.mapKeys = mapKeys;
exports.merge = merge;
exports.normalizePath = normalizePath;

var _knockout = require('knockout');

Expand Down Expand Up @@ -249,6 +250,19 @@ function merge(dest, src) {
extend(dest, src, createAsObservable, false);
}

function normalizePath(path) {
if (path.length === 0) {
return '/';
}
if (path[0] !== '/') {
path = ''.concat('/', path);
}
if (path[path.length - 1] === '/') {
path = path.substr(0, path.length - 1);
}
return path;
}

function fromJS(obj, parentIsArray) {
var obs = void 0;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ko-component-router",
"version": "3.10.6",
"version": "3.10.7",
"description": "Component-based routing for KnockoutJS",
"homepage": "https://github.com/Profiscience/ko-component-router",
"bugs": "https://github.com/Profiscience/ko-component-router/issues",
Expand Down
4 changes: 2 additions & 2 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ko from 'knockout'
import qs from 'qs'
import { factory as queryFactory } from './query'
import { factory as stateFactory } from './state'
import { cascade, deepEquals, extend, merge } from './utils'
import { cascade, deepEquals, extend, merge, normalizePath } from './utils'

export default class Context {
constructor(bindingCtx, config) {
Expand Down Expand Up @@ -98,7 +98,7 @@ export default class Context {
}

if (!query && querystring) {
query = qs.parse(querystring)[this.config.depth + pathname]
query = qs.parse(querystring)[normalizePath(this.config.depth + pathname)]
}

const canonicalPath = Context
Expand Down
16 changes: 8 additions & 8 deletions src/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ko from 'knockout'
import qs from 'qs'
import { clone, deepEquals, identity, isUndefined, mapKeys, merge } from './utils'
import { clone, deepEquals, identity, isUndefined, mapKeys, merge, normalizePath } from './utils'

const qsParams = {}
const trigger = ko.observable(true)
Expand All @@ -26,7 +26,7 @@ class Query {
get(prop, defaultVal, parser = identity) {
const query = this
const ctx = this.ctx
const guid = this.ctx.config.depth + ctx.pathname()
const guid = normalizePath(ctx.config.depth + ctx.pathname())

if (!cache[guid]) {
cache[guid] = {}
Expand Down Expand Up @@ -79,7 +79,7 @@ class Query {
}

getAll(asObservable = false, pathname = this.ctx.pathname()) {
const guid = this.ctx.config.depth + pathname
const guid = normalizePath(this.ctx.config.depth + pathname)
return asObservable
? ko.pureComputed({
read() {
Expand Down Expand Up @@ -110,14 +110,14 @@ class Query {
if (typeof pathname !== 'string') {
pathname = this.ctx.pathname()
}
const guid = this.ctx.config.depth + pathname
const guid = normalizePath(this.ctx.config.depth + pathname)
for (const pn in cache[guid]) {
const p = cache[guid][pn]
this.get(pn)(p.defaultVal)
}
}

reload(force = false, guid = this.ctx.config.depth + this.ctx.pathname()) {
reload(force = false, guid = normalizePath(this.ctx.config.depth + this.ctx.pathname())) {
if (!this.ctx.config.persistQuery || force) {
for (const p in qsParams[guid]) {
if (cache[guid] && cache[guid][p]) {
Expand All @@ -139,7 +139,7 @@ class Query {
}

update(query = {}, pathname = this.ctx.pathname()) {
const guid = this.ctx.config.depth + pathname
const guid = normalizePath(this.ctx.config.depth + pathname)

if (deepEquals(qsParams[guid], query)) {
return
Expand All @@ -151,7 +151,7 @@ class Query {

updateFromString(str, pathname) {
if (pathname) {
const guid = this.ctx.config.depth + pathname
const guid = normalizePath(this.ctx.config.depth + pathname)
merge(qsParams, { [guid]: qs.parse(str)[guid] }, false)
} else {
merge(qsParams, qs.parse(str), false)
Expand All @@ -164,7 +164,7 @@ class Query {
const workingParams = qsParams

if (query) {
merge(workingParams, { [this.ctx.config.depth + pathname]: query }, false)
merge(workingParams, { [normalizePath(this.ctx.config.depth + pathname)]: query }, false)
}

for (const id in workingParams) {
Expand Down
Loading

0 comments on commit 21928cc

Please sign in to comment.