dom.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. for (var i =0, nbAttachments = msg.attachments.length; i < nbAttachments; i++) {
  222. var attachment = msg.attachments[i];
  223. if (attachment) {
  224. var domAttachment = createAttachmentDom(channelId, msg, attachment, i);
  225. if (domAttachment)
  226. attachments.appendChild(domAttachment);
  227. }
  228. }
  229. }
  230. dom.appendChild(msgBlock);
  231. dom.appendChild(hover);
  232. return dom;
  233. }
  234. /**
  235. * @param {Chatter} user
  236. * @param {string} userName
  237. * @return {Element}
  238. **/
  239. function createMessageGroupDom(user, userName) {
  240. var dom = document.createElement("div")
  241. ,authorBlock = document.createElement("div")
  242. ,authorName = document.createElement("span");
  243. dom.authorImg = document.createElement("img")
  244. dom.authorImg.className = R.klass.msg.authorAvatar;
  245. authorName.className = R.klass.msg.authorname;
  246. if (user) {
  247. authorName.textContent = user.name;
  248. dom.authorImg.src = user.icons.small;
  249. } else {
  250. authorName.textContent = userName || "?";
  251. dom.authorImg.src = "";
  252. }
  253. authorBlock.appendChild(dom.authorImg);
  254. authorBlock.appendChild(authorName);
  255. authorBlock.className = R.klass.msg.author;
  256. dom.className = R.klass.msg.authorGroup;
  257. dom.appendChild(authorBlock);
  258. dom.content = document.createElement("div");
  259. dom.content.className = R.klass.msg.authorMessages;
  260. dom.appendChild(dom.content);
  261. return dom;
  262. }
  263. /**
  264. * @param {string} channelId
  265. * @param {Message} msg
  266. * @param {*} attachment
  267. * @param {number} attachmentIndex
  268. * @return {Element|null}
  269. **/
  270. function createAttachmentDom(channelId, msg, attachment, attachmentIndex) {
  271. var rootDom = document.createElement("li")
  272. ,attachmentBlock = document.createElement("div")
  273. ,pretext = document.createElement("div")
  274. ,titleBlock = document.createElement("a")
  275. ,authorBlock = document.createElement("div")
  276. ,authorImg = document.createElement("img")
  277. ,authorName = document.createElement("a")
  278. ,textBlock = document.createElement("div")
  279. ,textDom = document.createElement("div")
  280. ,thumbImgDom = document.createElement("div")
  281. ,imgDom = document.createElement("img")
  282. ,footerBlock = document.createElement("div")
  283. ;
  284. rootDom.className = R.klass.msg.attachment.container;
  285. //Color
  286. var color = "#e3e4e6";
  287. if (attachment["color"]) {
  288. if (attachment["color"][0] === '#')
  289. color = attachment["color"][0];
  290. else if (attachment["color"] === "good")
  291. color = "#2fa44f";
  292. else if (attachment["color"] === "warning")
  293. color = "#de9e31";
  294. else if (attachment["color"] === "danger")
  295. color = "#d50200";
  296. }
  297. attachmentBlock.style.borderColor = color;
  298. attachmentBlock.className = R.klass.msg.attachment.block;
  299. //Pretext
  300. pretext.className = R.klass.msg.attachment.pretext;
  301. if (attachment["pretext"]) {
  302. pretext.innerHTML = formatText(attachment["pretext"]);
  303. } else {
  304. pretext.classList.add(R.klass.hidden);
  305. }
  306. //Title
  307. titleBlock.target = "_blank";
  308. if (attachment["title"]) {
  309. titleBlock.innerHTML = formatText(attachment["title"]);
  310. if (attachment["title_link"]) {
  311. titleBlock.href = attachment["title_link"];
  312. }
  313. titleBlock.className = R.klass.msg.attachment.title;
  314. } else {
  315. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  316. }
  317. //Author
  318. authorName.target = "_blank";
  319. authorBlock.className = R.klass.msg.author;
  320. if (attachment["author_name"]) {
  321. authorName.innerHTML = formatText(attachment["author_name"]);
  322. authorName.href = attachment["author_link"] || "";
  323. authorName.className = R.klass.msg.authorname;
  324. authorImg.className = R.klass.msg.authorAvatar;
  325. if (attachment["author_icon"]) {
  326. authorImg.src = attachment["author_icon"];
  327. authorBlock.appendChild(authorImg);
  328. }
  329. authorBlock.appendChild(authorName);
  330. }
  331. //Text
  332. textBlock.className = R.klass.msg.attachment.content;
  333. var textContent = formatText(attachment["text"] || "");
  334. textDom.className = R.klass.msg.attachment.text;
  335. if (textContent && textContent != "") {
  336. textDom.innerHTML = textContent;
  337. } else {
  338. textDom.classList.add(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. if (attachment["video_html"])
  348. thumbImgDom.dataset.video = attachment["video_html"];
  349. } else {
  350. thumbImgDom.classList.add(R.klass.hidden);
  351. }
  352. //Img (the big one)
  353. imgDom.className = R.klass.msg.attachment.img;
  354. if (attachment["image_url"])
  355. imgDom.src = attachment["image_url"];
  356. else
  357. imgDom.classList.add(R.klass.hidden);
  358. //Footer
  359. footerBlock.className = R.klass.msg.attachment.footer;
  360. if (attachment["footer"]) {
  361. var footerText = document.createElement("span")
  362. footerText.className = R.klass.msg.attachment.footerText;
  363. footerText.innerHTML = formatText(attachment["footer"]);
  364. if (attachment["footer_icon"]) {
  365. var footerIcon = document.createElement("img")
  366. footerIcon.src = attachment["footer_icon"];
  367. footerIcon.className = R.klass.msg.attachment.footerIcon;
  368. footerBlock.appendChild(footerIcon);
  369. }
  370. footerBlock.appendChild(footerText);
  371. }
  372. //Ts
  373. if (attachment["ts"]) {
  374. var footerTs = document.createElement("span")
  375. footerTs.className = R.klass.msg.ts;
  376. footerTs.innerHTML = locale.formatDate(attachment["ts"]);
  377. footerBlock.appendChild(footerTs);
  378. }
  379. textBlock.appendChild(textDom);
  380. textBlock.appendChild(thumbImgDom);
  381. attachmentBlock.appendChild(titleBlock);
  382. attachmentBlock.appendChild(authorBlock);
  383. attachmentBlock.appendChild(textBlock);
  384. attachmentBlock.appendChild(imgDom);
  385. if (attachment["fields"] && attachment["fields"].length) {
  386. var fieldsContainer = document.createElement("ul");
  387. attachmentBlock.appendChild(fieldsContainer);
  388. fieldsContainer.className = R.klass.msg.attachment.field.container;
  389. attachment["fields"].forEach(function(fieldData) {
  390. var fieldDom = createFieldDom(fieldData["title"] || "", fieldData["value"] || "", !!fieldData["short"]);
  391. if (fieldDom) {
  392. fieldsContainer.appendChild(fieldDom);
  393. }
  394. });
  395. }
  396. if (attachment["actions"] && attachment["actions"].length) {
  397. var buttons;
  398. buttons = document.createElement("ul");
  399. buttons.className = R.klass.msg.attachment.actions;
  400. attachmentBlock.appendChild(buttons);
  401. for (var i =0, nbAttachments = attachment["actions"].length; i < nbAttachments; i++) {
  402. var action = attachment["actions"][i];
  403. if (action) {
  404. var button = createActionButtonDom(attachmentIndex, i, action);
  405. if (button) {
  406. buttons.appendChild(button);
  407. }
  408. }
  409. }
  410. }
  411. attachmentBlock.appendChild(footerBlock);
  412. rootDom.appendChild(pretext);
  413. rootDom.appendChild(attachmentBlock);
  414. return rootDom;
  415. }
  416. /**
  417. * @param {string} title
  418. * @param {string} text
  419. * @param {boolean} isShort
  420. * @return {Element}
  421. **/
  422. function createFieldDom(title, text, isShort) {
  423. var fieldDom = document.createElement("li")
  424. ,titleDom = document.createElement("div")
  425. ,textDom = document.createElement("div");
  426. fieldDom.className = R.klass.msg.attachment.field.item;
  427. if (!isShort) {
  428. fieldDom.classList.add(R.klass.msg.attachment.field.longField);
  429. }
  430. titleDom.className = R.klass.msg.attachment.field.title;
  431. titleDom.textContent = title;
  432. textDom.className = R.klass.msg.attachment.field.text;
  433. textDom.innerHTML = formatText(text);
  434. fieldDom.appendChild(titleDom);
  435. fieldDom.appendChild(textDom);
  436. return fieldDom;
  437. }
  438. /**
  439. * @param {number} attachmentIndex
  440. * @param {number} actionIndex
  441. * @param {*} action
  442. * @return {Element}
  443. **/
  444. function createActionButtonDom(attachmentIndex, actionIndex, action) {
  445. var li = document.createElement("li");
  446. li.textContent = action["text"];
  447. li.dataset.attachmentIndex = attachmentIndex;
  448. li.dataset.actionIndex = actionIndex;
  449. li.className = R.klass.msg.attachment.actionItem;
  450. return li;
  451. }
  452. /**
  453. * @param {Chatter} user
  454. * @return {Element}
  455. **/
  456. function makeUserIsTypingDom(user) {
  457. var dom = document.createElement("li")
  458. ,userName = document.createElement("span");
  459. userName.textContent = user.name;
  460. dom.appendChild(createTypingDisplay());
  461. dom.appendChild(userName);
  462. return dom;
  463. }
  464. /**
  465. * @param {string} servicename
  466. * @return {Element}
  467. **/
  468. function createSlashAutocompleteHeader(servicename) {
  469. var lh = document.createElement("lh");
  470. lh.textContent = servicename;
  471. lh.className = R.klass.commands.header;
  472. return lh;
  473. }
  474. /**
  475. * @param {Command} cmd
  476. * @return {Element}
  477. **/
  478. function createSlashAutocompleteDom(cmd) {
  479. var li = document.createElement("li")
  480. ,name = document.createElement("span")
  481. ,usage = document.createElement("span")
  482. ,desc = document.createElement("span");
  483. name.textContent = cmd.name;
  484. usage.textContent = cmd.usage;
  485. desc.textContent = cmd.desc;
  486. li.appendChild(name);
  487. li.appendChild(usage);
  488. li.appendChild(desc);
  489. li.className = R.klass.commands.item;
  490. name.className = R.klass.commands.name;
  491. usage.className = R.klass.commands.usage;
  492. desc.className = R.klass.commands.desc;
  493. return li;
  494. }