1
0

dom.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. * @param {string|undefined} alternateChanName
  22. * @return {Element}
  23. **/
  24. function createChanListItem(chan, alternateChanName) {
  25. var dom = document.createElement("li")
  26. ,link = document.createElement("a")
  27. ,linkText = document.createElement("span")
  28. ,info = document.createElement("a");
  29. dom.id = "room_" +chan.id;
  30. link.href = '#' +chan.id;
  31. if (chan.isPrivate) {
  32. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typePrivate;
  33. dom.dataset["count"] = Object.keys((chan.users) || {}).length;
  34. } else {
  35. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeChannel;
  36. }
  37. if (SELECTED_ROOM === chan)
  38. dom.classList.add(R.klass.selected);
  39. linkText.className = R.klass.chatList.roomInfo.name;
  40. linkText.textContent = alternateChanName || chan.name;
  41. info.textContent = "i";
  42. info.className = R.klass.chatList.roomInfo.info;
  43. link.appendChild(linkText);
  44. link.appendChild(info);
  45. dom.appendChild(createTypingDisplay());
  46. dom.appendChild(link);
  47. if (chan.lastMsg !== chan.lastRead && chan.lastMsg !== undefined) {
  48. dom.classList.add(R.klass.unread);
  49. if (HIGHLIGHTED_CHANS.indexOf(chan) >= 0)
  50. dom.classList.add(R.klass.unreadHi);
  51. }
  52. return dom;
  53. }
  54. /** @type {function(string):Element} */
  55. var createChanListHeader = (function() {
  56. var cache = {};
  57. return function(title) {
  58. var dom = cache[title];
  59. if (!dom) {
  60. dom = cache[title] = document.createElement("header");
  61. dom.textContent = title;
  62. }
  63. return dom;
  64. };
  65. })();
  66. /**
  67. * @param {PrivateMessageRoom} ims
  68. * @param {string|undefined} alternateChanName
  69. * @return {Element}
  70. **/
  71. function createImsListItem(ims, alternateChanName) {
  72. var dom = document.createElement("li")
  73. ,link = document.createElement("a")
  74. ,linkText = document.createElement("span")
  75. ,info = document.createElement("a");
  76. dom.id = "room_" +ims.id;
  77. link.href = '#' +ims.id;
  78. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeDirect +" " +R.klass.presenceIndicator;
  79. linkText.textContent = alternateChanName || ims.user.getName();
  80. linkText.className = R.klass.chatList.roomInfo.name;
  81. info.textContent = "i";
  82. info.className = R.klass.chatList.roomInfo.info;
  83. link.appendChild(linkText);
  84. link.appendChild(info);
  85. dom.appendChild(createTypingDisplay());
  86. dom.appendChild(link);
  87. if (!ims.user.presence)
  88. dom.classList.add(R.klass.presenceAway);
  89. if (SELECTED_ROOM === ims)
  90. dom.classList.add(R.klass.selected);
  91. if (ims.lastMsg !== ims.lastRead && ims.lastMsg !== undefined) {
  92. dom.classList.add(R.klass.unread);
  93. dom.classList.add(R.klass.unreadHi); // Always HI on private message
  94. }
  95. return dom;
  96. }
  97. /**
  98. * Try to resolve emoji from customized context
  99. * @param {string} emoji
  100. * @return {Element|string}
  101. **/
  102. function tryGetCustomEmoji(emoji) {
  103. var loop = {},
  104. emojiName = emoji;
  105. if (SELECTED_CONTEXT) {
  106. var ctx = SELECTED_CONTEXT.getChatContext();
  107. while (!loop[emoji]) {
  108. var emojisrc= ctx.emojis.data[emoji];
  109. if (emojisrc) {
  110. if (emojisrc.substr(0, 6) == "alias:") {
  111. loop[emoji] = true;
  112. emoji = emojisrc.substr(6);
  113. } else {
  114. var dom = document.createElement("span");
  115. dom.className = R.klass.emoji.custom +' ' +R.klass.emoji.emoji;
  116. dom.style.backgroundImage = "url('" +emojisrc +"')";
  117. dom.textContent = ':' +emojiName +':';
  118. dom.title = emojiName;
  119. return dom;
  120. }
  121. } else {
  122. return emoji; // Emoji not found, fallback to std emoji
  123. }
  124. }
  125. }
  126. return emojiName; //loop detected, return first emoji
  127. }
  128. function makeEmojiDom(emojiCode) {
  129. if ("makeEmoji" in window) {
  130. var emoji = tryGetCustomEmoji(emojiCode);
  131. if (typeof emoji === "string")
  132. emoji = window["makeEmoji"](emoji);
  133. return typeof emoji === "string" ? null : emoji;
  134. }
  135. return null;
  136. }
  137. /**
  138. * @param {string} chanId
  139. * @param {string} msgId
  140. * @param {string} reaction
  141. * @param {Array.<string>} users
  142. * @return {Element|null}
  143. **/
  144. function createReactionDom(chanId, msgId, reaction, users) {
  145. var emojiDom = makeEmojiDom(reaction);
  146. if (emojiDom) {
  147. var dom = document.createElement("li")
  148. ,a = document.createElement("a")
  149. ,emojiContainer = document.createElement("span")
  150. ,userList = document.createElement("span")
  151. ,userNames = [];
  152. for (var i =0, nbUser = users.length; i < nbUser; i++) {
  153. var user = DATA.context.getUser(users[i]);
  154. if (user)
  155. userNames.push(user.getName());
  156. }
  157. userNames.sort();
  158. userList.textContent = userNames.join(", ");
  159. emojiContainer.appendChild(emojiDom);
  160. emojiContainer.className = R.klass.emoji.small;
  161. a.href = "javascript:toggleReaction('" +chanId +"', '" +msgId +"', '" +reaction +"')";
  162. a.appendChild(emojiContainer);
  163. a.appendChild(userList);
  164. dom.className = R.klass.msg.reactions.item;
  165. dom.appendChild(a);
  166. return dom;
  167. } else {
  168. console.warn("Reaction id not found: " +reaction);
  169. }
  170. return null;
  171. }
  172. /**
  173. * @param {Element} hover
  174. * @param {UiMessage|UiMeMessage|UiNoticeMessage} msg
  175. **/
  176. function addHoverButtons(hover, msg) {
  177. var capacities = msg.context.getChatContext().capacities,
  178. isMe = DATA.context.isMe(msg.userId);
  179. if (capacities["replyToMsg"]) {
  180. var hoverReply = document.createElement("li");
  181. hoverReply.className = R.klass.msg.hover.reply;
  182. hoverReply.style.backgroundImage = 'url("repl.svg")';
  183. hover.appendChild(hoverReply);
  184. }
  185. if (capacities["reactMsg"]) {
  186. var hoverReaction = document.createElement("li");
  187. hoverReaction.className = R.klass.msg.hover.reaction;
  188. hoverReaction.style.backgroundImage = 'url("smile.svg")';
  189. hover.appendChild(hoverReaction);
  190. }
  191. if (isMe && capacities["editMsg"] || capacities["editOtherMsg"]) {
  192. var hoverEdit = document.createElement("li");
  193. hoverEdit.className = R.klass.msg.hover.edit;
  194. hoverEdit.style.backgroundImage = 'url("edit.svg")';
  195. hover.appendChild(hoverEdit);
  196. }
  197. if (capacities["starMsg"]) {
  198. hover.hoverStar = document.createElement("li")
  199. hover.hoverStar.className = R.klass.msg.hover.star;
  200. hover.appendChild(hover.hoverStar);
  201. }
  202. if (capacities["pinMsg"]) {
  203. var hoverPin = document.createElement("li")
  204. hoverPin.className = R.klass.msg.hover.pin;
  205. hover.appendChild(hoverPin);
  206. hoverPin.style.backgroundImage = 'url("pin.svg")';
  207. }
  208. if (isMe && capacities["removeMsg"] || capacities["moderate"]) {
  209. var hoverRemove = document.createElement("li")
  210. hoverRemove.className = R.klass.msg.hover.remove;
  211. hoverRemove.style.backgroundImage = 'url("remove.svg")';
  212. hover.appendChild(hoverRemove);
  213. }
  214. }
  215. /**
  216. * @param {UiMessage|UiMeMessage|UiNoticeMessage|null} msg
  217. * @return {Element}
  218. **/
  219. function doCreateMessageDom(msg) {
  220. var dom = document.createElement("div")
  221. ,msgBlock = document.createElement("div")
  222. ,hoverReaction = document.createElement("li")
  223. ,attachmentSummary = document.createElement("summary");
  224. dom.hover = document.createElement("ul");
  225. dom.attachments = document.createElement("ul");
  226. dom.reactions = document.createElement("ul");
  227. dom.ts = document.createElement("div");
  228. dom.textDom = document.createElement("div");
  229. dom.authorName = document.createElement("span");
  230. dom.attachmentWrapper = document.createElement("details");
  231. dom.attachmentWrapper.addEventListener("toggle", function() { this.toggled = true; });
  232. dom.className = R.klass.msg.item;
  233. dom.ts.className = R.klass.msg.ts;
  234. dom.textDom.className = R.klass.msg.msg;
  235. dom.authorName.className = R.klass.msg.authorname;
  236. if (msg) {
  237. dom.id = msg.channelId +"_" +msg.id;
  238. addHoverButtons(dom.hover, msg);
  239. dom.hover.className = R.klass.msg.hover.container;
  240. }
  241. msgBlock.appendChild(dom.authorName);
  242. msgBlock.appendChild(dom.textDom);
  243. msgBlock.appendChild(dom.ts);
  244. attachmentSummary.textContent = "attachment";
  245. dom.attachmentWrapper.appendChild(attachmentSummary);
  246. dom.attachmentWrapper.appendChild(dom.attachments);
  247. msgBlock.appendChild(dom.attachmentWrapper);
  248. dom.edited = document.createElement("div");
  249. dom.edited.className = R.klass.msg.edited;
  250. msgBlock.appendChild(dom.edited);
  251. msgBlock.appendChild(dom.reactions);
  252. msgBlock.className = R.klass.msg.content;
  253. dom.attachments.className = R.klass.msg.attachment.list;
  254. dom.reactions.className = R.klass.msg.reactions.container;
  255. dom.appendChild(msgBlock);
  256. dom.appendChild(dom.hover);
  257. return dom;
  258. }
  259. function makeUserColor(username) {
  260. if (!username.length) {
  261. return "black";
  262. }
  263. var hue = 0,
  264. saturation = 0,
  265. charCodes = [],
  266. sumCodes = 0,
  267. deviation = 0,
  268. maxDev = 0,
  269. avgCodes;
  270. for (var i =0, nbChars = username.length; i < nbChars; i++) {
  271. charCodes[i] = username.charCodeAt(i);
  272. sumCodes += charCodes[i];
  273. }
  274. avgCodes = sumCodes / username.length;
  275. charCodes.forEach(function(i) {
  276. var dev = Math.abs(avgCodes - i);
  277. deviation += dev;
  278. maxDev = Math.max(dev, maxDev);
  279. hue = i + ((hue << 5) - hue);
  280. });
  281. deviation /= username.length;
  282. hue = 360 * (Math.abs(hue - 60) % 199) / 199;
  283. saturation = maxDev === 0 ? 100 : Math.round(Math.min(100, Math.max(75, (deviation / maxDev) * 25 + 75)));
  284. return "hsl(" +hue +", 100%, " +saturation +"%)";
  285. }
  286. /**
  287. * @param {Chatter} user
  288. * @param {string} userName
  289. * @return {Element}
  290. **/
  291. function createMessageGroupDom(user, userName) {
  292. var dom = document.createElement("div")
  293. ,authorBlock = document.createElement("div")
  294. ,authorName = document.createElement("a")
  295. ,authorImg = document.createElement("img");
  296. dom.authorImgWrapper = document.createElement("span")
  297. dom.authorImgWrapper.className = R.klass.msg.authorAvatarWrapper;
  298. authorImg.className = R.klass.msg.authorAvatar;
  299. authorName.className = R.klass.msg.authorname;
  300. authorName.href = "#" +user.id;
  301. if (user) {
  302. authorName.textContent = user.getName();
  303. authorName.style.backgroundColor = makeUserColor(user.getName());
  304. authorImg.src = user.getSmallIcon();
  305. } else {
  306. authorName.textContent = userName || "?";
  307. authorImg.src = "";
  308. }
  309. dom.authorImgWrapper.appendChild(authorImg);
  310. authorBlock.appendChild(dom.authorImgWrapper);
  311. authorBlock.appendChild(authorName);
  312. authorBlock.className = R.klass.msg.author;
  313. dom.className = R.klass.msg.authorGroup;
  314. dom.appendChild(authorBlock);
  315. dom.content = document.createElement("div");
  316. dom.content.className = R.klass.msg.authorMessages;
  317. dom.appendChild(dom.content);
  318. return dom;
  319. }
  320. /**
  321. * @param {string=} colorText
  322. * @return {string}
  323. **/
  324. function getColor(colorText) {
  325. /** @const @type {Object.<string, string>} */
  326. var colorMap = {
  327. "good": "#2fa44f"
  328. ,"warning": "#de9e31"
  329. ,"danger": "#d50200"
  330. };
  331. if (colorText) {
  332. if (colorText[0] === '#')
  333. return colorText;
  334. else if (colorMap[colorText])
  335. return colorMap[colorText];
  336. }
  337. return "#e3e4e6";
  338. }
  339. /**
  340. * @param {string} channelId
  341. * @param {UiNoticeMessage|UiMessage} msg
  342. * @param {*} attachment
  343. * @param {number} attachmentIndex
  344. * @return {Element|null}
  345. **/
  346. function createAttachmentDom(channelId, msg, attachment, attachmentIndex) {
  347. var rootDom = document.createElement("li")
  348. ,attachmentBlock = document.createElement("div")
  349. ,pretext = document.createElement("div")
  350. ,titleBlock = document.createElement("a")
  351. ,authorBlock = document.createElement("div")
  352. ,authorImg = document.createElement("img")
  353. ,authorName = document.createElement("a")
  354. ,textBlock = document.createElement("div")
  355. ,textDom = document.createElement("div")
  356. ,thumbImgDom = document.createElement("div")
  357. ,imgDom = document.createElement("img")
  358. ,footerBlock = document.createElement("div")
  359. ;
  360. rootDom.className = R.klass.msg.attachment.container;
  361. //Color
  362. attachmentBlock.style.borderColor = getColor(attachment["color"] || "");
  363. attachmentBlock.className = R.klass.msg.attachment.block;
  364. //Pretext
  365. pretext.className = R.klass.msg.attachment.pretext;
  366. if (attachment["pretext"]) {
  367. pretext.innerHTML = msg.formatText(attachment["pretext"]);
  368. } else {
  369. pretext.classList.add(R.klass.hidden);
  370. }
  371. //Title
  372. titleBlock.target = "_blank";
  373. if (attachment["title"]) {
  374. titleBlock.innerHTML = msg.formatText(attachment["title"]);
  375. if (attachment["title_link"]) {
  376. titleBlock.href = attachment["title_link"];
  377. }
  378. titleBlock.className = R.klass.msg.attachment.title;
  379. } else {
  380. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  381. }
  382. //Author
  383. authorName.target = "_blank";
  384. authorBlock.className = R.klass.msg.author;
  385. if (attachment["author_name"]) {
  386. authorName.innerHTML = msg.formatText(attachment["author_name"]);
  387. authorName.href = attachment["author_link"] || "";
  388. authorName.className = R.klass.msg.authorname;
  389. authorImg.className = R.klass.msg.authorAvatar;
  390. if (attachment["author_icon"]) {
  391. authorImg.src = attachment["author_icon"];
  392. authorBlock.appendChild(authorImg);
  393. }
  394. authorBlock.appendChild(authorName);
  395. }
  396. // Img (small one)
  397. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  398. if (attachment["thumb_url"]) {
  399. var img = document.createElement("img");
  400. img.src = attachment["thumb_url"];
  401. thumbImgDom.appendChild(img);
  402. attachmentBlock.classList.add(R.klass.msg.attachment.hasThumb);
  403. if (attachment["video_html"])
  404. thumbImgDom.dataset["video"] = attachment["video_html"];
  405. } else {
  406. thumbImgDom.classList.add(R.klass.hidden);
  407. }
  408. //Text
  409. textBlock.className = R.klass.msg.attachment.content;
  410. var textContent = msg.formatText(attachment["text"] || "");
  411. textDom.className = R.klass.msg.attachment.text;
  412. if (textContent && textContent != "") {
  413. textDom.innerHTML = textContent;
  414. } else {
  415. textDom.classList.add(R.klass.hidden);
  416. }
  417. textBlock.appendChild(thumbImgDom);
  418. textBlock.appendChild(textDom);
  419. if (attachment["geo"]) {
  420. var geoTileDom = makeOSMTiles(attachment["geo"]);
  421. if (geoTileDom)
  422. textBlock.appendChild(geoTileDom);
  423. }
  424. //Img (the big one)
  425. imgDom.className = R.klass.msg.attachment.img;
  426. if (attachment["image_url"])
  427. imgDom.src = attachment["image_url"];
  428. else
  429. imgDom.classList.add(R.klass.hidden);
  430. //Footer
  431. footerBlock.className = R.klass.msg.attachment.footer;
  432. if (attachment["footer"]) {
  433. var footerText = document.createElement("span")
  434. footerText.className = R.klass.msg.attachment.footerText;
  435. footerText.innerHTML = msg.formatText(attachment["footer"]);
  436. if (attachment["footer_icon"]) {
  437. var footerIcon = document.createElement("img")
  438. footerIcon.src = attachment["footer_icon"];
  439. footerIcon.className = R.klass.msg.attachment.footerIcon;
  440. footerBlock.appendChild(footerIcon);
  441. }
  442. footerBlock.appendChild(footerText);
  443. }
  444. //Ts
  445. if (attachment["ts"]) {
  446. var footerTs = document.createElement("span")
  447. footerTs.className = R.klass.msg.ts;
  448. footerTs.innerHTML = locale.formatDate(attachment["ts"]);
  449. footerBlock.appendChild(footerTs);
  450. }
  451. attachmentBlock.appendChild(titleBlock);
  452. attachmentBlock.appendChild(authorBlock);
  453. attachmentBlock.appendChild(textBlock);
  454. attachmentBlock.appendChild(imgDom);
  455. // Fields
  456. if (attachment["fields"] && attachment["fields"].length) {
  457. var fieldsContainer = document.createElement("ul");
  458. attachmentBlock.appendChild(fieldsContainer);
  459. fieldsContainer.className = R.klass.msg.attachment.field.container;
  460. attachment["fields"].forEach(function(fieldData) {
  461. var fieldDom = createFieldDom(msg, fieldData["title"] || "", fieldData["value"] || "", !!fieldData["short"]);
  462. if (fieldDom) {
  463. fieldsContainer.appendChild(fieldDom);
  464. }
  465. });
  466. }
  467. // Buttons
  468. if (attachment["actions"] && attachment["actions"].length) {
  469. var buttons;
  470. buttons = document.createElement("ul");
  471. buttons.className = R.klass.msg.attachment.actions +' ' +R.klass.buttonContainer;
  472. attachmentBlock.appendChild(buttons);
  473. for (var i =0, nbAttachments = attachment["actions"].length; i < nbAttachments; i++) {
  474. var action = attachment["actions"][i];
  475. if (action) {
  476. var button = createActionButtonDom(attachmentIndex, i, action);
  477. if (button) {
  478. buttons.appendChild(button);
  479. }
  480. }
  481. }
  482. }
  483. attachmentBlock.appendChild(footerBlock);
  484. rootDom.appendChild(pretext);
  485. rootDom.appendChild(attachmentBlock);
  486. return rootDom;
  487. }
  488. /**
  489. * @param {UiMessage|UiNoticeMessage} msg
  490. * @param {string} title
  491. * @param {string} text
  492. * @param {boolean} isShort
  493. * @return {Element}
  494. **/
  495. function createFieldDom(msg, title, text, isShort) {
  496. var fieldDom = document.createElement("li")
  497. ,titleDom = document.createElement("div")
  498. ,textDom = document.createElement("div");
  499. fieldDom.className = R.klass.msg.attachment.field.item;
  500. if (!isShort) {
  501. fieldDom.classList.add(R.klass.msg.attachment.field.longField);
  502. }
  503. titleDom.className = R.klass.msg.attachment.field.title;
  504. titleDom.textContent = title;
  505. textDom.className = R.klass.msg.attachment.field.text;
  506. textDom.innerHTML = msg.formatText(text);
  507. fieldDom.appendChild(titleDom);
  508. fieldDom.appendChild(textDom);
  509. return fieldDom;
  510. }
  511. /**
  512. * @param {number} attachmentIndex
  513. * @param {number} actionIndex
  514. * @param {*} action
  515. * @return {Element}
  516. **/
  517. function createActionButtonDom(attachmentIndex, actionIndex, action) {
  518. var li = document.createElement("li")
  519. ,color = getColor(action["style"]);
  520. li.textContent = action["text"];
  521. if (color !== getColor())
  522. li.style.color = color;
  523. li.style.borderColor = color;
  524. li.dataset["attachmentIndex"] = attachmentIndex;
  525. li.dataset["actionIndex"] = actionIndex;
  526. li.className = R.klass.msg.attachment.actionItem +' ' +R.klass.button;
  527. return li;
  528. }
  529. /**
  530. * @param {Chatter} user
  531. * @return {Element}
  532. **/
  533. function makeUserIsTypingDom(user) {
  534. var dom = document.createElement("li")
  535. ,userName = document.createElement("span");
  536. userName.textContent = user.getName();
  537. dom.appendChild(createTypingDisplay());
  538. dom.appendChild(userName);
  539. return dom;
  540. }