/** * DO NOT EDIT THIS FILE. * See the following change record for more information, * https://www.drupal.org/node/2815083 * @preserve **/ (function ($, Drupal) { const states = { postponed: [] }; Drupal.states = states; function invert(a, invertState) { return invertState && typeof a !== 'undefined' ? !a : a; } function compare(a, b) { if (a === b) { return typeof a === 'undefined' ? a : true; } return typeof a === 'undefined' || typeof b === 'undefined'; } function ternary(a, b) { if (typeof a === 'undefined') { return b; } if (typeof b === 'undefined') { return a; } return a && b; } Drupal.behaviors.states = { attach(context, settings) { const $states = $(context).find('[data-drupal-states]'); const il = $states.length; for (let i = 0; i < il; i++) { const config = JSON.parse($states[i].getAttribute('data-drupal-states')); Object.keys(config || {}).forEach(state => { new states.Dependent({ element: $($states[i]), state: states.State.sanitize(state), constraints: config[state] }); }); } while (states.postponed.length) { states.postponed.shift()(); } } }; states.Dependent = function (args) { $.extend(this, { values: {}, oldValue: null }, args); this.dependees = this.getDependees(); Object.keys(this.dependees || {}).forEach(selector => { this.initializeDependee(selector, this.dependees[selector]); }); }; states.Dependent.comparisons = { RegExp(reference, value) { return reference.test(value); }, Function(reference, value) { return reference(value); }, Number(reference, value) { return typeof value === 'string' ? compare(reference.toString(), value) : compare(reference, value); } }; states.Dependent.prototype = { initializeDependee(selector, dependeeStates) { this.values[selector] = {}; Object.keys(dependeeStates).forEach(i => { let state = dependeeStates[i]; if ($.inArray(state, dependeeStates) === -1) { return; } state = states.State.sanitize(state); this.values[selector][state.name] = null; $(selector).on(`state:${state}`, { selector, state }, e => { this.update(e.data.selector, e.data.state, e.value); }); new states.Trigger({ selector, state }); }); }, compare(reference, selector, state) { const value = this.values[selector][state.name]; if (reference.constructor.name in states.Dependent.comparisons) { return states.Dependent.comparisons[reference.constructor.name](reference, value); } return compare(reference, value); }, update(selector, state, value) { if (value !== this.values[selector][state.name]) { this.values[selector][state.name] = value; this.reevaluate(); } }, reevaluate() { let value = this.verifyConstraints(this.constraints); if (value !== this.oldValue) { this.oldValue = value; value = invert(value, this.state.invert); this.element.trigger({ type: `state:${this.state}`, value, trigger: true }); } }, verifyConstraints(constraints, selector) { let result; if ($.isArray(constraints)) { const hasXor = $.inArray('xor', constraints) === -1; const len = constraints.length; for (let i = 0; i < len; i++) { if (constraints[i] !== 'xor') { const constraint = this.checkConstraints(constraints[i], selector, i); if (constraint && (hasXor || result)) { return hasXor; } result = result || constraint; } } } else if ($.isPlainObject(constraints)) { for (const n in constraints) { if (constraints.hasOwnProperty(n)) { result = ternary(result, this.checkConstraints(constraints[n], selector, n)); if (result === false) { return false; } } } } return result; }, checkConstraints(value, selector, state) { if (typeof state !== 'string' || /[0-9]/.test(state[0])) { state = null; } else if (typeof selector === 'undefined') { selector = state; state = null; } if (state !== null) { state = states.State.sanitize(state); return invert(this.compare(value, selector, state), state.invert); } return this.verifyConstraints(value, selector); }, getDependees() { const cache = {}; const _compare = this.compare; this.compare = function (reference, selector, state) { (cache[selector] || (cache[selector] = [])).push(state.name); }; this.verifyConstraints(this.constraints); this.compare = _compare; return cache; } }; states.Trigger = function (args) { $.extend(this, args); if (this.state in states.Trigger.states) { this.element = $(this.selector); if (!this.element.data(`trigger:${this.state}`)) { this.initialize(); } } }; states.Trigger.prototype = { initialize() { const trigger = states.Trigger.states[this.state]; if (typeof trigger === 'function') { trigger.call(window, this.element); } else { Object.keys(trigger || {}).forEach(event => { this.defaultTrigger(event, trigger[event]); }); } this.element.data(`trigger:${this.state}`, true); }, defaultTrigger(event, valueFn) { let oldValue = valueFn.call(this.element); this.element.on(event, $.proxy(function (e) { const value = valueFn.call(this.element, e); if (oldValue !== value) { this.element.trigger({ type: `state:${this.state}`, value, oldValue }); oldValue = value; } }, this)); states.postponed.push($.proxy(function () { this.element.trigger({ type: `state:${this.state}`, value: oldValue, oldValue: null }); }, this)); } }; states.Trigger.states = { empty: { keyup() { return this.val() === ''; } }, checked: { change() { let checked = false; this.each(function () { checked = $(this).prop('checked'); return !checked; }); return checked; } }, value: { keyup() { if (this.length > 1) { return this.filter(':checked').val() || false; } return this.val(); }, change() { if (this.length > 1) { return this.filter(':checked').val() || false; } return this.val(); } }, collapsed: { collapsed(e) { return typeof e !== 'undefined' && 'value' in e ? e.value : !this.is('[open]'); } } }; states.State = function (state) { this.pristine = state; this.name = state; let process = true; do { while (this.name.charAt(0) === '!') { this.name = this.name.substring(1); this.invert = !this.invert; } if (this.name in states.State.aliases) { this.name = states.State.aliases[this.name]; } else { process = false; } } while (process); }; states.State.sanitize = function (state) { if (state instanceof states.State) { return state; } return new states.State(state); }; states.State.aliases = { enabled: '!disabled', invisible: '!visible', invalid: '!valid', untouched: '!touched', optional: '!required', filled: '!empty', unchecked: '!checked', irrelevant: '!relevant', expanded: '!collapsed', open: '!collapsed', closed: 'collapsed', readwrite: '!readonly' }; states.State.prototype = { invert: false, toString() { return this.name; } }; const $document = $(document); $document.on('state:disabled', e => { if (e.trigger) { $(e.target).prop('disabled', e.value).closest('.js-form-item, .js-form-submit, .js-form-wrapper').toggleClass('form-disabled', e.value).find('select, input, textarea').prop('disabled', e.value); } }); $document.on('state:required', e => { if (e.trigger) { if (e.value) { const label = `label${e.target.id ? `[for=${e.target.id}]` : ''}`; const $label = $(e.target).attr({ required: 'required', 'aria-required': 'true' }).closest('.js-form-item, .js-form-wrapper').find(label); if (!$label.hasClass('js-form-required').length) { $label.addClass('js-form-required form-required'); } } else { $(e.target).removeAttr('required aria-required').closest('.js-form-item, .js-form-wrapper').find('label.js-form-required').removeClass('js-form-required form-required'); } } }); $document.on('state:visible', e => { if (e.trigger) { $(e.target).closest('.js-form-item, .js-form-submit, .js-form-wrapper').toggle(e.value); } }); $document.on('state:checked', e => { if (e.trigger) { $(e.target).prop('checked', e.value); } }); $document.on('state:collapsed', e => { if (e.trigger) { if ($(e.target).is('[open]') === e.value) { $(e.target).find('> summary').trigger('click'); } } }); })(jQuery, Drupal);