2015-05-06 04:06:13 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2015-11-11 17:18:49 +01:00
|
|
|
CalendarsController.$inject = ['$rootScope', '$scope', '$window', '$mdDialog', '$log', '$mdToast', 'FileUploader', 'sgFocus', 'Dialog', 'sgSettings', 'Calendar', 'User', 'stateCalendars'];
|
|
|
|
function CalendarsController($rootScope, $scope, $window, $mdDialog, $log, $mdToast, FileUploader, focus, Dialog, Settings, Calendar, User, stateCalendars) {
|
2015-05-06 04:06:13 +02:00
|
|
|
var vm = this;
|
|
|
|
|
|
|
|
vm.activeUser = Settings.activeUser;
|
|
|
|
vm.service = Calendar;
|
|
|
|
vm.newCalendar = newCalendar;
|
|
|
|
vm.addWebCalendar = addWebCalendar;
|
|
|
|
vm.confirmDelete = confirmDelete;
|
2015-09-17 16:33:12 +02:00
|
|
|
vm.editFolder = editFolder;
|
|
|
|
vm.revertEditing = revertEditing;
|
|
|
|
vm.renameFolder = renameFolder;
|
2015-05-06 04:06:13 +02:00
|
|
|
vm.share = share;
|
2015-11-11 17:18:49 +01:00
|
|
|
vm.importCalendar = importCalendar;
|
|
|
|
vm.exportCalendar = exportCalendar;
|
2015-06-12 19:34:08 +02:00
|
|
|
vm.showLinks = showLinks;
|
2015-06-15 15:53:10 +02:00
|
|
|
vm.showProperties = showProperties;
|
2015-05-06 04:06:13 +02:00
|
|
|
vm.subscribeToFolder = subscribeToFolder;
|
2015-09-22 16:17:17 +02:00
|
|
|
vm.today = today;
|
2015-05-06 04:06:13 +02:00
|
|
|
|
|
|
|
// Dispatch the event named 'calendars:list' when a calendar is activated or deactivated or
|
|
|
|
// when the color of a calendar is changed
|
|
|
|
$scope.$watch(
|
|
|
|
function() {
|
|
|
|
return _.union(
|
2015-07-24 22:14:53 +02:00
|
|
|
_.map(Calendar.$calendars, function(o) { return _.pick(o, ['id', 'active', 'color']); }),
|
|
|
|
_.map(Calendar.$subscriptions, function(o) { return _.pick(o, ['id', 'active', 'color']); }),
|
|
|
|
_.map(Calendar.$webcalendars, function(o) { return _.pick(o, ['id', 'active', 'color']); })
|
2015-05-06 04:06:13 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
function(newList, oldList) {
|
|
|
|
// Identify which calendar has changed
|
|
|
|
var ids = _.pluck(_.filter(newList, function(o, i) { return !_.isEqual(o, oldList[i]); }), 'id');
|
|
|
|
if (ids.length > 0) {
|
|
|
|
$log.debug(ids.join(', ') + ' changed');
|
|
|
|
_.each(ids, function(id) {
|
|
|
|
var calendar = Calendar.$get(id);
|
|
|
|
calendar.$setActivation().then(function() {
|
2015-11-06 21:12:24 +01:00
|
|
|
$rootScope.$emit('calendars:list');
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true // compare for object equality
|
|
|
|
);
|
|
|
|
|
|
|
|
function newCalendar(ev) {
|
|
|
|
Dialog.prompt(l('New calendar'), l('Name of the Calendar'))
|
|
|
|
.then(function(name) {
|
|
|
|
var calendar = new Calendar(
|
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
isEditable: true,
|
|
|
|
isRemote: false,
|
|
|
|
owner: UserLogin
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Calendar.$add(calendar);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addWebCalendar() {
|
|
|
|
Dialog.prompt(l('Subscribe to a web calendar...'), l('URL of the Calendar'), {inputType: 'url'})
|
|
|
|
.then(function(url) {
|
|
|
|
Calendar.$addWebCalendar(url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirmDelete(folder) {
|
|
|
|
if (folder.isSubscription) {
|
|
|
|
// Unsubscribe without confirmation
|
|
|
|
folder.$delete()
|
|
|
|
.then(function() {
|
2015-11-06 21:12:24 +01:00
|
|
|
$rootScope.$emit('calendars:list');
|
2015-05-06 04:06:13 +02:00
|
|
|
}, function(data, status) {
|
2015-08-17 19:44:21 +02:00
|
|
|
Dialog.alert(l('An error occured while deleting the calendar "%{0}".', folder.name),
|
2015-05-06 04:06:13 +02:00
|
|
|
l(data.error));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2015-08-17 19:44:21 +02:00
|
|
|
Dialog.confirm(l('Warning'), l('Are you sure you want to delete the calendar <em>%{0}</em>?', folder.name))
|
2015-05-06 04:06:13 +02:00
|
|
|
.then(function() {
|
|
|
|
folder.$delete()
|
|
|
|
.then(function() {
|
2015-11-06 21:12:24 +01:00
|
|
|
$rootScope.$emit('calendars:list');
|
2015-05-06 04:06:13 +02:00
|
|
|
}, function(data, status) {
|
2015-08-17 19:44:21 +02:00
|
|
|
Dialog.alert(l('An error occured while deleting the calendar "%{0}".', folder.name),
|
2015-05-06 04:06:13 +02:00
|
|
|
l(data.error));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:18:49 +01:00
|
|
|
function importCalendar($event, folder) {
|
|
|
|
$mdDialog.show({
|
|
|
|
parent: angular.element(document.body),
|
|
|
|
targetEvent: $event,
|
|
|
|
clickOutsideToClose: true,
|
|
|
|
escapeToClose: true,
|
|
|
|
templateUrl: 'UIxCalendarImportDialog',
|
|
|
|
controller: CalendarImportDialogController,
|
|
|
|
controllerAs: '$CalendarImportDialogController',
|
|
|
|
locals: {
|
|
|
|
folder: folder
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
|
|
|
CalendarImportDialogController.$inject = ['scope', '$mdDialog', 'folder'];
|
|
|
|
function CalendarImportDialogController(scope, $mdDialog, folder) {
|
|
|
|
var vm = this;
|
|
|
|
|
|
|
|
vm.uploader = new FileUploader({
|
|
|
|
url: ApplicationBaseURL + [folder.id, 'import'].join('/'),
|
|
|
|
autoUpload: true,
|
|
|
|
queueLimit: 1,
|
|
|
|
filters: [{ name: filterByExtension, fn: filterByExtension }],
|
|
|
|
onSuccessItem: function(item, response, status, headers) {
|
|
|
|
var msg;
|
|
|
|
|
|
|
|
$mdDialog.hide();
|
|
|
|
|
|
|
|
if (response.imported === 0)
|
|
|
|
msg = l('No event was imported.');
|
|
|
|
else {
|
|
|
|
msg = l('A total of %{0} events were imported in the calendar.', response.imported);
|
|
|
|
$rootScope.$emit('calendars:list');
|
|
|
|
}
|
|
|
|
|
|
|
|
$mdToast.show(
|
|
|
|
$mdToast.simple()
|
|
|
|
.content(msg)
|
|
|
|
.position('top right')
|
|
|
|
.hideDelay(3000));
|
|
|
|
},
|
|
|
|
onErrorItem: function(item, response, status, headers) {
|
|
|
|
$mdToast.show({
|
|
|
|
template: [
|
|
|
|
'<md-toast>',
|
|
|
|
' <md-icon class="md-warn md-hue-1">error_outline</md-icon>',
|
|
|
|
' <span>' + l('An error occurred while importing calendar.') + '</span>',
|
|
|
|
'</md-toast>'
|
|
|
|
].join(''),
|
|
|
|
position: 'top right',
|
|
|
|
hideDelay: 3000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
vm.close = function() {
|
|
|
|
$mdDialog.hide();
|
|
|
|
};
|
|
|
|
|
|
|
|
function filterByExtension(item) {
|
|
|
|
var isTextFile = item.type.indexOf('text') === 0 ||
|
|
|
|
/\.(ics)$/.test(item.name);
|
|
|
|
|
|
|
|
if (!isTextFile)
|
|
|
|
$mdToast.show({
|
|
|
|
template: [
|
|
|
|
'<md-toast>',
|
|
|
|
' <md-icon class="md-warn md-hue-1">error_outline</md-icon>',
|
|
|
|
' <span>' + l('Select an iCalendar file (.ics).') + '</span>',
|
|
|
|
'</md-toast>'
|
|
|
|
].join(''),
|
|
|
|
position: 'top right',
|
|
|
|
hideDelay: 3000
|
|
|
|
});
|
|
|
|
|
|
|
|
return isTextFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function exportCalendar(calendar) {
|
|
|
|
window.location.href = ApplicationBaseURL + '/' + calendar.id + '.ics' + '/export';
|
|
|
|
}
|
|
|
|
|
2015-08-17 19:44:21 +02:00
|
|
|
function showLinks(calendar) {
|
2015-06-12 19:34:08 +02:00
|
|
|
$mdDialog.show({
|
|
|
|
parent: angular.element(document.body),
|
|
|
|
clickOutsideToClose: true,
|
|
|
|
escapeToClose: true,
|
2015-08-17 19:44:21 +02:00
|
|
|
templateUrl: calendar.id + '/links',
|
|
|
|
controller: LinksDialogController,
|
|
|
|
controllerAs: 'links',
|
2015-06-12 19:34:08 +02:00
|
|
|
locals: {
|
2015-08-17 19:44:21 +02:00
|
|
|
calendar: calendar
|
|
|
|
}
|
2015-06-12 19:34:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2015-08-17 19:44:21 +02:00
|
|
|
LinksDialogController.$inject = ['$mdDialog', 'calendar'];
|
|
|
|
function LinksDialogController($mdDialog, calendar) {
|
|
|
|
var vm = this;
|
|
|
|
vm.calendar = calendar;
|
|
|
|
vm.close = close;
|
|
|
|
|
|
|
|
function close() {
|
2015-06-12 19:34:08 +02:00
|
|
|
$mdDialog.hide();
|
2015-08-17 19:44:21 +02:00
|
|
|
}
|
2015-06-15 15:53:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showProperties(calendar) {
|
2015-10-15 03:05:17 +02:00
|
|
|
var color = calendar.color;
|
2015-06-15 15:53:10 +02:00
|
|
|
$mdDialog.show({
|
|
|
|
templateUrl: calendar.id + '/properties',
|
|
|
|
controller: PropertiesDialogController,
|
|
|
|
controllerAs: 'properties',
|
|
|
|
clickOutsideToClose: true,
|
|
|
|
escapeToClose: true,
|
|
|
|
locals: {
|
2015-10-15 03:05:17 +02:00
|
|
|
srcCalendar: calendar
|
2015-06-12 19:34:08 +02:00
|
|
|
}
|
2015-10-15 03:05:17 +02:00
|
|
|
}).catch(function() {
|
|
|
|
// Restore original color when cancelling or closing the dialog
|
|
|
|
calendar.color = color;
|
2015-06-15 15:53:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2015-10-15 21:10:37 +02:00
|
|
|
PropertiesDialogController.$inject = ['$scope', '$mdDialog', 'srcCalendar'];
|
|
|
|
function PropertiesDialogController($scope, $mdDialog, srcCalendar) {
|
2015-06-15 15:53:10 +02:00
|
|
|
var vm = this;
|
|
|
|
|
2015-10-15 03:05:17 +02:00
|
|
|
vm.calendar = new Calendar(srcCalendar.$omit());
|
2015-07-31 17:18:29 +02:00
|
|
|
vm.saveProperties = saveProperties;
|
|
|
|
vm.close = close;
|
2015-06-15 15:53:10 +02:00
|
|
|
|
2015-10-15 21:10:37 +02:00
|
|
|
$scope.$watch('properties.calendar.color', function() {
|
|
|
|
srcCalendar.color = vm.calendar.color;
|
|
|
|
});
|
2015-10-15 03:05:17 +02:00
|
|
|
|
2015-07-31 17:18:29 +02:00
|
|
|
function saveProperties() {
|
2015-06-15 15:53:10 +02:00
|
|
|
vm.calendar.$save();
|
2015-07-31 17:18:29 +02:00
|
|
|
// Refresh list instance
|
2015-10-15 03:05:17 +02:00
|
|
|
srcCalendar.init(vm.calendar.$omit());
|
2015-06-15 16:07:56 +02:00
|
|
|
$mdDialog.hide();
|
2015-07-31 17:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
2015-10-15 03:05:17 +02:00
|
|
|
$mdDialog.cancel();
|
2015-07-31 17:18:29 +02:00
|
|
|
}
|
2015-06-12 19:34:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 16:33:12 +02:00
|
|
|
function editFolder(folder) {
|
|
|
|
vm.calendarName = folder.name;
|
|
|
|
vm.editMode = folder.id;
|
|
|
|
focus('calendarName_' + folder.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function revertEditing(folder) {
|
|
|
|
folder.$reset();
|
|
|
|
vm.editMode = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renameFolder(folder) {
|
|
|
|
folder.$rename()
|
|
|
|
.then(function(data) {
|
|
|
|
vm.editMode = false;
|
|
|
|
}, function(data, status) {
|
|
|
|
Dialog.alert(l('Warning'), data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:06:13 +02:00
|
|
|
function share(calendar) {
|
2015-07-04 03:45:59 +02:00
|
|
|
calendar.$acl.$users().then(function() {
|
|
|
|
$mdDialog.show({
|
|
|
|
templateUrl: calendar.id + '/UIxAclEditor', // UI/Templates/UIxAclEditor.wox
|
2015-07-28 16:53:29 +02:00
|
|
|
controller: 'AclController', // from the ng module SOGo.Common
|
2015-07-04 03:45:59 +02:00
|
|
|
controllerAs: 'acl',
|
|
|
|
clickOutsideToClose: true,
|
|
|
|
escapeToClose: true,
|
|
|
|
locals: {
|
|
|
|
usersWithACL: calendar.$acl.users,
|
|
|
|
User: User,
|
|
|
|
folder: calendar
|
|
|
|
}
|
|
|
|
});
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
2015-07-24 22:14:53 +02:00
|
|
|
}
|
2015-05-06 04:06:13 +02:00
|
|
|
|
|
|
|
// Callback of sgSubscribe directive
|
|
|
|
function subscribeToFolder(calendarData) {
|
|
|
|
$log.debug('subscribeToFolder ' + calendarData.owner + calendarData.name);
|
|
|
|
Calendar.$subscribe(calendarData.owner, calendarData.name).catch(function(data) {
|
|
|
|
Dialog.alert(l('Warning'), l('An error occured please try again.'));
|
|
|
|
});
|
|
|
|
}
|
2015-09-22 16:17:17 +02:00
|
|
|
|
|
|
|
function today() {
|
|
|
|
var fragments = $window.location.hash.split('/'),
|
|
|
|
state = fragments[1],
|
|
|
|
view = fragments[2],
|
|
|
|
now = new Date(),
|
|
|
|
path = ['#', state, view, now.getDayString()];
|
|
|
|
$window.location = path.join('/');
|
|
|
|
}
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
angular
|
2015-06-02 17:14:03 +02:00
|
|
|
.module('SOGo.SchedulerUI')
|
|
|
|
.controller('CalendarsController', CalendarsController);
|
2015-05-06 04:06:13 +02:00
|
|
|
})();
|