states.js 10 KB

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