Guest User

Untitled

a guest
Jul 18th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. // Title: modal_queue.js
  2. // Conatins the ModalQueue class.
  3. //
  4. // Original Author:
  5. // Michael Behan
  6.  
  7. // Class: ModalQueue
  8. var ModalQueue = ( function () {
  9. // Can be modified via ModalQueue.config.option = value;
  10. var config = {
  11. modal_id_prefix: 'modal'
  12. , close_dom_id_prefix: 'btnCloseModal'
  13. , container_class: 'modal_container'
  14. , height: '350px'
  15. , width: '600px'
  16. , fade_in_time: 500 // In ms
  17. , fade_out_time: 300 // In ms
  18. , mask_id: '#mask'
  19. , mask_opacity: 0.6 // 60%
  20. , show_publish: true // show Facebook publish button?
  21. };
  22.  
  23. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. // Private Variables:
  25. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. var queue = [];
  27. var i = 0;
  28. var j = 0;
  29.  
  30. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. // Public Methods
  32. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33.  
  34. // Method: add
  35. // Add content the modal queue
  36. //
  37. // Parameters:
  38. // html - Required. Markup for the modal being added
  39. // width - Optional. Width of modal in pixels.
  40. // height - Optional. Height of modal in pixels.
  41. function add (html, width, height) {
  42. var domId = config.modal_id_prefix + i.toString();
  43. var markup = '<div class="modal">'
  44. + ' <div id="' + config.close_dom_id_prefix + i.toString() + '" class="close close_modal" onclick="ModalQueue.showNextModal();"></div>'
  45. + ' <div class="wrapper">'
  46. + ' <div id="modalContent" class="content">' +
  47. + html
  48. + ' <a class="modal_fb_publish" href="#" onclick="TWOFace.publish();"><img src="/img/facebook/fb_publish.png" alt="Publish to Facebook" /></a></div><!-- end content -->'
  49. + ' </div><!-- end wrapper -->'
  50. + '</div><!-- end modal -->';
  51.  
  52. // Create wrapper
  53. var el = document.createElement('div');
  54. el.id = domId;
  55. el.className = config.container_class;
  56. el.innerHTML = markup;
  57. el.style.display = 'none';
  58. $(el).css('height', (height !== null ? height: config.height));
  59. $(el).css('width', (width !== null ? width: config.width));
  60.  
  61. //get the height and width of the window, and modal
  62. var window_width = $(window).width();
  63. var window_height = $(window).height();
  64. var modal_height = parseInt($(el).css('height'), 10);
  65. var modal_width = parseInt($(el).css('width'), 10);
  66.  
  67. // alert(modal_width + 'x' + modal_height);
  68.  
  69. //calculate top and left offset needed for centering
  70. var top = (window_height-modal_height)/2;
  71. var left = (window_width-modal_width)/2;
  72.  
  73. //apply new top and left css values
  74. $(el).css({'top' : top , 'left' : left});
  75.  
  76. i += 1;
  77.  
  78. // Add modal to the queue
  79. queue.push(el);
  80. }
  81.  
  82. // Method: play
  83. // Plays through the queue, showing each modal in the order it was added.
  84. function play () {
  85. showMask();
  86. showNextModal();
  87. }
  88. // END public
  89.  
  90. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91. // Protectd Methods
  92. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93.  
  94. // Method: showNextModal
  95. // Shows the next modal in the queue
  96. function showNextModal () {
  97. show_modal(j);
  98. showCloseButton(j);
  99. j++;
  100. }
  101. // END protected
  102.  
  103.  
  104. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. // Private Methods
  106. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107.  
  108. // Method: show_modal
  109. // Shows a modal window from the queue.
  110. //
  111. // Parameters:
  112. // num - Integer, index from the queue.
  113. function show_modal (num) {
  114. // Remove last modal from DOM
  115. if (num > 0) {
  116. $(queue[num-1]).fadeOut(config.fade_out_time);
  117. //$(queue[num-1]).remove();
  118. }
  119.  
  120. var el = queue[num];
  121.  
  122. // If no more elements, stop.
  123. if (el === undefined) {
  124. stop();
  125. }
  126.  
  127. //show the modal window
  128. $(document.body).append(el);
  129. $(el).fadeIn(config.fade_in_time);
  130.  
  131. return true;
  132. }
  133.  
  134. // Method: showCloseButton
  135. // Shows the close button. If we're on the first modal, the close
  136. // button will animate with a drop/bounce.
  137. function showCloseButton (num) {
  138. // Animate the close button into view:
  139. if (num === 0) {
  140. $('#btnCloseModal' + num.toString()).animate({top: '-12px'}, 800);
  141. // Give it a little bounce:
  142. $('#btnCloseModal' + num.toString()).animate({top: '-17px'}, 100);
  143. } else {
  144. $('#btnCloseModal' + num.toString()).css('top', '-17px');
  145. }
  146. }
  147.  
  148. // Method: showMask
  149. // Shows the mask layer.
  150. function showMask () {
  151. var document_height = $(document).height();
  152. $(config.mask_id).css({'height': document_height}); // IE6 requires document height to fully expand the height of the scrollable area
  153. $(config.mask_id).css({'display': 'block', opacity : 0});
  154. $(config.mask_id).fadeTo(config.fade_in_time, config.mask_opacity);
  155. }
  156.  
  157. // Method: hideMask
  158. // Hides mask layer.
  159. function hideMask () {
  160. $(config.mask_id).hide();
  161. }
  162.  
  163. // Method: stop
  164. // Hides the mask layer and resets conters.
  165. function stop () {
  166. hideMask();
  167. i = j = 0; // reset counters incase we want to call play() again.
  168. }
  169. // END private
  170.  
  171.  
  172. // >>>
  173. // Expose public and protected methods and attributes:
  174. // >>>
  175. return {
  176. add: add
  177. , play: play
  178. , showNextModal: showNextModal
  179. , config: config
  180. };
  181.  
  182. }) ();
  183.  
  184.  
  185. // Add some items to the queue and play them:
  186. ModalQueue.add('<h1>Hello 1</h1>');
  187. ModalQueue.add('<h1>Hello 2</h1>');
  188. ModalQueue.add('<h1>Hello 3</h1>');
  189. ModalQueue.play();
Add Comment
Please, Sign In to add comment