From ba4c7a5300d7eb5ea15c13a8b3fd9b27aa4cf53a Mon Sep 17 00:00:00 2001 From: Hivert Quentin Date: Thu, 20 Jul 2023 09:26:16 +0200 Subject: [PATCH] fix(preferences): add autocomplete for recipients in automatic forward --- UI/Templates/PreferencesUI/UIxPreferences.wox | 30 ++++- .../js/Preferences/PreferencesController.js | 106 +++++++++++++++++- 2 files changed, 130 insertions(+), 6 deletions(-) diff --git a/UI/Templates/PreferencesUI/UIxPreferences.wox b/UI/Templates/PreferencesUI/UIxPreferences.wox index b3ed9fe89..69863d653 100644 --- a/UI/Templates/PreferencesUI/UIxPreferences.wox +++ b/UI/Templates/PreferencesUI/UIxPreferences.wox @@ -1100,7 +1100,7 @@ md-autofocus="true" md-autoselect="true" label:placeholder="Enter an email" - label:secondary-placeholder="Add another email">> + label:secondary-placeholder="Add another email">
@@ -1301,13 +1301,35 @@ id="forwardAddress" name="forwardAddress" ng-model="app.preferences.defaults.Forward.forwardAddress" + md-transform-chip="app.addRecipient($chip)" ng-required="app.preferences.defaults.Forward.enabled == 1" md-separator-keys="app.emailSeparatorKeys" md-add-on-blur="true" - md-autocomplete-snap="width" - label:placeholder="Enter an email" - label:secondary-placeholder="Add another email"> + md-autocomplete-snap="width"> + + +
+
+
{{ user.$shortFormat(app.autocompleteForward.searchText) }}
+
+
{{ user.containername }}
+
+
+
diff --git a/UI/WebServerResources/js/Preferences/PreferencesController.js b/UI/WebServerResources/js/Preferences/PreferencesController.js index b553916df..6e03814b9 100644 --- a/UI/WebServerResources/js/Preferences/PreferencesController.js +++ b/UI/WebServerResources/js/Preferences/PreferencesController.js @@ -7,8 +7,8 @@ /** * @ngInject */ - PreferencesController.$inject = ['$q', '$window', '$state', '$mdMedia', '$mdSidenav', '$mdDialog', '$mdToast', 'sgSettings', 'sgFocus', 'Dialog', 'User', 'Account', 'Preferences', 'Authentication']; - function PreferencesController($q, $window, $state, $mdMedia, $mdSidenav, $mdDialog, $mdToast, sgSettings, focus, Dialog, User, Account, Preferences, Authentication) { + PreferencesController.$inject = ['$q', '$window', '$state', '$mdMedia', '$mdSidenav', '$mdDialog', '$mdToast', 'sgSettings', 'sgFocus', 'Dialog', 'User', 'Account', 'Preferences', 'Authentication', 'AddressBook']; + function PreferencesController($q, $window, $state, $mdMedia, $mdSidenav, $mdDialog, $mdToast, sgSettings, focus, Dialog, User, Account, Preferences, Authentication, AddressBook) { var vm = this, mailboxes = [], today = new Date().beginOfDay(); this.$onInit = function() { @@ -17,6 +17,7 @@ this.timeZonesList = $window.timeZonesList; this.timeZonesSearchText = ''; this.addressesSearchText = ''; + this.autocompleteForward = {}; this.mailLabelKeyRE = new RegExp(/^(?!^_\$)[^(){} %*\"\\\\]*?$/); this.emailSeparatorKeys = Preferences.defaults.emailSeparatorKeys; if (Preferences.defaults.SOGoMailAutoMarkAsReadMode == 'delay') @@ -615,6 +616,107 @@ } } }; + + this.contactFilter = function ($query) { + return AddressBook.$filterAll($query, [], {priority: 'gcs'}).then(function(cards) { + // Divide the matching cards by email addresses so the user can select + // the recipient address of her choice + var explodedCards = []; + _.forEach(_.invokeMap(cards, 'explode'), function(manyCards) { + _.forEach(manyCards, function(card) { + explodedCards.push(card); + }); + }); + // Remove duplicates + return _.uniqBy(explodedCards, function(card) { + return card.$$fullname + ' ' + card.$$email + ' ' + card.containername; + }); + }); + }; + + this.ignoreReturn = function ($event) { + if ($event.keyCode == 13) { + $event.stopPropagation(); + $event.preventDefault(); + return false; + } + if ($event.keyCode == 186 && $event.key == 'ü') { //Key code for separator ';' but is keycode for ü in german keyboard + $event.stopPropagation(); + $event.preventDefault(); + let element = $window.document.getElementById($event.target.id); + element.value = element.value + 'ü' + } + }; + + this.addRecipient = function (contact) { + var recipients, recipient, list, i, address; + + recipients = this.preferences.defaults.Forward.forwardAddress; + + if (angular.isString(contact)) { + // Examples that are handled: + // Smith, John + // ; + // foo@bar.com abc@xyz.com + address = ''; + for (i = 0; i < contact.length; i++) { + if ((contact.charCodeAt(i) == 9 || // tab + contact.charCodeAt(i) == 32 || // space + contact.charCodeAt(i) == 44 || // , + contact.charCodeAt(i) == 59) && // ; + address.isValidEmail() && + recipients.indexOf(address) < 0) { + recipients.push(address); + address = ''; + } + else { + address += contact.charAt(i); + } + } + if (address && recipients.indexOf(address) < 0) + recipients.push(address); + + return null; + } + + if (contact.$isList({expandable: true})) { + // If the list's members were already fetch, use them + if (angular.isDefined(contact.refs) && contact.refs.length) { + _.forEach(contact.refs, function(ref) { + if (ref.email.length && recipients.indexOf(ref.$shortFormat()) < 0) + recipients.push(ref.$shortFormat()); + }); + } + else { + list = Card.$find(contact.container, contact.c_name); + list.$id().then(function(listId) { + _.forEach(list.refs, function(ref) { + if (ref.email.length && recipients.indexOf(ref.$shortFormat()) < 0) + recipients.push(ref.$shortFormat()); + }); + }); + } + } + else if (contact.$isGroup({expandable: true})) { + recipient = { + toString: function () { return contact.$shortFormat(); }, + isExpandable: true, + members: [] + }; + contact.$members().then(function (members) { + recipient.members = members; + }); + } + else { + recipient = contact.$shortFormat(); + } + + if (recipient) + return recipient; + else + return null; + }; + } angular