off-canvas.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, debounce, displace) {
  8. Drupal.offCanvas = {
  9. position: null,
  10. minimumHeight: 30,
  11. minDisplaceWidth: 768,
  12. $mainCanvasWrapper: $('[data-off-canvas-main-canvas]'),
  13. isOffCanvas: function isOffCanvas($element) {
  14. return $element.is('#drupal-off-canvas');
  15. },
  16. removeOffCanvasEvents: function removeOffCanvasEvents($element) {
  17. $element.off('.off-canvas');
  18. $(document).off('.off-canvas');
  19. $(window).off('.off-canvas');
  20. },
  21. beforeCreate: function beforeCreate(_ref) {
  22. var settings = _ref.settings,
  23. $element = _ref.$element;
  24. Drupal.offCanvas.removeOffCanvasEvents($element);
  25. $('body').addClass('js-off-canvas-dialog-open');
  26. settings.position = {
  27. my: 'left top',
  28. at: Drupal.offCanvas.getEdge() + ' top',
  29. of: window
  30. };
  31. var position = settings.drupalOffCanvasPosition;
  32. var height = position === 'side' ? $(window).height() : settings.height;
  33. var width = position === 'side' ? settings.width : '100%';
  34. settings.height = height;
  35. settings.width = width;
  36. },
  37. beforeClose: function beforeClose(_ref2) {
  38. var $element = _ref2.$element;
  39. $('body').removeClass('js-off-canvas-dialog-open');
  40. Drupal.offCanvas.removeOffCanvasEvents($element);
  41. Drupal.offCanvas.resetPadding();
  42. },
  43. afterCreate: function afterCreate(_ref3) {
  44. var $element = _ref3.$element,
  45. settings = _ref3.settings;
  46. var eventData = { settings: settings, $element: $element, offCanvasDialog: this };
  47. $element.on('dialogContentResize.off-canvas', eventData, Drupal.offCanvas.handleDialogResize).on('dialogContentResize.off-canvas', eventData, Drupal.offCanvas.bodyPadding);
  48. Drupal.offCanvas.getContainer($element).attr('data-offset-' + Drupal.offCanvas.getEdge(), '');
  49. $(window).on('resize.off-canvas', eventData, debounce(Drupal.offCanvas.resetSize, 100)).trigger('resize.off-canvas');
  50. },
  51. render: function render(_ref4) {
  52. var settings = _ref4.settings;
  53. $('.ui-dialog-off-canvas, .ui-dialog-off-canvas .ui-dialog-titlebar').toggleClass('ui-dialog-empty-title', !settings.title);
  54. },
  55. handleDialogResize: function handleDialogResize(event) {
  56. var $element = event.data.$element;
  57. var $container = Drupal.offCanvas.getContainer($element);
  58. var $offsets = $container.find('> :not(#drupal-off-canvas, .ui-resizable-handle)');
  59. var offset = 0;
  60. $element.css({ height: 'auto' });
  61. var modalHeight = $container.height();
  62. $offsets.each(function (i, e) {
  63. offset += $(e).outerHeight();
  64. });
  65. var scrollOffset = $element.outerHeight() - $element.height();
  66. $element.height(modalHeight - offset - scrollOffset);
  67. },
  68. resetSize: function resetSize(event) {
  69. var $element = event.data.$element;
  70. var container = Drupal.offCanvas.getContainer($element);
  71. var position = event.data.settings.drupalOffCanvasPosition;
  72. if (Drupal.offCanvas.position && Drupal.offCanvas.position !== position) {
  73. container.removeAttr('data-offset-' + Drupal.offCanvas.position);
  74. }
  75. if (position === 'top') {
  76. $element.css('min-height', Drupal.offCanvas.minimumHeight + 'px');
  77. }
  78. displace();
  79. var offsets = displace.offsets;
  80. var topPosition = position === 'side' && offsets.top !== 0 ? '+' + offsets.top : '';
  81. var adjustedOptions = {
  82. position: {
  83. my: Drupal.offCanvas.getEdge() + ' top',
  84. at: Drupal.offCanvas.getEdge() + ' top' + topPosition,
  85. of: window
  86. }
  87. };
  88. var height = position === 'side' ? $(window).height() - (offsets.top + offsets.bottom) + 'px' : event.data.settings.height;
  89. container.css({
  90. position: 'fixed',
  91. height: height
  92. });
  93. $element.dialog('option', adjustedOptions).trigger('dialogContentResize.off-canvas');
  94. Drupal.offCanvas.position = position;
  95. },
  96. bodyPadding: function bodyPadding(event) {
  97. var position = event.data.settings.drupalOffCanvasPosition;
  98. if (position === 'side' && $('body').outerWidth() < Drupal.offCanvas.minDisplaceWidth) {
  99. return;
  100. }
  101. Drupal.offCanvas.resetPadding();
  102. var $element = event.data.$element;
  103. var $container = Drupal.offCanvas.getContainer($element);
  104. var $mainCanvasWrapper = Drupal.offCanvas.$mainCanvasWrapper;
  105. var width = $container.outerWidth();
  106. var mainCanvasPadding = $mainCanvasWrapper.css('padding-' + Drupal.offCanvas.getEdge());
  107. if (position === 'side' && width !== mainCanvasPadding) {
  108. $mainCanvasWrapper.css('padding-' + Drupal.offCanvas.getEdge(), width + 'px');
  109. $container.attr('data-offset-' + Drupal.offCanvas.getEdge(), width);
  110. displace();
  111. }
  112. var height = $container.outerHeight();
  113. if (position === 'top') {
  114. $mainCanvasWrapper.css('padding-top', height + 'px');
  115. $container.attr('data-offset-top', height);
  116. displace();
  117. }
  118. },
  119. getContainer: function getContainer($element) {
  120. return $element.dialog('widget');
  121. },
  122. getEdge: function getEdge() {
  123. return document.documentElement.dir === 'rtl' ? 'left' : 'right';
  124. },
  125. resetPadding: function resetPadding() {
  126. Drupal.offCanvas.$mainCanvasWrapper.css('padding-' + Drupal.offCanvas.getEdge(), 0);
  127. Drupal.offCanvas.$mainCanvasWrapper.css('padding-top', 0);
  128. displace();
  129. }
  130. };
  131. Drupal.behaviors.offCanvasEvents = {
  132. attach: function attach() {
  133. $(window).once('off-canvas').on({
  134. 'dialog:beforecreate': function dialogBeforecreate(event, dialog, $element, settings) {
  135. if (Drupal.offCanvas.isOffCanvas($element)) {
  136. Drupal.offCanvas.beforeCreate({ dialog: dialog, $element: $element, settings: settings });
  137. }
  138. },
  139. 'dialog:aftercreate': function dialogAftercreate(event, dialog, $element, settings) {
  140. if (Drupal.offCanvas.isOffCanvas($element)) {
  141. Drupal.offCanvas.render({ dialog: dialog, $element: $element, settings: settings });
  142. Drupal.offCanvas.afterCreate({ $element: $element, settings: settings });
  143. }
  144. },
  145. 'dialog:beforeclose': function dialogBeforeclose(event, dialog, $element) {
  146. if (Drupal.offCanvas.isOffCanvas($element)) {
  147. Drupal.offCanvas.beforeClose({ dialog: dialog, $element: $element });
  148. }
  149. }
  150. });
  151. }
  152. };
  153. })(jQuery, Drupal, Drupal.debounce, Drupal.displace);