ui.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. var
  2. /**
  3. * Minimum time between 2 notifications (ms)
  4. * @const
  5. * @type {number}
  6. **/
  7. NOTIFICATION_COOLDOWN = 30 * 1000, //30 sec
  8. /**
  9. * Maximum time the notification will stay visible (ms)
  10. * @const
  11. * @type {number}
  12. **/
  13. NOTIFICATION_DELAY = 5 * 1000, // 5 sec
  14. MSG_GROUPS = [],
  15. /** @type {number} */
  16. lastNotificationSpawn = 0;
  17. function onContextUpdated() {
  18. var chanListFram = document.createDocumentFragment(),
  19. sortedChans = DATA.context.getChannelIds(function(chan) {
  20. return !chan.archived && chan.isMember !== false;
  21. }),
  22. starred = [],
  23. channels = [],
  24. privs = [],
  25. priv = [],
  26. chanNames = {};
  27. sortedChans.sort(function(a, b) {
  28. if (a[0] !== b[0]) {
  29. return a[0] - b[0];
  30. }
  31. var aChanCtx = DATA.context.getChannelContext(a).getChatContext(),
  32. bChanCtx = DATA.context.getChannelContext(b).getChatContext(),
  33. aChan = aChanCtx.channels[a],
  34. bChan = bChanCtx.channels[b];
  35. if (aChan.name === bChan.name) {
  36. chanNames[aChan.id] = locale.chanName(aChanCtx.team.name, aChan.name);
  37. chanNames[bChan.id] = locale.chanName(bChanCtx.team.name, bChan.name);
  38. return aChanCtx.team.name.localeCompare(bChanCtx.team.name);
  39. }
  40. return aChan.name.localeCompare(bChan.name);
  41. });
  42. sortedChans.forEach(function(chanId) {
  43. var chan = DATA.context.getChannel(chanId),
  44. chanListItem;
  45. if (chan instanceof PrivateMessageRoom) {
  46. if (!chan.user.deleted) {
  47. if ((chanListItem = createImsListItem(chan, chanNames[chan.id]))) {
  48. if (chan.starred)
  49. starred.push(chanListItem);
  50. else
  51. priv.push(chanListItem);
  52. }
  53. }
  54. } else {
  55. if ((chanListItem = createChanListItem(chan, chanNames[chan.id]))) {
  56. if (chan.starred)
  57. starred.push(chanListItem);
  58. else if (chan.isPrivate)
  59. privs.push(chanListItem);
  60. else
  61. channels.push(chanListItem);
  62. }
  63. }
  64. });
  65. if (starred.length)
  66. chanListFram.appendChild(createChanListHeader(locale.starred));
  67. starred.forEach(function(dom) {
  68. chanListFram.appendChild(dom);
  69. });
  70. if (channels.length)
  71. chanListFram.appendChild(createChanListHeader(locale.channels));
  72. channels.forEach(function(dom) {
  73. chanListFram.appendChild(dom);
  74. });
  75. privs.forEach(function(dom) {
  76. chanListFram.appendChild(dom);
  77. });
  78. if (priv.length)
  79. chanListFram.appendChild(createChanListHeader(locale.privateMessageRoom));
  80. priv.forEach(function(dom) {
  81. chanListFram.appendChild(dom);
  82. });
  83. document.getElementById(R.id.chanList).textContent = "";
  84. document.getElementById(R.id.chanList).appendChild(chanListFram);
  85. filterChanList.apply(document.getElementById(R.id.chanFilter));
  86. setRoomFromHashBang();
  87. updateTitle();
  88. if (SELECTED_CONTEXT) {
  89. createContextBackground(SELECTED_CONTEXT.getChatContext().team.id, SELECTED_CONTEXT.getChatContext().users, function(imgData) {
  90. document.getElementById(R.id.context).style.backgroundImage = 'url(' +imgData +')';
  91. });
  92. }
  93. }
  94. function onTypingUpdated() {
  95. DATA.context.foreachContext(function(ctx) {
  96. var typing = ctx.typing;
  97. for (var chanId in ctx.self.channels) {
  98. if (!ctx.self.channels[chanId].archived) {
  99. var chanDom = document.getElementById("room_" +chanId);
  100. if (typing[chanId])
  101. chanDom.classList.add(R.klass.chatList.typing);
  102. else
  103. chanDom.classList.remove(R.klass.chatList.typing);
  104. }
  105. }
  106. for (var userId in ctx.users) {
  107. var ims = ctx.users[userId].privateRoom;
  108. if (ims && !ims.archived) {
  109. var userDom = document.getElementById("room_" +ims.id);
  110. if (userDom) {
  111. if (typing[ims.id])
  112. userDom.classList.add(R.klass.chatList.typing);
  113. else
  114. userDom.classList.remove(R.klass.chatList.typing);
  115. }
  116. }
  117. }
  118. });
  119. updateTypingChat();
  120. }
  121. function updateTypingChat() {
  122. var typing;
  123. document.getElementById(R.id.typing).textContent = "";
  124. if (SELECTED_CONTEXT && SELECTED_ROOM && (typing = SELECTED_CONTEXT.getChatContext().typing[SELECTED_ROOM.id])) {
  125. var areTyping = document.createDocumentFragment(),
  126. isOutOfSync = false;
  127. for (var i in typing) {
  128. var member = DATA.context.getUser(i);
  129. if (member)
  130. areTyping.appendChild(makeUserIsTypingDom(member));
  131. else
  132. isOutOfSync = true;
  133. }
  134. if (isOutOfSync)
  135. outOfSync();
  136. document.getElementById(R.id.typing).appendChild(areTyping);
  137. }
  138. }
  139. function onNetworkStateUpdated(isNetworkWorking) {
  140. if (isNetworkWorking)
  141. document.body.classList.remove(R.klass.noNetwork);
  142. else
  143. document.body.classList.add(R.klass.noNetwork);
  144. updateTitle();
  145. }
  146. function onRoomSelected() {
  147. var name = SELECTED_ROOM.name || (SELECTED_ROOM.user ? SELECTED_ROOM.user.getName() : undefined);
  148. if (!name) {
  149. console.error("No name provided for ", SELECTED_ROOM);
  150. /** @type {Array.<string>} */
  151. var members = [];
  152. for (var userId in SELECTED_ROOM.users)
  153. members.push(SELECTED_ROOM.users[userId].getName());
  154. name = members.join(", ");
  155. }
  156. var roomLi = document.getElementById("room_" +SELECTED_ROOM.id);
  157. document.getElementById(R.id.currentRoom.title).textContent = name;
  158. onRoomUpdated();
  159. focusInput();
  160. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  161. markRoomAsRead(SELECTED_ROOM);
  162. if (REPLYING_TO) {
  163. REPLYING_TO = null;
  164. onReplyingToUpdated();
  165. }
  166. if (EDITING) {
  167. EDITING = null;
  168. onReplyingToUpdated();
  169. }
  170. updateTitle();
  171. updateTypingChat();
  172. }
  173. function onReplyingToUpdated() {
  174. if (REPLYING_TO) {
  175. document.body.classList.add(R.klass.replyingTo);
  176. var domParent = document.getElementById(R.id.message.replyTo),
  177. closeLink = document.createElement("a");
  178. closeLink.addEventListener("click", function() {
  179. REPLYING_TO = null;
  180. onReplyingToUpdated();
  181. });
  182. closeLink.className = R.klass.msg.replyTo.close;
  183. closeLink.textContent = 'x';
  184. domParent.textContent = "";
  185. domParent.appendChild(closeLink);
  186. domParent.appendChild(REPLYING_TO.duplicateDom());
  187. focusInput();
  188. } else {
  189. document.body.classList.remove(R.klass.replyingTo);
  190. document.getElementById(R.id.message.replyTo).textContent = "";
  191. focusInput();
  192. }
  193. }
  194. function onEditingUpdated() {
  195. if (EDITING) {
  196. document.body.classList.add(R.klass.replyingTo);
  197. var domParent = document.getElementById(R.id.message.replyTo),
  198. closeLink = document.createElement("a");
  199. closeLink.addEventListener("click", function() {
  200. EDITING = null;
  201. onEditingUpdated();
  202. });
  203. closeLink.className = R.klass.msg.replyTo.close;
  204. closeLink.textContent = 'x';
  205. domParent.textContent = "";
  206. domParent.appendChild(closeLink);
  207. domParent.appendChild(EDITING.duplicateDom());
  208. document.getElementById(R.id.message.input).value = EDITING.text;
  209. focusInput();
  210. } else {
  211. document.body.classList.remove(R.klass.replyingTo);
  212. document.getElementById(R.id.message.replyTo).textContent = "";
  213. focusInput();
  214. }
  215. }
  216. /**
  217. * @param {string} chanId
  218. * @param {string} msgId
  219. * @param {string} reaction
  220. **/
  221. window['toggleReaction'] = function(chanId, msgId, reaction) {
  222. var hist = DATA.history[chanId],
  223. msg,
  224. ctx;
  225. if ((hist = DATA.history[chanId]) && (msg = hist.getMessageById(msgId)) && (ctx = DATA.context.getChannelContext(chanId))) {
  226. if (msg.hasReactionForUser(reaction, ctx.getChatContext().self.id)) {
  227. removeReaction(chanId, msgId, reaction);
  228. } else {
  229. addReaction(chanId, msgId, reaction);
  230. }
  231. }
  232. };
  233. /**
  234. * @param {number} unreadhi
  235. * @param {number} unread
  236. **/
  237. function setFavicon(unreadhi, unread) {
  238. if (!unreadhi && !unread)
  239. document.getElementById(R.id.favicon).href = "favicon_ok.png";
  240. else
  241. document.getElementById(R.id.favicon).href = "favicon.png?h="+unreadhi+"&m="+unread;
  242. }
  243. function setNetErrorFavicon() {
  244. document.getElementById(R.id.favicon).href = "favicon_err.png";
  245. }
  246. function updateTitle() {
  247. var hasHl = HIGHLIGHTED_CHANS.length,
  248. title = "";
  249. if (NEXT_RETRY) {
  250. title = '!' +locale.netErrorShort +' - Mimouchat';
  251. setNetErrorFavicon();
  252. } else if (hasHl) {
  253. title = "(!" +hasHl +")";
  254. setFavicon(hasHl, hasHl);
  255. } else {
  256. var hasUnread = 0;
  257. DATA.context.foreachChannels(function(i) {
  258. if (i.lastMsg > i.lastRead)
  259. hasUnread++;
  260. });
  261. if (hasUnread)
  262. title = "(" +hasUnread +")";
  263. setFavicon(0, hasUnread);
  264. }
  265. if (!title.length && SELECTED_ROOM)
  266. title = SELECTED_ROOM.name;
  267. document.title = title.length ? title : "Mimouchat";
  268. }
  269. function spawnNotification() {
  270. if (!("Notification" in window))
  271. {}
  272. else if (Notification.permission === "granted") {
  273. var now = Date.now();
  274. if (lastNotificationSpawn + NOTIFICATION_COOLDOWN < now) {
  275. var n = new Notification(locale.newMessage);
  276. lastNotificationSpawn = now;
  277. setTimeout(function() {
  278. n.close();
  279. }, NOTIFICATION_DELAY);
  280. }
  281. }
  282. else if (Notification.permission !== "denied")
  283. Notification.requestPermission();
  284. }
  285. function onRoomUpdated() {
  286. var chatFrag = document.createDocumentFragment(),
  287. currentRoomId = SELECTED_ROOM.id,
  288. prevMsg = null,
  289. firstTsCombo = 0,
  290. prevMsgDom = null,
  291. currentMsgGroupDom;
  292. if (SELECTED_ROOM.starred)
  293. document.getElementById(R.id.mainSection).classList.add(R.klass.starred);
  294. else
  295. document.getElementById(R.id.mainSection).classList.remove(R.klass.starred);
  296. MSG_GROUPS = [];
  297. if (DATA.history[currentRoomId])
  298. DATA.history[currentRoomId].messages.forEach(function(msg) {
  299. if (!msg.removed) {
  300. var dom = msg.getDom(),
  301. newGroupDom = false;
  302. if (prevMsg && prevMsg.userId === msg.userId && msg.userId) {
  303. if (Math.abs(firstTsCombo -msg.ts) < 30 && !(msg instanceof MeMessage))
  304. prevMsgDom.classList.add(R.klass.msg.sameTs);
  305. else
  306. firstTsCombo = msg.ts;
  307. } else {
  308. firstTsCombo = msg.ts;
  309. newGroupDom = true;
  310. }
  311. if ((!prevMsg || prevMsg.ts <= SELECTED_ROOM.lastRead) && msg.ts > SELECTED_ROOM.lastRead)
  312. dom.classList.add(R.klass.msg.firstUnread);
  313. else
  314. dom.classList.remove(R.klass.msg.firstUnread);
  315. if (msg instanceof MeMessage) {
  316. prevMsg = null;
  317. prevMsgDom = null;
  318. firstTsCombo = 0;
  319. newGroupDom = true;
  320. chatFrag.appendChild(dom);
  321. currentMsgGroupDom = null;
  322. } else {
  323. if (newGroupDom || !currentMsgGroupDom) {
  324. currentMsgGroupDom = createMessageGroupDom(DATA.context.getUser(msg.userId), msg.username);
  325. MSG_GROUPS.push(currentMsgGroupDom);
  326. chatFrag.appendChild(currentMsgGroupDom);
  327. }
  328. prevMsg = msg;
  329. prevMsgDom = dom;
  330. currentMsgGroupDom.content.appendChild(dom);
  331. }
  332. } else {
  333. msg.removeDom();
  334. }
  335. });
  336. var isFirstPending = true;
  337. PENDING_MESSAGES.forEach(function(msg) {
  338. if (msg.channel === SELECTED_ROOM.id) {
  339. if (isFirstPending) {
  340. currentMsgGroupDom = createMessageGroupDom(SELECTED_CONTEXT.self, "");
  341. chatFrag.appendChild(currentMsgGroupDom);
  342. isFirstPending = false;
  343. }
  344. currentMsgGroupDom.content.appendChild(msg.dom);
  345. }
  346. });
  347. var content = document.getElementById(R.id.currentRoom.content);
  348. //TODO lazy add dom if needed
  349. content.textContent = "";
  350. content.appendChild(chatFrag);
  351. //TODO scroll lock
  352. content.scrollTop = content.scrollHeight -content.clientHeight;
  353. updateAuthorAvatarImsOffset();
  354. if (window.hasFocus)
  355. markRoomAsRead(SELECTED_ROOM);
  356. }
  357. function onMsgClicked(target, msg) {
  358. if (target.classList.contains(R.klass.msg.hover.reply)) {
  359. if (EDITING) {
  360. EDITING = null;
  361. onEditingUpdated();
  362. }
  363. if (REPLYING_TO !== msg) {
  364. REPLYING_TO = msg;
  365. onReplyingToUpdated();
  366. }
  367. } else if (target.classList.contains(R.klass.msg.hover.reaction)) {
  368. var currentRoomId = SELECTED_ROOM.id,
  369. currentMsgId = msg.id;
  370. EMOJI_BAR.spawn(document.body, SELECTED_CONTEXT, function(emoji) {
  371. if (emoji)
  372. addReaction(currentRoomId, currentMsgId, emoji);
  373. });
  374. } else if (target.classList.contains(R.klass.msg.hover.edit)) {
  375. if (REPLYING_TO) {
  376. REPLYING_TO = null;
  377. onReplyingToUpdated();
  378. }
  379. if (EDITING !== msg) {
  380. EDITING = msg;
  381. onEditingUpdated();
  382. }
  383. } else if (target.classList.contains(R.klass.msg.hover.star)) {
  384. if (msg.starred)
  385. unstarMsg(SELECTED_ROOM, msg);
  386. else
  387. starMsg(SELECTED_ROOM, msg);
  388. } else if (target.classList.contains(R.klass.msg.hover.pin)) {
  389. if (msg.pinned)
  390. unpinMsg(SELECTED_ROOM, msg);
  391. else
  392. pinMsg(SELECTED_ROOM, msg);
  393. } else if (target.classList.contains(R.klass.msg.hover.remove)) {
  394. //TODO prompt confirm
  395. if (REPLYING_TO) {
  396. REPLYING_TO = null;
  397. onReplyingToUpdated();
  398. }
  399. if (EDITING) {
  400. EDITING = null;
  401. onEditingUpdated();
  402. }
  403. removeMsg(SELECTED_ROOM, msg);
  404. }
  405. }
  406. /**
  407. * @param {Room} channel
  408. * @param {string} input
  409. * @param {boolean} isMeMessage
  410. **/
  411. function displayTmpMessage(channel, input, isMeMessage) {
  412. var obj = {
  413. channel: channel.id,
  414. text: input.trim(),
  415. isMe: isMeMessage,
  416. dom: createTmpMsgDom(input, isMeMessage)
  417. };
  418. PENDING_MESSAGES.push(obj);
  419. if (channel === SELECTED_ROOM)
  420. onRoomUpdated();
  421. return obj;
  422. }
  423. function chatClickDelegate(e) {
  424. var target = e.target,
  425. getMessageId = function(e, target) {
  426. target = target || e.target;
  427. while (target !== e.currentTarget && target) {
  428. if (target.id && target.classList.contains(R.klass.msg.item)) {
  429. return target.id;
  430. }
  431. target = target.parentElement;
  432. }
  433. };
  434. while (target !== e.currentTarget && target) {
  435. if (target.classList.contains(R.klass.msg.hover.container)) {
  436. return;
  437. }
  438. var messageId,
  439. msg;
  440. if (target.parentElement && target.classList.contains(R.klass.msg.attachment.actionItem)) {
  441. var attachmentIndex = target.dataset["attachmentIndex"],
  442. actionIndex = target.dataset["actionIndex"];
  443. messageId = getMessageId(e, target);
  444. if (messageId && attachmentIndex !== undefined && actionIndex !== undefined) {
  445. messageId = messageId.substr(messageId.lastIndexOf("_") +1);
  446. msg = DATA.history[SELECTED_ROOM.id].getMessageById(messageId);
  447. if (msg && msg.attachments[attachmentIndex] && msg.attachments[attachmentIndex].actions && msg.attachments[attachmentIndex].actions[actionIndex]) {
  448. confirmAction(SELECTED_ROOM.id, msg, msg.attachments[attachmentIndex], msg.attachments[attachmentIndex].actions[actionIndex]);
  449. }
  450. return;
  451. }
  452. }
  453. if (target.parentElement && target.parentElement.classList.contains(R.klass.msg.hover.container)) {
  454. if ((messageId = getMessageId(e, target))) {
  455. messageId = messageId.substr(messageId.lastIndexOf("_") +1);
  456. msg = DATA.history[SELECTED_ROOM.id].getMessageById(messageId);
  457. if (msg)
  458. onMsgClicked(target, msg);
  459. }
  460. return;
  461. }
  462. target = target.parentElement;
  463. }
  464. }
  465. function confirmAction(roomId, msg, attachmentObject, actionObject) {
  466. var confirmed = function() {
  467. var payload = getActionPayload(roomId, msg, attachmentObject, actionObject);
  468. sendCommand(payload, msg.userId);
  469. };
  470. if (actionObject["confirm"]) {
  471. (new ConfirmDialog(actionObject["confirm"]["title"], actionObject["confirm"]["text"]))
  472. .setButtonText(actionObject["confirm"]["ok_text"], actionObject["confirm"]["dismiss_text"])
  473. .onConfirm(confirmed)
  474. .spawn();
  475. } else {
  476. confirmed();
  477. }
  478. }
  479. function updateAuthorAvatarImsOffset() {
  480. if (CONFIG.isDisplayAvatars()) {
  481. var chatDom = document.getElementById(R.id.currentRoom.content),
  482. chatTop = chatDom.getBoundingClientRect().top;
  483. MSG_GROUPS.forEach(function(group) {
  484. var imgDom = group.authorImgWrapper,
  485. imgSize = imgDom.clientHeight,
  486. domRect = group.getBoundingClientRect(),
  487. _top = 0;
  488. imgDom.style.top = Math.max(0, Math.min(chatTop -domRect.top, domRect.height -imgSize -(imgSize /2))) +"px";
  489. });
  490. }
  491. }
  492. function onEmojiReady() {
  493. var emojiButton = document.getElementById(R.id.message.emoji);
  494. invalidateAllMessages();
  495. EMOJI_BAR.reset();
  496. if ('makeEmoji' in window) {
  497. emojiButton.style.backgroundImage = 'url("smile.svg")';
  498. emojiButton.classList.remove(R.klass.hidden);
  499. } else {
  500. emojiButton.classList.add(R.klass.hidden);
  501. }
  502. }
  503. document.addEventListener('DOMContentLoaded', function() {
  504. initLang();
  505. // FIXME load config
  506. initHljs();
  507. initMsgInput();
  508. var chanSearch = document.getElementById(R.id.chanFilter);
  509. chanSearch.addEventListener("input", filterChanList);
  510. chanSearch.addEventListener("blur", filterChanList);
  511. document.getElementById(R.id.currentRoom.content).addEventListener("click", chatClickDelegate);
  512. window.addEventListener("hashchange", function(e) {
  513. if (document.location.hash && document.location.hash[0] === '#') {
  514. setRoomFromHashBang();
  515. }
  516. });
  517. document.addEventListener("mouseover", function(e) {
  518. var t = e.target;
  519. if (roomInfo.isParentOf(t)) {
  520. roomInfo.cancelHide();
  521. return;
  522. }
  523. while (t && t !== this) {
  524. if (t.nodeName === 'A') {
  525. var href = t.href,
  526. sepPosition = href.indexOf('#');
  527. if (sepPosition >= 0) {
  528. href = href.substr(sepPosition +1);
  529. var channelCtx = DATA.context.getChannelContext(href);
  530. if (channelCtx) {
  531. roomInfo.populate(channelCtx, channelCtx.getChatContext().channels[href]).show(t);
  532. return;
  533. }
  534. channelCtx = DATA.context.getUserContext(href);
  535. if (channelCtx) {
  536. var room = channelCtx.getChatContext().users[href].privateRoom;
  537. if (room) {
  538. roomInfo.populate(channelCtx, room).show(t);
  539. return;
  540. }
  541. }
  542. }
  543. }
  544. t = t.parentElement;
  545. }
  546. roomInfo.hideDelayed();
  547. });
  548. document.getElementById(R.id.contextButton).addEventListener("click", function(e) {
  549. var menu = document.getElementById(R.id.context);
  550. if (menu.classList.contains(R.klass.opened))
  551. menu.classList.remove(R.klass.opened);
  552. else
  553. menu.classList.add(R.klass.opened);
  554. });
  555. document.getElementById(R.id.currentRoom.starButton).addEventListener("click", function(e) {
  556. e.preventDefault();
  557. if (SELECTED_ROOM) {
  558. if (SELECTED_ROOM.starred) {
  559. unstarChannel(SELECTED_ROOM);
  560. } else {
  561. starChannel(SELECTED_ROOM);
  562. }
  563. }
  564. return false;
  565. });
  566. document.getElementById(R.id.message.file.cancel).addEventListener("click", function(e) {
  567. e.preventDefault();
  568. document.getElementById(R.id.message.file.error).classList.add(R.klass.hidden);
  569. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  570. document.getElementById(R.id.message.file.fileInput).value = "";
  571. return false;
  572. });
  573. document.getElementById(R.id.settings.logout).addEventListener("click", logout);
  574. document.getElementById(R.id.settings.menuButton).addEventListener("click", function(e) {
  575. e.preventDefault();
  576. Settings.display().setClosable(true);
  577. });
  578. document.getElementById(R.id.message.file.form).addEventListener("submit", function(e) {
  579. e.preventDefault();
  580. var fileInput = document.getElementById(R.id.message.file.fileInput),
  581. filename = fileInput.value;
  582. if (filename) {
  583. filename = filename.substr(filename.lastIndexOf('\\') +1);
  584. uploadFile(SELECTED_ROOM, filename, fileInput.files[0], function(errorMsg) {
  585. var error = document.getElementById(R.id.message.file.error);
  586. if (errorMsg) {
  587. error.textContent = errorMsg;
  588. error.classList.remove(R.klass.hidden);
  589. } else {
  590. error.classList.add(R.klass.hidden);
  591. document.getElementById(R.id.message.file.fileInput).value = "";
  592. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  593. }
  594. });
  595. }
  596. return false;
  597. });
  598. document.getElementById(R.id.message.file.bt).addEventListener("click", function(e) {
  599. e.preventDefault();
  600. if (SELECTED_ROOM) {
  601. document.getElementById(R.id.message.file.formContainer).classList.remove(R.klass.hidden);
  602. }
  603. return false;
  604. });
  605. document.getElementById(R.id.message.submitButton).addEventListener("click", function(e) {
  606. e.preventDefault();
  607. onMsgFormSubmit();
  608. return false;
  609. });
  610. document.getElementById(R.id.message.form).addEventListener("submit", function(e) {
  611. e.preventDefault();
  612. onMsgFormSubmit();
  613. return false;
  614. });
  615. window.addEventListener('blur', function() {
  616. window.hasFocus = false;
  617. });
  618. window.addEventListener('focus', function() {
  619. window.hasFocus = true;
  620. lastNotificationSpawn = 0;
  621. if (SELECTED_ROOM)
  622. markRoomAsRead(SELECTED_ROOM);
  623. focusInput();
  624. });
  625. document.getElementById(R.id.currentRoom.content).addEventListener('scroll', updateAuthorAvatarImsOffset);
  626. window.hasFocus = true;
  627. var emojiButton = document.getElementById(R.id.message.emoji);
  628. emojiButton.addEventListener("click", function() {
  629. if (SELECTED_CONTEXT)
  630. EMOJI_BAR.spawn(document.body, SELECTED_CONTEXT.getChatContext(), function(e) {
  631. if (e) document.getElementById(R.id.message.input).value += ":"+e+":";
  632. focusInput();
  633. });
  634. });
  635. startPolling();
  636. });
  637. function onMsgFormSubmit() {
  638. var input = document.getElementById(R.id.message.input);
  639. if (SELECTED_ROOM && input.value) {
  640. if (onTextEntered(input.value)) {
  641. input.value = "";
  642. if (REPLYING_TO) {
  643. REPLYING_TO = null;
  644. onReplyingToUpdated();
  645. }
  646. if (EDITING) {
  647. EDITING = null;
  648. onReplyingToUpdated();
  649. }
  650. document.getElementById(R.id.message.slashComplete).textContent = '';
  651. updateInputRowCount(input);
  652. }
  653. }
  654. focusInput();
  655. }