UNPKG

3.74 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.PushStateLocationService = void 0;
17var baseLocationService_1 = require("./baseLocationService");
18var common_1 = require("../common");
19/**
20 * A `LocationServices` that gets/sets the current location using the browser's `location` and `history` apis
21 *
22 * Uses `history.pushState` and `history.replaceState`
23 */
24var PushStateLocationService = /** @class */ (function (_super) {
25 __extends(PushStateLocationService, _super);
26 function PushStateLocationService(router) {
27 var _this = _super.call(this, router, true) || this;
28 _this._config = router.urlService.config;
29 common_1.root.addEventListener('popstate', _this._listener, false);
30 return _this;
31 }
32 /**
33 * Gets the base prefix without:
34 * - trailing slash
35 * - trailing filename
36 * - protocol and hostname
37 *
38 * If <base href='/base/'>, this returns '/base'.
39 * If <base href='/foo/base/'>, this returns '/foo/base'.
40 * If <base href='/base/index.html'>, this returns '/base'.
41 * If <base href='http://localhost:8080/base/index.html'>, this returns '/base'.
42 * If <base href='/base'>, this returns ''.
43 * If <base href='http://localhost:8080'>, this returns ''.
44 * If <base href='http://localhost:8080/'>, this returns ''.
45 *
46 * See: https://html.spec.whatwg.org/dev/semantics.html#the-base-element
47 */
48 PushStateLocationService.prototype._getBasePrefix = function () {
49 return common_1.stripLastPathElement(this._config.baseHref());
50 };
51 PushStateLocationService.prototype._get = function () {
52 var _a = this._location, pathname = _a.pathname, hash = _a.hash, search = _a.search;
53 search = common_1.splitQuery(search)[1]; // strip ? if found
54 hash = common_1.splitHash(hash)[1]; // strip # if found
55 var basePrefix = this._getBasePrefix();
56 var exactBaseHrefMatch = pathname === this._config.baseHref();
57 var startsWithBase = pathname.substr(0, basePrefix.length) === basePrefix;
58 pathname = exactBaseHrefMatch ? '/' : startsWithBase ? pathname.substring(basePrefix.length) : pathname;
59 return pathname + (search ? '?' + search : '') + (hash ? '#' + hash : '');
60 };
61 PushStateLocationService.prototype._set = function (state, title, url, replace) {
62 var basePrefix = this._getBasePrefix();
63 var slash = url && url[0] !== '/' ? '/' : '';
64 var fullUrl = url === '' || url === '/' ? this._config.baseHref() : basePrefix + slash + url;
65 if (replace) {
66 this._history.replaceState(state, title, fullUrl);
67 }
68 else {
69 this._history.pushState(state, title, fullUrl);
70 }
71 };
72 PushStateLocationService.prototype.dispose = function (router) {
73 _super.prototype.dispose.call(this, router);
74 common_1.root.removeEventListener('popstate', this._listener);
75 };
76 return PushStateLocationService;
77}(baseLocationService_1.BaseLocationServices));
78exports.PushStateLocationService = PushStateLocationService;
79//# sourceMappingURL=pushStateLocationService.js.map
\No newline at end of file