Advertisement
StuBob

media-modal.js

May 4th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. console.log("Got Here");
  2.  
  3. var CS_Media = {};
  4.  
  5.  
  6. // store modal dialog's top margin - only have to set it in CSS :)
  7. CS_Media.modalMarginTop = $(".media-modal__dialog").css("margin-top");
  8.  
  9. // Modal function for responsive modal that can handle any content
  10. CS_Media.setupModals = function() {
  11. var $body = $("body");
  12.  
  13. $(".media-modal").each(function() {
  14. var $this = $(this);
  15.  
  16. $this.on("openModal", function() {
  17. $this.addClass("media-modal--is-open");
  18. $body.addClass("no-scroll");
  19. // center modal dialog vertically
  20. CS_Media.vertCenterModal();
  21. })
  22. .on("closeModal", function() {
  23. $this.removeClass("media-modal--is-open");
  24. $body.removeClass("no-scroll");
  25. $this.find(".media-modal__dialog").css("margin-top", CS_Media.modalMarginTop);
  26. $(".video-placeholder").empty();
  27. $(".media-placeholder").empty();
  28. })
  29. .on("click",".js-close-modal", function(e){
  30. e.preventDefault();
  31. $this.trigger("closeModal");
  32. });
  33.  
  34. // ESC key can close the modal
  35. $body.keyup(function(e) {
  36. if (e.which===27) {
  37. $this.trigger("closeModal");
  38. }
  39. });
  40. });
  41.  
  42. $body.on("click", ".js-open-modal", function(e) {
  43. e.preventDefault();
  44. // open modal by putting ID in data-id attribute or href attribute
  45. var $this = $(this),
  46. videoKey = $this.attr('href').slice(1),
  47. media = $this.data('media'),
  48. id = $this.data('id') || videoKey;
  49.  
  50. if (videoKey) {
  51. $(".video-placeholder").mediaLoader("open", videoKey);
  52. } else if (media) {
  53. $(".media-placeholder").html(mediaList[media]).show();
  54. }
  55.  
  56. $('#' + id).trigger("openModal");
  57. });
  58. };
  59.  
  60. // center the open modal vertically
  61. CS_Media.vertCenterModal = function() {
  62. var modal = $('.media-modal--is-open .media-modal__dialog'),
  63. windowHeight = $(window).height(),
  64. modalHeight = modal.outerHeight();
  65.  
  66. // center only if window is taller than the modal
  67. if (windowHeight > modalHeight) {
  68. modal.css("margin-top", (windowHeight - modalHeight) / 2);
  69. } else {
  70. modal.css("margin-top", CS_Media.modalMarginTop);
  71. }
  72. };
  73.  
  74. CS_Media.init = function() {
  75. CS_Media.setupModals();
  76. $(".video-placeholder").mediaLoader({
  77. mediaList: window.videoList,
  78. useHash: false,
  79. playerOptions: {
  80. "wmode":"transparent",
  81. "isVid":"true",
  82. "isUI":"true",
  83. "dynamicStreaming":"true",
  84. "bgcolor":"#fff",
  85. "autoStart":"true"
  86. }
  87. });
  88.  
  89. $(window).on("resize",function(){
  90. if ($(".media-modal--is-open").length) {
  91. clearTimeout(CS_Media.modalResizeTimer);
  92. CS_Media.modalResizeTimer = setTimeout(function(){
  93. // keep modal vertically centered
  94. CS_Media.vertCenterModal();
  95. },30);
  96. }
  97. });
  98. };
  99.  
  100. $(function() {
  101. CS_Media.init();
  102. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement