ui.js 24 KB

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