jquery.metisMenu.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. //
  54. // if ($toggle) {
  55. // $(this).parent("li").siblings().removeClass("active").children("ul.in").collapse("hide");
  56. // }
  57. $(this).parent("li").addClass('active').children("ul").collapse("toggle")
  58. $(this).parent("li").siblings().removeClass("active").children("ul.in").collapse("hide");
  59. });
  60. },
  61. isIE: function() { //https://gist.github.com/padolsey/527683
  62. var undef,
  63. v = 3,
  64. div = document.createElement("div"),
  65. all = div.getElementsByTagName("i");
  66. while (
  67. div.innerHTML = "<!--[if gt IE " + (++v) + "]><i></i><![endif]-->",
  68. all[0]
  69. ) {
  70. return v > 4 ? v : undef;
  71. }
  72. },
  73. //Enable the link on the second click.
  74. doubleTapToGo: function(elem) {
  75. var $this = this.element;
  76. //if the class "doubleTapToGo" exists, remove it and return
  77. if (elem.hasClass("doubleTapToGo")) {
  78. elem.removeClass("doubleTapToGo");
  79. return true;
  80. }
  81. //does not exists, add a new class and return false
  82. if (elem.parent().children("ul").length) {
  83. //first remove all other class
  84. $this.find(".doubleTapToGo").removeClass("doubleTapToGo");
  85. //add the class on the current element
  86. elem.addClass("doubleTapToGo");
  87. return false;
  88. }
  89. },
  90. remove: function() {
  91. this.element.off("." + pluginName);
  92. this.element.removeData(pluginName);
  93. }
  94. };
  95. $.fn[pluginName] = function(options) {
  96. this.each(function () {
  97. var el = $(this);
  98. if (el.data(pluginName)) {
  99. el.data(pluginName).remove();
  100. }
  101. el.data(pluginName, new Plugin(this, options));
  102. });
  103. return this;
  104. };
  105. })(jQuery, window, document);