layout-builder.es6.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. (($, { ajax, behaviors }) => {
  2. behaviors.layoutBuilder = {
  3. attach(context) {
  4. $(context)
  5. .find('.layout-builder--layout__region')
  6. .sortable({
  7. items: '> .draggable',
  8. connectWith: '.layout-builder--layout__region',
  9. placeholder: 'ui-state-drop',
  10. /**
  11. * Updates the layout with the new position of the block.
  12. *
  13. * @param {jQuery.Event} event
  14. * The jQuery Event object.
  15. * @param {Object} ui
  16. * An object containing information about the item being sorted.
  17. */
  18. update(event, ui) {
  19. // Check if the region from the event and region for the item match.
  20. const itemRegion = ui.item.closest(
  21. '.layout-builder--layout__region',
  22. );
  23. if (event.target === itemRegion[0]) {
  24. // Find the destination delta.
  25. const deltaTo = ui.item
  26. .closest('[data-layout-delta]')
  27. .data('layout-delta');
  28. // If the block didn't leave the original delta use the destination.
  29. const deltaFrom = ui.sender
  30. ? ui.sender.closest('[data-layout-delta]').data('layout-delta')
  31. : deltaTo;
  32. ajax({
  33. url: [
  34. ui.item
  35. .closest('[data-layout-update-url]')
  36. .data('layout-update-url'),
  37. deltaFrom,
  38. deltaTo,
  39. itemRegion.data('region'),
  40. ui.item.data('layout-block-uuid'),
  41. ui.item
  42. .prev('[data-layout-block-uuid]')
  43. .data('layout-block-uuid'),
  44. ]
  45. .filter(element => element !== undefined)
  46. .join('/'),
  47. }).execute();
  48. }
  49. },
  50. });
  51. },
  52. };
  53. })(jQuery, Drupal);