diff --git a/app/assets/javascripts/controllers/calendar.coffee b/app/assets/javascripts/controllers/calendar.coffee index f8b2184ff..8c7e8ece3 100644 --- a/app/assets/javascripts/controllers/calendar.coffee +++ b/app/assets/javascripts/controllers/calendar.coffee @@ -4,52 +4,54 @@ # Controller used in the public calendar global ## -Application.Controllers.controller "CalendarController", ["$scope", "$state", "$uibModal", "moment", "Availability", 'Slot', 'Setting', 'growl', 'dialogs', 'bookingWindowStart', 'bookingWindowEnd', '_t', 'uiCalendarConfig', 'CalendarConfig' -($scope, $state, $uibModal, moment, Availability, Slot, Setting, growl, dialogs, bookingWindowStart, bookingWindowEnd, _t, uiCalendarConfig, CalendarConfig) -> +Application.Controllers.controller "CalendarController", ["$scope", "$state", "$uibModal", "moment", "Availability", 'Slot', 'Setting', 'growl', 'dialogs', 'bookingWindowStart', 'bookingWindowEnd', '_t', 'uiCalendarConfig', 'CalendarConfig', 'trainingsPromise', 'machinesPromise', +($scope, $state, $uibModal, moment, Availability, Slot, Setting, growl, dialogs, bookingWindowStart, bookingWindowEnd, _t, uiCalendarConfig, CalendarConfig, trainingsPromise, machinesPromise) -> ### PRIVATE STATIC CONSTANTS ### - availableTypes = ['machines', 'training', 'event'] - availabilitySource = - url: "/api/availabilities/public?#{$.param({available_type: availableTypes})}" - textColor: 'black' currentMachineEvent = null + machinesPromise.forEach((m) -> m.checked = true) + trainingsPromise.forEach((t) -> t.checked = true) ### PUBLIC SCOPE ### + ## List of trainings + $scope.trainings = trainingsPromise + + ## List of machines + $scope.machines = machinesPromise + + ## variable for filter event + $scope.evt = true + + ## variable for show/hidden slot no dispo + $scope.dispo = true + ## add availabilities source to event sources $scope.eventSources = [] - ## fullCalendar (v2) configuration - $scope.calendarConfig = CalendarConfig - events: availabilitySource.url - 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')) - eventClick: (event, jsEvent, view)-> - calendarEventClickCb(event, jsEvent, view) - viewRender: (view, element) -> - viewRenderCb(view, element) - eventRender: (event, element, view) -> - eventRenderCb(event, element) + ## filter availabilities if have change + $scope.filterAvailabilities = -> + $scope.filter = + trainings: $scope.isSelectAll('trainings') + machines: $scope.isSelectAll('machines') + $scope.calendarConfig.events = availabilitySourceUrl() - $scope.filterAvailableType = (type) -> - index = availableTypes.indexOf(type) - if index != -1 - availableTypes.splice(index, 1) - else - availableTypes.push(type) - availabilitySource.url = "/api/availabilities/public?#{$.param({available_type: availableTypes})}" - $scope.calendarConfig.events = availabilitySource.url + ## check all formation/machine is select in filter + $scope.isSelectAll = (type) -> + $scope[type].length == $scope[type].filter((t) -> t.checked).length + + ## a variable for formation/machine checkbox is or not checked + $scope.filter = + trainings: $scope.isSelectAll('trainings') + machines: $scope.isSelectAll('machines') + + ## toggle to select all formation/machine + $scope.toggleFilter = (type) -> + $scope[type].forEach((t) -> t.checked = $scope.filter[type]) + $scope.filterAvailabilities() - $scope.isAvailableTypeInactive = (type) -> - index = availableTypes.indexOf(type) - index == -1 ? true : false ### PRIVATE SCOPE ### @@ -98,4 +100,36 @@ Application.Controllers.controller "CalendarController", ["$scope", "$state", "$ html += "#{tag.name} " element.find('.fc-title').append("
"+html) return + + getFilter = -> + t = $scope.trainings.filter((t) -> t.checked).map((t) -> t.id) + m = $scope.machines.filter((m) -> m.checked).map((m) -> m.id) + {t: t, m: m, evt: $scope.evt, dispo: $scope.dispo} + + availabilitySourceUrl = -> + "/api/availabilities/public?#{$.param(getFilter())}" + + 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')) + eventClick: (event, jsEvent, view)-> + calendarEventClickCb(event, jsEvent, view) + viewRender: (view, element) -> + viewRenderCb(view, element) + eventRender: (event, element, view) -> + eventRenderCb(event, element) + + + + + ## !!! MUST BE CALLED AT THE END of the controller + initialize() ] diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index 2b9a36468..2342fa126 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -511,6 +511,12 @@ angular.module('application.router', ['ui.router']). bookingWindowEnd: ['Setting', (Setting)-> Setting.get(name: 'booking_window_end').$promise ] + trainingsPromise: ['Training', (Training)-> + Training.query().$promise + ] + machinesPromise: ['Machine', (Machine)-> + Machine.query().$promise + ] translations: [ 'Translations', (Translations) -> Translations.query(['app.public.calendar']).$promise ] diff --git a/app/assets/templates/calendar/calendar.html.erb b/app/assets/templates/calendar/calendar.html.erb index b7e3696e4..8f7364263 100644 --- a/app/assets/templates/calendar/calendar.html.erb +++ b/app/assets/templates/calendar/calendar.html.erb @@ -12,14 +12,10 @@
-
-
- - {{ 'filter' }} -
- {{ 'trainings' }} - {{ 'machines' }} - {{ 'events' }} +
+ {{ 'trainings' }} + {{ 'machines' }} + {{ 'events' }}
@@ -29,8 +25,46 @@
-
+
+
+
+
+

{{ 'filter' }}

+
+
+
+
+

{{ 'trainings' }}

+ +
+
+ {{::t.name}} + +
+
+
+
+

{{ 'machines' }}

+ +
+
+ {{::m.name}} + +
+
+
+

{{ 'events' }}

+ +
+
+

{{ 'show_no_disponible' }}

+ +
+
+
+
+
diff --git a/app/controllers/api/availabilities_controller.rb b/app/controllers/api/availabilities_controller.rb index f9e8c013f..088e98bb6 100644 --- a/app/controllers/api/availabilities_controller.rb +++ b/app/controllers/api/availabilities_controller.rb @@ -17,20 +17,21 @@ class API::AvailabilitiesController < API::ApiController def public start_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:start]) end_date = ActiveSupport::TimeZone[params[:timezone]].parse(params[:end]).end_of_day - available_type = params[:available_type] || [] @reservations = Reservation.includes(:slots, user: [:profile]).references(:slots, :user).where('slots.start_at >= ? AND slots.end_at <= ?', start_date, end_date) if in_same_day(start_date, end_date) - @training_and_event_availabilities = Availability.includes(:tags, :trainings, :event, :slots).where(available_type: available_type.dup.delete_if {|t| t == 'machines'}) + @training_and_event_availabilities = Availability.includes(:tags, :trainings, :event, :slots).where(available_type: ['training', 'event']) .where('start_at >= ? AND end_at <= ?', start_date, end_date) - @machine_availabilities = Availability.includes(:tags, :machines).where(available_type: available_type.dup.delete_if {|t| t == 'training' or t == 'event'}) + @machine_availabilities = Availability.includes(:tags, :machines).where(available_type: 'machines') .where('start_at >= ? AND end_at <= ?', start_date, end_date) @machine_slots = [] @machine_availabilities.each do |a| a.machines.each do |machine| - ((a.end_at - a.start_at)/SLOT_DURATION.minutes).to_i.times do |i| - slot = Slot.new(start_at: a.start_at + (i*SLOT_DURATION).minutes, end_at: a.start_at + (i*SLOT_DURATION).minutes + SLOT_DURATION.minutes, availability_id: a.id, availability: a, machine: machine, title: machine.name) - slot = verify_machine_is_reserved(slot, @reservations, current_user, '') - @machine_slots << slot + if params[:m] and params[:m].include?(machine.id.to_s) + ((a.end_at - a.start_at)/SLOT_DURATION.minutes).to_i.times do |i| + slot = Slot.new(start_at: a.start_at + (i*SLOT_DURATION).minutes, end_at: a.start_at + (i*SLOT_DURATION).minutes + SLOT_DURATION.minutes, availability_id: a.id, availability: a, machine: machine, title: machine.name) + slot = verify_machine_is_reserved(slot, @reservations, current_user, '') + @machine_slots << slot + end end end end @@ -38,12 +39,16 @@ class API::AvailabilitiesController < API::ApiController else @availabilities = Availability.includes(:tags, :machines, :trainings, :event, :slots) - .where(available_type: available_type) .where('start_at >= ? AND end_at <= ?', start_date, end_date) @availabilities.each do |a| - a = verify_training_event_is_reserved(a, @reservations) + if a.available_type != 'machines' + a = verify_training_event_is_reserved(a, @reservations) + end end end + machine_ids = params[:m] || [] + @title_filter = {machine_ids: machine_ids.map(&:to_i)} + @availabilities = filter_availabilites(@availabilities) end def show @@ -220,4 +225,36 @@ class API::AvailabilitiesController < API::ApiController def in_same_day(start_date, end_date) (end_date.to_date - start_date.to_date).to_i == 1 end + + def filter_availabilites(availabilities) + availabilities_filtered = [] + availabilities.to_ary.each do |a| + # machine slot + if !a.try(:available_type) + availabilities_filtered << a + else + # training + if params[:t] and a.available_type == 'training' + if params[:t].include?(a.trainings.first.id.to_s) + availabilities_filtered << a + end + end + # machines + if params[:m] and a.available_type == 'machines' + if (params[:m].map(&:to_i) & a.machine_ids).any? + availabilities_filtered << a + end + end + # event + if params[:evt] and params[:evt] == 'true' and a.available_type == 'event' + availabilities_filtered << a + end + end + end + availabilities_filtered.delete_if do |a| + if params[:dispo] == 'false' + a.is_reserved or (a.try(:is_completed) and a.is_completed) + end + end + end end diff --git a/app/models/availability.rb b/app/models/availability.rb index 37a59a47d..b3b5aded9 100644 --- a/app/models/availability.rb +++ b/app/models/availability.rb @@ -39,9 +39,12 @@ class Availability < ActiveRecord::Base end end - def title + def title(filter = {}) if available_type == 'machines' - machines.map(&:name).join(' - ') + if filter[:machine_ids] + return machines.to_ary.delete_if {|m| !filter[:machine_ids].include?(m.id)}.map(&:name).join(' - ') + end + return machines.map(&:name).join(' - ') elsif available_type == 'event' event.name else diff --git a/app/views/api/availabilities/public.json.jbuilder b/app/views/api/availabilities/public.json.jbuilder index df5ccbb2f..89e47cad8 100644 --- a/app/views/api/availabilities/public.json.jbuilder +++ b/app/views/api/availabilities/public.json.jbuilder @@ -1,12 +1,12 @@ json.array!(@availabilities) do |availability| json.id availability.id - json.title availability.title json.start availability.start_at.iso8601 json.end availability.end_at.iso8601 json.textColor 'black' json.backgroundColor 'white' # availability object if availability.try(:available_type) + json.title availability.title(@title_filter) if availability.available_type == 'event' json.event_id availability.event.id end @@ -35,6 +35,7 @@ json.array!(@availabilities) do |availability| # machine slot object ( here => availability = slot ) else + json.title availability.title json.machine_id availability.machine.id json.borderColor machines_slot_border_color(availability) json.tag_ids availability.availability.tag_ids @@ -42,5 +43,6 @@ json.array!(@availabilities) do |availability| json.id t.id json.name t.name end + json.is_reserved availability.is_reserved end end diff --git a/config/locales/app.public.en.yml b/config/locales/app.public.en.yml index 7f3780c48..e32e9c825 100644 --- a/config/locales/app.public.en.yml +++ b/config/locales/app.public.en.yml @@ -231,3 +231,4 @@ en: calendar: calendar: "Calendar" + show_no_disponible: "Show the slots no disponibles" diff --git a/config/locales/app.public.fr.yml b/config/locales/app.public.fr.yml index c23fd97a7..fbfb2a3c1 100644 --- a/config/locales/app.public.fr.yml +++ b/config/locales/app.public.fr.yml @@ -233,3 +233,4 @@ fr: calendar: calendar: "Calendrier" + show_no_disponible: "Afficher les crénaux non disponibles"