metisMenu.js 3.7 KB

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