Skip to content

Commit

Permalink
refactor(ncyBreadcrumbText): coding style suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
tradiff committed Apr 5, 2015
1 parent 223896c commit b2ebc0f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 76 deletions.
49 changes: 14 additions & 35 deletions dist/angular-breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function BreadcrumbTextDirective($interpolate, $breadcrumb, $rootScope) {
return {
restrict: 'A',
scope: {},
template: '{{ncyBreadcrumbLabel}}',
template: '{{ncyBreadcrumbChain}}',

compile: function(cElement, cAttrs) {
// Override the default template if ncyBreadcrumbText has a value
Expand All @@ -298,69 +298,48 @@ function BreadcrumbTextDirective($interpolate, $breadcrumb, $rootScope) {
cElement.html(template);
}

var separator = cElement.attr(cAttrs.$attr.ncyBreadcrumbTextSeparator);
if (!separator) {
separator = ' / ';
}
var separator = cElement.attr(cAttrs.$attr.ncyBreadcrumbTextSeparator) || ' / ';

return {
post: function postLink(scope) {
var labelWatchers = [];

var registerWatchersText = function(labelWatcherArray, interpolationFunction, viewScope, step) {
var registerWatchersText = function(labelWatcherArray, interpolationFunction, viewScope) {
angular.forEach(getExpression(interpolationFunction), function(expression) {
var watcher = viewScope.$watch(expression, function() {
step.ncyBreadcrumbLabel = interpolationFunction(viewScope);
renderLabel();
var watcher = viewScope.$watch(expression, function(newValue, oldValue) {
if (newValue !== oldValue) {
renderLabel();
}
});
labelWatcherArray.push(watcher);
});
};

var setupWatchers = function() {
var renderLabel = function() {
deregisterWatchers(labelWatchers);

var viewScope = $breadcrumb.$getLastViewScope();
var steps = $breadcrumb.getStatesChain();
var combinedLabels = [];
angular.forEach(steps, function (step) {
if (step.ncyBreadcrumb && step.ncyBreadcrumb.label) {
var parseLabel = $interpolate(step.ncyBreadcrumb.label);
combinedLabels.push(parseLabel(viewScope));
// Watcher for further viewScope updates
registerWatchersText(labelWatchers, parseLabel, viewScope, step);
}
});

renderLabel();
};

var renderLabel = function() {
var viewScope = $breadcrumb.$getLastViewScope();
var steps = $breadcrumb.getStatesChain();
var combinedLabel = '';
angular.forEach(steps, function (step) {
if (step.ncyBreadcrumb && step.ncyBreadcrumb.label) {
var parseLabel = $interpolate(step.ncyBreadcrumb.label);
combinedLabel += separator + parseLabel(viewScope);
registerWatchersText(labelWatchers, parseLabel, viewScope);
} else {
combinedLabel += separator + step.name;
combinedLabels.push(step.name);
}
});

// strip leading separator
if (combinedLabel.length > separator.length) {
combinedLabel = combinedLabel.substring(separator.length);
}

scope.ncyBreadcrumbLabel = combinedLabel;

scope.ncyBreadcrumbChain = combinedLabels.join(separator);
};

$rootScope.$on('$viewContentLoaded', function () {
setupWatchers();
renderLabel();
});

// View(s) may be already loaded while the directive's linking
setupWatchers();
renderLabel();
}
};
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-breadcrumb.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<script src="asset/ui-bootstrap-tpls.js"></script>
<script src="asset/angular-breadcrumb.js"></script>

<title ncy-breadcrumb-text="Sample app : {{ncyBreadcrumbLabel}}"></title>
<title ncy-breadcrumb-text="Sample app : {{ncyBreadcrumbChain}}"></title>

</head>
<body>
Expand Down
49 changes: 14 additions & 35 deletions src/angular-breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ function BreadcrumbTextDirective($interpolate, $breadcrumb, $rootScope) {
return {
restrict: 'A',
scope: {},
template: '{{ncyBreadcrumbLabel}}',
template: '{{ncyBreadcrumbChain}}',

compile: function(cElement, cAttrs) {
// Override the default template if ncyBreadcrumbText has a value
Expand All @@ -293,69 +293,48 @@ function BreadcrumbTextDirective($interpolate, $breadcrumb, $rootScope) {
cElement.html(template);
}

var separator = cElement.attr(cAttrs.$attr.ncyBreadcrumbTextSeparator);
if (!separator) {
separator = ' / ';
}
var separator = cElement.attr(cAttrs.$attr.ncyBreadcrumbTextSeparator) || ' / ';

return {
post: function postLink(scope) {
var labelWatchers = [];

var registerWatchersText = function(labelWatcherArray, interpolationFunction, viewScope, step) {
var registerWatchersText = function(labelWatcherArray, interpolationFunction, viewScope) {
angular.forEach(getExpression(interpolationFunction), function(expression) {
var watcher = viewScope.$watch(expression, function() {
step.ncyBreadcrumbLabel = interpolationFunction(viewScope);
renderLabel();
var watcher = viewScope.$watch(expression, function(newValue, oldValue) {
if (newValue !== oldValue) {
renderLabel();
}
});
labelWatcherArray.push(watcher);
});
};

var setupWatchers = function() {
var renderLabel = function() {
deregisterWatchers(labelWatchers);

var viewScope = $breadcrumb.$getLastViewScope();
var steps = $breadcrumb.getStatesChain();
var combinedLabels = [];
angular.forEach(steps, function (step) {
if (step.ncyBreadcrumb && step.ncyBreadcrumb.label) {
var parseLabel = $interpolate(step.ncyBreadcrumb.label);
combinedLabels.push(parseLabel(viewScope));
// Watcher for further viewScope updates
registerWatchersText(labelWatchers, parseLabel, viewScope, step);
}
});

renderLabel();
};

var renderLabel = function() {
var viewScope = $breadcrumb.$getLastViewScope();
var steps = $breadcrumb.getStatesChain();
var combinedLabel = '';
angular.forEach(steps, function (step) {
if (step.ncyBreadcrumb && step.ncyBreadcrumb.label) {
var parseLabel = $interpolate(step.ncyBreadcrumb.label);
combinedLabel += separator + parseLabel(viewScope);
registerWatchersText(labelWatchers, parseLabel, viewScope);
} else {
combinedLabel += separator + step.name;
combinedLabels.push(step.name);
}
});

// strip leading separator
if (combinedLabel.length > separator.length) {
combinedLabel = combinedLabel.substring(separator.length);
}

scope.ncyBreadcrumbLabel = combinedLabel;

scope.ncyBreadcrumbChain = combinedLabels.join(separator);
};

$rootScope.$on('$viewContentLoaded', function () {
setupWatchers();
renderLabel();
});

// View(s) may be already loaded while the directive's linking
setupWatchers();
renderLabel();
}
};
Expand Down
2 changes: 1 addition & 1 deletion test/spec/directive-text-basic-separator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Text directive with separator with basic conf', function() {
describe('with template', function() {

beforeEach(inject(function($rootScope, $compile) {
element = angular.element('<span ncy-breadcrumb-text="{{ncyBreadcrumbLabel}} - MyApp" ncy-breadcrumb-text-separator=">"></span>');
element = angular.element('<span ncy-breadcrumb-text="{{ncyBreadcrumbChain}} - MyApp" ncy-breadcrumb-text-separator=">"></span>');
var compile = $compile(element);
scope = $rootScope.$new();
compile(scope);
Expand Down
2 changes: 1 addition & 1 deletion test/spec/directive-text-basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Text directive with basic conf', function() {
describe('with template', function() {

beforeEach(inject(function($rootScope, $compile) {
element = angular.element('<span ncy-breadcrumb-text="{{ncyBreadcrumbLabel}} - MyApp"></span>');
element = angular.element('<span ncy-breadcrumb-text="{{ncyBreadcrumbChain}} - MyApp"></span>');
var compile = $compile(element);
scope = $rootScope.$new();
compile(scope);
Expand Down
Loading

0 comments on commit b2ebc0f

Please sign in to comment.