1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-07 16:06:15 +00:00

Move generateUrl to Sms object, add SmsSettings::init & drop it from angular app

This commit is contained in:
Loic Blot 2017-12-28 18:34:15 +01:00
parent 0c54bfbf06
commit cc14f86893
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987
5 changed files with 58 additions and 51 deletions

2
js/app.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -9,12 +9,24 @@
*/ */
var Sms = { var Sms = {
// Attributes
selectedConversation: null, selectedConversation: null,
unreadCountCurrentConv: 0, unreadCountCurrentConv: 0,
unreadCountAllConv: 0, unreadCountAllConv: 0,
unreadCountNotifStep: 12, unreadCountNotifStep: 12,
lastUnreadCountAllConv: 0, lastUnreadCountAllConv: 0,
originalTitle: document.title originalTitle: document.title,
_winRegexp: /(.*)\/ocsms.*/,
// Functions
generateURL: function (endpoint) {
var match = this._winRegexp.exec(window.location.href);
if (match.length !== 2) {
console.log("A very bad error happened when parsing window location");
}
return match[1] + '/ocsms' + endpoint;
}
}; };
var ContactRenderer = { var ContactRenderer = {

View File

@ -50,26 +50,17 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$scope.selectedContact = {}; $scope.selectedContact = {};
$scope.lastSearch = ''; $scope.lastSearch = '';
$scope.generateUrl = function (endpoint) {
var winRegexp = /(.*)\/ocsms.*/;
var match = winRegexp.exec(window.location.href);
if (match.length !== 2) {
console.log("A very bad error happened when parsing window location");
}
return match[1] + '/ocsms' + endpoint;
};
// Settings // Settings
$scope.sendCountry = function () { $scope.sendCountry = function () {
$.post($scope.generateUrl('/set/country'), {'country': $('select[name=intl_phone]').val()}); $.post(Sms.generateURL('/set/country'), {'country': $('select[name=intl_phone]').val()});
}; };
$scope.setMessageLimit = function () { $scope.setMessageLimit = function () {
$.post($scope.generateUrl('/set/msglimit'), {'limit': SmsSettings.messageLimit}); $.post(Sms.generateURL('/set/msglimit'), {'limit': SmsSettings.messageLimit});
}; };
$scope.setNotificationSetting = function () { $scope.setNotificationSetting = function () {
$.post($scope.generateUrl('/set/notification_state'), $.post(Sms.generateURL('/set/notification_state'),
{ {
'notification': SmsSettings.enableNotifications ? 1 : 0 'notification': SmsSettings.enableNotifications ? 1 : 0
} }
@ -77,7 +68,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
}; };
$scope.setContactOrderSetting = function () { $scope.setContactOrderSetting = function () {
$.post($scope.generateUrl('/set/contact_order'), $.post(Sms.generateURL('/set/contact_order'),
{ {
'attribute': SmsSettings.contactOrderBy, 'attribute': SmsSettings.contactOrderBy,
'reverse': SmsSettings.reverseContactOrder 'reverse': SmsSettings.reverseContactOrder
@ -106,7 +97,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$scope.messages = []; $scope.messages = [];
$scope.lastConvMessageDate = 0; $scope.lastConvMessageDate = 0;
$.getJSON($scope.generateUrl('/front-api/v1/conversation'), {'phoneNumber': $scope.selectedContact.nav}, $.getJSON(Sms.generateURL('/front-api/v1/conversation'), {'phoneNumber': $scope.selectedContact.nav},
function (jsondata, status) { function (jsondata, status) {
var phoneNumberLabel = $scope.selectedContact.nav; var phoneNumberLabel = $scope.selectedContact.nav;
@ -137,7 +128,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
); );
}; };
$scope.refreshConversation = function () { $scope.refreshConversation = function () {
$.getJSON($scope.generateUrl('/ocsms/front-api/v1/conversation'), $.getJSON(Sms.generateURL('/ocsms/front-api/v1/conversation'),
{ {
'phoneNumber': $scope.selectedContact.nav, 'phoneNumber': $scope.selectedContact.nav,
"lastDate": $scope.lastConvMessageDate "lastDate": $scope.lastConvMessageDate
@ -151,7 +142,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
if (document.hasFocus() === false) { if (document.hasFocus() === false) {
Sms.unreadCountCurrentConv += parseInt(fmt[0]); Sms.unreadCountCurrentConv += parseInt(fmt[0]);
document.title = Sms.originalTitle + " (" + Sms.unreadCountCurrentConv + ")"; document.title = Sms.originalTitle + " (" + Sms.unreadCountCurrentConv + ")";
$scope.desktopNotify(Sms.unreadCountCurrentConv + " unread message(s) in conversation with " + $scope.selectedContact.label); SmsNotifications.notify(Sms.unreadCountCurrentConv + " unread message(s) in conversation with " + $scope.selectedContact.label);
} }
} }
@ -162,7 +153,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
}; };
$scope.checkNewMessages = function () { $scope.checkNewMessages = function () {
Sms.unreadCountAllConv = 0; Sms.unreadCountAllConv = 0;
$.getJSON($scope.generateUrl('/front-api/v1/new_messages'), $.getJSON(Sms.generateURL('/front-api/v1/new_messages'),
{'lastDate': $scope.lastContactListMsgDate}, {'lastDate': $scope.lastContactListMsgDate},
function (jsondata, status) { function (jsondata, status) {
var bufferedContacts = []; var bufferedContacts = [];
@ -222,7 +213,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
* or if unreadCount changes * or if unreadCount changes
*/ */
if (Sms.unreadCountNotifStep === 0 || Sms.lastUnreadCountAllConv !== Sms.unreadCountAllConv) { if (Sms.unreadCountNotifStep === 0 || Sms.lastUnreadCountAllConv !== Sms.unreadCountAllConv) {
$scope.desktopNotify(Sms.unreadCountAllConv + " unread message(s) for all conversations"); SmsNotifications.notify(Sms.unreadCountAllConv + " unread message(s) for all conversations");
Sms.unreadCountNotifStep = 12; Sms.unreadCountNotifStep = 12;
Sms.lastUnreadCountAllConv = Sms.unreadCountAllConv; Sms.lastUnreadCountAllConv = Sms.unreadCountAllConv;
} }
@ -232,7 +223,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
}; };
$scope.removeConversation = function () { $scope.removeConversation = function () {
$.post($scope.generateUrl('/delete/conversation'), {"contact": $scope.selectedContact.label}, function (data) { $.post(Sms.generateURL('/delete/conversation'), {"contact": $scope.selectedContact.label}, function (data) {
// Reinit main window // Reinit main window
$scope.selectedContact.label = ""; $scope.selectedContact.label = "";
$scope.selectedContact.opt_numbers = ""; $scope.selectedContact.opt_numbers = "";
@ -307,7 +298,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
var curMsg = $scope.messages[i]; var curMsg = $scope.messages[i];
if (curMsg['id'] === msgId) { if (curMsg['id'] === msgId) {
$.post($scope.generateUrl('/delete/message'), $.post(Sms.generateURL('/delete/message'),
{"messageId": msgId, "phoneNumber": $scope.selectedContact.label}, function (data) { {"messageId": msgId, "phoneNumber": $scope.selectedContact.label}, function (data) {
$scope.$apply(function () { $scope.$apply(function () {
$scope.messages.splice(i, 1); $scope.messages.splice(i, 1);
@ -318,24 +309,6 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
} }
}; };
$scope.fetchInitialSettings = function () {
$.getJSON($scope.generateUrl('/front-api/v1/settings'), function (jsondata, status) {
if (jsondata['status'] === true) {
$('#sel_intl_phone').val(jsondata["country"]);
$('input[name=setting_msg_per_page]').val(parseInt(jsondata["message_limit"]));
$('select[name=setting_notif]').val(jsondata["notification_state"]);
$('select[name=setting_contact_order]').val(jsondata["contact_order"]);
$('input[name=setting_contact_order_reverse]').val(toBool(jsondata["contact_order_reverse"]));
SmsSettings.messageLimit = parseInt(jsondata["message_limit"]);
SmsSettings.enableNotifications = parseInt(jsondata["notification_state"]) !== 0;
SmsSettings.contactOrderBy = jsondata["contact_order"];
SmsSettings.reverseContactOrder = toBool(jsondata["contact_order_reverse"]);
}
});
};
$scope.fetchInitialPeerList = function (jsondata) { $scope.fetchInitialPeerList = function (jsondata) {
// Use a buffer for better jQuery performance // Use a buffer for better jQuery performance
var bufferedContacts = []; var bufferedContacts = [];
@ -419,14 +392,6 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
return [msgCount, buf]; return [msgCount, buf];
}; };
$scope.desktopNotify = function (msg) {
if (!SmsSettings.enableNotifications) {
return;
}
SmsNotifications.notify(msg);
};
$interval($scope.refreshConversation, 10000); $interval($scope.refreshConversation, 10000);
$interval($scope.checkNewMessages, 10000); $interval($scope.checkNewMessages, 10000);
@ -435,7 +400,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
Sms.originalTitle = document.title; Sms.originalTitle = document.title;
// Now bind the events when we click on the phone number // Now bind the events when we click on the phone number
$.getJSON($scope.generateUrl('/front-api/v1/peerlist'), function (jsondata, status) { $.getJSON(Sms.generateURL('/front-api/v1/peerlist'), function (jsondata, status) {
$scope.fetchInitialPeerList(jsondata); $scope.fetchInitialPeerList(jsondata);
var pnParam = $.urlParam('phonenumber'); var pnParam = $.urlParam('phonenumber');
@ -453,7 +418,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
} }
} }
}); });
$scope.fetchInitialSettings(); SmsSettings.init();
SmsNotifications.init(); SmsNotifications.init();
$scope.checkNewMessages(); $scope.checkNewMessages();
}); });

View File

@ -21,6 +21,10 @@ var SmsNotifications = {
}); });
}, },
notify: function (message) { notify: function (message) {
if (!SmsSettings.enableNotifications) {
return;
}
if (!("Notification" in window)) { if (!("Notification" in window)) {
return; return;
} }

View File

@ -9,8 +9,34 @@
*/ */
var SmsSettings = { var SmsSettings = {
// Attributes
messageLimit: 100, messageLimit: 100,
enableNotifications: true, enableNotifications: true,
contactOrderBy: 'lastmsg', contactOrderBy: 'lastmsg',
reverseContactOrder: true reverseContactOrder: true,
country: '',
// Functions
init: function () {
$.getJSON(Sms.generateURL('/front-api/v1/settings'), function (jsondata, status) {
if (jsondata['status'] === true) {
this.messageLimit = parseInt(jsondata["message_limit"]);
this.enableNotifications = parseInt(jsondata["notification_state"]) !== 0;
this.contactOrderBy = jsondata["contact_order"];
this.reverseContactOrder = toBool(jsondata["contact_order_reverse"]);
this.country = jsondata["country"];
this.updateView();
}
});
},
// This function should be moved to a renderer or something else
updateView: function () {
$('#sel_intl_phone').val(this.country);
$('input[name=setting_msg_per_page]').val(this.messageLimit);
$('select[name=setting_notif]').val(this.enableNotifications ? 1 : 0);
$('select[name=setting_contact_order]').val(this.contactOrderBy);
$('input[name=setting_contact_order_reverse]').val(this.reverseContactOrder);
}
}; };