roomInfo.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.duplicateDom());
  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. userCount.textContent = locale.userCount(currentChan.users ? Object.keys(currentChan.users).length : 0);
  112. /** @type {Array<Chatter>} */
  113. var memberIds = [];
  114. if (currentChan.users)
  115. for (var id in currentChan.users)
  116. memberIds.push(currentChan.users[id]);
  117. memberIds.sort(function(a, b) {
  118. if (a.presence && !b.presence)
  119. return -1;
  120. if (b.presence && !a.presence)
  121. return 1;
  122. return a.getName().localeCompare(b.getName());
  123. });
  124. var userFrag = document.createDocumentFragment();
  125. memberIds.forEach(function(user) {
  126. var li = document.createElement("li"),
  127. link = document.createElement("a");
  128. link.href = '#' +user.id;
  129. link.textContent = user.getName();
  130. li.appendChild(link);
  131. li.classList.add(R.klass.presenceIndicator);
  132. if (!user.presence)
  133. li.classList.add(R.klass.presenceAway);
  134. userFrag.appendChild(li);
  135. });
  136. userList.textContent = "";
  137. userList.appendChild(userFrag);
  138. dom.classList.add(R.klass.chatList.roomInfo.type.channel);
  139. dom.classList.remove(R.klass.chatList.roomInfo.type.user);
  140. }, updateForDm = function() {
  141. domHeader.style.backgroundImage = "url(" +currentChan.user.getLargeIcon() +")";
  142. topicContent.textContent = (currentChan.user.realName || ((currentChan.user.firstName || "") +' ' +(currentChan.user.lastName))).trim();
  143. headerContent.classList.add(R.klass.presenceIndicator);
  144. if (currentChan.user.presence)
  145. headerContent.classList.remove(R.klass.presenceAway);
  146. else
  147. headerContent.classList.add(R.klass.presenceAway);
  148. topicDom.classList.remove(R.klass.hidden);
  149. phone.classList.remove(R.klass.hidden);
  150. phone.textContent = currentChan.user.phone || "";
  151. purposeContent.textContent = currentChan.user.goal || "";
  152. purposeDom.classList.remove(R.klass.hidden);
  153. dom.classList.remove(R.klass.chatList.roomInfo.type.channel);
  154. dom.classList.add(R.klass.chatList.roomInfo.type.user);
  155. }, _update = function() {
  156. updateCommon();
  157. if (currentChan instanceof PrivateMessageRoom)
  158. updateForDm();
  159. else
  160. updateForChannel();
  161. };
  162. var hideTimeo = null;
  163. return {
  164. /** @type {function(ChatSystem, Room)} */
  165. populate: function(channelContext, channel) {
  166. this.cancelHide();
  167. currentChatCtx = channelContext;
  168. currentChan = channel;
  169. _update();
  170. return this;
  171. },
  172. update: function() {
  173. this.cancelHide();
  174. _update();
  175. return this;
  176. },
  177. /** @type {function(Element)} */
  178. show: function(domParent) {
  179. this.cancelHide();
  180. domParent.appendChild(dom);
  181. dom.classList.remove(R.klass.hidden);
  182. return this;
  183. },
  184. hide: function() {
  185. this.cancelHide();
  186. dom.classList.add(R.klass.hidden);
  187. return this;
  188. },
  189. cancelHide: function() {
  190. if (hideTimeo)
  191. clearTimeout(hideTimeo);
  192. hideTimeo = null;
  193. return this;
  194. },
  195. hideDelayed: function() {
  196. if (!hideTimeo)
  197. hideTimeo = setTimeout(function() {
  198. dom.classList.add(R.klass.hidden);
  199. hideTimeo = null;
  200. }, 300);
  201. return this;
  202. },
  203. isParentOf: function(_dom) {
  204. while (_dom) {
  205. if (_dom === dom)
  206. return true;
  207. _dom = _dom.parentNode;
  208. }
  209. return false;
  210. }
  211. }
  212. })();