Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. // mini front end router library
  2.  
  3. var Router = {
  4. routes: [],
  5. mode: null,
  6. root: '/',
  7. config: function (options) {
  8. this.mode = options && options.mode && options.mode == 'history'
  9. && !!(history.pushState) ? 'history' : 'hash';
  10. this.root = options && options.root ? '/' + this.clearSlashes(options.root) + '/' : '/';
  11. return this;
  12. },
  13. getFragment: function () {
  14. var fragment = '';
  15. if (this.mode === 'history') {
  16. fragment = this.clearSlashes(decodeURI(location.pathname + location.search));
  17. fragment = fragment.replace(/\?(.*)$/, '');
  18. fragment = this.root != '/' ? fragment.replace(this.root, '') : fragment;
  19. } else {
  20. var match = window.location.href.match(/#(.*)$/);
  21. fragment = match ? match[1] : '';
  22. }
  23. return this.clearSlashes(fragment);
  24. },
  25. clearSlashes: function (path) {
  26. return path.toString().replace(/\/$/, '').replace(/^\//, '');
  27. },
  28. add: function (re, handler) {
  29. if (typeof re == 'function') {
  30. handler = re;
  31. re = '';
  32. }
  33. this.routes.push({ re: re, handler: handler });
  34. return this;
  35. },
  36. remove: function (param) {
  37. for (var i = 0, r; i < this.routes.length, r = this.routes[i]; i++) {
  38. if (r.handler === param || r.re.toString() === param.toString()) {
  39. this.routes.splice(i, 1);
  40. return this;
  41. }
  42. }
  43. return this;
  44. },
  45. flush: function () {
  46. this.routes = [];
  47. this.mode = null;
  48. this.root = '/';
  49. return this;
  50. },
  51. check: function (f) {
  52. var fragment = f || this.getFragment();
  53. for (var i = 0; i < this.routes.length; i++) {
  54. var match = fragment.match(this.routes[i].re);
  55. if (match) {
  56. match.shift();
  57. this.routes[i].handler.apply({}, match);
  58. return this;
  59. }
  60. }
  61. return this;
  62. },
  63. listen: function () {
  64. var self = this;
  65. var current = self.getFragment();
  66. var fn = function () {
  67. console.log('current', current, 'self.getFragment()', self.getFragment());
  68.  
  69. if (current !== self.getFragment()) {
  70. current = self.getFragment();
  71. self.check(current);
  72. }
  73. }
  74. clearInterval(this.interval);
  75. this.interval = setInterval(fn, 50);
  76. return this;
  77. },
  78. navigate: function (path) {
  79. path = path ? path : '';
  80. if (this.mode === 'history') {
  81. history.pushState(null, null, this.root + this.clearSlashes(path));
  82. } else {
  83. window.location.href = window.location.href.replace(/#(.*)$/, '') + '#' + path;
  84. }
  85. return this;
  86. }
  87. }
  88.  
  89. Router.config({ mode: 'history' });
  90.  
  91.  
  92. Router
  93. .add(/about/, function () {
  94. console.log('about');
  95. })
  96. .add(/products\/(.*)\/edit\/(.*)/, function () {
  97. console.log('products', [...arguments]);
  98. })
  99. .add(/hello/, function name() {
  100. console.log('hello--->');
  101. })
  102. .add(function () {
  103. console.log('default');
  104. })
  105. .check('/products/12/edit/22').listen();
  106.  
  107. setTimeout(() => {
  108. Router.navigate('/about');
  109.  
  110. }, 2000)
  111.  
  112. setTimeout(() => {
  113. Router.navigate('/products/12/edit/22');
  114.  
  115. }, 4000)
  116.  
  117. setTimeout(() => {
  118. Router.navigate('hello');
  119.  
  120. }, 6000)
  121.  
  122. setTimeout(() => {
  123. Router.navigate('/');
  124.  
  125. }, 8000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement