1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/assets/javascripts/controllers/trainings.coffee.erb

419 lines
14 KiB
Plaintext
Raw Normal View History

2016-03-23 18:39:41 +01:00
'use strict'
2016-07-13 18:15:14 +02:00
##
# Public listing of the trainings
##
Application.Controllers.controller "TrainingsController", ['$scope', '$state', 'trainingsPromise', ($scope, $state, trainingsPromise) ->
## List of trainings
$scope.trainings = trainingsPromise
##
# Callback for the 'reserve' button
##
$scope.reserveTraining = (training, event) ->
2016-09-27 16:00:22 +02:00
$state.go('app.logged.trainings_reserve', {id: training.slug})
2016-07-13 18:15:14 +02:00
##
# Callback for the 'show' button
##
$scope.showTraining = (training) ->
2016-09-27 16:00:22 +02:00
$state.go('app.public.training_show', {id: training.slug})
2016-07-13 18:15:14 +02:00
]
##
# Public view of a specific training
##
Application.Controllers.controller "ShowTrainingController", ['$scope', '$state', 'trainingPromise', 'growl', '_t', 'dialogs', ($scope, $state, trainingPromise, growl, _t, dialogs) ->
2016-07-13 18:15:14 +02:00
## Current training
$scope.training = trainingPromise
##
# Callback to delete the current training (admins only)
##
$scope.delete = (training) ->
# check the permissions
if $scope.currentUser.role isnt 'admin'
console.error _t('unauthorized_operation')
else
dialogs.confirm
resolve:
object: ->
title: _t('confirmation_required')
msg: _t('do_you_really_want_to_delete_this_training')
, -> # deletion confirmed
# delete the training then redirect to the trainings listing
training.$delete ->
$state.go('app.public.trainings_list')
, (error)->
growl.warning(_t('the_training_cant_be_deleted_because_it_is_already_reserved_by_some_users'))
2016-07-13 18:15:14 +02:00
##
# Callback for the 'reserve' button
##
$scope.reserveTraining = (training, event) ->
$state.go('app.logged.trainings_reserve', {id: training.id})
2016-07-13 18:15:14 +02:00
##
# Revert view to the full list of trainings ("<-" button)
##
$scope.cancel = (event) ->
$state.go('app.public.trainings_list')
]
2016-03-23 18:39:41 +01:00
##
# Controller used in the training reservation agenda page.
# This controller is very similar to the machine reservation controller with one major difference: here, ONLY ONE
# training can be reserved during the reservation process (the shopping cart may contains only one training and a subscription).
##
2017-02-23 13:46:16 +01:00
Application.Controllers.controller "ReserveTrainingController", ["$scope", '$stateParams', 'Auth', '$timeout', 'Availability', 'Member', 'availabilityTrainingsPromise', 'plansPromise', 'groupsPromise', 'settingsPromise', 'trainingPromise', '_t', 'uiCalendarConfig', 'CalendarConfig'
($scope, $stateParams, Auth, $timeout, Availability, Member, availabilityTrainingsPromise, plansPromise, groupsPromise, settingsPromise, trainingPromise, _t, uiCalendarConfig, CalendarConfig) ->
2016-03-23 18:39:41 +01:00
### PRIVATE STATIC CONSTANTS ###
# Color of the selected event backgound
SELECTED_EVENT_BG_COLOR = '#ffdd00'
2017-02-23 17:45:55 +01:00
# Slot free to be booked
FREE_SLOT_BORDER_COLOR = '<%= AvailabilityHelper::TRAINING_COLOR %>'
2016-03-23 18:39:41 +01:00
### PUBLIC SCOPE ###
## bind the trainings availabilities with full-Calendar events
$scope.eventSources = [ { events: availabilityTrainingsPromise, textColor: 'black' } ]
## the user to deal with, ie. the current user for non-admins
$scope.ctrl =
member: {}
## list of plans, classified by group
$scope.plansClassifiedByGroup = []
for group in groupsPromise
groupObj = { id: group.id, name: group.name, plans: [] }
for plan in plansPromise
groupObj.plans.push(plan) if plan.group_id == group.id
$scope.plansClassifiedByGroup.push(groupObj)
## 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: *}
## the moment when the slot selection changed for the last time, used to trigger changes in the cart
$scope.selectionTime = null
2016-03-23 18:39:41 +01:00
## the last clicked event in the calender
$scope.selectedEvent = null
2016-03-23 18:39:41 +01:00
## indicates the state of the current view : calendar or plans information
$scope.plansAreShown = false
2016-03-23 18:39:41 +01:00
## will store the user's plan if he choosed to buy one
$scope.selectedPlan = null
## the moment when the plan selection changed for the last time, used to trigger changes in the cart
$scope.planSelectionTime = null
2016-03-23 18:39:41 +01:00
2017-02-23 13:50:45 +01:00
## Selected training
2016-07-13 18:15:14 +02:00
$scope.training = trainingPromise
2017-02-23 13:50:45 +01:00
## 'all' OR training's slug
$scope.mode = $stateParams.id
2016-03-23 18:39:41 +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'))
2016-03-23 18:39:41 +01:00
eventClick: (event, jsEvent, view) ->
calendarEventClickCb(event, jsEvent, view)
eventRender: (event, element, view) ->
eventRenderCb(event, element, view)
## Application global settings
$scope.settings = settingsPromise
2016-03-23 18:39:41 +01:00
## Global config: message to the end user concerning the subscriptions rules
$scope.subscriptionExplicationsAlert = settingsPromise.subscription_explications_alert
2016-03-23 18:39:41 +01:00
## Global config: message to the end user concerning the training reservation
$scope.trainingExplicationsAlert = settingsPromise.training_explications_alert
2016-03-23 18:39:41 +01:00
## Global config: message to the end user giving advice about the training reservation
$scope.trainingInformationMessage = settingsPromise.training_information_message
2016-03-23 18:39:41 +01:00
##
# Change the last selected slot's appearence to looks like 'added to cart'
2016-03-23 18:39:41 +01:00
##
$scope.markSlotAsAdded = ->
$scope.selectedEvent.backgroundColor = SELECTED_EVENT_BG_COLOR
updateCalendar()
2016-03-23 18:39:41 +01:00
##
# Change the last selected slot's appearence to looks like 'never added to cart'
2016-03-23 18:39:41 +01:00
##
$scope.markSlotAsRemoved = (slot) ->
slot.backgroundColor = 'white'
slot.title = slot.training.name
slot.borderColor = FREE_SLOT_BORDER_COLOR
slot.id = null
slot.isValid = false
slot.is_reserved = false
slot.can_modify = false
slot.offered = false
slot.is_completed = false if slot.is_completed
updateCalendar()
2016-03-23 18:39:41 +01:00
##
# Callback when a slot was successfully cancelled. Reset the slot style as 'ready to book'
##
$scope.slotCancelled = ->
$scope.markSlotAsRemoved($scope.selectedEvent)
2016-03-23 18:39:41 +01:00
##
# Change the last selected slot's appearence to looks like 'currently looking for a new destination to exchange'
2016-03-23 18:39:41 +01:00
##
$scope.markSlotAsModifying = ->
$scope.selectedEvent.backgroundColor = '#eee'
$scope.selectedEvent.title = $scope.selectedEvent.training.name + ' - ' + _t('i_change')
updateCalendar()
2016-03-23 18:39:41 +01:00
##
# Change the last selected slot's appearence to looks like 'the slot being exchanged will take this place'
2016-03-23 18:39:41 +01:00
##
$scope.changeModifyTrainingSlot = ->
if $scope.events.placable
$scope.events.placable.backgroundColor = 'white'
$scope.events.placable.title = $scope.events.placable.training.name
if !$scope.events.placable or $scope.events.placable._id != $scope.selectedEvent._id
$scope.selectedEvent.backgroundColor = '#bbb'
$scope.selectedEvent.title = $scope.selectedEvent.training.name + ' - ' + _t('i_shift')
updateCalendar()
2016-03-23 18:39:41 +01:00
##
# When modifying an already booked reservation, callback when the modification was successfully done.
2016-03-23 18:39:41 +01:00
##
$scope.modifyTrainingSlot = ->
$scope.events.placable.title = if $scope.currentUser.role isnt 'admin' then $scope.events.placable.training.name + " - " + _t('i_ve_reserved') else $scope.events.placable.training.name
$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
2016-03-23 18:39:41 +01:00
$scope.events.modifiable.backgroundColor = 'white'
$scope.events.modifiable.title = $scope.events.modifiable.training.name
$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
$scope.events.modifiable.is_completed = false if $scope.events.modifiable.is_completed
2016-03-23 18:39:41 +01:00
updateCalendar()
2016-03-23 18:39:41 +01:00
##
# Cancel the current booking modification, reseting the whole process
##
$scope.cancelModifyTrainingSlot = ->
if $scope.events.placable
$scope.events.placable.backgroundColor = 'white'
$scope.events.placable.title = $scope.events.placable.training.name
$scope.events.modifiable.title = if $scope.currentUser.role isnt 'admin' then $scope.events.modifiable.training.name + " - " + _t('i_ve_reserved') else $scope.events.modifiable.training.name
$scope.events.modifiable.backgroundColor = 'white'
2016-03-23 18:39:41 +01:00
updateCalendar()
2016-03-23 18:39:41 +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)
2016-03-23 18:39:41 +01:00
##
$scope.updateMember = ->
if $scope.ctrl.member
Member.get {id: $scope.ctrl.member.id}, (member) ->
2016-03-23 18:39:41 +01:00
$scope.ctrl.member = member
id = if $stateParams.id is 'all' then $stateParams.id else $scope.training.id
Availability.trainings {trainingId: id, member_id: $scope.ctrl.member.id}, (trainings) ->
uiCalendarConfig.calendars.calendar.fullCalendar 'removeEvents'
$scope.eventSources.splice(0, 1,
events: trainings
textColor: 'black'
)
# as the events are re-fetched for the new user, we must re-init the cart
$scope.events.reserved = []
$scope.selectedPlan = null
$scope.plansAreShown = false
2016-08-10 16:53:40 +02:00
2016-03-23 18:39:41 +01:00
##
# Add the provided plan to the current shopping cart
# @param plan {Object} the plan to subscribe
2016-03-23 18:39:41 +01:00
##
$scope.selectPlan = (plan) ->
# toggle selected plan
if $scope.selectedPlan != plan
$scope.selectedPlan = plan
else
$scope.selectedPlan = null
2017-02-23 12:50:12 +01:00
$scope.planSelectionTime = new Date()
2016-03-23 18:39:41 +01:00
2016-08-10 16:53:40 +02: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-
2016-08-10 16:53:40 +02:00
##
$scope.doNotSubscribePlan = (e)->
e.preventDefault()
$scope.plansAreShown = false
$scope.selectedPlan = null
2017-02-23 12:50:12 +01:00
$scope.planSelectionTime = new Date()
2016-08-10 16:53:40 +02:00
2016-03-23 18:39:41 +01:00
##
# Switch the user's view from the reservation agenda to the plan subscription
2016-03-23 18:39:41 +01:00
##
$scope.showPlans = ->
$scope.plansAreShown = true
2016-03-23 18:39:41 +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 = (reservation)->
$scope.events.paid[0].backgroundColor = 'white'
$scope.events.paid[0].is_reserved = true
$scope.events.paid[0].can_modify = true
updateTrainingSlotId($scope.events.paid[0], reservation)
$scope.events.paid[0].borderColor = '#b2e774'
$scope.events.paid[0].title = $scope.events.paid[0].training.name + " - " + _t('i_ve_reserved')
2016-03-23 18:39:41 +01:00
if $scope.selectedPlan
$scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan)
Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan)
$scope.plansAreShown = false
$scope.selectedPlan = null
$scope.ctrl.member.training_credits = angular.copy(reservation.user.training_credits)
$scope.ctrl.member.machine_credits = angular.copy(reservation.user.machine_credits)
Auth._currentUser.training_credits = angular.copy(reservation.user.training_credits)
Auth._currentUser.machine_credits = angular.copy(reservation.user.machine_credits)
refetchCalendar()
2016-03-23 18:39:41 +01:00
### PRIVATE SCOPE ###
2016-03-23 18:39:41 +01:00
##
# Kind of constructor: these actions will be realized first when the controller is loaded
2016-03-23 18:39:41 +01:00
##
initialize = ->
if $scope.currentUser.role isnt 'admin'
Member.get id: $scope.currentUser.id, (member) ->
$scope.ctrl.member = member
2016-03-23 18:39:41 +01:00
##
# Triggered when the user clicks 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).
# @see http://fullcalendar.io/docs/mouse/eventClick/
2016-03-23 18:39:41 +01:00
##
calendarEventClickCb = (event, jsEvent, view) ->
$scope.selectedEvent = event
if $stateParams.id is 'all'
$scope.training = event.training
$scope.selectionTime = new Date()
2016-03-23 18:39:41 +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/
2016-03-23 18:39:41 +01:00
##
eventRenderCb = (event, element, view)->
if $scope.currentUser.role is 'admin' and event.tags.length > 0
html = ''
for tag in event.tags
html += "<span class='label label-success text-white' title='#{tag.name}'>#{tag.name}</span>"
element.find('.fc-time').append(html)
return
2016-03-23 18:39:41 +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.
# @param slot {Object}
# @param reservation {Object}
##
updateTrainingSlotId = (slot, reservation)->
angular.forEach reservation.slots, (s)->
if slot.start_at == slot.start_at
slot.id = s.id
2016-03-23 18:39:41 +01:00
##
# Update the calendar's display to render the new attributes of the events
##
updateCalendar = ->
uiCalendarConfig.calendars.calendar.fullCalendar 'rerenderEvents'
##
# Asynchronously fetch the events from the API and refresh the calendar's view with these new events
##
refetchCalendar = ->
$timeout ->
uiCalendarConfig.calendars.calendar.fullCalendar 'refetchEvents'
uiCalendarConfig.calendars.calendar.fullCalendar 'rerenderEvents'
## !!! MUST BE CALLED AT THE END of the controller
2016-03-23 18:39:41 +01:00
initialize()
]