-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,83 +3,90 @@ | |
* | ||
* @link https://github.com/nonplus/angular-ui-router-default | ||
* | ||
* @license angular-ui-router-default v0.0.4 | ||
* @license angular-ui-router-default v0.0.5 | ||
* (c) Copyright Stepan Riha <[email protected]> | ||
* License MIT | ||
*/ | ||
|
||
(function(angular) { | ||
|
||
/* global angular */ | ||
"use strict"; | ||
var moduleName = 'ui.router.default'; | ||
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports) { | ||
module.exports = moduleName; | ||
} | ||
var max_redirects = 10; | ||
angular.module('ui.router.default', ['ui.router']) | ||
.config(['$provide', function($provide) { | ||
$provide.decorator('$state', ['$delegate', '$injector', '$q', function($delegate, $injector, $q) { | ||
var transitionTo = $delegate.transitionTo; | ||
var pendingPromise; | ||
$delegate.transitionTo = function(to, toParams, options) { | ||
var numRedirects = 0; | ||
var $state = this; | ||
var nextState = to.name || to; | ||
var nextParams = toParams; | ||
var nextOptions = options; | ||
angular.module(moduleName, ['ui.router']) | ||
.config(['$provide', function ($provide) { | ||
$provide.decorator('$state', ['$delegate', '$injector', '$q', function ($delegate, $injector, $q) { | ||
var transitionTo = $delegate.transitionTo; | ||
var pendingPromise; | ||
$delegate.transitionTo = function (to, toParams, options) { | ||
var numRedirects = 0; | ||
var $state = this; | ||
var nextState = to.name || to; | ||
var nextParams = toParams; | ||
var nextOptions = options; | ||
return fetchTarget(); | ||
function fetchTarget() { | ||
var target = $state.get(nextState, $state.$current); | ||
nextState = (target || {}).name; | ||
var absRedirectPromise = getAbstractRedirect(target); | ||
pendingPromise = absRedirectPromise; | ||
return $q.when(absRedirectPromise) | ||
.then(abstractTargetResolved); | ||
function abstractTargetResolved(abstractTarget) { | ||
if (absRedirectPromise !== pendingPromise) { | ||
return $q.reject(new Error('transition superseded')); | ||
} | ||
// we didn't get anything from the abstract target | ||
if (!abstractTarget) { | ||
return transitionTo.call($delegate, nextState, nextParams, nextOptions); | ||
} | ||
checkForMaxRedirect(); | ||
nextState = abstractTarget; | ||
return fetchTarget(); | ||
} | ||
function checkForMaxRedirect() { | ||
if (numRedirects === max_redirects) { | ||
throw new Error('Too many abstract state default redirects'); | ||
} | ||
numRedirects += 1; | ||
} | ||
} | ||
function getAbstractRedirect(state) { | ||
if (!state || !state.abstract || (state.abstract === true && !state.default)) { | ||
return null; | ||
} | ||
return invokeAbstract(state).then(abstractInvoked); | ||
function abstractInvoked(newState) { | ||
if (newState[0] === '.') { | ||
return nextState + newState; | ||
} | ||
else { | ||
return newState; | ||
} | ||
} | ||
} | ||
function invokeAbstract(state) { | ||
var defaultState; | ||
if (state.default) { | ||
defaultState = state.default; | ||
} | ||
else { | ||
defaultState = state.abstract; | ||
} | ||
if (defaultState instanceof Function || defaultState instanceof Array) { | ||
return $q.when($injector.invoke(defaultState)); | ||
} | ||
else { | ||
return $q.when(defaultState); | ||
} | ||
} | ||
}; | ||
return $delegate; | ||
}]); | ||
}]); | ||
|
||
return fetchTarget(); | ||
|
||
function fetchTarget() { | ||
var target = $state.get(nextState, $state.$current); | ||
nextState = (target|| {}).name; | ||
|
||
var absRedirectPromise = getAbstractRedirect(target); | ||
pendingPromise = absRedirectPromise; | ||
return $q.when(absRedirectPromise, abstractTargetResolved); | ||
|
||
function abstractTargetResolved(abstractTarget) { | ||
if(absRedirectPromise !== pendingPromise) { | ||
return $q.reject(new Error('transition superseded')); | ||
} | ||
// we didn't get anything from the abstract target | ||
if (!abstractTarget) { | ||
return transitionTo.call($delegate, nextState, nextParams, nextOptions); | ||
} | ||
checkForMaxRedirect(); | ||
nextState = abstractTarget; | ||
return fetchTarget(); | ||
} | ||
|
||
function checkForMaxRedirect() { | ||
if (numRedirects === max_redirects) { | ||
throw new Error('Too many abstract state default redirects'); | ||
} | ||
numRedirects += 1; | ||
} | ||
} | ||
function getAbstractRedirect(state) { | ||
if (!state || !state.abstract || state.abstract === true) { | ||
return null; | ||
} | ||
return invokeAbstract(state.abstract).then(abstractInvoked); | ||
function abstractInvoked(newState) { | ||
if (newState[0] === '.') { | ||
return nextState + newState; | ||
} else { | ||
return newState; | ||
} | ||
} | ||
|
||
} | ||
function invokeAbstract(abstract) { | ||
if (!angular.isString(abstract)) { | ||
return $q.when($injector.invoke(abstract)); | ||
} else { | ||
return $q.when(abstract); | ||
} | ||
} | ||
|
||
}; | ||
return $delegate; | ||
}]); | ||
}]); | ||
|
||
})(window.angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "angular-ui-router-default", | ||
"main": "angular-ui-router-default.js", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"homepage": "https://github.com/nonplus/angular-ui-router-default", | ||
"authors": [ | ||
"Stepan Riha <[email protected]>" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters