Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with angular router and route-segment uses side-by-side #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 79 additions & 74 deletions build/angular-route-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,103 +223,108 @@ mod.provider( '$routeSegment',
$rootScope.$on('$routeChangeSuccess', function(event, args) {

var route = args.$route || args.$$route;
if(route && route.segment) {
if(!route || !route.segment) {
// Clear outermost segment if this route wasn't configured by route-segment
$rootScope.$broadcast( 'routeSegmentChange', {
index: 0,
segment: {} } );
return;
}

var segmentName = route.segment;
var segmentNameChain = segmentName.split(".");
var updates = [], lastUpdateIndex = -1;
var segmentName = route.segment;
var segmentNameChain = segmentName.split(".");
var updates = [], lastUpdateIndex = -1;

for(var i=0; i < segmentNameChain.length; i++) {

for(var i=0; i < segmentNameChain.length; i++) {

var newSegment = getSegmentInChain( i, segmentNameChain );
var newSegment = getSegmentInChain( i, segmentNameChain );

if(resolvingSemaphoreChain[i] != newSegment.name || updates.length > 0 || isDependenciesChanged(newSegment)) {
if(resolvingSemaphoreChain[i] != newSegment.name || updates.length > 0 || isDependenciesChanged(newSegment)) {

if($routeSegment.chain[i] && $routeSegment.chain[i].name == newSegment.name &&
updates.length == 0 && !isDependenciesChanged(newSegment))
// if we went back to the same state as we were before resolving new segment
resolvingSemaphoreChain[i] = newSegment.name;
else {
updates.push({index: i, newSegment: newSegment});
lastUpdateIndex = i;
}
}
}
if($routeSegment.chain[i] && $routeSegment.chain[i].name == newSegment.name &&
updates.length == 0 && !isDependenciesChanged(newSegment))
// if we went back to the same state as we were before resolving new segment
resolvingSemaphoreChain[i] = newSegment.name;
else {
updates.push({index: i, newSegment: newSegment});
lastUpdateIndex = i;
}
}
}

var curSegmentPromise = $q.when();
var curSegmentPromise = $q.when();

if(updates.length > 0) {
if(updates.length > 0) {

for(var i=0; i<updates.length; i++) {
(function(i) {
curSegmentPromise = curSegmentPromise.then(function() {
for(var i=0; i<updates.length; i++) {
(function(i) {
curSegmentPromise = curSegmentPromise.then(function() {

return updateSegment(updates[i].index, updates[i].newSegment);
return updateSegment(updates[i].index, updates[i].newSegment);

}).then(function(result) {
}).then(function(result) {

if(result.success != undefined) {
if(result.success != undefined) {

broadcast(result.success);
broadcast(result.success);

for(var j = updates[i].index + 1; j < $routeSegment.chain.length; j++) {
for(var j = updates[i].index + 1; j < $routeSegment.chain.length; j++) {

if($routeSegment.chain[j]) {
$routeSegment.chain[j] = null;
updateSegment(j, null);
}
if($routeSegment.chain[j]) {
$routeSegment.chain[j] = null;
updateSegment(j, null);
}
}
})
})(i);
}
}
})
})(i);
}
}

curSegmentPromise.then(function() {
curSegmentPromise.then(function() {

// Removing redundant segment in case if new segment chain is shorter than old one
if($routeSegment.chain.length > segmentNameChain.length) {
var oldLength = $routeSegment.chain.length;
var shortenBy = $routeSegment.chain.length - segmentNameChain.length;
$routeSegment.chain.splice(-shortenBy, shortenBy);
for(var i=segmentNameChain.length; i < oldLength; i++) {
updateSegment(i, null);
lastUpdateIndex = $routeSegment.chain.length-1;
}
// Removing redundant segment in case if new segment chain is shorter than old one
if($routeSegment.chain.length > segmentNameChain.length) {
var oldLength = $routeSegment.chain.length;
var shortenBy = $routeSegment.chain.length - segmentNameChain.length;
$routeSegment.chain.splice(-shortenBy, shortenBy);
for(var i=segmentNameChain.length; i < oldLength; i++) {
updateSegment(i, null);
lastUpdateIndex = $routeSegment.chain.length-1;
}
}).then(function() {

var defaultChildUpdatePromise = $q.when();

if(lastUpdateIndex == $routeSegment.chain.length-1) {

var curSegment = getSegmentInChain(lastUpdateIndex, $routeSegment.name.split("."));

while(curSegment) {
var children = curSegment.children, index = lastUpdateIndex+1;
curSegment = null;
for (var i in children) {
(function(i, children, index) {
if (children[i].params['default']) {
defaultChildUpdatePromise = defaultChildUpdatePromise.then(function () {
return updateSegment(index, {name: children[i].name, params: children[i].params})
.then(function (result) {
if (result.success) broadcast(result.success);
});
});
curSegment = children[i];
lastUpdateIndex = index;
}
})(i, children, index);

}
}).then(function() {

var defaultChildUpdatePromise = $q.when();

if(lastUpdateIndex == $routeSegment.chain.length-1) {

var curSegment = getSegmentInChain(lastUpdateIndex, $routeSegment.name.split("."));

while(curSegment) {
var children = curSegment.children, index = lastUpdateIndex+1;
curSegment = null;
for (var i in children) {
(function(i, children, index) {
if (children[i].params['default']) {
defaultChildUpdatePromise = defaultChildUpdatePromise.then(function () {
return updateSegment(index, {name: children[i].name, params: children[i].params})
.then(function (result) {
if (result.success) broadcast(result.success);
});
});
curSegment = children[i];
lastUpdateIndex = index;
}
})(i, children, index);


}
}
}
}

return defaultChildUpdatePromise;
});
}
return defaultChildUpdatePromise;
});
});

function isDependenciesChanged(segment) {
Expand Down
Loading