roomInfo.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. var roomInfo = (function() {
  2. var /** @const @type {Element} */
  3. dom = document.createElement("div"),
  4. /** @const @type {Element} */
  5. domHeader = document.createElement("header"), // Contain chan name + backgroundImg if any
  6. /** @const @type {Element} */
  7. headerContent = document.createElement("h3"), // Contain chan name + backgroundImg if any
  8. /** @const @type {Element} */
  9. section = document.createElement("div"),
  10. /** @const @type {Element} */
  11. topicDom = document.createElement("div"),
  12. /** @const @type {Element} */
  13. topicContent = document.createElement("span"),
  14. /** @const @type {Element} */
  15. topicDetails = document.createElement("span"),
  16. /** @const @type {Element} */
  17. phone = document.createElement("div"),
  18. /** @const @type {Element} */
  19. purposeDom = document.createElement("div"),
  20. /** @const @type {Element} */
  21. purposeContent = document.createElement("span"),
  22. /** @const @type {Element} */
  23. purposeDetails = document.createElement("span"),
  24. /** @const @type {Element} */
  25. pinCount = document.createElement("div"),
  26. /** @const @type {Element} */
  27. pinList = document.createElement("ul"),
  28. /** @const @type {Element} */
  29. userCount = document.createElement("div"),
  30. /** @const @type {Element} */
  31. userList = document.createElement("ul"),
  32. /** @type {ChatSystem} */
  33. currentChatCtx,
  34. /** @type {Room} */
  35. currentChan;
  36. dom.className = R.klass.chatList.roomInfo.container;
  37. domHeader.className = R.klass.chatList.roomInfo.title;
  38. topicDom.className = R.klass.chatList.roomInfo.topic;
  39. purposeDom.className = R.klass.chatList.roomInfo.purpose;
  40. phone.className = R.klass.chatList.roomInfo.phone;
  41. pinCount.className = R.klass.chatList.roomInfo.pinCount;
  42. pinList.className = R.klass.chatList.roomInfo.pinList;
  43. userCount.className = R.klass.chatList.roomInfo.userCount;
  44. userList.className = R.klass.chatList.roomInfo.userList;
  45. purposeDetails.className = topicDetails.className = R.klass.chatList.roomInfo.author;
  46. domHeader.appendChild(headerContent);
  47. dom.appendChild(domHeader);
  48. dom.appendChild(section);
  49. topicDom.appendChild(topicContent);
  50. topicDom.appendChild(topicDetails);
  51. purposeDom.appendChild(purposeContent);
  52. purposeDom.appendChild(purposeDetails);
  53. section.appendChild(topicDom);
  54. section.appendChild(phone);
  55. section.appendChild(purposeDom);
  56. section.appendChild(pinCount);
  57. section.appendChild(pinList);
  58. section.appendChild(userCount);
  59. section.appendChild(userList);
  60. var removePin = function() {
  61. if (currentChan.pins)
  62. for (var i =0, nbPins = currentChan.pins.length; i < nbPins; i++)
  63. if (currentChan.pins[i].id === this.dataset["msgId"]) {
  64. unpinMsg(currentChan, currentChan.pins[i]);
  65. break;
  66. }
  67. }
  68. var updateCommon = function() {
  69. headerContent.textContent = currentChan.name;
  70. if (currentChan.pins) {
  71. pinCount.textContent = locale.pinCount(currentChan.pins.length);
  72. pinCount.classList.remove(R.klass.hidden);
  73. pinList.classList.remove(R.klass.hidden);
  74. var pinFrag = document.createDocumentFragment();
  75. currentChan.pins.forEach(function(uiMsg) {
  76. var li = document.createElement("li"),
  77. unpinButton = document.createElement("a");
  78. unpinButton.href = "javascript:void(0)";
  79. unpinButton.dataset["msgId"] = uiMsg.id;
  80. unpinButton.addEventListener("click", removePin);
  81. unpinButton.className = R.klass.button +' ' +R.klass.chatList.roomInfo.unpin;
  82. li.className = R.klass.chatList.roomInfo.pinItem;
  83. li.appendChild(uiMsg.getDom());
  84. li.appendChild(unpinButton);
  85. pinFrag.appendChild(li);
  86. });
  87. pinList.textContent = '';
  88. pinList.appendChild(pinFrag);
  89. } else {
  90. pinCount.classList.add(R.klass.hidden);
  91. pinList.classList.add(R.klass.hidden);
  92. }
  93. }, updateForChannel = function() {
  94. var ctx = currentChatCtx.getChatContext();
  95. if (ctx.capacities["topic"]) {
  96. topicDom.classList.remove(R.klass.hidden);
  97. topicContent.textContent = currentChan.topic || "";
  98. topicDetails.textContent = currentChan.topicCreator ? locale.topicDetail(currentChan.topicCreator, currentChan.topicTs) : "";
  99. } else {
  100. topicDom.classList.add(R.klass.hidden);
  101. }
  102. if (ctx.capacities["purpose"]) {
  103. purposeDom.classList.remove(R.klass.hidden);
  104. purposeContent.textContent = currentChan.purpose || "";
  105. purposeDetails.textContent = currentChan.purposeCreator ? locale.topicDetail(currentChan.purposeCreator, currentChan.purposeTs) : "";
  106. } else {
  107. purposeDom.classList.add(R.klass.hidden);
  108. }
  109. domHeader.style.backgroundImage = "";
  110. headerContent.classList.remove(R.klass.presenceIndicator);
  111. dom.classList.add(R.klass.chatList.roomInfo.type.channel);
  112. dom.classList.remove(R.klass.chatList.roomInfo.type.user);
  113. }, updateForDm = function() {
  114. domHeader.style.backgroundImage = "url(" +currentChan.user.getLargeIcon() +")";
  115. topicContent.textContent = (currentChan.user.realName || ((currentChan.user.firstName || "") +' ' +(currentChan.user.lastName))).trim();
  116. headerContent.classList.add(R.klass.presenceIndicator);
  117. if (currentChan.user.presence)
  118. headerContent.classList.remove(R.klass.presenceAway);
  119. else
  120. headerContent.classList.add(R.klass.presenceAway);
  121. topicDom.classList.remove(R.klass.hidden);
  122. phone.classList.remove(R.klass.hidden);
  123. phone.textContent = currentChan.user.phone || "";
  124. purposeContent.textContent = currentChan.user.goal || "";
  125. purposeDom.classList.remove(R.klass.hidden);
  126. dom.classList.remove(R.klass.chatList.roomInfo.type.channel);
  127. dom.classList.add(R.klass.chatList.roomInfo.type.user);
  128. }, _update = function() {
  129. updateCommon();
  130. if (currentChan instanceof PrivateMessageRoom)
  131. updateForDm();
  132. else
  133. updateForChannel();
  134. };
  135. return {
  136. /** @type {function(ChatSystem, Room)} */
  137. populate: function(channelContext, channel) {
  138. currentChatCtx = channelContext;
  139. currentChan = channel;
  140. _update();
  141. return this;
  142. },
  143. update: function() {
  144. _update();
  145. return this;
  146. },
  147. /** @type {function(Element)} */
  148. show: function(domParent) {
  149. domParent.appendChild(dom);
  150. dom.classList.remove(R.klass.hidden);
  151. return this;
  152. },
  153. hide: function() {
  154. dom.classList.add(R.klass.hidden);
  155. return this;
  156. },
  157. isParentOf: function(_dom) {
  158. while (_dom) {
  159. if (_dom === dom)
  160. return true;
  161. _dom = _dom.parentNode;
  162. }
  163. return false;
  164. }
  165. }
  166. })();