Subversion Repositories eFlore/Applications.cel

Rev

Rev 3976 | 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
3977 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
		});
3976 julien 98
 
3631 idir 99
		$.ajax({
100
			type       : "POST",
101
			cache      : false,
102
			url        : urlMessage,
103
			data       : donnees,
104
			beforeSend : function() {
105
				$( '.msg' ).remove();
106
			},
107
			success    : function( data ) {
108
				$( '#fc-zone-dialogue' ).append( '<pre class="msg info">' + data.message + '</pre>' );
109
			},
110
			error      : function( jqXHR, textStatus, errorThrown ) {
111
				erreurMsg += "Erreur Ajax :\ntype : " + textStatus + ' ' + errorThrown + "\n";
112
				reponse = jQuery.parseJSON( jqXHR.responseText );
113
				if ( lthis.valOk( reponse ) ) {
114
					$.each( reponse, function ( cle, valeur ) {
115
						erreurMsg += valeur + "\n";
116
					});
117
				}
118
			},
119
			complete   : function( jqXHR, textStatus ) {
120
				var debugMsg = '';
121
				if ( lthis.valOk( jqXHR.getResponseHeader( "X-DebugJrest-Data" ) ) ) {
122
					debugInfos = jQuery.parseJSON( jqXHR.getResponseHeader( "X-DebugJrest-Data" ) );
123
					if ( lthis.valOk( debugInfos ) ) {
124
						$.each( debugInfos, function ( cle, valeur ) {
125
							debugMsg += valeur + "\n";
126
						});
127
					}
128
				}
129
				if ( lthis.valOk( erreurMsg ) ) {
130
					$( '#fc-zone-dialogue' ).append(
131
						'<p class="msg">' +
132
							'Une erreur est survenue lors de la transmission de votre message.<br>' +
133
							'Vous pouvez signaler le disfonctionnement à '+
134
							'<a '+
135
								'href="mailto:cel-remarques@tela-botanica.org?'+
136
									'subject=Disfonctionnement du widget carto'+
137
									"&body=" + erreurMsg + "\nDébogage :\n" + debugMsg+
138
								'"' +
139
							'>'+
140
								'cel-remarques@tela-botanica.org'+
141
							'</a>'+
142
							'.'+
143
						'</p>'
144
					);
145
				}
146
			}
147
		});
3977 julien 148
 
3631 idir 149
	}
150
	return false;
151
};
152
 
153
 
154
WidgetPhotoContact.prototype.retourPopupPhoto = function() {
155
	const lthis = this;
156
	var popup_url = $( '#fc_annuler.popup_url' ).data( 'popup_url' );
157
	if ( lthis.valOk( popup_url ) ) {
158
		lthis.chargerContenuModale( popup_url );
159
	}
160
};