dom.js 14 KB

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