dom.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. ;
  280. rootDom.className = R.klass.msg.attachment.container;
  281. //Color
  282. var color = "#e3e4e6";
  283. if (attachment["color"]) {
  284. if (attachment["color"][0] === '#')
  285. color = attachment["color"][0];
  286. else if (attachment["color"] === "good")
  287. color = "#2fa44f";
  288. else if (attachment["color"] === "warning")
  289. color = "#de9e31";
  290. else if (attachment["color"] === "danger")
  291. color = "#d50200";
  292. }
  293. attachmentBlock.style.borderColor = color;
  294. attachmentBlock.className = R.klass.msg.attachment.block;
  295. //Pretext
  296. pretext.className = R.klass.msg.attachment.pretext;
  297. if (attachment["pretext"]) {
  298. pretext.innerHTML = formatText(attachment["pretext"]);
  299. } else {
  300. pretext.classList.add(R.klass.hidden);
  301. }
  302. //Title
  303. titleBlock.target = "_blank";
  304. if (attachment["title"]) {
  305. titleBlock.innerHTML = formatText(attachment["title"]);
  306. if (attachment["title_link"]) {
  307. titleBlock.href = attachment["title_link"];
  308. }
  309. titleBlock.className = R.klass.msg.attachment.title;
  310. } else {
  311. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  312. }
  313. //Author
  314. authorName.target = "_blank";
  315. authorBlock.className = R.klass.msg.author;
  316. if (attachment["author_name"]) {
  317. authorName.innerHTML = formatText(attachment["author_name"]);
  318. authorName.href = attachment["author_link"] || "";
  319. authorName.className = R.klass.msg.authorname;
  320. authorImg.className = R.klass.msg.authorAvatar;
  321. if (attachment["author_icon"]) {
  322. authorImg.src = attachment["author_icon"];
  323. authorBlock.appendChild(authorImg);
  324. }
  325. authorBlock.appendChild(authorName);
  326. }
  327. //Text
  328. textBlock.className = R.klass.msg.attachment.content;
  329. var textContent = formatText(attachment["text"] || "");
  330. if (textContent !== '') {
  331. textDom.innerHTML = textContent;
  332. textDom.className = R.klass.msg.attachment.text;
  333. } else {
  334. textDom.className = R.klass.msg.attachment.text +' ' +R.klass.hidden;
  335. }
  336. // Img (small one)
  337. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  338. if (attachment["thumb_url"]) {
  339. var img = document.createElement("img");
  340. img.src = attachment["thumb_url"];
  341. thumbImgDom.appendChild(img);
  342. attachmentBlock.classList.add(R.klass.msg.attachment.hasThumb);
  343. } else {
  344. thumbImgDom.classList.add(R.klass.hidden);
  345. }
  346. //Img (the big one)
  347. imgDom.className = R.klass.msg.attachment.img;
  348. if (attachment["image_url"])
  349. imgDom.src = attachment["image_url"];
  350. else
  351. imgDom.classList.add(R.klass.hidden);
  352. //Footer
  353. footerBlock.className = R.klass.msg.attachment.footer;
  354. if (attachment["footer"]) {
  355. var footerText = document.createElement("span")
  356. footerText.className = R.klass.msg.attachment.footerText;
  357. footerText.innerHTML = formatText(attachment["footer"]);
  358. if (attachment["footer_icon"]) {
  359. var footerIcon = document.createElement("img")
  360. footerIcon.src = attachment["footer_icon"];
  361. footerIcon.className = R.klass.msg.attachment.footerIcon;
  362. footerBlock.appendChild(footerIcon);
  363. }
  364. footerBlock.appendChild(footerText);
  365. }
  366. //Ts
  367. if (attachment["ts"]) {
  368. var footerTs = document.createElement("span")
  369. footerTs.className = R.klass.msg.ts;
  370. footerTs.innerHTML = locale.formatDate(attachment["ts"]);
  371. footerBlock.appendChild(footerTs);
  372. }
  373. // TODO Field [ {title, value, short } ]
  374. // TODO actions (button stuff)
  375. textBlock.appendChild(textDom);
  376. textBlock.appendChild(thumbImgDom);
  377. attachmentBlock.appendChild(titleBlock);
  378. attachmentBlock.appendChild(authorBlock);
  379. attachmentBlock.appendChild(textBlock);
  380. attachmentBlock.appendChild(imgDom);
  381. attachmentBlock.appendChild(footerBlock);
  382. rootDom.appendChild(pretext);
  383. rootDom.appendChild(attachmentBlock);
  384. return rootDom;
  385. }
  386. /**
  387. * @param {Chatter} user
  388. * @return {Element}
  389. **/
  390. function makeUserIsTypingDom(user) {
  391. var dom = document.createElement("li")
  392. ,userName = document.createElement("span");
  393. userName.textContent = user.name;
  394. dom.appendChild(createTypingDisplay());
  395. dom.appendChild(userName);
  396. return dom;
  397. }
  398. /**
  399. * @param {string} servicename
  400. * @return {Element}
  401. **/
  402. function createSlashAutocompleteHeader(servicename) {
  403. var lh = document.createElement("lh");
  404. lh.textContent = servicename;
  405. lh.className = R.klass.commands.header;
  406. return lh;
  407. }
  408. /**
  409. * @param {Command} cmd
  410. * @return {Element}
  411. **/
  412. function createSlashAutocompleteDom(cmd) {
  413. var li = document.createElement("li")
  414. ,name = document.createElement("span")
  415. ,usage = document.createElement("span")
  416. ,desc = document.createElement("span");
  417. name.textContent = cmd.name;
  418. usage.textContent = cmd.usage;
  419. desc.textContent = cmd.desc;
  420. li.appendChild(name);
  421. li.appendChild(usage);
  422. li.appendChild(desc);
  423. li.className = R.klass.commands.item;
  424. name.className = R.klass.commands.name;
  425. usage.className = R.klass.commands.usage;
  426. desc.className = R.klass.commands.desc;
  427. return li;
  428. }