dom.js 16 KB

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