ui.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. if (SELECTED_CONTEXT) {
  88. createContextBackground(SELECTED_CONTEXT.getChatContext().team.id, SELECTED_CONTEXT.getChatContext().users, function(imgData) {
  89. document.getElementById(R.id.context).style.backgroundImage = 'url(' +imgData +')';
  90. });
  91. }
  92. }
  93. function onTypingUpdated() {
  94. DATA.context.foreachContext(function(ctx) {
  95. var typing = ctx.typing;
  96. for (var chanId in ctx.self.channels) {
  97. if (!ctx.self.channels[chanId].archived) {
  98. var chanDom = document.getElementById("room_" +chanId);
  99. if (typing[chanId])
  100. chanDom.classList.add(R.klass.chatList.typing);
  101. else
  102. chanDom.classList.remove(R.klass.chatList.typing);
  103. }
  104. }
  105. for (var userId in ctx.users) {
  106. var ims = ctx.users[userId].privateRoom;
  107. if (ims && !ims.archived) {
  108. var userDom = document.getElementById("room_" +ims.id);
  109. if (userDom) {
  110. if (typing[ims.id])
  111. userDom.classList.add(R.klass.chatList.typing);
  112. else
  113. userDom.classList.remove(R.klass.chatList.typing);
  114. }
  115. }
  116. }
  117. });
  118. updateTypingChat();
  119. }
  120. function updateTypingChat() {
  121. var typing;
  122. document.getElementById(R.id.typing).textContent = "";
  123. if (SELECTED_CONTEXT && SELECTED_ROOM && (typing = SELECTED_CONTEXT.getChatContext().typing[SELECTED_ROOM.id])) {
  124. var areTyping = document.createDocumentFragment(),
  125. isOutOfSync = false;
  126. for (var i in typing) {
  127. var member = DATA.context.getUser(i);
  128. if (member)
  129. areTyping.appendChild(makeUserIsTypingDom(member));
  130. else
  131. isOutOfSync = true;
  132. }
  133. if (isOutOfSync)
  134. outOfSync();
  135. document.getElementById(R.id.typing).appendChild(areTyping);
  136. }
  137. }
  138. function onNetworkStateUpdated(isNetworkWorking) {
  139. if (isNetworkWorking)
  140. document.body.classList.remove(R.klass.noNetwork);
  141. else
  142. document.body.classList.add(R.klass.noNetwork);
  143. updateTitle();
  144. }
  145. function onRoomSelected() {
  146. var name = SELECTED_ROOM.name || (SELECTED_ROOM.user ? SELECTED_ROOM.user.getName() : undefined);
  147. if (!name) {
  148. console.error("No name provided for ", SELECTED_ROOM);
  149. /** @type {Array.<string>} */
  150. var members = [];
  151. for (var userId in SELECTED_ROOM.users)
  152. members.push(SELECTED_ROOM.users[userId].getName());
  153. name = members.join(", ");
  154. }
  155. var roomLi = document.getElementById("room_" +SELECTED_ROOM.id);
  156. document.getElementById(R.id.currentRoom.title).textContent = name;
  157. onRoomUpdated();
  158. focusInput();
  159. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  160. markRoomAsRead(SELECTED_ROOM);
  161. if (REPLYING_TO) {
  162. REPLYING_TO = null;
  163. onReplyingToUpdated();
  164. }
  165. if (EDITING) {
  166. EDITING = null;
  167. onReplyingToUpdated();
  168. }
  169. updateTypingChat();
  170. }
  171. function onReplyingToUpdated() {
  172. if (REPLYING_TO) {
  173. document.body.classList.add(R.klass.replyingTo);
  174. var domParent = document.getElementById(R.id.message.replyTo),
  175. closeLink = document.createElement("a");
  176. closeLink.addEventListener("click", function() {
  177. REPLYING_TO = null;
  178. onReplyingToUpdated();
  179. });
  180. closeLink.className = R.klass.msg.replyTo.close;
  181. closeLink.textContent = 'x';
  182. domParent.textContent = "";
  183. domParent.appendChild(closeLink);
  184. domParent.appendChild(REPLYING_TO.duplicateDom());
  185. focusInput();
  186. } else {
  187. document.body.classList.remove(R.klass.replyingTo);
  188. document.getElementById(R.id.message.replyTo).textContent = "";
  189. focusInput();
  190. }
  191. }
  192. function onEditingUpdated() {
  193. if (EDITING) {
  194. document.body.classList.add(R.klass.replyingTo);
  195. var domParent = document.getElementById(R.id.message.replyTo),
  196. closeLink = document.createElement("a");
  197. closeLink.addEventListener("click", function() {
  198. EDITING = null;
  199. onEditingUpdated();
  200. });
  201. closeLink.className = R.klass.msg.replyTo.close;
  202. closeLink.textContent = 'x';
  203. domParent.textContent = "";
  204. domParent.appendChild(closeLink);
  205. domParent.appendChild(EDITING.duplicateDom());
  206. document.getElementById(R.id.message.input).value = EDITING.text;
  207. focusInput();
  208. } else {
  209. document.body.classList.remove(R.klass.replyingTo);
  210. document.getElementById(R.id.message.replyTo).textContent = "";
  211. focusInput();
  212. }
  213. }
  214. /**
  215. * @param {string} chanId
  216. * @param {string} msgId
  217. * @param {string} reaction
  218. **/
  219. window['toggleReaction'] = function(chanId, msgId, reaction) {
  220. var hist = DATA.history[chanId],
  221. msg,
  222. ctx;
  223. if ((hist = DATA.history[chanId]) && (msg = hist.getMessageById(msgId)) && (ctx = DATA.context.getChannelContext(chanId))) {
  224. if (msg.hasReactionForUser(reaction, ctx.getChatContext().self.id)) {
  225. removeReaction(chanId, msgId, reaction);
  226. } else {
  227. addReaction(chanId, msgId, reaction);
  228. }
  229. }
  230. };
  231. /**
  232. * @param {number} unreadhi
  233. * @param {number} unread
  234. **/
  235. function setFavicon(unreadhi, unread) {
  236. if (!unreadhi && !unread)
  237. document.getElementById(R.id.favicon).href = "favicon_ok.png";
  238. else
  239. document.getElementById(R.id.favicon).href = "favicon.png?h="+unreadhi+"&m="+unread;
  240. }
  241. function setNetErrorFavicon() {
  242. document.getElementById(R.id.favicon).href = "favicon_err.png";
  243. }
  244. function updateTitle() {
  245. var hasHl = HIGHLIGHTED_CHANS.length,
  246. title = "";
  247. if (NEXT_RETRY) {
  248. title = '!' +locale.netErrorShort +' - Mimouchat';
  249. setNetErrorFavicon();
  250. } else if (hasHl) {
  251. title = "(!" +hasHl +")";
  252. setFavicon(hasHl, hasHl);
  253. } else {
  254. var hasUnread = 0;
  255. DATA.context.foreachChannels(function(i) {
  256. if (i.lastMsg > i.lastRead)
  257. hasUnread++;
  258. });
  259. if (hasUnread)
  260. title = "(" +hasUnread +")";
  261. setFavicon(0, hasUnread);
  262. }
  263. if (!title.length && SELECTED_ROOM)
  264. title = SELECTED_ROOM.name;
  265. title = title.length ? title : "Mimouchat";
  266. document.title = title;
  267. setNativeTitle(title);
  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. updateTitle();
  355. if (window.hasFocus)
  356. markRoomAsRead(SELECTED_ROOM);
  357. }
  358. function onMsgClicked(target, msg) {
  359. if (target.classList.contains(R.klass.msg.hover.reply)) {
  360. if (EDITING) {
  361. EDITING = null;
  362. onEditingUpdated();
  363. }
  364. if (REPLYING_TO !== msg) {
  365. REPLYING_TO = msg;
  366. onReplyingToUpdated();
  367. }
  368. } else if (target.classList.contains(R.klass.msg.hover.reaction)) {
  369. var currentRoomId = SELECTED_ROOM.id,
  370. currentMsgId = msg.id;
  371. EMOJI_BAR.spawn(document.body, SELECTED_CONTEXT, function(emoji) {
  372. if (emoji)
  373. addReaction(currentRoomId, currentMsgId, emoji);
  374. });
  375. } else if (target.classList.contains(R.klass.msg.hover.edit)) {
  376. if (REPLYING_TO) {
  377. REPLYING_TO = null;
  378. onReplyingToUpdated();
  379. }
  380. if (EDITING !== msg) {
  381. EDITING = msg;
  382. onEditingUpdated();
  383. }
  384. } else if (target.classList.contains(R.klass.msg.hover.star)) {
  385. if (msg.starred)
  386. unstarMsg(SELECTED_ROOM, msg);
  387. else
  388. starMsg(SELECTED_ROOM, msg);
  389. } else if (target.classList.contains(R.klass.msg.hover.pin)) {
  390. if (msg.pinned)
  391. unpinMsg(SELECTED_ROOM, msg);
  392. else
  393. pinMsg(SELECTED_ROOM, msg);
  394. } else if (target.classList.contains(R.klass.msg.hover.remove)) {
  395. //TODO prompt confirm
  396. if (REPLYING_TO) {
  397. REPLYING_TO = null;
  398. onReplyingToUpdated();
  399. }
  400. if (EDITING) {
  401. EDITING = null;
  402. onEditingUpdated();
  403. }
  404. removeMsg(SELECTED_ROOM, msg);
  405. }
  406. }
  407. /**
  408. * @param {Room} channel
  409. * @param {string} input
  410. * @param {boolean} isMeMessage
  411. **/
  412. function displayTmpMessage(channel, input, isMeMessage) {
  413. var obj = {
  414. channel: channel.id,
  415. text: input.trim(),
  416. isMe: isMeMessage,
  417. dom: createTmpMsgDom(input, isMeMessage)
  418. };
  419. PENDING_MESSAGES.push(obj);
  420. if (channel === SELECTED_ROOM)
  421. onRoomUpdated();
  422. return obj;
  423. }
  424. function chatClickDelegate(e) {
  425. var target = e.target,
  426. getMessageId = function(e, target) {
  427. target = target || e.target;
  428. while (target !== e.currentTarget && target) {
  429. if (target.id && target.classList.contains(R.klass.msg.item)) {
  430. return target.id;
  431. }
  432. target = target.parentElement;
  433. }
  434. };
  435. while (target !== e.currentTarget && target) {
  436. if (target.classList.contains(R.klass.msg.hover.container)) {
  437. return;
  438. }
  439. var messageId,
  440. msg;
  441. if (target.parentElement && target.classList.contains(R.klass.msg.attachment.actionItem)) {
  442. var attachmentIndex = target.dataset["attachmentIndex"],
  443. actionIndex = target.dataset["actionIndex"];
  444. messageId = getMessageId(e, target);
  445. if (messageId && attachmentIndex !== undefined && actionIndex !== undefined) {
  446. messageId = messageId.substr(messageId.lastIndexOf("_") +1);
  447. msg = DATA.history[SELECTED_ROOM.id].getMessageById(messageId);
  448. if (msg && msg.attachments[attachmentIndex] && msg.attachments[attachmentIndex].actions && msg.attachments[attachmentIndex].actions[actionIndex]) {
  449. confirmAction(SELECTED_ROOM.id, msg, msg.attachments[attachmentIndex], msg.attachments[attachmentIndex].actions[actionIndex]);
  450. }
  451. return;
  452. }
  453. }
  454. if (target.parentElement && target.parentElement.classList.contains(R.klass.msg.hover.container)) {
  455. if ((messageId = getMessageId(e, target))) {
  456. messageId = messageId.substr(messageId.lastIndexOf("_") +1);
  457. msg = DATA.history[SELECTED_ROOM.id].getMessageById(messageId);
  458. if (msg)
  459. onMsgClicked(target, msg);
  460. }
  461. return;
  462. }
  463. target = target.parentElement;
  464. }
  465. }
  466. function confirmAction(roomId, msg, attachmentObject, actionObject) {
  467. var confirmed = function() {
  468. var payload = getActionPayload(roomId, msg, attachmentObject, actionObject);
  469. sendCommand(payload, msg.userId);
  470. };
  471. if (actionObject["confirm"]) {
  472. (new ConfirmDialog(actionObject["confirm"]["title"], actionObject["confirm"]["text"]))
  473. .setButtonText(actionObject["confirm"]["ok_text"], actionObject["confirm"]["dismiss_text"])
  474. .onConfirm(confirmed)
  475. .spawn();
  476. } else {
  477. confirmed();
  478. }
  479. }
  480. function updateAuthorAvatarImsOffset() {
  481. if (CONFIG.isDisplayAvatars()) {
  482. var chatDom = document.getElementById(R.id.currentRoom.content),
  483. chatTop = chatDom.getBoundingClientRect().top;
  484. MSG_GROUPS.forEach(function(group) {
  485. var imgDom = group.authorImgWrapper,
  486. imgSize = imgDom.clientHeight,
  487. domRect = group.getBoundingClientRect(),
  488. _top = 0;
  489. imgDom.style.top = Math.max(0, Math.min(chatTop -domRect.top, domRect.height -imgSize -(imgSize /2))) +"px";
  490. });
  491. }
  492. }
  493. function onEmojiReady() {
  494. var emojiButton = document.getElementById(R.id.message.emoji);
  495. invalidateAllMessages();
  496. EMOJI_BAR.reset();
  497. if ('makeEmoji' in window) {
  498. emojiButton.style.backgroundImage = 'url("smile.svg")';
  499. emojiButton.classList.remove(R.klass.hidden);
  500. } else {
  501. emojiButton.classList.add(R.klass.hidden);
  502. }
  503. }
  504. function toggleMenu() {
  505. var menu = document.getElementById(R.id.context);
  506. if (menu.classList.contains(R.klass.opened))
  507. menu.classList.remove(R.klass.opened);
  508. else
  509. menu.classList.add(R.klass.opened);
  510. }
  511. document.addEventListener('DOMContentLoaded', function() {
  512. initLang();
  513. // FIXME load config
  514. initHljs();
  515. initMsgInput();
  516. var chanSearch = document.getElementById(R.id.chanFilter);
  517. chanSearch.addEventListener("input", filterChanList);
  518. chanSearch.addEventListener("blur", filterChanList);
  519. document.getElementById(R.id.currentRoom.content).addEventListener("click", chatClickDelegate);
  520. window.addEventListener("hashchange", function(e) {
  521. if (document.location.hash && document.location.hash[0] === '#') {
  522. setRoomFromHashBang();
  523. }
  524. });
  525. document.addEventListener("mouseover", function(e) {
  526. var t = e.target;
  527. if (roomInfo.isParentOf(t)) {
  528. roomInfo.cancelHide();
  529. return;
  530. }
  531. while (t && t !== this) {
  532. if (t.nodeName === 'A') {
  533. var href = t.href,
  534. sepPosition = href.indexOf('#');
  535. if (sepPosition >= 0) {
  536. href = href.substr(sepPosition +1);
  537. var channelCtx = DATA.context.getChannelContext(href);
  538. if (channelCtx) {
  539. roomInfo.populate(channelCtx, channelCtx.getChatContext().channels[href]).show(t);
  540. return;
  541. }
  542. channelCtx = DATA.context.getUserContext(href);
  543. if (channelCtx) {
  544. var room = channelCtx.getChatContext().users[href].privateRoom;
  545. if (room) {
  546. roomInfo.populate(channelCtx, room).show(t);
  547. return;
  548. }
  549. }
  550. }
  551. }
  552. t = t.parentElement;
  553. }
  554. roomInfo.hideDelayed();
  555. });
  556. document.getElementById(R.id.contextButton).addEventListener("click", function(e) {
  557. toggleMenu();
  558. });
  559. document.getElementById(R.id.currentRoom.starButton).addEventListener("click", function(e) {
  560. e.preventDefault();
  561. if (SELECTED_ROOM) {
  562. if (SELECTED_ROOM.starred) {
  563. unstarChannel(SELECTED_ROOM);
  564. } else {
  565. starChannel(SELECTED_ROOM);
  566. }
  567. }
  568. return false;
  569. });
  570. document.getElementById(R.id.message.file.cancel).addEventListener("click", function(e) {
  571. e.preventDefault();
  572. document.getElementById(R.id.message.file.error).classList.add(R.klass.hidden);
  573. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  574. document.getElementById(R.id.message.file.fileInput).value = "";
  575. return false;
  576. });
  577. document.getElementById(R.id.settings.logout).addEventListener("click", logout);
  578. document.getElementById(R.id.settings.menuButton).addEventListener("click", function(e) {
  579. e.preventDefault();
  580. Settings.display().setClosable(true);
  581. });
  582. document.getElementById(R.id.message.file.form).addEventListener("submit", function(e) {
  583. e.preventDefault();
  584. var fileInput = document.getElementById(R.id.message.file.fileInput),
  585. filename = fileInput.value;
  586. if (filename) {
  587. filename = filename.substr(filename.lastIndexOf('\\') +1);
  588. uploadFile(SELECTED_ROOM, filename, fileInput.files[0], function(errorMsg) {
  589. var error = document.getElementById(R.id.message.file.error);
  590. if (errorMsg) {
  591. error.textContent = errorMsg;
  592. error.classList.remove(R.klass.hidden);
  593. } else {
  594. error.classList.add(R.klass.hidden);
  595. document.getElementById(R.id.message.file.fileInput).value = "";
  596. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  597. }
  598. });
  599. }
  600. return false;
  601. });
  602. document.getElementById(R.id.message.file.bt).addEventListener("click", function(e) {
  603. e.preventDefault();
  604. if (SELECTED_ROOM) {
  605. document.getElementById(R.id.message.file.formContainer).classList.remove(R.klass.hidden);
  606. }
  607. return false;
  608. });
  609. document.getElementById(R.id.message.submitButton).addEventListener("click", function(e) {
  610. e.preventDefault();
  611. onMsgFormSubmit();
  612. return false;
  613. });
  614. document.getElementById(R.id.message.form).addEventListener("submit", function(e) {
  615. e.preventDefault();
  616. onMsgFormSubmit();
  617. return false;
  618. });
  619. window.addEventListener('blur', function() {
  620. window.hasFocus = false;
  621. });
  622. window.addEventListener('focus', function() {
  623. window.hasFocus = true;
  624. lastNotificationSpawn = 0;
  625. if (SELECTED_ROOM)
  626. markRoomAsRead(SELECTED_ROOM);
  627. focusInput();
  628. });
  629. document.getElementById(R.id.currentRoom.content).addEventListener('scroll', updateAuthorAvatarImsOffset);
  630. window.hasFocus = true;
  631. var emojiButton = document.getElementById(R.id.message.emoji);
  632. emojiButton.addEventListener("click", function() {
  633. if (SELECTED_CONTEXT)
  634. EMOJI_BAR.spawn(document.body, SELECTED_CONTEXT.getChatContext(), function(e) {
  635. if (e) document.getElementById(R.id.message.input).value += ":"+e+":";
  636. focusInput();
  637. });
  638. });
  639. if (isNative()) {
  640. __native.readSmsPermission();
  641. document.body.classList.add(R.klass.native);
  642. }
  643. if (!IS_LOCAL)
  644. startPolling();
  645. });
  646. function onMsgFormSubmit() {
  647. var input = document.getElementById(R.id.message.input);
  648. if (SELECTED_ROOM && input.value) {
  649. if (onTextEntered(input.value)) {
  650. input.value = "";
  651. if (REPLYING_TO) {
  652. REPLYING_TO = null;
  653. onReplyingToUpdated();
  654. }
  655. if (EDITING) {
  656. EDITING = null;
  657. onReplyingToUpdated();
  658. }
  659. document.getElementById(R.id.message.slashComplete).textContent = '';
  660. updateInputRowCount(input);
  661. }
  662. }
  663. focusInput();
  664. }