Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
3208 idir 1
"use strict";
2
 
3
/*************************************
4
 *  Fonctions de Style et Affichage  *
5
 *      des éléments "spéciaux"      *
6
 *************************************/
7
 
3239 idir 8
// Volet de profil/déconnexion
9
function connexionDprodownMenu() {
10
  $( '#utilisateur-connecte .volet-toggle, #profil-utilisateur a, #deconnexion a' ).click( function( event ) {
3288 idir 11
    if( $( this ).hasClass( 'volet-toggle' ) ) {
12
      event.preventDefault();
13
    }
3239 idir 14
    $( '#utilisateur-connecte .volet-menu' ).toggleClass( 'hidden' );
15
  });
16
}
17
 
3208 idir 18
// Logique d'affichage pour le input type=file
3217 idir 19
function inputFile() {
3208 idir 20
  // Initialisation des variables
21
  var $fileInput  = $( '.input-file' ),
22
      $button     = $( '.label-file' );
23
  // Action lorsque la "barre d'espace" ou "Entrée" est pressée
24
  $( '.label-file' ).keydown( function( event ) {
25
    if ( event.keyCode == 13 || event.keyCode == 32 ) {
26
      $( '#' + $( this ).attr( 'for' ) + '.input-file' ).click();
27
    }
28
  });
29
}
30
 
31
// Style et affichage des list-checkboxes
32
function inputListCheckbox() {
33
  // On écoute le click sur une list-checkbox ('.selectBox')
34
  // à tout moment de son insertion dans le dom
35
  // _ S'assurer de bien viser la bonne list-checkbox
36
  // _ Au click sur un autre champ remballer la list-checkbox
37
  $( document ).click( function( event ) {
38
    var target = event.target;
39
 
40
    if ( !$( target ).is( '.overSelect' ) && 0 === $( target ).closest( '.checkboxes' ).length ) {
41
      $( '.checkboxes' ).each( function () {
42
        $( this ).addClass( 'hidden' );
43
      });
44
      $( '.selectBox select.focus' ).each( function() {
45
        $( this ).removeClass( 'focus' );
46
      });
47
    }
48
  });
49
  $( '#zone-appli' ).on( 'click' , '.selectBox' , function() {
3240 idir 50
    // afficher/cacher le volet des checkboxes et focus
3208 idir 51
    $( this ).next().toggleClass( 'hidden' );
52
    $( this ).find( 'select' ).toggleClass( 'focus' );
53
 
3240 idir 54
    // Cacher le volet des autres checkboxes et retirer leur focus
55
    var $checkboxes = $( this ).next(),
56
        count = $( '.checkboxes' ).length;
3260 idir 57
 
3240 idir 58
    for ( var i = 0; i < count; i++ ) {
59
      if ( $( '.checkboxes' )[i] !== $checkboxes[0] && !$checkboxes.hasClass( 'hidden' ) ) {
60
        var $otherListCheckboxes = $( '.checkboxes' )[i];
61
        if ( !$otherListCheckboxes.classList.contains( 'hidden' ) ) {
62
          $otherListCheckboxes.classList.add( 'hidden' );
63
        }
64
        if( $otherListCheckboxes.previousElementSibling.firstElementChild.classList.contains( 'focus' ) ) {
65
          $otherListCheckboxes.previousElementSibling.firstElementChild.classList.remove( 'focus' );
66
        }
67
      }
68
    }
3208 idir 69
  });
70
}
71
 
72
// Style et affichage des input type="range"
73
function inputRangeDisplayNumber() {
3239 idir 74
  $( 'input[type="range"]' ).each( function() {
75
    $( this ).siblings( '.range-live-value' ).text( $( this ).val() );
3208 idir 76
  });
3239 idir 77
  $( '#zone-supp' ).on( 'input' , 'input[type="range"]' , function () {
78
    $( this ).siblings( '.range-live-value' ).text( $( this ).val() );
3208 idir 79
  });
3260 idir 80
  $( '#ajouter-obs' ).on( 'click', function() {
81
    $( '.range-live-value' ).each( function() {
82
      var $this = $( this );
83
      $this.text( '' );
84
    });
85
  });
3208 idir 86
}
87
 
88
// Activation/Desactivation et contenu de la modale Bootstrap
89
// https://getbootstrap.com/docs/3.3/javascript/#modals
3239 idir 90
function newFieldsHelpModal() {
3217 idir 91
  $( '#zone-appli' ).on( 'click' , '.help-button' , function ( event ) {
92
    var thisFieldKey = $( this ).data( 'key' ),
93
        fileMimeType = $( this ).data( 'mime-type' );
94
 
3208 idir 95
    // Titre
3217 idir 96
    $( '#help-modal-label' ).text( 'Aide pour : ' +  $( this ).data( 'name' ) );
97
    if( fileMimeType.match( 'image' ) ) {
3239 idir 98
      var extention = fileMimeType.replace( /(?:imag)?e\/?/g , '' );
99
      // var extention = 'jpg';
100
      $( '#print_content' ).append( '<img src="' + CHEMIN_FICHIERS + thisFieldKey + '.' + extention + '" style="max-width:100%" alt="' + thisFieldKey + '" />' );
3208 idir 101
    }
102
    // Sortie avec la touche escape
103
    $( '#help-modal' ).modal( { keyboard : true } );
104
    // Affichage
3217 idir 105
    $( '#help-modal' ).modal({ show: true });
3208 idir 106
    // Remplacer l'autofocus qui ne fonctionne plus en HTML5
107
    // Message dans la doc de bootstrap :
108
    // Due to how HTML5 defines its semantics,
109
    // the autofocus HTML attribute has no effect in Bootstrap modals.
110
    // To achieve the same effect, use some custom JavaScript
111
    $( '#help-modal' ).on( 'shown.bs.modal' , function () {
112
      $( '#myInput' ).trigger( 'focus' );
113
    })
114
    // Réinitialisation
115
    $( '#help-modal' ).on( 'hidden.bs.modal' , function () {
116
      $( '#help-modal-label' ).text();
117
      $( '#print_content' ).empty();
118
    })
119
  });
120
}
121
 
3239 idir 122
// Activation/Desactivation et contenu de la modale Bootstrap
123
// https://getbootstrap.com/docs/3.3/javascript/#modals
124
function projetHelpModale() {
125
  $( '#info-button' ).click( function ( event ) {
126
    var fileMimeType = $( this ).data( 'mime-info' );
127
 
128
    // Titre
129
    $( '#help-modal-label' ).text( 'Aide du projet : ' +  $( '#titre-projet' ).text() );
130
    if( fileMimeType.match( 'image' ) ) {
131
      var extention = fileMimeType.replace( /(?:imag)?e\/?/g , '' );
132
      $( '#print_content' ).append( '<img src="' + CHEMIN_FICHIERS + 'info.' + extention + '" style="max-width:100%" alt="info projet" />' );
133
    }
134
    // Sortie avec la touche escape
135
    $( '#help-modal' ).modal( { keyboard : true } );
136
    // Affichage
137
    $( '#help-modal' ).modal({ show: true });
138
    // Remplacer l'autofocus qui ne fonctionne plus en HTML5
139
    // Message dans la doc de bootstrap :
140
    // Due to how HTML5 defines its semantics,
141
    // the autofocus HTML attribute has no effect in Bootstrap modals.
142
    // To achieve the same effect, use some custom JavaScript
143
    $( '#help-modal' ).on( 'shown.bs.modal' , function () {
144
      $( '#myInput' ).trigger( 'focus' );
145
    })
146
    // Réinitialisation
147
    $( '#help-modal' ).on( 'hidden.bs.modal' , function () {
148
      $( '#help-modal-label' ).text();
149
      $( '#print_content' ).empty();
150
    });
151
 
152
  });
153
}
154
 
155
 
3208 idir 156
// Faire apparaitre un champ text "Autre"
157
function onOtherOption() {
158
  const PREFIX = 'collect-other-';
159
 
160
  // Ajouter un champ texte pour "Autre"
3260 idir 161
  function optionAdd( otherId, $target, element, dataName ) {
3208 idir 162
    $target.after(
3260 idir 163
      '<div class="control-group">'+
164
        '<label'+
165
          ' for="' + otherId + '"'+
166
          ' class="' + otherId + '"'+
167
        '>'+
168
          'Autre option :'+
169
        '</label>'+
170
        '<input'+
171
          ' type="text"'+
172
          ' id="' + otherId + '"'+
173
          ' name="' + otherId + '"'+
174
          ' class="collect-other form-control"'+
175
          ' data-name="' + dataName + '"'+
176
          ' data-element="' + element + '"'+
177
        '>'+
178
      '</div>'
3208 idir 179
    );
3260 idir 180
    $( '#' + otherId ).focus();
3208 idir 181
  }
182
 
183
  // Supprimer un champ texte pour "Autre"
3260 idir 184
  function optionRemove( otherId ) {
3208 idir 185
    $( '.' + otherId + ', #' + otherId ).remove();
186
  }
187
 
188
  $( '#form-supp .other' ).each( function() {
189
    if( $( this ).hasClass( 'is-select' ) ) {
3260 idir 190
      var dataName = $( this ).data( 'name' ),
191
          otherId  = PREFIX + dataName;
192
 
3208 idir 193
      // Insertion du champ "Autre" après les boutons
3260 idir 194
      optionAdd( otherId, $( this ).parent( '.add-field-select' ), 'select', dataName );
3208 idir 195
    } else if ( $( this ).is( ':checked' ) ) {
3260 idir 196
      var dataName = $( this ).data( 'name' ),
197
          otherId  = PREFIX + dataName,
198
          element = $( this ).data( 'element' );
3208 idir 199
      // Insertion du champ "Autre" après les boutons
3260 idir 200
      optionAdd( otherId, $( this ).parent( 'label' ), element, dataName );
3208 idir 201
    }
202
  });
203
 
3260 idir 204
  $( '#form-supp .select' ).change( function () {
205
    var dataName = $( this ).data( 'name' ),
206
        otherId  = PREFIX + dataName;
3208 idir 207
 
208
    if( 'other' === $( this ).val() ) {
209
        // Insertion du champ "Autre" après les boutons
3260 idir 210
        optionAdd( otherId, $( this ).parent( '.add-field-select' ), 'select', dataName );
3208 idir 211
      } else {
212
        // Suppression du champ autre
3260 idir 213
        optionRemove( otherId );
3208 idir 214
        $( this ).find( '.other' ).val( 'other' );
215
      }
216
  });
217
 
218
  $( '#form-supp input[type=radio]' ).change( function () {
3260 idir 219
    var dataName = $( this ).data( 'name' ),
220
        otherId  = PREFIX + dataName;
3208 idir 221
 
222
    if( 'other' === $( this ).val() ) {
3260 idir 223
      // Insertion du champ "Autre" après les boutons
224
      optionAdd( otherId, $( this ).parent( 'label' ), 'radio', dataName );
3208 idir 225
    } else {
226
      // Suppression du champ autre
3260 idir 227
      optionRemove( otherId );
228
      $( this ).closest( '.radio' ).find( '.other' ).val( 'other' );
3208 idir 229
    }
230
  });
231
 
232
  $( '#form-supp .list-checkbox .other, #form-supp .checkbox .other' ).click( function () {
3260 idir 233
    var dataName = $( this ).data( 'name' ),
234
        otherId  = PREFIX + dataName,
235
        element = $( this ).data( 'element' );
236
 
3208 idir 237
    if( $( this ).is( ':checked' ) ) {
238
        // Insertion du champ "Autre" après les boutons
3260 idir 239
        optionAdd( otherId, $( this ).parent( 'label' ), element, dataName );
3208 idir 240
      } else {
241
        // Suppression du champ autre
3260 idir 242
        optionRemove( otherId );
3208 idir 243
        $( this ).val( 'other' );
244
      }
245
  });
246
}
247
 
248
function collectOtherOption() {
249
  $( '#form-supp' ).on( 'change', '.collect-other', function () {
3260 idir 250
    var otherIdSuffix = $( this ).data( 'name' ).replace( '[]', '' );
251
    var element = $( this ).data( 'element' );
252
 
3208 idir 253
    if ( '' === $( this ).val() ){
254
      if ( 'select' === element ) {
3260 idir 255
        $( '#' + otherIdSuffix ).find( '.other' ).prop( 'selected', false ).val( 'other' );
3208 idir 256
      } else {
3260 idir 257
        $( '#other-' + otherIdSuffix ).prop( 'checked', false ).val( 'other' );
3208 idir 258
      }
3260 idir 259
      $( 'label.collect-other-' + otherIdSuffix ).remove();
260
      $( this ).remove();
3208 idir 261
    } else {
262
      if ( 'select' === element ) {
263
        $( '#' +  otherIdSuffix ).find( '.other' ).val( $( this ).val() );
3260 idir 264
        $( '#' +  otherIdSuffix ).val( $( this ).val() );
3208 idir 265
        $( '#' +  otherIdSuffix + ' option').not( '.other' ).prop( 'selected', false );
266
        $( '#' +  otherIdSuffix ).find( '.other' ).prop( 'selected', true );
267
      } else {
268
        if ( 'radio' === element ) {
269
          $( 'input[name=' + otherIdSuffix + ']' ).not( '#other-' + otherIdSuffix ).prop( 'checked', false );
270
        }
271
        $( '#other-' + otherIdSuffix ).val( $( this ).val() );
272
        $( '#other-' + otherIdSuffix ).prop( 'checked', true );
273
      }
274
    }
275
  });
276
}
277
 
278
/***************************
279
 *  Lancement des scripts  *
280
 ***************************/
3239 idir 281
const CHEMIN_FICHIERS = $( '#zone-appli' ).data('url-fichiers');
282
 
3208 idir 283
jQuery( document ).ready( function() {
3239 idir 284
  // Volet de profil/déconnexion
285
  connexionDprodownMenu();
286
  // Modale "aide" du projet
287
  projetHelpModale();
288
  // Affichage input file
3217 idir 289
  inputFile();
290
  // Affichage des List-checkbox
3208 idir 291
  inputListCheckbox();
3239 idir 292
  // Affichage des Range
293
  inputRangeDisplayNumber()
3217 idir 294
  // Modale "aide"
3239 idir 295
  newFieldsHelpModal();
3217 idir 296
  // Ajout/suppression d'un champ texte "Autre"
3208 idir 297
  onOtherOption();
3239 idir 298
  // Récupérer les données entrées dans "Autre"
3208 idir 299
  collectOtherOption();
300
});