1
0

ui.js 24 KB

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