mirror of
https://github.com/nerzhul/ocsms.git
synced 2025-06-08 16:36:25 +00:00
implement refresher. Also factorize code. Need tests
This commit is contained in:
parent
a188178329
commit
59a5353586
@ -77,7 +77,7 @@ class SmsController extends Controller {
|
||||
* @NoAdminRequired
|
||||
* @NoCSRFRequired
|
||||
*/
|
||||
public function getConversation ($phoneNumber) {
|
||||
public function getConversation ($phoneNumber, $lastDate = 0) {
|
||||
$messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber);
|
||||
// @ TODO: filter correctly
|
||||
return new JSONResponse(array("conversation" => $messages));
|
||||
|
53
js/script.js
53
js/script.js
@ -12,6 +12,10 @@
|
||||
// Some global vars to improve performances
|
||||
var selectedConversation = null;
|
||||
var curPhoneNumber = null;
|
||||
var lastMsgDate = 0;
|
||||
|
||||
var months = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.',
|
||||
'Oct.', 'Nov.', 'Dec.'];
|
||||
|
||||
// Source: http://www.sitepoint.com/url-parameters-jquery/
|
||||
$.urlParam = function(name){
|
||||
@ -25,23 +29,44 @@ $.urlParam = function(name){
|
||||
};
|
||||
|
||||
var refreshConversation = function() {
|
||||
|
||||
$.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'),
|
||||
{
|
||||
'phoneNumber': phoneNumber,
|
||||
"lastDate": lastMsgDate
|
||||
},
|
||||
function(jsondata, status) {
|
||||
conversationBuf = formatConversation(jsondata);
|
||||
$('.msg-endtag').addBefore(conversationBuf);
|
||||
$('#app-content').scrollTop(1E10);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
function fetchConversation(phoneNumber) {
|
||||
$.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'),
|
||||
{'phoneNumber': phoneNumber},
|
||||
{
|
||||
'phoneNumber': phoneNumber
|
||||
},
|
||||
function(jsondata, status) {
|
||||
conversationBuf = formatConversation(jsondata);
|
||||
conversationBuf += '<div class="msg-endtag"></div>';
|
||||
|
||||
$('#app-content').html(conversationBuf);
|
||||
$('#app-content').scrollTop(1E10);
|
||||
|
||||
curPhoneNumber = phoneNumber;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function formatConversation(jsondata) {
|
||||
// Improve jQuery performance
|
||||
var conversationBuf = "";
|
||||
var buf = "";
|
||||
// Improve JS performance
|
||||
var msgClass = '';
|
||||
var unixDate = '';
|
||||
var formatedDate = '';
|
||||
var formatedHour = '00';
|
||||
var formatedMin = '00';
|
||||
var months = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.',
|
||||
'Oct.', 'Nov.', 'Dec.'];
|
||||
|
||||
$.each(jsondata["conversation"], function(id, vals) {
|
||||
if (vals["type"] == 1) {
|
||||
@ -54,6 +79,11 @@ function fetchConversation(phoneNumber) {
|
||||
msgClass = '';
|
||||
}
|
||||
|
||||
// Store the greater msg date for refresher
|
||||
if (id > lastMsgDate) {
|
||||
id = lastMsgDate;
|
||||
}
|
||||
|
||||
// Multiplicate ID to permit date to use it properly
|
||||
msgDate = new Date(id*1);
|
||||
|
||||
@ -69,16 +99,13 @@ function fetchConversation(phoneNumber) {
|
||||
formatedDate = msgDate.getDate() + " " + months[msgDate.getMonth()] + " " +
|
||||
formatedHour + ":" + formatedMin;
|
||||
|
||||
conversationBuf += '<div><div class="' + msgClass + '"><div>' +
|
||||
buf += '<div><div class="' + msgClass + '"><div>' +
|
||||
vals["msg"] + '</div><div class="msg-date">' +
|
||||
formatedDate + '</div></div><div class="msg-spacer"></div></div>';
|
||||
curPhoneNumber = phoneNumber;
|
||||
|
||||
});
|
||||
|
||||
$('#app-content').html(conversationBuf);
|
||||
$('#app-content').scrollTop(1E10);
|
||||
}
|
||||
);
|
||||
return buf;
|
||||
}
|
||||
|
||||
function changeSelectedConversation(item) {
|
||||
@ -106,6 +133,8 @@ function changeSelectedConversation(item) {
|
||||
$('#app-mailbox-peers').find('a[mailbox-navigation]').on('click', function (event) {
|
||||
var phoneNumber = $(this).attr('mailbox-navigation');
|
||||
OC.Util.History.pushState('phonenumber=' + phoneNumber);
|
||||
// Reset it for refreshConversation
|
||||
lastMsgDate = 0;
|
||||
fetchConversation(phoneNumber);
|
||||
changeSelectedConversation($(this));
|
||||
event.preventDefault();
|
||||
|
Loading…
x
Reference in New Issue
Block a user