stateProvider.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /** @module ng1 */ /** for typedoc */
  4. var core_1 = require("@uirouter/core");
  5. /**
  6. * The Angular 1 `StateProvider`
  7. *
  8. * The `$stateProvider` works similar to Angular's v1 router, but it focuses purely
  9. * on state.
  10. *
  11. * A state corresponds to a "place" in the application in terms of the overall UI and
  12. * navigation. A state describes (via the controller / template / view properties) what
  13. * the UI looks like and does at that place.
  14. *
  15. * States often have things in common, and the primary way of factoring out these
  16. * commonalities in this model is via the state hierarchy, i.e. parent/child states aka
  17. * nested states.
  18. *
  19. * The `$stateProvider` provides interfaces to declare these states for your app.
  20. */
  21. var StateProvider = (function () {
  22. function StateProvider(stateRegistry, stateService) {
  23. this.stateRegistry = stateRegistry;
  24. this.stateService = stateService;
  25. core_1.createProxyFunctions(core_1.val(StateProvider.prototype), this, core_1.val(this));
  26. }
  27. /**
  28. * Decorates states when they are registered
  29. *
  30. * Allows you to extend (carefully) or override (at your own peril) the
  31. * `stateBuilder` object used internally by [[StateRegistry]].
  32. * This can be used to add custom functionality to ui-router,
  33. * for example inferring templateUrl based on the state name.
  34. *
  35. * When passing only a name, it returns the current (original or decorated) builder
  36. * function that matches `name`.
  37. *
  38. * The builder functions that can be decorated are listed below. Though not all
  39. * necessarily have a good use case for decoration, that is up to you to decide.
  40. *
  41. * In addition, users can attach custom decorators, which will generate new
  42. * properties within the state's internal definition. There is currently no clear
  43. * use-case for this beyond accessing internal states (i.e. $state.$current),
  44. * however, expect this to become increasingly relevant as we introduce additional
  45. * meta-programming features.
  46. *
  47. * **Warning**: Decorators should not be interdependent because the order of
  48. * execution of the builder functions in non-deterministic. Builder functions
  49. * should only be dependent on the state definition object and super function.
  50. *
  51. *
  52. * Existing builder functions and current return values:
  53. *
  54. * - **parent** `{object}` - returns the parent state object.
  55. * - **data** `{object}` - returns state data, including any inherited data that is not
  56. * overridden by own values (if any).
  57. * - **url** `{object}` - returns a {@link ui.router.util.type:UrlMatcher UrlMatcher}
  58. * or `null`.
  59. * - **navigable** `{object}` - returns closest ancestor state that has a URL (aka is
  60. * navigable).
  61. * - **params** `{object}` - returns an array of state params that are ensured to
  62. * be a super-set of parent's params.
  63. * - **views** `{object}` - returns a views object where each key is an absolute view
  64. * name (i.e. "viewName@stateName") and each value is the config object
  65. * (template, controller) for the view. Even when you don't use the views object
  66. * explicitly on a state config, one is still created for you internally.
  67. * So by decorating this builder function you have access to decorating template
  68. * and controller properties.
  69. * - **ownParams** `{object}` - returns an array of params that belong to the state,
  70. * not including any params defined by ancestor states.
  71. * - **path** `{string}` - returns the full path from the root down to this state.
  72. * Needed for state activation.
  73. * - **includes** `{object}` - returns an object that includes every state that
  74. * would pass a `$state.includes()` test.
  75. *
  76. * #### Example:
  77. * Override the internal 'views' builder with a function that takes the state
  78. * definition, and a reference to the internal function being overridden:
  79. * ```js
  80. * $stateProvider.decorator('views', function (state, parent) {
  81. * let result = {},
  82. * views = parent(state);
  83. *
  84. * angular.forEach(views, function (config, name) {
  85. * let autoName = (state.name + '.' + name).replace('.', '/');
  86. * config.templateUrl = config.templateUrl || '/partials/' + autoName + '.html';
  87. * result[name] = config;
  88. * });
  89. * return result;
  90. * });
  91. *
  92. * $stateProvider.state('home', {
  93. * views: {
  94. * 'contact.list': { controller: 'ListController' },
  95. * 'contact.item': { controller: 'ItemController' }
  96. * }
  97. * });
  98. * ```
  99. *
  100. *
  101. * ```js
  102. * // Auto-populates list and item views with /partials/home/contact/list.html,
  103. * // and /partials/home/contact/item.html, respectively.
  104. * $state.go('home');
  105. * ```
  106. *
  107. * @param {string} name The name of the builder function to decorate.
  108. * @param {object} func A function that is responsible for decorating the original
  109. * builder function. The function receives two parameters:
  110. *
  111. * - `{object}` - state - The state config object.
  112. * - `{object}` - super - The original builder function.
  113. *
  114. * @return {object} $stateProvider - $stateProvider instance
  115. */
  116. StateProvider.prototype.decorator = function (name, func) {
  117. return this.stateRegistry.decorator(name, func) || this;
  118. };
  119. StateProvider.prototype.state = function (name, definition) {
  120. if (core_1.isObject(name)) {
  121. definition = name;
  122. }
  123. else {
  124. definition.name = name;
  125. }
  126. this.stateRegistry.register(definition);
  127. return this;
  128. };
  129. /**
  130. * Registers an invalid state handler
  131. *
  132. * This is a passthrough to [[StateService.onInvalid]] for ng1.
  133. */
  134. StateProvider.prototype.onInvalid = function (callback) {
  135. return this.stateService.onInvalid(callback);
  136. };
  137. return StateProvider;
  138. }());
  139. exports.StateProvider = StateProvider;
  140. //# sourceMappingURL=stateProvider.js.map