dom.js 18 KB

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