stateDirectives.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. function parseStateRef(ref, current) {
  2. var preparsed = ref.match(/^\s*({[^}]*})\s*$/), parsed;
  3. if (preparsed) ref = current + '(' + preparsed[1] + ')';
  4. parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?$/);
  5. if (!parsed || parsed.length !== 4) throw new Error("Invalid state ref '" + ref + "'");
  6. return { state: parsed[1], paramExpr: parsed[3] || null };
  7. }
  8. function stateContext(el) {
  9. var stateData = el.parent().inheritedData('$uiView');
  10. if (stateData && stateData.state && stateData.state.name) {
  11. return stateData.state;
  12. }
  13. }
  14. /**
  15. * @ngdoc directive
  16. * @name ui.router.state.directive:ui-sref
  17. *
  18. * @requires ui.router.state.$state
  19. * @requires $timeout
  20. *
  21. * @restrict A
  22. *
  23. * @description
  24. * A directive that binds a link (`<a>` tag) to a state. If the state has an associated
  25. * URL, the directive will automatically generate & update the `href` attribute via
  26. * the {@link ui.router.state.$state#methods_href $state.href()} method. Clicking
  27. * the link will trigger a state transition with optional parameters.
  28. *
  29. * Also middle-clicking, right-clicking, and ctrl-clicking on the link will be
  30. * handled natively by the browser.
  31. *
  32. * You can also use relative state paths within ui-sref, just like the relative
  33. * paths passed to `$state.go()`. You just need to be aware that the path is relative
  34. * to the state that the link lives in, in other words the state that loaded the
  35. * template containing the link.
  36. *
  37. * You can specify options to pass to {@link ui.router.state.$state#go $state.go()}
  38. * using the `ui-sref-opts` attribute. Options are restricted to `location`, `inherit`,
  39. * and `reload`.
  40. *
  41. * @example
  42. * Here's an example of how you'd use ui-sref and how it would compile. If you have the
  43. * following template:
  44. * <pre>
  45. * <a ui-sref="home">Home</a> | <a ui-sref="about">About</a> | <a ui-sref="{page: 2}">Next page</a>
  46. *
  47. * <ul>
  48. * <li ng-repeat="contact in contacts">
  49. * <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
  50. * </li>
  51. * </ul>
  52. * </pre>
  53. *
  54. * Then the compiled html would be (assuming Html5Mode is off and current state is contacts):
  55. * <pre>
  56. * <a href="#/home" ui-sref="home">Home</a> | <a href="#/about" ui-sref="about">About</a> | <a href="#/contacts?page=2" ui-sref="{page: 2}">Next page</a>
  57. *
  58. * <ul>
  59. * <li ng-repeat="contact in contacts">
  60. * <a href="#/contacts/1" ui-sref="contacts.detail({ id: contact.id })">Joe</a>
  61. * </li>
  62. * <li ng-repeat="contact in contacts">
  63. * <a href="#/contacts/2" ui-sref="contacts.detail({ id: contact.id })">Alice</a>
  64. * </li>
  65. * <li ng-repeat="contact in contacts">
  66. * <a href="#/contacts/3" ui-sref="contacts.detail({ id: contact.id })">Bob</a>
  67. * </li>
  68. * </ul>
  69. *
  70. * <a ui-sref="home" ui-sref-opts="{reload: true}">Home</a>
  71. * </pre>
  72. *
  73. * @param {string} ui-sref 'stateName' can be any valid absolute or relative state
  74. * @param {Object} ui-sref-opts options to pass to {@link ui.router.state.$state#go $state.go()}
  75. */
  76. $StateRefDirective.$inject = ['$state', '$timeout'];
  77. function $StateRefDirective($state, $timeout) {
  78. var allowedOptions = ['location', 'inherit', 'reload'];
  79. return {
  80. restrict: 'A',
  81. require: ['?^uiSrefActive', '?^uiSrefActiveEq'],
  82. link: function(scope, element, attrs, uiSrefActive) {
  83. var ref = parseStateRef(attrs.uiSref, $state.current.name);
  84. var params = null, url = null, base = stateContext(element) || $state.$current;
  85. var newHref = null, isAnchor = element.prop("tagName") === "A";
  86. var isForm = element[0].nodeName === "FORM";
  87. var attr = isForm ? "action" : "href", nav = true;
  88. var options = { relative: base, inherit: true };
  89. var optionsOverride = scope.$eval(attrs.uiSrefOpts) || {};
  90. angular.forEach(allowedOptions, function(option) {
  91. if (option in optionsOverride) {
  92. options[option] = optionsOverride[option];
  93. }
  94. });
  95. var update = function(newVal) {
  96. if (newVal) params = angular.copy(newVal);
  97. if (!nav) return;
  98. newHref = $state.href(ref.state, params, options);
  99. var activeDirective = uiSrefActive[1] || uiSrefActive[0];
  100. if (activeDirective) {
  101. activeDirective.$$setStateInfo(ref.state, params);
  102. }
  103. if (newHref === null) {
  104. nav = false;
  105. return false;
  106. }
  107. attrs.$set(attr, newHref);
  108. };
  109. if (ref.paramExpr) {
  110. scope.$watch(ref.paramExpr, function(newVal, oldVal) {
  111. if (newVal !== params) update(newVal);
  112. }, true);
  113. params = angular.copy(scope.$eval(ref.paramExpr));
  114. }
  115. update();
  116. if (isForm) return;
  117. element.bind("click", function(e) {
  118. var button = e.which || e.button;
  119. if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) {
  120. // HACK: This is to allow ng-clicks to be processed before the transition is initiated:
  121. var transition = $timeout(function() {
  122. $state.go(ref.state, params, options);
  123. });
  124. e.preventDefault();
  125. // if the state has no URL, ignore one preventDefault from the <a> directive.
  126. var ignorePreventDefaultCount = isAnchor && !newHref ? 1: 0;
  127. e.preventDefault = function() {
  128. if (ignorePreventDefaultCount-- <= 0)
  129. $timeout.cancel(transition);
  130. };
  131. }
  132. });
  133. }
  134. };
  135. }
  136. /**
  137. * @ngdoc directive
  138. * @name ui.router.state.directive:ui-sref-active
  139. *
  140. * @requires ui.router.state.$state
  141. * @requires ui.router.state.$stateParams
  142. * @requires $interpolate
  143. *
  144. * @restrict A
  145. *
  146. * @description
  147. * A directive working alongside ui-sref to add classes to an element when the
  148. * related ui-sref directive's state is active, and removing them when it is inactive.
  149. * The primary use-case is to simplify the special appearance of navigation menus
  150. * relying on `ui-sref`, by having the "active" state's menu button appear different,
  151. * distinguishing it from the inactive menu items.
  152. *
  153. * ui-sref-active can live on the same element as ui-sref or on a parent element. The first
  154. * ui-sref-active found at the same level or above the ui-sref will be used.
  155. *
  156. * Will activate when the ui-sref's target state or any child state is active. If you
  157. * need to activate only when the ui-sref target state is active and *not* any of
  158. * it's children, then you will use
  159. * {@link ui.router.state.directive:ui-sref-active-eq ui-sref-active-eq}
  160. *
  161. * @example
  162. * Given the following template:
  163. * <pre>
  164. * <ul>
  165. * <li ui-sref-active="active" class="item">
  166. * <a href ui-sref="app.user({user: 'bilbobaggins'})">@bilbobaggins</a>
  167. * </li>
  168. * </ul>
  169. * </pre>
  170. *
  171. *
  172. * When the app state is "app.user" (or any children states), and contains the state parameter "user" with value "bilbobaggins",
  173. * the resulting HTML will appear as (note the 'active' class):
  174. * <pre>
  175. * <ul>
  176. * <li ui-sref-active="active" class="item active">
  177. * <a ui-sref="app.user({user: 'bilbobaggins'})" href="/users/bilbobaggins">@bilbobaggins</a>
  178. * </li>
  179. * </ul>
  180. * </pre>
  181. *
  182. * The class name is interpolated **once** during the directives link time (any further changes to the
  183. * interpolated value are ignored).
  184. *
  185. * Multiple classes may be specified in a space-separated format:
  186. * <pre>
  187. * <ul>
  188. * <li ui-sref-active='class1 class2 class3'>
  189. * <a ui-sref="app.user">link</a>
  190. * </li>
  191. * </ul>
  192. * </pre>
  193. */
  194. /**
  195. * @ngdoc directive
  196. * @name ui.router.state.directive:ui-sref-active-eq
  197. *
  198. * @requires ui.router.state.$state
  199. * @requires ui.router.state.$stateParams
  200. * @requires $interpolate
  201. *
  202. * @restrict A
  203. *
  204. * @description
  205. * The same as {@link ui.router.state.directive:ui-sref-active ui-sref-active} but will only activate
  206. * when the exact target state used in the `ui-sref` is active; no child states.
  207. *
  208. */
  209. $StateRefActiveDirective.$inject = ['$state', '$stateParams', '$interpolate'];
  210. function $StateRefActiveDirective($state, $stateParams, $interpolate) {
  211. return {
  212. restrict: "A",
  213. controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
  214. var state, params, activeClass;
  215. // There probably isn't much point in $observing this
  216. // uiSrefActive and uiSrefActiveEq share the same directive object with some
  217. // slight difference in logic routing
  218. activeClass = $interpolate($attrs.uiSrefActiveEq || $attrs.uiSrefActive || '', false)($scope);
  219. // Allow uiSref to communicate with uiSrefActive[Equals]
  220. this.$$setStateInfo = function (newState, newParams) {
  221. state = $state.get(newState, stateContext($element));
  222. params = newParams;
  223. update();
  224. };
  225. $scope.$on('$stateChangeSuccess', update);
  226. // Update route state
  227. function update() {
  228. if (isMatch()) {
  229. $element.addClass(activeClass);
  230. } else {
  231. $element.removeClass(activeClass);
  232. }
  233. }
  234. function isMatch() {
  235. if (typeof $attrs.uiSrefActiveEq !== 'undefined') {
  236. return state && $state.is(state.name, params);
  237. } else {
  238. return state && $state.includes(state.name, params);
  239. }
  240. }
  241. }]
  242. };
  243. }
  244. angular.module('ui.router.state')
  245. .directive('uiSref', $StateRefDirective)
  246. .directive('uiSrefActive', $StateRefActiveDirective)
  247. .directive('uiSrefActiveEq', $StateRefActiveDirective);