From a602bb3e9ce42b94833d79bec64ef962ca18657b Mon Sep 17 00:00:00 2001 From: Tima Maslyuchenko Date: Wed, 25 Sep 2013 14:43:13 +0300 Subject: [PATCH] added absolute option to $state.href --- src/state.js | 14 ++++++++++++-- test/stateSpec.js | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/state.js b/src/state.js index 464771c6a..bf1e0eedc 100644 --- a/src/state.js +++ b/src/state.js @@ -411,14 +411,24 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $ }; $state.href = function href(stateOrName, params, options) { - options = extend({ lossy: true, inherit: false, relative: $state.$current }, options || {}); + options = extend({ lossy: true, inherit: false, absolute: false, relative: $state.$current }, options || {}); var state = findState(stateOrName, options.relative); if (!isDefined(state)) return null; params = inheritParams($stateParams, params || {}, $state.$current, state); var nav = (state && options.lossy) ? state.navigable : state; var url = (nav && nav.url) ? nav.url.format(normalize(state.params, params || {})) : null; - return !$locationProvider.html5Mode() && url ? "#" + url : url; + if (!$locationProvider.html5Mode() && url) { + url = "#" + url; + } + if (options.absolute && url) { + url = $location.protocol() + '://' + + $location.host() + + ($location.port() == 80 || $location.port() == 443 ? '' : ':' + $location.port()) + + (!$locationProvider.html5Mode() && url ? '/' : '') + + url; + } + return url; }; $state.get = function (stateOrName) { diff --git a/test/stateSpec.js b/test/stateSpec.js index 33f0a3a63..6ca7f1d4c 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -510,6 +510,12 @@ describe('state', function () { expect($state.href("about.person", { person: "bob" })).toEqual("#/about/bob"); expect($state.href("about.person.item", { person: "bob", id: null })).toEqual("#/about/bob/"); })); + + it('generates absolute url when absolute is true', inject(function ($state) { + expect($state.href("about.sidebar", null, { absolute: true })).toEqual("http://server/#/about"); + locationProvider.html5Mode(true); + expect($state.href("about.sidebar", null, { absolute: true })).toEqual("http://server/about"); + })); }); describe('.get()', function () {