dom.js 14 KB

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