notification-bubbles.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ;(function($){
  2. const sMenuId = 'data-group-id',
  3. sParentsGroupSelector = 'data-talks-type',
  4. { menuAddonCounter } = window.oMessengerSelectors.SYSTEM;
  5. let oMenuBubbleStructure = null;
  6. $.fn.extend({
  7. updateMenuBubbles: function(iLotId, mixedMode, bAdd = true) {
  8. const { oNotifications } = this.get(0),
  9. sLotId = iLotId.toString();
  10. if (oMenuBubbleStructure === null || !iLotId)
  11. return;
  12. if (bAdd)
  13. return executeAction(mixedMode, 'add', sLotId);
  14. performAction('delete', sLotId);
  15. },
  16. initMenuBubbles: function(){
  17. const { oNotifications } = this.get(0),
  18. { collapsableArea, chevronUpIcon, addonWrapper } = window.oMessengerSelectors.MENU_AREA;
  19. if (oMenuBubbleStructure === null)
  20. oMenuBubbleStructure = oNotifications;
  21. const fFunc = function(){
  22. if (+$(menuAddonCounter, this).html())
  23. $(addonWrapper, this).removeClass('hidden');
  24. };
  25. $(collapsableArea).on('click', function(){
  26. if (!$(this).find(chevronUpIcon).is(':visible'))
  27. $(addonWrapper, this).addClass('hidden');
  28. else
  29. fFunc.apply(this);
  30. }).each(fFunc);
  31. }
  32. });
  33. function updateInbox(bInc = true){
  34. let iInbox = +oMenuBubbleStructure['inbox'];
  35. if (iInbox > 0 && !bInc)
  36. iInbox--;
  37. if (bInc)
  38. iInbox++;
  39. oMenuBubbleStructure['inbox'] = iInbox;
  40. }
  41. function executeAction(oType, sAction, iConvoId){
  42. const { type, groups_type, group_id } = oType;
  43. try {
  44. switch(sAction){
  45. case 'delete':
  46. if (typeof groups_type !== 'undefined') {
  47. delete oMenuBubbleStructure[type][groups_type][group_id][iConvoId];
  48. if (!Object.keys(oMenuBubbleStructure[type][groups_type][group_id]).length){
  49. delete oMenuBubbleStructure[type][groups_type][group_id];
  50. if (!Object.keys(oMenuBubbleStructure[type][groups_type]).length)
  51. delete oMenuBubbleStructure[type][groups_type][group_id];
  52. if (!Object.keys(oMenuBubbleStructure[type]).length)
  53. delete oMenuBubbleStructure[type];
  54. }
  55. } else
  56. delete oMenuBubbleStructure[type][iConvoId];
  57. updateInbox(false);
  58. break;
  59. case 'add':
  60. let bExecute = false;
  61. if ( typeof groups_type !== 'undefined' ) {
  62. if (typeof oMenuBubbleStructure[type][groups_type] === 'undefined') {
  63. oMenuBubbleStructure[type][groups_type] = {[group_id]: {[iConvoId]: 1}};
  64. bExecute = true;
  65. }
  66. else
  67. if (typeof oMenuBubbleStructure[type][groups_type][group_id] === 'undefined') {
  68. oMenuBubbleStructure[type][groups_type][group_id] = {[iConvoId]: 1};
  69. bExecute = true;
  70. }
  71. else
  72. if (typeof oMenuBubbleStructure[type][groups_type][group_id][iConvoId] === 'undefined') {
  73. oMenuBubbleStructure[type][groups_type][group_id][iConvoId] = 1;
  74. bExecute = true;
  75. }
  76. } else
  77. if ( typeof oMenuBubbleStructure[type][iConvoId] === 'undefined' ) {
  78. oMenuBubbleStructure[type][iConvoId] = 1;
  79. bExecute = true;
  80. }
  81. if (bExecute)
  82. updateInbox();
  83. }
  84. } catch (e) {
  85. console.log('error counter update', e);
  86. }
  87. updateNavMenuBubbles(type, groups_type);
  88. }
  89. function performAction(sAction, iConvoId){
  90. for (const sType in oMenuBubbleStructure){
  91. if (sType === 'inbox')
  92. continue;
  93. if (sType === 'groups'){
  94. const oGroups = oMenuBubbleStructure[sType];
  95. for(const sGroup in oGroups){
  96. for(const iGroupId in oGroups[sGroup]){
  97. const aConvos = Object.keys(oGroups[sGroup][iGroupId]);
  98. if (~aConvos.indexOf(iConvoId)){
  99. return executeAction({ type: sType, groups_type: sGroup, group_id: iGroupId}, sAction, iConvoId);
  100. }
  101. }
  102. }
  103. }
  104. else
  105. {
  106. const oItems = oMenuBubbleStructure[sType],
  107. aConvosList = Object.keys(oItems);
  108. if (~aConvosList.indexOf(iConvoId))
  109. return executeAction({ type: sType }, sAction, iConvoId);
  110. }
  111. }
  112. }
  113. function updateNavMenuBubbles(sType, sSubGroup){
  114. let iCounter = 0;
  115. if (sType === 'groups' && typeof sSubGroup === 'string') {
  116. if (typeof oMenuBubbleStructure[sType][sSubGroup] !== 'undefined'){
  117. let iMainCounter = Object.keys(oMenuBubbleStructure[sType][sSubGroup]).length;
  118. const oTalkObject = $(`[${sParentsGroupSelector}="${sSubGroup}"]`),
  119. fFunc = (oGroups, iGroupId) => {
  120. let iCounter = oGroups[iGroupId] ? Object.keys(oGroups[iGroupId]).length : 0,
  121. oGroup = $(`[${sMenuId}="${iGroupId}"] ${menuAddonCounter}`);
  122. oGroup.html(iCounter);
  123. if (!iCounter)
  124. oGroup.parent().addClass('hidden');
  125. else
  126. oGroup.parent().removeClass('hidden');
  127. };
  128. oTalkObject.html(iMainCounter);
  129. if (!iMainCounter)
  130. oTalkObject.parent().addClass('hidden');
  131. else
  132. oTalkObject.parent().removeClass('hidden');
  133. const oGroupItems = oMenuBubbleStructure[sType][sSubGroup];
  134. if (!Object.keys(oGroupItems).length)
  135. $(`li[class*="${sSubGroup}"] li`).each(function(){
  136. let iGroupId = +$(this).data('group-id');
  137. if (iGroupId)
  138. fFunc(oGroupItems, iGroupId);
  139. });
  140. else
  141. Object.keys(oGroupItems).forEach((iGroupId) => fFunc(oGroupItems, iGroupId));
  142. }
  143. }
  144. else
  145. {
  146. let oTalkType = $(`[${sParentsGroupSelector}="${sType}"]`);
  147. iCounter = +Object.keys(oMenuBubbleStructure[sType]).length;
  148. oTalkType.html(iCounter);
  149. if (!iCounter)
  150. oTalkType
  151. .parent()
  152. .addClass('hidden');
  153. else
  154. oTalkType
  155. .parent()
  156. .removeClass('hidden');
  157. }
  158. const oIndex = $(`[${sParentsGroupSelector}="inbox"]`).html(oMenuBubbleStructure['inbox']);
  159. if (+oMenuBubbleStructure['inbox'])
  160. oIndex.parent().removeClass('hidden');
  161. else
  162. oIndex.parent().addClass('hidden');
  163. }
  164. })(jQuery);