DRY-refactor notification display conditions

* Condense the different notification icons into one span with a
  dynamically selected ng-class.
* Move logic for determining which notification icon to show and whether
  there are no notifications into AuthCtrl.
* Refactor out the two similar sequences of logic conditions into a
  helper function.
This commit is contained in:
Brian Chen 2014-10-04 17:56:56 +08:00
parent ef83c9999b
commit 74be4b211b
2 changed files with 27 additions and 4 deletions

View file

@ -94,5 +94,30 @@ angular.module('authCtrl', [])
$scope.expandMenu = function(menu) {
$scope._expandedMenu = ($scope._expandedMenu == menu) ? null : menu;
};
function selectNotificationValue(mysteryValue, invitationValue, messageValue, noneValue) {
var user = $scope.user;
if (user.purchased.plan.mysteryItems.length) {
return mysteryValue;
} else if ((user.invitations.party && user.invitations.party.id) || user.invitations.guilds.length > 0) {
return invitationValue;
} else if (!(_.isEmpty(user.newMessages))) {
return messageValue;
} else {
return noneValue;
}
};
$scope.iconClasses = function() {
return selectNotificationValue(
"glyphicon-gift",
"glyphicon-user",
"glyphicon-comment",
"glyphicon-comment inactive");
};
$scope.hasNoNotifications = function() {
return selectNotificationValue(false, false, false, true);
}
}
]);

View file

@ -137,14 +137,12 @@ nav.toolbar(ng-controller='AuthCtrl', ng-class='{active: isToolbarHidden}')
ul.toolbar-options
li.toolbar-notifs
a(ng-click='expandMenu("notifs")')
span.glyphicon.glyphicon-comment(ng-if='!user.invitations.party.id && !user.purchased.plan.mysteryItems.length && user.invitations.guilds.length === 0', ng-class='{inactive: _.isEmpty(user.newMessages)}')
span.glyphicon.glyphicon-user(ng-if='!user.purchased.plan.mysteryItems.length && (user.invitations.party.id || user.invitations.guilds.length > 0)')
span.glyphicon.glyphicon-gift(ng-if='user.purchased.plan.mysteryItems.length')
span.glyphicon(ng-class='iconClasses()')
div(ng-if='_expandedMenu=="notifs"')
h4=env.t('notifications')
div
ul.toolbar-notifs-notifs
li.toolbar-notifs-no-messages(ng-if='!user.invitations.party.id && !user.purchased.plan.mysteryItems.length && user.invitations.guilds.length === 0 && _.isEmpty(user.newMessages)')=env.t('noNotifications')
li.toolbar-notifs-no-messages(ng-if='hasNoNotifications()')=env.t('noNotifications')
li(ng-if='user.purchased.plan.mysteryItems.length')
a(ng-click='$state.go("options.inventory.drops"); expandMenu(null)')
span.glyphicon.glyphicon-gift