| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- /* jshint sub: true */
- var
- /**
- * @type {number} next period to wait before next retry in case of failure, in seconds
- **/
- NEXT_RETRY = 0,
- /**
- * @type {Room|null}
- **/
- SELECTED_ROOM = null,
- /**
- * @type {SimpleChatSystem|null}
- **/
- SELECTED_CONTEXT = null,
- /** @type {Message|null} */
- REPLYING_TO = null,
- /** @type {Message|null} */
- EDITING = null,
- /** @const @type {number} */
- KEEP_MESSAGES = 100
- ;
- function initHljs() {
- var xhr = new XMLHttpRequest();
- xhr.timeout = 1000 * 60 * 1; // 3 min timeout
- xhr.onreadystatechange = function(e) {
- if (xhr.readyState === 4) {
- var script = document.createElement("script"),
- link = document.createElement("link");
- script.innerHTML = xhr.response;
- script.language = "text/javascript";
- link.href = "hljs-androidstudio.css";
- link.rel = "stylesheet";
- document.head.appendChild(link);
- document.body.appendChild(script);
- }
- };
- xhr.open('GET', 'highlight.pack.js', true);
- xhr.send(null);
- }
- /**
- * @param {Room} room
- * @param {function(boolean)} cb
- **/
- function fetchHistory(room, cb) {
- var xhr = new XMLHttpRequest(),
- self = this;
- xhr.open('GET', 'api/hist?room=' +room.id, true);
- xhr.onreadystatechange = function(e) {
- if (xhr.readyState === 4) {
- if (xhr.response) {
- var resp = xhr.response;
- try {
- resp = JSON.parse(/** @type {string} */ (resp));
- } catch (e) {}
- var history = DATA.history[room.id],
- updated;
- if (!history) {
- history = DATA.history[room.id] = new UiRoomHistory(room, KEEP_MESSAGES, /** @type {Array} */ (resp), Date.now());
- updated = true;
- } else {
- updated = !!history.pushAll(/** @type {Array} */ (resp), Date.now());
- }
- if (updated) {
- onMsgReceived(DATA.context.getChannelContext(room.id).getChatContext(), room, /** @type {Array} */ (resp));
- if (room === SELECTED_ROOM)
- onRoomUpdated();
- }
- } // TODO ui stop loading
- }
- };
- xhr.send(null);
- }
- function onConfigUpdated() {
- if (isObjectEmpty(CONFIG.services)) {
- Settings.setClosable(false).display(Settings.pages.services);
- }
- loadEmojiProvider(CONFIG.getEmojiProvider());
- }
- function poll(callback) {
- var xhr = new XMLHttpRequest();
- xhr.timeout = 1000 * 60 * 1; // 3 min timeout
- xhr.onreadystatechange = function(e) {
- if (xhr.readyState === 4) {
- if (xhr.status === 0) {
- if (NEXT_RETRY) {
- NEXT_RETRY = 0;
- onNetworkStateUpdated(true);
- }
- poll(callback); // retry on timeout
- return;
- }
- var resp = null,
- success = Math.floor(xhr.status / 100) === 2;
- if (success) {
- if (NEXT_RETRY) {
- NEXT_RETRY = 0;
- onNetworkStateUpdated(true);
- }
- resp = xhr.response;
- try {
- resp = JSON.parse(/** @type {string} */ (resp));
- } catch (exp) {
- resp = null;
- }
- } else {
- if (NEXT_RETRY) {
- NEXT_RETRY += Math.floor((NEXT_RETRY || 5)/2);
- NEXT_RETRY = Math.min(60, NEXT_RETRY);
- } else {
- NEXT_RETRY = 5;
- onNetworkStateUpdated(false);
- }
- }
- callback(success, resp);
- }
- };
- xhr.open('GET', 'api?v=' +DATA.lastServerVersion, true);
- xhr.send(null);
- }
- function outOfSync() {
- DATA.lastServerVersion = 0;
- }
- /**
- * @param {Room} room
- **/
- function sendTyping(room) {
- var xhr = new XMLHttpRequest(),
- url = 'api/typing?room=' +room.id;
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {boolean} success
- * @param {*} response
- **/
- function onPollResponse(success, response) {
- if (success) {
- if (response) {
- DATA.update(response);
- }
- startPolling();
- } else {
- setTimeout(startPolling, NEXT_RETRY * 1000);
- }
- }
- function startPolling() {
- poll(onPollResponse);
- }
- /**
- * @param {Room} room
- **/
- function selectRoom(room) {
- if (SELECTED_ROOM)
- unselectRoom();
- document.getElementById("room_" +room.id).classList.add(R.klass.selected);
- document.body.classList.remove(R.klass.noRoomSelected);
- SELECTED_ROOM = room;
- SELECTED_CONTEXT = /** @type {SimpleChatSystem} */ (DATA.context.getChannelContext(room.id));
- onRoomSelected();
- createContextBackground(SELECTED_CONTEXT.getChatContext().team.id, SELECTED_CONTEXT.getChatContext().users, function(imgData) {
- document.getElementById(R.id.context).style.backgroundImage = 'url(' +imgData +')';
- });
- if (!DATA.history[SELECTED_ROOM.id] || DATA.history[SELECTED_ROOM.id].messages.length < KEEP_MESSAGES)
- fetchHistory(SELECTED_ROOM, function(success) {});
- document.getElementById(R.id.mainSection).classList.remove(R.klass.noRoomSelected);
- }
- /**
- * @param {Room} room
- **/
- function starChannel(room) {
- var xhr = new XMLHttpRequest(),
- url = 'api/starChannel?room=' +room.id;
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {Room} room
- **/
- function unstarChannel(room) {
- var xhr = new XMLHttpRequest(),
- url = 'api/unstarChannel?room=' +room.id;
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- function unselectRoom() {
- document.getElementById("room_" +SELECTED_ROOM.id).classList.remove(R.klass.selected);
- document.getElementById(R.id.mainSection).classList.add(R.klass.noRoomSelected);
- }
- /**
- * @param {Room} chan
- * @param {string} filename
- * @param {File} file
- * @param {function(string?)} callback
- **/
- function uploadFile(chan, filename, file, callback) {
- var fileReader = new FileReader(),
- formData = new FormData(),
- xhr = new XMLHttpRequest();
- formData.append("file", file);
- formData.append("filename", filename);
- xhr.onreadystatechange = function() {
- if (xhr.readyState === 4) {
- if (xhr.status === 204) {
- callback(null);
- } else {
- callback(xhr.statusText);
- }
- }
- };
- xhr.open('POST', 'api/file?room=' +chan.id);
- xhr.send(formData);
- }
- /**
- * @param {string} payload
- * @param {string} serviceId
- * @param {(function((string|null)))=} callback
- **/
- function sendCommand(payload, serviceId, callback) {
- var xhr = new XMLHttpRequest();
- if (callback) {
- xhr.onreadystatechange = function() {
- if (xhr.readyState === 4) {
- if (xhr.status === 204) {
- callback(null);
- } else {
- callback(xhr.statusText);
- }
- }
- };
- }
- xhr.open('POST', "api/attachmentAction?serviceId=" +serviceId);
- xhr.send(JSON.stringify(payload));
- }
- function getActionPayload(channelId, msg, attachment, action) {
- var payload = {
- "actions": [ action ],
- "attachment_id": attachment["id"],
- "callback_id": attachment["callback_id"],
- "channel_id": channelId,
- "is_ephemeral": msg instanceof NoticeMessage,
- "message_ts": msg["id"]
- };
- return payload;
- }
- /**
- * @param {Room} chan
- * @param {Command!} cmd
- * @param {string} args
- **/
- function doCommand(chan, cmd, args) {
- var xhr = new XMLHttpRequest(),
- url = 'api/cmd?room=' +chan.id +"&cmd=" +encodeURIComponent(cmd.name.substr(1)) +"&args=" +encodeURIComponent(args);
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {Room} chan
- * @param {string} msg
- * @param {Message|null=} replyTo
- **/
- function sendMsg(chan, msg, replyTo) {
- var xhr = new XMLHttpRequest();
- var url = 'api/msg?room=' +chan.id +"&text=" +encodeURIComponent(msg);
- if (replyTo) {
- var sender = DATA.context.getUser(replyTo.userId),
- footer = chan.isPrivate ? locale.message : chan.name;
- var attachment = {
- "fallback": replyTo.text,
- "author_name": sender.name,
- "text": replyTo.text,
- "footer": footer,
- "ts": replyTo.ts
- };
- url += "&attachments=" +encodeURIComponent(JSON.stringify([attachment]));
- }
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {string} input
- * @param {boolean=} skipCommand
- * @return {boolean} true on recognized input
- **/
- function onTextEntered(input, skipCommand) {
- var success = true;
- if (EDITING) {
- editMsg(SELECTED_ROOM, input, EDITING);
- return true;
- }
- if (input[0] === '/' && skipCommand !== true) {
- var endCmd = input.indexOf(' '),
- cmd = input.substr(0, endCmd === -1 ? undefined : endCmd),
- args = endCmd === -1 ? "" : input.substr(endCmd),
- ctx = SELECTED_CONTEXT,
- cliCmdObject = CLIENT_COMMANDS.getCommand(cmd);
- if (cliCmdObject) {
- cliCmdObject.exec(ctx, SELECTED_ROOM, args.trim());
- return true;
- } else if (ctx) {
- var cmdObject = ctx.getChatContext().commands.data[cmd];
- if (cmdObject) {
- doCommand(SELECTED_ROOM, cmdObject, args.trim());
- return true;
- }
- }
- return false;
- }
- sendMsg(SELECTED_ROOM, input, REPLYING_TO);
- return true;
- }
- /**
- * @param {Room} chan
- * @param {string} text
- * @param {Message|null=} msg
- **/
- function editMsg(chan, text, msg) {
- var xhr = new XMLHttpRequest();
- var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id +"&text=" +encodeURIComponent(text);
- xhr.open('PUT', url, true);
- xhr.send(null);
- }
- /**
- * @param {Room} chan
- * @param {Message|null=} msg
- **/
- function removeMsg(chan, msg) {
- var xhr = new XMLHttpRequest();
- var url = 'api/msg?room=' +chan.id +"&ts=" +msg.id;
- xhr.open('DELETE', url, true);
- xhr.send(null);
- }
- /**
- * @param {Room} chan
- * @param {string} id
- * @param {number} ts
- **/
- function sendReadMarker(chan, id, ts) {
- var xhr = new XMLHttpRequest();
- var url = 'api/markread?room=' +chan.id +"&id=" +id +"&ts=" +ts;
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {string} channelId
- * @param {string} msgId
- * @param {string} reaction
- **/
- function addReaction(channelId, msgId, reaction) {
- var xhr = new XMLHttpRequest();
- var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
- xhr.open('POST', url, true);
- xhr.send(null);
- }
- /**
- * @param {string} channelId
- * @param {string} msgId
- * @param {string} reaction
- **/
- function removeReaction(channelId, msgId, reaction) {
- var xhr = new XMLHttpRequest();
- var url = 'api/reaction?room=' +channelId +"&msg=" +msgId +"&reaction=" +encodeURIComponent(reaction);
- xhr.open('DELETE', url, true);
- xhr.send(null);
- }
- /**
- * @this {Element}
- **/
- function filterChanList() {
- var chans = {},
- matchingChans = [],
- val = this.value;
- DATA.context.foreachChannels(function(chan) {
- chans[chan.id] = chan.matchString(val, Utils);
- });
- for (var chanId in chans) {
- var chanDom = document.getElementById("room_" +chanId);
- if (chanDom) {
- if (chans[chanId].name + chans[chanId].members + chans[chanId].topic +chans[chanId].purpose) {
- chanDom.classList.remove(R.klass.hidden);
- matchingChans.push(chanId);
- } else {
- chanDom.classList.add(R.klass.hidden);
- }
- }
- }
- //TODO sort
- }
|