ui.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. var
  2. /** @type {SlackMessage|null} */
  3. REPLYING_TO = null
  4. /**
  5. * Minimum time between 2 notifications (ms)
  6. * @const
  7. * @type {number}
  8. **/
  9. ,NOTIFICATION_COOLDOWN = 30 * 1000 //30 sec
  10. /**
  11. * Maximum time the notification will stay visible (ms)
  12. * @const
  13. * @type {number}
  14. **/
  15. ,NOTIFICATION_DELAY = 5 * 1000 // 5 sec
  16. /** @type {number} */
  17. ,lastNotificationSpawn = 0
  18. ;
  19. /**
  20. * @param {SlackChan|SlackGroup} chan
  21. * @return {Element}
  22. **/
  23. function createChanListItem(chan) {
  24. var dom = document.createElement("li")
  25. ,link = document.createElement("a");
  26. dom.id = chan.id;
  27. link.href = '#' +chan.id;
  28. if (chan.id[0] === 'D')
  29. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeDirect;
  30. else if (chan.id[0] === 'G')
  31. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeGroup;
  32. else if (chan.id[0] === 'C')
  33. dom.className = R.klass.chatList.entry + " " +R.klass.chatList.typeChannel;
  34. link.textContent = chan.name;
  35. dom.appendChild(link);
  36. return dom;
  37. }
  38. /**
  39. * @param {SlackIms} ims
  40. * @return {Element}
  41. **/
  42. function createImsListItem(ims) {
  43. var dom = document.createElement("li")
  44. ,link = document.createElement("a");
  45. dom.id = ims.id;
  46. link.href = '#' +ims.id;
  47. dom.className = R.klass.chatList.entry;
  48. link.textContent = ims.user.name;
  49. dom.appendChild(link);
  50. return dom;
  51. }
  52. function onContextUpdated() {
  53. var chanListFram = document.createDocumentFragment();
  54. var sortedChans = SLACK.context.self ? Object.keys(SLACK.context.self.channels) : [];
  55. sortedChans.sort(function(a, b) {
  56. if (a[0] !== b[0]) {
  57. return a[0] - b[0];
  58. }
  59. return (SLACK.context.channels[a] || SLACK.context.groups[a]).name.localeCompare((SLACK.context.channels[b] || SLACK.context.groups[b]).name);
  60. });
  61. sortedChans.forEach(function(chanId) {
  62. var chan = SLACK.context.channels[chanId] || SLACK.context.groups[chanId]
  63. ,chanListItem = createChanListItem(chan);
  64. if (chanListItem) {
  65. chanListFram.appendChild(chanListItem);
  66. }
  67. });
  68. var sortedUsers = SLACK.context.users ? Object.keys(SLACK.context.users) : [];
  69. sortedUsers.sort(function(a, b) {
  70. return SLACK.context.users[a].name.localeCompare(SLACK.context.users[b].name);
  71. });
  72. sortedUsers.forEach(function(userId) {
  73. var ims = SLACK.context.users[userId].ims
  74. ,imsListItem = createImsListItem(ims);
  75. if (imsListItem) {
  76. chanListFram.appendChild(imsListItem);
  77. }
  78. });
  79. document.getElementById(R.id.chanList).textContent = "";
  80. document.getElementById(R.id.chanList).appendChild(chanListFram);
  81. setRoomFromHashBang();
  82. }
  83. function onNetworkStateUpdated(isNetworkWorking) {
  84. isNetworkWorking ? document.body.classList.remove(R.klass.noNetwork) : document.body.classList.add(R.klass.noNetwork);
  85. }
  86. function onRoomSelected() {
  87. var name = SELECTED_ROOM.name || (SELECTED_ROOM.user ? SELECTED_ROOM.user.name : undefined);
  88. if (!name) {
  89. var members = [];
  90. for (var i in SELECTED_ROOM.members) {
  91. members.push(SELECTED_ROOM.members[i].name);
  92. }
  93. name = members.join(", ");
  94. }
  95. var roomLi = document.getElementById(SELECTED_ROOM.id);
  96. document.getElementById(R.id.currentRoom.title).textContent = name;
  97. onRoomUpdated();
  98. focusInput();
  99. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  100. markRoomAsRead(SELECTED_ROOM);
  101. if (REPLYING_TO) {
  102. REPLYING_TO = null;
  103. onReplyingToUpdated();
  104. }
  105. }
  106. function onReplyingToUpdated() {
  107. if (REPLYING_TO) {
  108. document.body.classList.add(R.klass.replyingTo);
  109. var domParent = document.getElementById(R.id.message.replyTo)
  110. ,closeLink = document.createElement("a");
  111. closeLink.addEventListener("click", function() {
  112. REPLYING_TO = null;
  113. onReplyingToUpdated();
  114. });
  115. closeLink.className = R.klass.msg.replyTo.close;
  116. closeLink.textContent = 'x';
  117. domParent.textContent = "";
  118. domParent.appendChild(closeLink);
  119. domParent.appendChild(createMessageDom("reply_" +SELECTED_ROOM.id, REPLYING_TO, true));
  120. } else {
  121. document.body.classList.remove(R.klass.replyingTo);
  122. }
  123. }
  124. /**
  125. * @param {string} channelId
  126. * @param {SlackMessage} msg
  127. * @param {boolean=} skipAttachment
  128. * @return {Element}
  129. **/
  130. function doCreateMessageDom(channelId, msg, skipAttachment) {
  131. var dom = document.createElement("div")
  132. ,ts = document.createElement("div")
  133. ,text = document.createElement("div")
  134. ,author = document.createElement("div")
  135. ,authorImg = document.createElement("img")
  136. ,authorName = document.createElement("span")
  137. ,hover = document.createElement("ul")
  138. ,hoverReply = document.createElement("li")
  139. ,attachments = document.createElement("ul")
  140. ,sender = msg.raw["user"] ?
  141. SLACK.context.users[msg.raw["user"]] :
  142. SLACK.context.bots[msg.raw["bot_id"]];
  143. dom.id = channelId +"_" +msg.ts;
  144. dom.className = R.klass.msg.item;
  145. ts.className = R.klass.msg.ts;
  146. text.className = R.klass.msg.msg;
  147. author.className = R.klass.msg.author;
  148. authorImg.className = R.klass.msg.authorAvatar;
  149. authorName.className = R.klass.msg.authorname;
  150. hover.className = R.klass.msg.hover.container;
  151. hoverReply.className = R.klass.msg.hover.reply;
  152. ts.innerHTML = formatDate(msg.ts);
  153. text.innerHTML = formatSlackText(msg.raw["text"] || "");
  154. authorName.textContent = sender ? sender.name : (msg.raw["username"] || "?");
  155. if (!sender && !msg.raw["username"])
  156. text.textContent = msg.raw["subtype"] || JSON.stringify(msg.raw);
  157. authorImg.src = sender ? sender.icons.image_48 : "";
  158. author.appendChild(authorImg);
  159. author.appendChild(authorName);
  160. hover.appendChild(hoverReply);
  161. dom.appendChild(author);
  162. dom.appendChild(text);
  163. dom.appendChild(ts);
  164. dom.appendChild(attachments);
  165. attachments.className = R.klass.msg.attachment.list;
  166. if (skipAttachment !== true && msg.raw["attachments"]) {
  167. msg.raw["attachments"].forEach(function(attachment) {
  168. var domAttachment = createAttachmentDom(channelId, msg, attachment);
  169. if (domAttachment)
  170. attachments.appendChild(domAttachment);
  171. });
  172. }
  173. dom.appendChild(hover);
  174. return dom;
  175. }
  176. /**
  177. * @param {string|number} ts seconds between EPOCH and event
  178. * @return {string}
  179. **/
  180. function formatDate(ts) {
  181. if (typeof(ts) !== "string")
  182. ts = parseFloat(ts);
  183. return (new Date(ts * 1000)).toLocaleTimeString();
  184. }
  185. /**
  186. * @param {string} fullText
  187. * @return {string}
  188. **/
  189. function formatSlackText(fullText) {
  190. var msgContents = fullText.split(/\r?\n/g);
  191. for (var msgContentIndex=0, nbMsgContents = msgContents.length; msgContentIndex < nbMsgContents; msgContentIndex++) {
  192. var msgContent = msgContents[msgContentIndex]
  193. ,_msgContent = ""
  194. ,currentMods = {}
  195. ,quote = false
  196. ,i =0
  197. ,msgLength = msgContent.length;
  198. var checkEnd = function(str, pos, c) {
  199. while (str[pos]) {
  200. if (str[pos] != ' ' && str[pos] != c && str[pos +1] == c) {
  201. return true;
  202. }
  203. pos++;
  204. }
  205. return false;
  206. } ,appendMod = function(mods) {
  207. if (!Object.keys(currentMods).length)
  208. return "";
  209. return '<span class="' +Object.keys(mods).join(' ') +'">';
  210. };
  211. // Skip trailing
  212. while (i < msgLength && (msgContent[i] === ' ' || msgContent[i] === '\t'))
  213. i++;
  214. if (msgContent.substr(i, 4) === '&gt;') {
  215. quote = true;
  216. i += 4;
  217. }
  218. for (; i < msgLength; i++) {
  219. var c = msgContent[i];
  220. if (!(currentMods[R.klass.msg.style.bold]) && c === '*' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
  221. if (Object.keys(currentMods).length)
  222. _msgContent += '</span>';
  223. currentMods[R.klass.msg.style.bold] = true;
  224. _msgContent += appendMod(currentMods);
  225. } else if (!(currentMods[R.klass.msg.style.strike]) && c === '~' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
  226. if (Object.keys(currentMods).length)
  227. _msgContent += '</span>';
  228. currentMods[R.klass.msg.style.strike] = true;
  229. _msgContent += appendMod(currentMods);
  230. } else if (!(currentMods[R.klass.msg.style.code]) && c === '`' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
  231. if (Object.keys(currentMods).length)
  232. _msgContent += '</span>';
  233. currentMods[R.klass.msg.style.code] = true;
  234. _msgContent += appendMod(currentMods);
  235. } else if (!(currentMods[R.klass.msg.style.italic]) && c === '_' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
  236. if (Object.keys(currentMods).length)
  237. _msgContent += '</span>';
  238. currentMods[R.klass.msg.style.italic] = true;
  239. _msgContent += appendMod(currentMods);
  240. } else {
  241. var finalFound = false;
  242. _msgContent += c;
  243. do {
  244. if ((currentMods[R.klass.msg.style.bold]) && c !== '*' && msgContent[i +1] === '*') {
  245. delete currentMods[R.klass.msg.style.bold];
  246. finalFound = true;
  247. } else if ((currentMods[R.klass.msg.style.strike]) && c !== '~' && msgContent[i +1] === '~') {
  248. delete currentMods[R.klass.msg.style.strike];
  249. finalFound = true;
  250. } else if ((currentMods[R.klass.msg.style.code]) && c !== '`' && msgContent[i +1] === '`') {
  251. delete currentMods[R.klass.msg.style.code];
  252. finalFound = true;
  253. } else if ((currentMods[R.klass.msg.style.italic]) && c !== '_' && msgContent[i +1] === '_') {
  254. delete currentMods[R.klass.msg.style.italic];
  255. finalFound = true;
  256. } else {
  257. break;
  258. }
  259. c = msgContent[++i];
  260. } while (i < msgLength);
  261. if (finalFound)
  262. _msgContent += '</span>' +appendMod(currentMods);
  263. }
  264. }
  265. if (currentMods) {
  266. // Should not append
  267. _msgContent += '</span>';
  268. }
  269. _msgContent = _msgContent.replace(new RegExp('<([@#]?)([^>]*)>', 'g'),
  270. function(matched, type, entity) {
  271. var sub = entity.split('|');
  272. if (type === '@') {
  273. if (!sub[1]) {
  274. var user = SLACK.context.getMember(sub[0]);
  275. sub[1] = user ? ('@' +user.name) : locale.unknownMember;
  276. } else if ('@' !== sub[1][0]) {
  277. sub[1] = '@' +sub[1];
  278. }
  279. sub[0] = '#' +sub[0];
  280. sub[2] = R.klass.msg.link +' ' +R.klass.msg.linkuser;
  281. } else if (type === '#') {
  282. if (!sub[1]) {
  283. var chan = SLACK.context.getChannel(sub[0]);
  284. sub[1] = chan ? ('#' +chan.name) : locale.unknownChannel;
  285. } else if ('#' !== sub[1][0]) {
  286. sub[1] = '#' +sub[1];
  287. }
  288. sub[0] = '#' +sub[0];
  289. sub[2] = R.klass.msg.link +' ' +R.klass.msg.linkchan;
  290. } else if (sub[0].indexOf("://") !== -1) {
  291. if (!sub[1])
  292. sub[1] = sub[0];
  293. sub[2] = R.klass.msg.link;
  294. } else {
  295. return matched;
  296. }
  297. return '<a href="' +sub[0] +'" class="' +sub[2] +'"' +(!type ? ' target="_blank"' : '') +'>' +sub[1] +'</a>';
  298. });
  299. if (quote)
  300. msgContents[msgContentIndex] = '<span class="' +R.klass.msg.style.quote +'">' +_msgContent +'</span>';
  301. else
  302. msgContents[msgContentIndex] = _msgContent;
  303. }
  304. return msgContents.join('<br/>');
  305. }
  306. /**
  307. * @param {string} channelId
  308. * @param {SlackMessage} msg
  309. * @param {*} attachment
  310. * @return {Element|null}
  311. **/
  312. function createAttachmentDom(channelId, msg, attachment) {
  313. var rootDom = document.createElement("li")
  314. ,attachmentBlock = document.createElement("div")
  315. ,pretext = document.createElement("div")
  316. ,titleBlock = document.createElement("a")
  317. ,authorBlock = document.createElement("div")
  318. ,authorImg = document.createElement("img")
  319. ,authorName = document.createElement("a")
  320. ,textBlock = document.createElement("div")
  321. ,textDom = document.createElement("div")
  322. ,thumbImgDom = document.createElement("img")
  323. ,imgDom = document.createElement("img")
  324. ,footerBlock = document.createElement("div")
  325. ,footerIcon = document.createElement("img")
  326. ,footerText = document.createElement("span")
  327. ,footerTs = document.createElement("span")
  328. ;
  329. rootDom.className = R.klass.msg.attachment.container;
  330. //Color
  331. var color = "#e3e4e6";
  332. if (attachment["color"]) {
  333. if (attachment["color"][0] === '#')
  334. color = attachment["color"][0];
  335. else if (attachment["color"] === "good")
  336. color = "#2fa44f";
  337. else if (attachment["color"] === "warning")
  338. color = "#de9e31";
  339. else if (attachment["color"] === "danger")
  340. color = "#d50200";
  341. }
  342. attachmentBlock.style.borderColor = color;
  343. //Pretext
  344. pretext.className = R.klass.msg.attachment.pretext;
  345. if (attachment["pretext"]) {
  346. pretext.innerHTML = formatSlackText(attachment["pretext"]);
  347. } else {
  348. pretext.classList.add(R.klass.hidden);
  349. }
  350. //Title
  351. titleBlock.target = "_blank";
  352. if (attachment["title"]) {
  353. titleBlock.innerHTML = formatSlackText(attachment["title"]);
  354. if (attachment["title_link"]) {
  355. titleBlock.href = attachment["title_link"];
  356. }
  357. titleBlock.className = R.klass.msg.attachment.title;
  358. } else {
  359. titleBlock.className = R.klass.hidden + " " +R.klass.msg.attachment.title;
  360. }
  361. //Author
  362. authorName.target = "_blank";
  363. authorBlock.className = R.klass.msg.author;
  364. if (attachment["author_name"]) {
  365. authorName.innerHTML = formatSlackText(attachment["author_name"]);
  366. authorName.href = attachment["author_link"] || "";
  367. authorName.className = R.klass.msg.authorname;
  368. authorImg.className = R.klass.msg.authorAvatar;
  369. if (attachment["author_icon"])
  370. authorImg.src = attachment["author_icon"];
  371. else
  372. authorImg.classList.add(R.klass.hidden);
  373. } else {
  374. authorBlock.classList.add(R.klass.hidden);
  375. }
  376. //Text
  377. textDom.innerHTML = formatSlackText(attachment["text"] || "");
  378. textDom.klassName = R.klass.msg.attachment.text;
  379. // Img (small one)
  380. thumbImgDom.className = R.klass.msg.attachment.thumbImg;
  381. if (attachment["thumb_url"])
  382. thumbImgDom.src = attachment["thumb_url"];
  383. else
  384. thumbImgDom.classList.add(R.klass.hidden);
  385. //Img (the big one)
  386. imgDom.className = R.klass.msg.attachment.img;
  387. if (attachment["image_url"])
  388. imgDom.src = attachment["image_url"];
  389. else
  390. imgDom.classList.add(R.klass.hidden);
  391. //Footer
  392. footerBlock.className = R.klass.msg.attachment.footer;
  393. footerText.className = R.klass.msg.attachment.footerText;
  394. footerIcon.className = R.klass.msg.attachment.footerIcon;
  395. if (attachment["footer"]) {
  396. footerText.innerHTML = formatSlackText(attachment["footer"]);
  397. if (attachment["footer_icon"])
  398. footerIcon.src = attachment["footer_icon"];
  399. else
  400. footerIcon.classList.add(R.klass.hidden);
  401. } else {
  402. footerIcon.classList.add(R.klass.hidden);
  403. footerText.classList.add(R.klass.hidden);
  404. }
  405. //Ts
  406. footerTs.className = R.klass.msg.ts;
  407. if (attachment["ts"])
  408. footerTs.innerHTML = formatDate(attachment["ts"]);
  409. else
  410. footerTs.classList.add(R.klass.hidden);
  411. // TODO Field [ {title, value, short } ]
  412. // TODO actions (button stuff)
  413. authorBlock.appendChild(authorImg);
  414. authorBlock.appendChild(authorName);
  415. textBlock.appendChild(textDom);
  416. textBlock.appendChild(thumbImgDom);
  417. footerBlock.appendChild(footerIcon);
  418. footerBlock.appendChild(footerText);
  419. footerBlock.appendChild(footerTs);
  420. attachmentBlock.appendChild(titleBlock);
  421. attachmentBlock.appendChild(authorBlock);
  422. attachmentBlock.appendChild(textBlock);
  423. attachmentBlock.appendChild(imgDom);
  424. attachmentBlock.appendChild(footerBlock);
  425. rootDom.appendChild(pretext);
  426. rootDom.appendChild(attachmentBlock);
  427. return rootDom;
  428. }
  429. /**
  430. * @param {string} channelId
  431. * @param {SlackMessage} msg
  432. * @param {boolean=} skipAttachment
  433. * @return {Element}
  434. **/
  435. function doCreateMeMessageDom(channelId, msg, skipAttachment) {
  436. var dom = doCreateMessageDom(channelId, msg, skipAttachment);
  437. dom.classList.add(R.klass.msg.meMessage);
  438. return dom;
  439. }
  440. /**
  441. * @param {string} channelId
  442. * @param {SlackMessage} msg
  443. * @param {boolean=} skipAttachment
  444. * @return {Element}
  445. **/
  446. function createMessageDom(channelId, msg, skipAttachment) {
  447. if (msg.subtype === "me_message") {
  448. return doCreateMeMessageDom(channelId, msg, skipAttachment);
  449. }
  450. return doCreateMessageDom(channelId, msg, skipAttachment);
  451. }
  452. function updateTitle() {
  453. var hasUnread = 0
  454. ,hasHl = 0
  455. ,title = "";
  456. for (var i in UNREAD_CHANS) {
  457. if (UNREAD_CHANS.hasOwnProperty(i)) {
  458. hasUnread += UNREAD_CHANS[i].unread;
  459. hasHl += UNREAD_CHANS[i].hl;
  460. }
  461. }
  462. if (hasHl)
  463. title = "(!" +hasHl +") - ";
  464. else if (hasUnread)
  465. title = "(" +hasUnread +") - ";
  466. title += SLACK.context.team.name;
  467. document.title = title;
  468. }
  469. function spawnNotification() {
  470. if (!("Notification" in window))
  471. {}
  472. else if (Notification.permission === "granted") {
  473. var now = Date.now();
  474. if (lastNotificationSpawn + NOTIFICATION_COOLDOWN < now) {
  475. var n = new Notification(locale.newMessage);
  476. lastNotificationSpawn = now;
  477. setTimeout(function() {
  478. n.close();
  479. }, NOTIFICATION_DELAY);
  480. }
  481. }
  482. else if (Notification.permission !== "denied")
  483. Notification.requestPermission();
  484. }
  485. function onRoomUpdated() {
  486. var chatFrag = document.createDocumentFragment()
  487. ,currentRoomId = SELECTED_ROOM.id;
  488. document.getElementById(R.id.currentRoom.content).textContent = "";
  489. if (SLACK.history[currentRoomId])
  490. SLACK.history[currentRoomId].messages.forEach(function(msg) {
  491. if (msg.type === "message") {
  492. var dom = createMessageDom(currentRoomId, msg);
  493. chatFrag.appendChild(dom);
  494. }
  495. });
  496. var content = document.getElementById(R.id.currentRoom.content);
  497. content.appendChild(chatFrag);
  498. //TODO scroll lock
  499. content.scrollTop = content.scrollHeight -content.clientHeight;
  500. }
  501. function chatClickDelegate(e) {
  502. var target = e.target
  503. ,getMessageId = function(e, target) {
  504. target = target || e.target;
  505. while (target !== e.currentTarget && target) {
  506. if (target.classList.contains(R.klass.msg.item)) {
  507. return target.id;
  508. }
  509. target = target.parentElement;
  510. }
  511. };
  512. while (target !== e.currentTarget && target) {
  513. if (target.classList.contains(R.klass.msg.hover.container)) {
  514. return;
  515. } else if (target.classList.contains(R.klass.msg.hover.reply)) {
  516. var messageId = getMessageId(e, target);
  517. if (messageId) {
  518. messageId = parseFloat(messageId.split("_")[1]);
  519. var history = SLACK.history[SELECTED_ROOM.id].messages;
  520. for (var i =0, histLen =history.length; i < histLen && history[i].ts <= messageId; i++) {
  521. if (history[i].ts === messageId) {
  522. if (REPLYING_TO !== history[i]) {
  523. REPLYING_TO = history[i];
  524. onReplyingToUpdated();
  525. }
  526. return;
  527. }
  528. }
  529. }
  530. return;
  531. }
  532. target = target.parentElement;
  533. }
  534. }
  535. function focusInput() {
  536. document.getElementById(R.id.message.input).focus();
  537. }
  538. function setRoomFromHashBang() {
  539. var hashId = document.location.hash.substr(1)
  540. ,room = SLACK.context.getChannel(hashId)
  541. ,user = SLACK.context.getMember(hashId);
  542. if (room && room !== SELECTED_ROOM)
  543. selectRoom(room);
  544. else if (user && user.ims)
  545. selectRoom(user.ims);
  546. }
  547. document.addEventListener('DOMContentLoaded', function() {
  548. initLang('en');
  549. document.getElementById(R.id.currentRoom.content).addEventListener("click", chatClickDelegate);
  550. window.addEventListener("hashchange", function(e) {
  551. if (document.location.hash && document.location.hash[0] === '#') {
  552. setRoomFromHashBang();
  553. }
  554. });
  555. document.getElementById(R.id.message.file.cancel).addEventListener("click", function(e) {
  556. e.preventDefault();
  557. document.getElementById(R.id.message.file.error).classList.add(R.klass.hidden);
  558. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  559. document.getElementById(R.id.message.file.fileInput).value = "";
  560. return false;
  561. });
  562. document.getElementById(R.id.message.file.form).addEventListener("submit", function(e) {
  563. e.preventDefault();
  564. var fileInput = document.getElementById(R.id.message.file.fileInput)
  565. ,filename = fileInput.value;
  566. if (filename) {
  567. filename = filename.substr(filename.lastIndexOf('\\') +1);
  568. uploadFile(SELECTED_ROOM, filename, fileInput.files[0], function(errorMsg) {
  569. var error = document.getElementById(R.id.message.file.error);
  570. if (errorMsg) {
  571. error.textContent = errorMsg;
  572. error.classList.remove(R.klass.hidden);
  573. } else {
  574. error.classList.add(R.klass.hidden);
  575. document.getElementById(R.id.message.file.fileInput).value = "";
  576. document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  577. }
  578. });
  579. }
  580. return false;
  581. });
  582. document.getElementById(R.id.message.file.bt).addEventListener("click", function(e) {
  583. e.preventDefault();
  584. if (SELECTED_ROOM) {
  585. document.getElementById(R.id.message.file.formContainer).classList.remove(R.klass.hidden);
  586. }
  587. return false;
  588. });
  589. document.getElementById(R.id.message.form).addEventListener("submit", function(e) {
  590. e.preventDefault();
  591. var input =document.getElementById(R.id.message.input);
  592. if (SELECTED_ROOM && input.value) {
  593. sendMsg(SELECTED_ROOM, input.value, REPLYING_TO);
  594. input.value = "";
  595. if (REPLYING_TO) {
  596. REPLYING_TO = null;
  597. onReplyingToUpdated();
  598. }
  599. }
  600. focusInput();
  601. return false;
  602. });
  603. window.addEventListener('blur', function() {
  604. window.hasFocus = false;
  605. });
  606. window.addEventListener('focus', function() {
  607. window.hasFocus = true;
  608. lastNotificationSpawn = 0;
  609. if (SELECTED_ROOM)
  610. markRoomAsRead(SELECTED_ROOM);
  611. focusInput();
  612. });
  613. window.hasFocus = true;
  614. startPolling();
  615. });