dom.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. * @return {Element!}
  3. **/
  4. function createTypingDisplay() {
  5. var dom = document.createElement("span")
  6. ,dot1 = document.createElement("span")
  7. ,dot2 = document.createElement("span")
  8. ,dot3 = document.createElement("span");
  9. dom.className = R.klass.typing.container;
  10. dot1.className = R.klass.typing.dot1;
  11. dot2.className = R.klass.typing.dot2;
  12. dot3.className = R.klass.typing.dot3;
  13. dot1.textContent = dot2.textContent = dot3.textContent = '.';
  14. dom.appendChild(dot1);
  15. dom.appendChild(dot2);
  16. dom.appendChild(dot3);
  17. return dom;
  18. }
  19. /**
  20. * @param {SlackChan|SlackGroup} chan
  21. * @return {Element}
  22. **/
  23. function createChanListItem(chan) {
  24. var dom = document.createElement("li")
  25. ,link = document.createElement("a");
  26. dom.id = chan.id;
  27. link.href = '#' +chan.id;
  28. if (chan instanceof SlackGroup) {
  29. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeGroup;
  30. dom.dataset["count"] = chan.nbMembers -1;
  31. }
  32. else if (chan instanceof SlackChan)
  33. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeChannel;
  34. if (SELECTED_ROOM === chan)
  35. dom.classList.add(R.klass.selected);
  36. link.textContent = chan.name;
  37. dom.appendChild(createTypingDisplay());
  38. dom.appendChild(link);
  39. if (UNREAD_CHANS[chan.id]) {
  40. if (UNREAD_CHANS[chan.id].hl)
  41. dom.classList.add(R.klass.unreadHi);
  42. if (UNREAD_CHANS[chan.id].unread)
  43. dom.classList.add(R.klass.unread);
  44. }
  45. return dom;
  46. }
  47. /**
  48. * @param {SlackIms} ims
  49. * @return {Element}
  50. **/
  51. function createImsListItem(ims) {
  52. var dom = document.createElement("li")
  53. ,link = document.createElement("a");
  54. dom.id = ims.id;
  55. link.href = '#' +ims.id;
  56. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeDirect;
  57. link.textContent = ims.user.name;
  58. dom.appendChild(createTypingDisplay());
  59. dom.appendChild(link);
  60. if (!ims.user.presence)
  61. dom.classList.add(R.klass.presenceAway);
  62. if (SELECTED_ROOM === ims)
  63. dom.classList.add(R.klass.selected);
  64. if (UNREAD_CHANS[ims.id]) {
  65. if (UNREAD_CHANS[ims.id].hl)
  66. dom.classList.add(R.klass.unreadHi);
  67. if (UNREAD_CHANS[ims.id].unread)
  68. dom.classList.add(R.klass.unread);
  69. }
  70. return dom;
  71. }
  72. /**
  73. * @param {string} chanId
  74. * @param {string} msgId
  75. * @param {string} reaction
  76. * @param {Array.<string>} users
  77. * @return {Element|null}
  78. **/
  79. function createReactionDom(chanId, msgId, reaction, users) {
  80. var emojiDom = makeEmojiDom(reaction);
  81. if (emojiDom) {
  82. var dom = document.createElement("li")
  83. ,a = document.createElement("a")
  84. ,emojiContainer = document.createElement("span")
  85. ,userList = document.createElement("span")
  86. ,userNames = [];
  87. for (var i =0, nbUser = users.length; i < nbUser; i++) {
  88. var user = SLACK.context.getMember(users[i]);
  89. if (user)
  90. userNames.push(user.name);
  91. }
  92. userNames.sort();
  93. userList.textContent = userNames.join(", ");
  94. emojiContainer.appendChild(emojiDom);
  95. emojiContainer.className = R.klass.emoji.small;
  96. a.href = "javascript:toggleReaction('" +chanId +"', '" +msgId +"', '" +reaction +"')";
  97. a.appendChild(emojiContainer);
  98. a.appendChild(userList);
  99. dom.className = R.klass.msg.reactions.item;
  100. dom.appendChild(a);
  101. return dom;
  102. }
  103. return null;
  104. }
  105. /**
  106. * @param {string} channelId
  107. * @param {SlackMessage} msg
  108. * @param {boolean=} skipAttachment
  109. * @return {Element}
  110. **/
  111. function doCreateMessageDom(channelId, msg, skipAttachment) {
  112. var dom = document.createElement("div")
  113. ,msgBlock = document.createElement("div")
  114. ,ts = document.createElement("div")
  115. ,text = document.createElement("div")
  116. ,authorImg = document.createElement("img")
  117. ,authorName = document.createElement("span")
  118. ,hover = document.createElement("ul")
  119. ,hoverReply = document.createElement("li")
  120. ,attachments = document.createElement("ul")
  121. ,reactions = document.createElement("ul")
  122. ,sender = SLACK.context.getMember(msg.userId);
  123. dom.id = channelId +"_" +msg.ts;
  124. dom.className = R.klass.msg.item;
  125. ts.className = R.klass.msg.ts;
  126. text.className = R.klass.msg.msg;
  127. authorImg.className = R.klass.msg.authorAvatar;
  128. authorName.className = R.klass.msg.authorname;
  129. hover.className = R.klass.msg.hover.container;
  130. hoverReply.className = R.klass.msg.hover.reply;
  131. ts.innerHTML = locale.formatDate(msg.ts);
  132. text.innerHTML = formatSlackText(msg.text);
  133. authorName.textContent = sender ? sender.name : (msg.username || "?");
  134. authorImg.src = sender ? sender.icons.image_48 : "";
  135. hover.appendChild(hoverReply);
  136. if ('makeEmoji' in window) {
  137. var hoverReaction = document.createElement("li")
  138. ,domReply = window['makeEmoji']("arrow_heading_down")
  139. ,domReaction = window['makeEmoji']("smile")
  140. ,domEdit = window['makeEmoji']("pencil2")
  141. ,domRemove = window['makeEmoji']("x");
  142. hoverReaction.className = R.klass.msg.hover.reaction;
  143. if (domReaction) {
  144. hoverReaction.classList.add(R.klass.emoji.small);
  145. hoverReaction.appendChild(domReaction);
  146. } else {
  147. hoverReaction.style.backgroundImage = 'url("smile.svg")';
  148. }
  149. if (domReply) {
  150. hoverReply.classList.add(R.klass.emoji.small);
  151. hoverReply.appendChild(domReply);
  152. } else {
  153. hoverReply.style.backgroundImage = 'url("repl.svg")';
  154. }
  155. hover.appendChild(hoverReaction);
  156. if (msg.userId === SLACK.context.self.id) {
  157. var hoverEdit = document.createElement("li");
  158. hoverEdit.className = R.klass.msg.hover.edit;
  159. if (domEdit)
  160. hoverEdit.classList.add(R.klass.emoji.small);
  161. else
  162. hoverEdit.style.backgroundImage = 'url("edit.svg")';
  163. hoverEdit.appendChild(domEdit);
  164. hover.appendChild(hoverEdit);
  165. var hoverRemove = document.createElement("li");
  166. hoverRemove.className = R.klass.msg.hover.remove;
  167. if (domRemove)
  168. hoverRemove.classList.add(R.klass.emoji.small);
  169. else
  170. hoverRemove.style.backgroundImage = 'url("remove.svg")';
  171. hoverRemove.appendChild(domRemove);
  172. hover.appendChild(hoverRemove);
  173. }
  174. } else {
  175. hoverReply.style.backgroundImage = 'url("repl.svg")';
  176. if (msg.userId === SLACK.context.self.id) {
  177. var hoverEdit = document.createElement("li");
  178. hoverEdit.className = R.klass.msg.hover.edit;
  179. hoverEdit.style.backgroundImage = 'url("edit.svg")';
  180. hover.appendChild(hoverEdit);
  181. var hoverRemove = document.createElement("li")
  182. hoverRemove.className = R.klass.msg.hover.remove;
  183. hoverRemove.style.backgroundImage = 'url("remove.svg")';
  184. hover.appendChild(hoverRemove);
  185. }
  186. }
  187. dom.appendChild(authorImg);
  188. msgBlock.appendChild(authorName);
  189. msgBlock.appendChild(text);
  190. msgBlock.appendChild(ts);
  191. msgBlock.appendChild(attachments);
  192. if (msg.edited) {
  193. var edited = document.createElement("div");
  194. edited.textContent = locale.edited;
  195. edited.className = R.klass.msg.edited;
  196. msgBlock.appendChild(edited);
  197. }
  198. msgBlock.appendChild(reactions);
  199. msgBlock.className = R.klass.msg.content;
  200. attachments.className = R.klass.msg.attachment.list;
  201. reactions.className = R.klass.msg.reactions.container;
  202. if (skipAttachment !== true) {
  203. if (msg.reactions) for (var reaction in msg.reactions) {
  204. var reac = createReactionDom(channelId, msg.id, reaction, msg.reactions[reaction]);
  205. reac && reactions.appendChild(reac);
  206. }
  207. msg.attachments.forEach(function(attachment) {
  208. var domAttachment = createAttachmentDom(channelId, msg, attachment);
  209. if (domAttachment)
  210. attachments.appendChild(domAttachment);
  211. });
  212. }
  213. dom.appendChild(msgBlock);
  214. dom.appendChild(hover);
  215. return dom;
  216. }
  217. /**
  218. * @param {string} channelId
  219. * @param {SlackMessage} msg
  220. * @param {*} attachment
  221. * @return {Element|null}
  222. **/
  223. function createAttachmentDom(channelId, msg, attachment) {
  224. var rootDom = document.createElement("li")
  225. ,attachmentBlock = document.createElement("div")
  226. ,pretext = document.createElement("div")
  227. ,titleBlock = document.createElement("a")
  228. ,authorBlock = document.createElement("div")
  229. ,authorImg = document.createElement("img")
  230. ,authorName = document.createElement("a")
  231. ,textBlock = document.createElement("div")
  232. ,textDom = document.createElement("div")
  233. ,thumbImgDom = document.createElement("img")
  234. ,imgDom = document.createElement("img")
  235. ,footerBlock = document.createElement("div")
  236. ,footerIcon = document.createElement("img")
  237. ,footerText = document.createElement("span")
  238. ,footerTs = document.createElement("span")
  239. ;
  240. rootDom.className = R.klass.msg.attachment.container;
  241. //Color
  242. var color = "#e3e4e6";
  243. if (attachment["color"]) {
  244. if (attachment["color"][0] === '#')
  245. color = attachment["color"][0];
  246. else if (attachment["color"] === "good")
  247. color = "#2fa44f";
  248. else if (attachment["color"] === "warning")
  249. color = "#de9e31";
  250. else if (attachment["color"] === "danger")
  251. color = "#d50200";
  252. }
  253. attachmentBlock.style.borderColor = color;
  254. attachmentBlock.className = R.klass.msg.attachment.block;
  255. //Pretext
  256. pretext.className = R.klass.msg.attachment.pretext;
  257. if (attachment["pretext"]) {
  258. pretext.innerHTML = formatSlackText(attachment["pretext"]);
  259. } else {
  260. pretext.classList.add(R.klass.hidden);
  261. }
  262. //Title
  263. titleBlock.target = "_blank";
  264. if (attachment["title"]) {
  265. titleBlock.innerHTML = formatSlackText(attachment["title"]);
  266. if (attachment["title_link"]) {
  267. titleBlock.href = attachment["title_link"];
  268. }
  269. titleBlock.className = R.klass.msg.attachment.title;
  270. } else {
  271. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  272. }
  273. //Author
  274. authorName.target = "_blank";
  275. authorBlock.className = R.klass.msg.author;
  276. if (attachment["author_name"]) {
  277. authorName.innerHTML = formatSlackText(attachment["author_name"]);
  278. authorName.href = attachment["author_link"] || "";
  279. authorName.className = R.klass.msg.authorname;
  280. authorImg.className = R.klass.msg.authorAvatar;
  281. if (attachment["author_icon"])
  282. authorImg.src = attachment["author_icon"];
  283. else
  284. authorImg.classList.add(R.klass.hidden);
  285. } else {
  286. authorBlock.classList.add(R.klass.hidden);
  287. }
  288. //Text
  289. textDom.innerHTML = formatSlackText(attachment["text"] || "");
  290. textDom.klassName = R.klass.msg.attachment.text;
  291. // Img (small one)
  292. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  293. if (attachment["thumb_url"])
  294. thumbImgDom.src = attachment["thumb_url"];
  295. else
  296. thumbImgDom.classList.add(R.klass.hidden);
  297. //Img (the big one)
  298. imgDom.className = R.klass.msg.attachment.img;
  299. if (attachment["image_url"])
  300. imgDom.src = attachment["image_url"];
  301. else
  302. imgDom.classList.add(R.klass.hidden);
  303. //Footer
  304. footerBlock.className = R.klass.msg.attachment.footer;
  305. footerText.className = R.klass.msg.attachment.footerText;
  306. footerIcon.className = R.klass.msg.attachment.footerIcon;
  307. if (attachment["footer"]) {
  308. footerText.innerHTML = formatSlackText(attachment["footer"]);
  309. if (attachment["footer_icon"])
  310. footerIcon.src = attachment["footer_icon"];
  311. else
  312. footerIcon.classList.add(R.klass.hidden);
  313. } else {
  314. footerIcon.classList.add(R.klass.hidden);
  315. footerText.classList.add(R.klass.hidden);
  316. }
  317. //Ts
  318. footerTs.className = R.klass.msg.ts;
  319. if (attachment["ts"])
  320. footerTs.innerHTML = locale.formatDate(attachment["ts"]);
  321. else
  322. footerTs.classList.add(R.klass.hidden);
  323. // TODO Field [ {title, value, short } ]
  324. // TODO actions (button stuff)
  325. authorBlock.appendChild(authorImg);
  326. authorBlock.appendChild(authorName);
  327. textBlock.appendChild(textDom);
  328. textBlock.appendChild(thumbImgDom);
  329. footerBlock.appendChild(footerIcon);
  330. footerBlock.appendChild(footerText);
  331. footerBlock.appendChild(footerTs);
  332. attachmentBlock.appendChild(titleBlock);
  333. attachmentBlock.appendChild(authorBlock);
  334. attachmentBlock.appendChild(textBlock);
  335. attachmentBlock.appendChild(imgDom);
  336. attachmentBlock.appendChild(footerBlock);
  337. rootDom.appendChild(pretext);
  338. rootDom.appendChild(attachmentBlock);
  339. return rootDom;
  340. }
  341. /**
  342. * @param {SlackCommand} cmd
  343. * @return {Element}
  344. **/
  345. function createSlashAutocompleteDom(cmd) {
  346. var li = document.createElement("li")
  347. ,name = document.createElement("span")
  348. ,usage = document.createElement("span")
  349. ,desc = document.createElement("span");
  350. name.textContent = cmd.name;
  351. usage.textContent = cmd.usage;
  352. desc.textContent = cmd.desc;
  353. li.appendChild(name);
  354. li.appendChild(usage);
  355. li.appendChild(desc);
  356. li.className = R.klass.commands.item;
  357. name.className = R.klass.commands.name;
  358. usage.className = R.klass.commands.usage;
  359. desc.className = R.klass.commands.desc;
  360. return li;
  361. }