habitica/public/js/services/notificationServices.js

81 lines
2.9 KiB
JavaScript
Raw Normal View History

/**
Set up "+1 Exp", "Level Up", etc notifications
*/
angular.module("notificationServices", [])
.factory("Notification", [function() {
function growl(html, type) {
$.bootstrapGrowl(html, {
ele: '#notification-area',
type: type || 'warning', //('info', 'text', 'warning', 'success', 'gp', 'xp', 'hp', 'lvl', 'death', 'mp', 'crit')
top_offset: 20,
align: 'right', //('left', 'right', or 'center')
width: 250, //(integer, or 'auto')
delay: (type=='error') ? 0 : 7000,
allow_dismiss: true,
stackup_spacing: 10 // spacing between consecutive stacecked growls.
});
};
/**
Show "+ 5 {gold_coin} 3 {silver_coin}"
*/
function coins(money) {
var absolute, gold, silver;
absolute = Math.abs(money);
gold = Math.floor(absolute);
silver = Math.floor((absolute - gold) * 100);
if (gold && silver > 0) {
2014-02-04 19:53:09 +00:00
return "" + gold + " <span class='icon-gold'></span> " + silver + " <span class='icon-silver'></span>";
} else if (gold > 0) {
2014-02-04 19:53:09 +00:00
return "" + gold + " <span class='icon-gold'></span>";
} else if (silver > 0) {
2014-02-04 19:53:09 +00:00
return "" + silver + " <span class='icon-silver'></span>";
}
};
var sign = function(number){
return number?number<0?'-':'+':'+';
}
var round = function(number){
return Math.abs(number.toFixed(1));
}
return {
coins: coins,
hp: function(val) {
// don't show notifications if user dead
2014-02-06 16:36:33 +00:00
growl("<span class='glyphicon glyphicon-heart'></span>&nbsp; " + sign(val) + " " + round(val) + " " + window.env.t('hp'), 'hp');
},
exp: function(val) {
if (val < -50) return; // don't show when they level up (resetting their exp)
2014-02-06 16:36:33 +00:00
growl("<span class='glyphicon glyphicon-star'></span>&nbsp; " + sign(val) + " " + round(val) + " " + window.env.t('xp'), 'xp');
},
gp: function(val, bonus) {
growl(sign(val) + " " + coins(val - bonus), 'gp');
},
text: function(val){
growl(val);
},
lvl: function(){
2014-02-06 16:36:33 +00:00
growl('<span class="glyphicon glyphicon-chevron-up"></span>&nbsp;' + window.env.t('levelUp'), 'lvl');
},
death: function(){
2014-02-06 16:36:33 +00:00
growl("<span class='glyphicon glyphicon-death'></span>&nbsp;" + window.env.t('respawn'), "death");
},
error: function(error){
growl("<span class='glyphicon glyphicon-exclamation-sign'></span>&nbsp; " + error, "danger");
2013-12-26 13:31:00 +00:00
},
mp: function(val) {
2014-02-06 16:36:33 +00:00
growl("<span class='glyphicon glyphicon-fire'></span>&nbsp; " + sign(val) + " " + round(val) + " " + window.env.t('mp'), 'mp');
},
crit: function(val) {
2014-02-06 16:36:33 +00:00
growl("<span class='glyphicon glyphicon-certificate'></span>&nbsp;" + window.env.t('critBonus') + Math.round(val) + "%", 'crit');
2014-01-15 04:02:22 +00:00
},
drop: function(val) {
2014-02-04 19:53:09 +00:00
growl("<span class='glyphicon glyphicon-gift'></span>&nbsp; " + val, 'drop');
}
};
}
]);