redux-router-middleware
Advanced tools
@@ -80,2 +80,17 @@ 'use strict'; | ||
} | ||
}); | ||
var _utils = require('./utils'); | ||
Object.defineProperty(exports, 'jsonToQueryString', { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils.jsonToQueryString; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'queryStringToJSON', { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils.queryStringToJSON; | ||
} | ||
}); |
@@ -15,5 +15,5 @@ 'use strict'; | ||
var actions = { | ||
"push": "push", | ||
"replace": "replace", | ||
"goback": "goBack" | ||
push: 'push', | ||
replace: 'replace', | ||
goback: 'goBack' | ||
}; /** | ||
@@ -43,3 +43,4 @@ two way to update state | ||
type: _constants.URL_CHANGE, | ||
from: "history", data: { location: location } | ||
from: 'history', | ||
data: { location: location } | ||
}); | ||
@@ -66,8 +67,10 @@ if (preUrlChangeReject) { | ||
type: _constants.URL_CHANGE, | ||
from: "history", data: { location: location } | ||
from: 'history', | ||
data: { location: location } | ||
}); | ||
}, function (e) { | ||
store.dispatch({ | ||
type: "URL_CHANGE_Failure", | ||
from: "history", data: { location: location, error: e } | ||
type: 'URL_CHANGE_Failure', | ||
from: 'history', | ||
data: { location: location, error: e } | ||
}); | ||
@@ -85,3 +88,3 @@ isURLChanging = false; | ||
from:"history",data:{ | ||
location:history.getCurrentLocation ? history.getCurrentLocation() : history.location // only works history ^3.0.0 | ||
location:history.getCurrentLocation ? history.getCurrentLocation() : history.location // only works history ^3.0.0 | ||
} | ||
@@ -94,4 +97,3 @@ })*/ | ||
return function (action) { | ||
if (action.type == _constants.URL_CHANGE && action.from == "history") { | ||
if (action.type == _constants.URL_CHANGE && action.from == 'history') { | ||
if (unblock) { | ||
@@ -102,11 +104,15 @@ unblock(); | ||
} | ||
return next(Object.assign(action, { from: null })); //from is a flag used for update url only via history api | ||
return next(Object.assign(action, { from: null })); //from is a flag used for update url only via history api | ||
} else if (action.type == _constants.URL_CHANGE) { | ||
if (action.data.location.action == "POP") { | ||
if (action.data.location.action == 'POP') { | ||
history.go(-1); | ||
} else { | ||
var location = Object.assign({}, action.data.location); | ||
var query = location.query; | ||
location.search = (0, _utils.jsonToQueryString)(location.query || {}); | ||
history[actions[action.data.location.action.toLowerCase()]](location); | ||
if (action.data.location.href) { | ||
history[actions[action.data.location.action.toLowerCase()]](action.data.location.href); | ||
} else { | ||
var location = Object.assign({}, action.data.location); | ||
var query = location.query; | ||
location.search = (0, _utils.jsonToQueryString)(location.query || {}); | ||
history[actions[action.data.location.action.toLowerCase()]](location); | ||
} | ||
} | ||
@@ -135,3 +141,2 @@ } else if (action.type == _constants.URL_CHANGE_BLOCK) { | ||
routing: routingReducer | ||
}; | ||
@@ -145,3 +150,4 @@ }; | ||
type: _constants.URL_CHANGE, | ||
from: "history", data: { | ||
from: 'history', | ||
data: { | ||
location: history.getCurrentLocation ? history.getCurrentLocation() : history.location /* only works history ^3.0.0 */ | ||
@@ -155,3 +161,4 @@ } | ||
type: _constants.URL_CHANGE, | ||
from: "history", data: { location: location } | ||
from: 'history', | ||
data: { location: location } | ||
}); | ||
@@ -162,3 +169,3 @@ }); | ||
return function (action) { | ||
if (action.type == _constants.URL_CHANGE && action.from == "history") { | ||
if (action.type == _constants.URL_CHANGE && action.from == 'history') { | ||
if (unblock) { | ||
@@ -169,11 +176,15 @@ unblock(); | ||
} | ||
return next(Object.assign(action, { from: null })); //from is a flag used for update url only via history api | ||
return next(Object.assign(action, { from: null })); //from is a flag used for update url only via history api | ||
} else if (action.type == _constants.URL_CHANGE) { | ||
if (action.data.location.action == "POP") { | ||
if (action.data.location.action == 'POP') { | ||
history.go(-1); | ||
} else { | ||
var location = Object.assign({}, action.data.location); | ||
var query = location.query; | ||
location.search = (0, _utils.jsonToQueryString)(location.query || {}); | ||
history[actions[action.data.location.action.toLowerCase()]](location); | ||
if (action.data.location.href) { | ||
history[actions[action.data.location.action.toLowerCase()]](action.data.location.href); | ||
} else { | ||
var location = Object.assign({}, action.data.location); | ||
var query = location.query; | ||
location.search = (0, _utils.jsonToQueryString)(location.query || {}); | ||
history[actions[action.data.location.action.toLowerCase()]](location); | ||
} | ||
} | ||
@@ -180,0 +191,0 @@ } else if (action.type == _constants.URL_CHANGE_BLOCK) { |
{ | ||
"name": "redux-router-middleware", | ||
"version": "1.0.0-beta.14", | ||
"version": "1.0.0-beta.15", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index", |
@@ -1,5 +0,7 @@ | ||
export { routing } from './reducer' | ||
export { push, replace,block, unblock, goBack /*, go, goForward*/ } from './actions';//have to add all history action | ||
export { historyMiddleware, reduxRouter } from './middleware' | ||
export { routing } from './reducer'; | ||
/*, go, goForward*/ | ||
export { push, replace, block, unblock, goBack } from './actions'; //have to add all history action | ||
export { historyMiddleware, reduxRouter } from './middleware'; | ||
export { Link } from './components/Link'; | ||
export { Match } from './components/Match'; | ||
export { jsonToQueryString, queryStringToJSON } from './utils'; |
@@ -10,176 +10,198 @@ /** | ||
import { URL_CHANGE, URL_CHANGE_BLOCK,URL_CHANGE_UNBLOCK } from './constants'; | ||
import { URL_CHANGE, URL_CHANGE_BLOCK, URL_CHANGE_UNBLOCK } from './constants'; | ||
import { queryStringToJSON, jsonToQueryString } from './utils'; | ||
import {routing} from './reducer'; | ||
const actions={ | ||
"push":"push", | ||
"replace":"replace", | ||
"goback":"goBack" | ||
} | ||
import { routing } from './reducer'; | ||
const actions = { | ||
push: 'push', | ||
replace: 'replace', | ||
goback: 'goBack' | ||
}; | ||
export const reduxRouter=(history,urls,onUrlChange)=>{ | ||
var routingReducer = routing(urls) | ||
function historyMiddleware( history ) { | ||
return ( store )=>{ | ||
var preUrlChangeReject=null; | ||
var isURLChanging = false; | ||
var storeActions =[]; | ||
var onChange=( location, action) => { | ||
isURLChanging=true; | ||
var search=location.search; | ||
if(search){ | ||
location.query=queryStringToJSON(search); | ||
export const reduxRouter = (history, urls, onUrlChange) => { | ||
var routingReducer = routing(urls); | ||
function historyMiddleware(history) { | ||
return store => { | ||
var preUrlChangeReject = null; | ||
var isURLChanging = false; | ||
var storeActions = []; | ||
var onChange = (location, action) => { | ||
isURLChanging = true; | ||
var search = location.search; | ||
if (search) { | ||
location.query = queryStringToJSON(search); | ||
} | ||
var newRoutingState = routingReducer(store.getState().routing,{ | ||
type:URL_CHANGE, | ||
from:"history",data:{location} | ||
}) | ||
if(preUrlChangeReject){ | ||
var newRoutingState = routingReducer(store.getState().routing, { | ||
type: URL_CHANGE, | ||
from: 'history', | ||
data: { location } | ||
}); | ||
if (preUrlChangeReject) { | ||
preUrlChangeReject(); | ||
storeActions=[]; | ||
preUrlChangeReject=null; | ||
storeActions = []; | ||
preUrlChangeReject = null; | ||
} | ||
var p=new Promise((res,rej)=>{ | ||
onUrlChange(newRoutingState,()=>{ | ||
res(); | ||
return p; | ||
},{dispatch:store.dispatch,getState:store.getState}); | ||
preUrlChangeReject=rej | ||
}).then(()=>{ | ||
preUrlChangeReject=null; | ||
isURLChanging=false; | ||
storeActions.forEach((action)=>{ | ||
store.dispatch(action); | ||
}) | ||
storeActions=[]; | ||
store.dispatch({ | ||
type:URL_CHANGE, | ||
from:"history",data:{location} | ||
}) | ||
},(e)=>{ | ||
store.dispatch({ | ||
type:"URL_CHANGE_Failure", | ||
from:"history",data:{location,error:e} | ||
}) | ||
isURLChanging=false; | ||
}) | ||
} | ||
/* init dispatch*/ | ||
setTimeout(()=>onChange(history.getCurrentLocation ? history.getCurrentLocation() : history.location),1) | ||
var p = new Promise((res, rej) => { | ||
onUrlChange( | ||
newRoutingState, | ||
() => { | ||
res(); | ||
return p; | ||
}, | ||
{ dispatch: store.dispatch, getState: store.getState } | ||
); | ||
preUrlChangeReject = rej; | ||
}).then( | ||
() => { | ||
preUrlChangeReject = null; | ||
isURLChanging = false; | ||
storeActions.forEach(action => { | ||
store.dispatch(action); | ||
}); | ||
storeActions = []; | ||
store.dispatch({ | ||
type: URL_CHANGE, | ||
from: 'history', | ||
data: { location } | ||
}); | ||
}, | ||
e => { | ||
store.dispatch({ | ||
type: 'URL_CHANGE_Failure', | ||
from: 'history', | ||
data: { location, error: e } | ||
}); | ||
isURLChanging = false; | ||
} | ||
); | ||
}; | ||
/* init dispatch*/ | ||
setTimeout( | ||
() => | ||
onChange( | ||
history.getCurrentLocation | ||
? history.getCurrentLocation() | ||
: history.location | ||
), | ||
1 | ||
); | ||
/*store.dispatch({ | ||
type:URL_CHANGE, | ||
from:"history",data:{ | ||
location:history.getCurrentLocation ? history.getCurrentLocation() : history.location // only works history ^3.0.0 | ||
location:history.getCurrentLocation ? history.getCurrentLocation() : history.location // only works history ^3.0.0 | ||
} | ||
})*/ | ||
var unlisten = history.listen(onChange) | ||
var unblock = null; | ||
return ( next ) => ( action ) => { | ||
if(action.type==URL_CHANGE && action.from=="history"){ | ||
if(unblock){ | ||
unblock(); | ||
unblock = null; | ||
next({type:URL_CHANGE_UNBLOCK}) | ||
} | ||
return next(Object.assign(action,{from:null})) //from is a flag used for update url only via history api | ||
} | ||
else if(action.type==URL_CHANGE){ | ||
if(action.data.location.action=="POP"){ | ||
history.go(-1); | ||
} | ||
else{ | ||
var location=Object.assign({},action.data.location); | ||
var query=location.query; | ||
location.search=jsonToQueryString(location.query || {}) | ||
history[actions[action.data.location.action.toLowerCase()]](location) | ||
var unlisten = history.listen(onChange); | ||
var unblock = null; | ||
return next => action => { | ||
if (action.type == URL_CHANGE && action.from == 'history') { | ||
if (unblock) { | ||
unblock(); | ||
unblock = null; | ||
next({ type: URL_CHANGE_UNBLOCK }); | ||
} | ||
return next(Object.assign(action, { from: null })); //from is a flag used for update url only via history api | ||
} else if (action.type == URL_CHANGE) { | ||
if (action.data.location.action == 'POP') { | ||
history.go(-1); | ||
} else { | ||
if (action.data.location.href) { | ||
history[actions[action.data.location.action.toLowerCase()]]( | ||
action.data.location.href | ||
); | ||
} else { | ||
var location = Object.assign({}, action.data.location); | ||
var query = location.query; | ||
location.search = jsonToQueryString(location.query || {}); | ||
history[actions[action.data.location.action.toLowerCase()]]( | ||
location | ||
); | ||
} | ||
} | ||
} else if (action.type == URL_CHANGE_BLOCK) { | ||
unblock = history.block( | ||
action.data.msg || 'Are you sure to leave the page?' | ||
); //i18n miss | ||
next(action); | ||
} else if (action.type == URL_CHANGE_UNBLOCK) { | ||
if (unblock) { | ||
unblock(); | ||
unblock = null; | ||
} | ||
next(action); | ||
} else if (isURLChanging) { | ||
storeActions.push(action); | ||
return action; | ||
} else { | ||
return next(action); | ||
} | ||
} | ||
else if(action.type==URL_CHANGE_BLOCK){ | ||
unblock = history.block(action.data.msg || 'Are you sure to leave the page?')//i18n miss | ||
next(action); | ||
} | ||
else if(action.type==URL_CHANGE_UNBLOCK){ | ||
if(unblock){ | ||
unblock() | ||
unblock = null; | ||
} | ||
next(action); | ||
} | ||
else if(isURLChanging){ | ||
storeActions.push(action); | ||
return action; | ||
} | ||
else{ | ||
return next(action) | ||
} | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
return { | ||
historyMiddleware:historyMiddleware(history) , | ||
routing:routingReducer | ||
historyMiddleware: historyMiddleware(history), | ||
routing: routingReducer | ||
}; | ||
}; | ||
} | ||
} | ||
export const historyMiddleware = ( history ) => ( store )=>{ | ||
export const historyMiddleware = history => store => { | ||
/* init dispatch*/ | ||
store.dispatch({ | ||
type:URL_CHANGE, | ||
from:"history",data:{ | ||
location:history.getCurrentLocation ? history.getCurrentLocation() : history.location /* only works history ^3.0.0 */ | ||
type: URL_CHANGE, | ||
from: 'history', | ||
data: { | ||
location: history.getCurrentLocation | ||
? history.getCurrentLocation() | ||
: history.location /* only works history ^3.0.0 */ | ||
} | ||
}) | ||
var unlisten = history.listen( ( location, action) => { | ||
var search=location.search; | ||
if(search) | ||
location.query=queryStringToJSON(search); | ||
store.dispatch({ | ||
type:URL_CHANGE, | ||
from:"history",data:{location} | ||
}) | ||
}) | ||
}); | ||
var unlisten = history.listen((location, action) => { | ||
var search = location.search; | ||
if (search) location.query = queryStringToJSON(search); | ||
store.dispatch({ | ||
type: URL_CHANGE, | ||
from: 'history', | ||
data: { location } | ||
}); | ||
}); | ||
var unblock = null; | ||
return ( next ) => ( action ) => { | ||
if(action.type==URL_CHANGE && action.from=="history"){ | ||
if(unblock){ | ||
return next => action => { | ||
if (action.type == URL_CHANGE && action.from == 'history') { | ||
if (unblock) { | ||
unblock(); | ||
unblock = null; | ||
next({type:URL_CHANGE_UNBLOCK}) | ||
} | ||
return next(Object.assign(action,{from:null})) //from is a flag used for update url only via history api | ||
} | ||
else if(action.type==URL_CHANGE){ | ||
if(action.data.location.action=="POP"){ | ||
next({ type: URL_CHANGE_UNBLOCK }); | ||
} | ||
return next(Object.assign(action, { from: null })); //from is a flag used for update url only via history api | ||
} else if (action.type == URL_CHANGE) { | ||
if (action.data.location.action == 'POP') { | ||
history.go(-1); | ||
} else { | ||
if (action.data.location.href) { | ||
history[actions[action.data.location.action.toLowerCase()]]( | ||
action.data.location.href | ||
); | ||
} else { | ||
var location = Object.assign({}, action.data.location); | ||
var query = location.query; | ||
location.search = jsonToQueryString(location.query || {}); | ||
history[actions[action.data.location.action.toLowerCase()]](location); | ||
} | ||
} | ||
else{ | ||
var location=Object.assign({},action.data.location); | ||
var query=location.query; | ||
location.search=jsonToQueryString(location.query || {}) | ||
history[actions[action.data.location.action.toLowerCase()]](location) | ||
} | ||
} | ||
else if(action.type==URL_CHANGE_BLOCK){ | ||
unblock = history.block(action.data.msg || 'Are you sure to leave the page?')//i18n miss | ||
} else if (action.type == URL_CHANGE_BLOCK) { | ||
unblock = history.block( | ||
action.data.msg || 'Are you sure to leave the page?' | ||
); //i18n miss | ||
next(action); | ||
} | ||
else if(action.type==URL_CHANGE_UNBLOCK){ | ||
if(unblock){ | ||
unblock() | ||
} else if (action.type == URL_CHANGE_UNBLOCK) { | ||
if (unblock) { | ||
unblock(); | ||
unblock = null; | ||
} | ||
} | ||
next(action); | ||
} else { | ||
return next(action); | ||
} | ||
else{ | ||
return next(action) | ||
} | ||
} | ||
} | ||
}; | ||
}; |
80406
2.16%1870
3.09%