page.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.page = factory());
  5. }(this, (function () { 'use strict';
  6. var isarray = Array.isArray || function (arr) {
  7. return Object.prototype.toString.call(arr) == '[object Array]';
  8. };
  9. /**
  10. * Expose `pathToRegexp`.
  11. */
  12. var pathToRegexp_1 = pathToRegexp;
  13. var parse_1 = parse;
  14. var compile_1 = compile;
  15. var tokensToFunction_1 = tokensToFunction;
  16. var tokensToRegExp_1 = tokensToRegExp;
  17. /**
  18. * The main path matching regexp utility.
  19. *
  20. * @type {RegExp}
  21. */
  22. var PATH_REGEXP = new RegExp([
  23. // Match escaped characters that would otherwise appear in future matches.
  24. // This allows the user to escape special characters that won't transform.
  25. '(\\\\.)',
  26. // Match Express-style parameters and un-named parameters with a prefix
  27. // and optional suffixes. Matches appear as:
  28. //
  29. // "/:test(\\d+)?" => ["/", "test", "\d+", undefined, "?", undefined]
  30. // "/route(\\d+)" => [undefined, undefined, undefined, "\d+", undefined, undefined]
  31. // "/*" => ["/", undefined, undefined, undefined, undefined, "*"]
  32. '([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^()])+)\\))?|\\(((?:\\\\.|[^()])+)\\))([+*?])?|(\\*))'
  33. ].join('|'), 'g');
  34. /**
  35. * Parse a string for the raw tokens.
  36. *
  37. * @param {String} str
  38. * @return {Array}
  39. */
  40. function parse (str) {
  41. var tokens = [];
  42. var key = 0;
  43. var index = 0;
  44. var path = '';
  45. var res;
  46. while ((res = PATH_REGEXP.exec(str)) != null) {
  47. var m = res[0];
  48. var escaped = res[1];
  49. var offset = res.index;
  50. path += str.slice(index, offset);
  51. index = offset + m.length;
  52. // Ignore already escaped sequences.
  53. if (escaped) {
  54. path += escaped[1];
  55. continue
  56. }
  57. // Push the current path onto the tokens.
  58. if (path) {
  59. tokens.push(path);
  60. path = '';
  61. }
  62. var prefix = res[2];
  63. var name = res[3];
  64. var capture = res[4];
  65. var group = res[5];
  66. var suffix = res[6];
  67. var asterisk = res[7];
  68. var repeat = suffix === '+' || suffix === '*';
  69. var optional = suffix === '?' || suffix === '*';
  70. var delimiter = prefix || '/';
  71. var pattern = capture || group || (asterisk ? '.*' : '[^' + delimiter + ']+?');
  72. tokens.push({
  73. name: name || key++,
  74. prefix: prefix || '',
  75. delimiter: delimiter,
  76. optional: optional,
  77. repeat: repeat,
  78. pattern: escapeGroup(pattern)
  79. });
  80. }
  81. // Match any characters still remaining.
  82. if (index < str.length) {
  83. path += str.substr(index);
  84. }
  85. // If the path exists, push it onto the end.
  86. if (path) {
  87. tokens.push(path);
  88. }
  89. return tokens
  90. }
  91. /**
  92. * Compile a string to a template function for the path.
  93. *
  94. * @param {String} str
  95. * @return {Function}
  96. */
  97. function compile (str) {
  98. return tokensToFunction(parse(str))
  99. }
  100. /**
  101. * Expose a method for transforming tokens into the path function.
  102. */
  103. function tokensToFunction (tokens) {
  104. // Compile all the tokens into regexps.
  105. var matches = new Array(tokens.length);
  106. // Compile all the patterns before compilation.
  107. for (var i = 0; i < tokens.length; i++) {
  108. if (typeof tokens[i] === 'object') {
  109. matches[i] = new RegExp('^' + tokens[i].pattern + '$');
  110. }
  111. }
  112. return function (obj) {
  113. var path = '';
  114. var data = obj || {};
  115. for (var i = 0; i < tokens.length; i++) {
  116. var token = tokens[i];
  117. if (typeof token === 'string') {
  118. path += token;
  119. continue
  120. }
  121. var value = data[token.name];
  122. var segment;
  123. if (value == null) {
  124. if (token.optional) {
  125. continue
  126. } else {
  127. throw new TypeError('Expected "' + token.name + '" to be defined')
  128. }
  129. }
  130. if (isarray(value)) {
  131. if (!token.repeat) {
  132. throw new TypeError('Expected "' + token.name + '" to not repeat, but received "' + value + '"')
  133. }
  134. if (value.length === 0) {
  135. if (token.optional) {
  136. continue
  137. } else {
  138. throw new TypeError('Expected "' + token.name + '" to not be empty')
  139. }
  140. }
  141. for (var j = 0; j < value.length; j++) {
  142. segment = encodeURIComponent(value[j]);
  143. if (!matches[i].test(segment)) {
  144. throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
  145. }
  146. path += (j === 0 ? token.prefix : token.delimiter) + segment;
  147. }
  148. continue
  149. }
  150. segment = encodeURIComponent(value);
  151. if (!matches[i].test(segment)) {
  152. throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
  153. }
  154. path += token.prefix + segment;
  155. }
  156. return path
  157. }
  158. }
  159. /**
  160. * Escape a regular expression string.
  161. *
  162. * @param {String} str
  163. * @return {String}
  164. */
  165. function escapeString (str) {
  166. return str.replace(/([.+*?=^!:${}()[\]|\/])/g, '\\$1')
  167. }
  168. /**
  169. * Escape the capturing group by escaping special characters and meaning.
  170. *
  171. * @param {String} group
  172. * @return {String}
  173. */
  174. function escapeGroup (group) {
  175. return group.replace(/([=!:$\/()])/g, '\\$1')
  176. }
  177. /**
  178. * Attach the keys as a property of the regexp.
  179. *
  180. * @param {RegExp} re
  181. * @param {Array} keys
  182. * @return {RegExp}
  183. */
  184. function attachKeys (re, keys) {
  185. re.keys = keys;
  186. return re
  187. }
  188. /**
  189. * Get the flags for a regexp from the options.
  190. *
  191. * @param {Object} options
  192. * @return {String}
  193. */
  194. function flags (options) {
  195. return options.sensitive ? '' : 'i'
  196. }
  197. /**
  198. * Pull out keys from a regexp.
  199. *
  200. * @param {RegExp} path
  201. * @param {Array} keys
  202. * @return {RegExp}
  203. */
  204. function regexpToRegexp (path, keys) {
  205. // Use a negative lookahead to match only capturing groups.
  206. var groups = path.source.match(/\((?!\?)/g);
  207. if (groups) {
  208. for (var i = 0; i < groups.length; i++) {
  209. keys.push({
  210. name: i,
  211. prefix: null,
  212. delimiter: null,
  213. optional: false,
  214. repeat: false,
  215. pattern: null
  216. });
  217. }
  218. }
  219. return attachKeys(path, keys)
  220. }
  221. /**
  222. * Transform an array into a regexp.
  223. *
  224. * @param {Array} path
  225. * @param {Array} keys
  226. * @param {Object} options
  227. * @return {RegExp}
  228. */
  229. function arrayToRegexp (path, keys, options) {
  230. var parts = [];
  231. for (var i = 0; i < path.length; i++) {
  232. parts.push(pathToRegexp(path[i], keys, options).source);
  233. }
  234. var regexp = new RegExp('(?:' + parts.join('|') + ')', flags(options));
  235. return attachKeys(regexp, keys)
  236. }
  237. /**
  238. * Create a path regexp from string input.
  239. *
  240. * @param {String} path
  241. * @param {Array} keys
  242. * @param {Object} options
  243. * @return {RegExp}
  244. */
  245. function stringToRegexp (path, keys, options) {
  246. var tokens = parse(path);
  247. var re = tokensToRegExp(tokens, options);
  248. // Attach keys back to the regexp.
  249. for (var i = 0; i < tokens.length; i++) {
  250. if (typeof tokens[i] !== 'string') {
  251. keys.push(tokens[i]);
  252. }
  253. }
  254. return attachKeys(re, keys)
  255. }
  256. /**
  257. * Expose a function for taking tokens and returning a RegExp.
  258. *
  259. * @param {Array} tokens
  260. * @param {Array} keys
  261. * @param {Object} options
  262. * @return {RegExp}
  263. */
  264. function tokensToRegExp (tokens, options) {
  265. options = options || {};
  266. var strict = options.strict;
  267. var end = options.end !== false;
  268. var route = '';
  269. var lastToken = tokens[tokens.length - 1];
  270. var endsWithSlash = typeof lastToken === 'string' && /\/$/.test(lastToken);
  271. // Iterate over the tokens and create our regexp string.
  272. for (var i = 0; i < tokens.length; i++) {
  273. var token = tokens[i];
  274. if (typeof token === 'string') {
  275. route += escapeString(token);
  276. } else {
  277. var prefix = escapeString(token.prefix);
  278. var capture = token.pattern;
  279. if (token.repeat) {
  280. capture += '(?:' + prefix + capture + ')*';
  281. }
  282. if (token.optional) {
  283. if (prefix) {
  284. capture = '(?:' + prefix + '(' + capture + '))?';
  285. } else {
  286. capture = '(' + capture + ')?';
  287. }
  288. } else {
  289. capture = prefix + '(' + capture + ')';
  290. }
  291. route += capture;
  292. }
  293. }
  294. // In non-strict mode we allow a slash at the end of match. If the path to
  295. // match already ends with a slash, we remove it for consistency. The slash
  296. // is valid at the end of a path match, not in the middle. This is important
  297. // in non-ending mode, where "/test/" shouldn't match "/test//route".
  298. if (!strict) {
  299. route = (endsWithSlash ? route.slice(0, -2) : route) + '(?:\\/(?=$))?';
  300. }
  301. if (end) {
  302. route += '$';
  303. } else {
  304. // In non-ending mode, we need the capturing groups to match as much as
  305. // possible by using a positive lookahead to the end or next path segment.
  306. route += strict && endsWithSlash ? '' : '(?=\\/|$)';
  307. }
  308. return new RegExp('^' + route, flags(options))
  309. }
  310. /**
  311. * Normalize the given path string, returning a regular expression.
  312. *
  313. * An empty array can be passed in for the keys, which will hold the
  314. * placeholder key descriptions. For example, using `/user/:id`, `keys` will
  315. * contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
  316. *
  317. * @param {(String|RegExp|Array)} path
  318. * @param {Array} [keys]
  319. * @param {Object} [options]
  320. * @return {RegExp}
  321. */
  322. function pathToRegexp (path, keys, options) {
  323. keys = keys || [];
  324. if (!isarray(keys)) {
  325. options = keys;
  326. keys = [];
  327. } else if (!options) {
  328. options = {};
  329. }
  330. if (path instanceof RegExp) {
  331. return regexpToRegexp(path, keys, options)
  332. }
  333. if (isarray(path)) {
  334. return arrayToRegexp(path, keys, options)
  335. }
  336. return stringToRegexp(path, keys, options)
  337. }
  338. pathToRegexp_1.parse = parse_1;
  339. pathToRegexp_1.compile = compile_1;
  340. pathToRegexp_1.tokensToFunction = tokensToFunction_1;
  341. pathToRegexp_1.tokensToRegExp = tokensToRegExp_1;
  342. /**
  343. * Module dependencies.
  344. */
  345. /**
  346. * Module exports.
  347. */
  348. var page_js = page;
  349. page.default = page;
  350. page.Context = Context;
  351. page.Route = Route;
  352. page.sameOrigin = sameOrigin;
  353. /**
  354. * Short-cuts for global-object checks
  355. */
  356. var hasDocument = ('undefined' !== typeof document);
  357. var hasWindow = ('undefined' !== typeof window);
  358. var hasHistory = ('undefined' !== typeof history);
  359. var hasProcess = typeof process !== 'undefined';
  360. /**
  361. * Detect click event
  362. */
  363. var clickEvent = hasDocument && document.ontouchstart ? 'touchstart' : 'click';
  364. /**
  365. * To work properly with the URL
  366. * history.location generated polyfill in https://github.com/devote/HTML5-History-API
  367. */
  368. var isLocation = hasWindow && !!(window.history.location || window.location);
  369. /**
  370. * Perform initial dispatch.
  371. */
  372. var dispatch = true;
  373. /**
  374. * Decode URL components (query string, pathname, hash).
  375. * Accommodates both regular percent encoding and x-www-form-urlencoded format.
  376. */
  377. var decodeURLComponents = true;
  378. /**
  379. * Base path.
  380. */
  381. var base = '';
  382. /**
  383. * Strict path matching.
  384. */
  385. var strict = false;
  386. /**
  387. * Running flag.
  388. */
  389. var running;
  390. /**
  391. * HashBang option
  392. */
  393. var hashbang = false;
  394. /**
  395. * Previous context, for capturing
  396. * page exit events.
  397. */
  398. var prevContext;
  399. /**
  400. * The window for which this `page` is running
  401. */
  402. var pageWindow;
  403. /**
  404. * Register `path` with callback `fn()`,
  405. * or route `path`, or redirection,
  406. * or `page.start()`.
  407. *
  408. * page(fn);
  409. * page('*', fn);
  410. * page('/user/:id', load, user);
  411. * page('/user/' + user.id, { some: 'thing' });
  412. * page('/user/' + user.id);
  413. * page('/from', '/to')
  414. * page();
  415. *
  416. * @param {string|!Function|!Object} path
  417. * @param {Function=} fn
  418. * @api public
  419. */
  420. function page(path, fn) {
  421. // <callback>
  422. if ('function' === typeof path) {
  423. return page('*', path);
  424. }
  425. // route <path> to <callback ...>
  426. if ('function' === typeof fn) {
  427. var route = new Route(/** @type {string} */ (path));
  428. for (var i = 1; i < arguments.length; ++i) {
  429. page.callbacks.push(route.middleware(arguments[i]));
  430. }
  431. // show <path> with [state]
  432. } else if ('string' === typeof path) {
  433. page['string' === typeof fn ? 'redirect' : 'show'](path, fn);
  434. // start [options]
  435. } else {
  436. page.start(path);
  437. }
  438. }
  439. /**
  440. * Callback functions.
  441. */
  442. page.callbacks = [];
  443. page.exits = [];
  444. /**
  445. * Current path being processed
  446. * @type {string}
  447. */
  448. page.current = '';
  449. /**
  450. * Number of pages navigated to.
  451. * @type {number}
  452. *
  453. * page.len == 0;
  454. * page('/login');
  455. * page.len == 1;
  456. */
  457. page.len = 0;
  458. /**
  459. * Get or set basepath to `path`.
  460. *
  461. * @param {string} path
  462. * @api public
  463. */
  464. page.base = function(path) {
  465. if (0 === arguments.length) return base;
  466. base = path;
  467. };
  468. /**
  469. * Get or set strict path matching to `enable`
  470. *
  471. * @param {boolean} enable
  472. * @api public
  473. */
  474. page.strict = function(enable) {
  475. if (0 === arguments.length) return strict;
  476. strict = enable;
  477. };
  478. /**
  479. * Bind with the given `options`.
  480. *
  481. * Options:
  482. *
  483. * - `click` bind to click events [true]
  484. * - `popstate` bind to popstate [true]
  485. * - `dispatch` perform initial dispatch [true]
  486. *
  487. * @param {Object} options
  488. * @api public
  489. */
  490. page.start = function(options) {
  491. options = options || {};
  492. if (running) return;
  493. running = true;
  494. pageWindow = options.window || (hasWindow && window);
  495. if (false === options.dispatch) dispatch = false;
  496. if (false === options.decodeURLComponents) decodeURLComponents = false;
  497. if (false !== options.popstate && hasWindow) pageWindow.addEventListener('popstate', onpopstate, false);
  498. if (false !== options.click && hasDocument) {
  499. pageWindow.document.addEventListener(clickEvent, onclick, false);
  500. }
  501. hashbang = !!options.hashbang;
  502. if(hashbang && hasWindow && !hasHistory) {
  503. pageWindow.addEventListener('hashchange', onpopstate, false);
  504. }
  505. if (!dispatch) return;
  506. var url;
  507. if(isLocation) {
  508. var loc = pageWindow.location;
  509. if(hashbang && ~loc.hash.indexOf('#!')) {
  510. url = loc.hash.substr(2) + loc.search;
  511. } else if (hashbang) {
  512. url = loc.search + loc.hash;
  513. } else {
  514. url = loc.pathname + loc.search + loc.hash;
  515. }
  516. }
  517. page.replace(url, null, true, dispatch);
  518. };
  519. /**
  520. * Unbind click and popstate event handlers.
  521. *
  522. * @api public
  523. */
  524. page.stop = function() {
  525. if (!running) return;
  526. page.current = '';
  527. page.len = 0;
  528. running = false;
  529. hasDocument && pageWindow.document.removeEventListener(clickEvent, onclick, false);
  530. hasWindow && pageWindow.removeEventListener('popstate', onpopstate, false);
  531. hasWindow && pageWindow.removeEventListener('hashchange', onpopstate, false);
  532. };
  533. /**
  534. * Show `path` with optional `state` object.
  535. *
  536. * @param {string} path
  537. * @param {Object=} state
  538. * @param {boolean=} dispatch
  539. * @param {boolean=} push
  540. * @return {!Context}
  541. * @api public
  542. */
  543. page.show = function(path, state, dispatch, push) {
  544. var ctx = new Context(path, state),
  545. prev = prevContext;
  546. prevContext = ctx;
  547. page.current = ctx.path;
  548. if (false !== dispatch) page.dispatch(ctx, prev);
  549. if (false !== ctx.handled && false !== push) ctx.pushState();
  550. return ctx;
  551. };
  552. /**
  553. * Goes back in the history
  554. * Back should always let the current route push state and then go back.
  555. *
  556. * @param {string} path - fallback path to go back if no more history exists, if undefined defaults to page.base
  557. * @param {Object=} state
  558. * @api public
  559. */
  560. page.back = function(path, state) {
  561. if (page.len > 0) {
  562. // this may need more testing to see if all browsers
  563. // wait for the next tick to go back in history
  564. hasHistory && pageWindow.history.back();
  565. page.len--;
  566. } else if (path) {
  567. setTimeout(function() {
  568. page.show(path, state);
  569. });
  570. }else{
  571. setTimeout(function() {
  572. page.show(getBase(), state);
  573. });
  574. }
  575. };
  576. /**
  577. * Register route to redirect from one path to other
  578. * or just redirect to another route
  579. *
  580. * @param {string} from - if param 'to' is undefined redirects to 'from'
  581. * @param {string=} to
  582. * @api public
  583. */
  584. page.redirect = function(from, to) {
  585. // Define route from a path to another
  586. if ('string' === typeof from && 'string' === typeof to) {
  587. page(from, function(e) {
  588. setTimeout(function() {
  589. page.replace(/** @type {!string} */ (to));
  590. }, 0);
  591. });
  592. }
  593. // Wait for the push state and replace it with another
  594. if ('string' === typeof from && 'undefined' === typeof to) {
  595. setTimeout(function() {
  596. page.replace(from);
  597. }, 0);
  598. }
  599. };
  600. /**
  601. * Replace `path` with optional `state` object.
  602. *
  603. * @param {string} path
  604. * @param {Object=} state
  605. * @param {boolean=} init
  606. * @param {boolean=} dispatch
  607. * @return {!Context}
  608. * @api public
  609. */
  610. page.replace = function(path, state, init, dispatch) {
  611. var ctx = new Context(path, state),
  612. prev = prevContext;
  613. prevContext = ctx;
  614. page.current = ctx.path;
  615. ctx.init = init;
  616. ctx.save(); // save before dispatching, which may redirect
  617. if (false !== dispatch) page.dispatch(ctx, prev);
  618. return ctx;
  619. };
  620. /**
  621. * Dispatch the given `ctx`.
  622. *
  623. * @param {Context} ctx
  624. * @api private
  625. */
  626. page.dispatch = function(ctx, prev) {
  627. var i = 0,
  628. j = 0;
  629. function nextExit() {
  630. var fn = page.exits[j++];
  631. if (!fn) return nextEnter();
  632. fn(prev, nextExit);
  633. }
  634. function nextEnter() {
  635. var fn = page.callbacks[i++];
  636. if (ctx.path !== page.current) {
  637. ctx.handled = false;
  638. return;
  639. }
  640. if (!fn) return unhandled(ctx);
  641. fn(ctx, nextEnter);
  642. }
  643. if (prev) {
  644. nextExit();
  645. } else {
  646. nextEnter();
  647. }
  648. };
  649. /**
  650. * Unhandled `ctx`. When it's not the initial
  651. * popstate then redirect. If you wish to handle
  652. * 404s on your own use `page('*', callback)`.
  653. *
  654. * @param {Context} ctx
  655. * @api private
  656. */
  657. function unhandled(ctx) {
  658. if (ctx.handled) return;
  659. var current;
  660. if (hashbang) {
  661. current = isLocation && getBase() + pageWindow.location.hash.replace('#!', '');
  662. } else {
  663. current = isLocation && pageWindow.location.pathname + pageWindow.location.search;
  664. }
  665. if (current === ctx.canonicalPath) return;
  666. page.stop();
  667. ctx.handled = false;
  668. isLocation && (pageWindow.location.href = ctx.canonicalPath);
  669. }
  670. /**
  671. * Register an exit route on `path` with
  672. * callback `fn()`, which will be called
  673. * on the previous context when a new
  674. * page is visited.
  675. */
  676. page.exit = function(path, fn) {
  677. if (typeof path === 'function') {
  678. return page.exit('*', path);
  679. }
  680. var route = new Route(path);
  681. for (var i = 1; i < arguments.length; ++i) {
  682. page.exits.push(route.middleware(arguments[i]));
  683. }
  684. };
  685. /**
  686. * Remove URL encoding from the given `str`.
  687. * Accommodates whitespace in both x-www-form-urlencoded
  688. * and regular percent-encoded form.
  689. *
  690. * @param {string} val - URL component to decode
  691. */
  692. function decodeURLEncodedURIComponent(val) {
  693. if (typeof val !== 'string') { return val; }
  694. return decodeURLComponents ? decodeURIComponent(val.replace(/\+/g, ' ')) : val;
  695. }
  696. /**
  697. * Initialize a new "request" `Context`
  698. * with the given `path` and optional initial `state`.
  699. *
  700. * @constructor
  701. * @param {string} path
  702. * @param {Object=} state
  703. * @api public
  704. */
  705. function Context(path, state) {
  706. var pageBase = getBase();
  707. if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path;
  708. var i = path.indexOf('?');
  709. this.canonicalPath = path;
  710. this.path = path.replace(pageBase, '') || '/';
  711. if (hashbang) this.path = this.path.replace('#!', '') || '/';
  712. this.title = (hasDocument && pageWindow.document.title);
  713. this.state = state || {};
  714. this.state.path = path;
  715. this.querystring = ~i ? decodeURLEncodedURIComponent(path.slice(i + 1)) : '';
  716. this.pathname = decodeURLEncodedURIComponent(~i ? path.slice(0, i) : path);
  717. this.params = {};
  718. // fragment
  719. this.hash = '';
  720. if (!hashbang) {
  721. if (!~this.path.indexOf('#')) return;
  722. var parts = this.path.split('#');
  723. this.path = this.pathname = parts[0];
  724. this.hash = decodeURLEncodedURIComponent(parts[1]) || '';
  725. this.querystring = this.querystring.split('#')[0];
  726. }
  727. }
  728. /**
  729. * Expose `Context`.
  730. */
  731. page.Context = Context;
  732. /**
  733. * Push state.
  734. *
  735. * @api private
  736. */
  737. Context.prototype.pushState = function() {
  738. page.len++;
  739. if (hasHistory) {
  740. pageWindow.history.pushState(this.state, this.title,
  741. hashbang && this.path !== '/' ? '#!' + this.path : this.canonicalPath);
  742. }
  743. };
  744. /**
  745. * Save the context state.
  746. *
  747. * @api public
  748. */
  749. Context.prototype.save = function() {
  750. if (hasHistory && pageWindow.location.protocol !== 'file:') {
  751. pageWindow.history.replaceState(this.state, this.title,
  752. hashbang && this.path !== '/' ? '#!' + this.path : this.canonicalPath);
  753. }
  754. };
  755. /**
  756. * Initialize `Route` with the given HTTP `path`,
  757. * and an array of `callbacks` and `options`.
  758. *
  759. * Options:
  760. *
  761. * - `sensitive` enable case-sensitive routes
  762. * - `strict` enable strict matching for trailing slashes
  763. *
  764. * @constructor
  765. * @param {string} path
  766. * @param {Object=} options
  767. * @api private
  768. */
  769. function Route(path, options) {
  770. options = options || {};
  771. options.strict = options.strict || strict;
  772. this.path = (path === '*') ? '(.*)' : path;
  773. this.method = 'GET';
  774. this.regexp = pathToRegexp_1(this.path,
  775. this.keys = [],
  776. options);
  777. }
  778. /**
  779. * Expose `Route`.
  780. */
  781. page.Route = Route;
  782. /**
  783. * Return route middleware with
  784. * the given callback `fn()`.
  785. *
  786. * @param {Function} fn
  787. * @return {Function}
  788. * @api public
  789. */
  790. Route.prototype.middleware = function(fn) {
  791. var self = this;
  792. return function(ctx, next) {
  793. if (self.match(ctx.path, ctx.params)) return fn(ctx, next);
  794. next();
  795. };
  796. };
  797. /**
  798. * Check if this route matches `path`, if so
  799. * populate `params`.
  800. *
  801. * @param {string} path
  802. * @param {Object} params
  803. * @return {boolean}
  804. * @api private
  805. */
  806. Route.prototype.match = function(path, params) {
  807. var keys = this.keys,
  808. qsIndex = path.indexOf('?'),
  809. pathname = ~qsIndex ? path.slice(0, qsIndex) : path,
  810. m = this.regexp.exec(decodeURIComponent(pathname));
  811. if (!m) return false;
  812. for (var i = 1, len = m.length; i < len; ++i) {
  813. var key = keys[i - 1];
  814. var val = decodeURLEncodedURIComponent(m[i]);
  815. if (val !== undefined || !(hasOwnProperty.call(params, key.name))) {
  816. params[key.name] = val;
  817. }
  818. }
  819. return true;
  820. };
  821. /**
  822. * Handle "populate" events.
  823. */
  824. var onpopstate = (function () {
  825. var loaded = false;
  826. if ( ! hasWindow ) {
  827. return;
  828. }
  829. if (hasDocument && document.readyState === 'complete') {
  830. loaded = true;
  831. } else {
  832. window.addEventListener('load', function() {
  833. setTimeout(function() {
  834. loaded = true;
  835. }, 0);
  836. });
  837. }
  838. return function onpopstate(e) {
  839. if (!loaded) return;
  840. if (e.state) {
  841. var path = e.state.path;
  842. page.replace(path, e.state);
  843. } else if (isLocation) {
  844. var loc = pageWindow.location;
  845. page.show(loc.pathname + loc.hash, undefined, undefined, false);
  846. }
  847. };
  848. })();
  849. /**
  850. * Handle "click" events.
  851. */
  852. /* jshint +W054 */
  853. function onclick(e) {
  854. if (1 !== which(e)) return;
  855. if (e.metaKey || e.ctrlKey || e.shiftKey) return;
  856. if (e.defaultPrevented) return;
  857. // ensure link
  858. // use shadow dom when available if not, fall back to composedPath() for browsers that only have shady
  859. var el = e.target;
  860. var eventPath = e.path || (e.composedPath ? e.composedPath() : null);
  861. if(eventPath) {
  862. for (var i = 0; i < eventPath.length; i++) {
  863. if (!eventPath[i].nodeName) continue;
  864. if (eventPath[i].nodeName.toUpperCase() !== 'A') continue;
  865. if (!eventPath[i].href) continue;
  866. el = eventPath[i];
  867. break;
  868. }
  869. }
  870. // continue ensure link
  871. // el.nodeName for svg links are 'a' instead of 'A'
  872. while (el && 'A' !== el.nodeName.toUpperCase()) el = el.parentNode;
  873. if (!el || 'A' !== el.nodeName.toUpperCase()) return;
  874. // check if link is inside an svg
  875. // in this case, both href and target are always inside an object
  876. var svg = (typeof el.href === 'object') && el.href.constructor.name === 'SVGAnimatedString';
  877. // Ignore if tag has
  878. // 1. "download" attribute
  879. // 2. rel="external" attribute
  880. if (el.hasAttribute('download') || el.getAttribute('rel') === 'external') return;
  881. // ensure non-hash for the same path
  882. var link = el.getAttribute('href');
  883. if(!hashbang && samePath(el) && (el.hash || '#' === link)) return;
  884. // Check for mailto: in the href
  885. if (link && link.indexOf('mailto:') > -1) return;
  886. // check target
  887. // svg target is an object and its desired value is in .baseVal property
  888. if (svg ? el.target.baseVal : el.target) return;
  889. // x-origin
  890. // note: svg links that are not relative don't call click events (and skip page.js)
  891. // consequently, all svg links tested inside page.js are relative and in the same origin
  892. if (!svg && !sameOrigin(el.href)) return;
  893. // rebuild path
  894. // There aren't .pathname and .search properties in svg links, so we use href
  895. // Also, svg href is an object and its desired value is in .baseVal property
  896. var path = svg ? el.href.baseVal : (el.pathname + el.search + (el.hash || ''));
  897. path = path[0] !== '/' ? '/' + path : path;
  898. // strip leading "/[drive letter]:" on NW.js on Windows
  899. if (hasProcess && path.match(/^\/[a-zA-Z]:\//)) {
  900. path = path.replace(/^\/[a-zA-Z]:\//, '/');
  901. }
  902. // same page
  903. var orig = path;
  904. var pageBase = getBase();
  905. if (path.indexOf(pageBase) === 0) {
  906. path = path.substr(base.length);
  907. }
  908. if (hashbang) path = path.replace('#!', '');
  909. if (pageBase && orig === path) return;
  910. e.preventDefault();
  911. page.show(orig);
  912. }
  913. /**
  914. * Event button.
  915. */
  916. function which(e) {
  917. e = e || (hasWindow && window.event);
  918. return null == e.which ? e.button : e.which;
  919. }
  920. /**
  921. * Convert to a URL object
  922. */
  923. function toURL(href) {
  924. if(typeof URL === 'function' && isLocation) {
  925. return new URL(href, location.toString());
  926. } else if (hasDocument) {
  927. var anc = document.createElement('a');
  928. anc.href = href;
  929. return anc;
  930. }
  931. }
  932. /**
  933. * Check if `href` is the same origin.
  934. */
  935. function sameOrigin(href) {
  936. if(!href || !isLocation) return false;
  937. var url = toURL(href);
  938. var loc = pageWindow.location;
  939. return loc.protocol === url.protocol &&
  940. loc.hostname === url.hostname &&
  941. loc.port === url.port;
  942. }
  943. function samePath(url) {
  944. if(!isLocation) return false;
  945. var loc = pageWindow.location;
  946. return url.pathname === loc.pathname &&
  947. url.search === loc.search;
  948. }
  949. /**
  950. * Gets the `base`, which depends on whether we are using History or
  951. * hashbang routing.
  952. */
  953. function getBase() {
  954. if(!!base) return base;
  955. var loc = hasWindow && pageWindow && pageWindow.location;
  956. return (hasWindow && hashbang && loc && loc.protocol === 'file:') ? loc.pathname : base;
  957. }
  958. page.sameOrigin = sameOrigin;
  959. return page_js;
  960. })));