Skip to content

Instantly share code, notes, and snippets.

@mhartington
Created March 31, 2016 16:32
Show Gist options
  • Save mhartington/b1fdde58d9a40fcafc8d43d777c40b36 to your computer and use it in GitHub Desktop.
Save mhartington/b1fdde58d9a40fcafc8d43d777c40b36 to your computer and use it in GitHub Desktop.
.directive('navBarClass', function() {
return {
restrict: 'A',
compile: function(element, attrs) {
// We need to be able to add a class the cached nav-bar
// Which provides the background color
var cachedNavBar = document.querySelector('.nav-bar-block[nav-bar="cached"]');
var cachedHeaderBar = cachedNavBar.querySelector('.bar-header');
// And also the active nav-bar
// which provides the right class for the title
var activeNavBar = document.querySelector('.nav-bar-block[nav-bar="active"]');
var activeHeaderBar = activeNavBar.querySelector('.bar-header');
var barClass = attrs.navBarClass;
var ogColors = [];
var colors = ['positive', 'stable', 'light', 'royal', 'dark', 'assertive', 'calm', 'energized'];
var cleanUp = function() {
for (var i = 0; i < colors.length; i++) {
var currentColor = activeHeaderBar.classList.contains('bar-' + colors[i]);
if (currentColor) {
ogColors.push('bar-' + colors[i]);
}
activeHeaderBar.classList.remove('bar-' + colors[i]);
cachedHeaderBar.classList.remove('bar-' + colors[i]);
}
};
return function($scope) {
$scope.$on('$ionicView.beforeEnter', function() {
cleanUp();
cachedHeaderBar.classList.add(barClass);
activeHeaderBar.classList.add(barClass);
});
$scope.$on('$ionicView.beforeLeave', function() {
for (var j = 0; j < ogColors.length; j++) {
activeHeaderBar.classList.add(ogColors[j]);
cachedHeaderBar.classList.add(ogColors[j]);
}
cachedHeaderBar.classList.remove(barClass);
activeHeaderBar.classList.remove(barClass);
ogColors = [];
});
};
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment