Subversion Repositories eFlore/Applications.cel

Rev

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