2015-03-30 19:52:26 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Calendar
|
|
|
|
* @constructor
|
|
|
|
* @param {object} futureCalendarData - either an object literal or a promise
|
|
|
|
*/
|
|
|
|
function Calendar(futureCalendarData) {
|
|
|
|
// Data is immediately available
|
2015-04-24 21:08:55 +02:00
|
|
|
this.init(futureCalendarData);
|
2015-03-30 19:52:26 +02:00
|
|
|
if (this.name && !this.id) {
|
|
|
|
// Create a new calendar on the server
|
|
|
|
var newCalendarData = Calendar.$$resource.create('createFolder', this.name);
|
2015-11-11 22:42:46 +01:00
|
|
|
this.$unwrap(newCalendarData);
|
2015-03-30 19:52:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc The factory we'll use to register with Angular
|
|
|
|
* @returns the Calendar constructor
|
|
|
|
*/
|
2015-12-14 21:04:31 +01:00
|
|
|
Calendar.$factory = ['$q', '$timeout', '$log', 'sgSettings', 'Resource', 'Preferences', 'Component', 'Acl', function($q, $timeout, $log, Settings, Resource, Preferences, Component, Acl) {
|
2015-03-30 19:52:26 +02:00
|
|
|
angular.extend(Calendar, {
|
|
|
|
$q: $q,
|
|
|
|
$timeout: $timeout,
|
|
|
|
$log: $log,
|
2015-08-03 17:53:54 +02:00
|
|
|
$$resource: new Resource(Settings.activeUser('folderURL') + 'Calendar', Settings.activeUser()),
|
2015-12-14 21:04:31 +01:00
|
|
|
$Preferences: Preferences,
|
2015-04-24 21:08:55 +02:00
|
|
|
$Component: Component,
|
2015-03-30 19:52:26 +02:00
|
|
|
$$Acl: Acl,
|
2015-11-06 21:12:24 +01:00
|
|
|
activeUser: Settings.activeUser(),
|
|
|
|
$view: null
|
2015-03-30 19:52:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return Calendar; // return constructor
|
|
|
|
}];
|
|
|
|
|
2015-07-29 17:47:01 +02:00
|
|
|
/**
|
|
|
|
* @module SOGo.SchedulerUI
|
|
|
|
* @desc Factory registration of Calendar in Angular module.
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
angular.module('SOGo.SchedulerUI');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
angular.module('SOGo.SchedulerUI', ['SOGo.Common']);
|
|
|
|
}
|
2015-03-30 19:52:26 +02:00
|
|
|
angular.module('SOGo.SchedulerUI')
|
2015-11-06 21:12:24 +01:00
|
|
|
.value('CalendarSettings', {
|
2016-10-28 17:47:40 +02:00
|
|
|
EventDragDayLength: 24 * 4, // hour quarters
|
|
|
|
EventDragHorizontalOffset: 3, // pixels
|
|
|
|
ConflictHTTPErrorCode: 409
|
2015-11-06 21:12:24 +01:00
|
|
|
})
|
2015-05-06 04:06:13 +02:00
|
|
|
.factory('Calendar', Calendar.$factory);
|
2015-03-30 19:52:26 +02:00
|
|
|
|
2015-12-14 21:04:31 +01:00
|
|
|
/**
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc Return the default calendar id according to the user's defaults.
|
|
|
|
* @returns a calendar id
|
|
|
|
*/
|
|
|
|
Calendar.$defaultCalendar = function() {
|
|
|
|
var defaultCalendar = Calendar.$Preferences.defaults.SOGoDefaultCalendar,
|
|
|
|
calendar;
|
|
|
|
|
|
|
|
if (defaultCalendar == 'first') {
|
|
|
|
calendar = _.find(Calendar.$findAll(null, true), function(calendar) {
|
|
|
|
return calendar.active;
|
|
|
|
});
|
|
|
|
if (calendar)
|
|
|
|
return calendar.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'personal';
|
|
|
|
};
|
|
|
|
|
2015-03-30 19:52:26 +02:00
|
|
|
/**
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc Add a new calendar to the static list of calendars
|
|
|
|
* @param {Calendar} calendar - an Calendar object instance
|
|
|
|
*/
|
|
|
|
Calendar.$add = function(calendar) {
|
|
|
|
// Insert new calendar at proper index
|
2016-12-08 16:17:03 +01:00
|
|
|
var list, sibling;
|
2015-04-24 21:08:55 +02:00
|
|
|
|
2015-05-01 15:56:30 +02:00
|
|
|
if (calendar.isWebCalendar)
|
|
|
|
list = this.$webcalendars;
|
|
|
|
else if (calendar.isSubscription)
|
|
|
|
list = this.$subscriptions;
|
|
|
|
else
|
|
|
|
list = this.$calendars;
|
|
|
|
|
2016-12-08 16:17:03 +01:00
|
|
|
sibling = _.findIndex(list, function(o, i) {
|
2016-03-03 15:29:12 +01:00
|
|
|
return (calendar.id == 'personal' ||
|
2016-12-08 16:17:03 +01:00
|
|
|
(o.id != 'personal' && o.name.localeCompare(calendar.name) > 0));
|
2015-03-30 19:52:26 +02:00
|
|
|
});
|
2016-12-08 16:17:03 +01:00
|
|
|
if (sibling < 0)
|
|
|
|
list.push(calendar);
|
|
|
|
else
|
|
|
|
list.splice(sibling, 0, calendar);
|
2016-04-27 21:48:31 +02:00
|
|
|
|
2017-06-01 19:46:57 +02:00
|
|
|
if (Calendar.$Preferences.settings.Calendar.FoldersOrder)
|
|
|
|
// Save list order
|
|
|
|
Calendar.saveFoldersOrder(_.flatMap(Calendar.$findAll(), 'id'));
|
2016-11-08 17:44:10 +01:00
|
|
|
// Refresh list of calendars to fetch links associated to new calendar
|
|
|
|
Calendar.$reloadAll();
|
2015-03-30 19:52:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc Set or get the list of calendars. Will instanciate a new Calendar object for each item.
|
|
|
|
* @param {object[]} [data] - the metadata of the calendars
|
2015-12-10 17:11:22 +01:00
|
|
|
* @param {bool} [writable] - if true, returns only the list of writable calendars
|
2015-03-30 19:52:26 +02:00
|
|
|
* @returns the list of calendars
|
|
|
|
*/
|
2015-12-10 17:11:22 +01:00
|
|
|
Calendar.$findAll = function(data, writable) {
|
2015-03-30 19:52:26 +02:00
|
|
|
var _this = this;
|
|
|
|
if (data) {
|
2015-04-24 21:08:55 +02:00
|
|
|
this.$calendars = [];
|
|
|
|
this.$subscriptions = [];
|
2015-05-01 15:56:30 +02:00
|
|
|
this.$webcalendars = [];
|
2015-03-30 19:52:26 +02:00
|
|
|
// Instanciate Calendar objects
|
2015-04-24 21:08:55 +02:00
|
|
|
angular.forEach(data, function(o, i) {
|
|
|
|
var calendar = new Calendar(o);
|
2015-05-01 15:56:30 +02:00
|
|
|
if (calendar.isWebCalendar)
|
|
|
|
_this.$webcalendars.push(calendar);
|
|
|
|
else if (calendar.isSubscription)
|
2015-04-24 21:08:55 +02:00
|
|
|
_this.$subscriptions.push(calendar);
|
|
|
|
else
|
|
|
|
_this.$calendars.push(calendar);
|
2015-03-30 19:52:26 +02:00
|
|
|
});
|
|
|
|
}
|
2016-02-10 22:15:00 +01:00
|
|
|
else if (angular.isUndefined(this.$calendars)) {
|
|
|
|
this.$calendars = [];
|
|
|
|
this.$subscriptions = [];
|
|
|
|
this.$webcalendars = [];
|
2016-04-27 21:48:31 +02:00
|
|
|
return Calendar.$$resource.fetch('calendarslist').then(function(data) {
|
|
|
|
return Calendar.$findAll(data.calendars, writable);
|
2016-02-10 22:15:00 +01:00
|
|
|
});
|
|
|
|
}
|
2015-12-10 17:11:22 +01:00
|
|
|
|
|
|
|
if (writable) {
|
2016-03-10 15:18:39 +01:00
|
|
|
return _.union(this.$calendars, _.filter(this.$subscriptions, function(calendar) {
|
|
|
|
return calendar.isOwned || calendar.acls.objectCreator;
|
|
|
|
}));
|
2015-12-10 17:11:22 +01:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:38:01 +02:00
|
|
|
return _.union(this.$calendars, this.$subscriptions, this.$webcalendars);
|
2015-03-30 19:52:26 +02:00
|
|
|
};
|
|
|
|
|
2016-06-17 22:43:24 +02:00
|
|
|
/**
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc Reload the list of known calendars.
|
|
|
|
*/
|
|
|
|
Calendar.$reloadAll = function() {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
Calendar.$$resource.fetch('calendarslist').then(function(data) {
|
|
|
|
_.forEach(data.calendars, function(calendarData) {
|
|
|
|
var group, calendar;
|
|
|
|
|
|
|
|
if (calendarData.isWebCalendar)
|
|
|
|
group = _this.$webcalendars;
|
2016-11-08 17:44:10 +01:00
|
|
|
else if (calendarData.owner != Calendar.activeUser.login)
|
2016-06-17 22:43:24 +02:00
|
|
|
group = _this.$subscriptions;
|
|
|
|
else
|
|
|
|
group = _this.$calendars;
|
|
|
|
|
|
|
|
calendar = _.find(group, function(o) { return o.id == calendarData.id; });
|
|
|
|
if (calendar)
|
|
|
|
calendar.init(calendarData);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-24 21:08:55 +02:00
|
|
|
/**
|
|
|
|
* @memberof Calendar
|
2015-06-15 17:33:21 +02:00
|
|
|
* @desc Find a calendar among local instances (personal calendars, subscriptions and Web calendars).
|
2015-04-24 21:08:55 +02:00
|
|
|
* @param {string} id - the calendar ID
|
|
|
|
* @returns an object literal of the matching Calendar instance
|
|
|
|
*/
|
|
|
|
Calendar.$get = function(id) {
|
|
|
|
var calendar;
|
|
|
|
|
2015-07-24 22:14:53 +02:00
|
|
|
calendar = _.find(Calendar.$calendars, function(o) { return o.id == id; });
|
2015-04-24 21:08:55 +02:00
|
|
|
if (!calendar)
|
2015-07-24 22:14:53 +02:00
|
|
|
calendar = _.find(Calendar.$subscriptions, function(o) { return o.id == id; });
|
2015-05-01 15:56:30 +02:00
|
|
|
if (!calendar)
|
2015-07-24 22:14:53 +02:00
|
|
|
calendar = _.find(Calendar.$webcalendars, function(o) { return o.id == id; });
|
2015-04-24 21:08:55 +02:00
|
|
|
|
|
|
|
return calendar;
|
|
|
|
};
|
|
|
|
|
2015-09-17 16:33:12 +02:00
|
|
|
/**
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc Find a calendar among local instances (personal calendars, subscriptions and Web calendars).
|
|
|
|
* @param {string} id - the calendar ID
|
|
|
|
* @returns an object literal of the matching Calendar instance
|
|
|
|
*/
|
|
|
|
Calendar.$getIndex = function(id) {
|
|
|
|
var i;
|
|
|
|
|
2016-03-03 19:38:54 +01:00
|
|
|
i = _.indexOf(_.map(Calendar.$calendars, 'id'), id);
|
2015-09-17 16:33:12 +02:00
|
|
|
if (i < 0)
|
2016-03-03 19:38:54 +01:00
|
|
|
i = _.indexOf(_.map(Calendar.$subscriptions, 'id'), id);
|
2015-09-17 16:33:12 +02:00
|
|
|
if (i < 0)
|
2016-03-03 19:38:54 +01:00
|
|
|
i = _.indexOf(_.map(Calendar.$webcalendars, 'id'), id);
|
2015-09-17 16:33:12 +02:00
|
|
|
|
|
|
|
return i;
|
|
|
|
};
|
|
|
|
|
2015-03-30 19:52:26 +02:00
|
|
|
/**
|
|
|
|
* @memberOf Calendar
|
|
|
|
* @desc Subscribe to another user's calendar and add it to the list of calendars.
|
|
|
|
* @param {string} uid - user id
|
|
|
|
* @param {string} path - path of folder for specified user
|
|
|
|
* @returns a promise of the HTTP query result
|
|
|
|
*/
|
|
|
|
Calendar.$subscribe = function(uid, path) {
|
|
|
|
var _this = this;
|
|
|
|
return Calendar.$$resource.userResource(uid).fetch(path, 'subscribe').then(function(calendarData) {
|
2016-03-04 15:47:59 +01:00
|
|
|
var calendar = new Calendar(angular.extend({ active: 1 }, calendarData));
|
2015-04-24 21:08:55 +02:00
|
|
|
if (!_.find(_this.$subscriptions, function(o) {
|
2015-03-30 19:52:26 +02:00
|
|
|
return o.id == calendarData.id;
|
|
|
|
})) {
|
|
|
|
Calendar.$add(calendar);
|
|
|
|
}
|
|
|
|
return calendar;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-01 15:56:30 +02:00
|
|
|
/**
|
|
|
|
* @memberOf Calendar
|
|
|
|
* @desc Subscribe to a remote Web calendar
|
|
|
|
* @param {string} url - URL of .ics file
|
|
|
|
* @returns a promise of the HTTP query result
|
|
|
|
*/
|
|
|
|
Calendar.$addWebCalendar = function(url) {
|
|
|
|
var _this = this,
|
|
|
|
d = Calendar.$q.defer();
|
|
|
|
|
|
|
|
if (_.find(_this.$webcalendars, function(o) {
|
|
|
|
return o.urls.webCalendarURL == url;
|
|
|
|
})) {
|
|
|
|
// Already subscribed
|
|
|
|
d.reject();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Calendar.$$resource.post(null, 'addWebCalendar', { url: url }).then(function(calendarData) {
|
|
|
|
angular.extend(calendarData, {
|
|
|
|
isWebCalendar: true,
|
|
|
|
isEditable: true,
|
|
|
|
isRemote: false,
|
|
|
|
owner: Calendar.activeUser.login,
|
|
|
|
urls: { webCalendarURL: url }
|
|
|
|
});
|
|
|
|
var calendar = new Calendar(calendarData);
|
|
|
|
Calendar.$$resource.fetch(calendar.id, 'reload').then(function(data) {
|
|
|
|
// TODO: show a toast of the reload status
|
|
|
|
Calendar.$log.debug(JSON.stringify(data, undefined, 2));
|
2016-05-13 21:50:24 +02:00
|
|
|
Calendar.$add(calendar);
|
|
|
|
d.resolve();
|
|
|
|
}, function(response) {
|
|
|
|
if (response.status == 401) {
|
|
|
|
// Web calendar requires authentication
|
|
|
|
d.resolve(calendar);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d.reject();
|
|
|
|
}
|
2015-05-01 15:56:30 +02:00
|
|
|
});
|
2016-05-13 21:50:24 +02:00
|
|
|
}, d.reject);
|
2015-05-01 15:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return d.promise;
|
|
|
|
};
|
|
|
|
|
2016-05-13 21:50:24 +02:00
|
|
|
/**
|
|
|
|
* @function reloadWebCalendars
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc Reload all Web calendars
|
|
|
|
* @return a promise combining the results of all HTTP operations
|
|
|
|
*/
|
|
|
|
Calendar.reloadWebCalendars = function() {
|
|
|
|
var promises = [];
|
|
|
|
|
|
|
|
_.forEach(this.$webcalendars, function(calendar) {
|
|
|
|
var promise = Calendar.$$resource.fetch(calendar.id, 'reload');
|
|
|
|
promise.then(function(data) {
|
|
|
|
calendar.$error = false;
|
|
|
|
}, function(response) {
|
|
|
|
calendar.$error = l(response.statusText);
|
|
|
|
});
|
|
|
|
promises.push(promise);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Calendar.$q.all(promises);
|
|
|
|
};
|
|
|
|
|
2015-09-17 16:33:12 +02:00
|
|
|
/**
|
|
|
|
* @function $deleteComponents
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc Delete multiple components from calendar.
|
|
|
|
* @return a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Calendar.$deleteComponents = function(components) {
|
2016-02-11 15:33:58 +01:00
|
|
|
var _this = this, calendars = {}, promises = [];
|
2015-09-17 16:33:12 +02:00
|
|
|
|
|
|
|
_.forEach(components, function(component) {
|
2016-02-11 15:33:58 +01:00
|
|
|
if (!angular.isDefined(calendars[component.pid]))
|
|
|
|
calendars[component.pid] = [];
|
|
|
|
calendars[component.pid].push(component.id);
|
2015-09-17 16:33:12 +02:00
|
|
|
});
|
|
|
|
|
2016-02-11 15:33:58 +01:00
|
|
|
_.forEach(calendars, function(uids, pid) {
|
|
|
|
promises.push(Calendar.$$resource.post(pid, 'batchDelete', {uids: uids}));
|
2015-09-17 16:33:12 +02:00
|
|
|
});
|
|
|
|
|
2016-02-11 15:33:58 +01:00
|
|
|
return Calendar.$q.all(promises);
|
2015-09-17 16:33:12 +02:00
|
|
|
};
|
|
|
|
|
2017-01-06 12:41:38 +01:00
|
|
|
/**
|
|
|
|
* @function saveFoldersActivation
|
|
|
|
* @memberof Calendar
|
|
|
|
* @desc Save to the user's settings the activation state of the calendars
|
|
|
|
* @param {string[]} folders - the folders IDs
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Calendar.saveFoldersActivation = function(ids) {
|
|
|
|
var request = {};
|
|
|
|
|
|
|
|
_.forEach(ids, function(id) {
|
|
|
|
var calendar = Calendar.$get(id);
|
|
|
|
request[calendar.id] = calendar.active;
|
|
|
|
});
|
|
|
|
|
|
|
|
return Calendar.$$resource.post(null, 'saveFoldersActivation', request);
|
|
|
|
};
|
|
|
|
|
2016-04-27 21:48:31 +02:00
|
|
|
/**
|
|
|
|
* @function saveFoldersOrder
|
|
|
|
* @desc Save to the user's settings the current calendars order.
|
|
|
|
* @param {string[]} folders - the folders IDs
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Calendar.saveFoldersOrder = function(folders) {
|
|
|
|
return this.$$resource.post(null, 'saveFoldersOrder', { folders: folders }).then(function() {
|
|
|
|
Calendar.$Preferences.settings.Calendar.FoldersOrder = folders;
|
|
|
|
if (!folders)
|
|
|
|
// Calendars order was reset; reload list
|
|
|
|
return Calendar.$$resource.fetch('calendarslist').then(function(data) {
|
|
|
|
return Calendar.$findAll(data.calendars);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-24 21:08:55 +02:00
|
|
|
/**
|
|
|
|
* @function init
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Extend instance with new data and compute additional attributes.
|
|
|
|
* @param {object} data - attributes of calendar
|
|
|
|
*/
|
|
|
|
Calendar.prototype.init = function(data) {
|
2015-11-11 22:42:46 +01:00
|
|
|
this.color = this.color || '#AAAAAA';
|
2016-05-13 21:50:24 +02:00
|
|
|
this.active = 1;
|
2015-04-24 21:08:55 +02:00
|
|
|
angular.extend(this, data);
|
2016-01-14 22:41:58 +01:00
|
|
|
if (this.id) {
|
|
|
|
this.$acl = new Calendar.$$Acl('Calendar/' + this.id);
|
|
|
|
}
|
2015-04-24 21:08:55 +02:00
|
|
|
// Add 'isOwned' and 'isSubscription' attributes based on active user (TODO: add it server-side?)
|
|
|
|
this.isOwned = Calendar.activeUser.isSuperUser || this.owner == Calendar.activeUser.login;
|
|
|
|
this.isSubscription = !this.isRemote && this.owner != Calendar.activeUser.login;
|
2015-09-17 16:33:12 +02:00
|
|
|
if (angular.isUndefined(this.$shadowData)) {
|
|
|
|
// Make a copy of the data for an eventual reset
|
|
|
|
this.$shadowData = this.$omit();
|
|
|
|
}
|
2015-04-24 21:08:55 +02:00
|
|
|
};
|
|
|
|
|
2015-11-11 22:42:46 +01:00
|
|
|
/**
|
|
|
|
* @function $id
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Resolve the calendar id.
|
|
|
|
* @returns a promise of the calendar id
|
|
|
|
*/
|
|
|
|
Calendar.prototype.$id = function() {
|
|
|
|
if (this.id) {
|
|
|
|
// Object already unwrapped
|
|
|
|
return Calendar.$q.when(this.id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Wait until object is unwrapped
|
|
|
|
return this.$futureCalendarData.then(function(calendar) {
|
|
|
|
return calendar.id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-24 21:08:55 +02:00
|
|
|
/**
|
|
|
|
* @function getClassName
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Return the calendar CSS class name based on its ID.
|
|
|
|
* @returns a string representing the foreground CSS class name
|
|
|
|
*/
|
2015-04-29 17:23:21 +02:00
|
|
|
Calendar.prototype.getClassName = function(base) {
|
|
|
|
if (angular.isUndefined(base))
|
|
|
|
base = 'fg';
|
|
|
|
return base + '-folder' + this.id;
|
2015-04-24 21:08:55 +02:00
|
|
|
};
|
|
|
|
|
2015-03-30 19:52:26 +02:00
|
|
|
/**
|
|
|
|
* @function $rename
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Rename the calendar and keep the list sorted
|
|
|
|
* @param {string} name - the new name
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2015-09-17 16:33:12 +02:00
|
|
|
Calendar.prototype.$rename = function() {
|
|
|
|
var _this = this,
|
|
|
|
i,
|
|
|
|
calendars;
|
|
|
|
|
|
|
|
if (this.name == this.$shadowData.name) {
|
|
|
|
// Name hasn't changed
|
|
|
|
return Calendar.$q.when();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isWebCalendar)
|
|
|
|
calendars = Calendar.$webcalendars;
|
|
|
|
else if (this.isSubscription)
|
|
|
|
calendars = Calendar.$subscriptions;
|
|
|
|
else
|
|
|
|
calendars = Calendar.$calendars;
|
|
|
|
|
2016-03-03 19:38:54 +01:00
|
|
|
i = _.indexOf(_.map(calendars, 'id'), this.id);
|
2015-09-17 16:33:12 +02:00
|
|
|
if (i > -1) {
|
|
|
|
return this.$save().then(function() {
|
|
|
|
calendars.splice(i, 1);
|
|
|
|
Calendar.$add(_this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Calendar.$q.reject();
|
|
|
|
}
|
2015-03-30 19:52:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function $delete
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Delete the calendar from the server and the static list of calendars.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Calendar.prototype.$delete = function() {
|
|
|
|
var _this = this,
|
2015-05-01 15:56:30 +02:00
|
|
|
list,
|
2015-03-30 19:52:26 +02:00
|
|
|
promise;
|
|
|
|
|
2015-05-01 15:56:30 +02:00
|
|
|
if (this.isSubscription) {
|
2015-03-30 19:52:26 +02:00
|
|
|
promise = Calendar.$$resource.fetch(this.id, 'unsubscribe');
|
2015-05-01 15:56:30 +02:00
|
|
|
list = Calendar.$subscriptions;
|
|
|
|
}
|
|
|
|
else {
|
2015-03-30 19:52:26 +02:00
|
|
|
promise = Calendar.$$resource.remove(this.id);
|
2015-05-01 15:56:30 +02:00
|
|
|
if (this.isWebCalendar)
|
|
|
|
list = Calendar.$webcalendars;
|
|
|
|
else
|
|
|
|
list = Calendar.$calendars;
|
|
|
|
}
|
2015-03-30 19:52:26 +02:00
|
|
|
|
2015-09-17 16:33:12 +02:00
|
|
|
return promise.then(function() {
|
2016-03-03 19:38:54 +01:00
|
|
|
var i = _.indexOf(_.map(list, 'id'), _this.id);
|
2015-05-01 15:56:30 +02:00
|
|
|
list.splice(i, 1);
|
2015-03-30 19:52:26 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-12 22:11:38 +02:00
|
|
|
/**
|
2015-09-17 16:33:12 +02:00
|
|
|
* @function $reset
|
2016-06-01 03:56:10 +02:00
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Reset the original state the calendar's data.
|
2015-08-12 22:11:38 +02:00
|
|
|
*/
|
2015-09-17 16:33:12 +02:00
|
|
|
Calendar.prototype.$reset = function() {
|
|
|
|
var _this = this;
|
|
|
|
angular.forEach(this, function(value, key) {
|
|
|
|
if (key != 'constructor' && key[0] != '$') {
|
|
|
|
delete _this[key];
|
|
|
|
}
|
2015-08-12 22:11:38 +02:00
|
|
|
});
|
2015-09-17 16:33:12 +02:00
|
|
|
angular.extend(this, this.$shadowData);
|
|
|
|
this.$shadowData = this.$omit();
|
2015-08-12 22:11:38 +02:00
|
|
|
};
|
|
|
|
|
2015-03-30 19:52:26 +02:00
|
|
|
/**
|
|
|
|
* @function $save
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Save the calendar properties to the server.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Calendar.prototype.$save = function() {
|
2015-09-17 16:33:12 +02:00
|
|
|
var _this = this;
|
|
|
|
|
2015-03-30 19:52:26 +02:00
|
|
|
return Calendar.$$resource.save(this.id, this.$omit()).then(function(data) {
|
2015-09-17 16:33:12 +02:00
|
|
|
// Make a copy of the data for an eventual reset
|
|
|
|
_this.$shadowData = _this.$omit();
|
|
|
|
return data;
|
|
|
|
}, function(data) {
|
|
|
|
Calendar.$log.error(JSON.stringify(data, undefined, 2));
|
|
|
|
// Restore previous version
|
|
|
|
_this.$reset();
|
2015-03-30 19:52:26 +02:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-05-13 21:50:24 +02:00
|
|
|
/**
|
|
|
|
* @function setCredentials
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Set the credentials for a Web calendar that requires authentication
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Calendar.prototype.setCredentials = function(username, password) {
|
|
|
|
var _this = this,
|
|
|
|
d = Calendar.$q.defer();
|
|
|
|
|
|
|
|
Calendar.$$resource.post(this.id, 'set-credentials', { username: username, password: password }).then(function() {
|
|
|
|
Calendar.$$resource.fetch(_this.id, 'reload').then(function(data) {
|
|
|
|
Calendar.$add(_this);
|
|
|
|
d.resolve();
|
|
|
|
}, function(response) {
|
|
|
|
if (response.status == 401) {
|
|
|
|
// Authentication failed
|
|
|
|
d.reject(l('Wrong username or password'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d.reject(response.statusText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, d.reject);
|
|
|
|
|
|
|
|
return d.promise;
|
|
|
|
};
|
|
|
|
|
2016-05-04 20:52:06 +02:00
|
|
|
/**
|
|
|
|
* @function export
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Export the calendar
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Calendar.prototype.export = function() {
|
2016-12-07 20:37:59 +01:00
|
|
|
var options;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
type: 'application/octet-stream',
|
|
|
|
filename: this.name + '.ics'
|
|
|
|
};
|
|
|
|
|
|
|
|
return Calendar.$$resource.download(this.id + '.ics', 'export', null, options);
|
2016-05-04 20:52:06 +02:00
|
|
|
};
|
|
|
|
|
2015-03-30 19:52:26 +02:00
|
|
|
/**
|
|
|
|
* @function $setActivation
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Either activate or deactivate the calendar.
|
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
|
|
|
Calendar.prototype.$setActivation = function() {
|
|
|
|
return Calendar.$$resource.fetch(this.id, (this.active?'':'de') + 'activateFolder');
|
|
|
|
};
|
|
|
|
|
2015-04-24 21:08:55 +02:00
|
|
|
/**
|
|
|
|
* @function $getComponent
|
|
|
|
* @memberof Calendar.prototype
|
2015-06-15 17:33:21 +02:00
|
|
|
* @desc Fetch a component attributes from the server.
|
2015-04-24 21:08:55 +02:00
|
|
|
* @returns a promise of the HTTP operation
|
|
|
|
*/
|
2015-07-27 22:02:05 +02:00
|
|
|
Calendar.prototype.$getComponent = function(componentId, recurrenceId) {
|
|
|
|
return Calendar.$Component.$find(this.id, componentId, recurrenceId);
|
2015-04-24 21:08:55 +02:00
|
|
|
};
|
|
|
|
|
2015-11-11 22:42:46 +01:00
|
|
|
/**
|
|
|
|
* @function $unwrap
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Unwrap a promise
|
|
|
|
* @param {promise} futureCalendarData - a promise of the Calendar's data
|
|
|
|
*/
|
|
|
|
Calendar.prototype.$unwrap = function(futureCalendarData) {
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
// Expose and resolve the promise
|
|
|
|
this.$futureCalendarData = futureCalendarData.then(function(data) {
|
|
|
|
return Calendar.$timeout(function() {
|
|
|
|
// Extend Calendar instance with received data
|
|
|
|
_this.init(data);
|
|
|
|
return _this;
|
|
|
|
});
|
|
|
|
}, function(data) {
|
|
|
|
_this.isError = true;
|
|
|
|
if (angular.isObject(data)) {
|
|
|
|
Calendar.$timeout(function() {
|
|
|
|
angular.extend(_this, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-30 19:52:26 +02:00
|
|
|
/**
|
|
|
|
* @function $omit
|
|
|
|
* @memberof Calendar.prototype
|
|
|
|
* @desc Return a sanitized object used to send to the server.
|
|
|
|
* @return an object literal copy of the Calendar instance
|
|
|
|
*/
|
|
|
|
Calendar.prototype.$omit = function() {
|
|
|
|
var calendar = {};
|
|
|
|
angular.forEach(this, function(value, key) {
|
|
|
|
if (key != 'constructor' &&
|
|
|
|
key[0] != '$') {
|
|
|
|
calendar[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return calendar;
|
|
|
|
};
|
|
|
|
})();
|