1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-12 23:09:03 +01:00
fab-manager/app/assets/javascripts/controllers/calendar.js

215 lines
8.0 KiB
JavaScript
Raw Normal View History

/* eslint-disable
no-return-assign,
no-undef,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2018-11-21 11:08:53 +01:00
'use strict';
/**
* Controller used in the public calendar global
*/
Application.Controllers.controller('CalendarController', ['$scope', '$state', '$aside', 'moment', 'Availability', 'Slot', 'Setting', 'growl', 'dialogs', 'bookingWindowStart', 'bookingWindowEnd', '_t', 'uiCalendarConfig', 'CalendarConfig', 'trainingsPromise', 'machinesPromise', 'spacesPromise',
function ($scope, $state, $aside, moment, Availability, Slot, Setting, growl, dialogs, bookingWindowStart, bookingWindowEnd, _t, uiCalendarConfig, CalendarConfig, trainingsPromise, machinesPromise, spacesPromise) {
/* PRIVATE STATIC CONSTANTS */
2018-11-21 11:08:53 +01:00
let currentMachineEvent = null;
machinesPromise.forEach(m => m.checked = true);
trainingsPromise.forEach(t => t.checked = true);
spacesPromise.forEach(s => s.checked = true);
// check all formation/machine is select in filter
2018-11-21 11:08:53 +01:00
const isSelectAll = (type, scope) => scope[type].length === scope[type].filter(t => t.checked).length;
/* PUBLIC SCOPE */
// List of trainings
2018-11-21 11:08:53 +01:00
$scope.trainings = trainingsPromise.filter(t => !t.disabled);
// List of machines
2018-11-21 11:08:53 +01:00
$scope.machines = machinesPromise.filter(t => !t.disabled);
// List of spaces
2018-11-21 11:08:53 +01:00
$scope.spaces = spacesPromise.filter(t => !t.disabled);
// add availabilities source to event sources
2018-11-21 11:08:53 +01:00
$scope.eventSources = [];
// filter availabilities if have change
$scope.filterAvailabilities = function (filter, scope) {
2018-11-21 11:08:53 +01:00
if (!scope) { scope = $scope; }
scope.filter = ($scope.filter = {
trainings: isSelectAll('trainings', scope),
machines: isSelectAll('machines', scope),
spaces: isSelectAll('spaces', scope),
evt: filter.evt,
dispo: filter.dispo
2018-11-21 11:08:53 +01:00
});
return $scope.calendarConfig.events = availabilitySourceUrl();
};
// a variable for formation/machine/event/dispo checkbox is or not checked
$scope.filter = {
trainings: isSelectAll('trainings', $scope),
machines: isSelectAll('machines', $scope),
spaces: isSelectAll('spaces', $scope),
evt: true,
dispo: true
2018-11-21 11:08:53 +01:00
};
// toggle to select all formation/machine
$scope.toggleFilter = function (type, filter) {
2018-11-21 11:08:53 +01:00
$scope[type].forEach(t => t.checked = filter[type]);
return $scope.filterAvailabilities(filter, $scope);
};
$scope.openFilterAside = () =>
$aside.open({
templateUrl: 'filterAside.html',
placement: 'right',
size: 'md',
backdrop: false,
resolve: {
trainings () {
2018-11-21 11:08:53 +01:00
return $scope.trainings;
},
machines () {
2018-11-21 11:08:53 +01:00
return $scope.machines;
},
spaces () {
2018-11-21 11:08:53 +01:00
return $scope.spaces;
},
filter () {
2018-11-21 11:08:53 +01:00
return $scope.filter;
},
toggleFilter () {
2018-11-21 11:08:53 +01:00
return $scope.toggleFilter;
},
filterAvailabilities () {
2018-11-21 11:08:53 +01:00
return $scope.filterAvailabilities;
}
},
controller: ['$scope', '$uibModalInstance', 'trainings', 'machines', 'spaces', 'filter', 'toggleFilter', 'filterAvailabilities', function ($scope, $uibModalInstance, trainings, machines, spaces, filter, toggleFilter, filterAvailabilities) {
2018-11-21 11:08:53 +01:00
$scope.trainings = trainings;
$scope.machines = machines;
$scope.spaces = spaces;
$scope.filter = filter;
2018-11-21 11:08:53 +01:00
$scope.toggleFilter = (type, filter) => toggleFilter(type, filter);
2018-11-21 11:08:53 +01:00
$scope.filterAvailabilities = filter => filterAvailabilities(filter, $scope);
return $scope.close = function (e) {
2018-11-21 11:08:53 +01:00
$uibModalInstance.dismiss();
return e.stopPropagation();
};
}
2018-11-21 11:08:53 +01:00
] });
/* PRIVATE SCOPE */
const calendarEventClickCb = function (event, jsEvent, view) {
// current calendar object
2018-11-21 11:08:53 +01:00
const { calendar } = uiCalendarConfig.calendars;
if (event.available_type === 'machines') {
2018-11-21 11:08:53 +01:00
currentMachineEvent = event;
calendar.fullCalendar('changeView', 'agendaDay');
return calendar.fullCalendar('gotoDate', event.start);
} else if (event.available_type === 'space') {
2018-11-21 11:08:53 +01:00
calendar.fullCalendar('changeView', 'agendaDay');
return calendar.fullCalendar('gotoDate', event.start);
} else if (event.available_type === 'event') {
2018-11-21 11:08:53 +01:00
return $state.go('app.public.events_show', { id: event.event_id });
} else if (event.available_type === 'training') {
2018-11-21 11:08:53 +01:00
return $state.go('app.public.training_show', { id: event.training_id });
} else {
if (event.machine_id) {
2018-11-21 11:08:53 +01:00
return $state.go('app.public.machines_show', { id: event.machine_id });
} else if (event.space_id) {
2018-11-21 11:08:53 +01:00
return $state.go('app.public.space_show', { id: event.space_id });
}
}
2018-11-21 11:08:53 +01:00
};
// agendaDay view: disable slotEventOverlap
// agendaWeek view: enable slotEventOverlap
const toggleSlotEventOverlap = function (view) {
// set defaultView, because when we change slotEventOverlap
// ui-calendar will trigger rerender calendar
2018-11-21 11:08:53 +01:00
$scope.calendarConfig.defaultView = view.type;
const today = currentMachineEvent ? currentMachineEvent.start : moment().utc().startOf('day');
if ((today > view.intervalStart) && (today < view.intervalEnd) && (today !== view.intervalStart)) {
2018-11-21 11:08:53 +01:00
$scope.calendarConfig.defaultDate = today;
} else {
2018-11-21 11:08:53 +01:00
$scope.calendarConfig.defaultDate = view.intervalStart;
}
if (view.type === 'agendaDay') {
2018-11-21 11:08:53 +01:00
return $scope.calendarConfig.slotEventOverlap = false;
} else {
2018-11-21 11:08:53 +01:00
return $scope.calendarConfig.slotEventOverlap = true;
}
2018-11-21 11:08:53 +01:00
};
// function is called when calendar view is rendered or changed
const viewRenderCb = function (view, element) {
2018-11-21 11:08:53 +01:00
toggleSlotEventOverlap(view);
if (view.type === 'agendaDay') {
// get availabilties by 1 day for show machine slots
2018-11-21 11:08:53 +01:00
return uiCalendarConfig.calendars.calendar.fullCalendar('refetchEvents');
}
2018-11-21 11:08:53 +01:00
};
const eventRenderCb = function (event, element) {
if (event.tags.length > 0) {
2018-11-21 11:08:53 +01:00
let html = '';
for (let tag of Array.from(event.tags)) {
2018-11-21 11:08:53 +01:00
html += `<span class='label label-success text-white'>${tag.name}</span> `;
}
2018-11-21 11:08:53 +01:00
element.find('.fc-title').append(`<br/>${html}`);
}
2018-11-21 11:08:53 +01:00
};
const getFilter = function () {
2018-11-21 11:08:53 +01:00
const t = $scope.trainings.filter(t => t.checked).map(t => t.id);
const m = $scope.machines.filter(m => m.checked).map(m => m.id);
const s = $scope.spaces.filter(s => s.checked).map(s => s.id);
return { t, m, s, evt: $scope.filter.evt, dispo: $scope.filter.dispo };
};
2018-11-21 11:08:53 +01:00
var availabilitySourceUrl = () => `/api/availabilities/public?${$.param(getFilter())}`;
const initialize = () =>
// fullCalendar (v2) configuration
$scope.calendarConfig = CalendarConfig({
events: availabilitySourceUrl(),
slotEventOverlap: true,
header: {
left: 'month agendaWeek agendaDay',
center: 'title',
right: 'today prev,next'
},
minTime: moment.duration(moment(bookingWindowStart.setting.value).format('HH:mm:ss')),
maxTime: moment.duration(moment(bookingWindowEnd.setting.value).format('HH:mm:ss')),
defaultView: window.innerWidth <= 480 ? 'agendaDay' : 'agendaWeek',
eventClick (event, jsEvent, view) {
2018-11-21 11:08:53 +01:00
return calendarEventClickCb(event, jsEvent, view);
},
viewRender (view, element) {
2018-11-21 11:08:53 +01:00
return viewRenderCb(view, element);
},
eventRender (event, element, view) {
2018-11-21 11:08:53 +01:00
return eventRenderCb(event, element);
}
2018-11-21 11:08:53 +01:00
});
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:08:53 +01:00
return initialize();
}
2018-11-21 11:08:53 +01:00
]);