dom.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 {Room} 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.isPrivate) {
  29. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typePrivate;
  30. dom.dataset["count"] = Object.keys((chan.users) || {}).length;
  31. } else {
  32. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeChannel;
  33. }
  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 (chan.lastMsg > chan.lastRead) {
  40. dom.classList.add(R.klass.unread);
  41. if (HIGHLIGHTED_CHANS.indexOf(chan) >= 0)
  42. dom.classList.add(R.klass.unreadHi);
  43. }
  44. return dom;
  45. }
  46. /**
  47. * @param {PrivateMessageRoom} ims
  48. * @return {Element}
  49. **/
  50. function createImsListItem(ims) {
  51. var dom = document.createElement("li")
  52. ,link = document.createElement("a");
  53. dom.id = ims.id;
  54. link.href = '#' +ims.id;
  55. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeDirect;
  56. link.textContent = ims.user.name;
  57. dom.appendChild(createTypingDisplay());
  58. dom.appendChild(link);
  59. if (!ims.user.presence)
  60. dom.classList.add(R.klass.presenceAway);
  61. if (SELECTED_ROOM === ims)
  62. dom.classList.add(R.klass.selected);
  63. if (ims.lastMsg > ims.lastRead) {
  64. dom.classList.add(R.klass.unread);
  65. if (HIGHLIGHTED_CHANS.indexOf(ims) >= 0)
  66. dom.classList.add(R.klass.unreadHi);
  67. }
  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.users[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. } else {
  101. console.warn("Reaction id not found: " +reaction);
  102. }
  103. return null;
  104. }
  105. /**
  106. * @param {string} channelId
  107. * @param {Message} 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.users[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 = formatText(msg.text);
  133. authorName.textContent = sender ? sender.name : (msg.username || "?");
  134. authorImg.src = sender ? sender.icons.small : "";
  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. if (msg instanceof NoticeMessage) {
  189. var onlyVisible = document.createElement("span");
  190. onlyVisible.className = R.klass.msg.notice;
  191. onlyVisible.textContent = locale.onlyVisible;
  192. msgBlock.appendChild(onlyVisible);
  193. }
  194. msgBlock.appendChild(authorName);
  195. msgBlock.appendChild(text);
  196. msgBlock.appendChild(ts);
  197. msgBlock.appendChild(attachments);
  198. if (msg.edited) {
  199. var edited = document.createElement("div");
  200. edited.textContent = locale.edited;
  201. edited.className = R.klass.msg.edited;
  202. msgBlock.appendChild(edited);
  203. }
  204. msgBlock.appendChild(reactions);
  205. msgBlock.className = R.klass.msg.content;
  206. attachments.className = R.klass.msg.attachment.list;
  207. reactions.className = R.klass.msg.reactions.container;
  208. if (skipAttachment !== true) {
  209. if (msg.reactions) for (var reaction in msg.reactions) {
  210. var reac = createReactionDom(channelId, msg.id, reaction, msg.reactions[reaction]);
  211. reac && reactions.appendChild(reac);
  212. }
  213. msg.attachments.forEach(function(attachment) {
  214. var domAttachment = createAttachmentDom(channelId, msg, attachment);
  215. if (domAttachment)
  216. attachments.appendChild(domAttachment);
  217. });
  218. }
  219. dom.appendChild(msgBlock);
  220. dom.appendChild(hover);
  221. return dom;
  222. }
  223. /**
  224. * @param {string} channelId
  225. * @param {Message} msg
  226. * @param {*} attachment
  227. * @return {Element|null}
  228. **/
  229. function createAttachmentDom(channelId, msg, attachment) {
  230. var rootDom = document.createElement("li")
  231. ,attachmentBlock = document.createElement("div")
  232. ,pretext = document.createElement("div")
  233. ,titleBlock = document.createElement("a")
  234. ,authorBlock = document.createElement("div")
  235. ,authorImg = document.createElement("img")
  236. ,authorName = document.createElement("a")
  237. ,textBlock = document.createElement("div")
  238. ,textDom = document.createElement("div")
  239. ,thumbImgDom = document.createElement("img")
  240. ,imgDom = document.createElement("img")
  241. ,footerBlock = document.createElement("div")
  242. ,footerIcon = document.createElement("img")
  243. ,footerText = document.createElement("span")
  244. ,footerTs = document.createElement("span")
  245. ;
  246. rootDom.className = R.klass.msg.attachment.container;
  247. //Color
  248. var color = "#e3e4e6";
  249. if (attachment["color"]) {
  250. if (attachment["color"][0] === '#')
  251. color = attachment["color"][0];
  252. else if (attachment["color"] === "good")
  253. color = "#2fa44f";
  254. else if (attachment["color"] === "warning")
  255. color = "#de9e31";
  256. else if (attachment["color"] === "danger")
  257. color = "#d50200";
  258. }
  259. attachmentBlock.style.borderColor = color;
  260. attachmentBlock.className = R.klass.msg.attachment.block;
  261. //Pretext
  262. pretext.className = R.klass.msg.attachment.pretext;
  263. if (attachment["pretext"]) {
  264. pretext.innerHTML = formatText(attachment["pretext"]);
  265. } else {
  266. pretext.classList.add(R.klass.hidden);
  267. }
  268. //Title
  269. titleBlock.target = "_blank";
  270. if (attachment["title"]) {
  271. titleBlock.innerHTML = formatText(attachment["title"]);
  272. if (attachment["title_link"]) {
  273. titleBlock.href = attachment["title_link"];
  274. }
  275. titleBlock.className = R.klass.msg.attachment.title;
  276. } else {
  277. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  278. }
  279. //Author
  280. authorName.target = "_blank";
  281. authorBlock.className = R.klass.msg.author;
  282. if (attachment["author_name"]) {
  283. authorName.innerHTML = formatText(attachment["author_name"]);
  284. authorName.href = attachment["author_link"] || "";
  285. authorName.className = R.klass.msg.authorname;
  286. authorImg.className = R.klass.msg.authorAvatar;
  287. if (attachment["author_icon"])
  288. authorImg.src = attachment["author_icon"];
  289. else
  290. authorImg.classList.add(R.klass.hidden);
  291. } else {
  292. authorBlock.classList.add(R.klass.hidden);
  293. }
  294. //Text
  295. textDom.innerHTML = formatText(attachment["text"] || "");
  296. textDom.klassName = R.klass.msg.attachment.text;
  297. // Img (small one)
  298. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  299. if (attachment["thumb_url"])
  300. thumbImgDom.src = attachment["thumb_url"];
  301. else
  302. thumbImgDom.classList.add(R.klass.hidden);
  303. //Img (the big one)
  304. imgDom.className = R.klass.msg.attachment.img;
  305. if (attachment["image_url"])
  306. imgDom.src = attachment["image_url"];
  307. else
  308. imgDom.classList.add(R.klass.hidden);
  309. //Footer
  310. footerBlock.className = R.klass.msg.attachment.footer;
  311. footerText.className = R.klass.msg.attachment.footerText;
  312. footerIcon.className = R.klass.msg.attachment.footerIcon;
  313. if (attachment["footer"]) {
  314. footerText.innerHTML = formatText(attachment["footer"]);
  315. if (attachment["footer_icon"])
  316. footerIcon.src = attachment["footer_icon"];
  317. else
  318. footerIcon.classList.add(R.klass.hidden);
  319. } else {
  320. footerIcon.classList.add(R.klass.hidden);
  321. footerText.classList.add(R.klass.hidden);
  322. }
  323. //Ts
  324. footerTs.className = R.klass.msg.ts;
  325. if (attachment["ts"])
  326. footerTs.innerHTML = locale.formatDate(attachment["ts"]);
  327. else
  328. footerTs.classList.add(R.klass.hidden);
  329. // TODO Field [ {title, value, short } ]
  330. // TODO actions (button stuff)
  331. authorBlock.appendChild(authorImg);
  332. authorBlock.appendChild(authorName);
  333. textBlock.appendChild(textDom);
  334. textBlock.appendChild(thumbImgDom);
  335. footerBlock.appendChild(footerIcon);
  336. footerBlock.appendChild(footerText);
  337. footerBlock.appendChild(footerTs);
  338. attachmentBlock.appendChild(titleBlock);
  339. attachmentBlock.appendChild(authorBlock);
  340. attachmentBlock.appendChild(textBlock);
  341. attachmentBlock.appendChild(imgDom);
  342. attachmentBlock.appendChild(footerBlock);
  343. rootDom.appendChild(pretext);
  344. rootDom.appendChild(attachmentBlock);
  345. return rootDom;
  346. }
  347. /**
  348. * @param {string} servicename
  349. * @return {Element}
  350. **/
  351. function createSlashAutocompleteHeader(servicename) {
  352. var lh = document.createElement("lh");
  353. lh.textContent = servicename;
  354. lh.className = R.klass.commands.header;
  355. return lh;
  356. }
  357. /**
  358. * @param {Command} cmd
  359. * @return {Element}
  360. **/
  361. function createSlashAutocompleteDom(cmd) {
  362. var li = document.createElement("li")
  363. ,name = document.createElement("span")
  364. ,usage = document.createElement("span")
  365. ,desc = document.createElement("span");
  366. name.textContent = cmd.name;
  367. usage.textContent = cmd.usage;
  368. desc.textContent = cmd.desc;
  369. li.appendChild(name);
  370. li.appendChild(usage);
  371. li.appendChild(desc);
  372. li.className = R.klass.commands.item;
  373. name.className = R.klass.commands.name;
  374. usage.className = R.klass.commands.usage;
  375. desc.className = R.klass.commands.desc;
  376. return li;
  377. }