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