dom.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. ,authorName = document.createElement("span")
  129. ,hover = document.createElement("ul")
  130. ,hoverReply = document.createElement("li")
  131. ,attachments = document.createElement("ul")
  132. ,reactions = document.createElement("ul")
  133. ,sender = SLACK.context.users[msg.userId];
  134. dom.id = channelId +"_" +msg.ts;
  135. dom.className = R.klass.msg.item;
  136. ts.className = R.klass.msg.ts;
  137. text.className = R.klass.msg.msg;
  138. authorName.className = R.klass.msg.authorname;
  139. hover.className = R.klass.msg.hover.container;
  140. hoverReply.className = R.klass.msg.hover.reply;
  141. ts.innerHTML = locale.formatDate(msg.ts);
  142. text.innerHTML = formatText(msg.text);
  143. authorName.textContent = sender ? sender.name : (msg.username || "?");
  144. hover.appendChild(hoverReply);
  145. if ('makeEmoji' in window) {
  146. var hoverReaction = document.createElement("li")
  147. ,domReply = window['makeEmoji']("arrow_heading_down")
  148. ,domReaction = window['makeEmoji']("smile")
  149. ,domEdit = window['makeEmoji']("pencil2")
  150. ,domRemove = window['makeEmoji']("x");
  151. hoverReaction.className = R.klass.msg.hover.reaction;
  152. if (domReaction) {
  153. hoverReaction.classList.add(R.klass.emoji.small);
  154. hoverReaction.appendChild(domReaction);
  155. } else {
  156. hoverReaction.style.backgroundImage = 'url("smile.svg")';
  157. }
  158. if (domReply) {
  159. hoverReply.classList.add(R.klass.emoji.small);
  160. hoverReply.appendChild(domReply);
  161. } else {
  162. hoverReply.style.backgroundImage = 'url("repl.svg")';
  163. }
  164. hover.appendChild(hoverReaction);
  165. if (msg.userId === SLACK.context.self.id) {
  166. var hoverEdit = document.createElement("li");
  167. hoverEdit.className = R.klass.msg.hover.edit;
  168. if (domEdit)
  169. hoverEdit.classList.add(R.klass.emoji.small);
  170. else
  171. hoverEdit.style.backgroundImage = 'url("edit.svg")';
  172. hoverEdit.appendChild(domEdit);
  173. hover.appendChild(hoverEdit);
  174. var hoverRemove = document.createElement("li");
  175. hoverRemove.className = R.klass.msg.hover.remove;
  176. if (domRemove)
  177. hoverRemove.classList.add(R.klass.emoji.small);
  178. else
  179. hoverRemove.style.backgroundImage = 'url("remove.svg")';
  180. hoverRemove.appendChild(domRemove);
  181. hover.appendChild(hoverRemove);
  182. }
  183. } else {
  184. hoverReply.style.backgroundImage = 'url("repl.svg")';
  185. if (msg.userId === SLACK.context.self.id) {
  186. var hoverEdit = document.createElement("li");
  187. hoverEdit.className = R.klass.msg.hover.edit;
  188. hoverEdit.style.backgroundImage = 'url("edit.svg")';
  189. hover.appendChild(hoverEdit);
  190. var hoverRemove = document.createElement("li")
  191. hoverRemove.className = R.klass.msg.hover.remove;
  192. hoverRemove.style.backgroundImage = 'url("remove.svg")';
  193. hover.appendChild(hoverRemove);
  194. }
  195. }
  196. if (msg instanceof NoticeMessage) {
  197. var onlyVisible = document.createElement("span");
  198. onlyVisible.className = R.klass.msg.notice;
  199. onlyVisible.textContent = locale.onlyVisible;
  200. msgBlock.appendChild(onlyVisible);
  201. }
  202. msgBlock.appendChild(authorName);
  203. msgBlock.appendChild(text);
  204. msgBlock.appendChild(ts);
  205. msgBlock.appendChild(attachments);
  206. if (msg.edited) {
  207. var edited = document.createElement("div");
  208. edited.innerHTML = locale.edited;
  209. edited.className = R.klass.msg.edited;
  210. msgBlock.appendChild(edited);
  211. }
  212. msgBlock.appendChild(reactions);
  213. msgBlock.className = R.klass.msg.content;
  214. attachments.className = R.klass.msg.attachment.list;
  215. reactions.className = R.klass.msg.reactions.container;
  216. if (skipAttachment !== true) {
  217. if (msg.reactions) for (var reaction in msg.reactions) {
  218. var reac = createReactionDom(channelId, msg.id, reaction, msg.reactions[reaction]);
  219. reac && reactions.appendChild(reac);
  220. }
  221. msg.attachments.forEach(function(attachment) {
  222. var domAttachment = createAttachmentDom(channelId, msg, attachment);
  223. if (domAttachment)
  224. attachments.appendChild(domAttachment);
  225. });
  226. }
  227. dom.appendChild(msgBlock);
  228. dom.appendChild(hover);
  229. return dom;
  230. }
  231. /**
  232. * @param {Chatter} user
  233. * @param {string} userName
  234. * @return {Element}
  235. **/
  236. function createMessageGroupDom(user, userName) {
  237. var dom = document.createElement("div")
  238. ,authorBlock = document.createElement("div")
  239. ,authorName = document.createElement("span");
  240. dom.authorImg = document.createElement("img")
  241. dom.authorImg.className = R.klass.msg.authorAvatar;
  242. authorName.className = R.klass.msg.authorname;
  243. if (user) {
  244. authorName.textContent = user.name;
  245. dom.authorImg.src = user.icons.small;
  246. } else {
  247. authorName.textContent = userName || "?";
  248. dom.authorImg.src = "";
  249. }
  250. authorBlock.appendChild(dom.authorImg);
  251. authorBlock.appendChild(authorName);
  252. authorBlock.className = R.klass.msg.author;
  253. dom.className = R.klass.msg.authorGroup;
  254. dom.appendChild(authorBlock);
  255. dom.content = document.createElement("div");
  256. dom.content.className = R.klass.msg.authorMessages;
  257. dom.appendChild(dom.content);
  258. return dom;
  259. }
  260. /**
  261. * @param {string} channelId
  262. * @param {Message} msg
  263. * @param {*} attachment
  264. * @return {Element|null}
  265. **/
  266. function createAttachmentDom(channelId, msg, attachment) {
  267. var rootDom = document.createElement("li")
  268. ,attachmentBlock = document.createElement("div")
  269. ,pretext = document.createElement("div")
  270. ,titleBlock = document.createElement("a")
  271. ,authorBlock = document.createElement("div")
  272. ,authorImg = document.createElement("img")
  273. ,authorName = document.createElement("a")
  274. ,textBlock = document.createElement("div")
  275. ,textDom = document.createElement("div")
  276. ,thumbImgDom = document.createElement("div")
  277. ,imgDom = document.createElement("img")
  278. ,footerBlock = document.createElement("div")
  279. ,footerIcon = document.createElement("img")
  280. ,footerText = document.createElement("span")
  281. ,footerTs = document.createElement("span")
  282. ;
  283. rootDom.className = R.klass.msg.attachment.container;
  284. //Color
  285. var color = "#e3e4e6";
  286. if (attachment["color"]) {
  287. if (attachment["color"][0] === '#')
  288. color = attachment["color"][0];
  289. else if (attachment["color"] === "good")
  290. color = "#2fa44f";
  291. else if (attachment["color"] === "warning")
  292. color = "#de9e31";
  293. else if (attachment["color"] === "danger")
  294. color = "#d50200";
  295. }
  296. attachmentBlock.style.borderColor = color;
  297. attachmentBlock.className = R.klass.msg.attachment.block;
  298. //Pretext
  299. pretext.className = R.klass.msg.attachment.pretext;
  300. if (attachment["pretext"]) {
  301. pretext.innerHTML = formatText(attachment["pretext"]);
  302. } else {
  303. pretext.classList.add(R.klass.hidden);
  304. }
  305. //Title
  306. titleBlock.target = "_blank";
  307. if (attachment["title"]) {
  308. titleBlock.innerHTML = formatText(attachment["title"]);
  309. if (attachment["title_link"]) {
  310. titleBlock.href = attachment["title_link"];
  311. }
  312. titleBlock.className = R.klass.msg.attachment.title;
  313. } else {
  314. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  315. }
  316. //Author
  317. authorName.target = "_blank";
  318. authorBlock.className = R.klass.msg.author;
  319. if (attachment["author_name"]) {
  320. authorName.innerHTML = formatText(attachment["author_name"]);
  321. authorName.href = attachment["author_link"] || "";
  322. authorName.className = R.klass.msg.authorname;
  323. authorImg.className = R.klass.msg.authorAvatar;
  324. if (attachment["author_icon"])
  325. authorImg.src = attachment["author_icon"];
  326. else
  327. authorImg.classList.add(R.klass.hidden);
  328. } else {
  329. authorBlock.classList.add(R.klass.hidden);
  330. }
  331. //Text
  332. textBlock.className = R.klass.msg.attachment.content;
  333. var textContent = formatText(attachment["text"] || "");
  334. if (textContent === '') {
  335. textDom.innerHTML = formatText(attachment["text"] || "");
  336. textDom.className = R.klass.msg.attachment.text;
  337. } else {
  338. textDom.className = R.klass.msg.attachment.text +' ' +R.klass.hidden;
  339. }
  340. // Img (small one)
  341. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  342. if (attachment["thumb_url"]) {
  343. var img = document.createElement("img");
  344. img.src = attachment["thumb_url"];
  345. thumbImgDom.appendChild(img);
  346. attachmentBlock.classList.add(R.klass.msg.attachment.hasThumb);
  347. } else {
  348. thumbImgDom.classList.add(R.klass.hidden);
  349. }
  350. //Img (the big one)
  351. imgDom.className = R.klass.msg.attachment.img;
  352. if (attachment["image_url"])
  353. imgDom.src = attachment["image_url"];
  354. else
  355. imgDom.classList.add(R.klass.hidden);
  356. //Footer
  357. footerBlock.className = R.klass.msg.attachment.footer;
  358. footerText.className = R.klass.msg.attachment.footerText;
  359. footerIcon.className = R.klass.msg.attachment.footerIcon;
  360. if (attachment["footer"]) {
  361. footerText.innerHTML = formatText(attachment["footer"]);
  362. if (attachment["footer_icon"])
  363. footerIcon.src = attachment["footer_icon"];
  364. else
  365. footerIcon.classList.add(R.klass.hidden);
  366. } else {
  367. footerIcon.classList.add(R.klass.hidden);
  368. footerText.classList.add(R.klass.hidden);
  369. }
  370. //Ts
  371. footerTs.className = R.klass.msg.ts;
  372. if (attachment["ts"])
  373. footerTs.innerHTML = locale.formatDate(attachment["ts"]);
  374. else
  375. footerTs.classList.add(R.klass.hidden);
  376. // TODO Field [ {title, value, short } ]
  377. // TODO actions (button stuff)
  378. authorBlock.appendChild(authorImg);
  379. authorBlock.appendChild(authorName);
  380. textBlock.appendChild(textDom);
  381. textBlock.appendChild(thumbImgDom);
  382. footerBlock.appendChild(footerIcon);
  383. footerBlock.appendChild(footerText);
  384. footerBlock.appendChild(footerTs);
  385. attachmentBlock.appendChild(titleBlock);
  386. attachmentBlock.appendChild(authorBlock);
  387. attachmentBlock.appendChild(textBlock);
  388. attachmentBlock.appendChild(imgDom);
  389. attachmentBlock.appendChild(footerBlock);
  390. rootDom.appendChild(pretext);
  391. rootDom.appendChild(attachmentBlock);
  392. return rootDom;
  393. }
  394. /**
  395. * @param {Chatter} user
  396. * @return {Element}
  397. **/
  398. function makeUserIsTypingDom(user) {
  399. var dom = document.createElement("li")
  400. ,userName = document.createElement("span");
  401. userName.textContent = user.name;
  402. dom.appendChild(createTypingDisplay());
  403. dom.appendChild(userName);
  404. return dom;
  405. }
  406. /**
  407. * @param {string} servicename
  408. * @return {Element}
  409. **/
  410. function createSlashAutocompleteHeader(servicename) {
  411. var lh = document.createElement("lh");
  412. lh.textContent = servicename;
  413. lh.className = R.klass.commands.header;
  414. return lh;
  415. }
  416. /**
  417. * @param {Command} cmd
  418. * @return {Element}
  419. **/
  420. function createSlashAutocompleteDom(cmd) {
  421. var li = document.createElement("li")
  422. ,name = document.createElement("span")
  423. ,usage = document.createElement("span")
  424. ,desc = document.createElement("span");
  425. name.textContent = cmd.name;
  426. usage.textContent = cmd.usage;
  427. desc.textContent = cmd.desc;
  428. li.appendChild(name);
  429. li.appendChild(usage);
  430. li.appendChild(desc);
  431. li.className = R.klass.commands.item;
  432. name.className = R.klass.commands.name;
  433. usage.className = R.klass.commands.usage;
  434. desc.className = R.klass.commands.desc;
  435. return li;
  436. }