Advertisement
Guest User

modal

a guest
Aug 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var BootstrapModaliOS = (function () {
  2.  
  3.     var disableBodyScroll = (function () { // 🔗 Thijs Huijssoon https://gist.github.com/thuijssoon
  4.  
  5.         /**
  6.          * Private variables
  7.          */
  8.         var _selector = false,
  9.             _element = false,
  10.             _clientY;
  11.  
  12.         /**
  13.          * Polyfills for Element.matches and Element.closest
  14.          */
  15.         if (!Element.prototype.matches)
  16.             Element.prototype.matches = Element.prototype.msMatchesSelector ||
  17.                 Element.prototype.webkitMatchesSelector;
  18.  
  19.         if (!Element.prototype.closest)
  20.             Element.prototype.closest = function (s) {
  21.                 var ancestor = this;
  22.                 if (!document.documentElement.contains(el)) return null;
  23.                 do {
  24.                     if (ancestor.matches(s)) return ancestor;
  25.                     ancestor = ancestor.parentElement;
  26.                 } while (ancestor !== null);
  27.                 return el;
  28.             };
  29.  
  30.         /**
  31.          * Prevent default unless within _selector
  32.          *
  33.          * @param  event object event
  34.          * @return void
  35.          */
  36.         var preventBodyScroll = function (event) {
  37.             if (false === _element || !event.target.closest(_selector)) {
  38.                 event.preventDefault();
  39.             }
  40.         };
  41.  
  42.         /**
  43.          * Cache the clientY co-ordinates for
  44.          * comparison
  45.          *
  46.          * @param  event object event
  47.          * @return void
  48.          */
  49.         var captureClientY = function (event) {
  50.             // only respond to a single touch
  51.             if (event.targetTouches.length === 1) {
  52.                 _clientY = event.targetTouches[0].clientY;
  53.             }
  54.         };
  55.  
  56.         /**
  57.          * Detect whether the element is at the top
  58.          * or the bottom of their scroll and prevent
  59.          * the user from scrolling beyond
  60.          *
  61.          * @param  event object event
  62.          * @return void
  63.          */
  64.         var preventOverscroll = function (event) {
  65.  
  66.             // only respond to a single touch
  67.             if (event.targetTouches.length !== 1) {
  68.                 return;
  69.             }
  70.  
  71.             var clientY = event.targetTouches[0].clientY - _clientY;
  72.  
  73.             // The element at the top of its scroll,
  74.             // and the user scrolls down
  75.             if (_element.scrollTop === 0 && clientY > 0) {
  76.                 event.preventDefault();
  77.             }
  78.  
  79.             // The element at the bottom of its scroll,
  80.             // and the user scrolls up
  81.             // https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
  82.             if ((_element.scrollHeight - _element.scrollTop <= _element.clientHeight) && clientY < 0) {
  83.                 event.preventDefault();
  84.             }
  85.  
  86.         };
  87.  
  88.         /**
  89.          * Disable body scroll. Scrolling with the selector is
  90.          * allowed if a selector is porvided.
  91.          *
  92.          * @param  boolean allow
  93.          * @param  string selector Selector to element to change scroll permission
  94.          * @return void
  95.          */
  96.         return function (allow, selector) {
  97.             if (typeof selector !== "undefined") {
  98.                 _selector = selector;
  99.                 _element = document.querySelector(selector);
  100.             }
  101.  
  102.             if (true === allow) {
  103.                 if (false !== _element) {
  104.                     _element.addEventListener('touchstart', captureClientY, {passive: false});
  105.                     _element.addEventListener('touchmove', preventOverscroll, {passive: false});
  106.                 }
  107.                 document.body.addEventListener("touchmove", preventBodyScroll, {passive: false});
  108.             } else {
  109.                 if (false !== _element) {
  110.                     _element.removeEventListener('touchstart', captureClientY, {passive: false});
  111.                     _element.removeEventListener('touchmove', preventOverscroll, {passive: false});
  112.                 }
  113.                 document.body.removeEventListener("touchmove", preventBodyScroll, {passive: false});
  114.             }
  115.         };
  116.     }());
  117.  
  118.     function adjustModal(e) {
  119.  
  120.         var modal = $('#ModalInBasket')[0];
  121.         var actual_viewport = window.innerHeight;
  122.         var offset_y = modal.getBoundingClientRect().y;
  123.  
  124.         modal.style.top = 0;
  125.         modal.style.bottom = 0;
  126.         var screen_height = modal.scrollHeight;
  127.  
  128.         if (!navigator.userAgent.match(/(iPod|iPhone|iPad)/i) || Math.abs(window.orientation) !== 90 || actual_viewport === screen_height) { // Only for mobile Safari in landscape mode, when the modal height doesn't match the actual viewport
  129.  
  130.             return;
  131.  
  132.         }
  133.  
  134.         if (typeof e !== 'undefined') { // On resize event (toolbars have appeared by tapping at the top or bottom area
  135.  
  136.             modal.style.top = (0 - offset_y) + 'px';
  137.             modal.style.bottom = (screen_height - actual_viewport + offset_y) + 'px';
  138.  
  139.         } else {
  140.  
  141.             if (actual_viewport <= screen_height) { // modal is cropped, adjust its top/bottom
  142.  
  143.                 if ((document.body.scrollHeight + document.body.getBoundingClientRect().y) === actual_viewport) { // page scrolled at the bottom
  144.  
  145.                     modal.style.bottom = 0;
  146.                     modal.style.top = (screen_height - actual_viewport) + 'px';
  147.  
  148.                 } else {
  149.  
  150.                     modal.style.top = 0;
  151.                     modal.style.bottom = (screen_height - actual_viewport) + 'px';
  152.                 }
  153.  
  154.             }
  155.  
  156.             if (modal.getBoundingClientRect().y !== 0) { // A little off
  157.  
  158.                 modal.style.top = (parseInt(modal.style.top) - modal.getBoundingClientRect().y) + 'px';
  159.                 modal.style.bottom = (parseInt(modal.style.bottom) + modal.getBoundingClientRect().y) + 'px';
  160.  
  161.             }
  162.  
  163.             if ((actual_viewport + parseInt(modal.style.top) + parseInt(modal.style.bottom)) > screen_height) { // Extra bug when scrolled near the bottom
  164.  
  165.                 modal.style.bottom = (screen_height - actual_viewport - parseInt(modal.style.top)) + 'px';
  166.  
  167.             }
  168.  
  169.         }
  170.  
  171.     }
  172.  
  173.     var previousScrollY = window.scrollY;
  174.  
  175.  
  176.     $('document').ready(function () {
  177.  
  178.         $('body').find("#ModalInBasket").on('shown.bs.modal', function (e) {
  179.             var active_modal = '#ModalInBasket .modal-dialog';
  180.             $(active_modal)[0].scrollTop = 0;
  181.             document.documentElement.classList.add('no-scroll');
  182.             disableBodyScroll(true, active_modal);
  183.             previousScrollY = window.scrollY;
  184.             adjustModal();
  185.             window.addEventListener('resize', adjustModal, {passive: false});
  186.             $(active_modal).on('click', function (e) {
  187.  
  188.                 if ($(e.target).hasClass('modal-dialog')) {
  189.  
  190.                     $(e.target.parentNode).modal('hide');
  191.  
  192.                 }
  193.  
  194.             });
  195.  
  196.         });
  197.  
  198.  
  199.         $('body').find("#ModalInBasket").on('hidden.bs.modal', function (e) {
  200.             document.documentElement.classList.remove('no-scroll');
  201.             $('body').animate({scrollTop: previousScrollY}, 200);
  202.             disableBodyScroll(false, '.modal-dialog');
  203.             window.removeEventListener('resize', adjustModal, {passive: false});
  204.  
  205.         });
  206.     })
  207.  
  208. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement