n10nBtnView.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Licensed Materials - Property of IBM
  3. *
  4. * IBM Cognos Products: SHARE
  5. *
  6. * (C) Copyright IBM Corp. 2015, 2018
  7. *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure
  9. * restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define([
  12. 'bi/glass/app/NavbarButtonSlideoutController', 'underscore', 'bi/notifications/app/n10nController', 'bi/sharecommon/utils/translator'
  13. ], function(View, _, controller, t) {
  14. 'use strict';
  15. var buttonView = View.extend({
  16. onRender: function(context) {
  17. if (context) {
  18. this.glassContext = context.glassContext;
  19. // initially place an aria-live tag on the button.
  20. context.target.plugin.$el.attr('aria-live', 'polite');
  21. }
  22. /*
  23. * Listen for the Notification Service's
  24. * 'notification_button:update' event to update the badge text on
  25. * this button
  26. */
  27. this.glassContext.getSvc('.Notification').then(function(notificationSvc) {
  28. notificationSvc.on('notifications:newCount', function(newCount) {
  29. this.updateButtonText(newCount);
  30. }.bind(this));
  31. notificationSvc.getNewNotifications();
  32. }.bind(this));
  33. },
  34. /**
  35. * Function to update the badge number on the Notifications button
  36. *
  37. * @param newCount number to use to update the badge with
  38. */
  39. updateButtonText: function(newCount) {
  40. var badgeText = '';
  41. // This grabs all notification buttons in all perspectives.
  42. var $button = $('.n10n_badge button');
  43. if ($button.length > 0) {
  44. if (newCount > 0) {
  45. if (newCount > controller.MAX_DISPLAY) {
  46. badgeText = controller.MAX_DISPLAY + '+';
  47. } else {
  48. badgeText = newCount;
  49. }
  50. $button.attr('data-badge', badgeText);
  51. $button.attr('aria-label', t.translate('notification_button_accessible_label', {count: badgeText}));
  52. } else {
  53. $button.removeAttr('data-badge');
  54. $button.removeAttr('aria-label');
  55. }
  56. }
  57. }
  58. });
  59. return buttonView;
  60. });