From e59a8e12ba8f8418091702dc3e2849581da35a5c Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Thu, 12 Oct 2017 15:44:14 -0400 Subject: [PATCH] (js) Allow edition of IMAP flags/labels --- NEWS | 1 + .../English.lproj/Localizable.strings | 4 +++ UI/Templates/PreferencesUI/UIxPreferences.wox | 35 ++++++++++++++----- .../js/Preferences/Preferences.service.js | 33 ++++++++--------- .../js/Preferences/PreferencesController.js | 32 ++++++++++++++--- 5 files changed, 72 insertions(+), 33 deletions(-) diff --git a/NEWS b/NEWS index 66cb40df1..268faa1e4 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,7 @@ Enhancements - [web] follow requested URL after user authentication - [web] added Simplified Chinese (zh_CN) translation - thanks to Thomas Kuiper - [web] now also give modify permission when selecting all calendar rights + - [web] allow edition of IMAP flags associated to mail labels Bug fixes - [core] yearly repeating events are not shown in web calendar (#4237) diff --git a/UI/PreferencesUI/English.lproj/Localizable.strings b/UI/PreferencesUI/English.lproj/Localizable.strings index 87c6c26d5..2d4adead1 100644 --- a/UI/PreferencesUI/English.lproj/Localizable.strings +++ b/UI/PreferencesUI/English.lproj/Localizable.strings @@ -147,6 +147,10 @@ /* Mailer */ "Labels" = "Labels"; "Label" = "Label"; +"IMAP Label" = "IMAP Label"; +"Invalid label" = "Don't use space, nor ( ) { } % * \" \\"; +"Duplicate label" = "Duplicate label"; +"IMAP labels must have unique names." = "IMAP labels must have unique names."; "New label" = "New label"; "Show subscribed mailboxes only" = "Show subscribed mailboxes only"; "Synchronize only default mail folders (EAS)" = "Synchronize only default mail folders (EAS)"; diff --git a/UI/Templates/PreferencesUI/UIxPreferences.wox b/UI/Templates/PreferencesUI/UIxPreferences.wox index 694a6adeb..911279b8f 100644 --- a/UI/Templates/PreferencesUI/UIxPreferences.wox +++ b/UI/Templates/PreferencesUI/UIxPreferences.wox @@ -771,18 +771,35 @@ id="mailLabelsView-content" class="md-padding"> - - - - + ng-repeat="key in app.preferences.defaults.SOGoMailLabelsColorsKeys + track by $index"> + +
+ + + + + + + +
+
+
+
+
+
+ ng-click="app.removeMailLabel($index, preferencesForm)"> remove_circle
diff --git a/UI/WebServerResources/js/Preferences/Preferences.service.js b/UI/WebServerResources/js/Preferences/Preferences.service.js index 860b8d9f2..0fcff8174 100644 --- a/UI/WebServerResources/js/Preferences/Preferences.service.js +++ b/UI/WebServerResources/js/Preferences/Preferences.service.js @@ -22,13 +22,13 @@ data = {}; } - // We swap $key -> _$key to avoid an Angular bug (https://github.com/angular/angular.js/issues/6266) - var labels = _.fromPairs(_.map(data.SOGoMailLabelsColors, function(value, key) { - if (key.charAt(0) == '$') - return ['_' + key, value]; - return [key, value]; - })); - data.SOGoMailLabelsColors = labels; + // Split mail labels keys and values + data.SOGoMailLabelsColorsKeys = []; + data.SOGoMailLabelsColorsValues = []; + _.forEach(data.SOGoMailLabelsColors, function (value, key) { + data.SOGoMailLabelsColorsKeys.push(key); + data.SOGoMailLabelsColorsValues.push(value); + }); _.forEach(data.SOGoSieveFilters, function(filter) { _.forEach(filter.actions, function(action) { @@ -268,18 +268,13 @@ } }); - // We swap _$key -> $key to avoid an Angular bug (https://github.com/angular/angular.js/issues/6266) - labels = _.fromPairs(_.map(preferences.defaults.SOGoMailLabelsColors, function(value, key) { - if (key.charAt(0) == '_' && key.charAt(1) == '$') { - // New key, let's take the value and flatten it - if (key.length > 2 && key.charAt(2) == '$') { - return [value[0].toLowerCase().replace(/[ \(\)\/\{%\*<>\\\"]/g, "_"), value]; - } - return [key.substring(1), value]; - } - return [key, value]; - })); - preferences.defaults.SOGoMailLabelsColors = labels; + // Merge back mail labels keys and values + preferences.defaults.SOGoMailLabelsColors = {}; + _.forEach(preferences.defaults.SOGoMailLabelsColorsKeys, function(key, i) { + preferences.defaults.SOGoMailLabelsColors[key] = preferences.defaults.SOGoMailLabelsColorsValues[i]; + }); + delete preferences.defaults.SOGoMailLabelsColorsKeys; + delete preferences.defaults.SOGoMailLabelsColorsValues; _.forEach(preferences.defaults.SOGoSieveFilters, function(filter) { _.forEach(filter.actions, function(action) { diff --git a/UI/WebServerResources/js/Preferences/PreferencesController.js b/UI/WebServerResources/js/Preferences/PreferencesController.js index 2d4f54899..633703418 100644 --- a/UI/WebServerResources/js/Preferences/PreferencesController.js +++ b/UI/WebServerResources/js/Preferences/PreferencesController.js @@ -17,6 +17,7 @@ this.timeZonesList = $window.timeZonesList; this.timeZonesSearchText = ''; this.sieveVariablesCapability = ($window.sieveCapabilities.indexOf('variables') >= 0); + this.mailLabelKeyRE = new RegExp("^[^(){} %*\"\\\\]*$"); if (sgSettings.activeUser('path').mail) { @@ -154,17 +155,23 @@ this.preferences.defaults.AuxiliaryMailAccounts.splice(index, 1); form.$setDirty(); }; - + + this.resetMailLabelValidity = function(index, form) { + form['mailIMAPLabel_' + index].$setValidity('duplicate', true); + }; + this.addMailLabel = function(form) { // See $omit() in the Preferences services for real key generation var key = '_$$' + guid(); - this.preferences.defaults.SOGoMailLabelsColors[key] = ["New label", "#aaa"]; - focus('mailLabel_' + (_.size(this.preferences.defaults.SOGoMailLabelsColors) - 1)); + this.preferences.defaults.SOGoMailLabelsColorsKeys.push("label"); + this.preferences.defaults.SOGoMailLabelsColorsValues.push(["New label", "#aaa"]); + focus('mailLabel_' + (_.size(this.preferences.defaults.SOGoMailLabelsColorsKeys) - 1)); form.$setDirty(); }; - this.removeMailLabel = function(key, form) { - delete this.preferences.defaults.SOGoMailLabelsColors[key]; + this.removeMailLabel = function(index, form) { + this.preferences.defaults.SOGoMailLabelsColorsKeys.splice(index, 1); + this.preferences.defaults.SOGoMailLabelsColorsValues.splice(index, 1); form.$setDirty(); }; @@ -310,6 +317,21 @@ } } + if (this.preferences.defaults.SOGoMailLabelsColorsKeys.length != + this.preferences.defaults.SOGoMailLabelsColorsValues.length || + this.preferences.defaults.SOGoMailLabelsColorsKeys.length != + _.uniq(this.preferences.defaults.SOGoMailLabelsColorsKeys).length) { + Dialog.alert(l('Error'), l("IMAP labels must have unique names.")); + _.forEach(this.preferences.defaults.SOGoMailLabelsColorsKeys, function (value, i, keys) { + if (form['mailIMAPLabel_' + i].$dirty && + (keys.indexOf(value) != i || + keys.indexOf(value, i+1) > -1)) { + form['mailIMAPLabel_' + i].$setValidity('duplicate', false); + sendForm = false; + } + }); + } + if (sendForm) return this.preferences.$save().then(function(data) { if (!options || !options.quick) {