1
0

ui.js 25 KB

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