Blame view

bower_components/Materialize/js/forms.js 14.6 KB
74249687   Luigi Serra   Cross browser con...
1
2
3
4
5
6
7
  (function ($) {
    $(document).ready(function() {
  
      // Function to update labels of text fields
      Materialize.updateTextFields = function() {
        var input_selector = 'input[type=text], input[type=password], input[type=email], input[type=url], input[type=tel], input[type=number], input[type=search], textarea';
        $(input_selector).each(function(index, element) {
c5169e0e   Renato De Donato   a new hope
8
9
          if ($(element).val().length > 0 || $(this).attr('placeholder') !== undefined || $(element)[0].validity.badInput === true) {
            $(this).siblings('label').addClass('active');
74249687   Luigi Serra   Cross browser con...
10
11
12
13
14
15
16
17
18
19
          }
          else {
            $(this).siblings('label, i').removeClass('active');
          }
        });
      };
  
      // Text based inputs
      var input_selector = 'input[type=text], input[type=password], input[type=email], input[type=url], input[type=tel], input[type=number], input[type=search], textarea';
  
c5169e0e   Renato De Donato   a new hope
20
21
22
      // Handle HTML5 autofocus
      $('input[autofocus]').siblings('label, i').addClass('active');
  
74249687   Luigi Serra   Cross browser con...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      // Add active if form auto complete
      $(document).on('change', input_selector, function () {
        if($(this).val().length !== 0 || $(this).attr('placeholder') !== undefined) {
          $(this).siblings('label').addClass('active');
        }
        validate_field($(this));
      });
  
      // Add active if input element has been pre-populated on document ready
      $(document).ready(function() {
        Materialize.updateTextFields();
      });
  
      // HTML DOM FORM RESET handling
      $(document).on('reset', function(e) {
        var formReset = $(e.target);
        if (formReset.is('form')) {
          formReset.find(input_selector).removeClass('valid').removeClass('invalid');
          formReset.find(input_selector).each(function () {
            if ($(this).attr('value') === '') {
              $(this).siblings('label, i').removeClass('active');
            }
          });
  
          // Reset select
          formReset.find('select.initialized').each(function () {
            var reset_text = formReset.find('option[selected]').text();
            formReset.siblings('input.select-dropdown').val(reset_text);
          });
        }
      });
  
      // Add active when element has focus
      $(document).on('focus', input_selector, function () {
        $(this).siblings('label, i').addClass('active');
      });
  
      $(document).on('blur', input_selector, function () {
        var $inputElement = $(this);
        if ($inputElement.val().length === 0 && $inputElement[0].validity.badInput !== true && $inputElement.attr('placeholder') === undefined) {
          $inputElement.siblings('label, i').removeClass('active');
        }
  
        if ($inputElement.val().length === 0 && $inputElement[0].validity.badInput !== true && $inputElement.attr('placeholder') !== undefined) {
          $inputElement.siblings('i').removeClass('active');
        }
        validate_field($inputElement);
      });
  
      window.validate_field = function(object) {
        var hasLength = object.attr('length') !== undefined;
        var lenAttr = parseInt(object.attr('length'));
        var len = object.val().length;
  
        if (object.val().length === 0 && object[0].validity.badInput === false) {
          if (object.hasClass('validate')) {
            object.removeClass('valid');
            object.removeClass('invalid');
          }
        }
        else {
          if (object.hasClass('validate')) {
            // Check for character counter attributes
            if ((object.is(':valid') && hasLength && (len <= lenAttr)) || (object.is(':valid') && !hasLength)) {
              object.removeClass('invalid');
              object.addClass('valid');
            }
            else {
              object.removeClass('valid');
              object.addClass('invalid');
            }
          }
        }
      };
  
  
      // Textarea Auto Resize
      var hiddenDiv = $('.hiddendiv').first();
      if (!hiddenDiv.length) {
        hiddenDiv = $('<div class="hiddendiv common"></div>');
        $('body').append(hiddenDiv);
      }
      var text_area_selector = '.materialize-textarea';
  
      function textareaAutoResize($textarea) {
        // Set font properties of hiddenDiv
  
        var fontFamily = $textarea.css('font-family');
        var fontSize = $textarea.css('font-size');
  
        if (fontSize) { hiddenDiv.css('font-size', fontSize); }
        if (fontFamily) { hiddenDiv.css('font-family', fontFamily); }
  
        if ($textarea.attr('wrap') === "off") {
          hiddenDiv.css('overflow-wrap', "normal")
                   .css('white-space', "pre");
        }
  
c5169e0e   Renato De Donato   a new hope
121
122
123
  
  
  
74249687   Luigi Serra   Cross browser con...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        hiddenDiv.text($textarea.val() + '\n');
        var content = hiddenDiv.html().replace(/\n/g, '<br>');
        hiddenDiv.html(content);
  
  
        // When textarea is hidden, width goes crazy.
        // Approximate with half of window size
  
        if ($textarea.is(':visible')) {
          hiddenDiv.css('width', $textarea.width());
        }
        else {
          hiddenDiv.css('width', $(window).width()/2);
        }
  
        $textarea.css('height', hiddenDiv.height());
      }
  
      $(text_area_selector).each(function () {
        var $textarea = $(this);
        if ($textarea.val().length) {
          textareaAutoResize($textarea);
        }
      });
  
      $('body').on('keyup keydown autoresize', text_area_selector, function () {
        textareaAutoResize($(this));
      });
  
c5169e0e   Renato De Donato   a new hope
153
  
74249687   Luigi Serra   Cross browser con...
154
      // File Input Path
c5169e0e   Renato De Donato   a new hope
155
  
74249687   Luigi Serra   Cross browser con...
156
157
158
159
160
161
162
163
164
165
166
167
      $(document).on('change', '.file-field input[type="file"]', function () {
        var file_field = $(this).closest('.file-field');
        var path_input = file_field.find('input.file-path');
        var files      = $(this)[0].files;
        var file_names = [];
        for (var i = 0; i < files.length; i++) {
          file_names.push(files[i].name);
        }
        path_input.val(file_names.join(", "));
        path_input.trigger('change');
      });
  
c5169e0e   Renato De Donato   a new hope
168
  
74249687   Luigi Serra   Cross browser con...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
      /****************
      *  Range Input  *
      ****************/
  
      var range_type = 'input[type=range]';
      var range_mousedown = false;
      var left;
  
      $(range_type).each(function () {
        var thumb = $('<span class="thumb"><span class="value"></span></span>');
        $(this).after(thumb);
      });
  
      var range_wrapper = '.range-field';
      $(document).on('change', range_type, function(e) {
        var thumb = $(this).siblings('.thumb');
        thumb.find('.value').html($(this).val());
      });
  
      $(document).on('input mousedown touchstart', range_type, function(e) {
        var thumb = $(this).siblings('.thumb');
74249687   Luigi Serra   Cross browser con...
190
191
192
193
  
        // If thumb indicator does not exist yet, create it
        if (thumb.length <= 0) {
          thumb = $('<span class="thumb"><span class="value"></span></span>');
c5169e0e   Renato De Donato   a new hope
194
          $(this).append(thumb);
74249687   Luigi Serra   Cross browser con...
195
196
197
198
199
200
201
202
203
204
205
206
        }
  
        // Set indicator value
        thumb.find('.value').html($(this).val());
  
        range_mousedown = true;
        $(this).addClass('active');
  
        if (!thumb.hasClass('active')) {
          thumb.velocity({ height: "30px", width: "30px", top: "-20px", marginLeft: "-15px"}, { duration: 300, easing: 'easeOutExpo' });
        }
  
c5169e0e   Renato De Donato   a new hope
207
208
209
210
211
        if(e.pageX === undefined || e.pageX === null){//mobile
           left = e.originalEvent.touches[0].pageX - $(this).offset().left;
        }
        else{ // desktop
           left = e.pageX - $(this).offset().left;
74249687   Luigi Serra   Cross browser con...
212
        }
c5169e0e   Renato De Donato   a new hope
213
        var width = $(this).outerWidth();
74249687   Luigi Serra   Cross browser con...
214
  
c5169e0e   Renato De Donato   a new hope
215
216
217
218
219
220
221
        if (left < 0) {
          left = 0;
        }
        else if (left > width) {
          left = width;
        }
        thumb.addClass('active').css('left', left);
74249687   Luigi Serra   Cross browser con...
222
        thumb.find('.value').html($(this).val());
c5169e0e   Renato De Donato   a new hope
223
224
  
  
74249687   Luigi Serra   Cross browser con...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
      });
  
      $(document).on('mouseup touchend', range_wrapper, function() {
        range_mousedown = false;
        $(this).removeClass('active');
      });
  
      $(document).on('mousemove touchmove', range_wrapper, function(e) {
        var thumb = $(this).children('.thumb');
        var left;
        if (range_mousedown) {
          if (!thumb.hasClass('active')) {
            thumb.velocity({ height: '30px', width: '30px', top: '-20px', marginLeft: '-15px'}, { duration: 300, easing: 'easeOutExpo' });
          }
          if (e.pageX === undefined || e.pageX === null) { //mobile
            left = e.originalEvent.touches[0].pageX - $(this).offset().left;
          }
          else{ // desktop
            left = e.pageX - $(this).offset().left;
          }
          var width = $(this).outerWidth();
  
          if (left < 0) {
            left = 0;
          }
          else if (left > width) {
            left = width;
          }
          thumb.addClass('active').css('left', left);
          thumb.find('.value').html(thumb.siblings(range_type).val());
        }
      });
  
      $(document).on('mouseout touchleave', range_wrapper, function() {
        if (!range_mousedown) {
  
          var thumb = $(this).children('.thumb');
  
          if (thumb.hasClass('active')) {
            thumb.velocity({ height: '0', width: '0', top: '10px', marginLeft: '-6px'}, { duration: 100 });
          }
          thumb.removeClass('active');
        }
      });
c5169e0e   Renato De Donato   a new hope
269
  
74249687   Luigi Serra   Cross browser con...
270
271
    }); // End of $(document).ready
  
c5169e0e   Renato De Donato   a new hope
272
273
274
275
  
  
  
    // Select Plugin
74249687   Luigi Serra   Cross browser con...
276
277
    $.fn.material_select = function (callback) {
      $(this).each(function(){
c5169e0e   Renato De Donato   a new hope
278
        $select = $(this);
74249687   Luigi Serra   Cross browser con...
279
  
c5169e0e   Renato De Donato   a new hope
280
        if ( $select.hasClass('browser-default')) {
74249687   Luigi Serra   Cross browser con...
281
282
283
          return; // Continue to next (return false breaks out of entire loop)
        }
  
c5169e0e   Renato De Donato   a new hope
284
285
        // Tear down structure if Select needs to be rebuilt
        var lastID = $select.data('select-id');
74249687   Luigi Serra   Cross browser con...
286
287
288
289
290
291
292
293
294
295
        if (lastID) {
          $select.parent().find('span.caret').remove();
          $select.parent().find('input').remove();
  
          $select.unwrap();
          $('ul#select-options-'+lastID).remove();
        }
  
        // If destroying the select, remove the selelct-id and reset it to it's uninitialized state.
        if(callback === 'destroy') {
c5169e0e   Renato De Donato   a new hope
296
297
            $select.data('select-id', null).removeClass('initialized');
            return;
74249687   Luigi Serra   Cross browser con...
298
299
300
301
302
303
        }
  
        var uniqueID = Materialize.guid();
        $select.data('select-id', uniqueID);
        var wrapper = $('<div class="select-wrapper"></div>');
        wrapper.addClass($select.attr('class'));
c5169e0e   Renato De Donato   a new hope
304
305
        var options = $('<ul id="select-options-' + uniqueID+'" class="dropdown-content select-dropdown"></ul>');
        var selectOptions = $select.children('option');
74249687   Luigi Serra   Cross browser con...
306
  
c5169e0e   Renato De Donato   a new hope
307
308
309
310
311
312
313
        var label;
        if ($select.find('option:selected') !== undefined) {
          label = $select.find('option:selected');
        }
        else {
          label = options.first();
        }
74249687   Luigi Serra   Cross browser con...
314
  
a1a3bc73   Luigi Serra   graphs updates
315
  
c5169e0e   Renato De Donato   a new hope
316
317
318
319
320
        // Create Dropdown structure
        selectOptions.each(function () {
          // Add disabled attr if disabled
          options.append($('<li class="' + (($(this).is(':disabled')) ? 'disabled' : '') + '"><span>' + $(this).html() + '</span></li>'));
        });
74249687   Luigi Serra   Cross browser con...
321
  
74249687   Luigi Serra   Cross browser con...
322
  
c5169e0e   Renato De Donato   a new hope
323
324
325
        options.find('li').each(function (i) {
          var $curr_select = $select;
          $(this).click(function () {
74249687   Luigi Serra   Cross browser con...
326
            // Check if option element is disabled
c5169e0e   Renato De Donato   a new hope
327
328
            if (!$(this).hasClass('disabled')) {
              $curr_select.find('option').eq(i).prop('selected', true);
74249687   Luigi Serra   Cross browser con...
329
              // Trigger onchange() event
c5169e0e   Renato De Donato   a new hope
330
331
              $curr_select.trigger('change');
              $curr_select.siblings('input.select-dropdown').val($(this).text());
74249687   Luigi Serra   Cross browser con...
332
333
              if (typeof callback !== 'undefined') callback();
            }
a1a3bc73   Luigi Serra   graphs updates
334
          });
c5169e0e   Renato De Donato   a new hope
335
  
74249687   Luigi Serra   Cross browser con...
336
337
338
339
340
341
        });
  
        // Wrap Elements
        $select.wrap(wrapper);
        // Add Select Display Element
        var dropdownIcon = $('<span class="caret">&#9660;</span>');
c5169e0e   Renato De Donato   a new hope
342
        if ( $select.is(':disabled') )
74249687   Luigi Serra   Cross browser con...
343
344
345
          dropdownIcon.addClass('disabled');
  
        // escape double quotes
c5169e0e   Renato De Donato   a new hope
346
        var sanitizedLabelHtml = label.html().replace(/"/g, '&quot;');
74249687   Luigi Serra   Cross browser con...
347
348
349
350
351
  
        var $newSelect = $('<input type="text" class="select-dropdown" readonly="true" ' + (($select.is(':disabled')) ? 'disabled' : '') + ' data-activates="select-options-' + uniqueID +'" value="'+ sanitizedLabelHtml +'"/>');
        $select.before($newSelect);
        $newSelect.before(dropdownIcon);
  
c5169e0e   Renato De Donato   a new hope
352
        $('body').append(options);
74249687   Luigi Serra   Cross browser con...
353
354
        // Check if section element is disabled
        if (!$select.is(':disabled')) {
c5169e0e   Renato De Donato   a new hope
355
          $newSelect.dropdown({"hover": false});
74249687   Luigi Serra   Cross browser con...
356
357
358
359
360
361
362
363
364
        }
  
        // Copy tabindex
        if ($select.attr('tabindex')) {
          $($newSelect[0]).attr('tabindex', $select.attr('tabindex'));
        }
  
        $select.addClass('initialized');
  
c5169e0e   Renato De Donato   a new hope
365
366
367
368
369
370
371
        $newSelect.on('focus', function(){
          $(this).trigger('open');
          label = $(this).val();
          selectedOption = options.find('li').filter(function() {
            return $(this).text().toLowerCase() === label.toLowerCase();
          })[0];
          activateOption(options, selectedOption);
74249687   Luigi Serra   Cross browser con...
372
373
        });
  
c5169e0e   Renato De Donato   a new hope
374
375
        $newSelect.on('blur', function(){
          $(this).trigger('close');
74249687   Luigi Serra   Cross browser con...
376
377
        });
  
74249687   Luigi Serra   Cross browser con...
378
379
        // Make option as selected and scroll to selected position
        activateOption = function(collection, newOption) {
c5169e0e   Renato De Donato   a new hope
380
381
382
          collection.find('li.active').removeClass('active');
          $(newOption).addClass('active');
          collection.scrollTo(newOption);
74249687   Luigi Serra   Cross browser con...
383
384
385
386
        };
  
        // Allow user to search by typing
        // this array is cleared after 1 second
c5169e0e   Renato De Donato   a new hope
387
        filterQuery = [];
74249687   Luigi Serra   Cross browser con...
388
  
c5169e0e   Renato De Donato   a new hope
389
390
391
392
393
394
        onKeyDown = function(event){
          // TAB - switch to another input
          if(event.which == 9){
            $newSelect.trigger('close');
            return;
          }
74249687   Luigi Serra   Cross browser con...
395
  
c5169e0e   Renato De Donato   a new hope
396
397
398
399
400
          // ARROW DOWN WHEN SELECT IS CLOSED - open select options
          if(event.which == 40 && !options.is(":visible")){
            $newSelect.trigger('open');
            return;
          }
74249687   Luigi Serra   Cross browser con...
401
  
c5169e0e   Renato De Donato   a new hope
402
403
404
405
          // ENTER WHEN SELECT IS CLOSED - submit form
          if(event.which == 13 && !options.is(":visible")){
            return;
          }
74249687   Luigi Serra   Cross browser con...
406
  
c5169e0e   Renato De Donato   a new hope
407
          event.preventDefault();
74249687   Luigi Serra   Cross browser con...
408
  
c5169e0e   Renato De Donato   a new hope
409
410
411
412
413
          // CASE WHEN USER TYPE LETTERS
          letter = String.fromCharCode(event.which).toLowerCase();
          var nonLetters = [9,13,27,38,40];
          if (letter && (nonLetters.indexOf(event.which) === -1)){
            filterQuery.push(letter);
74249687   Luigi Serra   Cross browser con...
414
  
c5169e0e   Renato De Donato   a new hope
415
            string = filterQuery.join("");
74249687   Luigi Serra   Cross browser con...
416
  
c5169e0e   Renato De Donato   a new hope
417
418
419
            newOption = options.find('li').filter(function() {
              return $(this).text().toLowerCase().indexOf(string) === 0;
            })[0];
74249687   Luigi Serra   Cross browser con...
420
  
c5169e0e   Renato De Donato   a new hope
421
422
423
424
            if(newOption){
              activateOption(options, newOption);
            }
          }
74249687   Luigi Serra   Cross browser con...
425
  
c5169e0e   Renato De Donato   a new hope
426
427
428
429
430
431
432
433
          // ENTER - select option and close when select options are opened
          if(event.which == 13){
            activeOption = options.find('li.active:not(.disabled)')[0];
            if(activeOption){
              $(activeOption).trigger('click');
              $newSelect.trigger('close');
            }
          }
74249687   Luigi Serra   Cross browser con...
434
  
c5169e0e   Renato De Donato   a new hope
435
436
437
438
439
440
441
          // ARROW DOWN - move to next not disabled option
          if(event.which == 40){
            newOption = options.find('li.active').next('li:not(.disabled)')[0];
            if(newOption){
              activateOption(options, newOption);
            }
          }
74249687   Luigi Serra   Cross browser con...
442
  
c5169e0e   Renato De Donato   a new hope
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
          // ESC - close options
          if(event.which == 27){
            $newSelect.trigger('close');
          }
  
          // ARROW UP - move to previous not disabled option
          if(event.which == 38){
            newOption = options.find('li.active').prev('li:not(.disabled)')[0];
            if(newOption){
              activateOption(options, newOption);
            }
          }
  
          // Automaticaly clean filter query so user can search again by starting letters
          setTimeout(function(){ filterQuery = []; }, 1000);
        };
  
        $newSelect.on('keydown', onKeyDown);
      });
74249687   Luigi Serra   Cross browser con...
462
463
464
    };
  
  }( jQuery ));