page.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. * Short-cuts for global-object checks
  347. */
  348. var hasDocument = ('undefined' !== typeof document);
  349. var hasWindow = ('undefined' !== typeof window);
  350. var hasHistory = ('undefined' !== typeof history);
  351. var hasProcess = typeof process !== 'undefined';
  352. /**
  353. * Detect click event
  354. */
  355. var clickEvent = hasDocument && document.ontouchstart ? 'touchstart' : 'click';
  356. /**
  357. * To work properly with the URL
  358. * history.location generated polyfill in https://github.com/devote/HTML5-History-API
  359. */
  360. var isLocation = hasWindow && !!(window.history.location || window.location);
  361. /**
  362. * The page instance
  363. * @api private
  364. */
  365. function Page() {
  366. // public things
  367. this.callbacks = [];
  368. this.exits = [];
  369. this.current = '';
  370. this.len = 0;
  371. // private things
  372. this._decodeURLComponents = true;
  373. this._base = '';
  374. this._strict = false;
  375. this._running = false;
  376. this._hashbang = false;
  377. // bound functions
  378. this.clickHandler = this.clickHandler.bind(this);
  379. this._onpopstate = this._onpopstate.bind(this);
  380. }
  381. /**
  382. * Configure the instance of page. This can be called multiple times.
  383. *
  384. * @param {Object} options
  385. * @api public
  386. */
  387. Page.prototype.configure = function(options) {
  388. var opts = options || {};
  389. this._window = opts.window || (hasWindow && window);
  390. this._decodeURLComponents = opts.decodeURLComponents !== false;
  391. this._popstate = opts.popstate !== false && hasWindow;
  392. this._click = opts.click !== false && hasDocument;
  393. this._hashbang = !!opts.hashbang;
  394. var _window = this._window;
  395. if(this._popstate) {
  396. _window.addEventListener('popstate', this._onpopstate, false);
  397. } else if(hasWindow) {
  398. _window.removeEventListener('popstate', this._onpopstate, false);
  399. }
  400. if (this._click) {
  401. _window.document.addEventListener(clickEvent, this.clickHandler, false);
  402. } else if(hasDocument) {
  403. _window.document.removeEventListener(clickEvent, this.clickHandler, false);
  404. }
  405. if(this._hashbang && hasWindow && !hasHistory) {
  406. _window.addEventListener('hashchange', this._onpopstate, false);
  407. } else if(hasWindow) {
  408. _window.removeEventListener('hashchange', this._onpopstate, false);
  409. }
  410. };
  411. /**
  412. * Get or set basepath to `path`.
  413. *
  414. * @param {string} path
  415. * @api public
  416. */
  417. Page.prototype.base = function(path) {
  418. if (0 === arguments.length) return this._base;
  419. this._base = path;
  420. };
  421. /**
  422. * Gets the `base`, which depends on whether we are using History or
  423. * hashbang routing.
  424. * @api private
  425. */
  426. Page.prototype._getBase = function() {
  427. var base = this._base;
  428. if(!!base) return base;
  429. var loc = hasWindow && this._window && this._window.location;
  430. if(hasWindow && this._hashbang && loc && loc.protocol === 'file:') {
  431. base = loc.pathname;
  432. }
  433. return base;
  434. };
  435. /**
  436. * Get or set strict path matching to `enable`
  437. *
  438. * @param {boolean} enable
  439. * @api public
  440. */
  441. Page.prototype.strict = function(enable) {
  442. if (0 === arguments.length) return this._strict;
  443. this._strict = enable;
  444. };
  445. /**
  446. * Bind with the given `options`.
  447. *
  448. * Options:
  449. *
  450. * - `click` bind to click events [true]
  451. * - `popstate` bind to popstate [true]
  452. * - `dispatch` perform initial dispatch [true]
  453. *
  454. * @param {Object} options
  455. * @api public
  456. */
  457. Page.prototype.start = function(options) {
  458. var opts = options || {};
  459. this.configure(opts);
  460. if (false === opts.dispatch) return;
  461. this._running = true;
  462. var url;
  463. if(isLocation) {
  464. var window = this._window;
  465. var loc = window.location;
  466. if(this._hashbang && ~loc.hash.indexOf('#!')) {
  467. url = loc.hash.substr(2) + loc.search;
  468. } else if (this._hashbang) {
  469. url = loc.search + loc.hash;
  470. } else {
  471. url = loc.pathname + loc.search + loc.hash;
  472. }
  473. }
  474. this.replace(url, null, true, opts.dispatch);
  475. };
  476. /**
  477. * Unbind click and popstate event handlers.
  478. *
  479. * @api public
  480. */
  481. Page.prototype.stop = function() {
  482. if (!this._running) return;
  483. this.current = '';
  484. this.len = 0;
  485. this._running = false;
  486. var window = this._window;
  487. this._click && window.document.removeEventListener(clickEvent, this.clickHandler, false);
  488. hasWindow && window.removeEventListener('popstate', this._onpopstate, false);
  489. hasWindow && window.removeEventListener('hashchange', this._onpopstate, false);
  490. };
  491. /**
  492. * Show `path` with optional `state` object.
  493. *
  494. * @param {string} path
  495. * @param {Object=} state
  496. * @param {boolean=} dispatch
  497. * @param {boolean=} push
  498. * @return {!Context}
  499. * @api public
  500. */
  501. Page.prototype.show = function(path, state, dispatch, push) {
  502. var ctx = new Context(path, state, this),
  503. prev = this.prevContext;
  504. this.prevContext = ctx;
  505. this.current = ctx.path;
  506. if (false !== dispatch) this.dispatch(ctx, prev);
  507. if (false !== ctx.handled && false !== push) ctx.pushState();
  508. return ctx;
  509. };
  510. /**
  511. * Goes back in the history
  512. * Back should always let the current route push state and then go back.
  513. *
  514. * @param {string} path - fallback path to go back if no more history exists, if undefined defaults to page.base
  515. * @param {Object=} state
  516. * @api public
  517. */
  518. Page.prototype.back = function(path, state) {
  519. var page = this;
  520. if (this.len > 0) {
  521. var window = this._window;
  522. // this may need more testing to see if all browsers
  523. // wait for the next tick to go back in history
  524. hasHistory && window.history.back();
  525. this.len--;
  526. } else if (path) {
  527. setTimeout(function() {
  528. page.show(path, state);
  529. });
  530. } else {
  531. setTimeout(function() {
  532. page.show(page._getBase(), state);
  533. });
  534. }
  535. };
  536. /**
  537. * Register route to redirect from one path to other
  538. * or just redirect to another route
  539. *
  540. * @param {string} from - if param 'to' is undefined redirects to 'from'
  541. * @param {string=} to
  542. * @api public
  543. */
  544. Page.prototype.redirect = function(from, to) {
  545. var inst = this;
  546. // Define route from a path to another
  547. if ('string' === typeof from && 'string' === typeof to) {
  548. page.call(this, from, function(e) {
  549. setTimeout(function() {
  550. inst.replace(/** @type {!string} */ (to));
  551. }, 0);
  552. });
  553. }
  554. // Wait for the push state and replace it with another
  555. if ('string' === typeof from && 'undefined' === typeof to) {
  556. setTimeout(function() {
  557. inst.replace(from);
  558. }, 0);
  559. }
  560. };
  561. /**
  562. * Replace `path` with optional `state` object.
  563. *
  564. * @param {string} path
  565. * @param {Object=} state
  566. * @param {boolean=} init
  567. * @param {boolean=} dispatch
  568. * @return {!Context}
  569. * @api public
  570. */
  571. Page.prototype.replace = function(path, state, init, dispatch) {
  572. var ctx = new Context(path, state, this),
  573. prev = this.prevContext;
  574. this.prevContext = ctx;
  575. this.current = ctx.path;
  576. ctx.init = init;
  577. ctx.save(); // save before dispatching, which may redirect
  578. if (false !== dispatch) this.dispatch(ctx, prev);
  579. return ctx;
  580. };
  581. /**
  582. * Dispatch the given `ctx`.
  583. *
  584. * @param {Context} ctx
  585. * @api private
  586. */
  587. Page.prototype.dispatch = function(ctx, prev) {
  588. var i = 0, j = 0, page = this;
  589. function nextExit() {
  590. var fn = page.exits[j++];
  591. if (!fn) return nextEnter();
  592. fn(prev, nextExit);
  593. }
  594. function nextEnter() {
  595. var fn = page.callbacks[i++];
  596. if (ctx.path !== page.current) {
  597. ctx.handled = false;
  598. return;
  599. }
  600. if (!fn) return unhandled.call(page, ctx);
  601. fn(ctx, nextEnter);
  602. }
  603. if (prev) {
  604. nextExit();
  605. } else {
  606. nextEnter();
  607. }
  608. };
  609. /**
  610. * Register an exit route on `path` with
  611. * callback `fn()`, which will be called
  612. * on the previous context when a new
  613. * page is visited.
  614. */
  615. Page.prototype.exit = function(path, fn) {
  616. if (typeof path === 'function') {
  617. return this.exit('*', path);
  618. }
  619. var route = new Route(path, null, this);
  620. for (var i = 1; i < arguments.length; ++i) {
  621. this.exits.push(route.middleware(arguments[i]));
  622. }
  623. };
  624. /**
  625. * Handle "click" events.
  626. */
  627. /* jshint +W054 */
  628. Page.prototype.clickHandler = function(e) {
  629. if (1 !== this._which(e)) return;
  630. if (e.metaKey || e.ctrlKey || e.shiftKey) return;
  631. if (e.defaultPrevented) return;
  632. // ensure link
  633. // use shadow dom when available if not, fall back to composedPath()
  634. // for browsers that only have shady
  635. var el = e.target;
  636. var eventPath = e.path || (e.composedPath ? e.composedPath() : null);
  637. if(eventPath) {
  638. for (var i = 0; i < eventPath.length; i++) {
  639. if (!eventPath[i].nodeName) continue;
  640. if (eventPath[i].nodeName.toUpperCase() !== 'A') continue;
  641. if (!eventPath[i].href) continue;
  642. el = eventPath[i];
  643. break;
  644. }
  645. }
  646. // continue ensure link
  647. // el.nodeName for svg links are 'a' instead of 'A'
  648. while (el && 'A' !== el.nodeName.toUpperCase()) el = el.parentNode;
  649. if (!el || 'A' !== el.nodeName.toUpperCase()) return;
  650. // check if link is inside an svg
  651. // in this case, both href and target are always inside an object
  652. var svg = (typeof el.href === 'object') && el.href.constructor.name === 'SVGAnimatedString';
  653. // Ignore if tag has
  654. // 1. "download" attribute
  655. // 2. rel="external" attribute
  656. if (el.hasAttribute('download') || el.getAttribute('rel') === 'external') return;
  657. // ensure non-hash for the same path
  658. var link = el.getAttribute('href');
  659. if(!this._hashbang && this._samePath(el) && (el.hash || '#' === link)) return;
  660. // Check for mailto: in the href
  661. if (link && link.indexOf('mailto:') > -1) return;
  662. // check target
  663. // svg target is an object and its desired value is in .baseVal property
  664. if (svg ? el.target.baseVal : el.target) return;
  665. // x-origin
  666. // note: svg links that are not relative don't call click events (and skip page.js)
  667. // consequently, all svg links tested inside page.js are relative and in the same origin
  668. if (!svg && !this.sameOrigin(el.href)) return;
  669. // rebuild path
  670. // There aren't .pathname and .search properties in svg links, so we use href
  671. // Also, svg href is an object and its desired value is in .baseVal property
  672. var path = svg ? el.href.baseVal : (el.pathname + el.search + (el.hash || ''));
  673. path = path[0] !== '/' ? '/' + path : path;
  674. // strip leading "/[drive letter]:" on NW.js on Windows
  675. if (hasProcess && path.match(/^\/[a-zA-Z]:\//)) {
  676. path = path.replace(/^\/[a-zA-Z]:\//, '/');
  677. }
  678. // same page
  679. var orig = path;
  680. var pageBase = this._getBase();
  681. if (path.indexOf(pageBase) === 0) {
  682. path = path.substr(pageBase.length);
  683. }
  684. if (this._hashbang) path = path.replace('#!', '');
  685. if (pageBase && orig === path && (!isLocation || this._window.location.protocol !== 'file:')) {
  686. return;
  687. }
  688. e.preventDefault();
  689. this.show(orig);
  690. };
  691. /**
  692. * Handle "populate" events.
  693. * @api private
  694. */
  695. Page.prototype._onpopstate = (function () {
  696. var loaded = false;
  697. if ( ! hasWindow ) {
  698. return function () {};
  699. }
  700. if (hasDocument && document.readyState === 'complete') {
  701. loaded = true;
  702. } else {
  703. window.addEventListener('load', function() {
  704. setTimeout(function() {
  705. loaded = true;
  706. }, 0);
  707. });
  708. }
  709. return function onpopstate(e) {
  710. if (!loaded) return;
  711. var page = this;
  712. if (e.state) {
  713. var path = e.state.path;
  714. page.replace(path, e.state);
  715. } else if (isLocation) {
  716. var loc = page._window.location;
  717. page.show(loc.pathname + loc.search + loc.hash, undefined, undefined, false);
  718. }
  719. };
  720. })();
  721. /**
  722. * Event button.
  723. */
  724. Page.prototype._which = function(e) {
  725. e = e || (hasWindow && this._window.event);
  726. return null == e.which ? e.button : e.which;
  727. };
  728. /**
  729. * Convert to a URL object
  730. * @api private
  731. */
  732. Page.prototype._toURL = function(href) {
  733. var window = this._window;
  734. if(typeof URL === 'function' && isLocation) {
  735. return new URL(href, window.location.toString());
  736. } else if (hasDocument) {
  737. var anc = window.document.createElement('a');
  738. anc.href = href;
  739. return anc;
  740. }
  741. };
  742. /**
  743. * Check if `href` is the same origin.
  744. * @param {string} href
  745. * @api public
  746. */
  747. Page.prototype.sameOrigin = function(href) {
  748. if(!href || !isLocation) return false;
  749. var url = this._toURL(href);
  750. var window = this._window;
  751. var loc = window.location;
  752. return loc.protocol === url.protocol &&
  753. loc.hostname === url.hostname &&
  754. loc.port === url.port;
  755. };
  756. /**
  757. * @api private
  758. */
  759. Page.prototype._samePath = function(url) {
  760. if(!isLocation) return false;
  761. var window = this._window;
  762. var loc = window.location;
  763. return url.pathname === loc.pathname &&
  764. url.search === loc.search;
  765. };
  766. /**
  767. * Remove URL encoding from the given `str`.
  768. * Accommodates whitespace in both x-www-form-urlencoded
  769. * and regular percent-encoded form.
  770. *
  771. * @param {string} val - URL component to decode
  772. * @api private
  773. */
  774. Page.prototype._decodeURLEncodedURIComponent = function(val) {
  775. if (typeof val !== 'string') { return val; }
  776. return this._decodeURLComponents ? decodeURIComponent(val.replace(/\+/g, ' ')) : val;
  777. };
  778. /**
  779. * Create a new `page` instance and function
  780. */
  781. function createPage() {
  782. var pageInstance = new Page();
  783. function pageFn(/* args */) {
  784. return page.apply(pageInstance, arguments);
  785. }
  786. // Copy all of the things over. In 2.0 maybe we use setPrototypeOf
  787. pageFn.callbacks = pageInstance.callbacks;
  788. pageFn.exits = pageInstance.exits;
  789. pageFn.base = pageInstance.base.bind(pageInstance);
  790. pageFn.strict = pageInstance.strict.bind(pageInstance);
  791. pageFn.start = pageInstance.start.bind(pageInstance);
  792. pageFn.stop = pageInstance.stop.bind(pageInstance);
  793. pageFn.show = pageInstance.show.bind(pageInstance);
  794. pageFn.back = pageInstance.back.bind(pageInstance);
  795. pageFn.redirect = pageInstance.redirect.bind(pageInstance);
  796. pageFn.replace = pageInstance.replace.bind(pageInstance);
  797. pageFn.dispatch = pageInstance.dispatch.bind(pageInstance);
  798. pageFn.exit = pageInstance.exit.bind(pageInstance);
  799. pageFn.configure = pageInstance.configure.bind(pageInstance);
  800. pageFn.sameOrigin = pageInstance.sameOrigin.bind(pageInstance);
  801. pageFn.clickHandler = pageInstance.clickHandler.bind(pageInstance);
  802. pageFn.create = createPage;
  803. Object.defineProperty(pageFn, 'len', {
  804. get: function(){
  805. return pageInstance.len;
  806. },
  807. set: function(val) {
  808. pageInstance.len = val;
  809. }
  810. });
  811. Object.defineProperty(pageFn, 'current', {
  812. get: function(){
  813. return pageInstance.current;
  814. },
  815. set: function(val) {
  816. pageInstance.current = val;
  817. }
  818. });
  819. // In 2.0 these can be named exports
  820. pageFn.Context = Context;
  821. pageFn.Route = Route;
  822. return pageFn;
  823. }
  824. /**
  825. * Register `path` with callback `fn()`,
  826. * or route `path`, or redirection,
  827. * or `page.start()`.
  828. *
  829. * page(fn);
  830. * page('*', fn);
  831. * page('/user/:id', load, user);
  832. * page('/user/' + user.id, { some: 'thing' });
  833. * page('/user/' + user.id);
  834. * page('/from', '/to')
  835. * page();
  836. *
  837. * @param {string|!Function|!Object} path
  838. * @param {Function=} fn
  839. * @api public
  840. */
  841. function page(path, fn) {
  842. // <callback>
  843. if ('function' === typeof path) {
  844. return page.call(this, '*', path);
  845. }
  846. // route <path> to <callback ...>
  847. if ('function' === typeof fn) {
  848. var route = new Route(/** @type {string} */ (path), null, this);
  849. for (var i = 1; i < arguments.length; ++i) {
  850. this.callbacks.push(route.middleware(arguments[i]));
  851. }
  852. // show <path> with [state]
  853. } else if ('string' === typeof path) {
  854. this['string' === typeof fn ? 'redirect' : 'show'](path, fn);
  855. // start [options]
  856. } else {
  857. this.start(path);
  858. }
  859. }
  860. /**
  861. * Unhandled `ctx`. When it's not the initial
  862. * popstate then redirect. If you wish to handle
  863. * 404s on your own use `page('*', callback)`.
  864. *
  865. * @param {Context} ctx
  866. * @api private
  867. */
  868. function unhandled(ctx) {
  869. if (ctx.handled) return;
  870. var current;
  871. var page = this;
  872. var window = page._window;
  873. if (page._hashbang) {
  874. current = isLocation && this._getBase() + window.location.hash.replace('#!', '');
  875. } else {
  876. current = isLocation && window.location.pathname + window.location.search;
  877. }
  878. if (current === ctx.canonicalPath) return;
  879. page.stop();
  880. ctx.handled = false;
  881. isLocation && (window.location.href = ctx.canonicalPath);
  882. }
  883. /**
  884. * Initialize a new "request" `Context`
  885. * with the given `path` and optional initial `state`.
  886. *
  887. * @constructor
  888. * @param {string} path
  889. * @param {Object=} state
  890. * @api public
  891. */
  892. function Context(path, state, pageInstance) {
  893. var _page = this.page = pageInstance || page;
  894. var window = _page._window;
  895. var hashbang = _page._hashbang;
  896. var pageBase = _page._getBase();
  897. if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path;
  898. var i = path.indexOf('?');
  899. this.canonicalPath = path;
  900. this.path = path.replace(pageBase, '') || '/';
  901. if (hashbang) this.path = this.path.replace('#!', '') || '/';
  902. this.title = (hasDocument && window.document.title);
  903. this.state = state || {};
  904. this.state.path = path;
  905. this.querystring = ~i ? _page._decodeURLEncodedURIComponent(path.slice(i + 1)) : '';
  906. this.pathname = _page._decodeURLEncodedURIComponent(~i ? path.slice(0, i) : path);
  907. this.params = {};
  908. // fragment
  909. this.hash = '';
  910. if (!hashbang) {
  911. if (!~this.path.indexOf('#')) return;
  912. var parts = this.path.split('#');
  913. this.path = this.pathname = parts[0];
  914. this.hash = _page._decodeURLEncodedURIComponent(parts[1]) || '';
  915. this.querystring = this.querystring.split('#')[0];
  916. }
  917. }
  918. /**
  919. * Push state.
  920. *
  921. * @api private
  922. */
  923. Context.prototype.pushState = function() {
  924. var page = this.page;
  925. var window = page._window;
  926. var hashbang = page._hashbang;
  927. page.len++;
  928. if (hasHistory) {
  929. window.history.pushState(this.state, this.title,
  930. hashbang && this.path !== '/' ? '#!' + this.path : this.canonicalPath);
  931. }
  932. };
  933. /**
  934. * Save the context state.
  935. *
  936. * @api public
  937. */
  938. Context.prototype.save = function() {
  939. var page = this.page;
  940. if (hasHistory && page._window.location.protocol !== 'file:') {
  941. page._window.history.replaceState(this.state, this.title,
  942. page._hashbang && this.path !== '/' ? '#!' + this.path : this.canonicalPath);
  943. }
  944. };
  945. /**
  946. * Initialize `Route` with the given HTTP `path`,
  947. * and an array of `callbacks` and `options`.
  948. *
  949. * Options:
  950. *
  951. * - `sensitive` enable case-sensitive routes
  952. * - `strict` enable strict matching for trailing slashes
  953. *
  954. * @constructor
  955. * @param {string} path
  956. * @param {Object=} options
  957. * @api private
  958. */
  959. function Route(path, options, page) {
  960. var _page = this.page = page || globalPage;
  961. var opts = options || {};
  962. opts.strict = opts.strict || page._strict;
  963. this.path = (path === '*') ? '(.*)' : path;
  964. this.method = 'GET';
  965. this.regexp = pathToRegexp_1(this.path, this.keys = [], opts);
  966. }
  967. /**
  968. * Return route middleware with
  969. * the given callback `fn()`.
  970. *
  971. * @param {Function} fn
  972. * @return {Function}
  973. * @api public
  974. */
  975. Route.prototype.middleware = function(fn) {
  976. var self = this;
  977. return function(ctx, next) {
  978. if (self.match(ctx.path, ctx.params)) return fn(ctx, next);
  979. next();
  980. };
  981. };
  982. /**
  983. * Check if this route matches `path`, if so
  984. * populate `params`.
  985. *
  986. * @param {string} path
  987. * @param {Object} params
  988. * @return {boolean}
  989. * @api private
  990. */
  991. Route.prototype.match = function(path, params) {
  992. var keys = this.keys,
  993. qsIndex = path.indexOf('?'),
  994. pathname = ~qsIndex ? path.slice(0, qsIndex) : path,
  995. m = this.regexp.exec(decodeURIComponent(pathname));
  996. if (!m) return false;
  997. for (var i = 1, len = m.length; i < len; ++i) {
  998. var key = keys[i - 1];
  999. var val = this.page._decodeURLEncodedURIComponent(m[i]);
  1000. if (val !== undefined || !(hasOwnProperty.call(params, key.name))) {
  1001. params[key.name] = val;
  1002. }
  1003. }
  1004. return true;
  1005. };
  1006. /**
  1007. * Module exports.
  1008. */
  1009. var globalPage = createPage();
  1010. var page_js = globalPage;
  1011. var default_1 = globalPage;
  1012. page_js.default = default_1;
  1013. return page_js;
  1014. })));