PageRenderTime 29ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascripts/jquery/plugins/jquery.mouseyDialog.js

https://github.com/acslocum/prfbbq
JavaScript | 187 lines | 117 code | 21 blank | 49 comment | 20 complexity | f5e00e4f3f1812afd2280ef5fb21548e MD5 | raw file
  1. /*
  2. jQuery mouseyDialog Plugin
  3. * Version 1.0
  4. * 04-30-2010
  5. * URL: http://github.com/mdbiscan/mouseyDialog
  6. * Author: M.Biscan
  7. * requires jQuery1.4.2
  8. Copyright (c) 2010 M.Biscan
  9. Permission is hereby granted, free of charge, to any person obtaining
  10. a copy of this software and associated documentation files (the
  11. "Software"), to deal in the Software without restriction, including
  12. without limitation the rights to use, copy, modify, merge, publish,
  13. distribute, sublicense, and/or sell copies of the Software, and to
  14. permit persons to whom the Software is furnished to do so, subject to
  15. the following conditions:
  16. The above copyright notice and this permission notice shall be
  17. included in all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. (function($){
  27. $.fn.mouseyDialog = function(options) {
  28. var settings = $.extend({}, $.fn.mouseyDialog.defaults, options);
  29. return this.each(function() {
  30. var $anchor = $(this),
  31. $url = $anchor.attr('href'),
  32. $dialog = getID($url);
  33. if(settings.eventType == 'click') {
  34. var $closeButton = $('<a href="#" class="mouseyDialog_close">close</a>').appendTo($dialog);
  35. }
  36. ///////////
  37. // Setup //
  38. ///////////
  39. $dialog
  40. .hide()
  41. .css({position:'absolute', zIndex:settings.zIndex})
  42. .addClass(settings.customClass)
  43. .appendTo('body');
  44. ////////////
  45. // Events //
  46. ////////////
  47. // Custom event
  48. $anchor.bind('toggleDialog', function(event, x, y) {
  49. if($dialog.hasClass('visible')) {
  50. closeDialog($dialog);
  51. } else {
  52. openDialog($dialog, x, y);
  53. }
  54. });
  55. var eventType = (settings.eventType == 'hover' ? 'mouseenter' : 'click');
  56. $anchor[eventType](function(event) {
  57. // Window
  58. var windowWidth = $(window).width(),
  59. windowHeight = $(window).height();
  60. // Screen
  61. var clientX = event.clientX,
  62. clientY = event.clientY;
  63. // Dialog
  64. var dialogWidth = getDialogDimensions().width,
  65. dialogHeight = getDialogDimensions().height;
  66. // Mouse
  67. var mouseX = event.pageX,
  68. mouseY = event.pageY;
  69. // X, Y
  70. var x = mouseX+settings.addOffset,
  71. y = mouseY+settings.addOffset;
  72. if((dialogWidth + clientX) > windowWidth) {
  73. x = mouseX-settings.addOffset-((dialogWidth + clientX)-windowWidth);
  74. }
  75. if((dialogHeight + clientY) > windowHeight) {
  76. y = mouseY-settings.addOffset-((dialogHeight + clientY)-windowHeight);
  77. }
  78. $(this).trigger('toggleDialog', [x, y]);
  79. var openDialog = $('.mouseyDialog.visible');
  80. if(openDialog.length == 1 && openDialog != $dialog) {
  81. closeDialog(openDialog);
  82. }
  83. return false;
  84. });
  85. if(settings.eventType == 'hover') {
  86. $anchor.click(function() {
  87. return false;
  88. });
  89. $anchor.mouseleave(function() {
  90. setTimeout(function() {
  91. if(!$dialog.hasClass('hover')) {
  92. closeDialog($dialog);
  93. }
  94. }, 150);
  95. });
  96. $dialog.hover(
  97. function() {
  98. $(this).addClass('hover');
  99. },
  100. function() {
  101. $(this).removeClass('hover');
  102. closeDialog(this);
  103. }
  104. );
  105. } else {
  106. $closeButton.click(function() {
  107. $anchor.trigger('toggleDialog');
  108. return false;
  109. });
  110. // Prevents the dialog from being closed when clicking inside it
  111. $dialog.click(function(event) {
  112. event.stopPropagation();
  113. });
  114. // Closes the dialog when clicking outside of it
  115. $(document).click(function(event) {
  116. if(event.target != this) {
  117. if($dialog.hasClass('visible')) {
  118. closeDialog($dialog);
  119. }
  120. }
  121. });
  122. }
  123. ///////////////////////
  124. // Private functions //
  125. ///////////////////////
  126. function getID(url) {
  127. var id = url.split("#");
  128. return $('#' + id[1]);
  129. };
  130. function getDialogDimensions() {
  131. $dialog.show();
  132. var height = $dialog.innerHeight(),
  133. width = $dialog.innerWidth();
  134. $dialog.hide();
  135. return {height:height, width:width};
  136. };
  137. function openDialog(dialog, x, y) {
  138. var animation = (settings.animation == 'slide' ? 'slideDown' : 'fadeIn');
  139. $(dialog).css({top:y, left:x})[animation](settings.animationSpeed, function() {
  140. $(this).addClass('visible');
  141. });
  142. };
  143. function closeDialog(dialog) {
  144. var animation = (settings.animation == 'slide' ? 'slideUp' : 'fadeOut');
  145. $(dialog)[animation](settings.animationSpeed, function() {
  146. $(this).removeClass('visible');
  147. });
  148. };
  149. });
  150. };
  151. ////////////////////
  152. // Default optons //
  153. ////////////////////
  154. $.fn.mouseyDialog.defaults = {
  155. zIndex:100,
  156. eventType:'click',
  157. addOffset:10,
  158. animation:'fade',
  159. animationSpeed:250,
  160. customClass:'mouseyDialog'
  161. };
  162. })(jQuery);