/** * ownCloud - ocsms * * This file is licensed under the Affero General Public License version 3 or * later. See the COPYING file. * * @author Loic Blot * @copyright Loic Blot 2014 */ // Some global vars to improve performances var selectedConversation = null; var curPhoneNumber = null; var lastMsgDate = 0; $.urlParam = function(name){ var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href); if (results == null) { return null; } else { return results[1] || 0; } }; var refreshConversation = function() { $.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'), { 'phoneNumber': curPhoneNumber, "lastDate": lastMsgDate }, function(jsondata, status) { conversationBuf = formatConversation(jsondata); if (conversationBuf != '') { $('.msg-endtag').before(conversationBuf); $('#app-content-wrapper').scrollTop(1E10); } } ); }; function fetchConversation(phoneNumber) { $.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'), { 'phoneNumber': phoneNumber }, function(jsondata, status) { conversationBuf = formatConversation(jsondata); conversationBuf += '
'; if (typeof jsondata['contactName'] == 'undefined') { $('#ocsms-phone-label').html(phoneNumber); $('#ocsms-phone-opt-number').html(''); } else { $('#ocsms-phone-label').html(jsondata['contactName']); $('#ocsms-phone-opt-number').html(phoneNumber); } $('#app-content-wrapper').html(conversationBuf); $('#app-content-wrapper').scrollTop(1E10); curPhoneNumber = phoneNumber; } ); } function formatConversation(jsondata) { // Improve jQuery performance var buf = ""; // Improve JS performance var msgClass = ''; 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) { msgClass = "msg-recv"; } else if (vals["type"] == 2) { msgClass = "msg-sent"; } else { msgClass = ''; } // Store the greater msg date for refresher if (id > lastMsgDate) { lastMsgDate = id; } // Multiplicate ID to permit date to use it properly msgDate = new Date(id*1); formatedHour = msgDate.getHours(); if (formatedHour < 10) { formatedHour = '0' + formatedHour; } formatedMin = msgDate.getMinutes(); if (formatedMin < 10) { formatedMin = '0' + formatedMin; } formatedDate = msgDate.getDate() + " " + months[msgDate.getMonth()] + " " + formatedHour + ":" + formatedMin; buf += '
' + vals["msg"] + '
' + formatedDate + '
'; }); return buf; } function changeSelectedConversation(item) { if (selectedConversation != null) { selectedConversation.parent().removeClass('selected'); } selectedConversation = item; selectedConversation.parent().addClass('selected'); } (function ($, OC) { $(document).ready(function () { // Now bind the events when we click on the phone number $.getJSON(OC.generateUrl('/apps/ocsms/get/peerlist'), function(jsondata, status) { // Use a buffer for better jQuery performance var peerListBuf = ""; $.each(jsondata['phonelist'], function(id, val) { var fn, peerLabel; if (typeof jsondata['contacts'][val] == 'undefined') { fn = ''; peerLabel = val; } else { fn = jsondata['contacts'][val]; peerLabel = fn; } peerListBuf += '
  • ' + peerLabel + '
  • '; }); $('#app-mailbox-peers ul').html(peerListBuf); // Now bind the events when we click on the phone number $('#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(); }); var urlPhoneNumber = decodeURIComponent($.urlParam('phonenumber')); if (urlPhoneNumber != null) { fetchConversation(urlPhoneNumber); var pObject = $("a[mailbox-navigation='" + urlPhoneNumber + "']"); if (pObject != null) { changeSelectedConversation(pObject); } } }); setInterval(refreshConversation, 10000); }); })(jQuery, OC);