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

rename app.js to legacy.js, move globals to app.js in Sms object and helpers to a dedicated file

This commit is contained in:
Loic Blot 2017-12-28 17:39:14 +01:00
parent 4f01b5c467
commit 20b77148c7
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987
5 changed files with 148 additions and 113 deletions

2
js/app.min.js vendored

File diff suppressed because one or more lines are too long

7
js/devel/README.md Normal file
View File

@ -0,0 +1,7 @@
# Generate bundle
Use uglifyjs to generate app.min.js bundle
```
uglifyjs devel/*.js > app.min.js
```

45
js/devel/app.js Normal file
View File

@ -0,0 +1,45 @@
/**
* Nextcloud - Phone Sync
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Loic Blot <loic.blot@unix-experience.fr>
* @copyright Loic Blot 2014-2017
*/
var Sms = {
selectedConversation: null,
unreadCountCurrentConv: 0,
unreadCountAllConv: 0,
unreadCountNotifStep: 12,
lastUnreadCountAllConv: 0,
originalTitle: document.title
};
var ContactRenderer = {
generateColor: function (input) {
if (typeof input === 'undefined') {
return '';
}
// Check if core has the new color generator
if (typeof input.toHsl === 'function') {
var hsl = input.toHsl();
return 'hsl(' + hsl[0] + ', ' + hsl[1] + '%, ' + hsl[2] + '%)';
} else {
// If not, we use the old one
/* global md5 */
var hash = md5(input).substring(0, 4),
maxRange = parseInt('ffff', 16),
hue = parseInt(hash, 16) / maxRange * 256;
return 'hsl(' + hue + ', 90%, 65%)';
}
},
generateFirstCharacter: function (input) {
if (input.charAt(0) === '+') {
return '#';
}
return input.charAt(0);
}
};

29
js/devel/helpers.js Normal file
View File

@ -0,0 +1,29 @@
/**
* Nextcloud - Phone Sync
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Loic Blot <loic.blot@unix-experience.fr>
* @copyright Loic Blot 2014-2017
*/
function inArray(val, arr) {
return ($.inArray(val, arr) !== -1);
}
function arrayUnique(arr) {
return arr.filter(function (item, i, arr) {
return i === arr.indexOf(item);
});
}
function toBool(str) {
if (str === "true") {
return true;
}
else if (str === "false") {
return false;
}
return null;
}

View File

@ -8,41 +8,12 @@
* @copyright Loic Blot 2014-2017 * @copyright Loic Blot 2014-2017
*/ */
// Some global vars to improve performances
var g_selectedConversation = null;
var g_unreadCountCurrentConv = 0;
var g_unreadCountAllConv = 0;
var g_unreadCountNotifStep = 12;
var g_lastUnreadCountAllConv = 0;
var g_originalTitle = document.title;
var app = angular.module('OcSms', []); var app = angular.module('OcSms', []);
function inArray(val, arr) { app.directive('toInt', function () {
return ($.inArray(val, arr) !== -1);
}
function arrayUnique(arr) {
return arr.filter(function (item, i, arr) {
return i === arr.indexOf(item);
});
}
function toBool(str) {
if (str === "true") {
return true;
}
else if (str === "false") {
return false;
}
return null;
}
app.directive('toInt', function() {
return { return {
require: 'ngModel', require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) { link: function (scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) { modelCtrl.$parsers.push(function (inputValue) {
return parseInt(inputValue, 10); return parseInt(inputValue, 10);
}); });
@ -51,35 +22,13 @@ app.directive('toInt', function() {
}); });
// Imported from contact app // Imported from contact app
app.filter('peerColor', function() { app.filter('peerColor', function () {
return function(input) { return ContactRenderer.generateColor;
if (typeof input === 'undefined') {
return '';
}
// Check if core has the new color generator
if (typeof input.toHsl === 'function') {
var hsl = input.toHsl();
return 'hsl('+hsl[0]+', '+hsl[1]+'%, '+hsl[2]+'%)';
} else {
// If not, we use the old one
/* global md5 */
var hash = md5(input).substring(0, 4),
maxRange = parseInt('ffff', 16),
hue = parseInt(hash, 16) / maxRange * 256;
return 'hsl(' + hue + ', 90%, 65%)';
}
};
}); });
app.filter('firstCharacter', function() { app.filter('firstCharacter', function () {
return function(input) { return ContactRenderer.generateFirstCharacter;
if (input.charAt(0) === '+') { });
return '#';
}
return input.charAt(0);
};
});
app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile', app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile',
@ -115,14 +64,14 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
// Settings // Settings
$scope.sendCountry = function () { $scope.sendCountry = function () {
$.post($scope.generateUrl('/set/country'),{'country': $('select[name=intl_phone]').val()}); $.post($scope.generateUrl('/set/country'), {'country': $('select[name=intl_phone]').val()});
}; };
$scope.setMessageLimit = function () { $scope.setMessageLimit = function () {
if ($scope.setting_msgLimit === null || $scope.setting_msgLimit === undefined) { if ($scope.setting_msgLimit === null || $scope.setting_msgLimit === undefined) {
return; return;
} }
$.post($scope.generateUrl('/set/msglimit'),{'limit': $scope.setting_msgLimit}); $.post($scope.generateUrl('/set/msglimit'), {'limit': $scope.setting_msgLimit});
}; };
$scope.setNotificationSetting = function () { $scope.setNotificationSetting = function () {
@ -130,7 +79,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$scope.setting_enableNotifications = 0; $scope.setting_enableNotifications = 0;
return; return;
} }
$.post($scope.generateUrl('/set/notification_state'),{'notification': $scope.setting_enableNotifications}); $.post($scope.generateUrl('/set/notification_state'), {'notification': $scope.setting_enableNotifications});
}; };
$scope.setContactOrderSetting = function () { $scope.setContactOrderSetting = function () {
@ -164,7 +113,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$scope.lastConvMessageDate = 0; $scope.lastConvMessageDate = 0;
$.getJSON($scope.generateUrl('/front-api/v1/conversation'), {'phoneNumber': $scope.selectedContact.nav}, $.getJSON($scope.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;
if (typeof jsondata['phoneNumbers'] !== 'undefined') { if (typeof jsondata['phoneNumbers'] !== 'undefined') {
@ -175,7 +124,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
// Reinit messages before showing conversation // Reinit messages before showing conversation
$scope.formatConversation(jsondata); $scope.formatConversation(jsondata);
$scope.$apply(function() { $scope.$apply(function () {
if (typeof jsondata['contactName'] === 'undefined' || jsondata['contactName'] === '') { if (typeof jsondata['contactName'] === 'undefined' || jsondata['contactName'] === '') {
$scope.selectedContact.label = phoneNumberLabel; $scope.selectedContact.label = phoneNumberLabel;
$scope.selectedContact.opt_numbers = ""; $scope.selectedContact.opt_numbers = "";
@ -193,22 +142,22 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
} }
); );
}; };
$scope.refreshConversation = function() { $scope.refreshConversation = function () {
$.getJSON($scope.generateUrl('/ocsms/front-api/v1/conversation'), $.getJSON($scope.generateUrl('/ocsms/front-api/v1/conversation'),
{ {
'phoneNumber': $scope.selectedContact.nav, 'phoneNumber': $scope.selectedContact.nav,
"lastDate": $scope.lastConvMessageDate "lastDate": $scope.lastConvMessageDate
}, },
function(jsondata, status) { function (jsondata, status) {
var fmt = $scope.formatConversation(jsondata); var fmt = $scope.formatConversation(jsondata);
var conversationBuf = fmt[1]; var conversationBuf = fmt[1];
if (conversationBuf === true) { if (conversationBuf === true) {
$('#app-content').scrollTop(1E10); $('#app-content').scrollTop(1E10);
// This will blink the tab because there is new messages // This will blink the tab because there is new messages
if (document.hasFocus() === false) { if (document.hasFocus() === false) {
g_unreadCountCurrentConv += parseInt(fmt[0]); Sms.unreadCountCurrentConv += parseInt(fmt[0]);
document.title = g_originalTitle + " (" + g_unreadCountCurrentConv + ")"; document.title = Sms.originalTitle + " (" + Sms.unreadCountCurrentConv + ")";
$scope.desktopNotify(g_unreadCountCurrentConv + " unread message(s) in conversation with " + $scope.selectedContact.label); $scope.desktopNotify(Sms.unreadCountCurrentConv + " unread message(s) in conversation with " + $scope.selectedContact.label);
} }
} }
@ -217,14 +166,14 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
} }
); );
}; };
$scope.checkNewMessages = function() { $scope.checkNewMessages = function () {
g_unreadCountAllConv = 0; Sms.unreadCountAllConv = 0;
$.getJSON($scope.generateUrl('/front-api/v1/new_messages'), $.getJSON($scope.generateUrl('/front-api/v1/new_messages'),
{ 'lastDate': $scope.lastContactListMsgDate }, {'lastDate': $scope.lastContactListMsgDate},
function(jsondata, status) { function (jsondata, status) {
var bufferedContacts = []; var bufferedContacts = [];
$.each(jsondata['phonelist'], function(id, val) { $.each(jsondata['phonelist'], function (id, val) {
var fn, peerLabel; var fn, peerLabel;
if (typeof jsondata['contacts'][id] === 'undefined') { if (typeof jsondata['contacts'][id] === 'undefined') {
peerLabel = id; peerLabel = id;
@ -259,7 +208,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
changeSelectedConversation($("a[mailbox-navigation='" + id + "']")); changeSelectedConversation($("a[mailbox-navigation='" + id + "']"));
} }
g_unreadCountAllConv += parseInt(val); Sms.unreadCountAllConv += parseInt(val);
} }
}); });
@ -269,27 +218,27 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
* there is new messages in all conversations * there is new messages in all conversations
*/ */
if (g_unreadCountNotifStep > 0) { if (Sms.unreadCountNotifStep > 0) {
g_unreadCountNotifStep--; Sms.unreadCountNotifStep--;
} }
if (g_unreadCountAllConv > 0) { if (Sms.unreadCountAllConv > 0) {
/* /*
* We notify user every two minutes for all messages * We notify user every two minutes for all messages
* or if unreadCount changes * or if unreadCount changes
*/ */
if (g_unreadCountNotifStep === 0 || g_lastUnreadCountAllConv !== g_unreadCountAllConv) { if (Sms.unreadCountNotifStep === 0 || Sms.lastUnreadCountAllConv !== Sms.unreadCountAllConv) {
$scope.desktopNotify(g_unreadCountAllConv + " unread message(s) for all conversations"); $scope.desktopNotify(Sms.unreadCountAllConv + " unread message(s) for all conversations");
g_unreadCountNotifStep = 12; Sms.unreadCountNotifStep = 12;
g_lastUnreadCountAllConv = g_unreadCountAllConv; Sms.lastUnreadCountAllConv = Sms.unreadCountAllConv;
} }
} }
} }
); );
}; };
$scope.removeConversation = function() { $scope.removeConversation = function () {
$.post($scope.generateUrl('/delete/conversation'), {"contact": $scope.selectedContact.label}, function(data) { $.post($scope.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 = "";
@ -309,7 +258,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
}; };
OC.Plugins.register('OCA.Search', { OC.Plugins.register('OCA.Search', {
attach: function(search) { attach: function (search) {
search.setFilter('sms', $scope.filterSms); search.setFilter('sms', $scope.filterSms);
} }
}); });
@ -325,7 +274,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$scope.removeContact = function (ct) { $scope.removeContact = function (ct) {
var len = $scope.contacts.length; var len = $scope.contacts.length;
for (var i=0; i < len; i++) { for (var i = 0; i < len; i++) {
var curCt = $scope.contacts[i]; var curCt = $scope.contacts[i];
if (curCt['nav'] === ct['nav']) { if (curCt['nav'] === ct['nav']) {
$scope.$apply(function () { $scope.$apply(function () {
@ -338,7 +287,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$scope.modifyContact = function (ct) { $scope.modifyContact = function (ct) {
var len = $scope.contacts.length; var len = $scope.contacts.length;
for (var i=0; i < len; i++) { for (var i = 0; i < len; i++) {
if ($scope.contacts[i]['nav'] === ct['nav']) { if ($scope.contacts[i]['nav'] === ct['nav']) {
$scope.$apply(function () { $scope.$apply(function () {
$scope.contacts[i].unread = parseInt(ct.unread); $scope.contacts[i].unread = parseInt(ct.unread);
@ -361,22 +310,22 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$scope.removeConversationMessage = function (msgId) { $scope.removeConversationMessage = function (msgId) {
var len = $scope.messages.length; var len = $scope.messages.length;
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($scope.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);
});
}); });
});
return; return;
} }
} }
}; };
$scope.fetchInitialSettings = function () { $scope.fetchInitialSettings = function () {
$.getJSON($scope.generateUrl('/front-api/v1/settings'), function(jsondata, status) { $.getJSON($scope.generateUrl('/front-api/v1/settings'), function (jsondata, status) {
if (jsondata['status'] === true) { if (jsondata['status'] === true) {
$('#sel_intl_phone').val(jsondata["country"]); $('#sel_intl_phone').val(jsondata["country"]);
@ -399,7 +348,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$scope.photoVersion = jsondata["photo_version"]; $scope.photoVersion = jsondata["photo_version"];
$.each(jsondata['phonelist'], function(id, val) { $.each(jsondata['phonelist'], function (id, val) {
var peerLabel; var peerLabel;
if (typeof jsondata['contacts'][id] === 'undefined') { if (typeof jsondata['contacts'][id] === 'undefined') {
peerLabel = id; peerLabel = id;
@ -411,7 +360,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
var contactObj = { var contactObj = {
'label': peerLabel, 'label': peerLabel,
'nav': id, 'nav': id,
'unread' : 0, 'unread': 0,
'lastmsg': parseInt(val) 'lastmsg': parseInt(val)
}; };
@ -430,7 +379,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
} }
}); });
$scope.$apply(function() { $scope.$apply(function () {
$scope.isContactsLoading = false; $scope.isContactsLoading = false;
}); });
@ -444,7 +393,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
} }
Notification.requestPermission(function (permission) { Notification.requestPermission(function (permission) {
if(!('permission' in Notification)) { if (!('permission' in Notification)) {
Notification.permission = permission; Notification.permission = permission;
} }
}); });
@ -458,7 +407,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
var msgClass = ''; var msgClass = '';
var msgCount = 0; var msgCount = 0;
$.each(jsondata["conversation"], function(id, vals) { $.each(jsondata["conversation"], function (id, vals) {
if (vals["type"] === 1) { if (vals["type"] === 1) {
msgClass = "recv"; msgClass = "recv";
} }
@ -471,17 +420,22 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
// Store the greater msg date for refresher // Store the greater msg date for refresher
// Note: we divide by 100 because number compare too large integers // Note: we divide by 100 because number compare too large integers
if ((id/100) > ($scope.lastConvMessageDate/100)) { if ((id / 100) > ($scope.lastConvMessageDate / 100)) {
$scope.lastConvMessageDate = id; $scope.lastConvMessageDate = id;
// Multiplicate ID to permit date to use it properly // Multiplicate ID to permit date to use it properly
$scope.addConversationMessage({'id': id, 'type': msgClass, 'date': new Date(id * 1), 'content': vals['msg']}); $scope.addConversationMessage({
'id': id,
'type': msgClass,
'date': new Date(id * 1),
'content': vals['msg']
});
buf = true; buf = true;
msgCount++; msgCount++;
} }
}); });
return [msgCount,buf]; return [msgCount, buf];
}; };
$scope.desktopNotify = function (msg) { $scope.desktopNotify = function (msg) {
@ -497,7 +451,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
} }
else if (Notification.permission !== 'denied') { else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) { Notification.requestPermission(function (permission) {
if(!('permission' in Notification)) { if (!('permission' in Notification)) {
Notification.permission = permission; Notification.permission = permission;
} }
if (permission === "granted") { if (permission === "granted") {
@ -512,10 +466,10 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
$timeout(function () { $timeout(function () {
// Register real title // Register real title
g_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($scope.generateUrl('/front-api/v1/peerlist'), function (jsondata, status) {
$scope.fetchInitialPeerList(jsondata); $scope.fetchInitialPeerList(jsondata);
var pnParam = $.urlParam('phonenumber'); var pnParam = $.urlParam('phonenumber');
@ -540,7 +494,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile'
} }
]); ]);
$.urlParam = function(name){ $.urlParam = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href); var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results == null) { if (results == null) {
return null; return null;
@ -555,20 +509,20 @@ function changeSelectedConversation(item) {
return; return;
} }
if (g_selectedConversation != null) { if (Sms.selectedConversation != null) {
g_selectedConversation.parent().removeClass('selected'); Sms.selectedConversation.parent().removeClass('selected');
} }
g_selectedConversation = item; Sms.selectedConversation = item;
g_selectedConversation.parent().addClass('selected'); Sms.selectedConversation.parent().addClass('selected');
g_selectedConversation.css("font-weight", "normal"); Sms.selectedConversation.css("font-weight", "normal");
g_selectedConversation.html(g_selectedConversation.attr("mailbox-label")); Sms.selectedConversation.html(Sms.selectedConversation.attr("mailbox-label"));
} }
(function ($, OC) { (function ($, OC) {
// reset count and title // reset count and title
window.onfocus = function () { window.onfocus = function () {
g_unreadCountCurrentConv = 0; Sms.unreadCountCurrentConv = 0;
document.title = g_originalTitle; document.title = Sms.originalTitle;
}; };
})(jQuery, OC); })(jQuery, OC);