1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/assets/javascripts/controllers/machines.js.erb

648 lines
24 KiB
Plaintext
Raw Normal View History

/* eslint-disable
handle-callback-err,
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
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2018-11-21 11:08:53 +01:00
'use strict';
/* COMMON CODE */
2018-11-19 16:17:49 +01:00
/**
* Provides a set of common callback methods to the $scope parameter. These methods are used
* in the various machines' admin controllers.
*
* Provides :
* - $scope.submited(content)
* - $scope.cancel()
* - $scope.fileinputClass(v)
* - $scope.addFile()
* - $scope.deleteFile(file)
*
* Requires :
* - $scope.machine.machine_files_attributes = []
* - $state (Ui-Router) [ 'app.public.machines_list' ]
2018-11-19 16:52:48 +01:00
*/
class MachinesController {
constructor ($scope, $state) {
2018-11-19 16:52:48 +01:00
/**
2018-11-19 16:17:49 +01:00
* For use with ngUpload (https://github.com/twilson63/ngUpload).
* Intended to be the callback when the upload is done: any raised error will be stacked in the
* $scope.alerts array. If everything goes fine, the user is redirected to the machines list.
* @param content {Object} JSON - The upload's result
*/
$scope.submited = function (content) {
if ((content.id == null)) {
2018-11-21 11:08:53 +01:00
$scope.alerts = [];
2018-11-19 16:52:48 +01:00
angular.forEach(content, function (v, k) {
angular.forEach(v, function (err) {
$scope.alerts.push({
msg: k + ': ' + err,
2015-05-05 03:10:25 +02:00
type: 'danger'
2018-11-21 11:08:53 +01:00
});
});
});
} else {
2018-11-21 11:08:53 +01:00
return $state.go('app.public.machines_list');
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Changes the current user's view, redirecting him to the machines list
*/
2018-11-21 11:08:53 +01:00
$scope.cancel = function () { $state.go('app.public.machines_list'); };
2018-11-19 16:17:49 +01:00
/**
* For use with 'ng-class', returns the CSS class name for the uploads previews.
* The preview may show a placeholder or the content of the file depending on the upload state.
* @param v {*} any attribute, will be tested for truthiness (see JS evaluation rules)
*/
$scope.fileinputClass = function (v) {
if (v) {
2018-11-21 11:08:53 +01:00
return 'fileinput-exists';
} else {
2018-11-21 11:08:53 +01:00
return 'fileinput-new';
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* This will create a single new empty entry into the machine attachements list.
*/
2018-11-21 11:08:53 +01:00
$scope.addFile = function () { $scope.machine.machine_files_attributes.push({}); };
2018-11-19 16:17:49 +01:00
/**
* This will remove the given file from the machine attachements list. If the file was previously uploaded
* to the server, it will be marked for deletion on the server. Otherwise, it will be simply truncated from
* the attachements array.
* @param file {Object} the file to delete
*/
$scope.deleteFile = function (file) {
2018-11-21 11:08:53 +01:00
const index = $scope.machine.machine_files_attributes.indexOf(file);
if (file.id != null) {
2018-11-21 11:08:53 +01:00
return file._destroy = true;
} else {
2018-11-21 11:08:53 +01:00
return $scope.machine.machine_files_attributes.splice(index, 1);
}
2018-11-21 11:08:53 +01:00
};
}
}
2018-11-19 16:17:49 +01:00
/**
* Manages the transition when a user clicks on the reservation button.
* According to the status of user currently logged into the system, redirect him to the reservation page,
* or display a modal window asking him to complete a training before he can book a machine reservation.
* @param machine {{id:number}} An object containg the id of the machine to book,
* the object will be completed before the fonction returns.
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
*/
const _reserveMachine = function (machine, e) {
2018-11-21 11:08:53 +01:00
const _this = this;
e.preventDefault();
e.stopPropagation();
// retrieve the full machine object
return machine = _this.Machine.get({ id: machine.id }, function () {
// if the currently logged'in user has completed the training for this machine, or this machine does not require
// a prior training, just redirect him to the machine's booking page
if (machine.current_user_is_training || (machine.trainings.length === 0)) {
2018-11-21 11:08:53 +01:00
return _this.$state.go('app.logged.machines_reserve', { id: machine.slug });
} else {
// otherwise, if a user is authenticated ...
if (_this.$scope.isAuthenticated()) {
// ... and have booked a training for this machine, tell him that he must wait for an admin to validate
// the training before he can book the reservation
if (machine.current_user_training_reservation) {
return _this.$uibModal.open({
templateUrl: '<%= asset_path "machines/training_reservation_modal.html" %>',
controller: ['$scope', '$uibModalInstance', function ($scope, $uibModalInstance) {
2018-11-21 11:08:53 +01:00
$scope.machine = machine;
return $scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
}]
});
// ... but does not have booked the training, tell him to register for a training session first
// unless all associated trainings are disabled
} else {
// if all trainings are disabled, just redirect the user to the reservation calendar
2018-11-21 11:08:53 +01:00
if (machine.trainings.map(function (t) { return t.disabled; }).reduce(function (acc, val) { return acc && val; }, true)) {
return _this.$state.go('app.logged.machines_reserve', { id: machine.slug });
// otherwise open the information modal
} else {
return _this.$uibModal.open({
templateUrl: '<%= asset_path "machines/request_training_modal.html" %>',
controller: ['$scope', '$uibModalInstance', '$state', function ($scope, $uibModalInstance, $state) {
2018-11-21 11:08:53 +01:00
$scope.machine = machine;
$scope.member = _this.$scope.currentUser;
// transform the name of the trainings associated with the machine to integrate them in a sentence
$scope.humanizeTrainings = function () {
2018-11-21 11:08:53 +01:00
let text = '';
angular.forEach($scope.machine.trainings, function (training) {
if (text.length > 0) {
2018-11-21 11:08:53 +01:00
text += _this._t('machines_list._or_the_');
}
2018-11-21 11:08:53 +01:00
return text += training.name.substr(0, 1).toLowerCase() + training.name.substr(1);
});
return text;
};
// modal is closed with validation
$scope.ok = function () {
2018-11-21 11:08:53 +01:00
$state.go('app.logged.trainings_reserve', { id: $scope.machine.trainings[0].id });
return $uibModalInstance.close(machine);
};
// modal is closed with escaping
return $scope.cancel = function (e) {
2018-11-21 11:08:53 +01:00
e.preventDefault();
return $uibModalInstance.dismiss('cancel');
};
}
2018-11-21 11:08:53 +01:00
] });
}
}
// if the user is not logged, open the login modal window
} else {
2018-11-21 11:08:53 +01:00
return _this.$scope.login();
}
}
2018-11-21 11:08:53 +01:00
});
};
2018-11-19 16:17:49 +01:00
/**
* Controller used in the public listing page, allowing everyone to see the list of machines
*/
Application.Controllers.controller('MachinesController', ['$scope', '$state', '_t', 'Machine', '$uibModal', 'machinesPromise',
function ($scope, $state, _t, Machine, $uibModal, machinesPromise) {
2018-11-21 10:59:07 +01:00
// Retrieve the list of machines
2018-11-21 11:08:53 +01:00
$scope.machines = machinesPromise;
2018-11-21 10:59:07 +01:00
/**
2018-11-19 16:17:49 +01:00
* Redirect the user to the machine details page
*/
2018-11-21 11:08:53 +01:00
$scope.showMachine = function (machine) { $state.go('app.public.machines_show', { id: machine.slug }); };
2018-11-21 10:59:07 +01:00
/**
2018-11-19 16:17:49 +01:00
* Callback to book a reservation for the current machine
*/
2018-11-21 10:59:07 +01:00
$scope.reserveMachine = _reserveMachine.bind({
$scope,
$state,
_t,
$uibModal,
Machine
2018-11-21 11:08:53 +01:00
});
2018-11-21 10:59:07 +01:00
// Default: we show only enabled machines
2018-11-21 11:08:53 +01:00
$scope.machineFiltering = 'enabled';
2018-11-21 10:59:07 +01:00
// Available options for filtering machines by status
return $scope.filterDisabled = [
'enabled',
'disabled',
'all'
2018-11-21 11:08:53 +01:00
];
2018-11-21 10:59:07 +01:00
}
2018-11-21 11:08:53 +01:00
]);
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
/**
* Controller used in the machine creation page (admin)
*/
Application.Controllers.controller('NewMachineController', ['$scope', '$state', 'CSRF', function ($scope, $state, CSRF) {
2018-11-21 11:08:53 +01:00
CSRF.setMetaTags();
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
// API URL where the form will be posted
2018-11-21 11:08:53 +01:00
$scope.actionUrl = '/api/machines/';
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
// Form action on the above URL
2018-11-21 11:08:53 +01:00
$scope.method = 'post';
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
// default machine parameters
2015-05-05 03:10:25 +02:00
$scope.machine =
2018-11-21 11:08:53 +01:00
{ machine_files_attributes: [] };
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
// Using the MachinesController
2018-11-21 11:08:53 +01:00
return new MachinesController($scope, $state);
}
2018-11-21 11:08:53 +01:00
]);
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
/**
* Controller used in the machine edition page (admin)
*/
Application.Controllers.controller('EditMachineController', ['$scope', '$state', '$stateParams', 'machinePromise', 'CSRF',
function ($scope, $state, $stateParams, machinePromise, CSRF) {
/* PUBLIC SCOPE */
2015-05-05 03:10:25 +02:00
2018-11-21 10:59:07 +01:00
// API URL where the form will be posted
2018-11-21 11:08:53 +01:00
$scope.actionUrl = `/api/machines/${$stateParams.id}`;
2015-05-05 03:10:25 +02:00
2018-11-21 10:59:07 +01:00
// Form action on the above URL
2018-11-21 11:08:53 +01:00
$scope.method = 'put';
2016-03-23 18:39:41 +01:00
2018-11-21 10:59:07 +01:00
// Retrieve the details for the machine id in the URL, if an error occurs redirect the user to the machines list
2018-11-21 11:08:53 +01:00
$scope.machine = machinePromise;
2016-03-23 18:39:41 +01:00
2018-11-21 10:59:07 +01:00
/* PRIVATE SCOPE */
2016-03-23 18:39:41 +01:00
2018-11-21 10:59:07 +01:00
/**
2018-11-19 16:17:49 +01:00
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
2018-11-21 10:59:07 +01:00
const initialize = function () {
2018-11-21 11:08:53 +01:00
CSRF.setMetaTags();
2016-03-23 18:39:41 +01:00
2018-11-21 10:59:07 +01:00
// Using the MachinesController
2018-11-21 11:08:53 +01:00
return new MachinesController($scope, $state);
};
2016-03-23 18:39:41 +01:00
2018-11-21 10:59:07 +01:00
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:08:53 +01:00
return initialize();
2018-11-21 10:59:07 +01:00
}
2018-11-21 11:08:53 +01:00
]);
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
/**
* Controller used in the machine details page (public)
*/
Application.Controllers.controller('ShowMachineController', ['$scope', '$state', '$uibModal', '$stateParams', '_t', 'Machine', 'growl', 'machinePromise', 'dialogs',
function ($scope, $state, $uibModal, $stateParams, _t, Machine, growl, machinePromise, dialogs) {
2018-11-19 16:17:49 +01:00
// Retrieve the details for the machine id in the URL, if an error occurs redirect the user to the machines list
2018-11-21 11:08:53 +01:00
$scope.machine = machinePromise;
2018-11-19 16:17:49 +01:00
/**
* Callback to delete the current machine (admins only)
*/
$scope.delete = function (machine) {
// check the permissions
if ($scope.currentUser.role !== 'admin') {
2018-11-21 11:08:53 +01:00
console.error(_t('unauthorized_operation'));
} else {
2018-11-19 16:52:48 +01:00
dialogs.confirm({
resolve: {
object () {
return {
title: _t('confirmation_required'),
msg: _t('do_you_really_want_to_delete_this_machine')
2018-11-21 11:08:53 +01:00
};
}
}
}
2018-11-21 10:59:07 +01:00
, function () { // deletion confirmed
// delete the machine then redirect to the machines listing
2018-11-19 16:52:48 +01:00
machine.$delete(
2018-11-21 11:08:53 +01:00
function () { $state.go('app.public.machines_list'); },
function (error) { growl.warning(_t('the_machine_cant_be_deleted_because_it_is_already_reserved_by_some_users')); console.error(error); }
);
});
}
2018-11-21 11:08:53 +01:00
};
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
/**
* Callback to book a reservation for the current machine
*/
return $scope.reserveMachine = _reserveMachine.bind({
$scope,
$state,
_t,
$uibModal,
Machine
2018-11-21 11:08:53 +01:00
});
}
2018-11-21 11:08:53 +01:00
]);
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
/**
* Controller used in the machine reservation page (for logged users who have completed the training and admins).
* This controller workflow is pretty similar to the trainings reservation controller.
*/
2016-03-23 18:39:41 +01:00
Application.Controllers.controller('ReserveMachineController', ['$scope', '$stateParams', '_t', 'moment', 'Auth', '$timeout', 'Member', 'Availability', 'plansPromise', 'groupsPromise', 'machinePromise', 'settingsPromise', 'uiCalendarConfig', 'CalendarConfig',
function ($scope, $stateParams, _t, moment, Auth, $timeout, Member, Availability, plansPromise, groupsPromise, machinePromise, settingsPromise, uiCalendarConfig, CalendarConfig) {
/* PRIVATE STATIC CONSTANTS */
2016-03-23 18:39:41 +01:00
// Slot free to be booked
2018-11-21 11:08:53 +01:00
const FREE_SLOT_BORDER_COLOR = '<%= AvailabilityHelper::MACHINE_COLOR %>';
2016-03-23 18:39:41 +01:00
// Slot already booked by another user
2018-11-21 11:08:53 +01:00
const UNAVAILABLE_SLOT_BORDER_COLOR = '<%= AvailabilityHelper::MACHINE_IS_RESERVED_BY_USER %>';
2016-03-23 18:39:41 +01:00
// Slot already booked by the current user
2018-11-21 11:08:53 +01:00
const BOOKED_SLOT_BORDER_COLOR = '<%= AvailabilityHelper::IS_RESERVED_BY_CURRENT_USER %>';
2016-03-23 18:39:41 +01:00
/* PUBLIC SCOPE */
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// bind the machine availabilities with full-Calendar events
2018-11-21 11:08:53 +01:00
$scope.eventSources = [];
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// indicates the state of the current view : calendar or plans information
2018-11-21 11:08:53 +01:00
$scope.plansAreShown = false;
2017-02-16 17:57:14 +01:00
2018-11-19 16:17:49 +01:00
// will store the user's plan if he choosed to buy one
2018-11-21 11:08:53 +01:00
$scope.selectedPlan = null;
2018-11-19 16:17:49 +01:00
// the moment when the plan selection changed for the last time, used to trigger changes in the cart
2018-11-21 11:08:53 +01:00
$scope.planSelectionTime = null;
2017-02-16 17:57:14 +01:00
2018-11-19 16:17:49 +01:00
// mapping of fullCalendar events.
$scope.events = {
reserved: [], // Slots that the user wants to book
modifiable: null, // Slot that the user wants to change
placable: null, // Destination slot for the change
paid: [], // Slots that were just booked by the user (transaction ok)
moved: null // Slots that were just moved by the user (change done) -> {newSlot:* oldSlot: *}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
// the moment when the slot selection changed for the last time, used to trigger changes in the cart
2018-11-21 11:08:53 +01:00
$scope.selectionTime = null;
2018-11-19 16:17:49 +01:00
// the last clicked event in the calender
2018-11-21 11:08:53 +01:00
$scope.selectedEvent = null;
2018-11-19 16:17:49 +01:00
// the application global settings
2018-11-21 11:08:53 +01:00
$scope.settings = settingsPromise;
2018-11-19 16:17:49 +01:00
// list of plans, classified by group
2018-11-21 11:08:53 +01:00
$scope.plansClassifiedByGroup = [];
for (let group of Array.from(groupsPromise)) {
2018-11-21 11:08:53 +01:00
const groupObj = { id: group.id, name: group.name, plans: [] };
for (let plan of Array.from(plansPromise)) {
2018-11-21 11:08:53 +01:00
if (plan.group_id === group.id) { groupObj.plans.push(plan); }
}
2018-11-21 11:08:53 +01:00
$scope.plansClassifiedByGroup.push(groupObj);
}
2018-11-19 16:17:49 +01:00
// the user to deal with, ie. the current user for non-admins
$scope.ctrl =
2018-11-21 11:08:53 +01:00
{ member: {} };
2018-11-19 16:17:49 +01:00
// current machine to reserve
2018-11-21 11:08:53 +01:00
$scope.machine = machinePromise;
2018-11-19 16:17:49 +01:00
// fullCalendar (v2) configuration
$scope.calendarConfig = CalendarConfig({
minTime: moment.duration(moment(settingsPromise.booking_window_start).format('HH:mm:ss')),
maxTime: moment.duration(moment(settingsPromise.booking_window_end).format('HH:mm:ss')),
eventClick (event, jsEvent, view) {
2018-11-21 11:08:53 +01:00
return calendarEventClickCb(event, jsEvent, view);
},
eventRender (event, element, view) {
2018-11-21 11:08:53 +01:00
return eventRenderCb(event, element);
}
2018-11-21 11:08:53 +01:00
});
2018-11-19 16:17:49 +01:00
// Global config: message to the end user concerning the subscriptions rules
2018-11-21 11:08:53 +01:00
$scope.subscriptionExplicationsAlert = settingsPromise.subscription_explications_alert;
2018-11-19 16:17:49 +01:00
// Global config: message to the end user concerning the machine bookings
2018-11-21 11:08:53 +01:00
$scope.machineExplicationsAlert = settingsPromise.machine_explications_alert;
2018-11-19 16:17:49 +01:00
/**
* Change the last selected slot's appearence to looks like 'added to cart'
*/
$scope.markSlotAsAdded = function () {
2018-11-21 11:08:53 +01:00
$scope.selectedEvent.backgroundColor = FREE_SLOT_BORDER_COLOR;
$scope.selectedEvent.title = _t('i_reserve');
return updateCalendar();
};
2018-11-19 16:17:49 +01:00
/**
* Change the last selected slot's appearence to looks like 'never added to cart'
*/
$scope.markSlotAsRemoved = function (slot) {
2018-11-21 11:08:53 +01:00
slot.backgroundColor = 'white';
slot.borderColor = FREE_SLOT_BORDER_COLOR;
slot.title = '';
slot.isValid = false;
slot.id = null;
slot.is_reserved = false;
slot.can_modify = false;
slot.offered = false;
return updateCalendar();
};
2018-11-19 16:17:49 +01:00
/**
* Callback when a slot was successfully cancelled. Reset the slot style as 'ready to book'
*/
2018-11-21 11:08:53 +01:00
$scope.slotCancelled = function () { $scope.markSlotAsRemoved($scope.selectedEvent); };
2018-11-19 16:17:49 +01:00
/**
* Change the last selected slot's appearence to looks like 'currently looking for a new destination to exchange'
*/
$scope.markSlotAsModifying = function () {
2018-11-21 11:08:53 +01:00
$scope.selectedEvent.backgroundColor = '#eee';
$scope.selectedEvent.title = _t('i_change');
return updateCalendar();
};
2018-11-19 16:17:49 +01:00
/**
* Change the last selected slot's appearence to looks like 'the slot being exchanged will take this place'
*/
$scope.changeModifyMachineSlot = function () {
if ($scope.events.placable) {
2018-11-21 11:08:53 +01:00
$scope.events.placable.backgroundColor = 'white';
$scope.events.placable.title = '';
}
if (!$scope.events.placable || ($scope.events.placable._id !== $scope.selectedEvent._id)) {
2018-11-21 11:08:53 +01:00
$scope.selectedEvent.backgroundColor = '#bbb';
$scope.selectedEvent.title = _t('i_shift');
}
2018-11-21 11:08:53 +01:00
return updateCalendar();
};
2018-11-19 16:17:49 +01:00
/**
* When modifying an already booked reservation, callback when the modification was successfully done.
*/
$scope.modifyMachineSlot = function () {
2018-11-21 11:08:53 +01:00
$scope.events.placable.title = $scope.currentUser.role !== 'admin' ? _t('i_ve_reserved') : _t('not_available');
$scope.events.placable.backgroundColor = 'white';
$scope.events.placable.borderColor = $scope.events.modifiable.borderColor;
$scope.events.placable.id = $scope.events.modifiable.id;
$scope.events.placable.is_reserved = true;
$scope.events.placable.can_modify = true;
$scope.events.modifiable.backgroundColor = 'white';
$scope.events.modifiable.title = '';
$scope.events.modifiable.borderColor = FREE_SLOT_BORDER_COLOR;
$scope.events.modifiable.id = null;
$scope.events.modifiable.is_reserved = false;
$scope.events.modifiable.can_modify = false;
return updateCalendar();
};
2018-11-19 16:17:49 +01:00
/**
* Cancel the current booking modification, reseting the whole process
*/
$scope.cancelModifyMachineSlot = function () {
if ($scope.events.placable) {
2018-11-21 11:08:53 +01:00
$scope.events.placable.backgroundColor = 'white';
$scope.events.placable.title = '';
}
2018-11-21 11:08:53 +01:00
$scope.events.modifiable.title = $scope.currentUser.role !== 'admin' ? _t('i_ve_reserved') : _t('not_available');
$scope.events.modifiable.backgroundColor = 'white';
2018-11-21 11:08:53 +01:00
return updateCalendar();
};
2018-11-19 16:17:49 +01:00
/**
* Callback to deal with the reservations of the user selected in the dropdown list instead of the current user's
* reservations. (admins only)
*/
$scope.updateMember = function () {
2018-11-21 11:08:53 +01:00
$scope.plansAreShown = false;
$scope.selectedPlan = null;
Member.get({ id: $scope.ctrl.member.id }, function (member) { $scope.ctrl.member = member; });
};
2018-11-19 16:17:49 +01:00
/**
* Changes the user current view from the plan subsription screen to the machine reservation agenda
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
*/
$scope.doNotSubscribePlan = function (e) {
2018-11-21 11:08:53 +01:00
e.preventDefault();
$scope.plansAreShown = false;
$scope.selectPlan($scope.selectedPlan);
return $scope.planSelectionTime = new Date();
};
2018-11-19 16:17:49 +01:00
/**
* Switch the user's view from the reservation agenda to the plan subscription
*/
2018-11-21 11:08:53 +01:00
$scope.showPlans = function () { $scope.plansAreShown = true; };
2018-11-19 16:17:49 +01:00
/**
* Add the provided plan to the current shopping cart
* @param plan {Object} the plan to subscribe
*/
$scope.selectPlan = function (plan) {
// toggle selected plan
if ($scope.selectedPlan !== plan) {
2018-11-21 11:08:53 +01:00
$scope.selectedPlan = plan;
} else {
2018-11-21 11:08:53 +01:00
$scope.selectedPlan = null;
}
2018-11-21 11:08:53 +01:00
return $scope.planSelectionTime = new Date();
};
2018-11-19 16:17:49 +01:00
/**
* Once the reservation is booked (payment process successfully completed), change the event style
* in fullCalendar, update the user's subscription and free-credits if needed
* @param reservation {Object}
*/
$scope.afterPayment = function (reservation) {
angular.forEach($scope.events.reserved, function (machineSlot, key) {
2018-11-21 11:08:53 +01:00
machineSlot.is_reserved = true;
machineSlot.can_modify = true;
if ($scope.currentUser.role !== 'admin') {
2018-11-21 11:08:53 +01:00
machineSlot.title = _t('i_ve_reserved');
machineSlot.borderColor = BOOKED_SLOT_BORDER_COLOR;
updateMachineSlot(machineSlot, reservation, $scope.currentUser);
} else {
2018-11-21 11:08:53 +01:00
machineSlot.title = _t('not_available');
machineSlot.borderColor = UNAVAILABLE_SLOT_BORDER_COLOR;
updateMachineSlot(machineSlot, reservation, $scope.ctrl.member);
}
2018-11-21 11:08:53 +01:00
return machineSlot.backgroundColor = 'white';
});
if ($scope.selectedPlan) {
2018-11-21 11:08:53 +01:00
$scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan);
Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan);
$scope.plansAreShown = false;
$scope.selectedPlan = null;
}
2018-11-21 11:08:53 +01:00
return refetchCalendar();
};
2018-11-19 16:17:49 +01:00
/**
* To use as callback in Array.prototype.filter to get only enabled plans
*/
2018-11-21 11:08:53 +01:00
$scope.filterDisabledPlans = function (plan) { return !plan.disabled; };
/* PRIVATE SCOPE */
2018-11-19 16:17:49 +01:00
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
2018-11-19 16:52:48 +01:00
Availability.machine({ machineId: $stateParams.id }, function (availabilities) {
$scope.eventSources.push({
events: availabilities,
textColor: 'black'
2018-11-21 11:08:53 +01:00
});
});
if ($scope.currentUser.role !== 'admin') {
2018-11-21 11:08:53 +01:00
return $scope.ctrl.member = $scope.currentUser;
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Triggered when the user click on a reservation slot in the agenda.
* Defines the behavior to adopt depending on the slot status (already booked, free, ready to be reserved ...),
* the user's subscription (current or about to be took) and the time (the user cannot modify a booked reservation
* if it's too late).
*/
var calendarEventClickCb = function (event, jsEvent, view) {
2018-11-21 11:08:53 +01:00
$scope.selectedEvent = event;
return $scope.selectionTime = new Date();
};
2018-11-19 16:17:49 +01:00
/**
* Triggered when fullCalendar tries to graphicaly render an event block.
* Append the event tag into the block, just after the event title.
* @see http://fullcalendar.io/docs/event_rendering/eventRender/
*/
var eventRenderCb = function (event, element) {
if (($scope.currentUser.role === 'admin') && (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' title='${tag.name}'>${tag.name}</span>`;
}
2018-11-21 11:08:53 +01:00
element.find('.fc-time').append(html);
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* After payment, update the id of the newly reserved slot with the id returned by the server.
* This will allow the user to modify the reservation he just booked. The associated user will also be registered
* with the slot.
* @param slot {Object}
* @param reservation {Object}
* @param user {Object} user associated with the slot
*/
2018-11-19 16:52:48 +01:00
var updateMachineSlot = function (slot, reservation, user) {
angular.forEach(reservation.slots, function (s) {
if (slot.start.isSame(s.start_at)) {
2018-11-21 11:08:53 +01:00
slot.id = s.id;
return slot.user = user;
}
2018-11-21 11:08:53 +01:00
});
};
2018-11-19 16:17:49 +01:00
/**
* Update the calendar's display to render the new attributes of the events
*/
2018-11-21 11:08:53 +01:00
var updateCalendar = function () { uiCalendarConfig.calendars.calendar.fullCalendar('rerenderEvents'); };
2018-11-19 16:17:49 +01:00
/**
* Asynchronously fetch the events from the API and refresh the calendar's view with these new events
*/
2018-11-19 16:52:48 +01:00
var refetchCalendar = function () {
$timeout(function () {
2018-11-21 11:08:53 +01:00
uiCalendarConfig.calendars.calendar.fullCalendar('refetchEvents');
return uiCalendarConfig.calendars.calendar.fullCalendar('rerenderEvents');
});
};
2018-11-19 16:17:49 +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
]);