PageSourceSearch

https://www.hemmingen.de/fileadmin/templates/js/libs/parsley-2.8.1/src/parsley/factory.js

js hemmingen.de collected 2026-10-02 16:48:30 UTC 6,171 bytes, 170 lines download raw bytes

1import '../../../jquery/jquery-3.3.1.js'
2import Utils from './utils.js';
3import Base from './base.js';
4import Form from './form.js';
5import Field from './field.js';
6import Multiple from './multiple.js';
7
8var Factory = function (element, options, parsleyFormInstance) {
9  this.element = element;
10  this.$element = $(element);
11
12  // If the element has already been bound, returns its saved Parsley instance
13  var savedparsleyFormInstance = this.$element.data('Parsley');
14  if (savedparsleyFormInstance) {
15
16    // If the saved instance has been bound without a Form parent and there is one given in this call, add it
17    if ('undefined' !== typeof parsleyFormInstance && savedparsleyFormInstance.parent === window.Parsley) {
18      savedparsleyFormInstance.parent = parsleyFormInstance;
19      savedparsleyFormInstance._resetOptions(savedparsleyFormInstance.options);
20    }
21
22    if ('object' === typeof options) {
23      Object.assign(savedparsleyFormInstance.options, options);
24    }
25
26    return savedparsleyFormInstance;
27  }
28
29  // Parsley must be instantiated with a DOM element or jQuery $element
30  if (!this.$element.length)
31    throw new Error('You must bind Parsley on an existing element.');
32
33  if ('undefined' !== typeof parsleyFormInstance && 'Form' !== parsleyFormInstance.__class__)
34    throw new Error('Parent instance must be a Form instance');
35
36  this.parent = parsleyFormInstance || window.Parsley;
37  return this.init(options);
38};
39
40Factory.prototype = {
41  init: function (options) {
42    this.__class__ = 'Parsley';
43    this.__version__ = '@@version';
44    this.__id__ = Utils.generateID();
45
46    // Pre-compute options
47    this._resetOptions(options);
48
49    // A Form instance is obviously a `<form>` element but also every node that is not an input and has the `data-parsley-validate` attribute
50    if (this.element.nodeName === 'FORM' || (Utils.checkAttr(this.element, this.options.namespace, 'validate') && !this.$element.is(this.options.inputs)))
51      return this.bind('parsleyForm');
52
53    // Every other element is bound as a `Field` or `FieldMultiple`
54    return this.isMultiple() ? this.handleMultiple() : this.bind('parsleyField');
55  },
56
57  isMultiple: function () {
58    var type = Utils.getType(this.element);
59    return ((type === 'radio' || type === 'checkbox') ||
60      (this.element.nodeName === 'SELECT' && null !== this.element.getAttribute('multiple')));
61  },
62
63  // Multiples fields are a real nightmare :(
64  // Maybe some refactoring would be appreciated here...
65  handleMultiple: function () {
66    var name;
67    var multiple;
68    var parsleyMultipleInstance;
69
70    // Handle multiple name
71    this.options.multiple = this.options.multiple ||
72      (name = this.element.getAttribute('name')) ||
73      this.element.getAttribute('id');
74
75    // Special select multiple input
76    if (this.element.nodeName === 'SELECT' && null !== this.element.getAttribute('multiple')) {
77      this.options.multiple = this.options.multiple || this.__id__;
78      return this.bind('parsleyFieldMultiple');
79
80    // Else for radio / checkboxes, we need a `name` or `data-parsley-multiple` to properly bind it
81    } else if (!this.options.multiple) {
82      Utils.warn('To be bound by Parsley, a radio, a checkbox and a multiple select input must have either a name or a multiple option.', this.$element);
83      return this;
84    }
85
86    // Remove special chars
87    this.options.multiple = this.options.multiple.replace(/(:|\.|\[|\]|\{|\}|\$)/g, '');
88
89    // Add proper `data-parsley-multiple` to siblings if we have a valid multiple name
90    if (name) {
91      $('input[name="' + name + '"]').each((i, input) => {
92        var type = Utils.getType(input);
93        if ((type === 'radio' || type === 'checkbox'))
94          input.setAttribute(this.options.namespace + 'multiple', this.options.multiple);
95      });
96    }
97
98    // Check here if we don't already have a related multiple instance saved
99    var $previouslyRelated = this._findRelated();
100    for (var i = 0; i < $previouslyRelated.length; i++) {
101      parsleyMultipleInstance = $($previouslyRelated.get(i)).data('Parsley');
102      if ('undefined' !== typeof parsleyMultipleInstance) {
103
104        if (!this.$element.data('FieldMultiple')) {
105          parsleyMultipleInstance.addElement(this.$element);
106        }
107
108        break;
109      }
110    }
111
112    // Create a secret Field instance for every multiple field. It will be stored in `data('FieldMultiple')`
113    // And will be useful later to access classic `Field` stuff while being in a `FieldMultiple` instance
114    this.bind('parsleyField', true);
115
116    return parsleyMultipleInstance || this.bind('parsleyFieldMultiple');
117  },
118
119  // Return proper `Form`, `Field` or `FieldMultiple`
120  bind: function (type, doNotStore) {
121    var parsleyInstance;
122
123    switch (type) {
124      case 'parsleyForm':
125        parsleyInstance = $.extend(
126          new Form(this.element, this.domOptions, this.options),
127          new Base(),
128          window.ParsleyExtend
129        )._bindFields();
130        break;
131      case 'parsleyField':
132        parsleyInstance = $.extend(
133          new Field(this.element, this.domOptions, this.options, this.parent),
134          new Base(),
135          window.ParsleyExtend
136        );
137        break;
138      case 'parsleyFieldMultiple':
139        parsleyInstance = $.extend(
140          new Field(this.element, this.domOptions, this.options, this.parent),
141          new Multiple(),
142          new Base(),
143          window.ParsleyExtend
144        )._init();
145        break;
146      default:
147        throw new Error(type + 'is not a supported Parsley type');
148    }
149
150    if (this.options.multiple)
151      Utils.setAttr(this.element, this.options.namespace, 'multiple', this.options.multiple);
152
153    if ('undefined' !== typeof doNotStore) {
154      this.$element.data('FieldMultiple', parsleyInstance);
155
156      return parsleyInstance;
157    }
158
159    // Store the freshly bound instance in a DOM element for later access using jQuery `data()`
160    this.$element.data('Parsley', parsleyInstance);
161
162    // Tell the world we have a new Form or Field instance!
163    parsleyInstance._actualizeTriggers();
164    parsleyInstance._trigger('init');
165
166    return parsleyInstance;
167  }
168};
169
170export default Factory;

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.