PageSourceSearch

https://www.a1components.com/BVModules/Themes/Foundation4%20Respon…/js/foundation/foundation.forms.js

js a1components.com collected 2026-10-06 22:14:59 UTC 18,750 bytes, 556 lines download raw bytes

1(function ($, window, document, undefined) {
2  'use strict';
3
4  Foundation.libs.forms = {
5    name : 'forms',
6
7    version: '4.3.2',
8
9    cache: {},
10
11    settings: {
12      disable_class: 'no-custom',
13      last_combo : null
14    },
15
16    init: function (scope, method, options) {
17
18      if (typeof method === 'object') {
19        $.extend(true, this.settings, method);
20      }
21
22      if (typeof method !== 'string') {
23        if (!this.settings.init) {
24          this.events();
25        }
26
27        this.assemble();
28
29        return this.settings.init;
30      } else {
31        return this[method].call(this, options);
32      }
33    },
34
35    assemble: function () {
36
37      var forms = this;
38
39      $('form.custom input[type="radio"],[type="checkbox"]', $(this.scope))
40        .not('[data-customforms="disabled"]')
41        .not('.' + this.settings.disable_class)
42        .each(function(idx, sel){
43          forms.set_custom_markup(sel);
44        })
45        .change(function(){
46          forms.set_custom_markup(this);
47        });
48
49      $('form.custom select', $(this.scope))
50        .not('[data-customforms="disabled"]')
51        .not('.' + this.settings.disable_class)
52        .not('[multiple=multiple]')
53        .each(this.append_custom_select);
54    },
55
56    events: function () {
57      var self = this;
58
59      $(this.scope)
60        .on('click.fndtn.forms', 'form.custom span.custom.checkbox', function (e) {
61          e.preventDefault();
62          e.stopPropagation();
63          self.toggle_checkbox($(this));
64        })
65        .on('click.fndtn.forms', 'form.custom span.custom.radio', function (e) {
66          e.preventDefault();
67          e.stopPropagation();
68          self.toggle_radio($(this));
69        })
70        .on('change.fndtn.forms', 'form.custom select', function (e, force_refresh) {
71          if ($(this).is('[data-customforms="disabled"]')) return;
72          self.refresh_custom_select($(this), force_refresh);
73        })
74        .on('click.fndtn.forms', 'form.custom label', function (e) {
75          if ($(e.target).is('label')) {
76            var $associatedElement = $('#' + self.escape($(this).attr('for'))).not('[data-customforms="disabled"]'),
77              $customCheckbox,
78              $customRadio;
79
80            if ($associatedElement.length !== 0) {
81              if ($associatedElement.attr('type') === 'checkbox') {
82                e.preventDefault();
83                $customCheckbox = $(this).find('span.custom.checkbox');
84                //the checkbox might be outside after the label or inside of another element
85                if ($customCheckbox.length === 0) {
86                  $customCheckbox = $associatedElement.add(this).siblings('span.custom.checkbox').first();
87                }
88                self.toggle_checkbox($customCheckbox);
89              } else if ($associatedElement.attr('type') === 'radio') {
90                e.preventDefault();
91                $customRadio = $(this).find('span.custom.radio');
92                //the radio might be outside after the label or inside of another element
93                if ($customRadio.length === 0) {
94                  $customRadio = $associatedElement.add(this).siblings('span.custom.radio').first();
95                }
96                self.toggle_radio($customRadio);
97              }
98            }
99          }
100        })
101        .on('mousedown.fndtn.forms', 'form.custom div.custom.dropdown', function () {
102          return false;
103        })
104        .on('click.fndtn.forms', 'form.custom div.custom.dropdown a.current, form.custom div.custom.dropdown a.selector', function (e) {
105          var $this = $(this),
106              $dropdown = $this.closest('div.custom.dropdown'),
107              $select = getFirstPrevSibling($dropdown, 'select');
108
109          // make sure other dropdowns close
110          if (!$dropdown.hasClass('open')) $(self.scope).trigger('click');
111
112          e.preventDefault();
113          if (false === $select.is(':disabled')) {
114            $dropdown.toggleClass('open');
115
116            if ($dropdown.hasClass('open')) {
117              $(self.scope).on('click.fndtn.forms.customdropdown', function () {
118                $dropdown.removeClass('open');
119                $(self.scope).off('.fndtn.forms.customdropdown');
120              });
121            } else {
122              $(self.scope).on('.fndtn.forms.customdropdown');
123            }
124            return false;
125          }
126        })
127        .on('click.fndtn.forms touchend.fndtn.forms', 'form.custom div.custom.dropdown li', function (e) {
128          var $this = $(this),
129              $customDropdown = $this.closest('div.custom.dropdown'),
130              $select = getFirstPrevSibling($customDropdown, 'select'),
131              selectedIndex = 0;
132
133          e.preventDefault();
134          e.stopPropagation();
135
136          if (!$(this).hasClass('disabled')) {
137            $('div.dropdown').not($customDropdown).removeClass('open');
138
139            var $oldThis = $this.closest('ul')
140              .find('li.selected');
141            $oldThis.removeClass('selected');
142
143            $this.addClass('selected');
144
145            $customDropdown.removeClass('open')
146              .find('a.current')
147              .text($this.text());
148
149            $this.closest('ul').find('li').each(function (index) {
150              if ($this[0] === this) {
151                selectedIndex = index;
152              }
153            });
154            $select[0].selectedIndex = selectedIndex;
155
156            //store the old value in data
157            $select.data('prevalue', $oldThis.html());
158            
159            // Kick off full DOM change event
160            if (typeof (document.createEvent) != 'undefined') {
161              var event = document.createEvent('HTMLEvents');
162              event.initEvent('change', true, true);
163              $select[0].dispatchEvent(event);
164            } else {
165              $select[0].fireEvent('onchange'); // for IE
166            }
167          }
168      });
169
170      $(window).on('keydown', function (e) {
171        var focus = document.activeElement,
172            self = Foundation.libs.forms,
173            dropdown = $('.custom.dropdown'),
174      select = getFirstPrevSibling(dropdown, 'select'),
175      inputs = $('input,select,textarea,button'); // Zepto-compatible jQuery(":input")
176
177        if (dropdown.length > 0 && dropdown.hasClass('open')) {
178          e.preventDefault();
179
180      if (e.which === 9) {
181          $(inputs[$(inputs).index(select) + 1]).focus();
182        dropdown.removeClass('open');
183      }
184
185          if (e.which === 13) {
186            dropdown.find('li.selected').trigger('click');
187          }
188
189          if (e.which === 27) {
190            dropdown.removeClass('open');
191          }
192
193          if (e.which >= 65 && e.which <= 90) {
194            var next = self.go_to(dropdown, e.which),
195                current = dropdown.find('li.selected');
196
197            if (next) {
198              current.removeClass('selected');
199              self.scrollTo(next.addClass('selected'), 300);
200            }
201          }
202
203          if (e.which === 38) {
204            var current = dropdown.find('li.selected'),
205                prev = current.prev(':not(.disabled)');
206
207            if (prev.length > 0) {
208              prev.parent()[0].scrollTop = prev.parent().scrollTop() - self.outerHeight(prev);
209              current.removeClass('selected');
210              prev.addClass('selected');
211            }
212          } else if (e.which === 40) {
213            var current = dropdown.find('li.selected'),
214                next = current.next(':not(.disabled)');
215
216            if (next.length > 0) {
217              next.parent()[0].scrollTop = next.parent().scrollTop() + self.outerHeight(next);
218              current.removeClass('selected');
219              next.addClass('selected');
220            }
221          }
222        }
223      });
224
225    $(window).on('keyup', function (e) {
226          var focus = document.activeElement,
227              dropdown = $('.custom.dropdown');
228
229      if (focus === dropdown.find('.current')[0]) {
230        dropdown.find('.selector').focus().click();
231      }
232    });
233
234      this.settings.init = true;
235    },
236
237    go_to: function (dropdown, character) {
238      var lis = dropdown.find('li'),
239          count = lis.length;
240
241      if (count > 0) {
242        for (var i = 0; i < count; i++) {
243          var first_letter = lis.eq(i).text().charAt(0).toLowerCase();
244          if (first_letter === String.fromCharCode(character).toLowerCase()) return lis.eq(i);
245        }
246      }
247    },
248
249    scrollTo: function (el, duration) {
250      if (duration < 0) return;
251      var parent = el.parent();
252      var li_height = this.outerHeight(el);
253      var difference = (li_height * (el.index())) - parent.scrollTop();
254      var perTick = difference / duration * 10;
255
256      this.scrollToTimerCache = setTimeout(function () {
257        if (!isNaN(parseInt(perTick, 10))) {
258          parent[0].scrollTop = parent.scrollTop() + perTick;
259          this.scrollTo(el, duration - 10);
260        }
261      }.bind(this), 10);
262    },
263
264    set_custom_markup: function (sel) {
265      var $this = $(sel),
266          type = $this.attr('type'),
267          $span = $this.next('span.custom.' + type);
268          
269      if (!$this.parent().hasClass('switch')) {
270        $this.addClass('hidden-field');
271      }
272
273      if ($span.length === 0) {
274        $span = $('<span class="custom ' + type + '"></span>').insertAfter($this);
275      }
276
277      $span.toggleClass('checked', $this.is(':checked'));
278      $span.toggleClass('disabled', $this.is(':disabled'));
279    },
280
281    append_custom_select: function (idx, sel) {
282        var self = Foundation.libs.forms,
283            $this = $(sel),
284            $customSelect = $this.next('div.custom.dropdown'),
285            $customList = $customSelect.find('ul'),
286            $selectCurrent = $customSelect.find(".current"),
287            $selector = $customSelect.find(".selector"),
288            $options = $this.find('option'),
289            $selectedOption = $options.filter(':selected'),
290            copyClasses = $this.attr('class') ? $this.attr('class').split(' ') : [],
291            maxWidth = 0,
292            liHtml = '',
293            $listItems,
294            $currentSelect = false;
295
296        if ($customSelect.length === 0) {
297          var customSelectSize = $this.hasClass('small') ? 'small' : $this.hasClass('medium') ? 'medium' : $this.hasClass('large') ? 'large' : $this.hasClass('expand') ? 'expand' : '';
298
299          $customSelect = $('<div class="' + ['custom', 'dropdown', customSelectSize].concat(copyClasses).filter(function (item, idx, arr) {
300            if (item === '') return false;
301            return arr.indexOf(item) === idx;
302          }).join(' ') + '"><a href="#" class="selector"></a><ul /></div>');
303
304          $selector = $customSelect.find(".selector");
305          $customList = $customSelect.find("ul");
306
307          liHtml = $options.map(function () {
308            var copyClasses = $(this).attr('class') ? $(this).attr('class') : '';
309            return "<li class='" + copyClasses + "'>" + $(this).html() + "</li>";
310          }).get().join('');
311
312          $customList.append(liHtml);
313
314          $currentSelect = $customSelect
315            .prepend('<a href="#" class="current">' + ($selectedOption.html() || '') + '</a>')
316            .find(".current");
317
318          $this.after($customSelect)
319            .addClass('hidden-field');
320        } else {
321          liHtml = $options.map(function () {
322              return "<li>" + $(this).html() + "</li>";
323            })
324            .get().join('');
325
326          $customList.html('')
327            .append(liHtml);
328
329        } // endif $customSelect.length === 0
330
331        self.assign_id($this, $customSelect);
332        $customSelect.toggleClass('disabled', $this.is(':disabled'));
333        $listItems = $customList.find('li');
334
335        // cache list length
336        self.cache[$customSelect.data('id')] = $listItems.length;
337
338        $options.each(function (index) {
339          if (this.selected) {
340            $listItems.eq(index).addClass('selected');
341
342            if ($currentSelect) {
343              $currentSelect.html($(this).html());
344            }
345          }
346          if ($(this).is(':disabled')) {
347            $listItems.eq(index).addClass('disabled');
348          }
349        });
350
351        //
352        // If we're not specifying a predetermined form size.
353        //
354        if (!$customSelect.is('.small, .medium, .large, .expand')) {
355
356          // ------------------------------------------------------------------------------------
357          // This is a work-around for when elements are contained within hidden parents.
358          // For example, when custom-form elements are inside of a hidden reveal modal.
359          //
360          // We need to display the current custom list element as well as hidden parent elements
361          // in order to properly calculate the list item element's width property.
362          // -------------------------------------------------------------------------------------
363
364          $customSelect.addClass('open');
365          //
366          // Quickly, display all parent elements.
367          // This should help us calcualate the width of the list item's within the drop down.
368          //
369          var self = Foundation.libs.forms;
370          self.hidden_fix.adjust($customList);
371
372          maxWidth = (self.outerWidth($listItems) > maxWidth) ? self.outerWidth($listItems) : maxWidth;
373
374          Foundation.libs.forms.hidden_fix.reset();
375
376          $customSelect.removeClass('open');
377
378        } // endif
379
380    },
381
382    assign_id: function ($select, $customSelect) {
383      var id = [+new Date(), Foundation.random_str(5)].join('-');
384      $select.attr('data-id', id);
385      $customSelect.attr('data-id', id);
386    },
387
388    refresh_custom_select: function ($select, force_refresh) {
389      var self = this;
390      var maxWidth = 0,
391          $customSelect = $select.next(),
392          $options = $select.find('option'),
393          $customList = $customSelect.find('ul'),
394          $listItems = $customSelect.find('li');
395
396      if ($options.length !== this.cache[$customSelect.data('id')] || force_refresh) {
397        $customList.html('');
398
399        // rebuild and re-populate all at once
400        var customSelectHtml = '';
401        $options.each(function () {
402          var $this = $(this), thisHtml = $this.html(), thisSelected = this.selected;
403          customSelectHtml += '<li class="' + (thisSelected ? ' selected ' : '') + ($this.is(':disabled') ? ' disabled ' : '') + '">' + thisHtml + '</li>';
404          if (thisSelected) {
405            $customSelect.find('.current').html(thisHtml);
406          }
407        });
408
409        $customList.html(customSelectHtml);
410
411        // fix width
412        $customSelect.removeAttr('style');
413        $customList.removeAttr('style');
414        $customSelect.find('li').each(function () {
415          $customSelect.addClass('open');
416          if (self.outerWidth($(this)) > maxWidth) {
417            maxWidth = self.outerWidth($(this));
418          }
419          $customSelect.removeClass('open');
420        });
421
422        $listItems = $customSelect.find('li');
423        // cache list length
424        this.cache[$customSelect.data('id')] = $listItems.length;
425      }
426    },
427    
428    refresh_custom_selection: function ($select) {
429      var selectedValue = $('option:selected', $select).text();
430      $('a.current', $select.next()).text(selectedValue);
431    },
432
433    toggle_checkbox: function ($element) {
434      var $input = $element.prev(),
435          input = $input[0];
436
437      if (false === $input.is(':disabled')) {
438        input.checked = ((input.checked) ? false : true);
439        $element.toggleClass('checked');
440
441        $input.trigger('change');
442      }
443    },
444
445    toggle_radio: function ($element) {
446        var $input = $element.prev(),
447            $form = $input.closest('form.custom'),
448            input = $input[0];
449
450        if (false === $input.is(':disabled')) {
451          $form.find('input[type="radio"][name="' + this.escape($input.attr('name')) + '"]')
452            .next().not($element).removeClass('checked');
453
454          if (!$element.hasClass('checked')) {
455            $element.toggleClass('checked');
456          }
457
458          input.checked = $element.hasClass('checked');
459
460          $input.trigger('change');
461        }
462    },
463
464    escape: function (text) {
465      if (!text) return '';
466      return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
467    },
468
469    hidden_fix: {
470        /**
471         * Sets all hidden parent elements and self to visibile.
472         *
473         * @method adjust
474         * @param {jQuery Object} $child
475         */
476
477        // We'll use this to temporarily store style properties.
478        tmp: [],
479
480        // We'll use this to set hidden parent elements.
481        hidden: null,
482
483        adjust: function ($child) {
484          // Internal reference.
485          var _self = this;
486
487          // Set all hidden parent elements, including this element.
488          _self.hidden = $child.parents();
489          _self.hidden = _self.hidden.add($child).filter(":hidden");
490
491          // Loop through all hidden elements.
492          _self.hidden.each(function () {
493
494            // Cache the element.
495            var $elem = $(this);
496
497            // Store the style attribute.
498            // Undefined if element doesn't have a style attribute.
499            _self.tmp.push($elem.attr('style'));
500
501            // Set the element's display property to block,
502            // but ensure it's visibility is hidden.
503            $elem.css({
504                'visibility': 'hidden',
505                'display': 'block'
506            });
507          });
508
509        }, // end adjust
510
511        /**
512         * Resets the elements previous state.
513         *
514         * @method reset
515         */
516        reset: function () {
517          // Internal reference.
518          var _self = this;
519          // Loop through our hidden element collection.
520          _self.hidden.each(function (i) {
521            // Cache this element.
522            var $elem = $(this),
523                _tmp = _self.tmp[i]; // Get the stored 'style' value for this element.
524
525            // If the stored value is undefined.
526            if (_tmp === undefined)
527            // Remove the style attribute.
528            $elem.removeAttr('style');
529            else
530            // Otherwise, reset the element style attribute.
531            $elem.attr('style', _tmp);
532          });
533          // Reset the tmp array.
534          _self.tmp = [];
535          // Reset the hidden elements variable.
536          _self.hidden = null;
537
538        } // end reset
539    },
540
541    off: function () {
542      $(this.scope).off('.fndtn.forms');
543    },
544
545    reflow : function () {}
546  };
547
548  var getFirstPrevSibling = function($el, selector) {
549    var $el = $el.prev();
550    while ($el.length) {
551      if ($el.is(selector)) return $el;
552      $el = $el.prev();
553    }
554    return $();
555  };
556}(Foundation.zj, this, this.document));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.