@@ -61,7 +61,6 @@ function loadPage(url, data) {
61
61
updateLinks ( ) ;
62
62
//websocket maintenance
63
63
64
- //alert(url)
65
64
if ( url . match ( "^/?question" ) ) {
66
65
chat . setUp ( url )
67
66
} else if ( chat . chatId ) {
@@ -81,13 +80,16 @@ function setTitle() {
81
80
//add click event listener to <a> tags
82
81
function updateLinks ( ) {
83
82
$ . each ( $ ( "a" ) , function ( ) {
84
- this . addEventListener ( "click" , function ( e ) {
83
+ this . addEventListener ( "click" , function ( event ) {
84
+ if ( event . which == 2 ) {
85
+ return true ;
86
+ }
85
87
var url = $ ( this ) . attr ( "href" ) ; //get url
86
88
if ( url != "/login" ) {
87
89
history . pushState ( null , null , url ) ; //update address bar
88
90
}
89
91
getPage ( url ) ; //load page
90
- e . preventDefault ( ) ; //prevent following the link
92
+ event . preventDefault ( ) ; //prevent following the link
91
93
} , true ) ;
92
94
} ) ;
93
95
}
@@ -118,17 +120,18 @@ function connectWebSocket() {
118
120
}
119
121
120
122
var questions = {
123
+ loaded : [ ] ,
124
+ shown : [ ] ,
125
+ earliestTime : new Date ( ) . getTime ( ) + 86400000 , // 100% max, timezones taken into account
126
+ earliestIds : [ ] , // we need to keep them to avoid reloading already loaded questions
121
127
subscription : undefined ,
122
128
isSet : false ,
123
129
setUp : function ( ) {
124
130
questions . subscribe ( ) ;
125
- //load questions
126
- $ . ajax ( {
127
- type : "GET" ,
128
- url : "/loadQuestions/-1" ,
129
- success : function ( response ) {
130
- questions . showQuestions ( response ) ;
131
- updateLinks ( ) ;
131
+ questions . loadQuestions ( questions . earliestTime ) ;
132
+ $ ( window ) . scroll ( function ( ) {
133
+ if ( $ ( document ) . height ( ) - ( $ ( window ) . scrollTop ( ) + $ ( window ) . height ( ) ) < 100 ) {
134
+ questions . loadMore ( ) ;
132
135
}
133
136
} ) ;
134
137
questions . isSet = true ;
@@ -156,14 +159,41 @@ var questions = {
156
159
questions . subscription . unsubscribe ( ) ;
157
160
}
158
161
} ,
159
- showQuestions : function ( list ) {
162
+ loadQuestions : function ( time ) {
163
+ $ . ajax ( {
164
+ type : "POST" ,
165
+ url : "/loadQuestions/" + time ,
166
+ data : { exclude : questions . earliestIds } ,
167
+ success : function ( response ) {
168
+ questions . addQuestions ( response ) ;
169
+ questions . showQuestions ( ) ;
170
+ }
171
+ } ) ;
172
+ } ,
173
+ loadMore : function ( ) {
174
+ questions . loadQuestions ( questions . earliestTime ) ;
175
+ } ,
176
+ addQuestions : function ( list ) {
160
177
$ . each ( list , function ( ) {
161
- questions . showQuestion ( this )
178
+ var q = this ;
179
+ questions . loaded . push ( q ) ;
180
+ if ( q . updatedTime != questions . earliestTime ) {
181
+ questions . earliestIds = [ ] ;
182
+ }
183
+ questions . earliestIds . push ( q . id ) ;
184
+ questions . earliestTime = Math . min ( questions . earliestTime , q . updatedTime ) ;
162
185
} ) ;
186
+ questions . sort ( ) ;
187
+ } ,
188
+ showQuestions : function ( ) {
189
+ for ( var i = 0 ; i < questions . loaded . length ; i ++ ) {
190
+ questions . showQuestion ( questions . loaded [ i ] ) ;
191
+ }
192
+ updateLinks ( ) ;
163
193
} ,
164
194
showQuestion : function ( q ) {
165
- if ( ! q . shown ) {
166
- q . shown = true ;
195
+ if ( ! questions . shown [ q . id ] ) {
196
+ questions . shown [ q . id ] = true ;
167
197
var date = new Date ( q . updatedTime ) ;
168
198
var html = [ ] ;
169
199
var i = 0 ;
@@ -207,6 +237,14 @@ var questions = {
207
237
questions . unsubscribe ( ) ;
208
238
questions . subscription = undefined ;
209
239
questions . isSet = false ;
240
+ } ,
241
+ sort : function ( ) {
242
+ questions . loaded . sort ( function ( a , b ) {
243
+ if ( isNaN ( a . updatedTime ) || isNaN ( b . updatedTime ) ) {
244
+ return a . updatedTime < b . updatedTime ? 1 : - 1 ;
245
+ }
246
+ return b . updatedTime - a . updatedTime ;
247
+ } ) ;
210
248
}
211
249
} ;
212
250
var chat = {
@@ -560,7 +598,6 @@ var chat = {
560
598
chat . showAnonymous ( ) ;
561
599
} ,
562
600
showSubscribed : function ( username ) {
563
- //alert("show " + username + ": " + chat.subscribedUsers[username])
564
601
if ( chat . subscribedUsers [ username ] == 1 ) {
565
602
$ ( "#users_in_chat .anon_users" ) . before ( "<li username='" + username + "'>" + username + "</li>" )
566
603
}
@@ -683,6 +720,15 @@ var utils = {
683
720
default :
684
721
return " сообщений" ;
685
722
}
723
+ } ,
724
+ sort : function ( arr ) {
725
+ arr . sort ( function ( a , b ) {
726
+ if ( isNaN ( a ) || isNaN ( b ) ) {
727
+ return a > b ? 1 : - 1 ;
728
+ }
729
+ return a - b ;
730
+ } ) ;
731
+ return arr ;
686
732
}
687
733
} ;
688
734
@@ -721,4 +767,4 @@ var message = {
721
767
message . hideTimeout = setTimeout ( message . hide , 50 )
722
768
}
723
769
}
724
- } ;
770
+ } ;
0 commit comments