2015-05-06 04:06:13 +02:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
2016-09-29 21:59:39 +02:00
|
|
|
/* jshint loopfunc: true */
|
2015-05-06 04:06:13 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngInject
|
|
|
|
*/
|
2016-09-27 21:19:24 +02:00
|
|
|
CalendarController.$inject = ['$scope', '$rootScope', '$state', '$stateParams', 'sgHotkeys', 'Calendar', 'Component', 'Preferences', 'stateEventsBlocks'];
|
|
|
|
function CalendarController($scope, $rootScope, $state, $stateParams, sgHotkeys, Calendar, Component, Preferences, stateEventsBlocks) {
|
|
|
|
var vm = this, deregisterCalendarsList, hotkeys = [];
|
2015-05-06 04:06:13 +02:00
|
|
|
|
2018-09-07 21:25:41 +02:00
|
|
|
this.$onInit = function() {
|
|
|
|
// Make the toolbar state of all-day events persistent
|
|
|
|
if (angular.isUndefined(CalendarController.expandedAllDays))
|
|
|
|
CalendarController.expandedAllDays = false;
|
2015-05-06 04:06:13 +02:00
|
|
|
|
2018-09-07 21:25:41 +02:00
|
|
|
this.selectedDate = $stateParams.day.asDate();
|
|
|
|
this.selectableDays = _.map(Preferences.defaults.SOGoCalendarWeekdays, function(day) {
|
|
|
|
return _.indexOf(['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'], day);
|
|
|
|
});
|
|
|
|
this.expandedAllDays = CalendarController.expandedAllDays;
|
|
|
|
this.views = stateEventsBlocks;
|
2016-09-27 21:19:24 +02:00
|
|
|
|
2017-06-01 19:46:57 +02:00
|
|
|
_registerHotkeys(hotkeys);
|
2016-09-27 21:19:24 +02:00
|
|
|
|
2018-09-07 21:25:41 +02:00
|
|
|
_formatDate(this.selectedDate);
|
2016-07-04 23:11:48 +02:00
|
|
|
|
2017-06-01 19:46:57 +02:00
|
|
|
// Refresh current view when the list of calendars is modified
|
2018-09-07 21:25:41 +02:00
|
|
|
deregisterCalendarsList = $rootScope.$on('calendars:list', _updateView);
|
2015-11-06 21:12:24 +01:00
|
|
|
|
2018-09-07 21:25:41 +02:00
|
|
|
// NOTE: $onDestroy won't work with ui-router (tested with v1.0.20).
|
2017-06-01 19:46:57 +02:00
|
|
|
$scope.$on('$destroy', function() {
|
|
|
|
// Destroy event listener when the controller is being deactivated
|
|
|
|
deregisterCalendarsList();
|
|
|
|
// Deregister hotkeys
|
|
|
|
_.forEach(hotkeys, function(key) {
|
|
|
|
sgHotkeys.deregisterHotkey(key);
|
|
|
|
});
|
2016-09-27 21:19:24 +02:00
|
|
|
});
|
2017-06-01 19:46:57 +02:00
|
|
|
};
|
2016-09-27 21:19:24 +02:00
|
|
|
|
|
|
|
function _registerHotkeys(keys) {
|
|
|
|
keys.push(sgHotkeys.createHotkey({
|
|
|
|
key: l('hotkey_today'),
|
|
|
|
description: l('Today'),
|
2018-09-07 21:25:41 +02:00
|
|
|
callback: vm.changeDate,
|
2016-09-27 21:19:24 +02:00
|
|
|
args: new Date()
|
|
|
|
}));
|
|
|
|
keys.push(sgHotkeys.createHotkey({
|
|
|
|
key: l('hotkey_dayview'),
|
|
|
|
description: l('Day'),
|
2018-09-07 21:25:41 +02:00
|
|
|
callback: vm.changeView,
|
2016-09-27 21:19:24 +02:00
|
|
|
args: 'day'
|
|
|
|
}));
|
|
|
|
keys.push(sgHotkeys.createHotkey({
|
|
|
|
key: l('hotkey_weekview'),
|
|
|
|
description: l('Week'),
|
2018-09-07 21:25:41 +02:00
|
|
|
callback: vm.changeView,
|
2016-09-27 21:19:24 +02:00
|
|
|
args: 'week'
|
|
|
|
}));
|
|
|
|
keys.push(sgHotkeys.createHotkey({
|
|
|
|
key: l('hotkey_monthview'),
|
|
|
|
description: l('Month'),
|
2018-09-07 21:25:41 +02:00
|
|
|
callback: vm.changeView,
|
2016-09-27 21:19:24 +02:00
|
|
|
args: 'month'
|
|
|
|
}));
|
|
|
|
keys.push(sgHotkeys.createHotkey({
|
|
|
|
key: l('hotkey_multicolumndayview'),
|
|
|
|
description: l('Multicolumn Day View'),
|
2018-09-07 21:25:41 +02:00
|
|
|
callback: vm.changeView,
|
2016-09-27 21:19:24 +02:00
|
|
|
args: 'multicolumnday'
|
|
|
|
}));
|
|
|
|
keys.push(sgHotkeys.createHotkey({
|
|
|
|
key: 'left',
|
|
|
|
description: l('Move backward'),
|
|
|
|
callback: _goToPeriod,
|
|
|
|
args: -1
|
|
|
|
}));
|
|
|
|
keys.push(sgHotkeys.createHotkey({
|
|
|
|
key: 'right',
|
|
|
|
description: l('Move forward'),
|
|
|
|
callback: _goToPeriod,
|
|
|
|
args: +1
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Register the hotkeys
|
|
|
|
_.forEach(keys, function(key) {
|
|
|
|
sgHotkeys.registerHotkey(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _goToPeriod($event, direction) {
|
|
|
|
var date;
|
|
|
|
|
|
|
|
if ($stateParams.view == 'week') {
|
|
|
|
date = vm.selectedDate.beginOfWeek(Preferences.defaults.SOGoFirstDayOfWeek).addDays(7 * direction);
|
|
|
|
}
|
|
|
|
else if ($stateParams.view == 'month') {
|
|
|
|
date = vm.selectedDate;
|
|
|
|
date.setDate(1);
|
|
|
|
date.setMonth(date.getMonth() + direction);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
date = vm.selectedDate.addDays(direction);
|
2018-09-07 21:25:41 +02:00
|
|
|
while (!vm.isSelectableDay(date)) {
|
|
|
|
date = date.addDays(direction);
|
|
|
|
}
|
2016-09-27 21:19:24 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 21:25:41 +02:00
|
|
|
vm.changeDate($event, date);
|
2016-09-27 21:19:24 +02:00
|
|
|
}
|
2015-11-06 21:12:24 +01:00
|
|
|
|
2016-10-12 17:01:59 +02:00
|
|
|
/**
|
|
|
|
* Format a date according to the current view.
|
|
|
|
* - Day/Multicolumn: name of weekday
|
|
|
|
* - Week: week number
|
|
|
|
* - Month: name of month
|
|
|
|
*/
|
2016-07-04 23:11:48 +02:00
|
|
|
function _formatDate(date) {
|
|
|
|
if ($stateParams.view == 'month') {
|
|
|
|
date.setDate(1);
|
|
|
|
date.setHours(12);
|
|
|
|
date.$dateFormat = '%B %Y';
|
|
|
|
}
|
|
|
|
else if ($stateParams.view == 'week') {
|
|
|
|
date.setTime(date.beginOfWeek(Preferences.defaults.SOGoFirstDayOfWeek).getTime());
|
|
|
|
date.$dateFormat = l('Week %d').replace('%d', '%U');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
date.$dateFormat = '%A';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 21:25:41 +02:00
|
|
|
function _updateView() {
|
2016-09-29 21:59:39 +02:00
|
|
|
// The list of calendars has changed; update the views
|
2015-09-29 20:08:05 +02:00
|
|
|
// See stateEventsBlocks in Scheduler.app.js
|
2015-05-06 04:06:13 +02:00
|
|
|
Component.$eventsBlocksForView($stateParams.view, $stateParams.day.asDate()).then(function(data) {
|
2016-09-29 21:59:39 +02:00
|
|
|
var i, j, view;
|
|
|
|
for (i = 0; i < data.length; i++) {
|
|
|
|
view = data[i];
|
|
|
|
if (vm.views[i]) {
|
|
|
|
_.forEach(view.allDayBlocks, function(blocks, day) {
|
|
|
|
vm.views[i].allDayBlocks[day] = blocks;
|
|
|
|
});
|
|
|
|
_.forEach(view.blocks, function(blocks, day) {
|
|
|
|
vm.views[i].blocks[day] = blocks;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm.views[i] = view;
|
|
|
|
}
|
2015-09-29 20:08:05 +02:00
|
|
|
if (view.id) {
|
2015-11-06 21:12:24 +01:00
|
|
|
// Note: this can't be done in Component service since it would make Component dependent on
|
|
|
|
// the Calendar service and create a circular dependency
|
2016-09-29 21:59:39 +02:00
|
|
|
vm.views[i].calendar = new Calendar({ id: view.id, name: view.calendarName });
|
2015-09-29 20:08:05 +02:00
|
|
|
}
|
2016-09-29 21:59:39 +02:00
|
|
|
}
|
|
|
|
// Remove previous views
|
|
|
|
for (j = vm.views.length; j >= i; j--)
|
|
|
|
vm.views.splice(j, 1);
|
2015-05-06 04:06:13 +02:00
|
|
|
});
|
2015-11-06 21:12:24 +01:00
|
|
|
}
|
2015-05-06 04:06:13 +02:00
|
|
|
|
2018-09-07 21:25:41 +02:00
|
|
|
// Expand or collapse all-day events
|
|
|
|
this.toggleAllDays = function() {
|
|
|
|
CalendarController.expandedAllDays = !CalendarController.expandedAllDays;
|
|
|
|
this.expandedAllDays = CalendarController.expandedAllDays;
|
|
|
|
};
|
|
|
|
|
2016-01-12 19:38:54 +01:00
|
|
|
// Change calendar's date
|
2018-09-07 21:25:41 +02:00
|
|
|
this.changeDate = function($event, newDate) {
|
2016-07-04 23:11:48 +02:00
|
|
|
var date = newDate? newDate.getDayString() : angular.element($event.currentTarget).attr('date');
|
|
|
|
if (newDate)
|
|
|
|
_formatDate(newDate);
|
2016-01-12 19:38:54 +01:00
|
|
|
$state.go('calendars.view', { day: date });
|
2018-09-07 21:25:41 +02:00
|
|
|
// $state.transitionTo('calendars.view', { day: date });
|
|
|
|
};
|
2016-01-12 19:38:54 +01:00
|
|
|
|
|
|
|
// Change calendar's view
|
2018-09-07 21:25:41 +02:00
|
|
|
this.changeView = function($event, view) {
|
2016-01-12 19:38:54 +01:00
|
|
|
$state.go('calendars.view', { view: view });
|
2018-09-07 21:25:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check if the week day should be visible/selectable
|
|
|
|
this.isSelectableDay = function(date) {
|
|
|
|
return _.includes(vm.selectableDays, date.getDay());
|
|
|
|
};
|
2016-01-12 19:38:54 +01:00
|
|
|
}
|
2015-09-29 20:08:05 +02:00
|
|
|
|
2015-05-06 04:06:13 +02:00
|
|
|
angular
|
|
|
|
.module('SOGo.SchedulerUI')
|
|
|
|
.controller('CalendarController', CalendarController);
|
|
|
|
})();
|