routie.custom.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*!
  2. * routie - a tiny hash router
  3. * v0.3.2
  4. * http://projects.jga.me/routie
  5. * copyright Greg Allen 2013
  6. * MIT License
  7. */
  8. (function(w) {
  9. var routes = [];
  10. var map = {};
  11. var reference = "router";
  12. var oldReference = w[reference];
  13. var Route = function(path, name) {
  14. this.name = name;
  15. this.path = path;
  16. this.keys = [];
  17. this.fns = [];
  18. this.params = {};
  19. this.regex = pathToRegexp(this.path, this.keys, false, false);
  20. };
  21. Route.prototype.addHandler = function(fn) {
  22. this.fns.push(fn);
  23. };
  24. Route.prototype.removeHandler = function(fn) {
  25. for (var i = 0, c = this.fns.length; i < c; i++) {
  26. var f = this.fns[i];
  27. if (fn == f) {
  28. this.fns.splice(i, 1);
  29. return;
  30. }
  31. }
  32. };
  33. Route.prototype.run = function(params) {
  34. for (var i = 0, c = this.fns.length; i < c; i++) {
  35. this.fns[i].apply(this, params);
  36. }
  37. };
  38. Route.prototype.match = function(path, params){
  39. var m = this.regex.exec(path);
  40. if (!m) return false;
  41. for (var i = 1, len = m.length; i < len; ++i) {
  42. var key = this.keys[i - 1];
  43. var val = ('string' == typeof m[i]) ? decodeURIComponent(m[i]) : m[i];
  44. if (key) {
  45. this.params[key.name] = val;
  46. }
  47. params.push(val);
  48. }
  49. return true;
  50. };
  51. Route.prototype.toURL = function(params) {
  52. var path = this.path;
  53. for (var param in params) {
  54. path = path.replace('/:'+param, '/'+params[param]);
  55. }
  56. path = path.replace(/\/:.*\?/g, '/').replace(/\?/g, '');
  57. if (path.indexOf(':') != -1) {
  58. throw new Error('missing parameters for url: '+path);
  59. }
  60. return path;
  61. };
  62. var pathToRegexp = function(path, keys, sensitive, strict) {
  63. if (path instanceof RegExp) return path;
  64. if (path instanceof Array) path = '(' + path.join('|') + ')';
  65. path = path
  66. .concat(strict ? '' : '/?')
  67. .replace(/\/\(/g, '(?:/')
  68. .replace(/\+/g, '__plus__')
  69. .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
  70. keys.push({ name: key, optional: !! optional });
  71. slash = slash || '';
  72. return '' + (optional ? '' : slash) + '(?:' + (optional ? slash : '') + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')' + (optional || '');
  73. })
  74. .replace(/([\/.])/g, '\\$1')
  75. .replace(/__plus__/g, '(.+)')
  76. .replace(/\*/g, '(.*)');
  77. return new RegExp('^' + path + '$', sensitive ? '' : 'i');
  78. };
  79. var addHandler = function(path, fn) {
  80. var s = path.split(' ');
  81. var name = (s.length == 2) ? s[0] : null;
  82. path = (s.length == 2) ? s[1] : s[0];
  83. if (!map[path]) {
  84. map[path] = new Route(path, name);
  85. routes.push(map[path]);
  86. }
  87. map[path].addHandler(fn);
  88. };
  89. var routie = function(path, fn) {
  90. if (typeof fn == 'function') {
  91. addHandler(path, fn);
  92. } else if (typeof path == 'object') {
  93. for (var p in path) {
  94. addHandler(p, path[p]);
  95. }
  96. } else if (typeof fn === 'undefined') {
  97. routie.navigate(path);
  98. }
  99. };
  100. routie.lookup = function(name, obj) {
  101. for (var i = 0, c = routes.length; i < c; i++) {
  102. var route = routes[i];
  103. if (route.name == name) {
  104. return route.toURL(obj);
  105. }
  106. }
  107. };
  108. routie.remove = function(path, fn) {
  109. var route = map[path];
  110. if (!route)
  111. return;
  112. route.removeHandler(fn);
  113. };
  114. routie.removeAll = function() {
  115. map = {};
  116. routes = [];
  117. };
  118. routie.navigate = function(path, options) {
  119. options = options || {};
  120. var silent = options.silent || false;
  121. if (silent) {
  122. removeListener();
  123. }
  124. setTimeout(function() {
  125. window.location.hash = path;
  126. if (silent) {
  127. setTimeout(function() {
  128. addListener();
  129. }, 1);
  130. }
  131. }, 1);
  132. };
  133. routie.noConflict = function() {
  134. w[reference] = oldReference;
  135. return routie;
  136. };
  137. var getHash = function() {
  138. return window.location.hash.substring(1);
  139. };
  140. var checkRoute = function(hash, route) {
  141. var params = [];
  142. if (route.match(hash, params)) {
  143. route.run(params);
  144. return true;
  145. }
  146. return false;
  147. };
  148. var hashChanged = routie.reload = function() {
  149. var hash = getHash();
  150. for (var i = 0, c = routes.length; i < c; i++) {
  151. var route = routes[i];
  152. if (checkRoute(hash, route)) {
  153. return;
  154. }
  155. }
  156. };
  157. var addListener = function() {
  158. if (w.addEventListener) {
  159. w.addEventListener('hashchange', hashChanged, false);
  160. } else {
  161. w.attachEvent('onhashchange', hashChanged);
  162. }
  163. };
  164. var removeListener = function() {
  165. if (w.removeEventListener) {
  166. w.removeEventListener('hashchange', hashChanged);
  167. } else {
  168. w.detachEvent('onhashchange', hashChanged);
  169. }
  170. };
  171. addListener();
  172. w[reference] = routie;
  173. })(window);