language-selector.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*!
  2. * Language Selector JS plugin
  3. * Copyright 2017 Clement G., Inc.
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. */
  6. var dropdownmenu={
  7. animspeed: 200, //reveal animation speed (in milliseconds)
  8. showhidedelay: [150, 150], //delay before menu appears and disappears when mouse rolls over it, in milliseconds
  9. //***** NO NEED TO EDIT BEYOND HERE
  10. builtdropdownids: [], //ids of dropdown already built (to prevent repeated building of same dropdown)
  11. stubboxenable: false,
  12. showbox:function($, $dropdown){
  13. this.stubboxenable = false
  14. clearTimeout($dropdown.data('timers').hidetimer)
  15. $dropdown.data('timers').showtimer=setTimeout(function(){$dropdown.show(dropdownmenu.animspeed)}, this.showhidedelay[0])
  16. },
  17. hidebox:function($, $dropdown){
  18. if(this.stubboxenable == false) {
  19. clearTimeout($dropdown.data('timers').showtimer)
  20. $dropdown.data('timers').hidetimer=setTimeout(function(){$dropdown.hide(100)}, this.showhidedelay[1]) //hide dropdown plus all of its sub ULs
  21. }
  22. },
  23. stubbox:function($, $dropdown){
  24. this.stubboxenable = true
  25. clearTimeout($dropdown.data('timers').hidetimer)
  26. $dropdown.data('timers').showtimer=setTimeout(function(){$dropdown.show(dropdownmenu.animspeed)}, this.showhidedelay[0])
  27. },
  28. builddropdown:function($, $menu, $target){
  29. $menu.css({display:'none'}).addClass('jqdropdown')
  30. $menu.bind('mouseenter', function(){
  31. clearTimeout($menu.data('timers').hidetimer)
  32. })
  33. $menu.bind('mouseleave', function(){ //hide menu when mouse moves out of it
  34. dropdownmenu.hidebox($, $menu)
  35. })
  36. $menu.data('dimensions', {w:$menu.outerWidth(), h:$menu.outerHeight()}) //remember main menu's dimensions
  37. $menu.data('timers', {})
  38. this.builtdropdownids.push($menu.get(0).id) //remember id of dropdown that was just built
  39. },
  40. init:function($, $target, $dropdown){
  41. if (this.builtdropdownids.length==0){ //only bind click event to document once
  42. $(document).bind("click", function(e){
  43. if (e.button==0){ //hide all dropdown (and their sub ULs) when left mouse button is clicked
  44. $('.jqdropdown').find('ul').andSelf().hide()
  45. }
  46. })
  47. }
  48. if (jQuery.inArray($dropdown.get(0).id, this.builtdropdownids)==-1) //if this dropdown hasn't been built yet
  49. this.builddropdown($, $dropdown, $target)
  50. if ($target.parents().filter('ul.jqdropdown').length>0) //if $target matches an element within the dropdown markup, don't bind ondropdown to that element
  51. return
  52. $target.bind("mouseenter", function(e){
  53. dropdownmenu.showbox($, $dropdown)
  54. })
  55. $target.bind("mouseleave", function(e){
  56. dropdownmenu.hidebox($, $dropdown)
  57. })
  58. $target.bind("click", function(e){
  59. dropdownmenu.stubbox($, $dropdown)
  60. })
  61. }
  62. };
  63. jQuery.fn.adddropdown=function(dropdownid){
  64. var $=jQuery
  65. return this.each(function(){ //return jQuery obj
  66. var $target=$(this)
  67. if ($('#'+dropdownid).length==1) //check dropdown is defined
  68. dropdownmenu.init($, $target, $('#'+dropdownid))
  69. })
  70. };
  71. //By default, add dropdown to anchor links with attribute "data-dropdown"
  72. jQuery(document).ready(function($){
  73. var $anchors=$('*[data-dropdown]')
  74. $anchors.each(function(){
  75. $(this).adddropdown(this.getAttribute('data-dropdown'))
  76. })
  77. });