2015-05-06 04:06:13 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2017-09-01 18:35:38 +02:00
|
|
|
CalendarsController.$inject = ['$rootScope', '$scope', '$window', '$mdDialog', '$log', '$mdToast', 'Dialog', 'sgSettings', 'Preferences', 'Calendar'];
|
|
|
|
function CalendarsController($rootScope, $scope, $window, $mdDialog, $log, $mdToast, Dialog, Settings, Preferences, Calendar) {
|
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.subscribeToFolder = subscribeToFolder;
|
|
|
|
|
2016-04-27 21:48:31 +02:00
|
|
|
vm.filter = { name: '' };
|
2016-07-08 21:44:47 +02:00
|
|
|
vm.sortableMode = false;
|
2016-04-27 21:48:31 +02:00
|
|
|
vm.toggleSortableMode = toggleSortableMode;
|
|
|
|
vm.resetSort = resetSort;
|
|
|
|
vm.sortableCalendars = {
|
2016-07-08 21:44:47 +02:00
|
|
|
scrollableContainer: '#sidenav-content',
|
|
|
|
containment: 'md-list',
|
|
|
|
orderChanged: _sortableEnd,
|
|
|
|
accept: _sortableAccept
|
2016-04-27 21:48:31 +02:00
|
|
|
};
|
|
|
|
|
2017-06-01 19:46:57 +02:00
|
|
|
this.$onInit = function() {
|
2015-11-17 18:22:06 +01:00
|
|
|
vm.categories = _.map(Preferences.defaults.SOGoCalendarCategories, function(name) {
|
|
|
|
return { id: name.asCSSIdentifier(),
|
|
|
|
name: name,
|
|
|
|
color: Preferences.defaults.SOGoCalendarCategoriesColors[name]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-06-01 19:46:57 +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(
|
|
|
|
_.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']); })
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function(newList, oldList) {
|
|
|
|
var commonList, ids, promise;
|
2016-03-04 15:47:59 +01:00
|
|
|
|
2017-06-01 19:46:57 +02:00
|
|
|
// Identify which calendar has changed
|
|
|
|
commonList = _.intersectionBy(newList, oldList, 'id');
|
|
|
|
ids = _.map(_.filter(commonList, function(o) {
|
|
|
|
var oldObject = _.find(oldList, { id: o.id });
|
|
|
|
return !_.isEqual(o, oldObject);
|
|
|
|
}), 'id');
|
|
|
|
promise = Calendar.$q.when();
|
2016-03-04 15:47:59 +01:00
|
|
|
|
2017-06-01 19:46:57 +02:00
|
|
|
if (ids.length > 0) {
|
|
|
|
$log.debug(ids.join(', ') + ' changed');
|
|
|
|
promise = Calendar.saveFoldersActivation(ids);
|
|
|
|
}
|
|
|
|
if (ids.length > 0 || commonList.length != newList.length || commonList.length != oldList.length)
|
|
|
|
promise.then(function() {
|
|
|
|
$rootScope.$emit('calendars:list');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
true // compare for object equality
|
|
|
|
);
|
|
|
|
};
|
2015-05-06 04:06:13 +02:00
|
|
|
|
2016-07-08 21:44:47 +02:00
|
|
|
/**
|
|
|
|
* Only allow to sort items within the same list.
|
|
|
|
*/
|
|
|
|
function _sortableAccept(sourceItemHandleScope, destSortableScope, destItemScope) {
|
|
|
|
return sourceItemHandleScope.sortableScope.element[0] == destSortableScope.element[0];
|
2016-04-27 21:48:31 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 21:44:47 +02:00
|
|
|
function _sortableEnd() {
|
2016-04-27 21:48:31 +02:00
|
|
|
Calendar.saveFoldersOrder(_.flatMap(Calendar.$findAll(), 'id'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleSortableMode() {
|
2016-07-08 21:44:47 +02:00
|
|
|
vm.sortableMode = !vm.sortableMode;
|
2016-04-27 21:48:31 +02:00
|
|
|
vm.filter.name = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetSort() {
|
|
|
|
Calendar.saveFoldersOrder();
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:06:13 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
);
|
2015-11-11 22:42:46 +01:00
|
|
|
calendar.$id().then(function() {
|
|
|
|
Calendar.$add(calendar);
|
|
|
|
});
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addWebCalendar() {
|
|
|
|
Dialog.prompt(l('Subscribe to a web calendar...'), l('URL of the Calendar'), {inputType: 'url'})
|
|
|
|
.then(function(url) {
|
2016-05-13 21:50:24 +02:00
|
|
|
Calendar.$addWebCalendar(url).then(function(calendar) {
|
|
|
|
if (angular.isObject(calendar)) {
|
|
|
|
// Web calendar requires HTTP authentication
|
|
|
|
$mdDialog.show({
|
|
|
|
parent: angular.element(document.body),
|
|
|
|
clickOutsideToClose: true,
|
|
|
|
escapeToClose: true,
|
|
|
|
templateUrl: 'UIxWebCalendarAuthDialog',
|
|
|
|
controller: WebCalendarAuthDialogController,
|
|
|
|
controllerAs: '$WebCalendarAuthDialogController',
|
|
|
|
locals: {
|
|
|
|
url: url,
|
|
|
|
calendar: calendar
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
2016-05-13 21:50:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
|
|
|
WebCalendarAuthDialogController.$inject = ['scope', '$mdDialog', 'url', 'calendar'];
|
|
|
|
function WebCalendarAuthDialogController(scope, $mdDialog, url, calendar) {
|
|
|
|
var vm = this,
|
|
|
|
parts = url.split("/"),
|
|
|
|
hostname = parts[2];
|
|
|
|
|
|
|
|
vm.title = l("Please identify yourself to %{0}").formatted(hostname);
|
|
|
|
vm.authenticate = function(form) {
|
|
|
|
if (form.$valid || !form.$error.required) {
|
|
|
|
calendar.setCredentials(vm.username, vm.password).then(function(message) {
|
|
|
|
$mdDialog.hide();
|
|
|
|
}, function(reason) {
|
|
|
|
form.password.$setValidity('credentials', false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
vm.cancel = function() {
|
|
|
|
$mdDialog.cancel();
|
|
|
|
};
|
|
|
|
}
|
2015-05-06 04:06:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Callback of sgSubscribe directive
|
|
|
|
function subscribeToFolder(calendarData) {
|
|
|
|
$log.debug('subscribeToFolder ' + calendarData.owner + calendarData.name);
|
2015-12-10 19:24:31 +01:00
|
|
|
Calendar.$subscribe(calendarData.owner, calendarData.name).then(function(data) {
|
|
|
|
$mdToast.show(
|
|
|
|
$mdToast.simple()
|
2016-01-13 18:48:20 +01:00
|
|
|
.content(l('Successfully subscribed to calendar'))
|
2015-12-10 19:24:31 +01:00
|
|
|
.position('top right')
|
|
|
|
.hideDelay(3000));
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
|
|
|
}
|
2015-09-22 16:17:17 +02:00
|
|
|
|
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
|
|
|
})();
|