Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Details | 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
 
8
// Logique d'affichage pour le input type=file
9
function inputFile( countFiles ) {
10
  // Initialisation des variables
11
  var $fileInput  = $( '.input-file' ),
12
      $button     = $( '.label-file' );
13
  // Action lorsque la "barre d'espace" ou "Entrée" est pressée
14
  $( '.label-file' ).keydown( function( event ) {
15
    if ( event.keyCode == 13 || event.keyCode == 32 ) {
16
      $( '#' + $( this ).attr( 'for' ) + '.input-file' ).click();
17
    }
18
  });
19
  // // Affiche un retour visuel dès que input:file change
20
  // $('#form-supp').on( 'change', '.input-file', function( event ) {
21
  //   // Il est possible de supprimer un fichier
22
  //   // donc on vérifie que le 'change' est un ajout ou modification
23
  //   if( !$.isEmptyObject( event.target.files[0] ) ) {
24
  //     var file = event.target.files[0],
25
  //         fileId = $( this ).attr( 'id' ),
26
  //         $thisFile = $( this ).parent('.label-file.' + fileId ),
27
  //         $imageContainer = $( '#miniatures' ),
28
  //         $theReturn = $( '.' + fileId  + 'Img') || false,
29
  //         fileImgHtml = '';
30
 
31
 
32
  //     if( file.type.match( 'image' ) ) {
33
  //       fileImgHtml =
34
  //         '<div class="' + fileId + 'Img mb-1">'+
35
  //           '<p> ' + file.name + '</p>'+
36
  //           '<img src="' + URL.createObjectURL( file ) + '">'+
37
  //         '</div>';
38
  //     }
39
  //     // Permettre d'enregistrer une nouvelle image
40
  //     if( 0 < $theReturn.length ) {
41
  //       // Changement du fichier
42
  //       $theReturn.html( fileImgHtml );
43
  //     } else {
44
  //       $imageContainer.append( fileImgHtml );
45
  //       $imageContainer.append( $thisFile  );
46
 
47
  //       $( '#photos-conteneur' ).html(
48
  //           '<label for="fichier' + countFiles + '" class="label-file btn btn-default fichier' + countFiles + '">'+
49
  //             '<i class="fas fa-download"></i> Ajouter une image'+
50
  //             '<input type="file" id="fichier' + countFiles + '" name="fichier' + countFiles + '" class="input-file" accept="image/jpeg">'+
51
  //             '<input type="hidden" name="MAX_FILE_SIZE" value="5242880">'+
52
  //           '</label>'+
53
  //           '<hr>'
54
  //       );
55
  //       countFiles++;
56
  //     }
57
  //     // Changer le text
58
  //     $thisFile.find( '.label-text').html( '<i class="fas fa-exchange-alt"></i> Changer cette image');
59
  //     $thisFile.css( 'background-color', '#ea9973' );
60
  //     $thisFile.hover( function() {
61
  //       $( this ).css( 'background-color', 'rgba(234, 153, 115, 0.7)' );
62
  //     });
63
  //     $( '.' + fileId  + 'Img img').attr( 'width', $thisFile.outerWidth() );
64
 
65
  //   }
66
  // });
67
  // // Annuler le téléchargement
68
  // $( '.remove-file' ).click( function() {
69
  //   var $thisFileInput = $( this ).prev( '.input-file-container' ).find( '.input-file' );
70
  //   $thisFileInput.wrap( '<form>' ).closest( 'form' ).get(0).reset();
71
  //   $thisFileInput.triggerHandler( 'change' );
72
  //   $thisFileInput.unwrap();
73
  //   $( this ).next( '.file-return' ).addClass( 'hidden' ).empty();
74
  // });
75
}
76
 
77
// Style et affichage des list-checkboxes
78
function inputListCheckbox() {
79
  // On écoute le click sur une list-checkbox ('.selectBox')
80
  // à tout moment de son insertion dans le dom
81
  // Todo :
82
  // _ S'assurer de bien viser la bonne list-checkbox
83
  // _ Au click sur un autre champ remballer la list-checkbox
84
  $( document ).click( function( event ) {
85
    var target = event.target;
86
 
87
    if ( !$( target ).is( '.overSelect' ) && 0 === $( target ).closest( '.checkboxes' ).length ) {
88
      $( '.checkboxes' ).each( function () {
89
        $( this ).addClass( 'hidden' );
90
      });
91
      $( '.selectBox select.focus' ).each( function() {
92
        $( this ).removeClass( 'focus' );
93
      });
94
    }
95
  });
96
 
97
  $( '#zone-appli' ).on( 'click' , '.selectBox' , function() {
98
    // $( '.checkboxes[data-id="' + $(this).attr( 'data-id' ) + '"]' ).toggleClass( 'hidden' );
99
    $( this ).next().toggleClass( 'hidden' );
100
    $( this ).find( 'select' ).toggleClass( 'focus' );
101
 
102
  });
103
}
104
 
105
// Style et affichage des input type="range"
106
function inputRangeDisplayNumber() {
107
  $( '#zone-supp' ).on( 'input' , '.range input[type="range"]' , function () {
108
    $( this ).next( 'input[type="number"]' ).val ( $( this ).val() );
109
  });
110
  $( '#zone-supp' ).on( 'input' , '.range input[type="number"]' , function () {
111
    $( this ).prev( 'input[type="range"]' ).val ( $( this ).val() );
112
  });
113
}
114
 
115
// Activation/Desactivation et contenu de la modale Bootstrap
116
// https://getbootstrap.com/docs/3.3/javascript/#modals
117
function previewFieldHelpModal() {
118
  $( '#zone-supp' ).on( 'click' , '.help-button' , function ( event ) {
119
    var index = $( this ).closest( '.preview-fields' ).attr( 'data-id' ),
120
        thisFieldset = $( '.new-field[data-id="' + index + '"]' ),
121
        file = $( '.field-help' , thisFieldset )[0].files[0],
122
        tmppath = URL.createObjectURL( file );
123
    // Titre
124
    $( '#help-modal-label' ).text( 'Aide pour : ' +  $( '.field-name' , thisFieldset ).val() );
125
    // Contenu
126
    if( file.type.match( 'image' ) ) {
127
        $( '#print_content' ).append( '<img src="' + tmppath + '" style="max-width:100%">' );
128
    } else if( file.type.match( 'pdf' ) ) {
129
        $( '#print_content' ).append( '<iframe src="' + tmppath + '" width="100%" height="650" align="middle" scrolling="no" frameborder="0"></iframe>' );
130
    }
131
    // Sortie avec la touche escape
132
    $( '#help-modal' ).modal( { keyboard : true } );
133
    // Affichage
134
    $( '#help-modal' ).modal( 'show' );
135
    // Remplacer l'autofocus qui ne fonctionne plus en HTML5
136
    // Message dans la doc de bootstrap :
137
    // Due to how HTML5 defines its semantics,
138
    // the autofocus HTML attribute has no effect in Bootstrap modals.
139
    // To achieve the same effect, use some custom JavaScript
140
    $( '#help-modal' ).on( 'shown.bs.modal' , function () {
141
      $( '#myInput' ).trigger( 'focus' );
142
    })
143
    // Réinitialisation
144
    $( '#help-modal' ).on( 'hidden.bs.modal' , function () {
145
      $( '#help-modal-label' ).text();
146
      $( '#print_content' ).empty();
147
    })
148
  });
149
}
150
 
151
// Faire apparaitre un champ text "Autre"
152
function onOtherOption() {
153
 
154
  const PREFIX = 'collect-other-';
155
 
156
  // Ajouter un champ texte pour "Autre"
157
  function optionAdd( otherId, $target, element ) {
158
    $target.after(
159
      '<label for="' + otherId + '" class="' + otherId + '">Autre option :</label>' +
160
      '<input type="text" id="' + otherId + '" name="' + otherId + '" class="collect-other" data-element="' + element + '">'
161
    );
162
  }
163
 
164
  // Supprimer un champ texte pour "Autre"
165
  function optionRemove( otherId, $this ) {
166
    $( '.' + otherId + ', #' + otherId ).remove();
167
  }
168
 
169
  $( '#form-supp .other' ).each( function() {
170
    if( $( this ).hasClass( 'is-select' ) ) {
171
      var otherId = PREFIX + $( this ).parent( 'select' ).attr( 'name' );
172
      // Insertion du champ "Autre" après les boutons
173
      optionAdd( otherId, $( this ).parent( '.select' ) , 'select' );
174
    } else if ( $( this ).is( ':checked' ) ) {
175
      var otherId = PREFIX + $( this ).attr( 'name' ),
176
          element = $( this ).attr( 'data-element' );
177
      // Insertion du champ "Autre" après les boutons
178
      optionAdd( otherId, $( this ).parent( 'label' ) , element );
179
    }
180
  });
181
 
182
  $( '#form-supp select' ).change( function () {
183
    var otherId = PREFIX + $( this ).attr( 'name' );
184
 
185
    if( 'other' === $( this ).val() ) {
186
        // Insertion du champ "Autre" après les boutons
187
        optionAdd( otherId, $( this ).parent( '.select' ), 'select' );
188
      } else {
189
        // Suppression du champ autre
190
        optionRemove( otherId, $( this ) );
191
        $( this ).find( '.other' ).val( 'other' );
192
      }
193
  });
194
 
195
  $( '#form-supp input[type=radio]' ).change( function () {
196
    var otherId = PREFIX + $( this ).attr( 'name' );
197
 
198
    if( 'other' === $( this ).val() ) {
199
        // Insertion du champ "Autre" après les boutons
200
        optionAdd( otherId, $( this ).parent( 'label' ), 'radio' );
201
    } else {
202
      // Suppression du champ autre
203
      optionRemove( otherId, $( this ) );
204
      $( this ).closest( 'div.control-group.radio' ).find( '.other' ).val( 'other' );
205
    }
206
  });
207
 
208
  $( '#form-supp .list-checkbox .other, #form-supp .checkbox .other' ).click( function () {
209
    var otherId = PREFIX + $( this ).attr( 'name' ),
210
        element = $( this ).attr( 'data-element' );
211
    // console.log(element);
212
    if( $( this ).is( ':checked' ) ) {
213
        // Insertion du champ "Autre" après les boutons
214
        optionAdd( otherId, $( this ).parent( 'label' ), element );
215
      } else {
216
        // Suppression du champ autre
217
        optionRemove( otherId, $( this ) );
218
        $( this ).val( 'other' );
219
      }
220
  });
221
}
222
 
223
function collectOtherOption() {
224
  $( '#form-supp' ).on( 'change', '.collect-other', function () {
225
    var otherIdSuffix = $( this ).attr( 'name' ).replace( 'collect-other-', '' ),
226
        element = $( this ).attr( 'data-element' );
227
    if ( '' === $( this ).val() ){
228
      if ( 'select' === element ) {
229
        $( '#' + otherIdSuffix ).find( '.other' ).prop( 'selected', false );
230
      } else {
231
        $( '#other-' + otherIdSuffix ).prop( 'checked', false );
232
      }
233
    } else {
234
      if ( 'select' === element ) {
235
        $( '#' +  otherIdSuffix ).find( '.other' ).val( $( this ).val() );
236
        $( '#' +  otherIdSuffix + ' option').not( '.other' ).prop( 'selected', false );
237
        $( '#' +  otherIdSuffix ).find( '.other' ).prop( 'selected', true );
238
      } else {
239
        if ( 'radio' === element ) {
240
          $( 'input[name=' + otherIdSuffix + ']' ).not( '#other-' + otherIdSuffix ).prop( 'checked', false );
241
        }
242
        // console.log( otherIdSuffix );
243
        $( '#other-' + otherIdSuffix ).val( $( this ).val() );
244
        $( '#other-' + otherIdSuffix ).prop( 'checked', true );
245
      }
246
 
247
    }
248
  });
249
}
250
 
251
/***************************
252
 *  Lancement des scripts  *
253
 ***************************/
254
jQuery( document ).ready( function() {
255
  // var countFiles = 2;
256
  // // Affichage des images ou nom des documents importés
257
  // inputFile( countFiles );
258
  // // Affichage des List-checkbox
259
  inputListCheckbox();
260
  // // Affichage des Range
261
  // inputRangeDisplayNumber()
262
  // // Modale "aide"
263
  // previewFieldHelpModal();
264
  // // Ajout/suppression d'un champ texte "Autre"
265
  // if( $( '.option-other-value' , thisFieldset ).is( ':checked' ) ) {
266
  onOtherOption();
267
  collectOtherOption();
268
  // }
269
});