smf_jquery_plugins.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * SMFtooltip, Basic JQuery function to provide styled tooltips
  3. *
  4. * - will use the hoverintent plugin if available
  5. * - shows the tooltip in a div with the class defined in tooltipClass
  6. * - moves all selector titles to a hidden div and removes the title attribute to
  7. * prevent any default browser actions
  8. * - attempts to keep the tooltip on screen
  9. *
  10. * Simple Machines Forum (SMF)
  11. *
  12. * @package SMF
  13. * @author Simple Machines http://www.simplemachines.org
  14. * @copyright 2011 Simple Machines
  15. * @license http://www.simplemachines.org/about/smf/license.php BSD
  16. *
  17. * @version 2.1 Alpha 1
  18. *
  19. */
  20. (function($) {
  21. $.fn.SMFtooltip = function(oInstanceSettings) {
  22. $.fn.SMFtooltip.oDefaultsSettings = {
  23. followMouse: 1,
  24. hoverIntent: {sensitivity: 10, interval: 300, timeout: 50},
  25. positionTop: 12,
  26. positionLeft: 12,
  27. tooltipID: 'smf_tooltip',
  28. tooltipClass: 'tooltip',
  29. tooltipTextID: 'smf_tooltipText'
  30. };
  31. // account for any user options
  32. var oSettings = $.extend({}, $.fn.SMFtooltip.oDefaultsSettings , oInstanceSettings || {});
  33. // move passed selector titles to a hidden span, then remove the selector title to prevent any default browser actions
  34. $(this).each(function()
  35. {
  36. var sTitle = $('<span class="' + oSettings.tooltipClass + '">' + this.title + '</span>').hide();
  37. $(this).append(sTitle).attr('title', '');
  38. });
  39. // determine where we are going to place the tooltip, while trying to keep it on screen
  40. var positionTooltip = function(event)
  41. {
  42. var iPosx = 0;
  43. var iPosy = 0;
  44. if (!event)
  45. var event = window.event;
  46. if (event.pageX || event.pageY)
  47. {
  48. iPosx = event.pageX;
  49. iPosy = event.pageY;
  50. }
  51. else if (event.clientX || event.clientY)
  52. {
  53. iPosx = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  54. iPosy = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  55. }
  56. var oPosition = {
  57. x: iPosx + oSettings.positionLeft,
  58. y: iPosy + oSettings.positionTop,
  59. w: $('#' + oSettings.tooltipID).width(),
  60. h: $('#' + oSettings.tooltipID).height()
  61. }
  62. var oLimits = {
  63. x: $(window).scrollLeft(),
  64. y: $(window).scrollTop(),
  65. w: $(window).width() - 20,
  66. h: $(window).height() - 20
  67. };
  68. // don't go off screen
  69. if ((oPosition.y + oPosition.h > oLimits.y + oLimits.h) && (oPosition.x + oPosition.w > oLimits.x + oLimits.w))
  70. {
  71. oPosition.x = (oPosition.x - oPosition.w) - 45;
  72. oPosition.y = (oPosition.y - oPosition.h) - 45;
  73. }
  74. else if ((oPosition.x + oPosition.w) > (oLimits.x + oLimits.w))
  75. {
  76. oPosition.x = oPosition.x - (((oPosition.x + oPosition.w) - (oLimits.x + oLimits.w)) + 20);
  77. }
  78. else if (oPosition.y + oPosition.h > oLimits.y + oLimits.h)
  79. {
  80. oPosition.y = oPosition.y - (((oPosition.y + oPosition.h) - (oLimits.y + oLimits.h)) + 20);
  81. }
  82. // finally set the position we determined
  83. $('#' + oSettings.tooltipID).css({'left': oPosition.x + 'px', 'top': oPosition.y + 'px'});
  84. }
  85. // used to show a tooltip
  86. var showTooltip = function(){
  87. $('#' + oSettings.tooltipID + ' #' + oSettings.tooltipTextID).show();
  88. }
  89. // used to hide a tooltip
  90. var hideTooltip = function(valueOfThis){
  91. $('#' + oSettings.tooltipID).fadeOut('fast').trigger("unload").remove();
  92. }
  93. // for all of the elements that match the selector on the page, lets set up some actions
  94. return this.each(function(index)
  95. {
  96. // if we find hoverIntent use it
  97. if ($.fn.hoverIntent)
  98. {
  99. $(this).hoverIntent({
  100. sensitivity: oSettings.hoverIntent.sensitivity,
  101. interval: oSettings.hoverIntent.interval,
  102. over: smf_tooltip_on,
  103. timeout: oSettings.hoverIntent.timeout,
  104. out: smf_tooltip_off
  105. });
  106. }
  107. else
  108. {
  109. // plain old hover it is
  110. $(this).hover(smf_tooltip_on, smf_tooltip_off);
  111. }
  112. // create the on tip action
  113. function smf_tooltip_on(event)
  114. {
  115. // Grab the text from the hidden span element we created on page load
  116. if ($(this).children('.' + oSettings.tooltipClass).text())
  117. {
  118. // create a ID'ed div with our style class that holds the tooltip info, hidden for now
  119. $('body').append('<div id="' + oSettings.tooltipID + '" class="' + oSettings.tooltipClass + '"><div id="' + oSettings.tooltipTextID + '" style="display:none;"></div></div>');
  120. // load information in to our newly created div
  121. var $tt = $('#' + oSettings.tooltipID);
  122. var $ttContent = $('#' + oSettings.tooltipID + ' #' + oSettings.tooltipTextID);
  123. // set the in the div and show it off
  124. positionTooltip(event);
  125. $ttContent.text($(this).children('.' + oSettings.tooltipClass).text());
  126. $tt.show();
  127. showTooltip();
  128. }
  129. return false;
  130. };
  131. // create the Bye bye tip
  132. function smf_tooltip_off(event)
  133. {
  134. hideTooltip(this);
  135. return false;
  136. };
  137. // create the tip move with the cursor
  138. if (oSettings.followMouse)
  139. {
  140. $(this).bind("mousemove", function(event){
  141. positionTooltip(event);
  142. return false;
  143. });
  144. }
  145. });
  146. };
  147. })(jQuery);
  148. /**
  149. * hoverIntent is similar to jQuery's built-in "hover" function except that
  150. * instead of firing the onMouseOver event immediately, hoverIntent checks
  151. * to see if the user's mouse has slowed down (beneath the sensitivity
  152. * threshold) before firing the onMouseOver event.
  153. *
  154. * hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
  155. * <http://cherne.net/brian/resources/jquery.hoverIntent.html>
  156. *
  157. * hoverIntent is currently available for use in all personal or commercial
  158. * projects under both MIT and GPL licenses. This means that you can choose
  159. * the license that best suits your project, and use it accordingly.
  160. *
  161. * // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
  162. * $("ul li").hoverIntent( showNav , hideNav );
  163. *
  164. * // advanced usage receives configuration object only
  165. * $("ul li").hoverIntent({
  166. * sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
  167. * interval: 100, // number = milliseconds of polling interval
  168. * over: showNav, // function = onMouseOver callback (required)
  169. * timeout: 0, // number = milliseconds delay before onMouseOut function call
  170. * out: hideNav // function = onMouseOut callback (required)
  171. * });
  172. *
  173. * @param f onMouseOver function || An object with configuration options
  174. * @param g onMouseOut function || Nothing (use configuration options object)
  175. * @author Brian Cherne brian(at)cherne(dot)net
  176. */
  177. /*
  178. * PLEASE READ THE FOLLOWING BEFORE PLAYING AROUND WITH ANYTHING. KTHNX.
  179. * SMF Dev copy - Antechinus - 20th October 2011.
  180. * Code has been tweaked to give responsive menus without compromising a11y.
  181. * If contemplating changes, testing for full functionality is essential or a11y will be degraded.
  182. * Since a11y is the whole point of this system, degradation is not at all desirable regardless of personal preferences.
  183. * If you do not understand the a11y advantages of this system, please ask before making changes.
  184. *
  185. * Full functionality means:
  186. * 1/ hoverIntent plugin functions so that drop menus do NOT open or close instantly when cursor touches first level anchor.
  187. * 2/ The drop menus should only open when the cursor actually stops on the first level anchor, or is moving very slowly.
  188. * 3/ There should be a delay before the drop menus close on mouseout, for people with less than perfect tracking ability.
  189. * 4/ Settings for custom tooltips will be done separately in another file.
  190. */
  191. (function($) {
  192. $.fn.hoverIntent = function(f,g) {
  193. // default configuration options
  194. var cfg = {
  195. sensitivity: 7,
  196. interval: 100,
  197. timeout: 0
  198. };
  199. // override configuration options with user supplied object
  200. cfg = $.extend(cfg, g ? { over: f, out: g } : f );
  201. // instantiate variables
  202. // cX, cY = current X and Y position of mouse, updated by mousemove event
  203. // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
  204. var cX, cY, pX, pY;
  205. // A private function for getting mouse position
  206. var track = function(ev) {
  207. cX = ev.pageX;
  208. cY = ev.pageY;
  209. };
  210. // A private function for comparing current and previous mouse position
  211. var compare = function(ev,ob) {
  212. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  213. // compare mouse positions to see if they've crossed the threshold
  214. if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
  215. $(ob).unbind("mousemove",track);
  216. // set hoverIntent state to true (so mouseOut can be called)
  217. ob.hoverIntent_s = 1;
  218. return cfg.over.apply(ob,[ev]);
  219. } else {
  220. // set previous coordinates for next time
  221. pX = cX; pY = cY;
  222. // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
  223. ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
  224. }
  225. };
  226. // A private function for delaying the mouseOut function
  227. var delay = function(ev,ob) {
  228. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  229. ob.hoverIntent_s = 0;
  230. return cfg.out.apply(ob,[ev]);
  231. };
  232. // A private function for handling mouse 'hovering'
  233. var handleHover = function(e) {
  234. // next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
  235. var p = (e.type == "mouseenter" ? e.fromElement : e.toElement) || e.relatedTarget;
  236. while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
  237. if ( p == this ) { return false; }
  238. // copy objects to be passed into t (required for event object to be passed in IE)
  239. var ev = jQuery.extend({},e);
  240. var ob = this;
  241. // cancel hoverIntent timer if it exists
  242. if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
  243. // if e.type == "mouseenter"
  244. if (e.type == "mouseenter") {
  245. // set "previous" X and Y position based on initial entry point
  246. pX = ev.pageX; pY = ev.pageY;
  247. // update "current" X and Y position based on mousemove
  248. $(ob).bind("mousemove",track);
  249. // start polling interval (self-calling timeout) to compare mouse coordinates over time
  250. if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
  251. // else e.type == "mouseleave"
  252. } else {
  253. // unbind expensive mousemove event
  254. $(ob).unbind("mousemove",track);
  255. // if hoverIntent state is true, then call the mouseOut function after the specified delay
  256. if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
  257. }
  258. };
  259. // bind the function to the two event listeners
  260. return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover);
  261. };
  262. })(jQuery);
  263. /*
  264. * Superfish v1.4.8 - jQuery menu widget
  265. * Copyright (c) 2008 Joel Birch
  266. *
  267. * Dual licensed under the MIT and GPL licenses:
  268. * http://www.opensource.org/licenses/mit-license.php
  269. * http://www.gnu.org/licenses/gpl.html
  270. *
  271. * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
  272. */
  273. /*
  274. * PLEASE READ THE FOLLOWING BEFORE PLAYING AROUND WITH ANYTHING. KTHNX.
  275. * Dev copy. Antechinus - 20th October 2011.
  276. * Code has been tweaked to remove stuff we do not need (IE7 fix, etc).
  277. * Remaining code appears to be essential for full functionality.
  278. * If contemplating changes, testing for full functionality is essential or a11y will be degraded.
  279. * Since a11y is the whole point of this system, degradation is not at all desirable regardless of personal preferences.
  280. * If you do not understand the a11y advantages of this system, please ask before making changes.
  281. *
  282. * Full functionality means:
  283. * 1/ hoverIntent plugin functions so that drop menus do NOT open or close instantly when cursor touches first level anchor.
  284. * 2/ The drop menus should only open when the cursor actually stops on the first level anchor, or is moving very slowly.
  285. * 3/ There should be a delay before the drop menus close on mouseout, for people with less than perfect tracking ability.
  286. * 4/ The drop menus must remain fully accessible via keyboard navigation (eg: the Tab key).
  287. */
  288. ;(function($){
  289. $.fn.superfish = function(op){
  290. var sf = $.fn.superfish,
  291. c = sf.c,
  292. over = function(){
  293. var $$ = $(this), menu = getMenu($$);
  294. clearTimeout(menu.sfTimer);
  295. $$.showSuperfishUl().siblings().hideSuperfishUl();
  296. },
  297. out = function(){
  298. var $$ = $(this), menu = getMenu($$), o = sf.op;
  299. clearTimeout(menu.sfTimer);
  300. menu.sfTimer=setTimeout(function(){
  301. o.retainPath=($.inArray($$[0],o.$path)>-1);
  302. $$.hideSuperfishUl();
  303. if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
  304. },o.delay);
  305. },
  306. getMenu = function($menu){
  307. var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
  308. sf.op = sf.o[menu.serial];
  309. return menu;
  310. },
  311. // This next line is essential, despite the other code for arrows being removed.
  312. // Changing the next line WILL break hoverIntent functionality. Very bad.
  313. addArrow = function($a){$a.addClass(c.anchorClass)};
  314. return this.each(function() {
  315. var s = this.serial = sf.o.length;
  316. var o = $.extend({},sf.defaults,op);
  317. var h = $.extend({},sf.hoverdefaults,{over: over, out: out},op);
  318. o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
  319. $(this).addClass([o.hoverClass,c.bcClass].join(' '))
  320. .filter('li:has(ul)').removeClass(o.pathClass);
  321. });
  322. sf.o[s] = sf.op = o;
  323. $('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](($.fn.hoverIntent && !o.disableHI) ? (h) : (over,out)).each(function() {})
  324. .not('.'+c.bcClass)
  325. .hideSuperfishUl();
  326. var $a = $('a',this);
  327. $a.each(function(i){
  328. var $li = $a.eq(i).parents('li');
  329. $a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
  330. });
  331. o.onInit.call(this);
  332. }).each(function() {
  333. var menuClasses = [c.menuClass];
  334. $(this).addClass(menuClasses.join(' '));
  335. });
  336. };
  337. var sf = $.fn.superfish;
  338. sf.o = [];
  339. sf.op = {};
  340. sf.c = {
  341. bcClass : 'sf-breadcrumb',
  342. menuClass : 'sf-js-enabled',
  343. anchorClass : 'sf-with-ul',
  344. };
  345. sf.defaults = {
  346. hoverClass : 'sfhover',
  347. pathClass : 'current',
  348. pathLevels : 1,
  349. delay : 700,
  350. animation : {opacity:'show', height:'show'},
  351. speed : 300,
  352. disableHI : false, // Leave as false. True disables hoverIntent detection (not good).
  353. onInit : function(){}, // callback functions
  354. onBeforeShow: function(){},
  355. onShow : function(){},
  356. onHide : function(){}
  357. };
  358. sf.hoverdefaults = {
  359. sensitivity : 10,
  360. interval : 40,
  361. timeout : 1
  362. };
  363. $.fn.extend({
  364. hideSuperfishUl : function(){
  365. var o = sf.op,
  366. not = (o.retainPath===true) ? o.$path : '';
  367. o.retainPath = false;
  368. var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
  369. .find('>ul').hide().css('opacity','0');
  370. o.onHide.call($ul);
  371. return this;
  372. },
  373. showSuperfishUl : function(){
  374. var o = sf.op,
  375. sh = sf.c,
  376. $ul = this.addClass(o.hoverClass)
  377. .find('>ul:hidden').css('opacity','1');
  378. o.onBeforeShow.call($ul);
  379. $ul.animate(o.animation,o.speed,function(){o.onShow.call($ul);});
  380. return this;
  381. }
  382. });
  383. })(jQuery);