PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/assets/js/modal.js

https://gitlab.com/andregaldino/plataforma-hostel
JavaScript | 184 lines | 145 code | 27 blank | 12 comment | 16 complexity | 7555dde863b8942a389e4c1f4c9fd9a7 MD5 | raw file
  1. (function($) {
  2. var _stack = 0,
  3. _lastID = 0,
  4. _generateID = function() {
  5. _lastID++;
  6. return 'materialize-modal-overlay-' + _lastID;
  7. };
  8. var methods = {
  9. init : function(options) {
  10. var defaults = {
  11. opacity: 0.5,
  12. in_duration: 350,
  13. out_duration: 250,
  14. ready: undefined,
  15. complete: undefined,
  16. dismissible: true,
  17. starting_top: '4%',
  18. ending_top: '10%'
  19. };
  20. // Override defaults
  21. options = $.extend(defaults, options);
  22. return this.each(function() {
  23. var $modal = $(this);
  24. var modal_id = $(this).attr("id") || '#' + $(this).data('target');
  25. var closeModal = function() {
  26. var overlayID = $modal.data('overlay-id');
  27. var $overlay = $('#' + overlayID);
  28. $modal.removeClass('open');
  29. // Enable scrolling
  30. $('body').css({
  31. overflow: '',
  32. width: ''
  33. });
  34. $modal.find('.modal-close').off('click.close');
  35. $(document).off('keyup.modal' + overlayID);
  36. $overlay.velocity( { opacity: 0}, {duration: options.out_duration, queue: false, ease: "easeOutQuart"});
  37. // Define Bottom Sheet animation
  38. var exitVelocityOptions = {
  39. duration: options.out_duration,
  40. queue: false,
  41. ease: "easeOutCubic",
  42. // Handle modal ready callback
  43. complete: function() {
  44. $(this).css({display:"none"});
  45. // Call complete callback
  46. if (typeof(options.complete) === "function") {
  47. options.complete.call(this, $modal);
  48. }
  49. $overlay.remove();
  50. _stack--;
  51. }
  52. };
  53. if ($modal.hasClass('bottom-sheet')) {
  54. $modal.velocity({bottom: "-100%", opacity: 0}, exitVelocityOptions);
  55. }
  56. else {
  57. $modal.velocity(
  58. { top: options.starting_top, opacity: 0, scaleX: 0.7},
  59. exitVelocityOptions
  60. );
  61. }
  62. };
  63. var openModal = function($trigger) {
  64. var $body = $('body');
  65. var oldWidth = $body.innerWidth();
  66. $body.css('overflow', 'hidden');
  67. $body.width(oldWidth);
  68. if ($modal.hasClass('open')) {
  69. return;
  70. }
  71. var overlayID = _generateID();
  72. var $overlay = $('<div class="modal-overlay"></div>');
  73. lStack = (++_stack);
  74. // Store a reference of the overlay
  75. $overlay.attr('id', overlayID).css('z-index', 1000 + lStack * 2);
  76. $modal.data('overlay-id', overlayID).css('z-index', 1000 + lStack * 2 + 1);
  77. $modal.addClass('open');
  78. $("body").append($overlay);
  79. if (options.dismissible) {
  80. $overlay.click(function() {
  81. closeModal();
  82. });
  83. // Return on ESC
  84. $(document).on('keyup.modal' + overlayID, function(e) {
  85. if (e.keyCode === 27) { // ESC key
  86. closeModal();
  87. }
  88. });
  89. }
  90. $modal.find(".modal-close").on('click.close', function(e) {
  91. closeModal();
  92. });
  93. $overlay.css({ display : "block", opacity : 0 });
  94. $modal.css({
  95. display : "block",
  96. opacity: 0
  97. });
  98. $overlay.velocity({opacity: options.opacity}, {duration: options.in_duration, queue: false, ease: "easeOutCubic"});
  99. $modal.data('associated-overlay', $overlay[0]);
  100. // Define Bottom Sheet animation
  101. var enterVelocityOptions = {
  102. duration: options.in_duration,
  103. queue: false,
  104. ease: "easeOutCubic",
  105. // Handle modal ready callback
  106. complete: function() {
  107. if (typeof(options.ready) === "function") {
  108. options.ready.call(this, $modal, $trigger);
  109. }
  110. }
  111. };
  112. if ($modal.hasClass('bottom-sheet')) {
  113. $modal.velocity({bottom: "0", opacity: 1}, enterVelocityOptions);
  114. }
  115. else {
  116. $.Velocity.hook($modal, "scaleX", 0.7);
  117. $modal.css({ top: options.starting_top });
  118. $modal.velocity({top: options.ending_top, opacity: 1, scaleX: '1'}, enterVelocityOptions);
  119. }
  120. };
  121. // Reset handlers
  122. $(document).off('click.modalTrigger', 'a[href="#' + modal_id + '"], [data-target="' + modal_id + '"]');
  123. $(this).off('openModal');
  124. $(this).off('closeModal');
  125. // Close Handlers
  126. $(document).on('click.modalTrigger', 'a[href="#' + modal_id + '"], [data-target="' + modal_id + '"]', function(e) {
  127. options.starting_top = ($(this).offset().top - $(window).scrollTop()) /1.15;
  128. openModal($(this));
  129. e.preventDefault();
  130. }); // done set on click
  131. $(this).on('openModal', function() {
  132. var modal_id = $(this).attr("href") || '#' + $(this).data('target');
  133. openModal();
  134. });
  135. $(this).on('closeModal', function() {
  136. closeModal();
  137. });
  138. }); // done return
  139. },
  140. open : function() {
  141. $(this).trigger('openModal');
  142. },
  143. close : function() {
  144. $(this).trigger('closeModal');
  145. }
  146. };
  147. $.fn.modal = function(methodOrOptions) {
  148. if ( methods[methodOrOptions] ) {
  149. return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
  150. } else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
  151. // Default to "init"
  152. return methods.init.apply( this, arguments );
  153. } else {
  154. $.error( 'Method ' + methodOrOptions + ' does not exist on jQuery.modal' );
  155. }
  156. };
  157. })(jQuery);