ui.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. var
  2. /** @type {SlackMessage|null} */
  3. REPLYING_TO = null
  4. ;
  5. /**
  6. * @param {SlackChan|SlackGroup} chan
  7. * @return {Element}
  8. **/
  9. function createChanListItem(chan) {
  10. var dom = document.createElement("li");
  11. dom.id = chan.id;
  12. if (chan.id[0] === 'D')
  13. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeDirect;
  14. else if (chan.id[0] === 'G')
  15. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeGroup;
  16. else if (chan.id[0] === 'C')
  17. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeChannel;
  18. dom.textContent = chan.name;
  19. return dom;
  20. }
  21. /**
  22. * @param {SlackIms} ims
  23. * @return {Element}
  24. **/
  25. function createImsListItem(ims) {
  26. var dom = document.createElement("li");
  27. dom.id = ims.id;
  28. dom.className = R.klass.chatList.entry;
  29. dom.textContent = ims.user.name;
  30. return dom;
  31. }
  32. function onContextUpdated() {
  33. var chanListFram = document.createDocumentFragment();
  34. var sortedChans = SLACK.context.self ? Object.keys(SLACK.context.self.channels) : [];
  35. sortedChans.sort(function(a, b) {
  36. if (a[0] !== b[0]) {
  37. return a[0] - b[0];
  38. }
  39. return (SLACK.context.channels[a] || SLACK.context.groups[a]).name.localeCompare((SLACK.context.channels[b] || SLACK.context.groups[b]).name);
  40. });
  41. sortedChans.forEach(function(chanId) {
  42. var chan = SLACK.context.channels[chanId] || SLACK.context.groups[chanId]
  43. ,chanListItem = createChanListItem(chan);
  44. if (chanListItem) {
  45. chanListFram.appendChild(chanListItem);
  46. }
  47. });
  48. var sortedUsers = SLACK.context.users ? Object.keys(SLACK.context.users) : [];
  49. sortedUsers.sort(function(a, b) {
  50. return SLACK.context.users[a].name.localeCompare(SLACK.context.users[b].name);
  51. });
  52. sortedUsers.forEach(function(userId) {
  53. var ims = SLACK.context.users[userId].ims
  54. ,imsListItem = createImsListItem(ims);
  55. if (imsListItem) {
  56. chanListFram.appendChild(imsListItem);
  57. }
  58. });
  59. document.getElementById(R.id.chanList).textContent = "";
  60. document.getElementById(R.id.chanList).appendChild(chanListFram);
  61. }
  62. function onNetworkStateUpdated(isNetworkWorking) {
  63. isNetworkWorking ? document.body.classList.remove(R.klass.noNetwork) : document.body.classList.add(R.klass.noNetwork);
  64. }
  65. function onRoomSelected() {
  66. var name = SELECTED_ROOM.name || (SELECTED_ROOM.user ? SELECTED_ROOM.user.name : undefined);
  67. if (!name) {
  68. var members = [];
  69. for (var i in SELECTED_ROOM.members) {
  70. members.push(SELECTED_ROOM.members[i].name);
  71. }
  72. name = members.join(", ");
  73. }
  74. var roomLi = document.getElementById(SELECTED_ROOM.id);
  75. document.getElementById(R.id.currentRoom.title).textContent = name;
  76. onRoomUpdated();
  77. focusInput();
  78. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  79. markRoomAsRead(SELECTED_ROOM);
  80. if (REPLYING_TO) {
  81. REPLYING_TO = null;
  82. onReplyingToUpdated();
  83. }
  84. }
  85. function onReplyingToUpdated() {
  86. if (REPLYING_TO) {
  87. document.body.classList.add(R.klass.replyingTo);
  88. var domParent = document.getElementById(R.id.message.replyTo)
  89. ,closeLink = document.createElement("a");
  90. closeLink.addEventListener("click", function() {
  91. REPLYING_TO = null;
  92. onReplyingToUpdated();
  93. });
  94. closeLink.className = R.klass.msg.replyTo.close;
  95. closeLink.textContent = 'x';
  96. domParent.textContent = "";
  97. domParent.appendChild(closeLink);
  98. domParent.appendChild(createMessageDom("reply_" +SELECTED_ROOM.id, REPLYING_TO, true));
  99. } else {
  100. document.body.classList.remove(R.klass.replyingTo);
  101. }
  102. }
  103. /**
  104. * @param {string} channelId
  105. * @param {SlackMessage} msg
  106. * @param {boolean=} skipAttachment
  107. * @return {Element}
  108. **/
  109. function doCreateMessageDom(channelId, msg, skipAttachment) {
  110. var dom = document.createElement("div")
  111. ,ts = document.createElement("div")
  112. ,text = document.createElement("div")
  113. ,author = document.createElement("div")
  114. ,authorImg = document.createElement("img")
  115. ,authorName = document.createElement("span")
  116. ,hover = document.createElement("ul")
  117. ,hoverReply = document.createElement("li")
  118. ,attachments = document.createElement("ul")
  119. ,sender = msg.raw["user"] ?
  120. SLACK.context.users[msg.raw["user"]] :
  121. SLACK.context.bots[msg.raw["bot_id"]];
  122. dom.id = channelId +"_" +msg.ts;
  123. dom.className = R.klass.msg.item;
  124. ts.className = R.klass.msg.ts;
  125. text.className = R.klass.msg.msg;
  126. author.className = R.klass.msg.author;
  127. authorImg.className = R.klass.msg.authorAvatar;
  128. authorName.className = R.klass.msg.authorname;
  129. hover.className = R.klass.msg.hover.container;
  130. hoverReply.className = R.klass.msg.hover.reply;
  131. ts.textContent = (new Date(msg.ts * 1000)).toLocaleTimeString();
  132. text.innerHTML = formatSlackText(msg.raw["text"] || "");
  133. authorName.textContent = sender ? sender.name : (msg.raw["username"] || "?");
  134. if (!sender && !msg.raw["username"])
  135. text.textContent = msg.raw["subtype"] || JSON.stringify(msg.raw);
  136. authorImg.src = sender ? sender.icons.image_48 : "";
  137. author.appendChild(authorImg);
  138. author.appendChild(authorName);
  139. hover.appendChild(hoverReply);
  140. dom.appendChild(author);
  141. dom.appendChild(text);
  142. dom.appendChild(ts);
  143. dom.appendChild(attachments);
  144. attachments.className = R.klass.msg.attachment.list;
  145. if (skipAttachment !== true && msg.raw["attachments"]) {
  146. msg.raw["attachments"].forEach(function(attachment) {
  147. var domAttachment = createAttachmentDom(channelId, msg, attachment);
  148. if (domAttachment)
  149. attachments.appendChild(domAttachment);
  150. });
  151. }
  152. dom.appendChild(hover);
  153. return dom;
  154. }
  155. /**
  156. * @param {string} fullText
  157. * @return {string}
  158. **/
  159. function formatSlackText(fullText) {
  160. var msgContents = fullText.split(/\r?\n/g);
  161. for (var msgContentIndex=0, nbMsgContents = msgContents.length; msgContentIndex < nbMsgContents; msgContentIndex++) {
  162. var msgContent = msgContents[msgContentIndex]
  163. ,_msgContent = ""
  164. ,currentMods = {}
  165. ,quote = false
  166. ,i =0
  167. ,msgLength = msgContent.length;
  168. var checkEnd = function(str, pos, c) {
  169. while (str[pos]) {
  170. if (str[pos] != ' ' && str[pos] != c && str[pos +1] == c) {
  171. return true;
  172. }
  173. pos++;
  174. }
  175. return false;
  176. } ,appendMod = function(mods) {
  177. if (!Object.keys(currentMods).length)
  178. return "";
  179. return '<span class="' +Object.keys(mods).join(' ') +'">';
  180. };
  181. // Skip trailing
  182. while (i < msgLength && (msgContent[i] === ' ' || msgContent[i] === '\t'))
  183. i++;
  184. if (msgContent.substr(i, 4) === '&gt;') {
  185. quote = true;
  186. i += 4;
  187. }
  188. for (; i < msgLength; i++) {
  189. var c = msgContent[i];
  190. if (!(currentMods[R.klass.msg.style.bold]) && c === '*' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
  191. if (Object.keys(currentMods).length)
  192. _msgContent += '</span>';
  193. currentMods[R.klass.msg.style.bold] = true;
  194. _msgContent += appendMod(currentMods);
  195. } else if (!(currentMods[R.klass.msg.style.strike]) && c === '~' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
  196. if (Object.keys(currentMods).length)
  197. _msgContent += '</span>';
  198. currentMods[R.klass.msg.style.strike] = true;
  199. _msgContent += appendMod(currentMods);
  200. } else if (!(currentMods[R.klass.msg.style.code]) && c === '`' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
  201. if (Object.keys(currentMods).length)
  202. _msgContent += '</span>';
  203. currentMods[R.klass.msg.style.code] = true;
  204. _msgContent += appendMod(currentMods);
  205. } else if (!(currentMods[R.klass.msg.style.italic]) && c === '_' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
  206. if (Object.keys(currentMods).length)
  207. _msgContent += '</span>';
  208. currentMods[R.klass.msg.style.italic] = true;
  209. _msgContent += appendMod(currentMods);
  210. } else {
  211. var finalFound = false;
  212. _msgContent += c;
  213. do {
  214. if ((currentMods[R.klass.msg.style.bold]) && c !== '*' && msgContent[i +1] === '*') {
  215. delete currentMods[R.klass.msg.style.bold];
  216. finalFound = true;
  217. } else if ((currentMods[R.klass.msg.style.strike]) && c !== '~' && msgContent[i +1] === '~') {
  218. delete currentMods[R.klass.msg.style.strike];
  219. finalFound = true;
  220. } else if ((currentMods[R.klass.msg.style.code]) && c !== '`' && msgContent[i +1] === '`') {
  221. delete currentMods[R.klass.msg.style.code];
  222. finalFound = true;
  223. } else if ((currentMods[R.klass.msg.style.italic]) && c !== '_' && msgContent[i +1] === '_') {
  224. delete currentMods[R.klass.msg.style.italic];
  225. finalFound = true;
  226. } else {
  227. break;
  228. }
  229. c = msgContent[++i];
  230. } while (i < msgLength);
  231. if (finalFound)
  232. _msgContent += '</span>' +appendMod(currentMods);
  233. }
  234. }
  235. if (currentMods) {
  236. // Should not append
  237. _msgContent += '</span>';
  238. }
  239. _msgContent = _msgContent.replace(new RegExp('<([@#]?)([^>]*)>', 'g'),
  240. function(matched, type, entity) {
  241. var sub = entity.split('|');
  242. if (type === '@') {
  243. if (!sub[1]) {
  244. var user = SLACK.context.getMember(sub[0]);
  245. sub[1] = user ? ('@' +user.name) : "Unknown member"; // TODO locale
  246. } else if ('@' !== sub[1][0]) {
  247. sub[1] = '@' +sub[1];
  248. }
  249. sub[0] = '#' +sub[0];
  250. sub[2] = R.klass.msg.link +' ' +R.klass.msg.linkuser;
  251. } else if (type === '#') {
  252. if (!sub[1]) {
  253. var chan = SLACK.context.getChannel(sub[0]);
  254. sub[1] = chan ? ('#' +chan.name) : "Unknown channel"; // TODO locale
  255. } else if ('#' !== sub[1][0]) {
  256. sub[1] = '#' +sub[1];
  257. }
  258. sub[0] = '#' +sub[0];
  259. sub[2] = R.klass.msg.link +' ' +R.klass.msg.linkchan;
  260. } else if (sub[0].indexOf("://") !== -1) {
  261. if (!sub[1])
  262. sub[1] = sub[0];
  263. sub[2] = R.klass.msg.link;
  264. } else {
  265. return matched;
  266. }
  267. return '<a href="' +sub[0] +'" class="' +sub[2] +'"' +(!type ? ' target="_blank"' : '') +'>' +sub[1] +'</a>';
  268. });
  269. if (quote)
  270. msgContents[msgContentIndex] = '<span class="' +R.klass.msg.style.quote +'">' +_msgContent +'</span>';
  271. else
  272. msgContents[msgContentIndex] = _msgContent;
  273. }
  274. return msgContents.join('<br/>');
  275. }
  276. /**
  277. * @param {string} channelId
  278. * @param {SlackMessage} msg
  279. * @param {*} attachment
  280. * @return {Element|null}
  281. **/
  282. function createAttachmentDom(channelId, msg, attachment) {
  283. var rootDom = document.createElement("li")
  284. ,attachmentBlock = document.createElement("div")
  285. ,pretext = document.createElement("div")
  286. ,titleBlock = document.createElement("a")
  287. ,authorBlock = document.createElement("div")
  288. ,authorImg = document.createElement("img")
  289. ,authorName = document.createElement("a")
  290. ,textBlock = document.createElement("div")
  291. ,textDom = document.createElement("div")
  292. ,thumbImgDom = document.createElement("img")
  293. ,imgDom = document.createElement("img")
  294. ,footerBlock = document.createElement("div")
  295. ,footerIcon = document.createElement("img")
  296. ,footerText = document.createElement("span")
  297. ,footerTs = document.createElement("span")
  298. ;
  299. rootDom.className = R.klass.msg.attachment.container;
  300. //Color
  301. var color = "#e3e4e6";
  302. if (attachment["color"]) {
  303. if (attachment["color"][0] === '#')
  304. color = attachment["color"][0];
  305. else if (attachment["color"] === "good")
  306. color = "#2fa44f";
  307. else if (attachment["color"] === "warning")
  308. color = "#de9e31";
  309. else if (attachment["color"] === "danger")
  310. color = "#d50200";
  311. }
  312. attachmentBlock.style.borderColor = color;
  313. //Pretext
  314. pretext.className = R.klass.msg.attachment.pretext;
  315. if (attachment["pretext"]) {
  316. pretext.innerHTML = formatSlackText(attachment["pretext"]);
  317. } else {
  318. pretext.classList.add(R.klass.hidden);
  319. }
  320. //Title
  321. titleBlock.target = "_blank";
  322. if (attachment["title"]) {
  323. titleBlock.innerHTML = formatSlackText(attachment["title"]);
  324. if (attachment["title_link"]) {
  325. titleBlock.href = attachment["title_link"];
  326. }
  327. titleBlock.className = R.klass.msg.attachment.title;
  328. } else {
  329. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  330. }
  331. //Author
  332. authorName.target = "_blank";
  333. authorBlock.className = R.klass.msg.author;
  334. if (attachment["author_name"]) {
  335. authorName.innerHTML = formatSlackText(attachment["author_name"]);
  336. authorName.href = attachment["author_link"] || "";
  337. authorName.className = R.klass.msg.authorname;
  338. authorImg.className = R.klass.msg.authorAvatar;
  339. if (attachment["author_icon"])
  340. authorImg.src = attachment["author_icon"];
  341. else
  342. authorImg.classList.add(R.klass.hidden);
  343. } else {
  344. authorBlock.classList.add(R.klass.hidden);
  345. }
  346. //Text
  347. textDom.innerHTML = formatSlackText(attachment["text"] || "");
  348. textDom.klassName = R.klass.msg.attachment.text;
  349. // Img (small one)
  350. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  351. if (attachment["thumb_url"])
  352. thumbImgDom.src = attachment["thumb_url"];
  353. else
  354. thumbImgDom.classList.add(R.klass.hidden);
  355. //Img (the big one)
  356. imgDom.className = R.klass.msg.attachment.img;
  357. if (attachment["image_url"])
  358. imgDom.src = attachment["image_url"];
  359. else
  360. imgDom.classList.add(R.klass.hidden);
  361. //Footer
  362. footerBlock.className = R.klass.msg.attachment.footer;
  363. footerText.className = R.klass.msg.attachment.footerText;
  364. footerIcon.className = R.klass.msg.attachment.footerIcon;
  365. if (attachment["footer"]) {
  366. footerText.innerHTML = formatSlackText(attachment["footer"]);
  367. if (attachment["footer_icon"])
  368. footerIcon.src = attachment["footer_icon"];
  369. else
  370. footerIcon.classList.add(R.klass.hidden);
  371. } else {
  372. footerIcon.classList.add(R.klass.hidden);
  373. footerText.classList.add(R.klass.hidden);
  374. }
  375. //Ts
  376. footerTs.className = R.klass.msg.ts;
  377. if (attachment["ts"])
  378. footerTs.textContent = new Date(parseFloat(attachment["ts"])).toString();
  379. else
  380. footerTs.classList.add(R.klass.hidden);
  381. // TODO Field [ {title, value, short } ]
  382. // TODO actions (button stuff)
  383. authorBlock.appendChild(authorImg);
  384. authorBlock.appendChild(authorName);
  385. textBlock.appendChild(textDom);
  386. textBlock.appendChild(thumbImgDom);
  387. footerBlock.appendChild(footerIcon);
  388. footerBlock.appendChild(footerText);
  389. footerBlock.appendChild(footerTs);
  390. attachmentBlock.appendChild(titleBlock);
  391. attachmentBlock.appendChild(authorBlock);
  392. attachmentBlock.appendChild(textBlock);
  393. attachmentBlock.appendChild(imgDom);
  394. attachmentBlock.appendChild(footerBlock);
  395. rootDom.appendChild(pretext);
  396. rootDom.appendChild(attachmentBlock);
  397. console.log(attachment);
  398. return rootDom;
  399. }
  400. /**
  401. * @param {string} channelId
  402. * @param {SlackMessage} msg
  403. * @param {boolean=} skipAttachment
  404. * @return {Element}
  405. **/
  406. function doCreateMeMessageDom(channelId, msg, skipAttachment) {
  407. var dom = doCreateMessageDom(channelId, msg, skipAttachment);
  408. dom.classList.add(R.klass.msg.meMessage);
  409. return dom;
  410. }
  411. /**
  412. * @param {string} channelId
  413. * @param {SlackMessage} msg
  414. * @param {boolean=} skipAttachment
  415. * @return {Element}
  416. **/
  417. function createMessageDom(channelId, msg, skipAttachment) {
  418. if (msg.subtype === "me_message") {
  419. return doCreateMeMessageDom(channelId, msg, skipAttachment);
  420. }
  421. return doCreateMessageDom(channelId, msg, skipAttachment);
  422. }
  423. function updateTitle() {
  424. var hasUnread = 0
  425. ,hasHl = 0
  426. ,title;
  427. for (var i in UNREAD_CHANS) {
  428. if (UNREAD_CHANS.hasOwnProperty(i)) {
  429. hasUnread += UNREAD_CHANS[i].unread;
  430. hasHl += UNREAD_CHANS[i].hl;
  431. }
  432. }
  433. if (hasHl) {
  434. title = "(!" +hasHl;
  435. }
  436. if (hasUnread) {
  437. title = (title ? (title+" - ") : "(") +hasUnread;
  438. }
  439. if (title)
  440. title += ") " +SLACK.context.team.name;
  441. else
  442. title = SLACK.context.team.name;
  443. document.title = title;
  444. }
  445. function onRoomUpdated() {
  446. var chatFrag = document.createDocumentFragment()
  447. ,currentRoomId = SELECTED_ROOM.id;
  448. document.getElementById(R.id.currentRoom.content).textContent = "";
  449. if (SLACK.history[currentRoomId])
  450. SLACK.history[currentRoomId].messages.forEach(function(msg) {
  451. if (msg.type === "message") {
  452. var dom = createMessageDom(currentRoomId, msg);
  453. chatFrag.appendChild(dom);
  454. }
  455. });
  456. var content = document.getElementById(R.id.currentRoom.content);
  457. content.appendChild(chatFrag);
  458. //TODO scroll lock
  459. content.scrollTop = content.scrollHeight -content.clientHeight;
  460. }
  461. function onChanClick(e) {
  462. while (e.target !== e.currentTarget && e.target) {
  463. if (e.target.classList.contains(R.klass.chatList.entry)) {
  464. var room = (SLACK.context.channels[e.target.id] ||
  465. SLACK.context.ims[e.target.id] ||
  466. SLACK.context.groups[e.target.id]);
  467. if (room && room !== SELECTED_ROOM) {
  468. selectRoom(room);
  469. }
  470. return;
  471. }
  472. e.target = e.target.parentElement;
  473. }
  474. }
  475. function chatClickDelegate(e) {
  476. var target = e.target
  477. ,getMessageId = function(e, target) {
  478. target = target || e.target;
  479. while (target !== e.currentTarget && target) {
  480. if (target.classList.contains(R.klass.msg.item)) {
  481. return target.id;
  482. }
  483. target = target.parentElement;
  484. }
  485. };
  486. while (target !== e.currentTarget && target) {
  487. if (target.classList.contains(R.klass.msg.hover.container)) {
  488. return;
  489. } else if (target.classList.contains(R.klass.msg.hover.reply)) {
  490. var messageId = getMessageId(e, target);
  491. if (messageId) {
  492. messageId = parseFloat(messageId.split("_")[1]);
  493. var history = SLACK.history[SELECTED_ROOM.id].messages;
  494. for (var i =0, histLen =history.length; i < histLen && history[i].ts <= messageId; i++) {
  495. if (history[i].ts === messageId) {
  496. if (REPLYING_TO !== history[i]) {
  497. REPLYING_TO = history[i];
  498. onReplyingToUpdated();
  499. }
  500. return;
  501. }
  502. }
  503. }
  504. return;
  505. }
  506. target = target.parentElement;
  507. }
  508. }
  509. function focusInput() {
  510. document.getElementById(R.id.message.input).focus();
  511. }
  512. document.addEventListener('DOMContentLoaded', function() {
  513. document.getElementById(R.id.chatList).addEventListener("click", onChanClick);
  514. document.getElementById(R.id.currentRoom.content).addEventListener("click", chatClickDelegate);
  515. document.getElementById(R.id.message.file.cancel).addEventListener("click", function(e) {
  516. e.preventDefault();
  517. document.getElementById(R.id.message.file.error).classList.add(R.klass.hidden);
  518. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  519. document.getElementById(R.id.message.file.fileInput).value = "";
  520. return false;
  521. });
  522. document.getElementById(R.id.message.file.form).addEventListener("submit", function(e) {
  523. e.preventDefault();
  524. var fileInput = document.getElementById(R.id.message.file.fileInput)
  525. ,filename = fileInput.value;
  526. if (filename) {
  527. filename = filename.substr(filename.lastIndexOf('\\') +1);
  528. uploadFile(SELECTED_ROOM, filename, fileInput.files[0], function(errorMsg) {
  529. var error = document.getElementById(R.id.message.file.error);
  530. if (errorMsg) {
  531. error.textContent = errorMsg;
  532. error.classList.remove(R.klass.hidden);
  533. } else {
  534. error.classList.add(R.klass.hidden);
  535. document.getElementById(R.id.message.file.fileInput).value = "";
  536. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  537. }
  538. });
  539. }
  540. return false;
  541. });
  542. document.getElementById(R.id.message.file.bt).addEventListener("click", function(e) {
  543. e.preventDefault();
  544. if (SELECTED_ROOM) {
  545. document.getElementById(R.id.message.file.formContainer).classList.remove(R.klass.hidden);
  546. }
  547. return false;
  548. });
  549. document.getElementById(R.id.message.form).addEventListener("submit", function(e) {
  550. e.preventDefault();
  551. var input =document.getElementById(R.id.message.input);
  552. if (SELECTED_ROOM && input.value) {
  553. sendMsg(SELECTED_ROOM, input.value, REPLYING_TO);
  554. input.value = "";
  555. if (REPLYING_TO) {
  556. REPLYING_TO = null;
  557. onReplyingToUpdated();
  558. }
  559. }
  560. focusInput();
  561. return false;
  562. });
  563. window.addEventListener('blur', function() {
  564. window.hasFocus = false;
  565. });
  566. window.addEventListener('focus', function() {
  567. window.hasFocus = true;
  568. if (SELECTED_ROOM)
  569. markRoomAsRead(SELECTED_ROOM);
  570. focusInput();
  571. });
  572. window.hasFocus = true;
  573. startPolling();
  574. });