locationServices.js 3.4 KB

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