slack.min.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. function ChatInfo(id){this.id=id;this.name;this.version=0}ChatInfo.prototype.toStatic=function(t){return t>=this.version?{}:{"id":this.id,"name":this.name}};ChatInfo.prototype.update=function(data,t){if(data["name"]!==undefined)this.name=data["name"];this.version=Math.max(this.version,t)};function Command(data){this.desc=data["desc"];this.name=data["name"];this.type=data["type"];this.usage=data["usage"];this.category=data["category"]}
  2. Command.prototype.toStatic=function(t){return{"desc":this.desc,"name":this.name,"type":this.type,"usage":this.usage,"category":this.category}};function SelfPreferences(){this.favoriteEmojis={};this.highlights=[]}
  3. SelfPreferences.prototype.update=function(prefs,t){this.favoriteEmojis=(JSON.parse(prefs["emoji_use"]));if(prefs["highlight_words"])this.highlights=(prefs["highlight_words"]||"").split(",").filter(function(i){return i.trim()!==""});else if(prefs["highlights"])this.highlights=prefs["highlights"];this.version=Math.max(this.version,t)};SelfPreferences.prototype.toStatic=function(t){return this.version>t?null:{"emoji_use":JSON.stringify(this.favoriteEmojis),"highlights":this.highlights}};
  4. function ChatContext(){this.team=null;this.channels={};this.users={};this.self=null;this.emojis={version:0,data:{}};this.commands={version:0,data:{}};this.typing={};this.staticV=0;this.liveV=0}ChatContext.prototype.userFactory=function(userData){return new Chatter(userData["id"])};ChatContext.prototype.roomFactory=function(roomData){return roomData["pv"]?new PrivateMessageRoom(roomData["id"],this.users[roomData["user"]]):new Room(roomData["id"])};ChatContext.prototype.teamFactory=function(id){return new ChatInfo(id)};
  5. ChatContext.prototype.commandFactory=function(data){return new Command(data)};
  6. ChatContext.prototype.updateStatic=function(data,t){if(data["users"])for(var i=0,nbUsers=data["users"].length;i<nbUsers;i++){var userObj=this.users[data["users"][i]["id"]];if(!userObj)userObj=this.users[data["users"][i]["id"]]=this.userFactory(data["users"][i]);userObj.update(data["users"][i],t)}if(data["channels"])for(var i=0,nbChan=data["channels"].length;i<nbChan;i++){var chanObj=this.channels[data["channels"][i]["id"]];if(!chanObj)chanObj=this.channels[data["channels"][i]["id"]]=this.roomFactory(data["channels"][i]);
  7. chanObj.update(data["channels"][i],this,t)}if(data["emojis"]){this.emojis.data=data["emojis"];this.emojis.version=t}if(data["commands"]!==undefined){this.commands.data={};for(var i in data["commands"])this.commands.data[i]=this.commandFactory(data["commands"][i]);this.commands.version=t}if(data["team"]){if(!this.team)this.team=this.teamFactory(data["team"]["id"]);this.team.update(data["team"],t)}this.staticV=Math.max(this.staticV,t);if(data["self"]){this.self=this.users[data["self"]["id"]];if(!this.self.prefs)this.self.prefs=
  8. new SelfPreferences;this.self.prefs.update(data["self"]["prefs"],t)}if(data["typing"]!==undefined){this.typing={};for(var i in data["typing"]){this.typing[i]={};for(var j in data["typing"][i])this.typing[i][j]=t}}};
  9. ChatContext.prototype.toStatic=function(t,now){var channels=[],users=[];var res={"team":this.team.toStatic(t),"self":{"id":this.self.id,"prefs":this.self.prefs.toStatic(t)},"emojis":this.emojis.version>t?this.emojis.data:undefined};if(this.commands.version>t){res["commands"]={};for(var i in this.commands.data)res["commands"][i]=this.commands.data[i].toStatic(t)}for(var chanId in this.channels){var chan=this.channels[chanId].toStatic(t);if(chan)channels.push(chan)}if(channels.length)res["channels"]=
  10. channels;for(var userId in this.users){var user=this.users[userId].toStatic(t);if(user)users.push(user)}if(users.length)res["users"]=users;for(var typingChan in this.typing){var tChan=null;for(var typingUser in this.typing[typingChan])if(this.typing[typingChan][typingUser]+3E3>=now){if(!tChan)tChan={};tChan[typingUser]=1}else delete this.typing[typingChan][typingUser];if(tChan){if(res["typing"]===undefined)res["typing"]={};res["typing"][typingChan]=tChan}else delete this.typing[typingChan]}return res};
  11. ChatContext.prototype.cleanTyping=function(now){var updated=false;for(var typingChan in this.typing){var chanEmpty=true;for(var typingUser in this.typing[typingChan])if(this.typing[typingChan][typingUser]+3E3<now){delete this.typing[typingChan][typingUser];updated=true}else chanEmpty=false;if(chanEmpty){delete this.typing[typingChan];updated=true}}return updated};
  12. (function(){if(typeof module!=="undefined"){module.exports.ChatContext=ChatContext;module.exports.ChatInfo=ChatInfo;module.exports.Command=Command}})();function Room(id){this.id=id;this.name;this.created;this.creator;this.archived;this.isMember;this.lastRead=0;this.lastMsg=0;this.users=[];this.topic;this.topicTs;this.topicCreator;this.purpose;this.purposeTs;this.purposeCreator;this.version=0;this.isPrivate}Room.prototype.setLastMsg=function(lastMsg,t){if(this.lastMsg<lastMsg){this.lastMsg=lastMsg;this.version=t;return true}return false};
  13. Room.prototype.toStatic=function(t){if(t>=this.version)return null;var res={"id":this.id,"name":this.name,"created":this.created,"creator":this.creator?this.creator.id:undefined,"is_archived":this.archived,"is_member":this.isMember,"last_read":this.lastRead,"last_msg":this.lastMsg,"is_private":this.isPrivate};if(this.isMember){res["members"]=this.members?Object.keys(this.members):[];res["topic"]={"value":this.topic,"creator":this.topicCreator?this.topicCreator.id:null,"last_set":this.topicTs};res["purpose"]=
  14. {"value":this.purpose,"creator":this.purposeCreator?this.purposeCreator.id:null,"last_set":this.purposeTs}}return res};
  15. Room.prototype.update=function(chanData,ctx,t){if(chanData["name"]!==undefined)this.name=chanData["name"];if(chanData["created"]!==undefined)this.created=chanData["created"];if(chanData["creator"]!==undefined)this.creator=ctx.users[chanData["creator"]];if(chanData["is_archived"]!==undefined)this.archived=chanData["is_archived"];if(chanData["is_member"]!==undefined)this.isMember=chanData["is_member"];if(chanData["last_read"]!==undefined)this.lastRead=Math.max(parseFloat(chanData["last_read"]),this.lastRead);
  16. if(chanData["last_msg"]!==undefined)this.lastMsg=parseFloat(chanData["last_msg"]);if(chanData["latest"])this.lastMsg=parseFloat(chanData["latest"]["ts"]);if(chanData["members"]){this.members={};if(chanData["members"])for(var i=0,nbMembers=chanData["members"].length;i<nbMembers;i++){var member=ctx.users[chanData["members"][i]];this.members[member.id]=member;member.channels[this.id]=this}}if(chanData["topic"]){this.topic=chanData["topic"]["value"];this.topicCreator=ctx.users[chanData["topic"]["creator"]];
  17. this.topicTs=chanData["topic"]["last_set"]}if(chanData["purpose"]){this.purpose=chanData["purpose"]["value"];this.purposeCreator=ctx.users[chanData["purpose"]["creator"]];this.purposeTs=chanData["purpose"]["last_set"]}this.version=Math.max(this.version,t)};function PrivateMessageRoom(id,user){Room.call(this,id);this.user=user;user.privateRoom=this}PrivateMessageRoom.prototype=Object.create(Room.prototype);PrivateMessageRoom.prototype.constructor=PrivateMessageRoom;
  18. PrivateMessageRoom.prototype.toStatic=function(t){return t>=this.version?null:{"id":this.id,"created":this.created,"user":this.user.id,"last_read":this.lastRead,"last_msg":this.lastMsg,"pv":true}};(function(){if(typeof module!=="undefined"){module.exports.Room=Room;module.exports.PrivateMessageRoom=PrivateMessageRoom}})();function Message(e,ts){this.userId=e.userId;this.username=e.userName;this.id=e.id;this.ts=e.ts;this.text;this.attachments;this.starred;this.pinned;this.edited;this.removed;this.reactions;this.version=ts;this.update(e,ts)}function MeMessage(e,ts){Message.call(this,e,ts)}function NoticeMessage(e,ts){Message.call(this,e,ts)}
  19. Message.prototype.update=function(e,ts){if(e){this.text=e.text||"";if(e.attachments)this.attachments=e.attachments;this.starred=!!e.is_starred;this.pinned=false;this.edited=!!e.edited;this.removed=!!e.removed;var _this=this;if(e.reactions){this.reactions={};e.reactions.forEach(function(r){_this.reactions[r["name"]]=[];r["users"].forEach(function(userId){_this.reactions[r["name"]].push(userId)})})}}else this.removed=true;this.version=ts};
  20. Message.prototype.toStatic=function(){var reactions=[];for(var reaction in this.reactions)reactions.push({"name":reaction,"users":this.reactions[reaction]});return{"user":this.userId,"username":this.username,"ts":this.id,"text":this.text,"attachments":this.attachments,"is_starred":this.is_starred||undefined,"edited":this.edited,"removed":this.removed,"reactions":reactions,"isMeMessage":this instanceof MeMessage,"isNotice":this instanceof NoticeMessage}};
  21. Message.prototype.addReaction=function(reaction,userId,ts){if(!this.reactions[reaction])this.reactions[reaction]=[];this.reactions[reaction].push(userId);this.version=ts};
  22. Message.prototype.removeReaction=function(reaction,userId,ts){var updated=false;if(this.reactions[reaction])if(this.reactions[reaction].length===1&&this.reactions[reaction][0]===userId){delete this.reactions[reaction];updated=true}else this.reactions[reaction]=this.reactions[reaction].filter(function(i){if(i!==userId)return false;updated=true;return true});if(updated)this.version=ts};
  23. Message.prototype.hasReactionForUser=function(reaction,userId){if(this.reactions[reaction])return this.reactions[reaction].indexOf(userId)!==-1;return false};function RoomHistory(room,keepMessages,evts,now){this.id=typeof room==="string"?room:room.id;this.messages=[];this.v=0;this.keepMessages=keepMessages;if(evts)this.pushAll(evts,now)}RoomHistory.prototype.pushAll=function(evts,t){var result=0;evts.forEach(function(e){result=Math.max(this.push(e,t),result)}.bind(this));this.resort();return result};
  24. RoomHistory.prototype.messageFactory=function(ev,ts){return new Message(ev,ts)};RoomHistory.prototype.push=function(e,t){var exists=false,ts;for(var i=0,nbMsg=this.messages.length;i<nbMsg;i++){var msgObj=this.messages[i];if(msgObj.id===e["id"]){ts=msgObj.update(e,t);exists=true;break}}if(!exists){var msgObj=this.messageFactory(e,t);this.messages.push(msgObj);ts=msgObj.ts}while(this.messages.length>this.keepMessages)this.messages.shift();return ts||0};
  25. RoomHistory.prototype.lastMessage=function(){return this.messages[this.messages.length-1]};RoomHistory.prototype.addReaction=function(reaction,userId,msgId,ts){var msg=this.getMessageById(msgId);if(msg)msg.addReaction(reaction,userId,ts);return msg};RoomHistory.prototype.removeReaction=function(reaction,userId,msgId,ts){var msg=this.getMessageById(msgId);if(msg)msg.removeReaction(reaction,userId,ts);return msg};
  26. RoomHistory.prototype.getMessage=function(ts){for(var i=0,nbMessages=this.messages.length;i<nbMessages&&ts>=this.messages[i].ts;i++)if(this.messages[i].ts===ts)return this.messages[i];return null};RoomHistory.prototype.getMessageById=function(id){for(var i=0,nbMessages=this.messages.length;i<nbMessages;i++)if(this.messages[i].id==id)return this.messages[i];return null};
  27. RoomHistory.prototype.toStatic=function(knownVersion){var result=[];for(var i=this.messages.length-1;i>=0;i--)if(this.messages[i].version>knownVersion)result.push(this.messages[i].toStatic());return result};RoomHistory.prototype.resort=function(){this.messages.sort(function(a,b){return a.ts-b.ts})};MeMessage.prototype=Object.create(Message.prototype);MeMessage.prototype.constructor=MeMessage;NoticeMessage.prototype=Object.create(Message.prototype);NoticeMessage.prototype.constructor=NoticeMessage;
  28. (function(){if(typeof module!=="undefined")module.exports={Message:Message,MeMessage:MeMessage,NoticeMessage:NoticeMessage,RoomHistory:RoomHistory}})();function Chatter(id){this.id=id;this.name;this.deleted;this.status;this.realName;this.presence;this.icons={small:"",large:""};this.email;this.firstName;this.lastName;this.channels={};this.ims=null;this.prefs=null;this.isBot;this.privateRoom=null;this.version=0}
  29. Chatter.prototype.toStatic=function(t){return t>=this.version?null:{"id":this.id,"name":this.name,"deleted":this.deleted,"status":this.status,"real_name":this.realName,"isPresent":this.presence,"isBot":this.isBot,"profile":{"email":this.email,"first_name":this.firstName,"last_name":this.lastName,"icon_small":this.icons.small,"icon_large":this.icons.large}}};
  30. Chatter.prototype.update=function(userData,t){if(userData["name"]!==undefined)this.name=userData["name"];if(userData["deleted"]!==undefined)this.deleted=userData["deleted"];if(userData["status"]!==undefined)this.status=userData["status"];if(userData["real_name"]!==undefined)this.realName=userData["real_name"];else if(userData["profile"]&&userData["profile"]["real_name"]!==undefined)this.realName=userData["profile"]["real_name"];if(userData["presence"]!==undefined)this.presence=userData["presence"]!==
  31. "away";if(userData["isPresent"]!==undefined)this.presence=userData["isPresent"];if(userData["isBot"])this.isBot=userData["isBot"];if(userData["profile"]){this.icons.small=userData["profile"]["image_48"];this.icons.large=userData["profile"]["image_512"];this.email=userData["profile"]["email"];this.firstName=userData["profile"]["first_name"];this.lastName=userData["profile"]["last_name"]}this.version=Math.max(this.version,t)};(function(){if(typeof module!=="undefined")module.exports.Chatter=Chatter})();var lang={},locale;function initLang(lg){if(!lg){for(var i=0,nbLang=navigator.languages.length;i<nbLang;i++)if(lang.hasOwnProperty(navigator.languages[i])){lg=navigator.languages[i];break}if(!lg)lg="en"}locale=lang[lg];console.log("Loading language pack: "+lg);if(locale.dom)for(var i in locale.dom)document.getElementById(i).textContent=locale.dom[i]};lang["fr"]={unknownMember:"Utilisateur inconnu",unknownChannel:"Channel inconnu",newMessage:"Nouveau message",netErrorShort:"Reseau",edited:"edit&eacute;",onlyVisible:"(visible seulement par vous)",areTyping:function(names){if(names.length===1)return names[0]+" est en train d'\u00e9crire";return names.join(", ")+" sont en train d'\u00e9crire"},formatDate:function(ts){if(typeof ts!=="string")ts=parseFloat(ts);var today=new Date,yesterday=new Date,dateObj=new Date(ts*1E3);today.setHours(0);today.setMinutes(0);
  32. today.setSeconds(0);today.setMilliseconds(0);yesterday.setTime(today.getTime());yesterday.setDate(yesterday.getDate()-1);if(dateObj.getTime()>today.getTime())return dateObj.toLocaleTimeString();if(dateObj.getTime()>yesterday.getTime())return"hier, "+dateObj.toLocaleTimeString();return dateObj.toLocaleString()},dom:{"fileUploadCancel":"Annuler","neterror":"Impossible de se connecter au chat !"}};lang["en"]={unknownMember:"Unknown member",unknownChannel:"Unknown channel",newMessage:"New message",netErrorShort:"Network",edited:"edited",onlyVisible:"(only visible to you)",areTyping:function(names){if(names.length===1)return names[0]+" is typing";return names.join(", ")+" are typing"},formatDate:function(ts){if(typeof ts!=="string")ts=parseFloat(ts);var today=new Date,yesterday=new Date,dateObj=new Date(ts*1E3);today.setHours(0);today.setMinutes(0);today.setSeconds(0);today.setMilliseconds(0);
  33. yesterday.setTime(today.getTime());yesterday.setDate(yesterday.getDate()-1);if(dateObj.getTime()>today.getTime())return dateObj.toLocaleTimeString();if(dateObj.getTime()>yesterday.getTime())return"yesterday, "+dateObj.toLocaleTimeString();return dateObj.toLocaleString()},dom:{"fileUploadCancel":"Cancel","neterror":"Cannot connect to chat !"}};var R={id:{chanList:"chanList",context:"slackCtx",chatList:"chatList",currentRoom:{title:"currentRoomTitle",content:"chatWindow"},typing:"whoistyping",message:{form:"msgForm",slashComplete:"slashList",input:"msgInput",replyTo:"replyToContainer",file:{bt:"attachFile",formContainer:"fileUploadContainer",fileInput:"fileUploadInput",form:"fileUploadForm",error:"fileUploadError",cancel:"fileUploadCancel"},emoji:"emojiButton"},favicon:"linkFavicon"},klass:{selected:"selected",hidden:"hidden",noRoomSelected:"no-room-selected",
  34. noNetwork:"no-network",unread:"unread",unreadHi:"unreadHi",replyingTo:"replyingTo",presenceAway:"away",typing:{container:"typing-container",dot1:"typing-dot1",dot2:"typing-dot2",dot3:"typing-dot3"},emoji:{emoji:"emoji",small:"emoji-small",medium:"emoji-medium",custom:"emoji-custom"},commands:{item:"slack-command-item",header:"slack-command-header",name:"slack-command-name",usage:"slack-command-usage",desc:"slack-command-desc"},emojibar:{container:"emojibar",emojis:"emojibar-emojis",close:"emojibar-close",
  35. header:"emojibar-header",list:"emojibar-list",item:"emojibar-list-item",search:"emojibar-search",overlay:"emojibar-overlay",detail:{container:"emojibar-detail",img:"emojibar-detail-img",name:"emojibar-detail-name"}},chatList:{entry:"slack-context-room",typeChannel:"slack-channel",typePrivate:"slack-group",typeDirect:"slack-ims",typing:"slack-context-typing"},msg:{item:"slackmsg-item",notice:"slackmsg-notice",firstUnread:"slackmsg-first-unread",content:"slackmsg-content",meMessage:"slackmsg-me_message",
  36. ts:"slackmsg-ts",author:"slackmsg-author",authorname:"slackmsg-author-name",authorAvatar:"slackmsg-author-img",msg:"slackmsg-msg",edited:"slackmsg-edited",same:{author:"slackmsg-same-author",ts:"slackmsg-same-ts"},hover:{container:"slackmsg-hover",reply:"slackmsg-hover-reply",reaction:"slackmsg-hover-reaction",edit:"slackmsg-hover-edit",remove:"slackmsg-hover-remove"},replyTo:{close:"replyto-close"},link:"slackmsg-link",linkuser:"slackmsg-link-user",linkchan:"slackmsg-link-chan",attachment:{container:"slackmsg-attachment",
  37. list:"slackmsg-attachments",pretext:"slackmsg-attachment-pretext",block:"slackmsg-attachment-block",title:"slackmsg-attachment-title",text:"slackmsg-attachment-text",thumbImg:"slackmsg-attachment-thumb",img:"slackmsg-attachment-img",footer:"slackmsg-attachment-footer",footerText:"slackmsg-attachment-footer-text",footerIcon:"slackmsg-attachment-footer-icon"},reactions:{container:"slackmsg-reactions",item:"slackmsg-reaction-item"},style:{bold:"slackmsg-style-bold",code:"slackmsg-style-code",italic:"slackmsg-style-italic",
  38. strike:"slackmsg-style-strike",quote:"slackmsg-style-quote"}}}};var NOTIFICATION_COOLDOWN=30*1E3,NOTIFICATION_DELAY=5*1E3,lastNotificationSpawn=0;
  39. function onContextUpdated(){var chanListFram=document.createDocumentFragment(),sortedChans=SLACK.context.self?Object.keys(SLACK.context.self.channels):[];sortedChans.sort(function(a,b){if(a[0]!==b[0])return a[0]-b[0];return SLACK.context.channels[a].name.localeCompare(SLACK.context.channels[b].name)});sortedChans.forEach(function(chanId){var chan=SLACK.context.channels[chanId];if(!chan.archived){var chanListItem=createChanListItem(chan);if(chanListItem)chanListFram.appendChild(chanListItem)}});var sortedUsers=
  40. SLACK.context.users?Object.keys(SLACK.context.users):[];sortedUsers.sort(function(a,b){return SLACK.context.users[a].name.localeCompare(SLACK.context.users[b].name)});sortedUsers.forEach(function(userId){var user=SLACK.context.users[userId];if(!user.deleted&&user.privateRoom){var imsListItem=createImsListItem(user.privateRoom);if(imsListItem)chanListFram.appendChild(imsListItem)}});document.getElementById(R.id.chanList).textContent="";document.getElementById(R.id.chanList).appendChild(chanListFram);
  41. setRoomFromHashBang();updateTitle();createContextBackground(function(imgData){document.getElementById(R.id.context).style.backgroundImage="url("+imgData+")"})}
  42. function onTypingUpdated(){var typing=SLACK.context.typing;for(var chanId in SLACK.context.self.channels)if(!SLACK.context.self.channels[chanId].archived){var dom=document.getElementById(chanId);if(typing[chanId])dom.classList.add(R.klass.chatList.typing);else dom.classList.remove(R.klass.chatList.typing)}for(var userId in SLACK.context.users){var ims=SLACK.context.users[userId].ims;if(ims&&!ims.archived){var dom=document.getElementById(ims.id);if(typing[ims.id])dom.classList.add(R.klass.chatList.typing);
  43. else dom.classList.remove(R.klass.chatList.typing)}}updateTypingChat()}
  44. function updateTypingChat(){var typing=SLACK.context.typing;if(SELECTED_ROOM&&typing[SELECTED_ROOM.id]){var typingUserNames=[],isOutOfSync=false;for(var i in typing[SELECTED_ROOM.id]){var member=SLACK.context.users[i];if(member)typingUserNames.push(member.name);else isOutOfSync=true}if(isOutOfSync)outOfSync();document.getElementById(R.id.typing).textContent=locale.areTyping(typingUserNames)}else document.getElementById(R.id.typing).textContent=""}
  45. function onNetworkStateUpdated(isNetworkWorking){isNetworkWorking?document.body.classList.remove(R.klass.noNetwork):document.body.classList.add(R.klass.noNetwork);updateTitle()}
  46. function onRoomSelected(){var name=SELECTED_ROOM.name||(SELECTED_ROOM.user?SELECTED_ROOM.user.name:undefined);if(!name){var members=[];SELECTED_ROOM.users.forEach(function(i){members.push(i.name)});name=members.join(", ")}var roomLi=document.getElementById(SELECTED_ROOM.id);document.getElementById(R.id.currentRoom.title).textContent=name;onRoomUpdated();focusInput();document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);markRoomAsRead(SELECTED_ROOM);if(REPLYING_TO){REPLYING_TO=
  47. null;onReplyingToUpdated()}if(EDITING){EDITING=null;onReplyingToUpdated()}updateTypingChat()}
  48. function onReplyingToUpdated(){if(REPLYING_TO){document.body.classList.add(R.klass.replyingTo);var domParent=document.getElementById(R.id.message.replyTo),closeLink=document.createElement("a");closeLink.addEventListener("click",function(){REPLYING_TO=null;onReplyingToUpdated()});closeLink.className=R.klass.msg.replyTo.close;closeLink.textContent="x";domParent.textContent="";domParent.appendChild(closeLink);domParent.appendChild(createMessageDom("reply_"+SELECTED_ROOM.id,REPLYING_TO,true));focusInput()}else{document.body.classList.remove(R.klass.replyingTo);
  49. document.getElementById(R.id.message.replyTo).textContent="";focusInput()}}
  50. function onEditingUpdated(){if(EDITING){document.body.classList.add(R.klass.replyingTo);var domParent=document.getElementById(R.id.message.replyTo),closeLink=document.createElement("a");closeLink.addEventListener("click",function(){EDITING=null;onEditingUpdated()});closeLink.className=R.klass.msg.replyTo.close;closeLink.textContent="x";domParent.textContent="";domParent.appendChild(closeLink);domParent.appendChild(createMessageDom("edit_"+SELECTED_ROOM.id,EDITING,true));document.getElementById(R.id.message.input).value=
  51. EDITING.text;focusInput()}else{document.body.classList.remove(R.klass.replyingTo);document.getElementById(R.id.message.replyTo).textContent="";focusInput()}}window["toggleReaction"]=function(chanId,msgId,reaction){var hist=SLACK.history[chanId];if(!hist)return;var msg=hist.getMessageById(msgId);if(msg)if(msg.hasReactionForUser(reaction,SLACK.context.self.id))removeReaction(chanId,msgId,reaction);else addReaction(chanId,msgId,reaction)};
  52. function tryGetCustomEmoji(emoji){var loop={};while(!loop[emoji]){var emojisrc=SLACK.context.emojis[emoji];if(emojisrc)if(emojisrc.substr(0,6)=="alias:"){loop[emoji]=true;emoji=emojisrc.substr(6)}else{var dom=document.createElement("span");dom.className=R.klass.emoji.custom+" "+R.klass.emoji.emoji;dom.style.backgroundImage="url('"+emojisrc+"')";return dom}return emoji}return emoji}
  53. function makeEmojiDom(emojiCode){var emoji=tryGetCustomEmoji(emojiCode);if(typeof emoji==="string"&&"makeEmoji"in window)emoji=window["makeEmoji"](emoji);return typeof emoji==="string"?null:emoji}
  54. function formatEmojis(inputString){return inputString.replace(/:([^ \t:]+):/g,function(returnFailed,emoji){var emojiDom=makeEmojiDom(emoji);if(emojiDom){var domParent=document.createElement("span");domParent.className=returnFailed===inputString?R.klass.emoji.medium:R.klass.emoji.small;domParent.appendChild(emojiDom);return domParent.outerHTML}return returnFailed})}
  55. function formatText(fullText){var msgContents=fullText.split(/\r?\n/g);for(var msgContentIndex=0,nbMsgContents=msgContents.length;msgContentIndex<nbMsgContents;msgContentIndex++){var msgContent=msgContents[msgContentIndex].trim(),_msgContent="",currentMods={},quote=false,i=0;msgContent=msgContent.replace(new RegExp("<([@#]?)([^>]*)>","g"),function(matched,type,entity){var sub=entity.split("|");if(type==="@"){if(!sub[1]){var user=SLACK.context.users[sub[0]];sub[1]=user?"@"+user.name:locale.unknownMember}else if("@"!==
  56. sub[1][0])sub[1]="@"+sub[1];sub[0]="#"+sub[0];sub[2]=R.klass.msg.link+" "+R.klass.msg.linkuser}else if(type==="#"){if(!sub[1]){var chan=SLACK.context.channels[sub[0]];sub[1]=chan?"#"+chan.name:locale.unknownChannel}else if("#"!==sub[1][0])sub[1]="#"+sub[1];sub[0]="#"+sub[0];sub[2]=R.klass.msg.link+" "+R.klass.msg.linkchan}else if(sub[0].indexOf("://")!==-1){if(!sub[1])sub[1]=sub[0];sub[2]=R.klass.msg.link}else return matched;return'<a href="'+sub[0]+'" class="'+sub[2]+'"'+(!type?' target="_blank"':
  57. "")+">"+sub[1]+"</a>"});msgContent=formatEmojis(msgContent);var msgLength=msgContent.length;var isAlphadec=function(c){return c>="A"&&c<="Z"||c>="a"&&c<="z"||c>="0"&&c<="9"||"\u00e0\u00e8\u00ec\u00f2\u00f9\u00c0\u00c8\u00cc\u00d2\u00d9\u00e1\u00e9\u00ed\u00f3\u00fa\u00fd\u00c1\u00c9\u00cd\u00d3\u00da\u00dd\u00e2\u00ea\u00ee\u00f4\u00fb\u00c2\u00ca\u00ce\u00d4\u00db\u00e3\u00f1\u00f5\u00c3\u00d1\u00d5\u00e4\u00eb\u00ef\u00f6\u00fc\u00ff\u00c4\u00cb\u00cf\u00d6\u00dc\u0178\u00e7\u00c7\u00df\u00d8\u00f8\u00c5\u00e5\u00c6\u00e6\u0153".indexOf(c)!==
  58. -1},checkEnd=function(str,pos,c){while(str[pos]){if(isAlphadec(str[pos])&&str[pos]!=c&&str[pos+1]==c)return true;pos++}return false},appendMod=function(mods){if(!Object.keys(currentMods).length)return"";return'<span class="'+Object.keys(mods).join(" ")+'">'};while(i<msgLength&&(msgContent[i]===" "||msgContent[i]==="\t"))i++;if(msgContent.substr(i,4)==="&gt;"){quote=true;i+=4}for(;i<msgLength;i++){var c=msgContent[i];if(c==="<"){do _msgContent+=msgContent[i++];while(msgContent[i-1]!==">");i--;continue}if(!currentMods[R.klass.msg.style.bold]&&
  59. c==="*"&&msgContent[i+1]&&checkEnd(msgContent,i,c)){if(Object.keys(currentMods).length)_msgContent+="</span>";currentMods[R.klass.msg.style.bold]=true;_msgContent+=appendMod(currentMods)}else if(!currentMods[R.klass.msg.style.strike]&&c==="~"&&msgContent[i+1]&&checkEnd(msgContent,i,c)){if(Object.keys(currentMods).length)_msgContent+="</span>";currentMods[R.klass.msg.style.strike]=true;_msgContent+=appendMod(currentMods)}else if(!currentMods[R.klass.msg.style.code]&&c==="`"&&msgContent[i+1]&&checkEnd(msgContent,
  60. i,c)){if(Object.keys(currentMods).length)_msgContent+="</span>";currentMods[R.klass.msg.style.code]=true;_msgContent+=appendMod(currentMods)}else if(!currentMods[R.klass.msg.style.italic]&&c==="_"&&msgContent[i+1]&&checkEnd(msgContent,i,c)){if(Object.keys(currentMods).length)_msgContent+="</span>";currentMods[R.klass.msg.style.italic]=true;_msgContent+=appendMod(currentMods)}else{var finalFound=false;_msgContent+=c;do{if(currentMods[R.klass.msg.style.bold]&&c!=="*"&&msgContent[i+1]==="*"){delete currentMods[R.klass.msg.style.bold];
  61. finalFound=true}else if(currentMods[R.klass.msg.style.strike]&&c!=="~"&&msgContent[i+1]==="~"){delete currentMods[R.klass.msg.style.strike];finalFound=true}else if(currentMods[R.klass.msg.style.code]&&c!=="`"&&msgContent[i+1]==="`"){delete currentMods[R.klass.msg.style.code];finalFound=true}else if(currentMods[R.klass.msg.style.italic]&&c!=="_"&&msgContent[i+1]==="_"){delete currentMods[R.klass.msg.style.italic];finalFound=true}else break;c=msgContent[++i]}while(i<msgLength);if(finalFound)_msgContent+=
  62. "</span>"+appendMod(currentMods)}}if(currentMods)_msgContent+="</span>";if(quote)msgContents[msgContentIndex]='<span class="'+R.klass.msg.style.quote+'">'+_msgContent+"</span>";else msgContents[msgContentIndex]=_msgContent}return msgContents.join("<br/>")}function doCreateMeMessageDom(channelId,msg,skipAttachment){var dom=doCreateMessageDom(channelId,msg,skipAttachment);dom.classList.add(R.klass.msg.meMessage);return dom}
  63. function createMessageDom(channelId,msg,skipAttachment){var dom=msg.isMeMessage?doCreateMeMessageDom(channelId,msg,skipAttachment):doCreateMessageDom(channelId,msg,skipAttachment);if(msg.edited)dom.classList.add(R.klass.msg.edited);if(msg.notice)dom.classList.add(R.klass.msg.notice);return dom}
  64. function setFavicon(unreadhi,unread){if(!unreadhi&&!unread)document.getElementById(R.id.favicon).href="favicon_ok.png";else document.getElementById(R.id.favicon).href="favicon.png?h="+unreadhi+"&m="+unread}function setNetErrorFavicon(){document.getElementById(R.id.favicon).href="favicon_err.png"}
  65. function updateTitle(){var hasHl=HIGHLIGHTED_CHANS.length,title="";if(NEXT_RETRY){title="!"+locale.netErrorShort+" - ";setNetErrorFavicon()}else if(hasHl){title="(!"+hasHl+") - ";setFavicon(hasHl,hasHl)}else{var hasUnread=0;for(var chanId in SLACK.context.channels){var i=SLACK.context.channels[chanId];if(i.lastMsg>i.lastRead)hasUnread++}if(hasUnread)title="("+hasUnread+") - ";setFavicon(0,hasUnread)}if(SLACK.context.team)title+=SLACK.context.team.name;document.title=title}
  66. function spawnNotification(){if(!("Notification"in window));else if(Notification.permission==="granted"){var now=Date.now();if(lastNotificationSpawn+NOTIFICATION_COOLDOWN<now){var n=new Notification(locale.newMessage);lastNotificationSpawn=now;setTimeout(function(){n.close()},NOTIFICATION_DELAY)}}else if(Notification.permission!=="denied")Notification.requestPermission()}
  67. function onRoomUpdated(){var chatFrag=document.createDocumentFragment(),currentRoomId=SELECTED_ROOM.id,prevMsg=null,firstTsCombo=0,prevMsgDom=null;if(SLACK.history[currentRoomId])SLACK.history[currentRoomId].messages.forEach(function(msg){if(!msg.removed){var dom=createMessageDom(currentRoomId,msg);if(prevMsg&&prevMsg.userId===msg.userId&&msg.userId){dom.classList.add(R.klass.msg.same.author);if(Math.abs(firstTsCombo-msg.ts)<30)prevMsgDom.classList.add(R.klass.msg.same.ts);else firstTsCombo=msg.ts}else firstTsCombo=
  68. msg.ts;if((!prevMsg||prevMsg.ts<=SELECTED_ROOM.lastRead)&&msg.ts>SELECTED_ROOM.lastRead)dom.classList.add(R.klass.msg.firstUnread);prevMsg=msg;prevMsgDom=dom;chatFrag.appendChild(dom)}});var content=document.getElementById(R.id.currentRoom.content);content.textContent="";content.appendChild(chatFrag);content.scrollTop=content.scrollHeight-content.clientHeight;if(window.hasFocus)markRoomAsRead(SELECTED_ROOM)}
  69. function chatClickDelegate(e){var target=e.target,getMessageId=function(e,target){target=target||e.target;while(target!==e.currentTarget&&target){if(target.classList.contains(R.klass.msg.item))return target.id;target=target.parentElement}};while(target!==e.currentTarget&&target){if(target.classList.contains(R.klass.msg.hover.container))return;else if(target.parentElement&&target.parentElement.classList.contains(R.klass.msg.hover.container)){var messageId=getMessageId(e,target);if(messageId){messageId=
  70. parseFloat(messageId.split("_")[1]);var msg=SLACK.history[SELECTED_ROOM.id].getMessage(messageId);if(msg&&target.classList.contains(R.klass.msg.hover.reply)){if(EDITING){EDITING=null;onEditingUpdated()}if(REPLYING_TO!==msg){REPLYING_TO=msg;onReplyingToUpdated()}}else if(msg&&target.classList.contains(R.klass.msg.hover.reaction))EMOJI_BAR.spawn(document.body,function(emoji){if(emoji)addReaction(SELECTED_ROOM.id,msg.id,emoji)});else if(msg&&target.classList.contains(R.klass.msg.hover.edit)){if(REPLYING_TO){REPLYING_TO=
  71. null;onReplyingToUpdated()}if(EDITING!==msg){EDITING=msg;onEditingUpdated()}}else if(msg&&target.classList.contains(R.klass.msg.hover.remove)){if(REPLYING_TO){REPLYING_TO=null;onReplyingToUpdated()}if(EDITING){EDITING=null;onEditingUpdated()}removeMsg(SELECTED_ROOM,msg)}}return}target=target.parentElement}}function focusInput(){document.getElementById(R.id.message.input).focus()}
  72. function setRoomFromHashBang(){var hashId=document.location.hash.substr(1),room=SLACK.context.channels[hashId];if(room&&room!==SELECTED_ROOM)selectRoom(room);else{var user=SLACK.context.users[hashId];if(user&&user.ims)selectRoom(user.ims)}}
  73. document.addEventListener("DOMContentLoaded",function(){initLang();document.getElementById(R.id.currentRoom.content).addEventListener("click",chatClickDelegate);window.addEventListener("hashchange",function(e){if(document.location.hash&&document.location.hash[0]==="#")setRoomFromHashBang()});document.getElementById(R.id.message.file.cancel).addEventListener("click",function(e){e.preventDefault();document.getElementById(R.id.message.file.error).classList.add(R.klass.hidden);document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden);
  74. document.getElementById(R.id.message.file.fileInput).value="";return false});document.getElementById(R.id.message.file.form).addEventListener("submit",function(e){e.preventDefault();var fileInput=document.getElementById(R.id.message.file.fileInput),filename=fileInput.value;if(filename){filename=filename.substr(filename.lastIndexOf("\\")+1);uploadFile(SELECTED_ROOM,filename,fileInput.files[0],function(errorMsg){var error=document.getElementById(R.id.message.file.error);if(errorMsg){error.textContent=
  75. errorMsg;error.classList.remove(R.klass.hidden)}else{error.classList.add(R.klass.hidden);document.getElementById(R.id.message.file.fileInput).value="";document.getElementById(R.id.message.file.formContainer).classList.add(R.klass.hidden)}})}return false});document.getElementById(R.id.message.file.bt).addEventListener("click",function(e){e.preventDefault();if(SELECTED_ROOM)document.getElementById(R.id.message.file.formContainer).classList.remove(R.klass.hidden);return false});document.getElementById(R.id.message.form).addEventListener("submit",
  76. function(e){e.preventDefault();var input=document.getElementById(R.id.message.input);if(SELECTED_ROOM&&input.value)if(onTextEntered(input.value)){input.value="";if(REPLYING_TO){REPLYING_TO=null;onReplyingToUpdated()}if(EDITING){EDITING=null;onReplyingToUpdated()}document.getElementById(R.id.message.slashComplete).textContent=""}focusInput();return false});window.addEventListener("blur",function(){window.hasFocus=false});window.addEventListener("focus",function(){window.hasFocus=true;lastNotificationSpawn=
  77. 0;if(SELECTED_ROOM)markRoomAsRead(SELECTED_ROOM);focusInput()});var lastKeyDown=0;document.getElementById(R.id.message.input).addEventListener("input",function(){if(SELECTED_ROOM){var now=Date.now();if(lastKeyDown+3E3<now&&(SLACK.context.self.presence||SELECTED_ROOM instanceof PrivateMessageRoom)){sendTyping(SELECTED_ROOM);lastKeyDown=now}var commands=[],input=this.value;if(this.value[0]==="/"){var endCmd=input.indexOf(" "),inputFinished=endCmd!==-1;endCmd=endCmd===-1?input.length:endCmd;var inputCmd=
  78. input.substr(0,endCmd);for(var i in SLACK.context.commands.data){var currentCmd=SLACK.context.commands.data[i];if(!inputFinished&&currentCmd.name.substr(0,endCmd)===inputCmd||inputFinished&&currentCmd.name===inputCmd)commands.push(currentCmd)}}commands.sort(function(a,b){return a.category.localeCompare(b.category)||a.name.localeCompare(b.name)});var slashDom=document.getElementById(R.id.message.slashComplete),slashFrag=document.createDocumentFragment(),prevService;slashDom.textContent="";for(var i=
  79. 0,nbCmd=commands.length;i<nbCmd;i++){var command=commands[i];if(prevService!==command.category){prevService=command.category;slashFrag.appendChild(createSlashAutocompleteHeader(command.category))}slashFrag.appendChild(createSlashAutocompleteDom(command))}slashDom.appendChild(slashFrag)}});window.hasFocus=true;(function(){var emojiButton=document.getElementById(R.id.message.emoji);if("makeEmoji"in window){var emojiDom=window["makeEmoji"]("smile");if(emojiDom)emojiButton.innerHTML="<span class='"+R.klass.emoji.small+
  80. "'>"+emojiDom.outerHTML+"</span>";else emojiButton.style.backgroundImage='url("smile.svg")';emojiDom=window["makeEmoji"]("paperclip");if(emojiDom)document.getElementById(R.id.message.file.bt).innerHTML="<span class='"+R.klass.emoji.small+"'>"+emojiDom.outerHTML+"</span>";else document.getElementById(R.id.message.file.bt).style.backgroundImage='url("public/paperclip.svg")';emojiButton.addEventListener("click",function(){EMOJI_BAR.spawn(document.body,function(e){if(e)document.getElementById(R.id.message.input).value+=
  81. ":"+e+":";focusInput()})})}else emojiButton.classList.add(R.klass.hidden)})();startPolling()});function createTypingDisplay(){var dom=document.createElement("span"),dot1=document.createElement("span"),dot2=document.createElement("span"),dot3=document.createElement("span");dom.className=R.klass.typing.container;dot1.className=R.klass.typing.dot1;dot2.className=R.klass.typing.dot2;dot3.className=R.klass.typing.dot3;dot1.textContent=dot2.textContent=dot3.textContent=".";dom.appendChild(dot1);dom.appendChild(dot2);dom.appendChild(dot3);return dom}
  82. function createChanListItem(chan){var dom=document.createElement("li"),link=document.createElement("a");dom.id=chan.id;link.href="#"+chan.id;if(chan.isPrivate){dom.className=R.klass.chatList.entry+" "+R.klass.chatList.typePrivate;dom.dataset["count"]=chan.users.length}else dom.className=R.klass.chatList.entry+" "+R.klass.chatList.typeChannel;if(SELECTED_ROOM===chan)dom.classList.add(R.klass.selected);link.textContent=chan.name;dom.appendChild(createTypingDisplay());dom.appendChild(link);if(chan.lastMsg>
  83. chan.lastRead){dom.classList.add(R.klass.unread);if(HIGHLIGHTED_CHANS.indexOf(chan)>=0)dom.classList.add(R.klass.unreadHi)}return dom}
  84. function createImsListItem(ims){var dom=document.createElement("li"),link=document.createElement("a");dom.id=ims.id;link.href="#"+ims.id;dom.className=R.klass.chatList.entry+" "+R.klass.chatList.typeDirect;link.textContent=ims.user.name;dom.appendChild(createTypingDisplay());dom.appendChild(link);if(!ims.user.presence)dom.classList.add(R.klass.presenceAway);if(SELECTED_ROOM===ims)dom.classList.add(R.klass.selected);if(ims.lastMsg>ims.lastRead){dom.classList.add(R.klass.unread);if(HIGHLIGHTED_CHANS.indexOf(ims)>=
  85. 0)dom.classList.add(R.klass.unreadHi)}return dom}
  86. function createReactionDom(chanId,msgId,reaction,users){var emojiDom=makeEmojiDom(reaction);if(emojiDom){var dom=document.createElement("li"),a=document.createElement("a"),emojiContainer=document.createElement("span"),userList=document.createElement("span"),userNames=[];for(var i=0,nbUser=users.length;i<nbUser;i++){var user=SLACK.context.users[users[i]];if(user)userNames.push(user.name)}userNames.sort();userList.textContent=userNames.join(", ");emojiContainer.appendChild(emojiDom);emojiContainer.className=
  87. R.klass.emoji.small;a.href="javascript:toggleReaction('"+chanId+"', '"+msgId+"', '"+reaction+"')";a.appendChild(emojiContainer);a.appendChild(userList);dom.className=R.klass.msg.reactions.item;dom.appendChild(a);return dom}return null}
  88. function doCreateMessageDom(channelId,msg,skipAttachment){var dom=document.createElement("div"),msgBlock=document.createElement("div"),ts=document.createElement("div"),text=document.createElement("div"),authorImg=document.createElement("img"),authorName=document.createElement("span"),hover=document.createElement("ul"),hoverReply=document.createElement("li"),attachments=document.createElement("ul"),reactions=document.createElement("ul"),sender=SLACK.context.users[msg.userId];dom.id=channelId+"_"+msg.ts;
  89. dom.className=R.klass.msg.item;ts.className=R.klass.msg.ts;text.className=R.klass.msg.msg;authorImg.className=R.klass.msg.authorAvatar;authorName.className=R.klass.msg.authorname;hover.className=R.klass.msg.hover.container;hoverReply.className=R.klass.msg.hover.reply;ts.innerHTML=locale.formatDate(msg.ts);text.innerHTML=formatText(msg.text);authorName.textContent=sender?sender.name:msg.username||"?";authorImg.src=sender?sender.icons.small:"";hover.appendChild(hoverReply);if("makeEmoji"in window){var hoverReaction=
  90. document.createElement("li"),domReply=window["makeEmoji"]("arrow_heading_down"),domReaction=window["makeEmoji"]("smile"),domEdit=window["makeEmoji"]("pencil2"),domRemove=window["makeEmoji"]("x");hoverReaction.className=R.klass.msg.hover.reaction;if(domReaction){hoverReaction.classList.add(R.klass.emoji.small);hoverReaction.appendChild(domReaction)}else hoverReaction.style.backgroundImage='url("smile.svg")';if(domReply){hoverReply.classList.add(R.klass.emoji.small);hoverReply.appendChild(domReply)}else hoverReply.style.backgroundImage=
  91. 'url("repl.svg")';hover.appendChild(hoverReaction);if(msg.userId===SLACK.context.self.id){var hoverEdit=document.createElement("li");hoverEdit.className=R.klass.msg.hover.edit;if(domEdit)hoverEdit.classList.add(R.klass.emoji.small);else hoverEdit.style.backgroundImage='url("edit.svg")';hoverEdit.appendChild(domEdit);hover.appendChild(hoverEdit);var hoverRemove=document.createElement("li");hoverRemove.className=R.klass.msg.hover.remove;if(domRemove)hoverRemove.classList.add(R.klass.emoji.small);else hoverRemove.style.backgroundImage=
  92. 'url("remove.svg")';hoverRemove.appendChild(domRemove);hover.appendChild(hoverRemove)}}else{hoverReply.style.backgroundImage='url("repl.svg")';if(msg.userId===SLACK.context.self.id){var hoverEdit=document.createElement("li");hoverEdit.className=R.klass.msg.hover.edit;hoverEdit.style.backgroundImage='url("edit.svg")';hover.appendChild(hoverEdit);var hoverRemove=document.createElement("li");hoverRemove.className=R.klass.msg.hover.remove;hoverRemove.style.backgroundImage='url("remove.svg")';hover.appendChild(hoverRemove)}}dom.appendChild(authorImg);
  93. if(msg.notice){var onlyVisible=document.createElement("span");onlyVisible.className=R.klass.msg.notice;onlyVisible.textContent=locale.onlyVisible;msgBlock.appendChild(onlyVisible)}msgBlock.appendChild(authorName);msgBlock.appendChild(text);msgBlock.appendChild(ts);msgBlock.appendChild(attachments);if(msg.edited){var edited=document.createElement("div");edited.textContent=locale.edited;edited.className=R.klass.msg.edited;msgBlock.appendChild(edited)}msgBlock.appendChild(reactions);msgBlock.className=
  94. R.klass.msg.content;attachments.className=R.klass.msg.attachment.list;reactions.className=R.klass.msg.reactions.container;if(skipAttachment!==true){if(msg.reactions)for(var reaction in msg.reactions){var reac=createReactionDom(channelId,msg.id,reaction,msg.reactions[reaction]);reac&&reactions.appendChild(reac)}msg.attachments.forEach(function(attachment){var domAttachment=createAttachmentDom(channelId,msg,attachment);if(domAttachment)attachments.appendChild(domAttachment)})}dom.appendChild(msgBlock);
  95. dom.appendChild(hover);return dom}
  96. function createAttachmentDom(channelId,msg,attachment){var rootDom=document.createElement("li"),attachmentBlock=document.createElement("div"),pretext=document.createElement("div"),titleBlock=document.createElement("a"),authorBlock=document.createElement("div"),authorImg=document.createElement("img"),authorName=document.createElement("a"),textBlock=document.createElement("div"),textDom=document.createElement("div"),thumbImgDom=document.createElement("img"),imgDom=document.createElement("img"),footerBlock=
  97. document.createElement("div"),footerIcon=document.createElement("img"),footerText=document.createElement("span"),footerTs=document.createElement("span");rootDom.className=R.klass.msg.attachment.container;var color="#e3e4e6";if(attachment["color"])if(attachment["color"][0]==="#")color=attachment["color"][0];else if(attachment["color"]==="good")color="#2fa44f";else if(attachment["color"]==="warning")color="#de9e31";else if(attachment["color"]==="danger")color="#d50200";attachmentBlock.style.borderColor=
  98. color;attachmentBlock.className=R.klass.msg.attachment.block;pretext.className=R.klass.msg.attachment.pretext;if(attachment["pretext"])pretext.innerHTML=formatText(attachment["pretext"]);else pretext.classList.add(R.klass.hidden);titleBlock.target="_blank";if(attachment["title"]){titleBlock.innerHTML=formatText(attachment["title"]);if(attachment["title_link"])titleBlock.href=attachment["title_link"];titleBlock.className=R.klass.msg.attachment.title}else titleBlock.className=R.klass.hidden+" "+R.klass.msg.attachment.title;
  99. authorName.target="_blank";authorBlock.className=R.klass.msg.author;if(attachment["author_name"]){authorName.innerHTML=formatText(attachment["author_name"]);authorName.href=attachment["author_link"]||"";authorName.className=R.klass.msg.authorname;authorImg.className=R.klass.msg.authorAvatar;if(attachment["author_icon"])authorImg.src=attachment["author_icon"];else authorImg.classList.add(R.klass.hidden)}else authorBlock.classList.add(R.klass.hidden);textDom.innerHTML=formatText(attachment["text"]||
  100. "");textDom.klassName=R.klass.msg.attachment.text;thumbImgDom.className=R.klass.msg.attachment.thumbImg;if(attachment["thumb_url"])thumbImgDom.src=attachment["thumb_url"];else thumbImgDom.classList.add(R.klass.hidden);imgDom.className=R.klass.msg.attachment.img;if(attachment["image_url"])imgDom.src=attachment["image_url"];else imgDom.classList.add(R.klass.hidden);footerBlock.className=R.klass.msg.attachment.footer;footerText.className=R.klass.msg.attachment.footerText;footerIcon.className=R.klass.msg.attachment.footerIcon;
  101. if(attachment["footer"]){footerText.innerHTML=formatText(attachment["footer"]);if(attachment["footer_icon"])footerIcon.src=attachment["footer_icon"];else footerIcon.classList.add(R.klass.hidden)}else{footerIcon.classList.add(R.klass.hidden);footerText.classList.add(R.klass.hidden)}footerTs.className=R.klass.msg.ts;if(attachment["ts"])footerTs.innerHTML=locale.formatDate(attachment["ts"]);else footerTs.classList.add(R.klass.hidden);authorBlock.appendChild(authorImg);authorBlock.appendChild(authorName);
  102. textBlock.appendChild(textDom);textBlock.appendChild(thumbImgDom);footerBlock.appendChild(footerIcon);footerBlock.appendChild(footerText);footerBlock.appendChild(footerTs);attachmentBlock.appendChild(titleBlock);attachmentBlock.appendChild(authorBlock);attachmentBlock.appendChild(textBlock);attachmentBlock.appendChild(imgDom);attachmentBlock.appendChild(footerBlock);rootDom.appendChild(pretext);rootDom.appendChild(attachmentBlock);return rootDom}
  103. function createSlashAutocompleteHeader(servicename){var lh=document.createElement("lh");lh.textContent=servicename;lh.className=R.klass.commands.header;return lh}
  104. function createSlashAutocompleteDom(cmd){var li=document.createElement("li"),name=document.createElement("span"),usage=document.createElement("span"),desc=document.createElement("span");name.textContent=cmd.name;usage.textContent=cmd.usage;desc.textContent=cmd.desc;li.appendChild(name);li.appendChild(usage);li.appendChild(desc);li.className=R.klass.commands.item;name.className=R.klass.commands.name;usage.className=R.klass.commands.usage;desc.className=R.klass.commands.desc;return li};var EMOJI_BAR=function(){var dom=document.createElement("div"),overlay=document.createElement("div"),emojisDom=document.createElement("div"),unicodeEmojis=document.createElement("ul"),customEmojis=document.createElement("ul"),searchBar=document.createElement("input"),emojiCache={unicode:{},custom:{}},emojiDetail=document.createElement("div"),emojiDetailImg=document.createElement("span"),emojiDetailName=document.createElement("span"),emojiSelectedHandler,isSupported=function(){return"searchEmojis"in
  105. window},makeHeader=function(imgSrc){var img=document.createElement("img"),dom=document.createElement("div");img.src=imgSrc;dom.appendChild(img);dom.className=R.klass.emojibar.header;return dom},wrapEmojiLi=function(emojiName,emojiDom){var dom=document.createElement("li");dom.appendChild(emojiDom);dom.className=R.klass.emojibar.item;dom.id="emojibar-"+emojiName;return{visible:false,dom:dom}},makeUnicodeEmojiLi=function(emojiName,emoji){var domEmoji=window["makeEmoji"](emoji),domParent=document.createElement("span");
  106. domParent.appendChild(domEmoji);domParent.className=R.klass.emoji.medium;return wrapEmojiLi(emojiName,domParent)},makeCustomEmoji=function(emojiName,emojiSrc){var domEmoji=document.createElement("span"),domParent=document.createElement("span");domEmoji.className=R.klass.emoji.emoji+" "+R.klass.emoji.custom;domEmoji.style.backgroundImage='url("'+emojiSrc+'")';domParent.appendChild(domEmoji);domParent.className=R.klass.emoji.medium;return wrapEmojiLi(emojiName,domParent)},sortEmojis=function(emojiObj,
  107. favoriteEmojis){var names=[],index=0;for(var i in emojiObj){var obj={name:i,pos:index,count:0};emojiObj[i].names.forEach(function(name){obj.count+=favoriteEmojis[name]||0});names.push(obj)}names=names.sort(function(a,b){var diff=b.count-a.count;if(diff)return diff;return a.pos-b.pos});return names},search=function(queryString){var emojiCount=0,toRemove=[],sortedEmojiNames;queryString=queryString===undefined?searchBar.value:queryString;if(isSupported()){var foundEmojis=window["searchEmojis"](queryString);
  108. sortedEmojiNames=sortEmojis(foundEmojis,SLACK.context.self.prefs.favoriteEmojis);for(var i in emojiCache.unicode)if(emojiCache.unicode[i].visible){emojiCache.unicode[i].visible=false;unicodeEmojis.removeChild(emojiCache.unicode[i].dom)}for(var i=0,nbEmojis=sortedEmojiNames.length;i<nbEmojis;i++){var emojiName=sortedEmojiNames[i].name,e=emojiCache.unicode[emojiName];if(!e)e=emojiCache.unicode[emojiName]=makeUnicodeEmojiLi(emojiName,foundEmojis[emojiName]);if(!e.visible){e.visible=true;unicodeEmojis.appendChild(e.dom)}emojiCount++}}for(var i in emojiCache.custom)if(emojiCache.custom[i].visible){emojiCache.custom[i].visible=
  109. false;customEmojis.removeChild(emojiCache.custom[i].dom)}sortedEmojiNames=sortEmojis(SLACK.context.emojis.data,SLACK.context.self.prefs.favoriteEmojis);for(var i=0,nbEmojis=sortedEmojiNames.length;i<nbEmojis;i++){var emojiName=sortedEmojiNames[i].name;if((queryString===""||emojiName.substr(0,queryString.length)===queryString)&&SLACK.context.emojis.data[emojiName].substr(0,6)!=="alias:"){var e=emojiCache.custom[emojiName];if(!e)e=emojiCache.custom[emojiName]=makeCustomEmoji(emojiName,SLACK.context.emojis.data[emojiName]);
  110. if(!e.visible){e.visible=true;customEmojis.appendChild(e.dom)}emojiCount++}}return emojiCount},spawn=function(domParent,handler){if(isSupported()){emojiSelectedHandler=handler;domParent.appendChild(overlay);domParent.appendChild(dom);searchBar.value="";search();searchBar.focus();return true}return false},doClose=function(){if(dom.parentElement){dom.parentElement.removeChild(overlay);dom.parentElement.removeChild(dom);return true}return false},close=function(){var closed=doClose();if(!closed)return false;
  111. if(emojiSelectedHandler)emojiSelectedHandler(null);return true},onEmojiSelected=function(emojiName){if(doClose()&&emojiSelectedHandler)emojiSelectedHandler(emojiName)};overlay.addEventListener("click",function(e){var bounds=dom.getBoundingClientRect();if(e.screenY<bounds.top||e.screenY>bounds.bottom||e.screenX<bounds.left||e.screenX>bounds.right)close()});overlay.className=R.klass.emojibar.overlay;dom.className=R.klass.emojibar.container;emojisDom.className=R.klass.emojibar.emojis;emojiDetail.className=
  112. R.klass.emojibar.detail.container;emojiDetailImg.className=R.klass.emojibar.detail.img;emojiDetailName.className=R.klass.emojibar.detail.name;unicodeEmojis.className=customEmojis.className=R.klass.emojibar.list;searchBar.className=R.klass.emojibar.search;emojiDetail.appendChild(emojiDetailImg);emojiDetail.appendChild(emojiDetailName);emojisDom.appendChild(makeHeader(window["emojiProviderHeader"]));emojisDom.appendChild(unicodeEmojis);emojisDom.appendChild(makeHeader("emojicustom.png"));emojisDom.appendChild(customEmojis);
  113. dom.appendChild(emojisDom);dom.appendChild(emojiDetail);dom.appendChild(searchBar);searchBar.addEventListener("keyup",function(){search()});var makeDelegate=function(e,cb){var target=e.target;while(target!==dom&&target&&target.nodeName!=="LI")target=target.parentElement;if(target&&target.nodeName==="LI"&&target.id&&target.id.substr(0,"emojibar-".length)==="emojibar-"){var emojiId=target.id.substr("emojibar-".length);return cb(emojiId)}cb(null)};dom.addEventListener("mousemove",function(e){makeDelegate(e,
  114. function(emoji){var emojiCached=emoji?emojiCache.unicode[emoji]||emojiCache.custom[emoji]:null;if(emojiCached){emojiDetailImg.innerHTML=emojiCached.dom.outerHTML;emojiDetailName.textContent=":"+emoji+":"}else{emojiDetailImg.textContent="";emojiDetailName.textContent=""}})});dom.addEventListener("click",function(e){makeDelegate(e,function(emoji){if(emoji)onEmojiSelected(emoji)})});return{isSupported:isSupported,spawn:spawn,search:search,close:close}}();var SLACK,HIGHLIGHTED_CHANS=[];function SlackWrapper(){this.lastServerVersion=0;this.context=new ChatContext;this.history={}}
  115. SlackWrapper.prototype.update=function(data){var now=Date.now();if(data["v"])this.lastServerVersion=data["v"];if(data["static"])this.context.updateStatic(data["static"],Date.now());for(var chanId in this.context.channels){var i=this.context.channels[chanId];if(i.lastMsg===i.lastRead){var pos=HIGHLIGHTED_CHANS.indexOf(i);if(pos!==-1)HIGHLIGHTED_CHANS.splice(pos,1)}}if(data["live"]){for(var i in data["live"]){var history=this.history[i];if(!history)history=this.history[i]=new RoomHistory(i,250,data["live"][i],
  116. now);else history.pushAll(data["live"][i],now)}for(var roomId in data["live"]){var chan=this.context.channels[roomId];if(chan){if(this.history[roomId].messages.length)chan.lastMsg=Math.max(chan.lastMsg,this.history[roomId].lastMessage().ts);if(!chan.archived){onMsgReceived(chan,data["live"][roomId]);if(SELECTED_ROOM&&data["live"][SELECTED_ROOM.id])onRoomUpdated()}}else outOfSync()}}if(data["static"]){onContextUpdated();if(data["static"]["typing"])onTypingUpdated()}};
  117. setInterval(function(){if(SLACK.context.cleanTyping(Date.now()))onTypingUpdated()},1E3);function isHighlighted(text){var highlights=SLACK.context.self.prefs.highlights;for(var i=0,nbHighlights=highlights.length;i<nbHighlights;i++)if(text.indexOf(highlights[i])!==-1)return true;return false}
  118. function onMsgReceived(chan,msg){if(chan!==SELECTED_ROOM||!window.hasFocus){var selfReg=new RegExp("<@"+SLACK.context.self.id),highligted=false,areNew=false,newHighlited=false;msg.forEach(function(i){if(parseFloat(i["ts"])<=chan.lastRead)return;areNew=true;if(chan instanceof PrivateMessageRoom||i.text.match(selfReg)||isHighlighted(i.text)){if(HIGHLIGHTED_CHANS.indexOf(chan)===-1){newHighlited=true;HIGHLIGHTED_CHANS.push(chan)}highligted=true}});if(areNew){updateTitle();var dom=document.getElementById(chan.id);
  119. if(dom){dom.classList.add(R.klass.unread);if(highligted)dom.classList.add(R.klass.unreadHi)}if(newHighlited&&!window.hasFocus)spawnNotification()}}}
  120. function markRoomAsRead(room){var highlightIndex=HIGHLIGHTED_CHANS.indexOf(room);if(room.lastMsg>room.lastRead){sendReadMArker(room,room.lastMsg);room.lastRead=room.lastMsg}if(highlightIndex>=0){HIGHLIGHTED_CHANS.splice(highlightIndex,1);updateTitle()}var roomLi=document.getElementById(room.id);roomLi.classList.remove(R.klass.unread);roomLi.classList.remove(R.klass.unreadHi)}SLACK=new SlackWrapper;var createContextBackground=function(){var canvas=document.createElement("canvas"),ctx=canvas.getContext("2d"),WIDTH=canvas.width=250,HEIGHT=canvas.height=290,MARGIN=20,NB_ITEM=3,ITEM_SIZE=(WIDTH-2*MARGIN)/NB_ITEM,ITEM_SPACING=.1*ITEM_SIZE,ITEM_INNER_SIZE=Math.floor(ITEM_SIZE-ITEM_SPACING*2),AVATAR_SIZE=ITEM_INNER_SIZE*.5,RESULT;var drawItem=function(background,avatar,x0,y0){var y0Index=Math.floor(y0),y1Index=Math.floor(y0+ITEM_SIZE),topColor=[background.data[y0Index*WIDTH*4+0],background.data[y0Index*
  121. WIDTH*4+1],background.data[y0Index*WIDTH*4+2]],botColor=[background.data[y1Index*WIDTH*4+0],background.data[y1Index*WIDTH*4+1],background.data[y1Index*WIDTH*4+2]],targetPcent=1.1,targetColor=(topColor[0]*targetPcent<<16|topColor[1]*targetPcent<<8|topColor[2]*targetPcent).toString(16);ctx.fillStyle="#"+targetColor;ctx.beginPath();ctx.moveTo(x0+ITEM_SIZE/2,y0+ITEM_SPACING);ctx.lineTo(x0-ITEM_SPACING+ITEM_SIZE,y0+ITEM_SIZE/2);ctx.lineTo(x0+ITEM_SIZE/2,y0-ITEM_SPACING+ITEM_SIZE);ctx.lineTo(x0+ITEM_SPACING,
  122. y0+ITEM_SIZE/2);ctx.closePath();ctx.fill();ctx.putImageData(blend(ctx.getImageData(x0+ITEM_SPACING,y0+ITEM_SPACING,ITEM_INNER_SIZE,ITEM_INNER_SIZE),avatar),x0+ITEM_SPACING,y0+ITEM_SPACING)};var blend=function(img,img2){var margin=(img.height-img2.height)/2;for(var i=0;i<img2.height;i++)for(var j=0;j<img2.width;j++){var img2Grey=img2.data[(i*img2.width+j)*4]/255,iImg=((i+margin)*img.width+j+margin)*4;img.data[iImg]*=img2Grey;img.data[iImg+1]*=img2Grey;img.data[iImg+2]*=img2Grey}return img};var drawBackground=
  123. function(){var grd=ctx.createLinearGradient(0,0,0,HEIGHT);grd.addColorStop(0,"#4D394B");grd.addColorStop(1,"#201820");ctx.fillStyle=grd;ctx.fillRect(0,0,WIDTH,HEIGHT);return ctx.getImageData(0,0,WIDTH,HEIGHT)};var filterImage=function(img){var pixelSum=0;for(var i=0;i<img.width*img.height*4;i+=4){img.data[i]=img.data[i+1]=img.data[i+2]=(img.data[i]+img.data[i+1]+img.data[i+2])/3;img.data[i+3]=50;pixelSum+=img.data[i]}if(pixelSum/(img.height*img.width)<50)for(var i=0;i<img.width*img.height*4;i+=4)img.data[i]=
  124. img.data[i+1]=img.data[i+2]=255-img.data[i];return img};var loadImgFromUrl=function(src,cb){var xhr=new XMLHttpRequest;xhr.responseType="blob";xhr.onreadystatechange=function(){if(xhr.readyState===4)if(xhr.response){var img=new Image;img.onload=function(){var imgCanvas=document.createElement("canvas");imgCanvas.height=imgCanvas.width=AVATAR_SIZE;var imgCtx=imgCanvas.getContext("2d");imgCtx.drawImage(img,0,0,AVATAR_SIZE,AVATAR_SIZE);cb(filterImage(imgCtx.getImageData(0,0,AVATAR_SIZE,AVATAR_SIZE)))};
  125. img.onerror=function(){cb(null)};img.src=window.URL.createObjectURL((xhr.response))}else cb(null)};xhr.open("GET",src,true);xhr.send(null)};var loadImages=function(userImgs,doneImgLoading){for(var i=0,nbImgs=userImgs.length;i<nbImgs;i++)if(userImgs[i].img===undefined){loadImgFromUrl(userImgs[i].src,function(img){userImgs[i].img=img;loadImages(userImgs,doneImgLoading)});return}var imgs=[];userImgs.forEach(function(i){if(i.img)imgs.push(i.img)});doneImgLoading(imgs)};var renderAvatars=function(background,
  126. imgs){imgs.sort(function(a,b){return Math.random()-.5});for(var imgIndex=0,i=MARGIN;i<WIDTH-MARGIN*2;i+=ITEM_SIZE)for(var j=0;j+ITEM_SIZE<=HEIGHT;j+=ITEM_SIZE){drawItem(background,imgs[imgIndex],i,j);imgIndex++;if(imgIndex===imgs.length){imgs.sort(function(a,b){if(!a.img)return 1;if(!b.img)return-1;return Math.random()-.5});imgIndex=0}}};var callbacks=[],isLoading=false;return function(cb){if(RESULT)cb(RESULT);else if(isLoading)callbacks.push(cb);else{var background=drawBackground(),userImgs=[];isLoading=
  127. true;callbacks.push(cb);for(var userId in SLACK.context.users)if(!SLACK.context.users[userId].deleted)userImgs.push({src:"api/avatar?user="+userId});loadImages(userImgs,function(imgs){renderAvatars(background,imgs);RESULT=canvas.toDataURL();callbacks.forEach(function(i){i(RESULT)})})}}}();var NEXT_RETRY=0,SELECTED_ROOM=null,REPLYING_TO=null,EDITING=null;function fetchHistory(room,cb){var xhr=new XMLHttpRequest;xhr.open("GET","api/hist?room="+room.id,true);xhr.send(null)}
  128. function poll(callback){var xhr=new XMLHttpRequest;xhr.timeout=1E3*60*1;xhr.onreadystatechange=function(e){if(xhr.readyState===4){if(xhr.status===0){if(NEXT_RETRY){NEXT_RETRY=0;onNetworkStateUpdated(true)}poll(callback);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((resp))}catch(e){resp=null}}else if(NEXT_RETRY){NEXT_RETRY+=Math.floor((NEXT_RETRY||5)/2);NEXT_RETRY=Math.min(60,NEXT_RETRY)}else{NEXT_RETRY=
  129. 5;onNetworkStateUpdated(false)}callback(success,resp)}};xhr.open("GET","api?v="+SLACK.lastServerVersion,true);xhr.send(null)}function outOfSync(){SLACK.lastServerVersion=0}function sendTyping(room){var xhr=new XMLHttpRequest,url="api/typing?room="+room.id;xhr.open("POST",url,true);xhr.send(null)}function onPollResponse(success,response){if(success){if(response)SLACK.update(response);startPolling()}else setTimeout(startPolling,NEXT_RETRY*1E3)}function startPolling(){poll(onPollResponse)}
  130. function selectRoom(room){if(SELECTED_ROOM)unselectRoom();document.getElementById(room.id).classList.add(R.klass.selected);document.body.classList.remove(R.klass.noRoomSelected);SELECTED_ROOM=room;onRoomSelected();if(SELECTED_ROOM.lastRead&&!SLACK.history[SELECTED_ROOM.id])fetchHistory(SELECTED_ROOM,function(success){})}function unselectRoom(){document.getElementById(SELECTED_ROOM.id).classList.remove(R.klass.selected)}
  131. 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)}
  132. 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)}
  133. function sendMsg(chan,msg,replyTo){var xhr=new XMLHttpRequest;var url="api/msg?room="+chan.id+"&text="+encodeURIComponent(msg);if(replyTo){var sender=SLACK.context.users[replyTo.userId],footer="Message";if(chan.id[0]==="C")footer="Channel message";else if(chan.id[0]==="D")footer="Direct message";else if(chan.id[0]==="G")footer="Group message";var attachment={"fallback":replyTo.text,"author_name":"<@"+sender.id+"|"+sender.name+">","author_icon":sender.icons.small,"text":replyTo.text,"footer":footer,
  134. "ts":replyTo.ts};url+="&attachments="+encodeURIComponent(JSON.stringify([attachment]))}xhr.open("POST",url,true);xhr.send(null)}
  135. 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),cmdObject=SLACK.context.commands.data[cmd];if(cmdObject){doCommand(SELECTED_ROOM,cmdObject,args.trim());return true}return false}sendMsg(SELECTED_ROOM,input,REPLYING_TO);return true}
  136. 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)}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)}function sendReadMArker(chan,ts){var xhr=new XMLHttpRequest;var url="api/markread?room="+chan.id+"&ts="+ts;xhr.open("POST",url,true);xhr.send(null)}
  137. 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)}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)};