dom.js 17 KB

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