layout-builder.es6.js 1.6 KB

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