2015-02-25 23:21:07 +01:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2015-02-25 23:21:07 +01:00
|
|
|
/**
|
|
|
|
* @name Preferences
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function Preferences() {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
this.defaults = {};
|
|
|
|
this.settings = {};
|
|
|
|
|
2015-07-08 20:15:39 +02:00
|
|
|
this.defaultsPromise = Preferences.$$resource.fetch("jsonDefaults").then(function(data) {
|
2015-08-17 16:10:57 +02:00
|
|
|
// We swap $key -> _$key to avoid an Angular bug (https://github.com/angular/angular.js/issues/6266)
|
2016-03-09 22:08:51 +01:00
|
|
|
var labels = _.fromPairs(_.map(data.SOGoMailLabelsColors, function(value, key) {
|
2015-08-17 16:10:57 +02:00
|
|
|
if (key.charAt(0) == '$')
|
|
|
|
return ['_' + key, value];
|
|
|
|
return [key, value];
|
|
|
|
}));
|
|
|
|
|
|
|
|
data.SOGoMailLabelsColors = labels;
|
|
|
|
|
2016-02-11 17:40:36 +01:00
|
|
|
// Mail editor autosave is a number of minutes or 0 if disabled
|
|
|
|
data.SOGoMailAutoSave = parseInt(data.SOGoMailAutoSave) || 0;
|
|
|
|
|
2016-02-11 22:45:58 +01:00
|
|
|
// Specify a base font size for HTML messages when SOGoMailComposeFontSize is not zero
|
|
|
|
data.SOGoMailComposeFontSizeEnabled = parseInt(data.SOGoMailComposeFontSize) > 0;
|
|
|
|
|
2016-02-12 18:03:26 +01:00
|
|
|
if (window.CKEDITOR && data.SOGoMailComposeFontSizeEnabled) {
|
2016-02-11 22:45:58 +01:00
|
|
|
// HTML editor is enabled; set user's preferred font size
|
|
|
|
window.CKEDITOR.config.fontSize_defaultLabel = data.SOGoMailComposeFontSize;
|
|
|
|
window.CKEDITOR.addCss('.cke_editable { font-size: ' + data.SOGoMailComposeFontSize + 'px; }');
|
|
|
|
}
|
|
|
|
|
2015-08-17 16:10:57 +02:00
|
|
|
// We convert our list of autoReplyEmailAddresses/forwardAddress into a string.
|
|
|
|
// We also convert our date objects into real date, otherwise we'll have strings
|
|
|
|
// or undefined values and the md-datepicker does NOT like this.
|
|
|
|
if (data.Vacation) {
|
|
|
|
if (data.Vacation.endDate)
|
|
|
|
data.Vacation.endDate = new Date(parseInt(data.Vacation.endDate) * 1000);
|
|
|
|
else {
|
|
|
|
data.Vacation.endDateEnabled = 0;
|
|
|
|
data.Vacation.endDate = new Date();
|
|
|
|
}
|
2015-12-09 20:06:07 +01:00
|
|
|
if (data.Vacation.autoReplyEmailAddresses && data.Vacation.autoReplyEmailAddresses.length)
|
2015-05-06 18:12:27 +02:00
|
|
|
data.Vacation.autoReplyEmailAddresses = data.Vacation.autoReplyEmailAddresses.join(",");
|
2015-12-09 20:06:07 +01:00
|
|
|
else
|
|
|
|
delete data.Vacation.autoReplyEmailAddresses;
|
|
|
|
} else
|
2015-08-17 16:10:57 +02:00
|
|
|
data.Vacation = {};
|
2015-12-09 20:06:07 +01:00
|
|
|
|
2015-12-10 01:28:26 +01:00
|
|
|
if (angular.isUndefined(data.Vacation.autoReplyEmailAddresses) &&
|
2015-12-09 20:06:07 +01:00
|
|
|
angular.isDefined(window.defaultEmailAddresses))
|
|
|
|
data.Vacation.autoReplyEmailAddresses = window.defaultEmailAddresses;
|
|
|
|
|
2015-12-10 01:28:26 +01:00
|
|
|
if (angular.isUndefined(data.Vacation.daysBetweenResponse))
|
2015-12-09 20:06:07 +01:00
|
|
|
data.Vacation.daysBetweenResponse = 7;
|
|
|
|
|
2015-12-10 01:28:26 +01:00
|
|
|
if (angular.isUndefined(data.Vacation.endDate)) {
|
2015-08-17 16:10:57 +02:00
|
|
|
data.Vacation.endDateEnabled = 0;
|
|
|
|
data.Vacation.endDate = new Date();
|
|
|
|
}
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2015-08-17 16:10:57 +02:00
|
|
|
if (data.Forward && data.Forward.forwardAddress)
|
|
|
|
data.Forward.forwardAddress = data.Forward.forwardAddress.join(",");
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2015-12-10 01:28:26 +01:00
|
|
|
if (angular.isUndefined(data.SOGoCalendarCategoriesColors)) {
|
|
|
|
data.SOGoCalendarCategoriesColors = {};
|
|
|
|
data.SOGoCalendarCategories = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (angular.isUndefined(data.SOGoContactsCategories))
|
|
|
|
data.SOGoContactsCategories = [];
|
|
|
|
|
2015-08-17 16:10:57 +02:00
|
|
|
angular.extend(_this.defaults, data);
|
2015-07-08 20:15:39 +02:00
|
|
|
|
2016-01-26 13:20:02 +01:00
|
|
|
// Configure date locale
|
2016-02-10 22:18:12 +01:00
|
|
|
_this.$mdDateLocaleProvider = Preferences.$mdDateLocaleProvider;
|
|
|
|
angular.extend(_this.$mdDateLocaleProvider, data.locale);
|
|
|
|
_this.$mdDateLocaleProvider.firstDayOfWeek = parseInt(data.SOGoFirstDayOfWeek);
|
|
|
|
_this.$mdDateLocaleProvider.weekNumberFormatter = function(weekNumber) {
|
2016-01-11 22:32:12 +01:00
|
|
|
return l('Week %d', weekNumber);
|
|
|
|
};
|
2016-02-10 22:18:12 +01:00
|
|
|
_this.$mdDateLocaleProvider.msgCalendar = l('Calender');
|
|
|
|
_this.$mdDateLocaleProvider.msgOpenCalendar = l('Open Calendar');
|
|
|
|
_this.$mdDateLocaleProvider.parseDate = function(dateString) {
|
|
|
|
return dateString? dateString.parseDate(_this.$mdDateLocaleProvider, data.SOGoShortDateFormat) : new Date(NaN);
|
2016-01-11 22:32:12 +01:00
|
|
|
};
|
2016-02-10 22:18:12 +01:00
|
|
|
_this.$mdDateLocaleProvider.formatDate = function(date) {
|
|
|
|
return date? date.format(_this.$mdDateLocaleProvider, data.SOGoShortDateFormat) : '';
|
2016-01-11 22:32:12 +01:00
|
|
|
};
|
2016-02-10 22:18:12 +01:00
|
|
|
_this.$mdDateLocaleProvider.formatTime = function(date) {
|
|
|
|
return date? date.format(_this.$mdDateLocaleProvider, data.SOGoTimeFormat) : '';
|
2016-01-11 22:32:12 +01:00
|
|
|
};
|
|
|
|
|
2015-07-08 20:15:39 +02:00
|
|
|
return _this.defaults;
|
2015-05-06 18:12:27 +02:00
|
|
|
});
|
|
|
|
|
2015-07-08 20:15:39 +02:00
|
|
|
this.settingsPromise = Preferences.$$resource.fetch("jsonSettings").then(function(data) {
|
2016-01-11 22:32:12 +01:00
|
|
|
// We convert our PreventInvitationsWhitelist hash into a array of user
|
2015-12-07 17:05:01 +01:00
|
|
|
if (data.Calendar) {
|
|
|
|
if (data.Calendar.PreventInvitationsWhitelist)
|
2015-05-06 18:12:27 +02:00
|
|
|
data.Calendar.PreventInvitationsWhitelist = _.map(data.Calendar.PreventInvitationsWhitelist, function(value, key) {
|
2015-05-19 17:32:12 +02:00
|
|
|
var match = /^(.+)\s<(\S+)>$/.exec(value);
|
|
|
|
return new Preferences.$User({uid: key, cn: match[1], c_email: match[2]});
|
2015-05-06 18:12:27 +02:00
|
|
|
});
|
2015-12-07 17:05:01 +01:00
|
|
|
else
|
|
|
|
data.Calendar.PreventInvitationsWhitelist = [];
|
|
|
|
}
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2015-07-08 20:15:39 +02:00
|
|
|
angular.extend(_this.settings, data);
|
|
|
|
|
|
|
|
return _this.settings;
|
2015-05-06 18:12:27 +02:00
|
|
|
});
|
2015-02-25 23:21:07 +01:00
|
|
|
}
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2015-02-25 23:21:07 +01:00
|
|
|
/**
|
|
|
|
* @memberof Preferences
|
|
|
|
* @desc The factory we'll use to register with Angular
|
|
|
|
* @returns the Preferences constructor
|
|
|
|
*/
|
2016-01-11 22:32:12 +01:00
|
|
|
Preferences.$factory = ['$q', '$timeout', '$log', '$mdDateLocale', 'sgSettings', 'Resource', 'User', function($q, $timeout, $log, $mdDateLocaleProvider, Settings, Resource, User) {
|
2015-02-25 23:21:07 +01:00
|
|
|
angular.extend(Preferences, {
|
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
|
|
|
$log: $log,
|
2016-01-11 22:32:12 +01:00
|
|
|
$mdDateLocaleProvider: $mdDateLocaleProvider,
|
2015-08-03 17:53:54 +02:00
|
|
|
$$resource: new Resource(Settings.activeUser('folderURL'), Settings.activeUser()),
|
2015-08-03 22:18:23 +02:00
|
|
|
activeUser: Settings.activeUser(),
|
2015-04-20 23:28:43 +02:00
|
|
|
$User: User
|
2015-02-25 23:21:07 +01:00
|
|
|
});
|
|
|
|
|
2015-07-08 20:15:39 +02:00
|
|
|
return new Preferences(); // return unique instance
|
2015-02-25 23:21:07 +01:00
|
|
|
}];
|
|
|
|
|
2015-07-29 21:16:43 +02:00
|
|
|
/* Initialize module if necessary */
|
|
|
|
try {
|
|
|
|
angular.module('SOGo.PreferencesUI');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
angular.module('SOGo.PreferencesUI', ['SOGo.Common']);
|
|
|
|
}
|
|
|
|
|
2015-02-25 23:21:07 +01:00
|
|
|
/* Factory registration in Angular module */
|
|
|
|
angular.module('SOGo.PreferencesUI')
|
2015-05-06 04:06:13 +02:00
|
|
|
.factory('Preferences', Preferences.$factory);
|
2015-02-25 23:21:07 +01:00
|
|
|
|
2015-07-08 20:15:39 +02:00
|
|
|
/**
|
|
|
|
* @function ready
|
|
|
|
* @memberof Preferences.prototype
|
|
|
|
* @desc Combine promises used to load user's defaults and settings.
|
|
|
|
* @return a combined promise
|
|
|
|
*/
|
|
|
|
Preferences.prototype.ready = function() {
|
|
|
|
return Preferences.$q.all([this.defaultsPromise, this.settingsPromise]);
|
|
|
|
};
|
|
|
|
|
2015-02-25 23:21:07 +01:00
|
|
|
/**
|
|
|
|
* @function $save
|
|
|
|
* @memberof Preferences.prototype
|
|
|
|
* @desc Save the preferences to the server.
|
|
|
|
*/
|
2015-05-06 18:12:27 +02:00
|
|
|
Preferences.prototype.$save = function() {
|
2015-02-25 23:21:07 +01:00
|
|
|
var _this = this;
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2015-05-19 17:32:12 +02:00
|
|
|
return Preferences.$$resource.save("Preferences", this.$omit(true))
|
2015-02-25 23:21:07 +01:00
|
|
|
.then(function(data) {
|
|
|
|
// Make a copy of the data for an eventual reset
|
|
|
|
//_this.$shadowData = _this.$omit(true);
|
|
|
|
return data;
|
|
|
|
});
|
2015-05-06 18:12:27 +02:00
|
|
|
};
|
2015-02-25 23:21:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $omit
|
|
|
|
* @memberof Preferences.prototype
|
|
|
|
* @desc Return a sanitized object used to send to the server.
|
|
|
|
* @param {Boolean} [deep] - make a deep copy if true
|
|
|
|
* @return an object literal copy of the Preferences instance
|
|
|
|
*/
|
2015-05-06 18:12:27 +02:00
|
|
|
Preferences.prototype.$omit = function(deep) {
|
2015-11-24 21:05:03 +01:00
|
|
|
var preferences, labels, whitelist;
|
|
|
|
|
|
|
|
preferences = {};
|
|
|
|
whitelist = {};
|
|
|
|
|
2015-02-25 23:21:07 +01:00
|
|
|
angular.forEach(this, function(value, key) {
|
2015-03-21 20:34:18 +01:00
|
|
|
if (key != 'constructor' && key[0] != '$') {
|
2015-02-25 23:21:07 +01:00
|
|
|
if (deep)
|
|
|
|
preferences[key] = angular.copy(value);
|
|
|
|
else
|
|
|
|
preferences[key] = value;
|
|
|
|
}
|
|
|
|
});
|
2015-03-21 20:34:18 +01:00
|
|
|
|
|
|
|
// We swap _$key -> $key to avoid an Angular bug (https://github.com/angular/angular.js/issues/6266)
|
2016-03-09 22:08:51 +01:00
|
|
|
labels = _.fromPairs(_.map(preferences.defaults.SOGoMailLabelsColors, function(value, key) {
|
2015-12-10 21:12:12 +01:00
|
|
|
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];
|
|
|
|
}
|
2015-03-21 20:34:18 +01:00
|
|
|
return [key.substring(1), value];
|
2015-12-10 21:12:12 +01:00
|
|
|
}
|
2015-03-21 20:34:18 +01:00
|
|
|
return [key, value];
|
|
|
|
}));
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2015-03-21 20:34:18 +01:00
|
|
|
preferences.defaults.SOGoMailLabelsColors = labels;
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2016-02-11 22:45:58 +01:00
|
|
|
if (!preferences.defaults.SOGoMailComposeFontSizeEnabled)
|
|
|
|
preferences.defaults.SOGoMailComposeFontSize = 0;
|
2016-02-12 18:03:26 +01:00
|
|
|
delete preferences.defaults.SOGoMailComposeFontSizeEnabled;
|
2016-02-11 22:45:58 +01:00
|
|
|
|
2015-08-17 16:10:57 +02:00
|
|
|
if (preferences.defaults.Vacation) {
|
|
|
|
if (preferences.defaults.Vacation.endDateEnabled)
|
|
|
|
preferences.defaults.Vacation.endDate = preferences.defaults.Vacation.endDate.getTime()/1000;
|
|
|
|
else
|
|
|
|
preferences.defaults.Vacation.endDate = 0;
|
|
|
|
|
|
|
|
if (preferences.defaults.Vacation.autoReplyEmailAddresses)
|
2015-12-10 00:53:35 +01:00
|
|
|
preferences.defaults.Vacation.autoReplyEmailAddresses = _.filter(preferences.defaults.Vacation.autoReplyEmailAddresses.split(","), function(v) { return v.length; });
|
2015-09-19 01:55:37 +02:00
|
|
|
else
|
|
|
|
preferences.defaults.Vacation.autoReplyEmailAddresses = [];
|
2015-08-17 16:10:57 +02:00
|
|
|
}
|
2015-04-20 23:28:43 +02:00
|
|
|
|
2015-04-28 19:56:50 +02:00
|
|
|
if (preferences.defaults.Forward && preferences.defaults.Forward.forwardAddress)
|
|
|
|
preferences.defaults.Forward.forwardAddress = preferences.defaults.Forward.forwardAddress.split(",");
|
|
|
|
|
2015-04-20 23:28:43 +02:00
|
|
|
if (preferences.settings.Calendar && preferences.settings.Calendar.PreventInvitationsWhitelist) {
|
2016-03-03 19:38:54 +01:00
|
|
|
_.forEach(preferences.settings.Calendar.PreventInvitationsWhitelist, function(user) {
|
2015-11-24 21:05:03 +01:00
|
|
|
whitelist[user.uid] = user.$shortFormat();
|
2015-04-20 23:28:43 +02:00
|
|
|
});
|
2015-11-24 21:05:03 +01:00
|
|
|
preferences.settings.Calendar.PreventInvitationsWhitelist = whitelist;
|
2015-04-20 23:28:43 +02:00
|
|
|
}
|
2015-05-06 18:12:27 +02:00
|
|
|
|
2015-02-25 23:21:07 +01:00
|
|
|
return preferences;
|
2015-05-06 18:12:27 +02:00
|
|
|
};
|
|
|
|
|
2015-02-25 23:21:07 +01:00
|
|
|
})();
|