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 };
9 function stateContext(el) {
10 var stateData = el.parent().inheritedData('$uiView');
12 if (stateData && stateData.state && stateData.state.name) {
13 return stateData.state;
19 * @name ui.router.state.directive:ui-sref
21 * @requires ui.router.state.$state
27 * A directive that binds a link (`<a>` tag) to a state. If the state has an associated
28 * URL, the directive will automatically generate & update the `href` attribute via
29 * the {@link ui.router.state.$state#methods_href $state.href()} method. Clicking
30 * the link will trigger a state transition with optional parameters.
32 * Also middle-clicking, right-clicking, and ctrl-clicking on the link will be
33 * handled natively by the browser.
35 * You can also use relative state paths within ui-sref, just like the relative
36 * paths passed to `$state.go()`. You just need to be aware that the path is relative
37 * to the state that the link lives in, in other words the state that loaded the
38 * template containing the link.
40 * You can specify options to pass to {@link ui.router.state.$state#go $state.go()}
41 * using the `ui-sref-opts` attribute. Options are restricted to `location`, `inherit`,
45 * Here's an example of how you'd use ui-sref and how it would compile. If you have the
48 * <a ui-sref="home">Home</a> | <a ui-sref="about">About</a> | <a ui-sref="{page: 2}">Next page</a>
51 * <li ng-repeat="contact in contacts">
52 * <a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
57 * Then the compiled html would be (assuming Html5Mode is off and current state is contacts):
59 * <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>
62 * <li ng-repeat="contact in contacts">
63 * <a href="#/contacts/1" ui-sref="contacts.detail({ id: contact.id })">Joe</a>
65 * <li ng-repeat="contact in contacts">
66 * <a href="#/contacts/2" ui-sref="contacts.detail({ id: contact.id })">Alice</a>
68 * <li ng-repeat="contact in contacts">
69 * <a href="#/contacts/3" ui-sref="contacts.detail({ id: contact.id })">Bob</a>
73 * <a ui-sref="home" ui-sref-opts="{reload: true}">Home</a>
76 * @param {string} ui-sref 'stateName' can be any valid absolute or relative state
77 * @param {Object} ui-sref-opts options to pass to {@link ui.router.state.$state#go $state.go()}
79 $StateRefDirective.$inject = ['$state', '$timeout'];
80 function $StateRefDirective($state, $timeout) {
81 var allowedOptions = ['location', 'inherit', 'reload'];
85 require: ['?^uiSrefActive', '?^uiSrefActiveEq'],
86 link: function(scope, element, attrs, uiSrefActive) {
87 var ref = parseStateRef(attrs.uiSref, $state.current.name);
88 var params = null, url = null, base = stateContext(element) || $state.$current;
89 var newHref = null, isAnchor = element.prop("tagName") === "A";
90 var isForm = element[0].nodeName === "FORM";
91 var attr = isForm ? "action" : "href", nav = true;
93 var options = { relative: base, inherit: true };
94 var optionsOverride = scope.$eval(attrs.uiSrefOpts) || {};
96 angular.forEach(allowedOptions, function(option) {
97 if (option in optionsOverride) {
98 options[option] = optionsOverride[option];
102 var update = function(newVal) {
103 if (newVal) params = angular.copy(newVal);
106 newHref = $state.href(ref.state, params, options);
108 var activeDirective = uiSrefActive[1] || uiSrefActive[0];
109 if (activeDirective) {
110 activeDirective.$$setStateInfo(ref.state, params);
112 if (newHref === null) {
116 attrs.$set(attr, newHref);
120 scope.$watch(ref.paramExpr, function(newVal, oldVal) {
121 if (newVal !== params) update(newVal);
123 params = angular.copy(scope.$eval(ref.paramExpr));
129 element.bind("click", function(e) {
130 var button = e.which || e.button;
131 if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) {
132 // HACK: This is to allow ng-clicks to be processed before the transition is initiated:
133 var transition = $timeout(function() {
134 $state.go(ref.state, params, options);
138 // if the state has no URL, ignore one preventDefault from the <a> directive.
139 var ignorePreventDefaultCount = isAnchor && !newHref ? 1: 0;
140 e.preventDefault = function() {
141 if (ignorePreventDefaultCount-- <= 0)
142 $timeout.cancel(transition);
152 * @name ui.router.state.directive:ui-sref-active
154 * @requires ui.router.state.$state
155 * @requires ui.router.state.$stateParams
156 * @requires $interpolate
161 * A directive working alongside ui-sref to add classes to an element when the
162 * related ui-sref directive's state is active, and removing them when it is inactive.
163 * The primary use-case is to simplify the special appearance of navigation menus
164 * relying on `ui-sref`, by having the "active" state's menu button appear different,
165 * distinguishing it from the inactive menu items.
167 * ui-sref-active can live on the same element as ui-sref or on a parent element. The first
168 * ui-sref-active found at the same level or above the ui-sref will be used.
170 * Will activate when the ui-sref's target state or any child state is active. If you
171 * need to activate only when the ui-sref target state is active and *not* any of
172 * it's children, then you will use
173 * {@link ui.router.state.directive:ui-sref-active-eq ui-sref-active-eq}
176 * Given the following template:
179 * <li ui-sref-active="active" class="item">
180 * <a href ui-sref="app.user({user: 'bilbobaggins'})">@bilbobaggins</a>
186 * When the app state is "app.user" (or any children states), and contains the state parameter "user" with value "bilbobaggins",
187 * the resulting HTML will appear as (note the 'active' class):
190 * <li ui-sref-active="active" class="item active">
191 * <a ui-sref="app.user({user: 'bilbobaggins'})" href="/users/bilbobaggins">@bilbobaggins</a>
196 * The class name is interpolated **once** during the directives link time (any further changes to the
197 * interpolated value are ignored).
199 * Multiple classes may be specified in a space-separated format:
202 * <li ui-sref-active='class1 class2 class3'>
203 * <a ui-sref="app.user">link</a>
211 * @name ui.router.state.directive:ui-sref-active-eq
213 * @requires ui.router.state.$state
214 * @requires ui.router.state.$stateParams
215 * @requires $interpolate
220 * The same as {@link ui.router.state.directive:ui-sref-active ui-sref-active} but will only activate
221 * when the exact target state used in the `ui-sref` is active; no child states.
224 $StateRefActiveDirective.$inject = ['$state', '$stateParams', '$interpolate'];
225 function $StateRefActiveDirective($state, $stateParams, $interpolate) {
228 controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
229 var state, params, activeClass;
231 // There probably isn't much point in $observing this
232 // uiSrefActive and uiSrefActiveEq share the same directive object with some
233 // slight difference in logic routing
234 activeClass = $interpolate($attrs.uiSrefActiveEq || $attrs.uiSrefActive || '', false)($scope);
236 // Allow uiSref to communicate with uiSrefActive[Equals]
237 this.$$setStateInfo = function (newState, newParams) {
238 state = $state.get(newState, stateContext($element));
243 $scope.$on('$stateChangeSuccess', update);
245 // Update route state
248 $element.addClass(activeClass);
250 $element.removeClass(activeClass);
255 if (typeof $attrs.uiSrefActiveEq !== 'undefined') {
256 return state && $state.is(state.name, params);
258 return state && $state.includes(state.name, params);
265 angular.module('ui.router.state')
266 .directive('uiSref', $StateRefDirective)
267 .directive('uiSrefActive', $StateRefActiveDirective)
268 .directive('uiSrefActiveEq', $StateRefActiveDirective);