1
0

dom.js 15 KB

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