/
CalendarListController.js
267 lines (239 loc) · 9.01 KB
/
CalendarListController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
(function() {
'use strict';
/**
* @ngInject
*/
CalendarListController.$inject = ['$rootScope', '$timeout', '$state', '$mdDialog', 'Dialog', 'Preferences', 'Calendar', 'Component'];
function CalendarListController($rootScope, $timeout, $state, $mdDialog, Dialog, Preferences, Calendar, Component) {
var vm = this;
vm.component = Component;
vm.componentType = 'events';
vm.selectedList = 0;
vm.selectComponentType = selectComponentType;
vm.unselectComponents = unselectComponents;
vm.selectAll = selectAll;
vm.toggleComponentSelection = toggleComponentSelection;
vm.confirmDeleteSelectedComponents = confirmDeleteSelectedComponents;
vm.openEvent = openEvent;
vm.openTask = openTask;
vm.newComponent = newComponent;
vm.filter = filter;
vm.filteredBy = filteredBy;
vm.sort = sort;
vm.sortedBy = sortedBy;
vm.reload = reload;
vm.cancelSearch = cancelSearch;
vm.mode = { search: false };
// Select list based on user's settings
Preferences.ready().then(function() {
var type = 'events';
if (Preferences.settings.Calendar.SelectedList == 'tasksListView') {
vm.selectedList = 1;
type = 'tasks';
}
selectComponentType(type, { reload: true });
});
// Refresh current list when the list of calendars is modified
$rootScope.$on('calendars:list', function() {
Component.$filter(vm.componentType, { reload: true });
});
// Update the component being dragged
$rootScope.$on('calendar:dragend', updateComponentFromGhost);
// Switch between components tabs
function selectComponentType(type, options) {
if (options && options.reload || vm.componentType != type) {
if (angular.isUndefined(Component['$' + type]))
Component.$filter(type);
vm.unselectComponents();
vm.componentType = type;
Component.saveSelectedList(type);
}
}
function unselectComponents() {
_.each(Component['$' + vm.componentType], function(component) { component.selected = false; });
}
function selectAll() {
_.each(Component['$' + vm.componentType], function(component) {
component.selected = true;
});
}
function toggleComponentSelection($event, component) {
component.selected = !component.selected;
$event.preventDefault();
$event.stopPropagation();
}
function confirmDeleteSelectedComponents() {
Dialog.confirm(l('Warning'),
l('Are you sure you want to delete the selected components?'),
{ ok: l('Delete') })
.then(function() {
// User confirmed the deletion
var components = _.filter(Component['$' + vm.componentType], function(component) {
return component.selected;
});
Calendar.$deleteComponents(components).then(function() {
$rootScope.$emit('calendars:list');
});
});
}
function openEvent($event, event) {
openComponent($event, event, 'appointment');
}
function openTask($event, task) {
openComponent($event, task, 'task');
}
function openComponent($event, component, type) {
if (component.viewable) {
// UI/Templates/SchedulerUI/UIxAppointmentViewTemplate.wox or
// UI/Templates/SchedulerUI/UIxTaskViewTemplate.wox
var templateUrl = 'UIx' + type.capitalize() + 'ViewTemplate';
$mdDialog.show({
parent: angular.element(document.body),
targetEvent: $event,
clickOutsideToClose: true,
escapeToClose: true,
templateUrl: templateUrl,
controller: 'ComponentController',
controllerAs: 'editor',
locals: {
stateComponent: component
}
});
}
}
function newComponent($event, baseComponent) {
var type = 'appointment', component;
if (baseComponent) {
component = baseComponent;
type = baseComponent.type;
}
else {
if (vm.componentType == 'tasks')
type = 'task';
component = new Component({ pid: Calendar.$defaultCalendar(), type: type });
}
// UI/Templates/SchedulerUI/UIxAppointmentEditorTemplate.wox or
// UI/Templates/SchedulerUI/UIxTaskEditorTemplate.wox
var templateUrl = 'UIx' + type.capitalize() + 'EditorTemplate';
return $mdDialog.show({
parent: angular.element(document.body),
targetEvent: $event,
clickOutsideToClose: true,
escapeToClose: true,
templateUrl: templateUrl,
controller: 'ComponentEditorController',
controllerAs: 'editor',
locals: {
stateComponent: component
}
});
}
// Adjust component or create new component through drag'n'drop
function updateComponentFromGhost($event) {
var component, pointerHandler, coordinates, delta, params, calendarNumber, activeCalendars;
component = Component.$ghost.component;
pointerHandler = Component.$ghost.pointerHandler;
if (component.isNew) {
coordinates = pointerHandler.currentEventCoordinates;
component.summary = '';
if (component.isAllDay)
coordinates.duration -= 96;
component.setDelta(coordinates.duration * 15);
newComponent(null, component).finally(function() {
$timeout(function() {
Component.$ghost.pointerHandler = null;
Component.$ghost.component = null;
});
});
}
else {
delta = pointerHandler.currentEventCoordinates.getDelta(pointerHandler.originalEventCoordinates);
params = {
days: delta.dayNumber,
start: delta.start * 15,
duration: delta.duration * 15
};
if (pointerHandler.originalCalendar && delta.dayNumber !== 0) {
// The day number actually represents the destination calendar among the active calendars
calendarNumber = pointerHandler.currentEventCoordinates.dayNumber;
activeCalendars = _.filter(Calendar.$findAll(), { active: 1 });
params.destination = activeCalendars[calendarNumber].id;
params.days = 0;
}
if (component.isException || !component.occurrenceId)
// Component is an exception to a recurrence or is not recurrent;
// Immediately perform the adjustments
component.$adjust(params).then(function() {
$rootScope.$emit('calendars:list');
$timeout(function() {
Component.$ghost = {};
});
});
else if (component.occurrenceId) {
$mdDialog.show({
clickOutsideToClose: true,
escapeToClose: true,
locals: {
component: component,
params: params
},
template: [
'<md-dialog flex="50" md-flex="80" sm-flex="90">',
' <md-dialog-content class="md-dialog-content">',
' <p>' + l('editRepeatingItem') + '</p>',
' </md-dialog-content>',
' <md-dialog-actions>',
' <md-button ng-click="updateThisOccurrence()">' + l('button_thisOccurrenceOnly') + '</md-button>',
' <md-button ng-click="updateAllOccurrences()">' + l('button_allOccurrences') + '</md-button>',
' </md-dialog-actions>',
'</md-dialog>'
].join(''),
controller: RecurrentComponentDialogController
}).then(function() {
$rootScope.$emit('calendars:list');
}).finally(function() {
$timeout(function() {
Component.$ghost = {};
});
});
}
}
/**
* @ngInject
*/
RecurrentComponentDialogController.$inject = ['$scope', '$mdDialog', 'component', 'params'];
function RecurrentComponentDialogController($scope, $mdDialog, component, params) {
$scope.updateThisOccurrence = function() {
component.$adjust(params).then($mdDialog.hide, $mdDialog.cancel);
};
$scope.updateAllOccurrences = function() {
delete component.occurrenceId;
component.$adjust(params).then($mdDialog.hide, $mdDialog.cancel);
};
}
}
function filter(filterpopup) {
Component.$filter(vm.componentType, { filterpopup: filterpopup });
}
function filteredBy(filterpopup) {
return Component['$query' + vm.componentType.capitalize()].filterpopup == filterpopup;
}
function sort(field) {
Component.$filter(vm.componentType, { sort: field });
}
function sortedBy(field) {
return Component['$query' + vm.componentType.capitalize()].sort == field;
}
function reload() {
$rootScope.$emit('calendars:list');
}
function cancelSearch() {
vm.mode.search = false;
Component.$filter(vm.componentType, { value: '' });
}
}
angular
.module('SOGo.SchedulerUI')
.controller('CalendarListController', CalendarListController);
})();