dom.js 19 KB

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