omi-router v0.1.1 - support defaultRoute option

This commit is contained in:
dntzhang 2017-03-31 08:38:24 +08:00
parent 17c3e6b743
commit 71476c2c23
6 changed files with 47 additions and 19 deletions

View File

@ -1,5 +1,5 @@
/*!
* OmiRouter v0.1.0 By dntzhang
* OmiRouter v0.1.1 By dntzhang
* Github: https://github.com/AlloyTeam/omi
* MIT Licensed.
*/
@ -95,6 +95,12 @@ return /******/ (function(modules) { // webpackBootstrap
hashMapping(dom.getAttribute('to'))
}, false)
})
var hash = window.location.hash.replace('#', '')
hashMapping(hash ? hash : routerOption.defaultRoute, renderTo)
if(hash) {
render()
}
}
function getParams(toArr, pathArr) {
@ -129,6 +135,10 @@ return /******/ (function(modules) { // webpackBootstrap
window.addEventListener('hashchange', function() {
hashMapping(window.location.hash.replace('#',''), renderTo)
render()
}, false)
function render(){
if(store){
store.$route = { }
store.$route.params = params
@ -151,7 +161,7 @@ return /******/ (function(modules) { // webpackBootstrap
})
preInstance = instance
preRenderTo = renderTo
}, false)
}
function deleteInstance(instance){
for(var key in Omi.instances){

View File

@ -1,6 +1,6 @@
/*!
* OmiRouter v0.1.0 By dntzhang
* OmiRouter v0.1.1 By dntzhang
* Github: https://github.com/AlloyTeam/omi
* MIT Licensed.
*/
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("omi")):"function"==typeof define&&define.amd?define(["omi"],t):"object"==typeof exports?exports.OmiRouter=t(require("omi")):e.OmiRouter=t(e.omi)}(this,function(e){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){!function(){function t(e,t){var r={};return e.forEach(function(e,n){n>0&&(r[t[n].replace(":","")]=e)}),r}function n(e){d.routes.every(function(r){var n=e.match(r.reg);if(n){var i=r.path.match(r.reg);return f=t(n,i),p=r.renderTo||d.renderTo,l=r.store||d.store,s=r.component,o(e),!1}return!0})}function o(e){window.location.hash=e}function i(e){for(var t in u.instances)if(u.instances.hasOwnProperty(t)){u.instances[t].id=e.id,delete u.instances[t],e=null;break}}var a={},u=r(1),c=r(2),p=null,f={},s=null,l=null,d={},h=null,g=null;a.init=function(e){d=e,e.routes.forEach(function(e){e.reg=c(e.path)}),u.extendPlugin("omi-router",function(e,t){e.setAttribute("href","javascript:void(0)"),e.addEventListener("click",function(){n(e.getAttribute("to"))},!1)})},window.addEventListener("hashchange",function(){n(window.location.hash.replace("#",""),p),l?(l.$route={},l.$route.params=f):l={methods:{install:function(){this.$route={},this.$route.params=f}}},h===p&&g&&i(g);var e=new s;u.render(e,p,{store:l}),g=e,h=p},!1),a.destroy=function(){delete u.plugins["omi-router"]},e.exports=a}()},function(t,r){t.exports=e},function(e,t,r){function n(e,t){for(var r,n=[],o=0,i=0,a="",u=t&&t.delimiter||"/";null!=(r=v.exec(e));){var f=r[0],s=r[1],l=r.index;if(a+=e.slice(i,l),i=l+f.length,s)a+=s[1];else{var d=e[i],h=r[2],g=r[3],m=r[4],x=r[5],y=r[6],w=r[7];a&&(n.push(a),a="");var E=null!=h&&null!=d&&d!==h,b="+"===y||"*"===y,$="?"===y||"*"===y,R=r[2]||u,j=m||x;n.push({name:g||o++,prefix:h||"",delimiter:R,optional:$,repeat:b,partial:E,asterisk:!!w,pattern:j?p(j):w?".*":"[^"+c(R)+"]+?"})}}return i<e.length&&(a+=e.substr(i)),a&&n.push(a),n}function o(e,t){return u(n(e,t))}function i(e){return encodeURI(e).replace(/[\/?#]/g,function(e){return"%"+e.charCodeAt(0).toString(16).toUpperCase()})}function a(e){return encodeURI(e).replace(/[?#]/g,function(e){return"%"+e.charCodeAt(0).toString(16).toUpperCase()})}function u(e){for(var t=new Array(e.length),r=0;r<e.length;r++)"object"==typeof e[r]&&(t[r]=new RegExp("^(?:"+e[r].pattern+")$"));return function(r,n){for(var o="",u=r||{},c=n||{},p=c.pretty?i:encodeURIComponent,f=0;f<e.length;f++){var s=e[f];if("string"!=typeof s){var l,d=u[s.name];if(null==d){if(s.optional){s.partial&&(o+=s.prefix);continue}throw new TypeError('Expected "'+s.name+'" to be defined')}if(x(d)){if(!s.repeat)throw new TypeError('Expected "'+s.name+'" to not repeat, but received `'+JSON.stringify(d)+"`");if(0===d.length){if(s.optional)continue;throw new TypeError('Expected "'+s.name+'" to not be empty')}for(var h=0;h<d.length;h++){if(l=p(d[h]),!t[f].test(l))throw new TypeError('Expected all "'+s.name+'" to match "'+s.pattern+'", but received `'+JSON.stringify(l)+"`");o+=(0===h?s.prefix:s.delimiter)+l}}else{if(l=s.asterisk?a(d):p(d),!t[f].test(l))throw new TypeError('Expected "'+s.name+'" to match "'+s.pattern+'", but received "'+l+'"');o+=s.prefix+l}}else o+=s}return o}}function c(e){return e.replace(/([.+*?=^!:${}()[\]|\/\\])/g,"\\$1")}function p(e){return e.replace(/([=!:$\/()])/g,"\\$1")}function f(e,t){return e.keys=t,e}function s(e){return e.sensitive?"":"i"}function l(e,t){var r=e.source.match(/\((?!\?)/g);if(r)for(var n=0;n<r.length;n++)t.push({name:n,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,asterisk:!1,pattern:null});return f(e,t)}function d(e,t,r){for(var n=[],o=0;o<e.length;o++)n.push(m(e[o],t,r).source);var i=new RegExp("(?:"+n.join("|")+")",s(r));return f(i,t)}function h(e,t,r){return g(n(e,r),t,r)}function g(e,t,r){x(t)||(r=t||r,t=[]),r=r||{};for(var n=r.strict,o=r.end!==!1,i="",a=0;a<e.length;a++){var u=e[a];if("string"==typeof u)i+=c(u);else{var p=c(u.prefix),l="(?:"+u.pattern+")";t.push(u),u.repeat&&(l+="(?:"+p+l+")*"),l=u.optional?u.partial?p+"("+l+")?":"(?:"+p+"("+l+"))?":p+"("+l+")",i+=l}}var d=c(r.delimiter||"/"),h=i.slice(-d.length)===d;return n||(i=(h?i.slice(0,-d.length):i)+"(?:"+d+"(?=$))?"),i+=o?"$":n&&h?"":"(?="+d+"|$)",f(new RegExp("^"+i,s(r)),t)}function m(e,t,r){return x(t)||(r=t||r,t=[]),r=r||{},e instanceof RegExp?l(e,t):x(e)?d(e,t,r):h(e,t,r)}var x=r(3);e.exports=m,e.exports.parse=n,e.exports.compile=o,e.exports.tokensToFunction=u,e.exports.tokensToRegExp=g;var v=new RegExp(["(\\\\.)","([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))"].join("|"),"g")},function(e,t){e.exports=Array.isArray||function(e){return"[object Array]"==Object.prototype.toString.call(e)}}])});
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("omi")):"function"==typeof define&&define.amd?define(["omi"],t):"object"==typeof exports?exports.OmiRouter=t(require("omi")):e.OmiRouter=t(e.omi)}(this,function(e){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){!function(){function t(e,t){var r={};return e.forEach(function(e,n){n>0&&(r[t[n].replace(":","")]=e)}),r}function n(e){h.routes.every(function(r){var n=e.match(r.reg);if(n){var i=r.path.match(r.reg);return l=t(n,i),f=r.renderTo||h.renderTo,d=r.store||h.store,s=r.component,o(e),!1}return!0})}function o(e){window.location.hash=e}function i(){d?(d.$route={},d.$route.params=l):d={methods:{install:function(){this.$route={},this.$route.params=l}}},g===f&&m&&a(m);var e=new s;c.render(e,f,{store:d}),m=e,g=f}function a(e){for(var t in c.instances)if(c.instances.hasOwnProperty(t)){c.instances[t].id=e.id,delete c.instances[t],e=null;break}}var u={},c=r(1),p=r(2),f=null,l={},s=null,d=null,h={},g=null,m=null;u.init=function(e){h=e,e.routes.forEach(function(e){e.reg=p(e.path)}),c.extendPlugin("omi-router",function(e,t){e.setAttribute("href","javascript:void(0)"),e.addEventListener("click",function(){n(e.getAttribute("to"))},!1)});var t=window.location.hash.replace("#","");n(t?t:h.defaultRoute,f),t&&i()},window.addEventListener("hashchange",function(){n(window.location.hash.replace("#",""),f),i()},!1),u.destroy=function(){delete c.plugins["omi-router"]},e.exports=u}()},function(t,r){t.exports=e},function(e,t,r){function n(e,t){for(var r,n=[],o=0,i=0,a="",u=t&&t.delimiter||"/";null!=(r=x.exec(e));){var f=r[0],l=r[1],s=r.index;if(a+=e.slice(i,s),i=s+f.length,l)a+=l[1];else{var d=e[i],h=r[2],g=r[3],m=r[4],v=r[5],w=r[6],y=r[7];a&&(n.push(a),a="");var E=null!=h&&null!=d&&d!==h,b="+"===w||"*"===w,R="?"===w||"*"===w,$=r[2]||u,j=m||v;n.push({name:g||o++,prefix:h||"",delimiter:$,optional:R,repeat:b,partial:E,asterisk:!!y,pattern:j?p(j):y?".*":"[^"+c($)+"]+?"})}}return i<e.length&&(a+=e.substr(i)),a&&n.push(a),n}function o(e,t){return u(n(e,t))}function i(e){return encodeURI(e).replace(/[\/?#]/g,function(e){return"%"+e.charCodeAt(0).toString(16).toUpperCase()})}function a(e){return encodeURI(e).replace(/[?#]/g,function(e){return"%"+e.charCodeAt(0).toString(16).toUpperCase()})}function u(e){for(var t=new Array(e.length),r=0;r<e.length;r++)"object"==typeof e[r]&&(t[r]=new RegExp("^(?:"+e[r].pattern+")$"));return function(r,n){for(var o="",u=r||{},c=n||{},p=c.pretty?i:encodeURIComponent,f=0;f<e.length;f++){var l=e[f];if("string"!=typeof l){var s,d=u[l.name];if(null==d){if(l.optional){l.partial&&(o+=l.prefix);continue}throw new TypeError('Expected "'+l.name+'" to be defined')}if(v(d)){if(!l.repeat)throw new TypeError('Expected "'+l.name+'" to not repeat, but received `'+JSON.stringify(d)+"`");if(0===d.length){if(l.optional)continue;throw new TypeError('Expected "'+l.name+'" to not be empty')}for(var h=0;h<d.length;h++){if(s=p(d[h]),!t[f].test(s))throw new TypeError('Expected all "'+l.name+'" to match "'+l.pattern+'", but received `'+JSON.stringify(s)+"`");o+=(0===h?l.prefix:l.delimiter)+s}}else{if(s=l.asterisk?a(d):p(d),!t[f].test(s))throw new TypeError('Expected "'+l.name+'" to match "'+l.pattern+'", but received "'+s+'"');o+=l.prefix+s}}else o+=l}return o}}function c(e){return e.replace(/([.+*?=^!:${}()[\]|\/\\])/g,"\\$1")}function p(e){return e.replace(/([=!:$\/()])/g,"\\$1")}function f(e,t){return e.keys=t,e}function l(e){return e.sensitive?"":"i"}function s(e,t){var r=e.source.match(/\((?!\?)/g);if(r)for(var n=0;n<r.length;n++)t.push({name:n,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,asterisk:!1,pattern:null});return f(e,t)}function d(e,t,r){for(var n=[],o=0;o<e.length;o++)n.push(m(e[o],t,r).source);var i=new RegExp("(?:"+n.join("|")+")",l(r));return f(i,t)}function h(e,t,r){return g(n(e,r),t,r)}function g(e,t,r){v(t)||(r=t||r,t=[]),r=r||{};for(var n=r.strict,o=r.end!==!1,i="",a=0;a<e.length;a++){var u=e[a];if("string"==typeof u)i+=c(u);else{var p=c(u.prefix),s="(?:"+u.pattern+")";t.push(u),u.repeat&&(s+="(?:"+p+s+")*"),s=u.optional?u.partial?p+"("+s+")?":"(?:"+p+"("+s+"))?":p+"("+s+")",i+=s}}var d=c(r.delimiter||"/"),h=i.slice(-d.length)===d;return n||(i=(h?i.slice(0,-d.length):i)+"(?:"+d+"(?=$))?"),i+=o?"$":n&&h?"":"(?="+d+"|$)",f(new RegExp("^"+i,l(r)),t)}function m(e,t,r){return v(t)||(r=t||r,t=[]),r=r||{},e instanceof RegExp?s(e,t):v(e)?d(e,t,r):h(e,t,r)}var v=r(3);e.exports=m,e.exports.parse=n,e.exports.compile=o,e.exports.tokensToFunction=u,e.exports.tokensToRegExp=g;var x=new RegExp(["(\\\\.)","([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))"].join("|"),"g")},function(e,t){e.exports=Array.isArray||function(e){return"[object Array]"==Object.prototype.toString.call(e)}}])});

View File

@ -94,10 +94,9 @@
value: function install() {
_index2.default.init({
routes: [{ path: '/', component: _home2.default }, { path: '/about', component: _about2.default }, { path: '/user-list', component: _userList2.default }, { path: '/user/:name/category/:category', component: _user2.default }],
renderTo: "#view"
renderTo: "#view",
defaultRoute: '/'
});
_omi2.default.render(new _home2.default(), "#view");
}
}, {
key: 'style',
@ -3162,6 +3161,12 @@
hashMapping(dom.getAttribute('to'));
}, false);
});
var hash = window.location.hash.replace('#', '');
hashMapping(hash ? hash : routerOption.defaultRoute, renderTo);
if (hash) {
render();
}
};
function getParams(toArr, pathArr) {
@ -3196,6 +3201,10 @@
window.addEventListener('hashchange', function () {
hashMapping(window.location.hash.replace('#', ''), renderTo);
render();
}, false);
function render() {
if (store) {
store.$route = {};
store.$route.params = params;
@ -3218,7 +3227,7 @@
});
preInstance = instance;
preRenderTo = renderTo;
}, false);
}
function deleteInstance(instance) {
for (var key in Omi.instances) {

View File

@ -8,18 +8,17 @@ import UserList from './user-list.js'
class App extends Omi.Component {
install(){
install() {
OmiRouter.init({
routes : [
{ path: '/', component: Home },
{ path: '/about', component: About },
{ path: '/user-list', component: UserList },
{ path: '/user/:name/category/:category', component: User }
routes: [
{path: '/', component: Home},
{path: '/about', component: About},
{path: '/user-list', component: UserList},
{path: '/user/:name/category/:category', component: User}
],
renderTo:"#view"
renderTo: "#view",
defaultRoute: '/'
})
Omi.render(new Home(),"#view")
}
style(){

View File

@ -34,6 +34,12 @@
hashMapping(dom.getAttribute('to'))
}, false)
})
var hash = window.location.hash.replace('#', '')
hashMapping(hash ? hash : routerOption.defaultRoute, renderTo)
if(hash) {
render()
}
}
function getParams(toArr, pathArr) {
@ -68,6 +74,10 @@
window.addEventListener('hashchange', function() {
hashMapping(window.location.hash.replace('#',''), renderTo)
render()
}, false)
function render(){
if(store){
store.$route = { }
store.$route.params = params
@ -90,7 +100,7 @@
})
preInstance = instance
preRenderTo = renderTo
}, false)
}
function deleteInstance(instance){
for(var key in Omi.instances){

View File

@ -1,6 +1,6 @@
{
"name": "omi-router",
"version": "0.1.0",
"version": "0.1.1",
"description": "Router for Omi.",
"main": "omi-router.js",
"scripts": {