| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- var
- /**
- * @type SlackWrapper
- **/
- SLACK
- /**
- * @type {Object.<string, {hl: number, unread: number}>} number of unread per chan
- **/
- ,UNREAD_CHANS = {};
- ;
- /**
- * @constructor
- **/
- function SlackWrapper() {
- /** @type {number} */
- this.lastServerVersion = 0;
- /** @type {SlackData} */
- this.context = new SlackData(null);
- /** @type {!Object.<string, SlackHistory>} **/
- this.history = {};
- }
- SlackWrapper.prototype.update = function(data) {
- if (data["v"])
- this.lastServerVersion = data["v"];
- if (data["static"]) {
- this.context.updateStatic(data["static"]);
- onContextUpdated();
- }
- if (data["live"]) {
- for (var i in data["live"]) {
- var history = this.history[i];
- if (!history)
- this.history[i] = new SlackHistory(i, 250, data["live"][i]);
- else
- history.pushAll(data["live"][i]);
- }
- for (var roomId in data["live"]) {
- onMsgReceived(this.context.getChannel(roomId), data["live"][roomId]);
- if (SELECTED_ROOM && data["live"][SELECTED_ROOM.id])
- onRoomUpdated();
- }
- }
- };
- /**
- * @param {SlackChan|SlackGroup|SlackIms} chan
- * @param {Array.<*>} msg
- **/
- function onMsgReceived(chan, msg) {
- if (chan && (chan !== SELECTED_ROOM || !window.hasFocus)) {
- var selfReg = new RegExp("<@" +SLACK.context.self.id)
- ,highligted = false;
- if (!UNREAD_CHANS[chan.id]) {
- UNREAD_CHANS[chan.id] = { hl: 0, unread: 0 };
- }
- msg.forEach(function(i) {
- // TODO check read
- if (i.type === "message" && i.text) {
- if (chan.id[0] === 'D' || i.text.match(selfReg)) {
- UNREAD_CHANS[chan.id].hl++;
- highligted = true;
- } else {
- UNREAD_CHANS[chan.id].unread++;
- }
- }
- });
- updateTitle();
- document.getElementById(chan.id).classList.add(R.klass.unread);
- if (highligted) {
- document.getElementById(chan.id).classList.add(R.klass.unreadHi);
- }
- }
- }
- /**
- * @param {SlackChan|SlackGroup|SlackIms} room
- **/
- function markRoomAsRead(room) {
- if (UNREAD_CHANS[room.id]) {
- UNREAD_CHANS[room.id] = { hl: 0, unread: 0 };
- updateTitle();
- }
- var roomLi = document.getElementById(room.id);
- roomLi.classList.remove(R.klass.unread);
- roomLi.classList.remove(R.klass.unreadHi);
- }
- SLACK = new SlackWrapper();
|