ui.js 23 KB

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