1
0

dom.js 17 KB

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