dom.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. dom.appendChild(authorBlock);
  303. dom.content = document.createElement("div");
  304. dom.content.className = R.klass.msg.authorMessages;
  305. dom.messages = [];
  306. dom.appendChild(dom.content);
  307. dom.ts = firstTs;
  308. dom.meMessage = isMeMessage;
  309. dom.userId = user.id;
  310. return dom;
  311. }
  312. /**
  313. * @param {string=} colorText
  314. * @return {string}
  315. **/
  316. function getColor(colorText) {
  317. /** @const @type {Object.<string, string>} */
  318. var colorMap = {
  319. "good": "#2fa44f"
  320. ,"warning": "#de9e31"
  321. ,"danger": "#d50200"
  322. };
  323. if (colorText) {
  324. if (colorText[0] === '#')
  325. return colorText;
  326. else if (colorMap[colorText])
  327. return colorMap[colorText];
  328. }
  329. return "#e3e4e6";
  330. }
  331. /**
  332. * @param {string} channelId
  333. * @param {UiNoticeMessage|UiMessage} msg
  334. * @param {*} attachment
  335. * @param {number} attachmentIndex
  336. * @return {Element|null}
  337. **/
  338. function createAttachmentDom(channelId, msg, attachment, attachmentIndex) {
  339. var rootDom = document.createElement("li")
  340. ,attachmentBlock = document.createElement("div")
  341. ,pretext = document.createElement("div")
  342. ,titleBlock = document.createElement("a")
  343. ,authorBlock = document.createElement("div")
  344. ,authorImg = document.createElement("img")
  345. ,authorName = document.createElement("a")
  346. ,textBlock = document.createElement("div")
  347. ,textDom = document.createElement("div")
  348. ,thumbImgDom = document.createElement("div")
  349. ,imgDom = document.createElement("img")
  350. ,footerBlock = document.createElement("div")
  351. ;
  352. rootDom.className = R.klass.msg.attachment.container;
  353. //Color
  354. attachmentBlock.style.borderColor = getColor(attachment["color"] || "");
  355. attachmentBlock.className = R.klass.msg.attachment.block;
  356. //Pretext
  357. pretext.className = R.klass.msg.attachment.pretext;
  358. if (attachment["pretext"]) {
  359. pretext.innerHTML = msg.formatText(attachment["pretext"]);
  360. } else {
  361. pretext.classList.add(R.klass.hidden);
  362. }
  363. //Title
  364. titleBlock.target = "_blank";
  365. if (attachment["title"]) {
  366. titleBlock.innerHTML = msg.formatText(attachment["title"]);
  367. if (attachment["title_link"]) {
  368. titleBlock.href = attachment["title_link"];
  369. }
  370. titleBlock.className = R.klass.msg.attachment.title;
  371. } else {
  372. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  373. }
  374. //Author
  375. authorName.target = "_blank";
  376. authorBlock.className = R.klass.msg.author;
  377. if (attachment["author_name"]) {
  378. authorName.innerHTML = msg.formatText(attachment["author_name"]);
  379. authorName.href = attachment["author_link"] || "";
  380. authorName.className = R.klass.msg.authorname;
  381. authorImg.className = R.klass.msg.authorAvatar;
  382. if (attachment["author_icon"]) {
  383. authorImg.src = attachment["author_icon"];
  384. authorBlock.appendChild(authorImg);
  385. }
  386. authorBlock.appendChild(authorName);
  387. }
  388. // Img (small one)
  389. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  390. if (attachment["thumb_url"]) {
  391. var img = document.createElement("img");
  392. img.src = attachment["thumb_url"];
  393. thumbImgDom.appendChild(img);
  394. attachmentBlock.classList.add(R.klass.msg.attachment.hasThumb);
  395. if (attachment["video_html"])
  396. thumbImgDom.dataset["video"] = attachment["video_html"];
  397. } else {
  398. thumbImgDom.classList.add(R.klass.hidden);
  399. }
  400. //Text
  401. textBlock.className = R.klass.msg.attachment.content;
  402. var textContent = msg.formatText(attachment["text"] || "");
  403. textDom.className = R.klass.msg.attachment.text;
  404. if (textContent && textContent != "") {
  405. textDom.innerHTML = textContent;
  406. } else {
  407. textDom.classList.add(R.klass.hidden);
  408. }
  409. textBlock.appendChild(thumbImgDom);
  410. textBlock.appendChild(textDom);
  411. if (attachment["geo"]) {
  412. var geoTileDom = makeOSMTiles(attachment["geo"]);
  413. if (geoTileDom)
  414. textBlock.appendChild(geoTileDom);
  415. }
  416. //Img (the big one)
  417. imgDom.className = R.klass.msg.attachment.img;
  418. if (attachment["image_url"])
  419. imgDom.src = attachment["image_url"];
  420. else
  421. imgDom.classList.add(R.klass.hidden);
  422. //Footer
  423. footerBlock.className = R.klass.msg.attachment.footer;
  424. if (attachment["footer"]) {
  425. var footerText = document.createElement("span")
  426. footerText.className = R.klass.msg.attachment.footerText;
  427. footerText.innerHTML = msg.formatText(attachment["footer"]);
  428. if (attachment["footer_icon"]) {
  429. var footerIcon = document.createElement("img")
  430. footerIcon.src = attachment["footer_icon"];
  431. footerIcon.className = R.klass.msg.attachment.footerIcon;
  432. footerBlock.appendChild(footerIcon);
  433. }
  434. footerBlock.appendChild(footerText);
  435. }
  436. //Ts
  437. if (attachment["ts"]) {
  438. var footerTs = document.createElement("span")
  439. footerTs.className = R.klass.msg.ts;
  440. footerTs.innerHTML = locale.formatDate(attachment["ts"]);
  441. footerBlock.appendChild(footerTs);
  442. }
  443. attachmentBlock.appendChild(titleBlock);
  444. attachmentBlock.appendChild(authorBlock);
  445. attachmentBlock.appendChild(textBlock);
  446. attachmentBlock.appendChild(imgDom);
  447. // Fields
  448. if (attachment["fields"] && attachment["fields"].length) {
  449. var fieldsContainer = document.createElement("ul");
  450. attachmentBlock.appendChild(fieldsContainer);
  451. fieldsContainer.className = R.klass.msg.attachment.field.container;
  452. attachment["fields"].forEach(function(fieldData) {
  453. var fieldDom = createFieldDom(msg, fieldData["title"] || "", fieldData["value"] || "", !!fieldData["short"]);
  454. if (fieldDom) {
  455. fieldsContainer.appendChild(fieldDom);
  456. }
  457. });
  458. }
  459. // Buttons
  460. if (attachment["actions"] && attachment["actions"].length) {
  461. var buttons;
  462. buttons = document.createElement("ul");
  463. buttons.className = R.klass.msg.attachment.actions +' ' +R.klass.buttonContainer;
  464. attachmentBlock.appendChild(buttons);
  465. for (var i =0, nbAttachments = attachment["actions"].length; i < nbAttachments; i++) {
  466. var action = attachment["actions"][i];
  467. if (action) {
  468. var button = createActionButtonDom(attachmentIndex, i, action);
  469. if (button) {
  470. buttons.appendChild(button);
  471. }
  472. }
  473. }
  474. }
  475. attachmentBlock.appendChild(footerBlock);
  476. rootDom.appendChild(pretext);
  477. rootDom.appendChild(attachmentBlock);
  478. return rootDom;
  479. }
  480. /**
  481. * @param {UiMessage|UiNoticeMessage} msg
  482. * @param {string} title
  483. * @param {string} text
  484. * @param {boolean} isShort
  485. * @return {Element}
  486. **/
  487. function createFieldDom(msg, title, text, isShort) {
  488. var fieldDom = document.createElement("li")
  489. ,titleDom = document.createElement("div")
  490. ,textDom = document.createElement("div");
  491. fieldDom.className = R.klass.msg.attachment.field.item;
  492. if (!isShort) {
  493. fieldDom.classList.add(R.klass.msg.attachment.field.longField);
  494. }
  495. titleDom.className = R.klass.msg.attachment.field.title;
  496. titleDom.textContent = title;
  497. textDom.className = R.klass.msg.attachment.field.text;
  498. textDom.innerHTML = msg.formatText(text);
  499. fieldDom.appendChild(titleDom);
  500. fieldDom.appendChild(textDom);
  501. return fieldDom;
  502. }
  503. /**
  504. * @param {number} attachmentIndex
  505. * @param {number} actionIndex
  506. * @param {*} action
  507. * @return {Element}
  508. **/
  509. function createActionButtonDom(attachmentIndex, actionIndex, action) {
  510. var li = document.createElement("li")
  511. ,color = getColor(action["style"]);
  512. li.textContent = action["text"];
  513. if (color !== getColor())
  514. li.style.color = color;
  515. li.style.borderColor = color;
  516. li.dataset["attachmentIndex"] = attachmentIndex;
  517. li.dataset["actionIndex"] = actionIndex;
  518. li.className = R.klass.msg.attachment.actionItem +' ' +R.klass.button;
  519. return li;
  520. }
  521. /**
  522. * @param {Chatter} user
  523. * @return {Element}
  524. **/
  525. function makeUserIsTypingDom(user) {
  526. var dom = document.createElement("li")
  527. ,userName = document.createElement("span");
  528. userName.textContent = user.getName();
  529. dom.appendChild(createTypingDisplay());
  530. dom.appendChild(userName);
  531. return dom;
  532. }