smf_jquery_plugins.js 15 KB

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