Skip to content

Commit

Permalink
add dist
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengmz committed Mar 15, 2019
1 parent fdd0d83 commit 7b36a60
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 3 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
*.log
*.swp
dist
132 changes: 132 additions & 0 deletions dist/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// UMD封装
(function (global, factory) {
if (typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if (typeof define === 'function' && define.amd)
define('Router', [], factory);
else if (typeof exports === 'object')
exports.Router = factory();
else
global.Router = factory();
}(this, (function () {
'use strict';

//构造函数
function Router() {
this.version = '1.0.0';
}

//内部变量与函数
var routes = [],
mode = null, // 路由模式
root = window.location.pathname, // 路由根目录
// 清除前后/字符
clearSlashes = function (path) {
return path.toString().replace(/\/$/, '').replace(/^\//, '');
};

//原型继承
Router.prototype = {
// 配置
config: function (options) {
mode = options && options.mode && options.mode == 'history' &&
!!(history.pushState) ? 'history' : 'hash';
//root = options && options.root ? '/' + clearSlashes(options.root) + '/' : '/';
// 支持对具体文件的路由如, 如/test/router/a.html
root = options && options.root ? options.root : root;
if (mode === 'history') {
root = '/' + clearSlashes(root) + '/';
}
return this;
},
// 获取当前URL, 含查询内容
getFragment: function () {
var fragment = '';
if (mode === 'history') {
fragment = clearSlashes(decodeURI(location.pathname + location.search));
//删除?以后(含)的内容
fragment = fragment.replace(/\?(.*)$/, '');
//root会自动增加前后的'/'符号, 所以fragment也需要加, 然后替换
fragment = '/' + fragment + '/';
fragment = root != '/' ? fragment.replace(root, '') : fragment;
} else {
var match = window.location.href.match(/#(.*)$/);
fragment = match ? match[1] : '';
}
return clearSlashes(fragment);
},
// 增加路由
add: function (re, handler) {
if (typeof re == 'function') {
handler = re;
re = '';
}
routes.push({
re: re,
handler: handler
});
return this;
},
// 删除路由
remove: function (param) {
for (var i = 0, r; r = routes[i], i < routes.length; i++) {
if (r.handler === param || r.re.toString() === param.toString()) {
routes.splice(i, 1);
return this;
}
}
return this;
},
// 清除路由, 系统初始化
flush: function () {
routes = [];
mode = null;
//root = '/';
root = window.location.pathname;
return this;
},
// 检测入口并处理URL
check: function (f) {
var fragment = f || this.getFragment();
for (var i = 0; i < routes.length; i++) {
var match = fragment.match(routes[i].re);
if (match) {
match.shift();
routes[i].handler.apply({}, match);
return this;
}
}
return this;
},
// 监控变化:使用这种方式可以支持到ie6, 用事件方式只能支持ie8+
listen: function () {
var self = this;
var current = self.getFragment();
var fn = function () {
if (current !== self.getFragment()) {
current = self.getFragment();
self.check(current);
}
};
clearInterval(this.interval);
this.interval = setInterval(fn, 50);
return this;
},
// 改变当前URL以及触发处理
navigate: function (path) {
path = path ? path : '';
if (mode === 'history') {
history.pushState(null, null, root + clearSlashes(path));
} else {
//window.location.href = window.location.href.replace(/#(.*)$/, '') + '#' + path;
window.location.href = root + '#' + path;
}
return this;
}
};

//原型构造函数修改
Router.prototype.constructor = Router;
//实例化
return new Router();
})));
1 change: 1 addition & 0 deletions dist/router.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "routerjs",
"version": "1.0.0",
"version": "0.1.0",
"description": "pure router for javascript",
"main": "router.js",
"scripts": {
Expand All @@ -12,7 +12,10 @@
},
"keywords": [
"router",
"javascript"
"pure",
"indepence",
"javascript",
"ie5+"
],
"author": "zhengmz",
"license": "MIT",
Expand Down

0 comments on commit 7b36a60

Please sign in to comment.