document.observe("dom:loaded", function() { Global.Main=new Control.MainController(Global.event_id, Global.date); }); Control.MainController=Class.create({ initialize:function(event_id, today) { this.event_id=event_id; this.AddListener(); if (today=="") { $('Calendar').hide(); $('NoMoreDate').show(); } else { $('NoMoreDate').hide(); $('Calendar').show(); $('listejours').hide(); $('choixcreneaux').hide(); $('reserver').hide(); this.LoadCal(today); } }, AddListener: function() { $$('form').each(function(item){ item.observe('submit',this.SendResaRequest.bind(this)); }.bind(this)); $$('[role=lastmonth]').each(function(item){ item.observe('click',this.ChangeMonth.bind(this)); }.bind(this)); $$('[role=nextmonth]').each(function(item){ item.observe('click',this.ChangeMonth.bind(this)); }.bind(this)); }, ChangeMonth: function(ev) { el=ev.findElement('button'); if (el.readAttribute('role')=="lastmonth") { today=this.LastMonth; } if (el.readAttribute('role')=="nextmonth") { today=this.NextMonth; } $('NoMoreDate').hide(); $('Calendar').show(); $('listejours').hide(); $('choixcreneaux').hide(); $('reserver').hide(); this.LoadCal(today); }, SendResaRequest: function(ev) { form=ev.findElement('form'); form.down('button[type=submit]').disable(); ev.preventDefault(); data={} form.select('.form-control').each(function(item){ data[item.name]=item.getValue(); }.bind(this)); this.JSONRequest('api/calendar/reserving', data, this.PostReserving.bind(this)); }, PostReserving: function(result) { // Tester si la réservation a bien été prise en compte. header=result.responseJSON.header; if (header.error=="invalidform") { message=""; header.messages.each(function(msg) { message=message+msg; }); $$('button[type=submit]').each(function(item){ item.enable();}); alert(message); } if (header.error=="notavailaible") { alert("Ce créneau n'est plus disponible... Quelqu'un de plus rapide que vous a pris la dernière place."); document.location.reload(true); } if (header.error=="soonregister") { alert("Cette adresse mail est déjà enregistrée pour ce créneau horaire."); $$('button[type=submit]').each(function(item){ item.enable();}); } if (header.error=="soonregisteratday") { alert("Vous ne pouvez reserver qu'un seul créneau horaire par jour."); $$('button[type=submit]').each(function(item){ item.enable();}); } if (header.error=="none") { $('Calendar').hide(); $('Confirmation').show(); } }, LoadCal: function(today) { $('calendrier').update(); this.JSONRequest('gui/calendar/show', {date:today,event_id:this.event_id}, this.ShowCalendar.bind(this)); }, ShowCalendar: function(result) { $('listejours').show(); data=result.responseJSON.data; $('currentmonth').update(data.Mois+' '+data.Annee); this.NextMonth=data.Next; this.LastMonth=data.Last; tableau=new Element('table'); lignetitre=new Element('tr'); lignetitre.insert(new Element('th').update('L')); lignetitre.insert(new Element('th').update('M')); lignetitre.insert(new Element('th').update('M')); lignetitre.insert(new Element('th').update('J')); lignetitre.insert(new Element('th').update('V')); lignetitre.insert(new Element('th').update('S')); lignetitre.insert(new Element('th').update('D')); tableau.insert(lignetitre); data.Calendrier.each(function(item) { ligne=new Element('tr'); item.each(function(jour){ if (jour.notinmonth==1) { ligne.insert(new Element('td',{ class:'notinmonth'}).update(' ')); } else { if (jour.active==1) { classe='jourlibre'; } else { classe='jouroccupe'; } el=new Element('td',{role:'jourcalendrier',date:jour.date, class:classe}).update(jour.jour); if (jour.active==1) {el.observe('click',this.SelectDay.bind(this));} ligne.insert(el); } }.bind(this)); tableau.insert(ligne); }.bind(this)); $('calendrier').insert(tableau); }, SelectDay: function(ev) { el=ev.findElement('td'); src=ev.findElement('table'); src.select('td[role=jourcalendrier]').invoke('removeClassName','selected'); el.addClassName('selected'); today=el.readAttribute('date'); this.JSONRequest('api/calendar/getfreespace', {date:today,event_id:this.event_id}, this.ShowCreneaux.bind(this)); }, ShowCreneaux: function(result) { $('choixcreneaux').show(); $('reserver').hide(); $('listecreneaux').update(); result.responseJSON.data.each(function(item) { bloc=new Element('div',{class:'col-md-12 clickable btn btn-default btn-lg creneauunselected', role:'selectablereservation', idcreneau:item.id_creneau}); bloc.update(item.libelle+' ('+item.remain+' places restantes)'); bloc.observe('click',this.SelectCreneau.bind(this)); $('listecreneaux').insert(bloc); }.bind(this)); }, SelectCreneau: function(ev) { el=ev.findElement('[role=selectablereservation]'); $$('[role=selectablereservation]').each(function(item){ item.removeClassName('creneauunselected'); item.removeClassName('creneauselected'); }); el.addClassName('creneauselected'); $('idcreneau').setValue(el.readAttribute('idcreneau')); $('reserver').show(); $('reserver').down('button[type=submit]').enable(); }, JSONRequest: function(route,parameters,callback) { new Ajax.Request(route, { method: 'post', parameters: parameters, onSuccess: callback, onFailure: function(result) { alert('erreur de communication'); } }); }, });