sidebar-nav.js 4.0 KB

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