dom.js 16 KB

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