Subversion Repositories eFlore/Applications.cel

Rev

Rev 3638 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3844 idir 1
import {WidgetsSaisiesASL} from './WidgetsSaisiesASL.js';
2
import {valOk} from './Utils.js';
3
 
3638 delphine 4
/**
5
 * Constructeur PlantesEtLichensASL par défaut
6
 * S'applique au squelette apaforms.tpl.html
7
 * Qui se charge dans apa.tpl.php
8
 * Lors de la saisie des plantes ou des lichens
9
 */
10
// ASL : APA, sTREETs, Lichen's Go!
3844 idir 11
export function PlantesEtLichensASL(plantesEtLichensProp) {
12
	if  ( valOk( plantesEtLichensProp ) && valOk( widgetProp ) ) {
13
		this.sujet             = plantesEtLichensProp.sujet;
14
		this.tagImg            = plantesEtLichensProp.tagImg;
15
		this.separationTagImg  = plantesEtLichensProp.separationTagImg;
16
		this.tagImg            = plantesEtLichensProp.tagImg;
17
		this.tagObs            = plantesEtLichensProp.tagObs;
18
		this.separationTagObs  = plantesEtLichensProp.separationTagObs;
19
		this.nomSciReferentiel = plantesEtLichensProp.nomSciReferentiel;
20
		this.referentielImpose = plantesEtLichensProp.referentielImpose;
3638 delphine 21
		this.tagsMotsCles      = widgetProp.tagsMotsCles + ',' + this.sujet;
22
	}
23
	this.isTaxonListe = false;
24
	this.numArbre     = 0;
25
}
3844 idir 26
PlantesEtLichensASL.prototype = new WidgetsSaisiesASL();
3638 delphine 27
 
28
/**
29
 * Initialise le formulaire, les validateurs, les listes de complétion...
30
 */
31
PlantesEtLichensASL.prototype.initForm = function() {
32
	$('[type="date"]').prop('max', function(){
33
		return new Date().toJSON().split('T')[0];
34
	});
35
	this.initFormTaxonListe();
36
	this.configurerFormValidator();
37
	this.definirReglesFormValidator();
38
};
39
 
40
/**
41
 * Initialise les écouteurs d'événements
42
 */
43
PlantesEtLichensASL.prototype.initEvts = function() {
3844 idir 44
	const idUtilisateur = $( '#id_utilisateur' ).val();
3638 delphine 45
 
3844 idir 46
	if( valOk( idUtilisateur ) ) {
3638 delphine 47
		// #releve-data est modifié, bouton dans #releves-utilisateur
48
		this.btnsChargerForm( '#bouton-nouveau-releve' );
3844 idir 49
		if( valOk( $( '#releve-data' ).val() ) ) {
3638 delphine 50
			this.releveDatas = $.parseJSON( $( '#releve-data' ).val() );
3844 idir 51
			if ( valOk( this.releveDatas[0].utilisateur, true, idUtilisateur ) ) {
3638 delphine 52
				// Sur téléchargement image
53
				this.initEvtsFichier();
54
				// Création / Suppression / Transmission des obs
55
				// Défilement des miniatures dans le résumé obs
56
				this.initEvtsObs();
3844 idir 57
 
3638 delphine 58
				// chargement plantes ou lichens, ajout du bouton #poursuivre
3844 idir 59
				const btnChargementForm = this.determinerBtnsChargementForm( '#', true );
60
 
3638 delphine 61
				// #releve-data n'est pas modifié, bouton dans #charger-form
62
				this.btnsChargerForm( btnChargementForm, false, false );
63
				if ( 'lichens' === this.sujet ) {
64
					this.checkboxToutesLesFaces();
65
				}
66
				// Alertes et aides
67
				this.initEvtsAlertes();
68
			}
69
		}
70
	}
71
};
72
 
73
// Ajouter Obs ****************************************************************/
74
PlantesEtLichensASL.prototype.reinitialiserForm = function() {
75
	this.supprimerMiniatures();
76
	$( '#taxon,#taxon-autre,#commentaire' ).val( '' );
77
	$( '#taxon' ).removeData([
78
		'value',
79
		'numNomSel',
80
		'nomRet',
81
		'numNomRet',
82
		'nt',
83
		'famille'
84
	]);
85
	$( '#taxon-liste,#certitude' ).find( 'option' ).each( function() {
86
		if ( $( this ).hasClass( 'choisir' ) ) {
87
			$( this ).attr( 'selected', true );
88
		} else {
89
			$( this ).attr( 'selected', false );
90
		}
91
	});
92
	$( '#taxon-input-groupe' ).addClass( 'hidden' );
93
	if ( 'lichens' === this.sujet ) {
94
		$( 'input[name=lichens-tronc]:checked' ).each( function() {
95
			$( this ).prop( 'checked', false );
96
		});
97
	}
98
};
99
 
100
PlantesEtLichensASL.prototype.checkboxToutesLesFaces = function() {
101
	$('input[name=lichens-tronc]').on( 'click', function( event ) {
3844 idir 102
		const face = $( this ).data( 'face' );
3638 delphine 103
 
104
		if ( $( this ).is( ':checked' ) ) {
105
			if( $( this ).hasClass( 'lichens-tronc-all' ) ) {
3844 idir 106
				for ( let i = 1; i <= 5 ; i++ ) {
3638 delphine 107
					$( '#lichens-tronc-' + face + i ).prop( 'checked', false );
108
				}
109
			} else {
110
				$( '#lichens-tronc-all-' + face ).prop( 'checked', false );
111
			}
112
		}
113
 
114
	});
115
};
116
 
117
/**
118
 * Retourne un Array contenant les valeurs des champs
119
 * dont les données seront transmises dans la table cel-obs-etendues
120
 */
121
PlantesEtLichensASL.prototype.getObsChpSpecifiques = function( numArbre ) {
3844 idir 122
	const retour = [
3638 delphine 123
		{ cle : 'num-arbre', valeur : numArbre },
124
		{ cle : 'id_obs_arbre', valeur : this.releveDatas[numArbre]['id_observation'] },
125
		{ cle : 'rue' , valeur : this.releveDatas[0].rue  }
126
	];
127
 
128
	if ( 'lichens' === this.sujet ) {
3844 idir 129
		const $lichensTronc = $( 'input[name=lichens-tronc]:checked' ),
130
			LTLenght = $lichensTronc.length;
131
		let valeursLT  = '';
3638 delphine 132
 
133
		$( 'input[name=lichens-tronc]:checked' ).each( function( i, value ) {
134
			valeursLT += $(value).val();
135
			if( i < LTLenght ) {
136
				valeursLT += ';';
137
			}
138
		});
139
		retour.push({ cle : 'loc-sur-tronc', valeur : valeursLT });
140
	}
141
 
142
	return retour;
143
};
144
 
145
// Form Validator *************************************************************/
146
PlantesEtLichensASL.prototype.definirReglesFormValidator = function() {
147
	const lthis = this;
148
 
149
	$( 'input[type=date]' ).on( 'input', function() {
150
		$( this ).valid();
151
	});
152
	// Validation Taxon si pas de miniature
153
	$( '#taxon' ).on( 'change', function() {
3844 idir 154
		const images = valOk( $( '#miniatures .miniature' ) );
155
 
156
		lthis.validerTaxonImage( valOk( $( this ).val() ), images );
3638 delphine 157
	});
158
 
159
	// // Validation miniatures avec MutationObserver
160
	// this.surPresenceAbsenceMiniature();
161
 
162
	$( '#form-' + this.sujet ).validate({
163
		rules : {
164
			'choisir-arbre' : {
165
				required : true,
166
				minlength : 1
167
			},
168
			'obs-date' : {
169
				required : true,
170
				'dateCel' : true
171
			},
172
			certitude : {
173
				required : true,
174
				minlength : 1
175
			}
176
		}
177
	});
178
	$( '#form-observateur' ).validate({
179
		rules : {
180
			courriel : {
181
				required : true,
182
				minlength : 1,
183
				email : true,
184
				'userEmailOk' : true
185
			},
186
			mdp : {
187
				required : true,
188
				minlength : 1
189
			}
190
		}
191
	});
192
	$( '#connexion,#inscription,#oublie' ).on( 'click', function() {
193
		$( '#tb-observateur .control-group' ).removeClass( 'error' );
194
	});
195
};
196
 
197
PlantesEtLichensASL.prototype.validerTaxonImage = function( taxon = false, images = false ) {
3844 idir 198
	const taxonOuImage = images || taxon;
199
 
200
	if ( taxonOuImage ) {
3638 delphine 201
		this.masquerPanneau( '#dialogue-taxon-or-image' );
202
		$( '#bloc-taxon' ).removeClass( 'error' )
203
			.find( 'span.error' ).hide();
204
		$( '#fichier' ).parent( 'label.label-file' ).removeClass( 'error' );
205
		$( '#photos-conteneur').removeClass( 'error' ).find( 'span.error' ).hide();
206
		// faire passer la certitude à 'à déterminer' si on a une image et pas de taxon
207
		if( !taxon ) {
208
			$( '#certitude' ).find( 'option' ).each( function() {
209
				if ( $( this ).hasClass( 'aDeterminer' ) ) {
210
					$( this ).attr( 'selected', true );
211
				} else {
212
					$( this ).attr( 'selected', false );
213
				}
214
			});
215
		}
216
	} else {
217
		this.afficherPanneau( '#dialogue-taxon-or-image' );
218
		$( '#bloc-taxon' ).addClass( 'error' )
219
			.find( 'span.error' ).show();
220
		$( '#fichier' ).parent( 'label.label-file' ).addClass( 'error' );
221
		$( '#photos-conteneur').addClass( 'error' ).find( 'span.error' ).show();
222
	}
3844 idir 223
 
224
	return taxonOuImage;
3638 delphine 225
};
226
 
227
/**
228
 * Valide le formulaire au click sur un bouton "suivant"
229
 */
230
PlantesEtLichensASL.prototype.validerForm = function() {
3844 idir 231
	const images     = valOk( $( '#miniatures .miniature' ) ),
232
		taxon        = valOk( $( '#taxon' ).val() ),
233
		taxonOuImage = this.validerTaxonImage( taxon, images ),
234
		observateur  = $( '#form-observateur' ).valid() && $( '#courriel' ).valid(),
235
		obs          = $( '#form-' + this.sujet ).valid();
3638 delphine 236
 
237
	// panneau observateur
238
	if ( observateur ) {
239
		this.masquerPanneau( '#dialogue-utilisateur-non-identifie' );
240
		$( '#tb-observateur .control-group' ).removeClass( 'error' );
241
	} else {
242
		this.afficherPanneau( '#dialogue-utilisateur-non-identifie' );
243
		$( '#tb-observateur .control-group' ).addClass( 'error' );
244
	}
245
 
246
	return ( observateur && obs && taxonOuImage );
247
};