jquery.metisMenu.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * metismenu - v1.1.3
  3. * Easy menu jQuery plugin for Twitter Bootstrap 3
  4. * https://github.com/onokumus/metisMenu
  5. *
  6. * Made by Osman Nuri Okumus
  7. * Under MIT License
  8. */
  9. ;(function($, window, document, undefined) {
  10. var pluginName = "metisMenu",
  11. defaults = {
  12. toggle: true,
  13. doubleTapToGo: false
  14. };
  15. function Plugin(element, options) {
  16. this.element = $(element);
  17. this.settings = $.extend({}, defaults, options);
  18. this._defaults = defaults;
  19. this._name = pluginName;
  20. this.init();
  21. }
  22. Plugin.prototype = {
  23. init: function() {
  24. var $this = this.element,
  25. $toggle = this.settings.toggle,
  26. obj = this;
  27. if (this.isIE() <= 9) {
  28. $this.find("li.active").has("ul").children("ul").collapse("show");
  29. $this.find("li").not(".active").has("ul").children("ul").collapse("hide");
  30. } else {
  31. $this.find("li.active").has("ul").children("ul").addClass("collapse in");
  32. $this.find("li").not(".active").has("ul").children("ul").addClass("collapse");
  33. }
  34. //add the "doubleTapToGo" class to active items if needed
  35. if (obj.settings.doubleTapToGo) {
  36. $this.find("li.active").has("ul").children("a").addClass("doubleTapToGo");
  37. }
  38. $(document.body).on("click" + "." + pluginName,"#"+$this[0].id+' li a', function(e) {
  39. if(!$(this).parent().has('ul')) {
  40. return false;
  41. }
  42. e.preventDefault();
  43. //Do we need to enable the double tap
  44. if (obj.settings.doubleTapToGo) {
  45. //if we hit a second time on the link and the href is valid, navigate to that url
  46. if (obj.doubleTapToGo($(this)) && $(this).attr("href") !== "#" && $(this).attr("href") !== "") {
  47. e.stopPropagation();
  48. document.location = $(this).attr("href");
  49. return;
  50. }
  51. }
  52. $(this).parent("li").toggleClass("active").children("ul").collapse("toggle");
  53. if ($toggle) {
  54. $(this).parent("li").siblings().removeClass("active").children("ul.in").collapse("hide");
  55. }
  56. });
  57. },
  58. isIE: function() { //https://gist.github.com/padolsey/527683
  59. var undef,
  60. v = 3,
  61. div = document.createElement("div"),
  62. all = div.getElementsByTagName("i");
  63. while (
  64. div.innerHTML = "<!--[if gt IE " + (++v) + "]><i></i><![endif]-->",
  65. all[0]
  66. ) {
  67. return v > 4 ? v : undef;
  68. }
  69. },
  70. //Enable the link on the second click.
  71. doubleTapToGo: function(elem) {
  72. var $this = this.element;
  73. //if the class "doubleTapToGo" exists, remove it and return
  74. if (elem.hasClass("doubleTapToGo")) {
  75. elem.removeClass("doubleTapToGo");
  76. return true;
  77. }
  78. //does not exists, add a new class and return false
  79. if (elem.parent().children("ul").length) {
  80. //first remove all other class
  81. $this.find(".doubleTapToGo").removeClass("doubleTapToGo");
  82. //add the class on the current element
  83. elem.addClass("doubleTapToGo");
  84. return false;
  85. }
  86. },
  87. remove: function() {
  88. this.element.off("." + pluginName);
  89. this.element.removeData(pluginName);
  90. }
  91. };
  92. $.fn[pluginName] = function(options) {
  93. this.each(function () {
  94. var el = $(this);
  95. if (el.data(pluginName)) {
  96. el.data(pluginName).remove();
  97. }
  98. el.data(pluginName, new Plugin(this, options));
  99. });
  100. return this;
  101. };
  102. })(jQuery, window, document);