Skip to content

Commit 99acad2

Browse files
committed
update index page
1 parent 6e33829 commit 99acad2

File tree

5 files changed

+83
-35
lines changed

5 files changed

+83
-35
lines changed

src/main/java/ru/kapahgaiii/qa/controller/AjaxController.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,16 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.ui.Model;
6-
import org.springframework.web.bind.annotation.PathVariable;
7-
import org.springframework.web.bind.annotation.RequestMapping;
8-
import org.springframework.web.bind.annotation.RequestParam;
9-
import org.springframework.web.bind.annotation.ResponseBody;
6+
import org.springframework.web.bind.annotation.*;
107
import ru.kapahgaiii.qa.core.objects.Subscriber;
118
import ru.kapahgaiii.qa.domain.Question;
12-
import ru.kapahgaiii.qa.dto.ChatInitial;
13-
import ru.kapahgaiii.qa.dto.MessageDTO;
14-
import ru.kapahgaiii.qa.dto.Online;
15-
import ru.kapahgaiii.qa.dto.QuestionDTO;
9+
import ru.kapahgaiii.qa.dto.*;
1610
import ru.kapahgaiii.qa.service.ChatService;
1711
import ru.kapahgaiii.qa.service.UserService;
1812

1913
import javax.servlet.http.HttpServletRequest;
2014
import java.security.Principal;
2115
import java.sql.Timestamp;
22-
import java.util.Date;
2316
import java.util.List;
2417
import java.util.Set;
2518

@@ -112,11 +105,10 @@ Online getOnline() {
112105
@RequestMapping("/loadQuestions/{time}")
113106
public
114107
@ResponseBody
115-
QuestionDTO[] getQuestions(@PathVariable(value = "time") Long time) {
116-
time = time < 0 ? (new Date()).getTime() : time;
117-
List<QuestionDTO> questions = chatService.getQuestionDTOsList(new Timestamp(time));
108+
QuestionDTO[] getQuestions(@PathVariable(value = "time") Long time,
109+
@RequestParam(value = "exclude[]", required = false) Integer[] exclude) {
110+
List<QuestionDTO> questions = chatService.getQuestionDTOsList(new Timestamp(time), exclude);
118111
return questions.toArray(new QuestionDTO[questions.size()]);
119-
// return new Question[0];
120112
}
121113

122114
private boolean isAjax(HttpServletRequest request) {

src/main/java/ru/kapahgaiii/qa/repository/implementations/ChatDAOImpl.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,20 @@ public void updateMessage(Message message) {
7777

7878
@Override
7979
@SuppressWarnings("unchecked")
80-
public List<Question> getQuestionsList(Timestamp time) {
81-
return sessionFactory.getCurrentSession().createQuery("from Question where updatedTime <= :time")
80+
public List<Question> getQuestionsList(Timestamp time, Integer[] exclude) {
81+
int limit = 15;
82+
if (exclude == null){
83+
return sessionFactory.getCurrentSession()
84+
.createQuery("from Question where updatedTime <= :time order by updatedTime desc")
85+
.setParameter("time", time)
86+
.setMaxResults(limit)
87+
.list();
88+
}
89+
return sessionFactory.getCurrentSession()
90+
.createQuery("from Question where updatedTime <= :time and id not in (:ids) order by updatedTime desc")
8291
.setParameter("time", time)
83-
.setMaxResults(20)
92+
.setParameterList("ids", exclude)
93+
.setMaxResults(limit)
8494
.list();
8595
}
8696

src/main/java/ru/kapahgaiii/qa/repository/interfaces/QuestionDAO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface QuestionDAO {
99

1010
public Question getQuestionById(Integer id);
1111

12-
public List<Question> getQuestionsList(Timestamp time);
12+
public List<Question> getQuestionsList(Timestamp time, Integer[] exclude);
1313

1414
public void saveQuestion(Question question);
1515

src/main/java/ru/kapahgaiii/qa/service/ChatService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public Set<Subscriber> getChatSubscribers(Question question) {
9292
return sessionController.getChatSubscribers(question);
9393
}
9494

95-
public List<QuestionDTO> getQuestionDTOsList(Timestamp time) {
96-
List<Question> questions = chatDAO.getQuestionsList(time);
95+
public List<QuestionDTO> getQuestionDTOsList(Timestamp time, Integer[] exclude) {
96+
List<Question> questions = chatDAO.getQuestionsList(time, exclude);
9797
List<QuestionDTO> questionDTOs = new ArrayList<QuestionDTO>();
9898
for (Question question : questions) {
9999
QuestionDTO questionDTO = new QuestionDTO(question);

src/main/webapp/js/engine.js

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ function loadPage(url, data) {
6161
updateLinks();
6262
//websocket maintenance
6363

64-
//alert(url)
6564
if (url.match("^/?question")) {
6665
chat.setUp(url)
6766
} else if (chat.chatId) {
@@ -81,13 +80,16 @@ function setTitle() {
8180
//add click event listener to <a> tags
8281
function updateLinks() {
8382
$.each($("a"), function () {
84-
this.addEventListener("click", function (e) {
83+
this.addEventListener("click", function (event) {
84+
if (event.which == 2) {
85+
return true;
86+
}
8587
var url = $(this).attr("href"); //get url
8688
if (url != "/login") {
8789
history.pushState(null, null, url); //update address bar
8890
}
8991
getPage(url); //load page
90-
e.preventDefault(); //prevent following the link
92+
event.preventDefault(); //prevent following the link
9193
}, true);
9294
});
9395
}
@@ -118,17 +120,18 @@ function connectWebSocket() {
118120
}
119121

120122
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
121127
subscription: undefined,
122128
isSet: false,
123129
setUp: function () {
124130
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();
132135
}
133136
});
134137
questions.isSet = true;
@@ -156,14 +159,41 @@ var questions = {
156159
questions.subscription.unsubscribe();
157160
}
158161
},
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) {
160177
$.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);
162185
});
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();
163193
},
164194
showQuestion: function (q) {
165-
if (!q.shown) {
166-
q.shown = true;
195+
if (!questions.shown[q.id]) {
196+
questions.shown[q.id] = true;
167197
var date = new Date(q.updatedTime);
168198
var html = [];
169199
var i = 0;
@@ -207,6 +237,14 @@ var questions = {
207237
questions.unsubscribe();
208238
questions.subscription = undefined;
209239
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+
});
210248
}
211249
};
212250
var chat = {
@@ -560,7 +598,6 @@ var chat = {
560598
chat.showAnonymous();
561599
},
562600
showSubscribed: function (username) {
563-
//alert("show " + username + ": " + chat.subscribedUsers[username])
564601
if (chat.subscribedUsers[username] == 1) {
565602
$("#users_in_chat .anon_users").before("<li username='" + username + "'>" + username + "</li>")
566603
}
@@ -683,6 +720,15 @@ var utils = {
683720
default:
684721
return " сообщений";
685722
}
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;
686732
}
687733
};
688734

@@ -721,4 +767,4 @@ var message = {
721767
message.hideTimeout = setTimeout(message.hide, 50)
722768
}
723769
}
724-
};
770+
};

0 commit comments

Comments
 (0)