UNPKG

3.78 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Ng1LocationServices = void 0;
4/** @publicapi @module ng1 */ /** */
5var core_1 = require("@uirouter/core");
6var core_2 = require("@uirouter/core");
7/**
8 * Implements UI-Router LocationServices and LocationConfig using Angular 1's $location service
9 * @internalapi
10 */
11var Ng1LocationServices = /** @class */ (function () {
12 function Ng1LocationServices($locationProvider) {
13 // .onChange() registry
14 this._urlListeners = [];
15 this.$locationProvider = $locationProvider;
16 var _lp = core_2.val($locationProvider);
17 core_2.createProxyFunctions(_lp, this, _lp, ['hashPrefix']);
18 }
19 /**
20 * Applys ng1-specific path parameter encoding
21 *
22 * The Angular 1 `$location` service is a bit weird.
23 * It doesn't allow slashes to be encoded/decoded bi-directionally.
24 *
25 * See the writeup at https://github.com/angular-ui/ui-router/issues/2598
26 *
27 * This code patches the `path` parameter type so it encoded/decodes slashes as ~2F
28 *
29 * @param router
30 */
31 Ng1LocationServices.monkeyPatchPathParameterType = function (router) {
32 var pathType = router.urlMatcherFactory.type('path');
33 pathType.encode = function (x) {
34 return x != null ? x.toString().replace(/(~|\/)/g, function (m) { return ({ '~': '~~', '/': '~2F' }[m]); }) : x;
35 };
36 pathType.decode = function (x) {
37 return x != null ? x.toString().replace(/(~~|~2F)/g, function (m) { return ({ '~~': '~', '~2F': '/' }[m]); }) : x;
38 };
39 };
40 // eslint-disable-next-line @typescript-eslint/no-empty-function
41 Ng1LocationServices.prototype.dispose = function () { };
42 Ng1LocationServices.prototype.onChange = function (callback) {
43 var _this = this;
44 this._urlListeners.push(callback);
45 return function () { return core_2.removeFrom(_this._urlListeners)(callback); };
46 };
47 Ng1LocationServices.prototype.html5Mode = function () {
48 var html5Mode = this.$locationProvider.html5Mode();
49 html5Mode = core_2.isObject(html5Mode) ? html5Mode.enabled : html5Mode;
50 return html5Mode && this.$sniffer.history;
51 };
52 Ng1LocationServices.prototype.baseHref = function () {
53 return this._baseHref || (this._baseHref = this.$browser.baseHref() || this.$window.location.pathname);
54 };
55 Ng1LocationServices.prototype.url = function (newUrl, replace, state) {
56 if (replace === void 0) { replace = false; }
57 if (core_1.isDefined(newUrl))
58 this.$location.url(newUrl);
59 if (replace)
60 this.$location.replace();
61 if (state)
62 this.$location.state(state);
63 return this.$location.url();
64 };
65 Ng1LocationServices.prototype._runtimeServices = function ($rootScope, $location, $sniffer, $browser, $window) {
66 var _this = this;
67 this.$location = $location;
68 this.$sniffer = $sniffer;
69 this.$browser = $browser;
70 this.$window = $window;
71 // Bind $locationChangeSuccess to the listeners registered in LocationService.onChange
72 $rootScope.$on('$locationChangeSuccess', function (evt) { return _this._urlListeners.forEach(function (fn) { return fn(evt); }); });
73 var _loc = core_2.val($location);
74 // Bind these LocationService functions to $location
75 core_2.createProxyFunctions(_loc, this, _loc, ['replace', 'path', 'search', 'hash']);
76 // Bind these LocationConfig functions to $location
77 core_2.createProxyFunctions(_loc, this, _loc, ['port', 'protocol', 'host']);
78 };
79 return Ng1LocationServices;
80}());
81exports.Ng1LocationServices = Ng1LocationServices;
82//# sourceMappingURL=locationServices.js.map
\No newline at end of file