language-selector.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*!
  2. * Language Selector JS plugin
  3. * Copyright 2017 Clement G., Inc.
  4. * Licensed under MIT
  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').addBack().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. //By default, add dropdown to anchor links with attribute "data-dropdown"
  64. jQuery(document).ready(function($){
  65. jQuery.fn.adddropdown=function(dropdownid){
  66. var $=jQuery;
  67. return this.each(function(){ //return jQuery obj
  68. var $target=$(this);
  69. var $dropdownId = $('#'+dropdownid);
  70. if ($dropdownId.length === 1) //check dropdown is defined
  71. dropdownmenu.init($, $target, $dropdownId)
  72. })
  73. };
  74. var $anchors=$('*[data-dropdown]');
  75. $anchors.each(function(){
  76. $(this).adddropdown(this.getAttribute('data-dropdown'))
  77. })
  78. });