Subversion Repositories eFlore/Applications.cel

Rev

Rev 3823 | Rev 3976 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3631 idir 1
function WidgetPhotoContact () {}
2
 
3
WidgetPhotoContact.prototype = new WidgetPhotoCommun();
4
 
5
WidgetPhotoContact.prototype.initTpl = function() {
6
	$( '#form-contact' ).validate({
7
		rules: {
8
			fc_sujet : 'required',
9
			fc_message : 'required',
10
			fc_utilisateur_courriel : {
11
				required : true,
12
				email : true
13
			}
14
		}
15
	});
16
};
17
 
18
WidgetPhotoContact.prototype.initEvts = function() {
19
	const lthis = this;
20
 
21
	$( '#form-contact' ).on( 'submit', function( event ) {
22
		event.preventDefault();
23
		lthis.envoyerCourriel();
24
	});
25
	this.initEvtsRetourPopupPhoto();
26
};
27
 
28
WidgetPhotoContact.prototype.initEvtsRetourPopupPhoto = function() {
29
	const lthis = this;
30
 
31
	$( '#fc_annuler.popup_url' ).on( 'click', function( event ) {
32
		event.preventDefault();
33
		lthis.retourPopupPhoto();
34
	});
35
	$( '#fc_annuler.popup_url' ).on( 'keydown', function( event ) {
36
		var isEnter = false;
37
 
38
		event = event || window.event;
39
		// event.keyCode déprécié, on tente d'abord event.key
40
		if ( 'key' in event) {
41
			isEnter = ( event.key === 'Enter' );
42
		} else {
43
			isEnter = ( event.keyCode === 13 );
44
		}
45
		if ( isEnter ) {
46
			lthis.retourPopupPhoto();
47
		}
48
	});
49
	$( 'body' ).on( 'keyup', function( event ) {
50
		if( $( '#fenetre-modal' ).hasClass( 'show' ) ) {
51
			var isEscape = false;
52
 
53
			event = event || window.event;
54
			// event.keyCode déprécié, on tente d'abord event.key
55
			if ( 'key' in event) {
56
				isEscape = ( event.key === 'Escape' || event.key === 'Esc' );
57
			} else {
58
				isEscape = ( event.keyCode === 27 );
59
			}
60
			if ( isEscape ) {
61
				lthis.retourPopupPhoto();
62
			}
63
		}
64
	});
65
};
66
 
67
WidgetPhotoContact.prototype.envoyerCourriel = function() {
68
	const lthis = this;
3823 idir 69
	var donnees = [];
3631 idir 70
 
71
	if ( $( '#form-contact' ).valid() ) {
72
		var destinataireId = $( '#fc_destinataire_id' ).val(),
73
			typeEnvoi      = $( '#fc_type_envoi' ).val(),
74
		// l'envoi aux non inscrits passe par le service intermédiaire du cel
75
		// qui va récupérer le courriel associé à l'image indiquée
3971 julien 76
			urlMessage     = 'https://api.tela-botanica.org/service:cel:celMessage/image/' + destinataireId,
3631 idir 77
			erreurMsg      = '';
78
 
79
		$.each( $( '#form-contact' ).serializeArray(), function ( index, champ ) {
80
			var cle = champ.name;
81
 
82
			cle = cle.replace( /^fc_/, '' );
83
			if ( cle === 'sujet' ) {
84
				champ.value += ' - Carnet en ligne - Tela Botanica';
85
			}
86
			if ( cle === 'message' ) {
87
				champ.value +=
88
					"\n--\n" +
89
					"Ce message vous est envoyé par l'intermédiaire du widget photo " +
90
					"du Carnet en Ligne du réseau Tela Botanica.\n" +
91
					"http://www.tela-botanica.org/widget:cel:photo";
92
			}
93
			donnees[index] = {
94
				'name' : cle,
95
				'value': champ.value
96
			};
97
		});
98
		$.ajax({
99
			type       : "POST",
100
			cache      : false,
101
			url        : urlMessage,
102
			data       : donnees,
103
			beforeSend : function() {
104
				$( '.msg' ).remove();
105
			},
106
			success    : function( data ) {
107
				$( '#fc-zone-dialogue' ).append( '<pre class="msg info">' + data.message + '</pre>' );
108
			},
109
			error      : function( jqXHR, textStatus, errorThrown ) {
110
				erreurMsg += "Erreur Ajax :\ntype : " + textStatus + ' ' + errorThrown + "\n";
111
				reponse = jQuery.parseJSON( jqXHR.responseText );
112
				if ( lthis.valOk( reponse ) ) {
113
					$.each( reponse, function ( cle, valeur ) {
114
						erreurMsg += valeur + "\n";
115
					});
116
				}
117
			},
118
			complete   : function( jqXHR, textStatus ) {
119
				var debugMsg = '';
120
				if ( lthis.valOk( jqXHR.getResponseHeader( "X-DebugJrest-Data" ) ) ) {
121
					debugInfos = jQuery.parseJSON( jqXHR.getResponseHeader( "X-DebugJrest-Data" ) );
122
					if ( lthis.valOk( debugInfos ) ) {
123
						$.each( debugInfos, function ( cle, valeur ) {
124
							debugMsg += valeur + "\n";
125
						});
126
					}
127
				}
128
				if ( lthis.valOk( erreurMsg ) ) {
129
					$( '#fc-zone-dialogue' ).append(
130
						'<p class="msg">' +
131
							'Une erreur est survenue lors de la transmission de votre message.<br>' +
132
							'Vous pouvez signaler le disfonctionnement à '+
133
							'<a '+
134
								'href="mailto:cel-remarques@tela-botanica.org?'+
135
									'subject=Disfonctionnement du widget carto'+
136
									"&body=" + erreurMsg + "\nDébogage :\n" + debugMsg+
137
								'"' +
138
							'>'+
139
								'cel-remarques@tela-botanica.org'+
140
							'</a>'+
141
							'.'+
142
						'</p>'
143
					);
144
				}
145
			}
146
		});
147
	}
148
	return false;
149
};
150
 
151
 
152
WidgetPhotoContact.prototype.retourPopupPhoto = function() {
153
	const lthis = this;
154
	var popup_url = $( '#fc_annuler.popup_url' ).data( 'popup_url' );
155
	if ( lthis.valOk( popup_url ) ) {
156
		lthis.chargerContenuModale( popup_url );
157
	}
158
};