page.mjs 30 KB

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