|
| 1 | +function ChatSession(options) { |
| 2 | + this.options = options; |
| 3 | + this.from = options.from || 0; |
| 4 | + this.onUpdate = options.onUpdate || function(){}; |
| 5 | +} |
| 6 | + |
| 7 | +ChatSession.prototype = { |
| 8 | + start: function() { |
| 9 | + this.socket = io.connect(this.options.url); |
| 10 | + var self = this; |
| 11 | + this.socket.on('update', function() { |
| 12 | + self.update(self.onUpdate); |
| 13 | + }); |
| 14 | + }, |
| 15 | + |
| 16 | + send: function(msg) { |
| 17 | + this.socket.emit('msg', { |
| 18 | + nick: this.options.nickname, |
| 19 | + channel: this.options.channel, |
| 20 | + msg: msg |
| 21 | + }); |
| 22 | + }, |
| 23 | + |
| 24 | + update: function(next) { |
| 25 | + var self = this; |
| 26 | + $.getJSON('?from=' + this.from, function(data) { |
| 27 | + if (!data.html) return; |
| 28 | + self.from = data.last_no + 1; |
| 29 | + if (next) next.call(self, data.html); |
| 30 | + }); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +function LogView(el) { |
| 35 | + this.el = el; |
| 36 | +} |
| 37 | + |
| 38 | +LogView.prototype = { |
| 39 | + append: function(data) { |
| 40 | + this.el.append(data); |
| 41 | + apply_noreferrer(this.el); |
| 42 | + }, |
| 43 | + |
| 44 | + scrollToBottom: function() { |
| 45 | + $(window).scrollTop($(document).height() + 100000); |
| 46 | + }, |
| 47 | + |
| 48 | + shouldScrollToBottom: function() { |
| 49 | + var $doc = $(document), |
| 50 | + $window = $(window); |
| 51 | + return $doc.height() <= $window.scrollTop() + $window.height() + 20; |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +function ChatController(options) { |
| 56 | + this.options = options; |
| 57 | + this.session = options.session; |
| 58 | + this.session.onUpdate = $.proxy(this.onUpdate, this); |
| 59 | + |
| 60 | + var self = this; |
| 61 | + this.options.startChatView.on('click', function() { |
| 62 | + self.startChat(); |
| 63 | + return false; |
| 64 | + }); |
| 65 | + this.options.inputFormView.on('submit', function() { |
| 66 | + self.sendMessage(); |
| 67 | + return false; |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +ChatController.prototype = { |
| 72 | + startChat: function() { |
| 73 | + this.options.startChatView.slideUp(); |
| 74 | + this.options.inputFormView.slideDown(); |
| 75 | + var self = this; |
| 76 | + |
| 77 | + this.normalTitle = document.title; |
| 78 | + observeWindowVisibility(function(visible) { |
| 79 | + self.setWindowVisible(visible); |
| 80 | + }); |
| 81 | + |
| 82 | + this.session.update(function(data) { |
| 83 | + self.appendLog(data); |
| 84 | + self.options.logView.scrollToBottom(); |
| 85 | + }); |
| 86 | + this.session.start(); |
| 87 | + }, |
| 88 | + |
| 89 | + onUpdate: function(data) { |
| 90 | + var willScroll = this.options.logView.shouldScrollToBottom(); |
| 91 | + this.appendLog(data); |
| 92 | + if (willScroll) |
| 93 | + this.options.logView.scrollToBottom(); |
| 94 | + if (!this._windowVisible) |
| 95 | + this.notifyPendingLog(); |
| 96 | + }, |
| 97 | + |
| 98 | + appendLog: function(data) { |
| 99 | + this.options.logView.append(data); |
| 100 | + }, |
| 101 | + |
| 102 | + sendMessage: function() { |
| 103 | + var msg = this.options.inputView.val(); |
| 104 | + if (msg) { |
| 105 | + this.session.send(msg); |
| 106 | + this.options.inputView.val(''); |
| 107 | + } |
| 108 | + }, |
| 109 | + |
| 110 | + setWindowVisible: function(visible) { |
| 111 | + if (!this._windowVisible && visible) { |
| 112 | + // Become visible |
| 113 | + document.title = this.normalTitle; |
| 114 | + } |
| 115 | + this._windowVisible = visible; |
| 116 | + }, |
| 117 | + |
| 118 | + notifyPendingLog: function() { |
| 119 | + document.title = '+ ' + this.normalTitle; |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +(function(exports) { |
| 124 | + var hidden, visibilityChange; |
| 125 | + if (typeof document.hidden !== "undefined") { |
| 126 | + hidden = "hidden"; |
| 127 | + visibilityChange = "visibilitychange"; |
| 128 | + } else if (typeof document.mozHidden !== "undefined") { |
| 129 | + hidden = "mozHidden"; |
| 130 | + visibilityChange = "mozvisibilitychange"; |
| 131 | + } else if (typeof document.msHidden !== "undefined") { |
| 132 | + hidden = "msHidden"; |
| 133 | + visibilityChange = "msvisibilitychange"; |
| 134 | + } else if (typeof document.webkitHidden !== "undefined") { |
| 135 | + hidden = "webkitHidden"; |
| 136 | + visibilityChange = "webkitvisibilitychange"; |
| 137 | + } |
| 138 | + |
| 139 | + exports.isWindowVisible = function() { |
| 140 | + return !document[hidden]; |
| 141 | + }; |
| 142 | + |
| 143 | + exports.observeWindowVisibility = function(callback) { |
| 144 | + if (typeof document.addEventListener !== "undefined" && |
| 145 | + typeof hidden !== "undefined") { |
| 146 | + callback(exports.isWindowVisible()); |
| 147 | + document.addEventListener(visibilityChange, function() { |
| 148 | + callback(exports.isWindowVisible()); |
| 149 | + }, false); |
| 150 | + } |
| 151 | + }; |
| 152 | +})(window); |
0 commit comments