1 | "use strict";
|
2 | var __extends = (this && this.__extends) || function (d, b) {
|
3 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
4 | function __() { this.constructor = d; }
|
5 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
6 | };
|
7 | var core_1 = require('@angular/core');
|
8 | var common_1 = require('@angular/common');
|
9 | var tokens_1 = require('./tokens');
|
10 | var nodeUrl = require('url');
|
11 | var NodeLocation = (function () {
|
12 | function NodeLocation(config) {
|
13 | this.assign(config);
|
14 | }
|
15 | Object.defineProperty(NodeLocation.prototype, "origin", {
|
16 | get: function () {
|
17 | return this.protocol + '//' + this.hostname + ':' + this.port;
|
18 | },
|
19 | enumerable: true,
|
20 | configurable: true
|
21 | });
|
22 | NodeLocation.prototype.parse = function (url) {
|
23 | return nodeUrl.parse(url);
|
24 | };
|
25 | NodeLocation.prototype.format = function (obj) {
|
26 | return nodeUrl.format(obj);
|
27 | };
|
28 | NodeLocation.prototype.assign = function (parsed) {
|
29 | this.pathname = parsed.pathname || '';
|
30 | this.search = parsed.search || '';
|
31 | this.hash = parsed.hash || '';
|
32 | this.host = parsed.host;
|
33 | this.hostname = parsed.hostname;
|
34 | this.href = parsed.href;
|
35 | this.port = parsed.port;
|
36 | this.protocol = parsed.protocol;
|
37 | return this;
|
38 | };
|
39 | NodeLocation.prototype.toJSON = function () {
|
40 | var config = {
|
41 | hash: this.hash,
|
42 | host: this.host,
|
43 | hostname: this.hostname,
|
44 | href: this.href,
|
45 | pathname: this.pathname,
|
46 | port: this.port,
|
47 | protocol: this.protocol,
|
48 | search: this.search
|
49 | };
|
50 | return config;
|
51 | };
|
52 | return NodeLocation;
|
53 | }());
|
54 | exports.NodeLocation = NodeLocation;
|
55 | var State = (function () {
|
56 | function State(state, title, url) {
|
57 | this.state = state;
|
58 | this.title = title;
|
59 | this.url = url;
|
60 | }
|
61 | State.prototype.toJSON = function () {
|
62 | return {
|
63 | state: this.state,
|
64 | title: this.title,
|
65 | url: this.url
|
66 | };
|
67 | };
|
68 | return State;
|
69 | }());
|
70 | exports.State = State;
|
71 | var PopStateEvent = (function () {
|
72 | function PopStateEvent(state) {
|
73 | this.state = state;
|
74 | this.type = 'popstate';
|
75 | }
|
76 | PopStateEvent.prototype.toJSON = function () {
|
77 | return {
|
78 | state: this.state
|
79 | };
|
80 | };
|
81 | return PopStateEvent;
|
82 | }());
|
83 | exports.PopStateEvent = PopStateEvent;
|
84 | var NodePlatformLocation = (function (_super) {
|
85 | __extends(NodePlatformLocation, _super);
|
86 | function NodePlatformLocation(originUrl, requestUrl, baseUrl) {
|
87 | _super.call(this);
|
88 | this._stack = [];
|
89 | this._stackIndex = -1;
|
90 | this._popStateListeners = [];
|
91 | this._baseUrl = '/';
|
92 | this._originUrl = originUrl;
|
93 | this._baseUrl = baseUrl || '/';
|
94 | this.pushState(null, null, requestUrl);
|
95 | }
|
96 | NodePlatformLocation.prototype.updateUrl = function (originUrl, baseUrl) {
|
97 | if (baseUrl === void 0) { baseUrl = '/'; }
|
98 | this._originUrl = originUrl;
|
99 | this._baseUrl = baseUrl || '/';
|
100 | };
|
101 | Object.defineProperty(NodePlatformLocation.prototype, "search", {
|
102 | get: function () { return this._loc.search; },
|
103 | enumerable: true,
|
104 | configurable: true
|
105 | });
|
106 | Object.defineProperty(NodePlatformLocation.prototype, "hash", {
|
107 | get: function () { return this._loc.hash; },
|
108 | enumerable: true,
|
109 | configurable: true
|
110 | });
|
111 | Object.defineProperty(NodePlatformLocation.prototype, "pathname", {
|
112 | get: function () { return this._loc.pathname; },
|
113 | set: function (newPathname) { this._loc.pathname = newPathname; },
|
114 | enumerable: true,
|
115 | configurable: true
|
116 | });
|
117 | NodePlatformLocation.prototype.getBaseHrefFromDOM = function () {
|
118 | throw new Error("\n Attempt to get base href from DOM on the server.\n You have to provide a value for the APP_BASE_HREF token through DI.\n ");
|
119 | };
|
120 | NodePlatformLocation.prototype.getBaseHref = function () { return this._baseUrl; };
|
121 | NodePlatformLocation.prototype.path = function () { return this._loc.pathname; };
|
122 | NodePlatformLocation.prototype.pushState = function (state, title, url) {
|
123 | this._stack.push(new State(state, title, url));
|
124 | this._stackIndex++;
|
125 | this._updateLocation();
|
126 | };
|
127 | NodePlatformLocation.prototype.replaceState = function (state, title, url) {
|
128 | this._stack[this._stackIndex] = new State(state, title, url);
|
129 | this._updateLocation();
|
130 | };
|
131 | NodePlatformLocation.prototype.onPopState = function (fn) { this._popStateListeners.push(fn); };
|
132 | NodePlatformLocation.prototype.onHashChange = function (_fn) { };
|
133 | NodePlatformLocation.prototype.back = function () {
|
134 | if (this._stackIndex === 0) {
|
135 | return;
|
136 | }
|
137 | this._stackIndex--;
|
138 | this._updateLocation();
|
139 | this._callPopStateListeners();
|
140 | };
|
141 | NodePlatformLocation.prototype.forward = function () {
|
142 | if (this._stackIndex === this._stack.length - 1) {
|
143 | return;
|
144 | }
|
145 | this._stackIndex++;
|
146 | this._updateLocation();
|
147 | this._callPopStateListeners();
|
148 | };
|
149 | NodePlatformLocation.prototype.prepareExternalUrl = function (internal) {
|
150 | return joinWithSlash(this._baseUrl, internal);
|
151 | };
|
152 | NodePlatformLocation.prototype.toJSON = function () {
|
153 | return {
|
154 | location: this._loc,
|
155 | stack: this._stack,
|
156 | stackIndex: this._stackIndex,
|
157 | popStateListeners: this._popStateListeners,
|
158 | baseHref: this._baseUrl
|
159 | };
|
160 | };
|
161 | NodePlatformLocation.prototype._updateLocation = function () {
|
162 | var state = this._stack[this._stackIndex];
|
163 | var url = state.url;
|
164 | this._setLocationByUrl(url);
|
165 | };
|
166 | NodePlatformLocation.prototype._setLocationByUrl = function (url) {
|
167 | var resolvedOriginBase = nodeUrl.resolve(this._originUrl, this._baseUrl);
|
168 | var resolvedWithUrl = nodeUrl.resolve(resolvedOriginBase, url);
|
169 | var nodeLocation = nodeUrl.parse(resolvedWithUrl);
|
170 | this._loc = new NodeLocation(nodeLocation);
|
171 | };
|
172 | NodePlatformLocation.prototype._callPopStateListeners = function () {
|
173 | var state = this._stack[this._stackIndex].state;
|
174 | var event = new PopStateEvent(state);
|
175 | this._popStateListeners.forEach(function (listener) { return listener(event); });
|
176 | };
|
177 | NodePlatformLocation.decorators = [
|
178 | { type: core_1.Injectable },
|
179 | ];
|
180 | NodePlatformLocation.ctorParameters = [
|
181 | { type: undefined, decorators: [{ type: core_1.Optional }, { type: core_1.Inject, args: [tokens_1.ORIGIN_URL,] },] },
|
182 | { type: undefined, decorators: [{ type: core_1.Optional }, { type: core_1.Inject, args: [tokens_1.REQUEST_URL,] },] },
|
183 | { type: undefined, decorators: [{ type: core_1.Optional }, { type: core_1.Inject, args: [common_1.APP_BASE_HREF,] },] },
|
184 | ];
|
185 | return NodePlatformLocation;
|
186 | }(common_1.PlatformLocation));
|
187 | exports.NodePlatformLocation = NodePlatformLocation;
|
188 | function joinWithSlash(start, end) {
|
189 | if (start.length === 0) {
|
190 | return end;
|
191 | }
|
192 | if (end.length === 0) {
|
193 | return start;
|
194 | }
|
195 | var slashes = 0;
|
196 | if (start.endsWith('/')) {
|
197 | slashes++;
|
198 | }
|
199 | if (end.startsWith('/')) {
|
200 | slashes++;
|
201 | }
|
202 | if (slashes === 2) {
|
203 | return start + end.substring(1);
|
204 | }
|
205 | if (slashes === 1) {
|
206 | return start + end;
|
207 | }
|
208 | return start + '/' + end;
|
209 | }
|
210 | exports.joinWithSlash = joinWithSlash;
|
211 | exports.NODE_LOCATION_PROVIDERS = [
|
212 | { provide: common_1.PlatformLocation, useClass: NodePlatformLocation }
|
213 | ];
|
214 |
|
\ | No newline at end of file |