domain_source.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @file
  3. * Attaches behaviors for the Domain Source module.
  4. *
  5. * If Domain Access is present, we show/hide selected publishing domains. This approach
  6. * currently only works with a select field.
  7. */
  8. (function ($) {
  9. "use strict";
  10. /**
  11. *
  12. * @type {Drupal~behavior}
  13. */
  14. Drupal.behaviors.domainSourceAllowed = {
  15. attach: function () {
  16. // Get the initial setting so that it can be reset.
  17. var initialOption = $("#edit-field-domain-source").val();
  18. // Onload, fire initial show/hide.
  19. getDomains();
  20. // Get the domains selected by the domain access field.
  21. function getDomains() {
  22. var domains = new Array();
  23. $("#edit-field-domain-access :checked").each(function(index, obj) {
  24. domains.push(obj.value);
  25. });
  26. setOptions(domains);
  27. }
  28. // Based on selected domains, show/hide the selection options.
  29. function setOptions(domains) {
  30. $("#edit-field-domain-source option").each(function(index, obj) {
  31. if (jQuery.inArray(obj.value, domains) == -1 && obj.value != '_none') {
  32. // If the current selection is removed, reset the selection to _none.
  33. if ($("#edit-field-domain-source").val() == obj.value) {
  34. $("#edit-field-domain-source").val('_none');
  35. }
  36. $("#edit-field-domain-source option[value=" + obj.value + "]").hide();
  37. }
  38. else {
  39. $("#edit-field-domain-source option[value=" + obj.value + "]").show();
  40. // If we reselected the initial value, reset the select option.
  41. if (obj.value == initialOption) {
  42. $("#edit-field-domain-source").val(obj.value);
  43. }
  44. }
  45. });
  46. }
  47. // When the selections change, recalculate the select options.
  48. $( "#edit-field-domain-access" ).on( "change", getDomains );
  49. }
  50. };
  51. })(jQuery);