states.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal) {
  8. var states = {
  9. postponed: []
  10. };
  11. Drupal.states = states;
  12. function invert(a, invertState) {
  13. return invertState && typeof a !== 'undefined' ? !a : a;
  14. }
  15. function _compare2(a, b) {
  16. if (a === b) {
  17. return typeof a === 'undefined' ? a : true;
  18. }
  19. return typeof a === 'undefined' || typeof b === 'undefined';
  20. }
  21. Drupal.behaviors.states = {
  22. attach: function attach(context, settings) {
  23. var $states = $(context).find('[data-drupal-states]');
  24. var il = $states.length;
  25. var _loop = function _loop(i) {
  26. var config = JSON.parse($states[i].getAttribute('data-drupal-states'));
  27. Object.keys(config || {}).forEach(function (state) {
  28. new states.Dependent({
  29. element: $($states[i]),
  30. state: states.State.sanitize(state),
  31. constraints: config[state]
  32. });
  33. });
  34. };
  35. for (var i = 0; i < il; i++) {
  36. _loop(i);
  37. }
  38. while (states.postponed.length) {
  39. states.postponed.shift()();
  40. }
  41. }
  42. };
  43. states.Dependent = function (args) {
  44. var _this = this;
  45. $.extend(this, { values: {}, oldValue: null }, args);
  46. this.dependees = this.getDependees();
  47. Object.keys(this.dependees || {}).forEach(function (selector) {
  48. _this.initializeDependee(selector, _this.dependees[selector]);
  49. });
  50. };
  51. states.Dependent.comparisons = {
  52. RegExp: function RegExp(reference, value) {
  53. return reference.test(value);
  54. },
  55. Function: function Function(reference, value) {
  56. return reference(value);
  57. },
  58. Number: function Number(reference, value) {
  59. return typeof value === 'string' ? _compare2(reference.toString(), value) : _compare2(reference, value);
  60. }
  61. };
  62. states.Dependent.prototype = {
  63. initializeDependee: function initializeDependee(selector, dependeeStates) {
  64. var _this2 = this;
  65. this.values[selector] = {};
  66. Object.keys(dependeeStates).forEach(function (i) {
  67. var state = dependeeStates[i];
  68. if ($.inArray(state, dependeeStates) === -1) {
  69. return;
  70. }
  71. state = states.State.sanitize(state);
  72. _this2.values[selector][state.name] = null;
  73. $(selector).on('state:' + state, { selector: selector, state: state }, function (e) {
  74. _this2.update(e.data.selector, e.data.state, e.value);
  75. });
  76. new states.Trigger({ selector: selector, state: state });
  77. });
  78. },
  79. compare: function compare(reference, selector, state) {
  80. var value = this.values[selector][state.name];
  81. if (reference.constructor.name in states.Dependent.comparisons) {
  82. return states.Dependent.comparisons[reference.constructor.name](reference, value);
  83. }
  84. return _compare2(reference, value);
  85. },
  86. update: function update(selector, state, value) {
  87. if (value !== this.values[selector][state.name]) {
  88. this.values[selector][state.name] = value;
  89. this.reevaluate();
  90. }
  91. },
  92. reevaluate: function reevaluate() {
  93. var value = this.verifyConstraints(this.constraints);
  94. if (value !== this.oldValue) {
  95. this.oldValue = value;
  96. value = invert(value, this.state.invert);
  97. this.element.trigger({
  98. type: 'state:' + this.state,
  99. value: value,
  100. trigger: true
  101. });
  102. }
  103. },
  104. verifyConstraints: function verifyConstraints(constraints, selector) {
  105. var _this3 = this;
  106. var result = void 0;
  107. if ($.isArray(constraints)) {
  108. var hasXor = $.inArray('xor', constraints) === -1;
  109. var len = constraints.length;
  110. for (var i = 0; i < len; i++) {
  111. if (constraints[i] !== 'xor') {
  112. var constraint = this.checkConstraints(constraints[i], selector, i);
  113. if (constraint && (hasXor || result)) {
  114. return hasXor;
  115. }
  116. result = result || constraint;
  117. }
  118. }
  119. } else if ($.isPlainObject(constraints)) {
  120. result = Object.keys(constraints).every(function (constraint) {
  121. var check = _this3.checkConstraints(constraints[constraint], selector, constraint);
  122. return typeof check === 'undefined' ? true : check;
  123. });
  124. }
  125. return result;
  126. },
  127. checkConstraints: function checkConstraints(value, selector, state) {
  128. if (typeof state !== 'string' || /[0-9]/.test(state[0])) {
  129. state = null;
  130. } else if (typeof selector === 'undefined') {
  131. selector = state;
  132. state = null;
  133. }
  134. if (state !== null) {
  135. state = states.State.sanitize(state);
  136. return invert(this.compare(value, selector, state), state.invert);
  137. }
  138. return this.verifyConstraints(value, selector);
  139. },
  140. getDependees: function getDependees() {
  141. var cache = {};
  142. var _compare = this.compare;
  143. this.compare = function (reference, selector, state) {
  144. (cache[selector] || (cache[selector] = [])).push(state.name);
  145. };
  146. this.verifyConstraints(this.constraints);
  147. this.compare = _compare;
  148. return cache;
  149. }
  150. };
  151. states.Trigger = function (args) {
  152. $.extend(this, args);
  153. if (this.state in states.Trigger.states) {
  154. this.element = $(this.selector);
  155. if (!this.element.data('trigger:' + this.state)) {
  156. this.initialize();
  157. }
  158. }
  159. };
  160. states.Trigger.prototype = {
  161. initialize: function initialize() {
  162. var _this4 = this;
  163. var trigger = states.Trigger.states[this.state];
  164. if (typeof trigger === 'function') {
  165. trigger.call(window, this.element);
  166. } else {
  167. Object.keys(trigger || {}).forEach(function (event) {
  168. _this4.defaultTrigger(event, trigger[event]);
  169. });
  170. }
  171. this.element.data('trigger:' + this.state, true);
  172. },
  173. defaultTrigger: function defaultTrigger(event, valueFn) {
  174. var oldValue = valueFn.call(this.element);
  175. this.element.on(event, $.proxy(function (e) {
  176. var value = valueFn.call(this.element, e);
  177. if (oldValue !== value) {
  178. this.element.trigger({
  179. type: 'state:' + this.state,
  180. value: value,
  181. oldValue: oldValue
  182. });
  183. oldValue = value;
  184. }
  185. }, this));
  186. states.postponed.push($.proxy(function () {
  187. this.element.trigger({
  188. type: 'state:' + this.state,
  189. value: oldValue,
  190. oldValue: null
  191. });
  192. }, this));
  193. }
  194. };
  195. states.Trigger.states = {
  196. empty: {
  197. keyup: function keyup() {
  198. return this.val() === '';
  199. }
  200. },
  201. checked: {
  202. change: function change() {
  203. var checked = false;
  204. this.each(function () {
  205. checked = $(this).prop('checked');
  206. return !checked;
  207. });
  208. return checked;
  209. }
  210. },
  211. value: {
  212. keyup: function keyup() {
  213. if (this.length > 1) {
  214. return this.filter(':checked').val() || false;
  215. }
  216. return this.val();
  217. },
  218. change: function change() {
  219. if (this.length > 1) {
  220. return this.filter(':checked').val() || false;
  221. }
  222. return this.val();
  223. }
  224. },
  225. collapsed: {
  226. collapsed: function collapsed(e) {
  227. return typeof e !== 'undefined' && 'value' in e ? e.value : !this.is('[open]');
  228. }
  229. }
  230. };
  231. states.State = function (state) {
  232. this.pristine = state;
  233. this.name = state;
  234. var process = true;
  235. do {
  236. while (this.name.charAt(0) === '!') {
  237. this.name = this.name.substring(1);
  238. this.invert = !this.invert;
  239. }
  240. if (this.name in states.State.aliases) {
  241. this.name = states.State.aliases[this.name];
  242. } else {
  243. process = false;
  244. }
  245. } while (process);
  246. };
  247. states.State.sanitize = function (state) {
  248. if (state instanceof states.State) {
  249. return state;
  250. }
  251. return new states.State(state);
  252. };
  253. states.State.aliases = {
  254. enabled: '!disabled',
  255. invisible: '!visible',
  256. invalid: '!valid',
  257. untouched: '!touched',
  258. optional: '!required',
  259. filled: '!empty',
  260. unchecked: '!checked',
  261. irrelevant: '!relevant',
  262. expanded: '!collapsed',
  263. open: '!collapsed',
  264. closed: 'collapsed',
  265. readwrite: '!readonly'
  266. };
  267. states.State.prototype = {
  268. invert: false,
  269. toString: function toString() {
  270. return this.name;
  271. }
  272. };
  273. var $document = $(document);
  274. $document.on('state:disabled', function (e) {
  275. if (e.trigger) {
  276. $(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);
  277. }
  278. });
  279. $document.on('state:required', function (e) {
  280. if (e.trigger) {
  281. if (e.value) {
  282. var label = 'label' + (e.target.id ? '[for=' + e.target.id + ']' : '');
  283. var $label = $(e.target).attr({ required: 'required', 'aria-required': 'aria-required' }).closest('.js-form-item, .js-form-wrapper').find(label);
  284. if (!$label.hasClass('js-form-required').length) {
  285. $label.addClass('js-form-required form-required');
  286. }
  287. } else {
  288. $(e.target).removeAttr('required aria-required').closest('.js-form-item, .js-form-wrapper').find('label.js-form-required').removeClass('js-form-required form-required');
  289. }
  290. }
  291. });
  292. $document.on('state:visible', function (e) {
  293. if (e.trigger) {
  294. $(e.target).closest('.js-form-item, .js-form-submit, .js-form-wrapper').toggle(e.value);
  295. }
  296. });
  297. $document.on('state:checked', function (e) {
  298. if (e.trigger) {
  299. $(e.target).prop('checked', e.value);
  300. }
  301. });
  302. $document.on('state:collapsed', function (e) {
  303. if (e.trigger) {
  304. if ($(e.target).is('[open]') === e.value) {
  305. $(e.target).find('> summary').trigger('click');
  306. }
  307. }
  308. });
  309. })(jQuery, Drupal);