supermodal.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * SuperModal (https://github.com/GianlucaChiarani/SuperModal)
  3. * @version 0.8 beta1
  4. * @author Gianluca Chiarani
  5. * @license The MIT License (MIT)
  6. */
  7. (function ($) {
  8. $.supermodal = function( options ) {
  9. var settings = $.extend({
  10. backButton: true,
  11. maxWidth: '1024px',
  12. maxHeight: '100%',
  13. background: '#fff',
  14. color: '',
  15. shadow: '0 0 60px 10px rgba(0, 0, 0, 0.3)',
  16. containerBackground: 'rgba(0,0,0,0.2)',
  17. containerClass: '',
  18. containerZIndex: '100',
  19. showTitle: true,
  20. title: '',
  21. titleColor: '#fff',
  22. titleBackground: '#212529',
  23. closeButton: true,
  24. closeButtonIcon: 'fa fa-times',
  25. lazyLoading: false
  26. }, options );
  27. var selector = '[data-modal]';
  28. $(selector).click(function() {
  29. initSuperModal(this,settings);
  30. });
  31. };
  32. $(window).on('hashchange', function() {
  33. if (window.location.hash=='') {
  34. closeModal($('.supermodal-container'));
  35. } else {
  36. var hash = window.location.hash.replace('#','').split('_');
  37. $('.supermodal-container').each(function() {
  38. var level = $(this).attr('data-modal-level');
  39. if (level>hash[0]) {
  40. closeModal($(this));
  41. }
  42. });
  43. }
  44. });
  45. $(document).ready(function() {
  46. if (window.location.hash!='') {
  47. var hash = window.location.hash.replace('#','').split('_');
  48. $('[data-modal="'+hash[1]+'"').click();
  49. }
  50. });
  51. function initSuperModal(selector,settings) {
  52. var id = $(selector).attr('data-modal');
  53. var modalDiv = $('#'+id);
  54. var title;
  55. var localSettings = Object.assign({}, settings);
  56. for ([key, value] of Object.entries(settings)) {
  57. if ($(selector).attr('data-modal-'+key)) {
  58. var val = $(selector).attr('data-modal-'+key);
  59. if (val=='true') val=true;
  60. if (val=='false') val=false;
  61. localSettings[key] = val;
  62. }
  63. }
  64. if (modalDiv.length) {
  65. var modalHtml = modalDiv.html();
  66. var modalSelector = '.supermodal-container[id="modal_'+id+'"]';
  67. var level = 1;
  68. if ($('.supermodal-container').length)
  69. level = $('.supermodal-container').length+1;
  70. $('body').append('<div id="modal_'+id+'" data-modal-level="'+level+'" class="supermodal-container"><div class="supermodal-window"><div class="supermodal-body">'+modalHtml+'</div></div></div>');
  71. if (localSettings.showTitle) {
  72. if (localSettings.title)
  73. title = localSettings.title;
  74. else
  75. title = id;
  76. $(modalSelector+' .supermodal-window').prepend('<div class="supermodal-title">'+title+'</div>');
  77. }
  78. if (localSettings.closeButton) {
  79. $(modalSelector+' .supermodal-window .supermodal-title').prepend('<div class="supermodal-close"><i class="'+localSettings.closeButtonIcon+'"></i></div>');
  80. }
  81. $(modalSelector+' .supermodal-window').css('max-height',localSettings.maxHeight);
  82. $(modalSelector+' .supermodal-window').css('max-width',localSettings.maxWidth);
  83. $(modalSelector+' .supermodal-window').css('background',localSettings.background);
  84. $(modalSelector+' .supermodal-window').css('color',localSettings.color);
  85. $(modalSelector+' .supermodal-window').css('box-shadow',localSettings.shadow);
  86. $(modalSelector).css('background',localSettings.containerBackground);
  87. $(modalSelector).css('z-index',localSettings.containerZIndex);
  88. $(modalSelector+' .supermodal-window .supermodal-title').css('background',localSettings.titleBackground);
  89. $(modalSelector+' .supermodal-window .supermodal-title').css('color',localSettings.titleColor);
  90. if (localSettings.containerClass!='') {
  91. $(modalSelector).addClass(localSettings.containerClass);
  92. }
  93. $(modalSelector+' .supermodal-window').addClass('show');
  94. if (localSettings.backButton)
  95. window.location.hash = level+'_'+id;
  96. $(modalSelector+' .supermodal-window .supermodal-close, '+modalSelector+' [data-modal-close]').click(function() {
  97. closeButton($(modalSelector),localSettings);
  98. });
  99. $('html').css('overflow','hidden');
  100. $(modalSelector+' [data-modal]').click(function() {
  101. initSuperModal(this,localSettings)
  102. });
  103. if (localSettings.lazyLoading) {
  104. $(modalSelector+' img[data-src]').each(function() {
  105. $(this).attr('src',$(this).attr('data-src'));
  106. });
  107. }
  108. } else {
  109. console.log('SuperModal: invalid id');
  110. }
  111. }
  112. function closeButton(obj, settings = Array()) {
  113. if (settings.backButton)
  114. window.history.back();
  115. else
  116. closeModal(obj);
  117. }
  118. function closeModal(obj) {
  119. var window = false;
  120. var container = false;
  121. if (obj.find('.supermodal-window').length) {
  122. window = obj.find('.supermodal-window');
  123. container = obj;
  124. } else if (obj.parents('.supermodal-window:first').length) {
  125. window = obj.parents('.supermodal-window:first');
  126. container = obj.parents('.supermodal-container:first');
  127. }
  128. if (window) {
  129. window.removeClass('show').addClass('hide');
  130. setTimeout(function() {
  131. container.remove();
  132. }, 300);
  133. }
  134. }
  135. }(jQuery));