habitica/public/js/services/notificationServices.js

75 lines
2.5 KiB
JavaScript
Raw Normal View History

/**
Set up "+1 Exp", "Level Up", etc notifications
*/
angular.module("notificationServices", [])
.factory("Notification", [function() {
2014-04-08 17:16:06 +00:00
function notify(html, type, icon) {
var notice = $.pnotify({
type: type || 'warning', //('info', 'text', 'warning', 'success', 'gp', 'xp', 'hp', 'lvl', 'death', 'mp', 'crit')
2014-04-08 17:16:06 +00:00
text: html,
opacity: 1,
addclass: 'alert-' + type,
icon: icon || false
}).click(function() { notice.pnotify_remove() });
};
/**
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) {
return "" + gold + " <span class='notification-icon shop_gold'></span> " + silver + " <span class='notification-icon shop_silver'></span>";
} else if (gold > 0) {
return "" + gold + " <span class='notification-icon shop_gold'></span>";
} else if (silver > 0) {
return "" + silver + " <span class='notification-icon shop_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-04-08 17:16:06 +00:00
notify(sign(val) + " " + round(val) + " " + window.env.t('hp'), 'hp', 'glyphicon glyphicon-heart');
},
exp: function(val) {
if (val < -50) return; // don't show when they level up (resetting their exp)
2014-04-08 17:16:06 +00:00
notify(sign(val) + " " + round(val) + " " + window.env.t('xp'), 'xp', 'glyphicon glyphicon-star');
},
gp: function(val, bonus) {
2014-04-08 17:16:06 +00:00
notify(sign(val) + " " + coins(val - bonus), 'gp');
},
text: function(val){
2014-04-08 17:16:06 +00:00
notify(val, 'info');
},
lvl: function(){
2014-04-08 17:16:06 +00:00
notify(window.env.t('levelUp'), 'lvl', 'glyphicon glyphicon-chevron-up');
},
error: function(error){
2014-04-08 17:16:06 +00:00
notify(error, "danger", 'glyphicon glyphicon-exclamation-sign');
2013-12-26 13:31:00 +00:00
},
mp: function(val) {
2014-04-08 17:16:06 +00:00
notify(sign(val) + " " + round(val) + " " + window.env.t('mp'), 'mp', 'glyphicon glyphicon-fire');
},
crit: function(val) {
2014-04-08 17:16:06 +00:00
notify(window.env.t('critBonus') + Math.round(val) + "%", 'crit', 'glyphicon glyphicon-certificate');
2014-01-15 04:02:22 +00:00
},
drop: function(val) {
2014-04-08 17:16:06 +00:00
notify(val, 'drop', 'glyphicon glyphicon-gift');
}
};
}
]);