1
0

ui.js 24 KB

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