page.js 30 KB

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