From 57220c567655d2c1a766d2152bbb5c2b11cbb6e0 Mon Sep 17 00:00:00 2001 From: Ramiro Saenz Date: Tue, 12 Jul 2016 18:52:25 -0300 Subject: [PATCH] Badge bug fixed Bug when badge in title is like (2.300) Fixes #91 Fixes #92 --- app/Application.js | 2 +- app/util/Format.js | 14 +++++++ app/ux/WebView.js | 22 ++++++++--- app/ux/mixin/Badge.js | 90 +++++++++++++++++++++---------------------- 4 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 app/util/Format.js diff --git a/app/Application.js b/app/Application.js index 77fef7da..3ad2c61a 100644 --- a/app/Application.js +++ b/app/Application.js @@ -149,7 +149,7 @@ Ext.define('Rambox.Application', { ,updateTotalNotifications: function( newValue, oldValue ) { newValue = parseInt(newValue); if ( newValue > 0 ) { - document.title = 'Rambox (' + newValue + ')'; + document.title = 'Rambox (' + Rambox.util.Format.formatNumber(newValue) + ')'; } else { document.title = 'Rambox'; } diff --git a/app/util/Format.js b/app/util/Format.js new file mode 100644 index 00000000..eef3b5b6 --- /dev/null +++ b/app/util/Format.js @@ -0,0 +1,14 @@ +/** + * Created by vsxed on 7/11/2016. + */ +Ext.define('Rambox.util.Format', { + singleton: true + + ,formatNumber: function(n) { + return n.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); + } + + ,stripNumber: function(n) { + return (typeof n == "number") ? n : parseInt(n.match(/\d+/g).join("")); + } +}); diff --git a/app/ux/WebView.js b/app/ux/WebView.js index 3a46c59b..fb561eed 100644 --- a/app/ux/WebView.js +++ b/app/ux/WebView.js @@ -5,6 +5,10 @@ Ext.define('Rambox.ux.WebView',{ extend: 'Ext.panel.Panel' ,xtype: 'webview' + ,requires: [ + 'Rambox.util.Format' + ] + // private ,notifications: 0 ,zoomLevel: 0 @@ -74,7 +78,11 @@ Ext.define('Rambox.ux.WebView',{ ,onBadgeTextChange: function( tab, badgeText, oldBadgeText ) { if ( oldBadgeText === null ) oldBadgeText = 0; var actualNotifications = Rambox.app.getTotalNotifications(); - Rambox.app.setTotalNotifications(actualNotifications - parseInt(oldBadgeText) + parseInt(badgeText)); + + oldBadgeText = Rambox.util.Format.stripNumber(oldBadgeText); + badgeText = Rambox.util.Format.stripNumber(badgeText); + + Rambox.app.setTotalNotifications(actualNotifications - oldBadgeText + badgeText); } ,onAfterRender: function() { @@ -131,8 +139,10 @@ Ext.define('Rambox.ux.WebView',{ webview.addEventListener("page-title-updated", function(e) { var count = e.title.match(/\(([^)]+)\)/); // Get text between (...) count = count ? count[1] : '0'; - count = count.match(/\d+/g); // Some services have special characters. Example: (•) - count = count ? parseInt(count[0]) : 0; + count = count.match(/\d+/g).join(""); // Some services have special characters. Example: (•) + count = count ? parseInt(count) : 0; + + var formattedCount = Rambox.util.Format.formatNumber(count); switch ( me.type ) { case 'messenger': @@ -140,7 +150,7 @@ Ext.define('Rambox.ux.WebView',{ me.notifications = count; } if ( count || e.title === 'Messenger' ) { - me.tab.setBadgeText(count); + me.tab.setBadgeText(formattedCount); } if ( e.title === 'Messenger' ) me.notifications = 0; break; @@ -149,12 +159,12 @@ Ext.define('Rambox.ux.WebView',{ me.notifications = count; } if ( count || e.title === 'Google Hangouts' ) { - me.tab.setBadgeText(count); + me.tab.setBadgeText(formattedCount); } if ( e.title === 'Google Hangouts' ) me.notifications = 0; break; default: - me.tab.setBadgeText(count); + me.tab.setBadgeText(formattedCount); me.notifications = count; break; } diff --git a/app/ux/mixin/Badge.js b/app/ux/mixin/Badge.js index e584f397..985c2f07 100644 --- a/app/ux/mixin/Badge.js +++ b/app/ux/mixin/Badge.js @@ -1,49 +1,49 @@ /** - * Created by whiskeredwonder on 7/30/2015. - */ +* Created by whiskeredwonder on 7/30/2015. +*/ Ext.define('Rambox.ux.mixin.Badge', { - extend: 'Ext.Mixin', - - requires: [ - //require this for the override - 'Ext.button.Button' - ], - - mixinConfig: { - id: 'badge', - after: { - onRender: 'renderBadgeText' - } - }, - - config: { - badgeText: null - }, - - renderBadgeText: function() { - var badgeText = this.getBadgeText(); - - if (badgeText) { - this.updateBadgeText(badgeText); - } - }, - - updateBadgeText: function(badgeText, oldBadgeText) { - var me = this, - el = me.el; - - if (me.rendered) { - el.set({ - 'data-badge-text': badgeText - }); - - el.toggleCls(Ext.baseCSSPrefix + 'badge', !! badgeText); - - me.fireEvent('badgetextchange', me, badgeText, oldBadgeText); - } - } + extend: 'Ext.Mixin', + + requires: [ + //require this for the override + 'Ext.button.Button' + ], + + mixinConfig: { + id: 'badge', + after: { + onRender: 'renderBadgeText' + } + }, + + config: { + badgeText: null + }, + + renderBadgeText: function() { + var badgeText = this.getBadgeText(); + + if (badgeText) { + this.updateBadgeText(badgeText); + } + }, + + updateBadgeText: function(badgeText, oldBadgeText) { + var me = this, + el = me.el; + + if (me.rendered && badgeText !== '0') { + el.set({ + 'data-badge-text': badgeText + }); + + el.toggleCls(Ext.baseCSSPrefix + 'badge', !! badgeText); + + me.fireEvent('badgetextchange', me, badgeText, oldBadgeText); + } + } }, function(BadgeMixin) { - Ext.override(Ext.button.Button, { - mixins: [BadgeMixin] - }); + Ext.override(Ext.button.Button, { + mixins: [BadgeMixin] + }); });