Subversion Repositories eFlore/Applications.cel

Rev

Rev 3881 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3881 Rev 3901
Line 1... Line 1...
1
import {WidgetsSaisiesCommun,utils} from './WidgetsSaisiesCommun.js';
1
import {WidgetsSaisiesCommun,utils} from './WidgetsSaisiesCommun.js';
2
import {valOk} from './Utils.js';
2
import {valOk,tryParseJson} from './Utils.js';
Line 3... Line 3...
3
 
3
 
4
/**
4
/**
5
 * Constructeur WidgetSaisie par défaut
5
 * Constructeur WidgetSaisie par défaut
6
 */
6
 */
Line 356... Line 356...
356
	if ( valOk( $( '#form-supp' ) ) ) {
356
	if ( valOk( $( '#form-supp' ) ) ) {
357
		$( '#form-supp' ).validate().resetForm();
357
		$( '#form-supp' ).validate().resetForm();
358
	}
358
	}
359
};
359
};
Line -... Line 360...
-
 
360
 
-
 
361
WidgetSaisie.prototype.validateGeometry = function( geometry ) {
-
 
362
	const isLineString = !!geometry && 'LineString' === geometry.type,
-
 
363
		validateTypeOfCoordinates = coordinates => isLineString ? Array.isArray( coordinates ) : ['number','string'].includes( typeof coordinates );
-
 
364
 
-
 
365
	if ( !valOk( geometry.coordinates ) ) {
-
 
366
		return false;
-
 
367
	}
-
 
368
 
-
 
369
	let isValid = true;
-
 
370
 
-
 
371
	$.each(geometry.coordinates, (i, coordinates) => {
-
 
372
		if ( !validateTypeOfCoordinates( coordinates ) ) {
-
 
373
			isValid = false;
-
 
374
		}
-
 
375
	});
-
 
376
 
-
 
377
	const isValidLength = isLineString ? ( geometry.coordinates.length >= 2 ) : ( geometry.coordinates.length === 2 );
-
 
378
 
-
 
379
	return isValid && isValidLength;
-
 
380
}
360
 
381
 
361
// Géolocalisation *************************************************************/
382
// Géolocalisation *************************************************************/
362
/**
383
/**
363
 * Fonction handler de l'évenement location du module tb-geoloc
384
 * Fonction handler de l'évenement location du module tb-geoloc
364
 */
385
 */
365
WidgetSaisie.prototype.locationHandler = function( location ) {
386
WidgetSaisie.prototype.locationHandler = function( location ) {
-
 
387
	const locDatas          = location.originalEvent.detail,
Line 366... Line 388...
366
	const locDatas = location.originalEvent.detail;
388
		$geolocControlGroup = $( '#geoloc' ).closest( '.control-group' );
367
 
389
 
368
	if ( valOk( locDatas ) ) {
-
 
369
		console.log( locDatas );
-
 
370
 
-
 
371
		const geometry = JSON.stringify( locDatas.geometry ),
-
 
372
			altitude   = ( valOk( locDatas.elevation ) ) ? locDatas.elevation : '',
-
 
373
			pays       = ( valOk( locDatas.osmCountryCode ) ) ? locDatas.osmCountryCode.toUpperCase() : 'FR',
-
 
374
			rue        = ( valOk( locDatas.osmRoad ) ) ? locDatas.osmRoad : '';
-
 
375
		let latitude      = '',
-
 
376
			longitude     = '',
-
 
377
			coordLineaire = '',
-
 
378
			nomCommune    = '',
-
 
379
			communeInsee  = '';
-
 
380
 
-
 
381
		if ( valOk( locDatas.geometry.coordinates ) &&
-
 
382
			valOk( locDatas.centroid.coordinates ) &&
-
 
383
			valOk( locDatas.centroid.coordinates[0] ) &&
390
	if ( !valOk( locDatas ) ) {
384
			valOk( locDatas.centroid.coordinates[1] )
-
 
385
		) {
-
 
386
			longitude = locDatas.centroid.coordinates[0];
-
 
387
			latitude = locDatas.centroid.coordinates[1];
391
		console.warn( 'Error location' );
388
		}
-
 
389
		if ( valOk( locDatas.inseeData ) ) {
-
 
390
			nomCommune = locDatas.inseeData.nom;
-
 
391
			communeInsee = ( valOk( locDatas.inseeData.code ) ) ? locDatas.inseeData.code : '';
-
 
392
		} else if ( valOk( locDatas.locality ) ) {
-
 
393
			nomCommune = locDatas.locality;
392
	} else {
394
		} else if ( valOk( locDatas.locality ) ) {
-
 
395
			nomCommune = locDatas.osmCounty;
393
		if ( !this.validateGeometry( locDatas.geometry ) ) {
396
		}
-
 
397
		$( '#geometry' ).val( geometry );
-
 
398
		$( '#coord-lineaire' ).val( coordLineaire );
-
 
399
		$( '#latitude' ).val( latitude );
-
 
400
		$( '#longitude' ).val( longitude );
-
 
401
		$( '#commune-nom' ).val( nomCommune );
-
 
402
		$( '#commune-insee' ).val( communeInsee );
-
 
403
		$( '#altitude' ).val( altitude );
-
 
404
		$( '#pays' ).val( pays );
-
 
405
		$( '#station' ).val( rue );
-
 
406
		$( '#latitude, #longitude' ).valid();
-
 
407
		if ( valOk( $( '#latitude' ).val() ) && valOk( $( '#longitude' ).val() ) ) {
394
			$geolocControlGroup.addClass( 'error' );
-
 
395
			$( '#geometry' ).val( '' );
-
 
396
		} else {
-
 
397
			console.log( locDatas );
-
 
398
 
-
 
399
			const geometry = JSON.stringify( locDatas.geometry ),
-
 
400
				altitude   = ( valOk( locDatas.elevation ) ) ? locDatas.elevation : '',
-
 
401
				pays       = ( valOk( locDatas.osmCountryCode ) ) ? locDatas.osmCountryCode.toUpperCase() : 'FR',
-
 
402
				rue        = ( valOk( locDatas.osmRoad ) ) ? locDatas.osmRoad : '';
-
 
403
			let latitude      = '',
-
 
404
				longitude     = '',
-
 
405
				coordLineaire = '',
-
 
406
				nomCommune    = '',
-
 
407
				communeInsee  = '';
-
 
408
 
-
 
409
			if ( valOk( locDatas.geometry.coordinates ) &&
-
 
410
				valOk( locDatas.centroid.coordinates ) &&
-
 
411
				valOk( locDatas.centroid.coordinates[0] ) &&
-
 
412
				valOk( locDatas.centroid.coordinates[1] )
-
 
413
			) {
-
 
414
				longitude = locDatas.centroid.coordinates[0];
-
 
415
				latitude = locDatas.centroid.coordinates[1];
-
 
416
			}
-
 
417
			if ( valOk( locDatas.inseeData ) ) {
-
 
418
				nomCommune = locDatas.inseeData.nom;
-
 
419
				communeInsee = ( valOk( locDatas.inseeData.code ) ) ? locDatas.inseeData.code : '';
-
 
420
			} else if ( valOk( locDatas.locality ) ) {
-
 
421
				nomCommune = locDatas.locality;
-
 
422
			} else if ( valOk( locDatas.locality ) ) {
-
 
423
				nomCommune = locDatas.osmCounty;
-
 
424
			}
-
 
425
			$( '#geometry' ).val( geometry );
-
 
426
			$( '#coord-lineaire' ).val( coordLineaire );
-
 
427
			$( '#latitude' ).val( latitude );
-
 
428
			$( '#longitude' ).val( longitude );
-
 
429
			$( '#commune-nom' ).val( nomCommune );
-
 
430
			$( '#commune-insee' ).val( communeInsee );
-
 
431
			$( '#altitude' ).val( altitude );
-
 
432
			$( '#pays' ).val( pays );
408
			$( '#geoloc' ).closest( '.control-group' ).removeClass( 'error' );
433
			$( '#station' ).val( rue );
-
 
434
			$( '#latitude, #longitude' ).valid();
-
 
435
			$geolocControlGroup.toggleClass(
-
 
436
					'error',
409
		} else {
437
					!valOk( $( '#latitude' ).val() ) || !valOk( $( '#longitude' ).val() )
410
			$( '#geoloc' ).closest( '.control-group' ).addClass( 'error' );
-
 
411
		}
-
 
412
	} else {
438
				);
413
		console.warn( 'Error location' );
439
		}
Line 414... Line 440...
414
	}
440
	}
415
}
441
}
Line 617... Line 643...
617
};
643
};
Line 618... Line 644...
618
 
644
 
619
 
645
 
620
WidgetSaisie.prototype.validerCertitudeTaxonImage = function( hasTaxon = false, hasImages = false ) {
646
WidgetSaisie.prototype.validerCertitudeTaxonImage = function( hasTaxon = false, hasImages = false ) {
Line 621... Line 647...
621
	const isCertain = 'certain' === $( '#certitude' ).val();
647
	const isCertain = 'certain' === $( '#certitude' ).val();
622
	let isvalide = true ;
648
	let isvalid = true ;
623
 
649
 
624
	if ( this.photoObligatoire || !isCertain ) {
650
	if ( this.photoObligatoire || !isCertain ) {
625
		isvalide = this.validerImageRequise( hasImages );
651
		isvalid = this.validerImageRequise( hasImages );
626
	}
652
	}
Line 627... Line 653...
627
	if ( isCertain ) {
653
	if ( isCertain ) {
628
		isvalide &= this.validerTaxonRequis( hasTaxon );
-
 
629
	}
-
 
630
 
654
		isvalid &= this.validerTaxonRequis( hasTaxon );
Line 631... Line 655...
631
	return isvalide;
655
	}
632
 
656
 
Line 696... Line 720...
696
	// Commence à observer le noeud cible pour les mutations précédemment configurées
720
	// Commence à observer le noeud cible pour les mutations précédemment configurées
697
	this.observer.observe( targetNode, { childList: true } );
721
	this.observer.observe( targetNode, { childList: true } );
698
};
722
};
Line 699... Line 723...
699
 
723
 
700
WidgetSaisie.prototype.validerForm = function() {
724
WidgetSaisie.prototype.validerForm = function() {
701
	const observateur = ( $( '#form-observateur' ).valid() && $( '#courriel' ).valid() && $( '#courriel_confirmation' ).valid() ),
725
	const observateur  = ( $( '#form-observateur' ).valid() && $( '#courriel' ).valid() && $( '#courriel_confirmation' ).valid() ),
-
 
726
		obs            = $( '#form-observation' ).valid(),
702
		obs           = $( '#form-observation' ).valid(),
727
		parsedGeometry = tryParseJson( $( '#geometry' ).val() ),
703
		geoloc        = ( valOk( $( '#latitude' ).val() ) && valOk( $( '#longitude' ).val() ) ) ,
728
		geoloc         = this.validateGeometry( parsedGeometry ) && ( valOk( $( '#latitude' ).val() ) && valOk( $( '#longitude' ).val() ) ) ,
704
		// validation et panneau taxon/images
729
		// validation et panneau taxon/images
705
		certitudeTaxonImage  = this.validerCertitudeTaxonImage(
730
		certitudeTaxonImage  = this.validerCertitudeTaxonImage(
706
			valOk( $( '#taxon' ).val() ),
731
			valOk( $( '#taxon' ).val() ),
707
			valOk( $( '#miniatures .miniature' ) )
732
			valOk( $( '#miniatures .miniature' ) )