zidan-select 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * ZidanForm v2.1.1
2
+ * ZidanForm v2.2.0
3
3
  * Zero-dependency form enhancement library: select / combobox / tags,
4
4
  * checkboxes, switches, radio groups, file upload with drag & drop and
5
5
  * upload progress, text / number / phone / date / range inputs and textarea.
@@ -17,12 +17,14 @@
17
17
  var api = factory(root);
18
18
  if (typeof window !== 'undefined') {
19
19
  window.ZidanForm = api.ZidanForm;
20
- window.ZidanSelect = api.ZidanSelect;
21
- window.zidanSelect = api.zidanSelect;
22
- window.initZidanSelects = api.initZidanSelects;
23
- window.zidanField = api.zidanField;
24
- window.initZidanForms = api.initZidanForms;
25
- window.registerZidanIcon = api.registerIcon;
20
+ if (api.ZidanSelect) {
21
+ window.ZidanSelect = api.ZidanSelect;
22
+ window.zidanSelect = api.zidanSelect;
23
+ window.initZidanSelects = api.initZidanSelects;
24
+ }
25
+ if (api.zidanField) window.zidanField = api.zidanField;
26
+ if (api.initZidanForms) window.initZidanForms = api.initZidanForms;
27
+ if (api.registerIcon) window.registerZidanIcon = api.registerIcon;
26
28
  }
27
29
  if (typeof module !== 'undefined' && module.exports) {
28
30
  module.exports = api;
@@ -30,7 +32,7 @@
30
32
  })(typeof window !== 'undefined' ? window : this, function (global) {
31
33
  'use strict';
32
34
 
33
- var VERSION = '2.1.1';
35
+ var VERSION = '2.2.0';
34
36
 
35
37
  // Loading the file twice (classic script + module wrapper, or two bundles)
36
38
  // must not replace the classes and orphan the live instances.
@@ -40,6 +42,7 @@
40
42
 
41
43
  var doc = typeof document !== 'undefined' ? document : null;
42
44
 
45
+ /* @zidan-build:shared */
43
46
  /* ------------------------------------------------------------------ *
44
47
  * Constants & tiny helpers
45
48
  * ------------------------------------------------------------------ */
@@ -50,7 +53,9 @@
50
53
  var FIELD_LAYOUTS = ['stacked', 'inline', 'card', 'compact', 'floating', 'outlined'];
51
54
  var FIELD_KINDS = ['checkbox', 'radio', 'radio-group', 'file', 'input', 'textarea'];
52
55
  var FIELDS_SELECTOR = '[data-zidan], [data-zidan-checkbox], [data-zidan-switch], [data-zidan-radio], ' +
53
- '[data-zidan-radio-group], [data-zidan-file], [data-zidan-input], [data-zidan-textarea]';
56
+ '[data-zidan-radio-group], [data-zidan-file], [data-zidan-input], [data-zidan-textarea], .zidan-field';
57
+ var SELECT_SELECTOR = '[data-zidan-select], .zidan-select';
58
+ var FORM_SCOPE_SELECTOR = '[data-zidan-form], .zidan-form';
54
59
  var SCROLLBARS = ['default', 'thin', 'fancy', 'hover', 'hidden'];
55
60
  var EVENT_PREFIX = 'zidan:';
56
61
  var uidSeq = 0;
@@ -102,6 +107,36 @@
102
107
  return !(v === 'false' || v === '0' || v === 'no' || v === 'off');
103
108
  }
104
109
 
110
+ /* copy own keys from each source; later sources win */
111
+ function mergeOptions() {
112
+ var out = {};
113
+ for (var i = 0; i < arguments.length; i++) {
114
+ (function (source) {
115
+ if (!source || typeof source !== 'object') return;
116
+ Object.keys(source).forEach(function (key) {
117
+ if (source[key] !== undefined) out[key] = source[key];
118
+ });
119
+ })(arguments[i]);
120
+ }
121
+ return out;
122
+ }
123
+
124
+ /* data-zidan-options='{"mode":"tags",...}' — options for CMS/JSON-only setups */
125
+ function parseOptionsAttr(node) {
126
+ if (!node || !node.getAttribute) return null;
127
+ var raw = node.getAttribute('data-zidan-options');
128
+ if (!raw) return null;
129
+ try {
130
+ var parsed = JSON.parse(raw);
131
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
132
+ } catch (error) {
133
+ if (typeof console !== 'undefined' && console.warn) {
134
+ console.warn('[ZidanForm] ignoring invalid data-zidan-options JSON on', node, error);
135
+ }
136
+ return null;
137
+ }
138
+ }
139
+
105
140
  function clamp(value, min, max) {
106
141
  return Math.min(max, Math.max(min, value));
107
142
  }
@@ -344,6 +379,7 @@
344
379
  return fillTemplate(map[key] || FIELD_MESSAGES[key] || map.generic, vars);
345
380
  }
346
381
 
382
+ /* @zidan-build:phone */
347
383
  /* ---------------- phone formatting ---------------- */
348
384
 
349
385
  var PHONE_COUNTRIES = {
@@ -494,6 +530,7 @@
494
530
  return text.length;
495
531
  }
496
532
 
533
+ /* @zidan-build:shared2 */
497
534
  /* first non-empty value of any of `names`, walking up the tree */
498
535
  function inheritedAttr(node, names) {
499
536
  var current = node;
@@ -544,6 +581,7 @@
544
581
  return debounced;
545
582
  }
546
583
 
584
+ /* @zidan-build:select-defaults */
547
585
  /* ------------------------------------------------------------------ *
548
586
  * Defaults
549
587
  * ------------------------------------------------------------------ */
@@ -609,6 +647,7 @@
609
647
  onError: null
610
648
  };
611
649
 
650
+ /* @zidan-build:select */
612
651
  /* ------------------------------------------------------------------ *
613
652
  * ZidanSelect
614
653
  * ------------------------------------------------------------------ */
@@ -629,7 +668,8 @@
629
668
  this.layout = this.opts.layout;
630
669
  this.id = this.opts.id || uid('zs');
631
670
  this.isMount = /^(DIV|SPAN|SECTION|ARTICLE|FORM-FIELD)$/i.test(element.tagName) ||
632
- element.hasAttribute('data-zidan-select') && !/^(SELECT|INPUT|TEXTAREA)$/i.test(element.tagName);
671
+ (element.hasAttribute('data-zidan-select') || hasClass(element, 'zidan-select')) &&
672
+ !/^(SELECT|INPUT|TEXTAREA)$/i.test(element.tagName);
633
673
  this.native = this.isMount ? null : element;
634
674
  this.syncEl = null;
635
675
 
@@ -666,6 +706,7 @@
666
706
  /* ---------------- option normalisation ---------------- */
667
707
 
668
708
  ZidanSelect.prototype._normalizeOptions = function (options) {
709
+ options = mergeOptions(parseOptionsAttr(this.el), options);
669
710
  var d = this.el.dataset || {};
670
711
  var isNativeSelect = this.el.tagName === 'SELECT';
671
712
  var isTagsMount = /tags/i.test(d.zidanSelect || '') || /tags/i.test(d.zidanMode || '');
@@ -1110,6 +1151,7 @@
1110
1151
 
1111
1152
  if (!this.opts.multiple && this.mode !== 'tags') next = next.slice(0, 1);
1112
1153
  this.value_ = next;
1154
+ if (next.length && this.opts.validation === 'invalid') this.opts.validation = null;
1113
1155
  this._render();
1114
1156
  this._syncNative();
1115
1157
  if (!cfg.silent) {
@@ -1179,6 +1221,13 @@
1179
1221
  this.root.classList.toggle('zs-root--disabled', !!this.disabled_);
1180
1222
  this.root.classList.toggle('zs-root--invalid', opts.validation === 'invalid');
1181
1223
  this.root.classList.toggle('zs-root--valid', opts.validation === 'valid');
1224
+ if (opts.validation === 'invalid') this.root.setAttribute('data-zs-validation', 'invalid');
1225
+ else if (opts.validation === 'valid') this.root.setAttribute('data-zs-validation', 'valid');
1226
+ else this.root.removeAttribute('data-zs-validation');
1227
+ if (opts.validation === 'invalid') this.input.setAttribute('aria-invalid', 'true');
1228
+ else if (opts.validation === 'valid') this.input.setAttribute('aria-invalid', 'false');
1229
+ else this.input.removeAttribute('aria-invalid');
1230
+ if (this.el.required || opts.required) this.input.setAttribute('aria-required', 'true');
1182
1231
 
1183
1232
  if (opts.validation === 'invalid') this.controlEl.classList.add('is-invalid');
1184
1233
  else this.controlEl.classList.remove('is-invalid');
@@ -1657,6 +1706,7 @@
1657
1706
  var tag = { value: formatted, label: formatted };
1658
1707
  this.value_.push(tag);
1659
1708
  this.query = '';
1709
+ if (this.opts.validation === 'invalid') this.opts.validation = null;
1660
1710
 
1661
1711
  if (!cfg.silent) {
1662
1712
  this._render();
@@ -1980,6 +2030,14 @@
1980
2030
 
1981
2031
  this.debouncedFetch = debounce(function (query) { self._fetch(query); }, this.opts.debounce);
1982
2032
 
2033
+ if (this.el && this.el.addEventListener) {
2034
+ this.onNativeInvalid = function () {
2035
+ self.setValidation('invalid');
2036
+ if (self.input && self.input.focus) self.input.focus();
2037
+ };
2038
+ this.el.addEventListener('invalid', this.onNativeInvalid);
2039
+ }
2040
+
1983
2041
  this.input.addEventListener('input', this.onInput);
1984
2042
  this.input.addEventListener('keydown', this.onKeyDown);
1985
2043
  this.input.addEventListener('focus', this.onFocus);
@@ -2019,6 +2077,7 @@
2019
2077
  var previous = this.getTags();
2020
2078
  this.value_ = [];
2021
2079
  raw.forEach(function (tag) { this._addTagObject(tag, { silent: true }); }, this);
2080
+ if (this.value_.length && this.opts.validation === 'invalid') this.opts.validation = null;
2022
2081
  this._render();
2023
2082
  this._syncNative();
2024
2083
  if (!cfg.silent) {
@@ -2064,6 +2123,16 @@
2064
2123
  }
2065
2124
  return self.opts.allowFreeText;
2066
2125
  });
2126
+ if (this.native && this.native.tagName === 'SELECT') {
2127
+ while (this.native.firstChild) this.native.removeChild(this.native.firstChild);
2128
+ this.items.forEach(function (item) {
2129
+ var option = doc.createElement('option');
2130
+ option.value = item.value;
2131
+ option.textContent = item.label;
2132
+ if (item.disabled) option.disabled = true;
2133
+ self.native.appendChild(option);
2134
+ });
2135
+ }
2067
2136
  }
2068
2137
  this.asyncItems = null;
2069
2138
  this.activeIndex = -1;
@@ -2203,6 +2272,7 @@
2203
2272
 
2204
2273
  doc.removeEventListener('pointerdown', this.onDocumentPointerDown, true);
2205
2274
  global.removeEventListener('resize', this.onWindowResize);
2275
+ if (this.el && this.onNativeInvalid) this.el.removeEventListener('invalid', this.onNativeInvalid);
2206
2276
 
2207
2277
  this.emit('destroy', { instance: this });
2208
2278
 
@@ -2234,6 +2304,7 @@
2234
2304
  return this;
2235
2305
  };
2236
2306
 
2307
+ /* @zidan-build:fields */
2237
2308
  /* ================================================================== *
2238
2309
  * Part 2 — opt-in form fields
2239
2310
  *
@@ -2525,6 +2596,47 @@
2525
2596
  return this.validation;
2526
2597
  };
2527
2598
 
2599
+ ZidanFieldBase.prototype.getValue = function () {
2600
+ var el = this.el;
2601
+ if (!el) return null;
2602
+ if (this.kind === 'file') {
2603
+ return this.getFiles ? this.getFiles() : Array.prototype.slice.call(el.files || []);
2604
+ }
2605
+ if (el.type === 'checkbox') return !!el.checked;
2606
+ if (el.multiple && el.options) {
2607
+ return Array.prototype.filter.call(el.options, function (option) { return option.selected; })
2608
+ .map(function (option) { return option.value; });
2609
+ }
2610
+ return el.value;
2611
+ };
2612
+
2613
+ ZidanFieldBase.prototype.setValue = function (value) {
2614
+ var el = this.el;
2615
+ if (!el) return this;
2616
+ if (this.kind === 'file') {
2617
+ var files = value === null || value === undefined ? [] : (Array.isArray(value) ? value : [value]);
2618
+ if (!files.length) return this.clear ? this.clear() : this;
2619
+ if (this.addFiles) this.addFiles(files);
2620
+ return this;
2621
+ }
2622
+ if (el.type === 'checkbox') {
2623
+ if (this.setChecked) return this.setChecked(!!value);
2624
+ el.checked = !!value;
2625
+ el.dispatchEvent(new Event('change', { bubbles: true }));
2626
+ return this.update();
2627
+ }
2628
+ if (el.multiple && el.options) {
2629
+ var wanted = (Array.isArray(value) ? value : [value]).map(String);
2630
+ Array.prototype.forEach.call(el.options, function (option) {
2631
+ option.selected = wanted.indexOf(option.value) !== -1;
2632
+ });
2633
+ } else {
2634
+ el.value = value === null || value === undefined ? '' : String(value);
2635
+ }
2636
+ el.dispatchEvent(new Event('change', { bubbles: true }));
2637
+ return this.update();
2638
+ };
2639
+
2528
2640
  ZidanFieldBase.prototype.update = function () {
2529
2641
  if (this._sync) this._sync();
2530
2642
  this._mirrorValidity();
@@ -2552,6 +2664,7 @@
2552
2664
  return this;
2553
2665
  };
2554
2666
 
2667
+ /* @zidan-build:checkbox */
2555
2668
  /* ---------------- checkbox ---------------- */
2556
2669
 
2557
2670
  function ZidanCheckbox(target, options) {
@@ -2733,6 +2846,7 @@
2733
2846
  if (this._restoreInput) this._restoreInput();
2734
2847
  };
2735
2848
 
2849
+ /* @zidan-build:radio */
2736
2850
  /* ---------------- radio group ---------------- */
2737
2851
 
2738
2852
  function ZidanRadioGroup(target, options) {
@@ -2905,6 +3019,7 @@
2905
3019
  return '';
2906
3020
  };
2907
3021
 
3022
+ /* @zidan-build:file */
2908
3023
  /* ---------------- file upload ---------------- */
2909
3024
 
2910
3025
  function formatBytes(bytes) {
@@ -3698,6 +3813,7 @@
3698
3813
  if (this._restoreInput) this._restoreInput();
3699
3814
  };
3700
3815
 
3816
+ /* @zidan-build:text */
3701
3817
  /* ---------------- text inputs & textarea ---------------- */
3702
3818
 
3703
3819
  var TEXT_TYPES = ['text', 'email', 'password', 'number', 'tel', 'url', 'search', 'date', 'time', 'datetime-local', 'month', 'week', 'range', 'color'];
@@ -4426,6 +4542,10 @@
4426
4542
  this.counter.classList.toggle('zs-counter--limit', length >= this.counterMax);
4427
4543
  }
4428
4544
  if (this.range) this._syncRange(value);
4545
+ if (this.rangeOutput) {
4546
+ this.rangeOutput.textContent = value;
4547
+ this.rangeOutput.value = value;
4548
+ }
4429
4549
  if (this.stepper) {
4430
4550
  var min = parseFloat(input.min);
4431
4551
  var max = parseFloat(input.max);
@@ -4451,6 +4571,7 @@
4451
4571
  if (this._restoreInput) this._restoreInput();
4452
4572
  };
4453
4573
 
4574
+ /* @zidan-build:factory */
4454
4575
  /* ---------------- factory, registry, auto-init ---------------- */
4455
4576
 
4456
4577
  function fieldKindFor(node) {
@@ -4467,20 +4588,31 @@
4467
4588
  if (type === 'checkbox') return 'checkbox';
4468
4589
  if (type === 'radio') return 'radio';
4469
4590
  if (type === 'file') return 'file';
4591
+ if (hasClass(node, 'zidan-field')) {
4592
+ if (node.tagName === 'FIELDSET') return node.querySelector('input[type="radio"]') ? 'radio-group' : null;
4593
+ if (node.tagName === 'TEXTAREA') return 'textarea';
4594
+ if (node.tagName !== 'INPUT') return null;
4595
+ // a lone radio needs its group container, so only the fieldset form is inferred
4596
+ if (type === 'radio') return null;
4597
+ if (typeof TEXT_TYPES !== 'undefined' && TEXT_TYPES.indexOf(type) !== -1) return 'input';
4598
+ }
4470
4599
  return null;
4471
4600
  }
4472
4601
 
4473
4602
  function createField(node, kind, options) {
4474
- var opts = options || {};
4603
+ var opts = mergeOptions(parseOptionsAttr(node), options);
4475
4604
  if (node.__zidanField && !opts.force) return node.__zidanField;
4476
4605
  switch (kind) {
4477
- case 'checkbox': return new ZidanCheckbox(node, opts);
4478
- case 'radio': return new ZidanRadioGroup(node, opts);
4479
- case 'radio-group': return new ZidanRadioGroup(node, opts);
4480
- case 'file': return new ZidanFileUpload(node, opts);
4606
+ case 'checkbox':
4607
+ return typeof ZidanCheckbox !== 'undefined' ? new ZidanCheckbox(node, opts) : null;
4608
+ case 'radio':
4609
+ case 'radio-group':
4610
+ return typeof ZidanRadioGroup !== 'undefined' ? new ZidanRadioGroup(node, opts) : null;
4611
+ case 'file':
4612
+ return typeof ZidanFileUpload !== 'undefined' ? new ZidanFileUpload(node, opts) : null;
4481
4613
  case 'input':
4482
4614
  case 'textarea':
4483
- return new ZidanTextField(node, opts);
4615
+ return typeof ZidanTextField !== 'undefined' ? new ZidanTextField(node, opts) : null;
4484
4616
  default: return null;
4485
4617
  }
4486
4618
  }
@@ -4497,13 +4629,9 @@
4497
4629
  var created = [];
4498
4630
  nodes.forEach(function (node) {
4499
4631
  if (node.__zidanField) return;
4500
- var kind = fieldKindFor(node);
4501
4632
  // zidanField() accepts a plain input/textarea too, so JS-only options
4502
4633
  // (output, counter, clearable, …) work without a data attribute
4503
- if (!kind && (node.tagName === 'TEXTAREA' ||
4504
- (node.tagName === 'INPUT' && TEXT_TYPES.indexOf(String(node.type).toLowerCase()) !== -1))) {
4505
- kind = node.tagName === 'TEXTAREA' ? 'textarea' : 'input';
4506
- }
4634
+ var kind = fieldKindFor(node) || inferredTextKind(node);
4507
4635
  if (!kind) return;
4508
4636
  if (kind === 'radio' && !node.hasAttribute('data-zidan-radio')) {
4509
4637
  kind = 'radio-group';
@@ -4514,14 +4642,10 @@
4514
4642
  return created.length === 1 ? created[0] : created;
4515
4643
  }
4516
4644
 
4517
- function initZidanForms(container, options) {
4518
- var scope = container || doc;
4519
- if (!scope || !scope.querySelectorAll) return [];
4520
- var nodes = Array.prototype.slice.call(scope.querySelectorAll(FIELDS_SELECTOR));
4521
- if (isElement(scope) && scope.matches && scope.matches(FIELDS_SELECTOR)) nodes.unshift(scope);
4645
+ function initFieldList(nodes, options) {
4522
4646
  var instances = [];
4523
4647
  nodes.forEach(function (node) {
4524
- if (node.__zidanField) return;
4648
+ if (!isElement(node) || node.__zidanField) return;
4525
4649
  var kind = fieldKindFor(node);
4526
4650
  if (!kind) return;
4527
4651
  if (kind === 'radio') kind = 'radio-group';
@@ -4531,6 +4655,14 @@
4531
4655
  return instances;
4532
4656
  }
4533
4657
 
4658
+ function initZidanForms(container, options) {
4659
+ var scope = container || doc;
4660
+ if (!scope || !scope.querySelectorAll) return [];
4661
+ var nodes = Array.prototype.slice.call(scope.querySelectorAll(FIELDS_SELECTOR));
4662
+ if (isElement(scope) && scope.matches && scope.matches(FIELDS_SELECTOR)) nodes.unshift(scope);
4663
+ return initFieldList(nodes, options);
4664
+ }
4665
+
4534
4666
  var ZidanForm = {
4535
4667
  version: VERSION,
4536
4668
  __zidanFormGuard: VERSION,
@@ -4562,7 +4694,7 @@
4562
4694
  destroyAll: function () {
4563
4695
  ZidanForm.fields.slice().forEach(function (field) { field.destroy(); });
4564
4696
  ZidanForm.fields = [];
4565
- if (ZidanSelect && ZidanSelect.destroyAll) ZidanSelect.destroyAll();
4697
+ if (typeof ZidanSelect !== 'undefined' && ZidanSelect.destroyAll) ZidanSelect.destroyAll();
4566
4698
  return ZidanForm;
4567
4699
  },
4568
4700
  /* keep a subtree in sync when it is re-rendered by a framework */
@@ -4581,9 +4713,11 @@
4581
4713
  ZidanForm.fields.slice().forEach(function (field) {
4582
4714
  if (!field.el.isConnected) field.destroy();
4583
4715
  });
4584
- ZidanSelect.instances.slice().forEach(function (instance) {
4585
- if (!instance.el.isConnected) instance.destroy();
4586
- });
4716
+ if (typeof ZidanSelect !== 'undefined') {
4717
+ ZidanSelect.instances.slice().forEach(function (instance) {
4718
+ if (!instance.el.isConnected) instance.destroy();
4719
+ });
4720
+ }
4587
4721
  });
4588
4722
  observer.observe(root, { childList: true, subtree: true });
4589
4723
  return observer;
@@ -4595,38 +4729,41 @@
4595
4729
  /* registries, presets and helpers shared by every field type */
4596
4730
  ZidanForm.registerIcon = registerIcon;
4597
4731
  ZidanForm.icons = ICON_REGISTRY;
4598
- ZidanForm.phoneCountries = PHONE_COUNTRIES;
4599
- ZidanForm.phoneFormats = PHONE_FORMATS.slice();
4600
- ZidanForm.formatPhone = formatPhone;
4601
- ZidanForm.detectPhoneCountry = detectPhoneCountry;
4732
+ if (typeof PHONE_COUNTRIES !== 'undefined') {
4733
+ ZidanForm.phoneCountries = PHONE_COUNTRIES;
4734
+ ZidanForm.phoneFormats = PHONE_FORMATS.slice();
4735
+ ZidanForm.formatPhone = formatPhone;
4736
+ ZidanForm.detectPhoneCountry = detectPhoneCountry;
4737
+ }
4602
4738
  ZidanForm.messages = FIELD_MESSAGES;
4603
- ZidanForm.variants = {
4604
- checkbox: CHECKBOX_VARIANTS.slice(),
4605
- switch: SWITCH_STYLES.slice(),
4606
- radio: RADIO_VARIANTS.slice(),
4607
- file: FILE_VARIANTS.slice(),
4608
- range: RANGE_VARIANTS.slice()
4609
- };
4610
- ZidanForm.stepperModes = STEPPER_MODES.slice();
4739
+ ZidanForm.variants = {};
4740
+ if (typeof CHECKBOX_VARIANTS !== 'undefined') ZidanForm.variants.checkbox = CHECKBOX_VARIANTS.slice();
4741
+ if (typeof SWITCH_STYLES !== 'undefined') ZidanForm.variants.switch = SWITCH_STYLES.slice();
4742
+ if (typeof RADIO_VARIANTS !== 'undefined') ZidanForm.variants.radio = RADIO_VARIANTS.slice();
4743
+ if (typeof FILE_VARIANTS !== 'undefined') ZidanForm.variants.file = FILE_VARIANTS.slice();
4744
+ if (typeof RANGE_VARIANTS !== 'undefined') ZidanForm.variants.range = RANGE_VARIANTS.slice();
4745
+ if (typeof STEPPER_MODES !== 'undefined') ZidanForm.stepperModes = STEPPER_MODES.slice();
4611
4746
  ZidanForm.fieldLayouts = FIELD_LAYOUTS.slice();
4612
4747
 
4613
4748
  /* static helpers */
4614
- ZidanSelect.version = VERSION;
4615
- ZidanSelect.__zidanSelectGuard = VERSION;
4616
- ZidanSelect.layouts = LAYOUTS.slice();
4617
- ZidanSelect.frameworks = FRAMEWORKS.slice();
4618
- ZidanSelect.scrollbars = SCROLLBARS.slice();
4619
- ZidanSelect.defaults = DEFAULTS;
4620
- ZidanSelect.get = function (target) {
4621
- var element = typeof target === 'string' ? doc.querySelector(target) : target;
4622
- if (!isElement(element)) return null;
4623
- return element.__zidanSelect || null;
4624
- };
4625
- ZidanSelect.instances = [];
4626
- ZidanSelect.destroyAll = function () {
4627
- ZidanSelect.instances.slice().forEach(function (instance) { instance.destroy(); });
4749
+ if (typeof ZidanSelect !== 'undefined') {
4750
+ ZidanSelect.version = VERSION;
4751
+ ZidanSelect.__zidanSelectGuard = VERSION;
4752
+ ZidanSelect.layouts = LAYOUTS.slice();
4753
+ ZidanSelect.frameworks = FRAMEWORKS.slice();
4754
+ ZidanSelect.scrollbars = SCROLLBARS.slice();
4755
+ ZidanSelect.defaults = DEFAULTS;
4756
+ ZidanSelect.get = function (target) {
4757
+ var element = typeof target === 'string' ? doc.querySelector(target) : target;
4758
+ if (!isElement(element)) return null;
4759
+ return element.__zidanSelect || null;
4760
+ };
4628
4761
  ZidanSelect.instances = [];
4629
- };
4762
+ ZidanSelect.destroyAll = function () {
4763
+ ZidanSelect.instances.slice().forEach(function (instance) { instance.destroy(); });
4764
+ ZidanSelect.instances = [];
4765
+ };
4766
+ }
4630
4767
 
4631
4768
  /* ------------------------------------------------------------------ *
4632
4769
  * Factory + auto-init
@@ -4651,34 +4788,120 @@
4651
4788
  return instances.length === 1 ? instances[0] : instances;
4652
4789
  }
4653
4790
 
4791
+ function initSelectList(nodes, options) {
4792
+ return nodes.filter(function (node) {
4793
+ return isElement(node) && !node.__zidanSelect;
4794
+ }).map(function (node) {
4795
+ ZidanSelect.instances.push(new ZidanSelect(node, options || {}));
4796
+ return node.__zidanSelect;
4797
+ });
4798
+ }
4799
+
4654
4800
  function initZidanSelects(container, options) {
4655
4801
  var scope = container || doc;
4656
4802
  if (!scope || !scope.querySelectorAll) return [];
4657
- var nodes = Array.prototype.slice.call(scope.querySelectorAll('[data-zidan-select]'));
4658
- if (isElement(scope) && scope.hasAttribute('data-zidan-select')) nodes.unshift(scope);
4659
- return nodes
4660
- .filter(function (node) { return !node.__zidanSelect; })
4661
- .map(function (node) {
4662
- ZidanSelect.instances.push(new ZidanSelect(node, options || {}));
4663
- return node.__zidanSelect;
4803
+ var nodes = Array.prototype.slice.call(scope.querySelectorAll(SELECT_SELECTOR));
4804
+ if (isElement(scope) && scope.matches && scope.matches(SELECT_SELECTOR)) nodes.unshift(scope);
4805
+ return initSelectList(nodes, options);
4806
+ }
4807
+
4808
+ /* a config target is a CSS selector string or { selector, options } */
4809
+ function eachTarget(list, run) {
4810
+ toArray(list).forEach(function (entry) {
4811
+ if (!entry) return;
4812
+ if (typeof entry === 'string') run(entry, null);
4813
+ else if (entry.selector) run(String(entry.selector), entry.options || null);
4814
+ });
4815
+ }
4816
+
4817
+ function initSelectTargets(list, shared) {
4818
+ eachTarget(list, function (selector, options) {
4819
+ initSelectList(Array.prototype.slice.call(doc.querySelectorAll(selector)),
4820
+ mergeOptions(shared, options));
4821
+ });
4822
+ }
4823
+
4824
+ function initFieldTargets(list, shared) {
4825
+ eachTarget(list, function (selector, options) {
4826
+ var opts = mergeOptions(shared, options);
4827
+ Array.prototype.slice.call(doc.querySelectorAll(selector)).forEach(function (node) {
4828
+ if (!isElement(node) || node.__zidanField) return;
4829
+ var kind = fieldKindFor(node) || inferredTextKind(node);
4830
+ if (!kind) return;
4831
+ if (kind === 'radio') kind = 'radio-group';
4832
+ createField(node, kind, opts);
4664
4833
  });
4834
+ });
4835
+ }
4836
+
4837
+ /* explicit targets may point at a plain text input or textarea */
4838
+ function inferredTextKind(node) {
4839
+ if (typeof TEXT_TYPES === 'undefined' || !isElement(node)) return null;
4840
+ if (node.tagName === 'TEXTAREA') return 'textarea';
4841
+ if (node.tagName !== 'INPUT') return null;
4842
+ return TEXT_TYPES.indexOf(String(node.type).toLowerCase()) !== -1 ? 'input' : null;
4843
+ }
4844
+
4845
+ /* a form scope enhances every native select inside, marked or not */
4846
+ function initScopeSelects(scope, options) {
4847
+ if (typeof ZidanSelect === 'undefined') return [];
4848
+ var selector = SELECT_SELECTOR + ', select';
4849
+ var nodes = Array.prototype.slice.call(scope.querySelectorAll(selector));
4850
+ if (isElement(scope) && scope.matches && scope.matches(selector)) nodes.unshift(scope);
4851
+ return initSelectList(nodes, options);
4852
+ }
4853
+
4854
+ function initScope(scope, selectOptions, fieldOptions) {
4855
+ if (!isElement(scope)) return;
4856
+ initScopeSelects(scope, selectOptions);
4857
+ if (typeof ZidanFileUpload !== 'undefined') {
4858
+ // a form scope also picks up plain file inputs, not just marked ones
4859
+ initFieldList(Array.prototype.slice.call(scope.querySelectorAll('input[type="file"]')), fieldOptions);
4860
+ }
4861
+ if (typeof ZidanCheckbox !== 'undefined' || typeof ZidanRadioGroup !== 'undefined' ||
4862
+ typeof ZidanFileUpload !== 'undefined' || typeof ZidanTextField !== 'undefined') {
4863
+ initZidanForms(scope, fieldOptions);
4864
+ }
4865
+ }
4866
+
4867
+ function initFormScopes(list, selectOptions, fieldOptions) {
4868
+ Array.prototype.slice.call(doc.querySelectorAll(FORM_SCOPE_SELECTOR))
4869
+ .forEach(function (scope) { initScope(scope, selectOptions, fieldOptions); });
4870
+ eachTarget(list, function (selector, options) {
4871
+ Array.prototype.slice.call(doc.querySelectorAll(selector)).forEach(function (scope) {
4872
+ initScope(scope, mergeOptions(selectOptions, options), mergeOptions(fieldOptions, options));
4873
+ });
4874
+ });
4665
4875
  }
4666
4876
 
4667
4877
  function autoInit() {
4668
4878
  if (!doc) return;
4669
- var config = global.ZIDAN_SELECT_CONFIG || {};
4879
+ var config = global.ZIDAN_FORM_CONFIG || global.ZIDAN_SELECT_CONFIG || {};
4670
4880
  if (config.autoInit === false) return;
4671
4881
  if (global.ZidanSelect && global.ZidanSelect.autoInit === false) return;
4672
4882
  if (doc.documentElement.getAttribute('data-zidan-auto-init') === 'false') return;
4673
- if (config.selector) {
4674
- Array.prototype.slice.call(doc.querySelectorAll(config.selector)).forEach(function (node) {
4675
- if (node.__zidanSelect) return;
4676
- ZidanSelect.instances.push(new ZidanSelect(node, config.options || {}));
4677
- });
4678
- return;
4883
+
4884
+ var hasSelect = typeof ZidanSelect !== 'undefined';
4885
+ var hasFields = typeof ZidanCheckbox !== 'undefined' || typeof ZidanRadioGroup !== 'undefined' ||
4886
+ typeof ZidanFileUpload !== 'undefined' || typeof ZidanTextField !== 'undefined';
4887
+ var selectOptions = config.options || {};
4888
+ var fieldOptions = config.fieldOptions || config.fields || {};
4889
+
4890
+ // explicit targets run first so their per-target options win over the
4891
+ // generic attribute scan (the scan then skips what is already initialised)
4892
+ if (hasSelect) {
4893
+ if (config.selector) {
4894
+ // legacy: an explicit selector replaces the data-attribute scan
4895
+ initSelectTargets(config.selector, selectOptions);
4896
+ } else {
4897
+ initSelectTargets(config.targets || config.selectors, selectOptions);
4898
+ }
4679
4899
  }
4680
- initZidanSelects(doc, config.options || {});
4681
- initZidanForms(doc, config.fields || {});
4900
+ if (hasFields) initFieldTargets(config.fieldTargets, fieldOptions);
4901
+ initFormScopes(config.formTargets, selectOptions, fieldOptions);
4902
+
4903
+ if (hasSelect && !config.selector) initZidanSelects(doc, selectOptions);
4904
+ if (hasFields) initZidanForms(doc, fieldOptions);
4682
4905
  }
4683
4906
 
4684
4907
  if (doc) {
@@ -4691,19 +4914,26 @@
4691
4914
 
4692
4915
  var api = {
4693
4916
  ZidanForm: ZidanForm,
4694
- ZidanSelect: ZidanSelect,
4695
- ZidanCheckbox: ZidanCheckbox,
4696
- ZidanRadioGroup: ZidanRadioGroup,
4697
- ZidanFileUpload: ZidanFileUpload,
4698
- ZidanTextField: ZidanTextField,
4699
- zidanSelect: zidanSelect,
4700
- zidanField: zidanField,
4701
- initZidanSelects: initZidanSelects,
4702
- initZidanForms: initZidanForms,
4703
- registerIcon: registerIcon,
4704
- formatPhone: formatPhone,
4705
- detectPhoneCountry: detectPhoneCountry
4917
+ registerIcon: registerIcon
4706
4918
  };
4919
+ if (typeof ZidanSelect !== 'undefined') {
4920
+ api.ZidanSelect = ZidanSelect;
4921
+ api.zidanSelect = zidanSelect;
4922
+ api.initZidanSelects = initZidanSelects;
4923
+ }
4924
+ if (typeof ZidanCheckbox !== 'undefined' || typeof ZidanRadioGroup !== 'undefined' ||
4925
+ typeof ZidanFileUpload !== 'undefined' || typeof ZidanTextField !== 'undefined') {
4926
+ api.zidanField = zidanField;
4927
+ api.initZidanForms = initZidanForms;
4928
+ }
4929
+ if (typeof ZidanCheckbox !== 'undefined') api.ZidanCheckbox = ZidanCheckbox;
4930
+ if (typeof ZidanRadioGroup !== 'undefined') api.ZidanRadioGroup = ZidanRadioGroup;
4931
+ if (typeof ZidanFileUpload !== 'undefined') api.ZidanFileUpload = ZidanFileUpload;
4932
+ if (typeof ZidanTextField !== 'undefined') api.ZidanTextField = ZidanTextField;
4933
+ if (typeof formatPhone === 'function') {
4934
+ api.formatPhone = formatPhone;
4935
+ api.detectPhoneCountry = detectPhoneCountry;
4936
+ }
4707
4937
 
4708
4938
  ZidanForm.api = api;
4709
4939