dom.js 19 KB

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