jquery.metisMenu.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $this.find("li").has("ul").children("a").on("click" + "." + pluginName, function(e) {
  39. e.preventDefault();
  40. //Do we need to enable the double tap
  41. if (obj.settings.doubleTapToGo) {
  42. //if we hit a second time on the link and the href is valid, navigate to that url
  43. if (obj.doubleTapToGo($(this)) && $(this).attr("href") !== "#" && $(this).attr("href") !== "") {
  44. e.stopPropagation();
  45. document.location = $(this).attr("href");
  46. return;
  47. }
  48. }
  49. $(this).parent("li").toggleClass("active").children("ul").collapse("toggle");
  50. if ($toggle) {
  51. $(this).parent("li").siblings().removeClass("active").children("ul.in").collapse("hide");
  52. }
  53. });
  54. },
  55. isIE: function() { //https://gist.github.com/padolsey/527683
  56. var undef,
  57. v = 3,
  58. div = document.createElement("div"),
  59. all = div.getElementsByTagName("i");
  60. while (
  61. div.innerHTML = "<!--[if gt IE " + (++v) + "]><i></i><![endif]-->",
  62. all[0]
  63. ) {
  64. return v > 4 ? v : undef;
  65. }
  66. },
  67. //Enable the link on the second click.
  68. doubleTapToGo: function(elem) {
  69. var $this = this.element;
  70. //if the class "doubleTapToGo" exists, remove it and return
  71. if (elem.hasClass("doubleTapToGo")) {
  72. elem.removeClass("doubleTapToGo");
  73. return true;
  74. }
  75. //does not exists, add a new class and return false
  76. if (elem.parent().children("ul").length) {
  77. //first remove all other class
  78. $this.find(".doubleTapToGo").removeClass("doubleTapToGo");
  79. //add the class on the current element
  80. elem.addClass("doubleTapToGo");
  81. return false;
  82. }
  83. },
  84. remove: function() {
  85. this.element.off("." + pluginName);
  86. this.element.removeData(pluginName);
  87. }
  88. };
  89. $.fn[pluginName] = function(options) {
  90. this.each(function () {
  91. var el = $(this);
  92. if (el.data(pluginName)) {
  93. el.data(pluginName).remove();
  94. }
  95. el.data(pluginName, new Plugin(this, options));
  96. });
  97. return this;
  98. };
  99. })(jQuery, window, document);