Subversion Repositories eFlore/Applications.cel

Rev

Rev 3638 | Rev 3852 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3638 Rev 3844
1
"use strict";
-
 
2
 
-
 
3
function Utils(){
1
export function Utils() {
4
  this.cheminFichiers = $( '#zone-appli' ).data( 'url-fichiers' );
2
  this.cheminFichiers = $( '#zone-appli' ).data( 'url-fichiers' );
5
  // système de traduction minimaliste
3
  // système de traduction minimaliste
6
  this.msgs                 = {
4
  this.msgs = {
7
    fr: {
5
    fr: {
8
      'arbre'                             : 'Arbre',
6
      'arbre'                             : 'Arbre',
9
      'dupliquer'                         : 'Dupliquer',
7
      'dupliquer'                         : 'Dupliquer',
10
      'saisir-plantes'                    : 'Saisir les plantes',
8
      'saisir-plantes'                    : 'Saisir les plantes',
11
      'saisir-lichens'                    : 'Saisir les lichens',
9
      'saisir-lichens'                    : 'Saisir les lichens',
12
      'upload-non-suppote'                : 'Votre navigateur ne permet pas le téléchargement de fichiers.',
10
      'upload-non-suppote'                : 'Votre navigateur ne permet pas le téléchargement de fichiers.',
13
      'format-non-supporte'               : 'Le format de fichier n\'est pas supporté, les formats acceptés sont',
11
      'format-non-supporte'               : 'Le format de fichier n\'est pas supporté, les formats acceptés sont',
14
      'image-deja-chargee'                : 'Cette image a déjà été utilisée',
12
      'image-deja-chargee'                : 'Cette image a déjà été utilisée',
15
      'date-incomplete'                   : 'Format : jj/mm/aaaa.',
13
      'date-incomplete'                   : 'Format : jj/mm/aaaa.',
16
      'observations-transmises'           : 'observations transmises',
14
      'observations-transmises'           : 'observations transmises',
17
      'supprimer-observation-liste'       : 'Supprimer cette observation de la liste à transmettre',
15
      'supprimer-observation-liste'       : 'Supprimer cette observation de la liste à transmettre',
18
      'confirmation-suppression'          : 'Êtes-vous sûr de vouloir supprimer cette observation',
16
      'confirmation-suppression'          : 'Êtes-vous sûr de vouloir supprimer cette observation',
19
      'milieu'                            : 'Milieu',
17
      'milieu'                            : 'Milieu',
20
      'commentaires'                      : 'Commentaires',
18
      'commentaires'                      : 'Commentaires',
21
      'non-lie-au-ref'                    : 'non lié au référentiel',
19
      'non-lie-au-ref'                    : 'non lié au référentiel',
22
      'obs-le'                            : 'le',
20
      'obs-le'                            : 'le',
23
      'non-connexion'                     : 'Veuillez entrer votre login et votre mot de passe',
21
      'non-connexion'                     : 'Veuillez entrer votre login et votre mot de passe',
24
      'obs-numero'                        : 'Observation n°',
22
      'obs-numero'                        : 'Observation n°',
25
      'erreur'                            : 'Erreur',
23
      'erreur'                            : 'Erreur',
26
      'erreur-inconnue'                   : 'Erreur inconnue',
24
      'erreur-inconnue'                   : 'Erreur inconnue',
27
      'erreur-image'                      : 'Erreur lors du chargement des images',
25
      'erreur-image'                      : 'Erreur lors du chargement des images',
28
      'erreur-ajax'                       : 'Erreur Ajax',
26
      'erreur-ajax'                       : 'Erreur Ajax',
29
      'erreur-chargement'                 : 'Erreur lors du chargement de l\'observation',
27
      'erreur-chargement'                 : 'Erreur lors du chargement de l\'observation',
30
      'erreur-chargement-obs-utilisateur' : 'Erreur lors du chargement des observations de cet utilisateur',
28
      'erreur-chargement-obs-utilisateur' : 'Erreur lors du chargement des observations de cet utilisateur',
31
      'erreur-formulaire'                 : 'Erreur: impossible de charger le formulaire',
29
      'erreur-formulaire'                 : 'Erreur: impossible de charger le formulaire',
32
      'lieu-obs'                          : 'observé à',
30
      'lieu-obs'                          : 'observé à',
33
      'lieu-dit'                          : 'Lieu-dit',
31
      'lieu-dit'                          : 'Lieu-dit',
34
      'taxon-ou-image'                     : 'Veuillez transmettre au moins, soit une image, soit une espèce',
32
      'taxon-ou-image'                     : 'Veuillez transmettre au moins, soit une image, soit une espèce',
35
      'station'                           : 'Station',
33
      'station'                           : 'Station',
36
      'date-rue'                          : 'Un releve existe dejà à cette date pour cette rue.',
34
      'date-rue'                          : 'Un releve existe dejà à cette date pour cette rue.',
37
      'rechargement-page'                 : 'Êtes vous sûr de vouloir quiter la page?\nLes observations saisies mais non transmises seront perdues.',
35
      'rechargement-page'                 : 'Êtes vous sûr de vouloir quiter la page?\nLes observations saisies mais non transmises seront perdues.',
38
      'courriel-connu'                    : 'Un compte existe pour ce courriel, connectez-vous pour saisir votre observation'
36
      'courriel-connu'                    : 'Un compte existe pour ce courriel, connectez-vous pour saisir votre observation'
39
    },
37
    },
40
    en: {
38
    en: {
41
      'arbre'                             : 'Tree',
39
      'arbre'                             : 'Tree',
42
      'dupliquer'                         : 'Duplicate',
40
      'dupliquer'                         : 'Duplicate',
43
      'saisir-plantes'                    : 'Enter the plants',
41
      'saisir-plantes'                    : 'Enter the plants',
44
      'saisir-lichens'                    : 'Enter the lichens',
42
      'saisir-lichens'                    : 'Enter the lichens',
45
      'upload-non-suppote'                : 'Your browser does not support file upload.',
43
      'upload-non-suppote'                : 'Your browser does not support file upload.',
46
      'format-non-supporte'               : 'The file format is not supported, the accepted formats are',
44
      'format-non-supporte'               : 'The file format is not supported, the accepted formats are',
47
      'image-deja-chargee'                : 'This image has already been used',
45
      'image-deja-chargee'                : 'This image has already been used',
48
      'date-incomplete'                   : 'Format: dd/mm/yyyy.',
46
      'date-incomplete'                   : 'Format: dd/mm/yyyy.',
49
      'observations-transmises'           : 'observations transmitted',
47
      'observations-transmises'           : 'observations transmitted',
50
      'supprimer-observation-liste'       : 'Delete this observation from the list to be transmitted',
48
      'supprimer-observation-liste'       : 'Delete this observation from the list to be transmitted',
51
      'confirmation-suppression'          : 'Are you sure you want to delete this observation',
49
      'confirmation-suppression'          : 'Are you sure you want to delete this observation',
52
      'milieu'                            : 'Environment',
50
      'milieu'                            : 'Environment',
53
      'commentaires'                      : 'Comments',
51
      'commentaires'                      : 'Comments',
54
      'non-lie-au-ref'                    : 'unrelated to the referencial ',
52
      'non-lie-au-ref'                    : 'unrelated to the referencial ',
55
      'obs-le'                            : 'the',
53
      'obs-le'                            : 'the',
56
      'non-connexion'                     : 'Please enter your login and password',
54
      'non-connexion'                     : 'Please enter your login and password',
57
      'obs-numero'                        : 'Observation number ',
55
      'obs-numero'                        : 'Observation number ',
58
      'erreur'                            : 'Error',
56
      'erreur'                            : 'Error',
59
      'erreur-inconnue'                   : 'Unknown error',
57
      'erreur-inconnue'                   : 'Unknown error',
60
      'erreur-image'                      : 'Error loading the images',
58
      'erreur-image'                      : 'Error loading the images',
61
      'erreur-ajax'                       : 'Ajax Error',
59
      'erreur-ajax'                       : 'Ajax Error',
62
      'erreur-chargement'                 : 'Error loading the observation',
60
      'erreur-chargement'                 : 'Error loading the observation',
63
      'erreur-chargement-obs-utilisateur' : 'Error loading this user\'s observations',
61
      'erreur-chargement-obs-utilisateur' : 'Error loading this user\'s observations',
64
      'erreur-formulaire'                 : 'Error: couldn\'t load form',
62
      'erreur-formulaire'                 : 'Error: couldn\'t load form',
65
      'lieu-obs'                          : 'observed at',
63
      'lieu-obs'                          : 'observed at',
66
      'lieu-dit'                          : 'Locality',
64
      'lieu-dit'                          : 'Locality',
67
      'taxon-ou-image'                    : 'Please transmit at least one image or one species',
65
      'taxon-ou-image'                    : 'Please transmit at least one image or one species',
68
      'station'                           : 'Place',
66
      'station'                           : 'Place',
69
      'date-rue'                          : 'A record already exists on this date for this street',
67
      'date-rue'                          : 'A record already exists on this date for this street',
70
      'rechargement-page'                 : 'Are you sure you want to leave the page?\nAll untransmitted observations will be lost.',
68
      'rechargement-page'                 : 'Are you sure you want to leave the page?\nAll untransmitted observations will be lost.',
71
      'courriel-connu'                    : 'An account exists for this email, log in to enter your observation'
69
      'courriel-connu'                    : 'An account exists for this email, log in to enter your observation'
72
    }
70
    }
73
  };
71
  };
74
};
72
};
75
 
-
 
76
var lthis = Utils.prototype;
-
 
77
 
73
 
78
Utils.prototype.init = function() {
74
Utils.prototype.init = function() {
79
  // Modale "aide" du projet
75
  // Modale "aide" du projet
80
  this.projetHelpModale();
76
  this.projetHelpModale();
81
  // Affichage input file
77
  // Affichage input file
82
  this.inputFile();
78
  this.inputFile();
83
  // Affichage des List-checkbox
79
  // Affichage des List-checkbox
84
  this.inputListCheckbox();
80
  this.inputListCheckbox();
85
  // Affichage des Range
81
  // Affichage des Range
86
  this.inputRangeDisplayNumber()
82
  this.inputRangeDisplayNumber()
87
  // Modale "aide"
83
  // Modale "aide"
88
  this.newFieldsHelpModal();
84
  this.newFieldsHelpModal();
89
  // Ajout/suppression d'un champ texte "Autre"
85
  // Ajout/suppression d'un champ texte "Autre"
90
  this.onOtherOption();
86
  this.onOtherOption();
91
  // Récupérer les données entrées dans "Autre"
87
  // Récupérer les données entrées dans "Autre"
92
  this.collectOtherOption();
88
  this.collectOtherOption();
93
};
89
};
94
 
-
 
95
/**
-
 
96
* Permet à la fois de vérifier qu'une valeur ou objet existe et n'est pas vide
90
 
97
* et de comparer à une autre valeur :
-
 
98
* Vérifie qu'une variable ou objet n'est pas : vide, null, undefined, NaN
-
 
99
* Si comparer est défini on le compare à valeur en fonction de sensComparaison
91
// aide dans le titre du projet
100
* Un booléen est une variable valide : on retourne true
-
 
101
* @param { string || number || object || undefined } valeur
-
 
102
* @param { boolean } sensComparaison : true = rechercher, false = refuser
-
 
103
* @param { string || number || object || undefined || boolean } comparer :valeur à comparer
92
Utils.prototype.projetHelpModale = function() {
104
* @returns {boolean}
93
  const lthis = this;
105
*/
94
 
106
Utils.prototype.valOk = function (valeur, sensComparaison = true, comparer = undefined ) {
-
 
107
	var retour = true;
-
 
108
	if ( 'boolean' !== typeof valeur ) {
-
 
109
		switch( typeof valeur ) {
-
 
110
			case 'string' :
-
 
111
				retour = ( '' !== valeur );
-
 
112
			 break;
-
 
113
			case 'number' :
95
  $( '#top' ).on ( 'click', '#info-button', function ( event ) {
114
					retour = ( NaN !== valeur );
-
 
115
				break;
-
 
116
			case 'object' :
96
    const fileMimeType = $( this ).data( 'mime-info' ),
117
					retour = ( null !== valeur && undefined !== valeur && !$.isEmptyObject( valeur ) );
-
 
118
					if (  null !== valeur && undefined !== valeur.length ) {
-
 
119
						retour = ( retour  && 0 < valeur.length );
-
 
120
					}
-
 
121
				break;
-
 
122
			case 'undefined' :
-
 
123
			default :
-
 
124
				retour = false;
97
        label          = 'Aide du projet : ' +  $( '#titre-projet' ).text();
125
		}
98
 
126
		if ( retour && comparer !== undefined ) {
99
    if( fileMimeType.match( 'image' ) ) {
127
			var resultComparaison = ( comparer === valeur );
100
      const extention = fileMimeType.replace( /(?:imag)?e\/?/g , '' ),
128
			retour = ( sensComparaison ) ? resultComparaison : !resultComparaison ;
101
          content     = '<img id="modale-aide-img" src="' + lthis.cheminFichiers + 'info.' + extention + '" style="" alt="info projet" />';
129
		}
-
 
130
 
-
 
131
		return retour;
-
 
132
 
-
 
133
	} else {
102
    }
134
		// Un booléen est une valeur valable
-
 
135
		return true;
103
    lthis.activerModale( label, content );
136
	}
104
  });
137
};
-
 
138
 
-
 
139
/*************************************
-
 
140
 *  Fonctions de Style et Affichage  *
-
 
141
 *      des éléments "spéciaux"      *
-
 
142
 *************************************/
105
};
143
 
106
 
144
// Logique d'affichage pour le input type=file
107
// Logique d'affichage pour le input type=file
145
Utils.prototype.inputFile = function() {
108
Utils.prototype.inputFile = function() {
146
  // Initialisation des variables
-
 
147
  var $fileInput  = $( '.input-file' ),
-
 
148
      $button     = $( '.label-file' );
-
 
149
 
-
 
150
  // Action lorsque la "barre d'espace" ou "Entrée" est pressée
109
  // Action lorsque la "barre d'espace" ou "Entrée" est pressée
151
  $( '#formulaire' ).on( 'keydown', '.label-file', function( event ) {
110
  $( '#formulaire' ).on( 'keydown', '.label-file', function( event ) {
152
    if ( event.keyCode == 13 || event.keyCode == 32 ) {
111
    if ( event.keyCode == 13 || event.keyCode == 32 ) {
153
      $( '#' + $( this ).attr( 'for' ) + '.input-file' ).click();
112
      $( '#' + $( this ).attr( 'for' ) + '.input-file' ).click();
154
    }
113
    }
155
  });
114
  });
156
};
115
};
157
 
116
 
158
// Style et affichage des list-checkboxes
117
// Style et affichage des list-checkboxes
159
Utils.prototype.inputListCheckbox = function() {
118
Utils.prototype.inputListCheckbox = function() {
160
  // On écoute le click sur une list-checkbox ('.selectBox')
119
  // On écoute le click sur une list-checkbox ('.selectBox')
161
  // à tout moment de son insertion dans le dom
120
  // à tout moment de son insertion dans le dom
162
  // _ S'assurer de bien viser la bonne list-checkbox
121
  // _ S'assurer de bien viser la bonne list-checkbox
163
  // _ Au click sur un autre champ remballer la list-checkbox
122
  // _ Au click sur un autre champ remballer la list-checkbox
164
  $( document ).click( function( event ) {
123
  $( document ).click( function( event ) {
165
    var target = event.target;
124
    const target = event.target;
166
 
125
 
167
    if ( !$( target ).is( '.overSelect' ) && 0 === $( target ).closest( '.checkboxes' ).length ) {
126
    if ( !$( target ).is( '.overSelect' ) && 0 === $( target ).closest( '.checkboxes' ).length ) {
168
      $( '.checkboxes' ).each( function () {
127
      $( '.checkboxes' ).each( function () {
169
        $( this ).addClass( 'hidden' );
128
        $( this ).addClass( 'hidden' );
170
      });
129
      });
171
      $( '.selectBox select.focus', '#zone-appli' ).each( function() {
130
      $( '.selectBox select.focus', '#zone-appli' ).each( function() {
172
        $( this ).removeClass( 'focus' );
131
        $( this ).removeClass( 'focus' );
173
      });
132
      });
174
    }
133
    }
175
  });
134
  });
176
  $( '#zone-appli' ).on( 'click' , '.selectBox' , function() {
135
  $( '#zone-appli' ).on( 'click' , '.selectBox' , function() {
177
    // afficher/cacher le volet des checkboxes et focus
136
    // afficher/cacher le volet des checkboxes et focus
178
    $( this ).next().toggleClass( 'hidden' );
137
    $( this ).next().toggleClass( 'hidden' );
179
    $( this ).find( 'select' ).toggleClass( 'focus' );
138
    $( this ).find( 'select' ).toggleClass( 'focus' );
180
 
139
 
181
    // Cacher le volet des autres checkboxes et retirer leur focus
140
    // Cacher le volet des autres checkboxes et retirer leur focus
182
    var $checkboxes = $( this ).next(),
141
    const $checkboxes = $( this ).next(),
183
        count = $( '.checkboxes' ).length;
142
        count         = $( '.checkboxes' ).length;
184
 
143
 
185
    for ( var i = 0; i < count; i++ ) {
144
    for ( let i = 0; i < count; i++ ) {
186
      if ( $( '.checkboxes' )[i] !== $checkboxes[0] && !$checkboxes.hasClass( 'hidden' ) ) {
145
      if ( $( '.checkboxes' )[i] !== $checkboxes[0] && !$checkboxes.hasClass( 'hidden' ) ) {
-
 
146
        const $otherListCheckboxes = $( '.checkboxes' )[i];
187
        var $otherListCheckboxes = $( '.checkboxes' )[i];
147
 
188
        if ( !$otherListCheckboxes.classList.contains( 'hidden' ) ) {
148
        if ( !$otherListCheckboxes.classList.contains( 'hidden' ) ) {
189
          $otherListCheckboxes.classList.add( 'hidden' );
149
          $otherListCheckboxes.classList.add( 'hidden' );
190
        }
150
        }
191
        if( $otherListCheckboxes.previousElementSibling.firstElementChild.classList.contains( 'focus' ) ) {
151
        if( $otherListCheckboxes.previousElementSibling.firstElementChild.classList.contains( 'focus' ) ) {
192
          $otherListCheckboxes.previousElementSibling.firstElementChild.classList.remove( 'focus' );
152
          $otherListCheckboxes.previousElementSibling.firstElementChild.classList.remove( 'focus' );
193
        }
153
        }
194
      }
154
      }
195
    }
155
    }
196
  });
156
  });
197
};
157
};
198
 
158
 
199
// Style et affichage des input type="range"
159
// Style et affichage des input type="range"
200
Utils.prototype.inputRangeDisplayNumber = function() {
160
Utils.prototype.inputRangeDisplayNumber = function() {
201
  $( 'input[type="range"]' ).each( function() {
161
  $( 'input[type="range"]' ).each( function() {
202
    $( this ).siblings( '.range-live-value' ).text( $( this ).val() );
162
    $( this ).siblings( '.range-live-value' ).text( $( this ).val() );
203
  });
163
  });
204
  $( '#top' ).on( 'input' , 'input[type="range"]' , function () {
164
  $( '#top' ).on( 'input' , 'input[type="range"]' , function () {
205
    $( this ).siblings( '.range-live-value' ).text( $( this ).val() );
165
    $( this ).siblings( '.range-live-value' ).text( $( this ).val() );
206
  });
166
  });
207
  $( '#top' ).on( 'click', '#ajouter-obs', function() {
167
  $( '#top' ).on( 'click', '#ajouter-obs', function() {
208
    $( '.range-live-value' ).each( function() {
168
    $( '.range-live-value' ).each( function() {
209
      var $this = $( this );
-
 
210
 
-
 
211
      $this.text( '' );
169
      $( this ).text( '' );
212
    });
170
    });
213
  });
171
  });
214
};
172
};
215
 
173
 
216
// Activation/Desactivation et contenu de la modale Bootstrap
174
// Activation/Desactivation et contenu de la modale Bootstrap
217
// https://getbootstrap.com/docs/3.3/javascript/#modals
175
// https://getbootstrap.com/docs/3.3/javascript/#modals
218
Utils.prototype.newFieldsHelpModal = function() {
176
Utils.prototype.newFieldsHelpModal = function() {
219
  const lthis = this;
177
  const lthis = this;
220
 
178
 
221
  $( '#zone-appli' ).on( 'click' , '.help-button' , function ( event ) {
179
  $( '#zone-appli' ).on( 'click' , '.help-button' , function ( event ) {
222
    var thisFieldKey = $( this ).data( 'key' ),
180
    const thisFieldKey = $( this ).data( 'key' ),
223
        fileMimeType = $( this ).data( 'mime-type' ),
181
        fileMimeType   = $( this ).data( 'mime-type' ),
224
        label = 'Aide pour : ' +  $( this ).data( 'name' );
182
        label          = 'Aide pour : ' +  $( this ).data( 'name' );
225
 
183
 
226
    if( fileMimeType.match( 'image' ) ) {
184
    if( fileMimeType.match( 'image' ) ) {
227
      var extention = fileMimeType.replace( /(?:imag)?e\/?/g , '' ),
185
      const extention = fileMimeType.replace( /(?:imag)?e\/?/g , '' ),
228
          content = '<img id="modale-aide-img" src="' + lthis.cheminFichiers + thisFieldKey + '.' + extention + '" style="" alt="' + thisFieldKey + '" />';
186
          content     = '<img id="modale-aide-img" src="' + lthis.cheminFichiers + thisFieldKey + '.' + extention + '" style="" alt="' + thisFieldKey + '" />';
229
    }
187
    }
230
    lthis.activerModale( label, content );
188
    lthis.activerModale( label, content );
231
  });
189
  });
232
};
190
};
233
 
-
 
234
// aide dans le titre du projet
-
 
235
Utils.prototype.projetHelpModale = function() {
-
 
236
  const lthis = this;
-
 
237
 
-
 
238
  $( '#top' ).on ( 'click', '#info-button', function ( event ) {
-
 
239
    var fileMimeType = $( this ).data( 'mime-info' ),
-
 
240
        label        = 'Aide du projet : ' +  $( '#titre-projet' ).text();
-
 
241
 
-
 
242
    if( fileMimeType.match( 'image' ) ) {
-
 
243
      var extention = fileMimeType.replace( /(?:imag)?e\/?/g , '' ),
-
 
244
          content   = '<img id="modale-aide-img" src="' + lthis.cheminFichiers + 'info.' + extention + '" style="" alt="info projet" />';
-
 
245
    }
-
 
246
    lthis.activerModale( label, content );
-
 
247
  });
-
 
248
};
-
 
249
 
-
 
250
 
191
 
251
// Activation/Desactivation et contenu de la modale Bootstrap
192
// Activation/Desactivation et contenu de la modale Bootstrap
252
// https://getbootstrap.com/docs/3.3/javascript/#modals
193
// https://getbootstrap.com/docs/3.3/javascript/#modals
253
Utils.prototype.activerModale = function( label, content = '', buttons = [] ) {
194
Utils.prototype.activerModale = function( label, content = '', buttons = [] ) {
254
  var dismiss         = '',
195
  const lthis = this,
255
      buttonsHtmlBase = '<button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>',
196
    buttonsHtmlBase = '<button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>';
-
 
197
  let dismiss     = '',
256
      buttonsHtml     = buttonsHtmlBase;
198
      buttonsHtml = buttonsHtmlBase;
257
 
199
 
258
  // Titre
200
  // Titre
259
  $( '#fenetre-modal-label' ).text( label );
201
  $( '#fenetre-modal-label' ).text( label );
260
  if ( '' !== content ) {
202
  if ( '' !== content ) {
261
    $( '#print_content' ).append( content );
203
    $( '#print_content' ).append( content );
262
  }
204
  }
263
  // Sortie avec la touche escape
205
  // Sortie avec la touche escape
264
  $( '#fenetre-modal' ).modal( { keyboard : true } );
206
  $( '#fenetre-modal' ).modal( { keyboard : true } );
265
  // Affichage
207
  // Affichage
266
  $( '#fenetre-modal' ).modal({ show: true });
208
  $( '#fenetre-modal' ).modal({ show: true });
267
  // Remplacer l'autofocus qui ne fonctionne plus en HTML5
209
  // Remplacer l'autofocus qui ne fonctionne plus en HTML5
268
  // Message dans la doc de bootstrap :
210
  // Message dans la doc de bootstrap :
269
  // Due to how HTML5 defines its semantics,
211
  // Due to how HTML5 defines its semantics,
270
  // the autofocus HTML attribute has no effect in Bootstrap modals.
212
  // the autofocus HTML attribute has no effect in Bootstrap modals.
271
  // To achieve the same effect, use some custom JavaScript
213
  // To achieve the same effect, use some custom JavaScript
272
  $( '#fenetre-modal' ).off( 'shown.bs.modal' ).on( 'shown.bs.modal' , function () {
214
  $( '#fenetre-modal' ).off( 'shown.bs.modal' ).on( 'shown.bs.modal' , function () {
273
    $( '#myInput' ).trigger( 'focus' );
215
    $( '#myInput' ).trigger( 'focus' );
274
    if( lthis.valOk( $( '#modale-aide-img' ) ) ) {
216
    if( lthis.valOk( $( '#modale-aide-img' ) ) ) {
275
      lthis.redimentionnerImgAide();
217
      lthis.redimentionnerImgAide();
276
      $( window ).on( 'resize', lthis.redimentionnerImgAide.bind( lthis ) );
218
      $( window ).on( 'resize', lthis.redimentionnerImgAide.bind( lthis ) );
277
    }
219
    }
278
  });
220
  });
279
  if ( this.valOk( buttons ) ) {
221
  if ( this.valOk( buttons ) ) {
280
    buttonsHtml = '';
222
    buttonsHtml = '';
281
    $.each( buttons, function( i, button ) {
223
    $.each( buttons, function( i, button ) {
282
      dismiss = button.dismiss ? 'data-dismiss="modal"' : '';
224
      dismiss = button.dismiss ? 'data-dismiss="modal"' : '';
283
      buttonsHtml += '<button type="button" class="btn ' + button.class + '" ' + dismiss + '>' + button.label + '</button>';
225
      buttonsHtml += '<button type="button" class="btn ' + button.class + '" ' + dismiss + '>' + button.label + '</button>';
284
    });
226
    });
285
  }
227
  }
286
  $( '.modal-footer' ).html( buttonsHtml );
228
  $( '.modal-footer' ).html( buttonsHtml );
287
  // Réinitialisation
229
  // Réinitialisation
288
  $( '#fenetre-modal' ).on( 'hidden.bs.modal' , function () {
230
  $( '#fenetre-modal' ).on( 'hidden.bs.modal' , function () {
289
    $( '#confirm-modal-label' ).text();
231
    $( '#confirm-modal-label' ).text();
290
    $( '#print_content' ).empty();
232
    $( '#print_content' ).empty();
291
    $( '.modal-footer' ).html( buttonsHtmlBase );
233
    $( '.modal-footer' ).html( buttonsHtmlBase );
292
  });
234
  });
293
};
235
};
294
 
236
 
295
Utils.prototype.redimentionnerImgAide = function() {
237
Utils.prototype.redimentionnerImgAide = function() {
296
  var espHorizDisp = $( '.modal-dialog' ).innerWidth() <= 1200 ? $( '.modal-dialog' ).innerWidth() - 30 : 1200,
238
  const espHorizDisp = $( '.modal-dialog' ).innerWidth() <= 1200 ? $( '.modal-dialog' ).innerWidth() - 30 : 1200,
297
      cssImg       = {
239
    cssImg           = {
298
        'width': espHorizDisp,
240
      'width': espHorizDisp,
299
        'height' : 'auto'
241
      'height' : 'auto'
300
      };
242
    };
301
 
243
 
302
  $( '#modale-aide-img' ).css(cssImg).show(50);
244
  $( '#modale-aide-img' ).css(cssImg).show(50);
303
};
245
};
304
 
246
 
305
// Faire apparaitre un champ text "Autre"
247
// Faire apparaitre un champ text "Autre"
306
Utils.prototype.onOtherOption = function() {
248
Utils.prototype.onOtherOption = function() {
-
 
249
  const lthis = this,
307
  const PREFIX = 'collect-other-';
250
    PREFIX = 'collect-other-';
308
 
-
 
309
  // Ajouter un champ texte pour "Autre"
251
  // Ajouter un champ texte pour "Autre"
310
  function optionAdd( otherId, $target, element, dataName, dataLabel ) {
252
  const optionAdd = (
-
 
253
      otherId,
-
 
254
      $target,
-
 
255
      element,
-
 
256
      dataName,
-
 
257
      dataLabel
-
 
258
    ) => {
311
    $target.after(
259
      $target.after(
312
      '<div class="control-group">'+
260
        '<div class="control-group">'+
313
        '<label'+
261
          '<label'+
314
          ' for="' + otherId + '"'+
262
            ' for="' + otherId + '"'+
315
          ' class="' + otherId + '"'+
263
            ' class="' + otherId + '"'+
316
        '>'+
264
          '>'+
317
          'Autre option "' + dataLabel.toLowerCase() + '" :'+
265
            'Autre option "' + dataLabel.toLowerCase() + '" :'+
318
        '</label>'+
266
          '</label>'+
319
        '<input'+
267
          '<input'+
320
          ' type="text"'+
268
            ' type="text"'+
321
          ' id="' + otherId + '"'+
269
            ' id="' + otherId + '"'+
322
          ' name="' + otherId + '"'+
270
            ' name="' + otherId + '"'+
323
          ' class="collect-other form-control"'+
271
            ' class="collect-other form-control"'+
324
          ' data-name="' + dataName + '"'+
272
            ' data-name="' + dataName + '"'+
325
          ' data-element="' + element + '"'+
273
            ' data-element="' + element + '"'+
326
        '>'+
274
          '>'+
327
      '</div>'
275
        '</div>'
328
    );
276
      );
329
    $( '#' + otherId ).focus();
277
      $( '#' + otherId ).focus();
330
  }
278
    };
331
 
-
 
332
  // Supprimer un champ texte pour "Autre"
279
  // Supprimer un champ texte pour "Autre"
333
  function optionRemove( otherId ) {
-
 
334
    $( '#' + otherId ).closest('.control-group').remove();
280
  const optionRemove = otherId => $( '#' + otherId ).closest('.control-group').remove();
335
  }
281
 
-
 
282
  let dataName  = '',
-
 
283
      otherId   = '',
-
 
284
      dataLabel = '',
-
 
285
      element   = '';
336
 
286
 
337
  $( '.other', '#formulaire' ).each( function() {
287
  $( '.other', '#formulaire' ).each( function() {
338
    if( $( this ).hasClass( 'is-select' ) ) {
288
    if( $( this ).hasClass( 'is-select' ) ) {
339
      var dataName = $( this ).data( 'name' ),
289
      dataName  = $( this ).data( 'name' );
340
          otherId  = PREFIX + dataName,
290
      otherId   = PREFIX + dataName;
341
          dataLabel = $( '.select' ).data( 'label' );
291
      dataLabel = $( '.select' ).data( 'label' );
342
 
-
 
343
      // Insertion du champ "Autre" après les boutons
292
      // Insertion du champ "Autre" après les boutons
344
      if ( !lthis.valOk( $( '#'+otherId ) ) ) {
293
      if ( !lthis.valOk( $( '#'+otherId ) ) ) {
345
        optionAdd( otherId, $( this ).parent( '.add-field-select' ), 'select', dataName, dataLabel );
294
        optionAdd( otherId, $( this ).parent( '.add-field-select' ), 'select', dataName, dataLabel );
346
      }
295
      }
347
    } else if ( $( this ).is( ':checked' ) ) {
296
    } else if ( $( this ).is( ':checked' ) ) {
348
      var dataName = $( this ).data( 'name' ),
297
      dataName  = $( this ).data( 'name' );
349
          otherId  = PREFIX + dataName,
298
      otherId   = PREFIX + dataName;
350
          element = $( this ).data( 'element' ),
299
      dataLabel = $( this ).data( 'label' );
351
          dataLabel = $( this ).data( 'label' );
300
      element = $( this ).data( 'element' );
352
 
-
 
353
      // Insertion du champ "Autre" après les boutons
301
      // Insertion du champ "Autre" après les boutons
354
      if ( !lthis.valOk( $( '#'+ otherId ) ) ) {
302
      if ( !lthis.valOk( $( '#'+ otherId ) ) ) {
355
        optionAdd( otherId, $( this ).parent( 'label' ), element, dataName, dataLabel );
303
        optionAdd( otherId, $( this ).parent( 'label' ), element, dataName, dataLabel );
356
      }
304
      }
357
    }
305
    }
358
  });
306
  });
359
 
307
 
360
  $( '#formulaire' ).on( 'change', '.select', function () {
308
  $( '#formulaire' ).on( 'change', '.select', function () {
361
    var dataName = $( this ).data( 'name' ),
309
    dataName  = $( this ).data( 'name' );
362
        otherId  = PREFIX + dataName,
310
    otherId   = PREFIX + dataName;
363
        dataLabel = $( this ).data( 'label' );
311
    dataLabel = $( this ).data( 'label' );
364
 
-
 
365
    if( 'other' === $( this ).val() ) {
312
    if( 'other' === $( this ).val() ) {
366
        // Insertion du champ "Autre" après les boutons
313
        // Insertion du champ "Autre" après les boutons
367
        if ( !lthis.valOk( $( '#'+otherId ) ) ) {
314
        if ( !lthis.valOk( $( '#'+otherId ) ) ) {
368
          optionAdd( otherId, $( this ).parent( '.add-field-select' ), 'select', dataName, dataLabel );
315
          optionAdd( otherId, $( this ).parent( '.add-field-select' ), 'select', dataName, dataLabel );
369
        }
316
        }
370
      } else {
317
      } else {
371
        // Suppression du champ autre
318
        // Suppression du champ autre
372
        optionRemove( otherId );
319
        optionRemove( otherId );
373
        $( this ).find( '.other' ).val( 'other' );
320
        $( this ).find( '.other' ).val( 'other' );
374
      }
321
      }
375
  });
322
  });
376
 
323
 
377
  $( '#formulaire' ).on( 'change', 'input[type=radio]', function () {
324
  $( '#formulaire' ).on( 'change', 'input[type=radio]', function () {
378
    var dataName = $( this ).data( 'name' ),
325
    dataName = $( this ).data( 'name' );
379
        otherId  = PREFIX + dataName,
326
    otherId  = PREFIX + dataName;
380
        dataLabel = $( this ).data( 'label' );
327
    dataLabel = $( this ).data( 'label' );
381
 
-
 
382
    if( 'other' === $( this ).val() ) {
328
    if( 'other' === $( this ).val() ) {
383
      // Insertion du champ "Autre" après les boutons
329
      // Insertion du champ "Autre" après les boutons
384
      if ( !lthis.valOk( $( '#'+otherId ) ) ) {
330
      if ( !lthis.valOk( $( '#'+otherId ) ) ) {
385
        optionAdd( otherId, $( this ).parent( 'label' ), 'radio', dataName, dataLabel );
331
        optionAdd( otherId, $( this ).parent( 'label' ), 'radio', dataName, dataLabel );
386
      }
332
      }
387
    } else {
333
    } else {
388
      // Suppression du champ autre
334
      // Suppression du champ autre
389
      optionRemove( otherId );
335
      optionRemove( otherId );
390
      $( this ).closest( '.radio' ).find( '.other' ).val( 'other' );
336
      $( this ).closest( '.radio' ).find( '.other' ).val( 'other' );
391
    }
337
    }
392
  });
338
  });
393
 
339
 
394
  $( '#formulaire' ).on( 'click', '.list-checkbox .other,.checkbox .other', function () {
340
  $( '#formulaire' ).on( 'click', '.list-checkbox .other,.checkbox .other', function () {
395
    var dataName = $( this ).data( 'name' ),
341
    dataName = $( this ).data( 'name' );
396
        otherId  = PREFIX + dataName,
342
    otherId  = PREFIX + dataName;
397
        element = $( this ).data( 'element' ),
343
    dataLabel = $( this ).data( 'label' );
398
        dataLabel = $( this ).data( 'label' );
344
    element = $( this ).data( 'element' );
399
 
-
 
400
    if( $( this ).is( ':checked' ) ) {
345
    if( $( this ).is( ':checked' ) ) {
401
        // Insertion du champ "Autre" après les boutons
346
        // Insertion du champ "Autre" après les boutons
402
        if ( !lthis.valOk( $( '#'+otherId ) ) ) {
347
        if ( !lthis.valOk( $( '#'+otherId ) ) ) {
403
          optionAdd( otherId, $( this ).parent( 'label' ), element, dataName, dataLabel );
348
          optionAdd( otherId, $( this ).parent( 'label' ), element, dataName, dataLabel );
404
        }
349
        }
405
      } else {
350
      } else {
406
        // Suppression du champ autre
351
        // Suppression du champ autre
407
        optionRemove( otherId );
352
        optionRemove( otherId );
408
        $( this ).val( 'other' );
353
        $( this ).val( 'other' );
409
      }
354
      }
410
  });
355
  });
411
};
356
};
412
 
357
 
413
Utils.prototype.collectOtherOption = function() {
358
Utils.prototype.collectOtherOption = function() {
414
  $( '#formulaire' ).on( 'change', '.collect-other', function () {
359
  $( '#formulaire' ).on( 'change', '.collect-other', function () {
415
    var otherIdSuffix = $( this ).data( 'name' ).replace( '[]', '' );
360
    const otherIdSuffix = $( this ).data( 'name' ).replace( '[]', '' ),
416
    var element = $( this ).data( 'element' );
361
      element           = $( this ).data( 'element' );
417
 
362
 
418
    if ( '' === $( this ).val() ){
363
    if ( '' === $( this ).val() ){
419
      if ( 'select' === element ) {
364
      if ( 'select' === element ) {
420
        $( '#' + otherIdSuffix ).find( '.other' ).prop( 'selected', false ).val( 'other' );
365
        $( '#' + otherIdSuffix ).find( '.other' ).prop( 'selected', false ).val( 'other' );
421
      } else {
366
      } else {
422
        $( '#other-' + otherIdSuffix ).prop( 'checked', false ).val( 'other' );
367
        $( '#other-' + otherIdSuffix ).prop( 'checked', false ).val( 'other' );
423
      }
368
      }
424
      $( 'label.collect-other-' + otherIdSuffix ).remove();
369
      $( 'label.collect-other-' + otherIdSuffix ).remove();
425
      $( this ).remove();
370
      $( this ).remove();
426
    } else {
371
    } else {
427
      if ( 'select' === element ) {
372
      if ( 'select' === element ) {
428
        $( '#' +  otherIdSuffix ).find( '.other' ).val( $( this ).val() );
373
        $( '#' +  otherIdSuffix ).find( '.other' ).val( $( this ).val() );
429
        $( '#' +  otherIdSuffix ).val( $( this ).val() );
374
        $( '#' +  otherIdSuffix ).val( $( this ).val() );
430
        $( '#' +  otherIdSuffix + ' option').not( '.other' ).prop( 'selected', false );
375
        $( '#' +  otherIdSuffix + ' option').not( '.other' ).prop( 'selected', false );
431
        $( '#' +  otherIdSuffix ).find( '.other' ).prop( 'selected', true );
376
        $( '#' +  otherIdSuffix ).find( '.other' ).prop( 'selected', true );
432
      } else {
377
      } else {
433
        if ( 'radio' === element ) {
378
        if ( 'radio' === element ) {
434
          $( 'input[name=' + otherIdSuffix + ']' ).not( '#other-' + otherIdSuffix ).prop( 'checked', false );
379
          $( 'input[name=' + otherIdSuffix + ']' ).not( '#other-' + otherIdSuffix ).prop( 'checked', false );
435
        }
380
        }
436
        $( '#other-' + otherIdSuffix ).val( $( this ).val() );
381
        $( '#other-' + otherIdSuffix ).val( $( this ).val() );
437
        $( '#other-' + otherIdSuffix ).prop( 'checked', true );
382
        $( '#other-' + otherIdSuffix ).prop( 'checked', true );
438
      }
383
      }
439
    }
384
    }
440
  });
385
  });
441
};
386
};
-
 
387
 
-
 
388
/**
-
 
389
* Permet à la fois de vérifier qu'une valeur ou objet existe et n'est pas vide
-
 
390
* et de comparer à une autre valeur :
-
 
391
* Vérifie qu'une variable ou objet n'est pas : vide, null, undefined, NaN
-
 
392
* Si comparer est défini on le compare à valeur en fonction de sensComparaison
-
 
393
* Un booléen est une variable valide : on retourne true
-
 
394
* @param { string || number || object || undefined } valeur
-
 
395
* @param { boolean } sensComparaison : true = rechercher, false = refuser
-
 
396
* @param { string || number || object || undefined || boolean } comparer :valeur à comparer
-
 
397
* @returns {boolean}
-
 
398
*/
-
 
399
export const valOk = (
-
 
400
  valeur,
-
 
401
  sensComparaison = true,
-
 
402
  comparer = undefined
-
 
403
) => {
-
 
404
  let retour = true;
-
 
405
 
-
 
406
  if ( 'boolean' !== typeof valeur ) {
-
 
407
    switch( typeof valeur ) {
-
 
408
      case 'string' :
-
 
409
        retour = ( '' !== valeur );
-
 
410
       break;
-
 
411
      case 'number' :
-
 
412
          retour = ( NaN !== valeur );
-
 
413
        break;
-
 
414
      case 'object' :
-
 
415
          retour = ( null !== valeur && undefined !== valeur && !$.isEmptyObject( valeur ) );
-
 
416
          if (  null !== valeur && undefined !== valeur.length ) {
-
 
417
            retour = ( retour  && 0 < valeur.length );
-
 
418
          }
-
 
419
        break;
-
 
420
      case 'undefined' :
-
 
421
      default :
-
 
422
        retour = false;
-
 
423
    }
-
 
424
    if ( retour && comparer !== undefined ) {
-
 
425
      const resultComparaison = ( comparer === valeur );
-
 
426
 
-
 
427
      retour = ( sensComparaison ) ? resultComparaison : !resultComparaison ;
-
 
428
    }
-
 
429
 
-
 
430
    return retour;
-
 
431
 
-
 
432
  } else {
-
 
433
    // Un booléen est une valeur valable
-
 
434
    return true;
-
 
435
  }
442
 
436
};