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