allowed-countries.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  3. * @author Mediotype https://www.mediotype.com/
  4. */
  5. define(['jquery', 'jquery/ui'], function ($) {
  6. 'use strict';
  7. $.widget('vertex.allowedCountries', {
  8. /**
  9. * Bind all optgroups under the attached element to mass-select/mass-deselect their children on click
  10. *
  11. * @private
  12. */
  13. _create: function () {
  14. $(this.element).on('click', 'optgroup', this.filterClick.bind(this));
  15. },
  16. /**
  17. * Filter out any clicks where the target was not explicitly the optgroup
  18. *
  19. * @param {Event} event
  20. * @return void
  21. */
  22. filterClick: function (event) {
  23. if (!$(event.target).is('optgroup')) {
  24. return;
  25. }
  26. this._processClick(event);
  27. },
  28. /**
  29. * Decide to select or unselect all child elements and execute the chosen task
  30. *
  31. * @private
  32. * @param {Event} event
  33. * @return void
  34. */
  35. _processClick: function (event) {
  36. var optgroup = $(event.target),
  37. select = optgroup.closest('select'),
  38. scrollTop = select.scrollTop();
  39. if (optgroup.children('option:not(:selected)').length === 0) {
  40. optgroup.children('option').prop('selected', false);
  41. } else {
  42. optgroup.children('option').prop('selected', true);
  43. }
  44. // Maintain current scroll position
  45. // Default behavior, in chrome at least, is to jump to some other selected option
  46. setTimeout(function () {
  47. select.scrollTop(scrollTop);
  48. }, 0);
  49. }
  50. });
  51. return $.vertex.allowedCountries;
  52. });