ui.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @param {SlackChan|SlackGroup} chan
  3. * @return {Element}
  4. **/
  5. function createChanListItem(chan) {
  6. var dom = document.createElement("li");
  7. dom.id = chan.id;
  8. if (chan.id[0] === 'D')
  9. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeDirect;
  10. else if (chan.id[0] === 'G')
  11. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeGroup;
  12. else if (chan.id[0] === 'C')
  13. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeChannel;
  14. dom.textContent = chan.name;
  15. return dom;
  16. }
  17. /**
  18. * @param {SlackIms} ims
  19. * @return {Element}
  20. **/
  21. function createImsListItem(ims) {
  22. var dom = document.createElement("li");
  23. dom.id = ims.id;
  24. dom.className = R.klass.chatList.entry;
  25. dom.textContent = ims.user.name;
  26. return dom;
  27. }
  28. function onContextUpdated() {
  29. var chanListFram = document.createDocumentFragment();
  30. var sortedChans = SLACK.context.self ? Object.keys(SLACK.context.self.channels) : [];
  31. sortedChans.sort(function(a, b) {
  32. if (a[0] !== b[0]) {
  33. return a[0] - b[0];
  34. }
  35. return (SLACK.context.channels[a] || SLACK.context.groups[a]).name.localeCompare((SLACK.context.channels[b] || SLACK.context.groups[b]).name);
  36. });
  37. sortedChans.forEach(function(chanId) {
  38. var chan = SLACK.context.channels[chanId] || SLACK.context.groups[chanId]
  39. ,chanListItem = createChanListItem(chan);
  40. if (chanListItem) {
  41. chanListFram.appendChild(chanListItem);
  42. }
  43. });
  44. var sortedUsers = SLACK.context.users ? Object.keys(SLACK.context.users) : [];
  45. sortedUsers.sort(function(a, b) {
  46. return SLACK.context.users[a].name.localeCompare(SLACK.context.users[b].name);
  47. });
  48. sortedUsers.forEach(function(userId) {
  49. var ims = SLACK.context.users[userId].ims
  50. ,imsListItem = createImsListItem(ims);
  51. if (imsListItem) {
  52. chanListFram.appendChild(imsListItem);
  53. }
  54. });
  55. document.getElementById(R.id.chanList).textContent = "";
  56. document.getElementById(R.id.chanList).appendChild(chanListFram);
  57. }
  58. function onRoomSelected() {
  59. var name = SELECTED_ROOM.name || (SELECTED_ROOM.user ? SELECTED_ROOM.user.name : undefined);
  60. if (!name) {
  61. var members = [];
  62. for (var i in SELECTED_ROOM.members) {
  63. members.push(SELECTED_ROOM.members[i].name);
  64. }
  65. name = members.join(", ");
  66. }
  67. var roomLi = document.getElementById(SELECTED_ROOM.id);
  68. roomLi.classList.remove(R.klass.unread);
  69. roomLi.classList.remove(R.klass.unreadHi);
  70. document.getElementById(R.id.currentRoom.title).textContent = name;
  71. onRoomUpdated();
  72. }
  73. function createMessageDom(channelId, msg) {
  74. var dom = document.createElement("div")
  75. ,ts = document.createElement("div")
  76. ,text = document.createElement("div")
  77. ,author = document.createElement("div")
  78. ,authorImg = document.createElement("img")
  79. ,authorName = document.createElement("span")
  80. ,hover = document.createElement("ul")
  81. ,hoverReply = document.createElement("li")
  82. ,sender = msg.raw["user"] ?
  83. SLACK.context.users[msg.raw["user"]] :
  84. SLACK.context.bots[msg.raw["bot_id"]];
  85. dom.id = channelId +"_" +msg.ts;
  86. dom.className = R.klass.msg.item;
  87. ts.className = R.klass.msg.ts;
  88. text.className = R.klass.msg.msg;
  89. author.className = R.klass.msg.author;
  90. authorImg.className = R.klass.msg.authorAvatar;
  91. authorName.className = R.klass.msg.authorname;
  92. hover.className = R.klass.msg.hover.container;
  93. hoverReply.className = R.klass.msg.hover.reply;
  94. ts.textContent = (new Date(msg.ts * 1000)).toLocaleTimeString();
  95. text.textContent = msg.raw["text"];
  96. authorName.textContent = sender ? sender.name : (msg.raw["username"] || "?");
  97. authorImg.src = sender ? sender.icons.image_48 : "";
  98. author.appendChild(authorImg);
  99. author.appendChild(authorName);
  100. hover.appendChild(hoverReply);
  101. dom.appendChild(author);
  102. dom.appendChild(text);
  103. dom.appendChild(ts);
  104. dom.appendChild(hover);
  105. return dom;
  106. }
  107. /**
  108. * @param {SlackChan|SlackGroup|SlackIms} chan
  109. * @param {Array.<*>} msg
  110. **/
  111. function onMsgReceived(chan, msg) {
  112. chan && document.getElementById(chan.id).classList.add(R.klass.unread);
  113. }
  114. function onRoomUpdated() {
  115. var chatFrag = document.createDocumentFragment()
  116. ,currentRoomId = SELECTED_ROOM.id;
  117. document.getElementById(R.id.currentRoom.content).textContent = "";
  118. if (SLACK.history[currentRoomId])
  119. SLACK.history[currentRoomId].messages.forEach(function(msg) {
  120. chatFrag.appendChild(createMessageDom(currentRoomId, msg));
  121. });
  122. var content = document.getElementById(R.id.currentRoom.content);
  123. content.appendChild(chatFrag);
  124. //TODO scroll lock
  125. content.scrollTop = content.scrollHeight -content.clientHeight;
  126. }
  127. function onChanClick(e) {
  128. while (e.target !== e.currentTarget && e.target) {
  129. if (e.target.classList.contains(R.klass.chatList.entry)) {
  130. var room = (SLACK.context.channels[e.target.id] ||
  131. SLACK.context.ims[e.target.id] ||
  132. SLACK.context.groups[e.target.id]);
  133. if (room && room !== SELECTED_ROOM) {
  134. selectRoom(room);
  135. }
  136. return;
  137. }
  138. e.target = e.target.parentElement;
  139. }
  140. }
  141. function replyTo(msg) {
  142. console.log("Replying to ", msg);
  143. }
  144. function chatClickDelegate(e) {
  145. var target = e.target
  146. ,getMessageId = function(e, target) {
  147. target = target || e.target;
  148. while (target !== e.currentTarget && target) {
  149. if (target.classList.contains(R.klass.msg.item)) {
  150. return target.id;
  151. }
  152. target = target.parentElement;
  153. }
  154. };
  155. while (target !== e.currentTarget && target) {
  156. if (target.classList.contains(R.klass.msg.hover.container)) {
  157. return;
  158. } else if (target.classList.contains(R.klass.msg.hover.reply)) {
  159. var messageId = getMessageId(e, target);
  160. if (messageId) {
  161. messageId = parseFloat(messageId.split("_")[1]);
  162. var history = SLACK.history[SELECTED_ROOM.id].messages;
  163. for (var i =0, histLen =history.length; i < histLen && history[i].ts <= messageId; i++) {
  164. if (history[i].ts === messageId) {
  165. replyTo(history[i]);
  166. return;
  167. }
  168. }
  169. }
  170. return;
  171. }
  172. target = target.parentElement;
  173. }
  174. }
  175. document.addEventListener('DOMContentLoaded', function() {
  176. document.getElementById(R.id.chatList).addEventListener("click", onChanClick);
  177. document.getElementById(R.id.currentRoom.content).addEventListener("click", chatClickDelegate);
  178. document.getElementById(R.id.message.form).addEventListener("submit", function(e) {
  179. e.preventDefault();
  180. var input =document.getElementById(R.id.message.input);
  181. if (SELECTED_ROOM && input.value) {
  182. sendMsg(SELECTED_ROOM, input.value);
  183. input.value = "";
  184. }
  185. return false;
  186. });
  187. startPolling();
  188. });