dom.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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) {
  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;
  70. link.textContent = alternateChanName || ims.user.name;
  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) {
  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. * @param {string} chanId
  85. * @param {string} msgId
  86. * @param {string} reaction
  87. * @param {Array.<string>} users
  88. * @return {Element|null}
  89. **/
  90. function createReactionDom(chanId, msgId, reaction, users) {
  91. var emojiDom = makeEmojiDom(reaction);
  92. if (emojiDom) {
  93. var dom = document.createElement("li")
  94. ,a = document.createElement("a")
  95. ,emojiContainer = document.createElement("span")
  96. ,userList = document.createElement("span")
  97. ,userNames = [];
  98. for (var i =0, nbUser = users.length; i < nbUser; i++) {
  99. var user = DATA.context.getUser(users[i]);
  100. if (user)
  101. userNames.push(user.name);
  102. }
  103. userNames.sort();
  104. userList.textContent = userNames.join(", ");
  105. emojiContainer.appendChild(emojiDom);
  106. emojiContainer.className = R.klass.emoji.small;
  107. a.href = "javascript:toggleReaction('" +chanId +"', '" +msgId +"', '" +reaction +"')";
  108. a.appendChild(emojiContainer);
  109. a.appendChild(userList);
  110. dom.className = R.klass.msg.reactions.item;
  111. dom.appendChild(a);
  112. return dom;
  113. } else {
  114. console.warn("Reaction id not found: " +reaction);
  115. }
  116. return null;
  117. }
  118. /**
  119. * @param {UiMessage|UiMeMessage|UiNoticeMessage} msg
  120. * @param {boolean=} skipAttachment
  121. * @return {Element}
  122. **/
  123. function doCreateMessageDom(msg, skipAttachment) {
  124. var channelId = msg.channelId;
  125. var dom = document.createElement("div")
  126. ,msgBlock = document.createElement("div")
  127. ,hover = document.createElement("ul")
  128. ,hoverReply = document.createElement("li")
  129. dom.attachments = document.createElement("ul");
  130. dom.reactions = document.createElement("ul");
  131. dom.ts = document.createElement("div");
  132. dom.textDom = document.createElement("div");
  133. dom.authorName = document.createElement("span");
  134. dom.id = channelId +"_" +msg.id;
  135. dom.className = R.klass.msg.item;
  136. dom.ts.className = R.klass.msg.ts;
  137. dom.textDom.className = R.klass.msg.msg;
  138. dom.authorName.className = R.klass.msg.authorname;
  139. hover.className = R.klass.msg.hover.container;
  140. hoverReply.className = R.klass.msg.hover.reply;
  141. hover.appendChild(hoverReply);
  142. if ('makeEmoji' in window) {
  143. var hoverReaction = document.createElement("li")
  144. ,domReply = window['makeEmoji']("arrow_heading_down")
  145. ,domReaction = window['makeEmoji']("smile")
  146. ,domEdit = window['makeEmoji']("pencil2")
  147. ,domRemove = window['makeEmoji']("x");
  148. hoverReaction.className = R.klass.msg.hover.reaction;
  149. if (domReaction) {
  150. hoverReaction.classList.add(R.klass.emoji.small);
  151. hoverReaction.appendChild(domReaction);
  152. } else {
  153. hoverReaction.style.backgroundImage = 'url("smile.svg")';
  154. }
  155. if (domReply) {
  156. hoverReply.classList.add(R.klass.emoji.small);
  157. hoverReply.appendChild(domReply);
  158. } else {
  159. hoverReply.style.backgroundImage = 'url("repl.svg")';
  160. }
  161. hover.appendChild(hoverReaction);
  162. if (DATA.context.isMe(msg.userId)) {
  163. var hoverEdit = document.createElement("li");
  164. hoverEdit.className = R.klass.msg.hover.edit;
  165. if (domEdit)
  166. hoverEdit.classList.add(R.klass.emoji.small);
  167. else
  168. hoverEdit.style.backgroundImage = 'url("edit.svg")';
  169. hoverEdit.appendChild(domEdit);
  170. hover.appendChild(hoverEdit);
  171. var hoverRemove = document.createElement("li");
  172. hoverRemove.className = R.klass.msg.hover.remove;
  173. if (domRemove)
  174. hoverRemove.classList.add(R.klass.emoji.small);
  175. else
  176. hoverRemove.style.backgroundImage = 'url("remove.svg")';
  177. hoverRemove.appendChild(domRemove);
  178. hover.appendChild(hoverRemove);
  179. }
  180. } else {
  181. hoverReply.style.backgroundImage = 'url("repl.svg")';
  182. if (DATA.context.isMe(msg.userId)) {
  183. var hoverEdit = document.createElement("li");
  184. hoverEdit.className = R.klass.msg.hover.edit;
  185. hoverEdit.style.backgroundImage = 'url("edit.svg")';
  186. hover.appendChild(hoverEdit);
  187. var hoverRemove = document.createElement("li")
  188. hoverRemove.className = R.klass.msg.hover.remove;
  189. hoverRemove.style.backgroundImage = 'url("remove.svg")';
  190. hover.appendChild(hoverRemove);
  191. }
  192. }
  193. msgBlock.appendChild(dom.authorName);
  194. msgBlock.appendChild(dom.textDom);
  195. msgBlock.appendChild(dom.ts);
  196. msgBlock.appendChild(dom.attachments);
  197. dom.edited = document.createElement("div");
  198. dom.edited.className = R.klass.msg.edited;
  199. msgBlock.appendChild(dom.edited);
  200. msgBlock.appendChild(dom.reactions);
  201. msgBlock.className = R.klass.msg.content;
  202. dom.attachments.className = R.klass.msg.attachment.list;
  203. dom.reactions.className = R.klass.msg.reactions.container;
  204. dom.appendChild(msgBlock);
  205. dom.appendChild(hover);
  206. return dom;
  207. }
  208. /**
  209. * @param {Chatter} user
  210. * @param {string} userName
  211. * @return {Element}
  212. **/
  213. function createMessageGroupDom(user, userName) {
  214. var dom = document.createElement("div")
  215. ,authorBlock = document.createElement("div")
  216. ,authorName = document.createElement("span")
  217. ,authorImg = document.createElement("img");
  218. dom.authorImgWrapper = document.createElement("span")
  219. dom.authorImgWrapper.className = R.klass.msg.authorAvatarWrapper;
  220. authorImg.className = R.klass.msg.authorAvatar;
  221. authorName.className = R.klass.msg.authorname;
  222. if (user) {
  223. authorName.textContent = user.name;
  224. authorImg.src = user.getSmallIcon();
  225. } else {
  226. authorName.textContent = userName || "?";
  227. authorImg.src = "";
  228. }
  229. dom.authorImgWrapper.appendChild(authorImg);
  230. authorBlock.appendChild(dom.authorImgWrapper);
  231. authorBlock.appendChild(authorName);
  232. authorBlock.className = R.klass.msg.author;
  233. dom.className = R.klass.msg.authorGroup;
  234. dom.appendChild(authorBlock);
  235. dom.content = document.createElement("div");
  236. dom.content.className = R.klass.msg.authorMessages;
  237. dom.appendChild(dom.content);
  238. return dom;
  239. }
  240. /**
  241. * @param {string=} colorText
  242. * @return {string}
  243. **/
  244. function getColor(colorText) {
  245. /** @const @type {Object.<string, string>} */
  246. var colorMap = {
  247. "good": "#2fa44f"
  248. ,"warning": "#de9e31"
  249. ,"danger": "#d50200"
  250. };
  251. if (colorText) {
  252. if (colorText[0] === '#')
  253. return colorText;
  254. else if (colorMap[colorText])
  255. return colorMap[colorText];
  256. }
  257. return "#e3e4e6";
  258. }
  259. /**
  260. * @param {string} channelId
  261. * @param {UiNoticeMessage|UiMessage} msg
  262. * @param {*} attachment
  263. * @param {number} attachmentIndex
  264. * @return {Element|null}
  265. **/
  266. function createAttachmentDom(channelId, msg, attachment, attachmentIndex) {
  267. var rootDom = document.createElement("li")
  268. ,attachmentBlock = document.createElement("div")
  269. ,pretext = document.createElement("div")
  270. ,titleBlock = document.createElement("a")
  271. ,authorBlock = document.createElement("div")
  272. ,authorImg = document.createElement("img")
  273. ,authorName = document.createElement("a")
  274. ,textBlock = document.createElement("div")
  275. ,textDom = document.createElement("div")
  276. ,thumbImgDom = document.createElement("div")
  277. ,imgDom = document.createElement("img")
  278. ,footerBlock = document.createElement("div")
  279. ;
  280. rootDom.className = R.klass.msg.attachment.container;
  281. //Color
  282. attachmentBlock.style.borderColor = getColor(attachment["color"] || "");
  283. attachmentBlock.className = R.klass.msg.attachment.block;
  284. //Pretext
  285. pretext.className = R.klass.msg.attachment.pretext;
  286. if (attachment["pretext"]) {
  287. pretext.innerHTML = msg.formatText(attachment["pretext"]);
  288. } else {
  289. pretext.classList.add(R.klass.hidden);
  290. }
  291. //Title
  292. titleBlock.target = "_blank";
  293. if (attachment["title"]) {
  294. titleBlock.innerHTML = msg.formatText(attachment["title"]);
  295. if (attachment["title_link"]) {
  296. titleBlock.href = attachment["title_link"];
  297. }
  298. titleBlock.className = R.klass.msg.attachment.title;
  299. } else {
  300. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  301. }
  302. //Author
  303. authorName.target = "_blank";
  304. authorBlock.className = R.klass.msg.author;
  305. if (attachment["author_name"]) {
  306. authorName.innerHTML = msg.formatText(attachment["author_name"]);
  307. authorName.href = attachment["author_link"] || "";
  308. authorName.className = R.klass.msg.authorname;
  309. authorImg.className = R.klass.msg.authorAvatar;
  310. if (attachment["author_icon"]) {
  311. authorImg.src = attachment["author_icon"];
  312. authorBlock.appendChild(authorImg);
  313. }
  314. authorBlock.appendChild(authorName);
  315. }
  316. // Img (small one)
  317. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  318. if (attachment["thumb_url"]) {
  319. var img = document.createElement("img");
  320. img.src = attachment["thumb_url"];
  321. thumbImgDom.appendChild(img);
  322. attachmentBlock.classList.add(R.klass.msg.attachment.hasThumb);
  323. if (attachment["video_html"])
  324. thumbImgDom.dataset["video"] = attachment["video_html"];
  325. } else {
  326. thumbImgDom.classList.add(R.klass.hidden);
  327. }
  328. //Text
  329. textBlock.className = R.klass.msg.attachment.content;
  330. var textContent = msg.formatText(attachment["text"] || "");
  331. textDom.className = R.klass.msg.attachment.text;
  332. if (textContent && textContent != "") {
  333. textDom.innerHTML = textContent;
  334. } else {
  335. textDom.classList.add(R.klass.hidden);
  336. }
  337. textBlock.appendChild(thumbImgDom);
  338. textBlock.appendChild(textDom);
  339. if (attachment["geo"]) {
  340. var geoTileDom = makeOSMTiles(attachment["geo"]);
  341. if (geoTileDom)
  342. textBlock.appendChild(geoTileDom);
  343. }
  344. //Img (the big one)
  345. imgDom.className = R.klass.msg.attachment.img;
  346. if (attachment["image_url"])
  347. imgDom.src = attachment["image_url"];
  348. else
  349. imgDom.classList.add(R.klass.hidden);
  350. //Footer
  351. footerBlock.className = R.klass.msg.attachment.footer;
  352. if (attachment["footer"]) {
  353. var footerText = document.createElement("span")
  354. footerText.className = R.klass.msg.attachment.footerText;
  355. footerText.innerHTML = msg.formatText(attachment["footer"]);
  356. if (attachment["footer_icon"]) {
  357. var footerIcon = document.createElement("img")
  358. footerIcon.src = attachment["footer_icon"];
  359. footerIcon.className = R.klass.msg.attachment.footerIcon;
  360. footerBlock.appendChild(footerIcon);
  361. }
  362. footerBlock.appendChild(footerText);
  363. }
  364. //Ts
  365. if (attachment["ts"]) {
  366. var footerTs = document.createElement("span")
  367. footerTs.className = R.klass.msg.ts;
  368. footerTs.innerHTML = locale.formatDate(attachment["ts"]);
  369. footerBlock.appendChild(footerTs);
  370. }
  371. attachmentBlock.appendChild(titleBlock);
  372. attachmentBlock.appendChild(authorBlock);
  373. attachmentBlock.appendChild(textBlock);
  374. attachmentBlock.appendChild(imgDom);
  375. // Fields
  376. if (attachment["fields"] && attachment["fields"].length) {
  377. var fieldsContainer = document.createElement("ul");
  378. attachmentBlock.appendChild(fieldsContainer);
  379. fieldsContainer.className = R.klass.msg.attachment.field.container;
  380. attachment["fields"].forEach(function(fieldData) {
  381. var fieldDom = createFieldDom(msg, fieldData["title"] || "", fieldData["value"] || "", !!fieldData["short"]);
  382. if (fieldDom) {
  383. fieldsContainer.appendChild(fieldDom);
  384. }
  385. });
  386. }
  387. // Buttons
  388. if (attachment["actions"] && attachment["actions"].length) {
  389. var buttons;
  390. buttons = document.createElement("ul");
  391. buttons.className = R.klass.msg.attachment.actions +' ' +R.klass.buttonContainer;
  392. attachmentBlock.appendChild(buttons);
  393. for (var i =0, nbAttachments = attachment["actions"].length; i < nbAttachments; i++) {
  394. var action = attachment["actions"][i];
  395. if (action) {
  396. var button = createActionButtonDom(attachmentIndex, i, action);
  397. if (button) {
  398. buttons.appendChild(button);
  399. }
  400. }
  401. }
  402. }
  403. attachmentBlock.appendChild(footerBlock);
  404. rootDom.appendChild(pretext);
  405. rootDom.appendChild(attachmentBlock);
  406. return rootDom;
  407. }
  408. /**
  409. * @param {UiMessage|UiNoticeMessage} msg
  410. * @param {string} title
  411. * @param {string} text
  412. * @param {boolean} isShort
  413. * @return {Element}
  414. **/
  415. function createFieldDom(msg, title, text, isShort) {
  416. var fieldDom = document.createElement("li")
  417. ,titleDom = document.createElement("div")
  418. ,textDom = document.createElement("div");
  419. fieldDom.className = R.klass.msg.attachment.field.item;
  420. if (!isShort) {
  421. fieldDom.classList.add(R.klass.msg.attachment.field.longField);
  422. }
  423. titleDom.className = R.klass.msg.attachment.field.title;
  424. titleDom.textContent = title;
  425. textDom.className = R.klass.msg.attachment.field.text;
  426. textDom.innerHTML = msg.formatText(text);
  427. fieldDom.appendChild(titleDom);
  428. fieldDom.appendChild(textDom);
  429. return fieldDom;
  430. }
  431. /**
  432. * @param {number} attachmentIndex
  433. * @param {number} actionIndex
  434. * @param {*} action
  435. * @return {Element}
  436. **/
  437. function createActionButtonDom(attachmentIndex, actionIndex, action) {
  438. var li = document.createElement("li")
  439. ,color = getColor(action["style"]);
  440. li.textContent = action["text"];
  441. if (color !== getColor())
  442. li.style.color = color;
  443. li.style.borderColor = color;
  444. li.dataset["attachmentIndex"] = attachmentIndex;
  445. li.dataset["actionIndex"] = actionIndex;
  446. li.className = R.klass.msg.attachment.actionItem +' ' +R.klass.button;
  447. return li;
  448. }
  449. /**
  450. * @param {Chatter} user
  451. * @return {Element}
  452. **/
  453. function makeUserIsTypingDom(user) {
  454. var dom = document.createElement("li")
  455. ,userName = document.createElement("span");
  456. userName.textContent = user.name;
  457. dom.appendChild(createTypingDisplay());
  458. dom.appendChild(userName);
  459. return dom;
  460. }
  461. /**
  462. * @param {string} servicename
  463. * @return {Element}
  464. **/
  465. function createSlashAutocompleteHeader(servicename) {
  466. var lh = document.createElement("lh");
  467. lh.textContent = servicename;
  468. lh.className = R.klass.commands.header;
  469. return lh;
  470. }
  471. /**
  472. * @param {Command} cmd
  473. * @return {Element}
  474. **/
  475. function createSlashAutocompleteDom(cmd) {
  476. var li = document.createElement("li")
  477. ,name = document.createElement("span")
  478. ,usage = document.createElement("span")
  479. ,desc = document.createElement("span");
  480. name.textContent = cmd.name;
  481. usage.textContent = cmd.usage;
  482. desc.textContent = cmd.desc;
  483. li.appendChild(name);
  484. li.appendChild(usage);
  485. li.appendChild(desc);
  486. li.className = R.klass.commands.item;
  487. name.className = R.klass.commands.name;
  488. usage.className = R.klass.commands.usage;
  489. desc.className = R.klass.commands.desc;
  490. return li;
  491. }