dom.js 17 KB

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