1
0

dom.js 19 KB

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