PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/javascripts/jquery.slimmenu.js

https://gitlab.com/zohaibsaleem/shipcliq
JavaScript | 175 lines | 146 code | 22 blank | 7 comment | 14 complexity | 51c3708d5dd39a585fe826af85579c1f MD5 | raw file
  1. /**
  2. * jquery.slimmenu.js
  3. * http://adnantopal.github.io/slimmenu/
  4. * Author: @adnantopal
  5. * Copyright 2013, Adnan Topal (atopal.com)
  6. * Licensed under the MIT license.
  7. */
  8. ;(function ( $, window, document, undefined )
  9. {
  10. var pluginName = "slimmenu",
  11. defaults =
  12. {
  13. resizeWidth: '767',
  14. collapserTitle: '',
  15. animSpeed: 'medium',
  16. easingEffect: null,
  17. indentChildren: false,
  18. childrenIndenter: '  '
  19. };
  20. var oldWindowWidth = 0;
  21. function Plugin( element, options )
  22. {
  23. this.element = element;
  24. this.$elem = $(this.element);
  25. this.options = $.extend( {}, defaults, options );
  26. this.oldwidth = 0;
  27. this.init();
  28. }
  29. Plugin.prototype = {
  30. init: function()
  31. {
  32. var $options = this.options,
  33. $menu = this.$elem,
  34. $collapser = '<div class="menu-collapser">'+$options.collapserTitle+'<div class="collapse-button"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></div></div>',
  35. $menu_collapser;
  36. $menu.before($collapser);
  37. $menu_collapser = $menu.prev('.menu-collapser');
  38. $menu.on('click', '.sub-collapser', function(e)
  39. {
  40. e.preventDefault();
  41. e.stopPropagation();
  42. var $parent_li = $(this).closest('li');
  43. if ($(this).hasClass('expanded'))
  44. {
  45. $(this).removeClass('expanded');
  46. $(this).find('i').html('&#9660;');
  47. $parent_li.find('>ul').slideUp($options.animSpeed, $options.easingEffect);
  48. }
  49. else
  50. {
  51. $(this).addClass('expanded');
  52. $(this).find('i').html('&#9650;');
  53. $parent_li.find('>ul').slideDown($options.animSpeed, $options.easingEffect);
  54. }
  55. });
  56. $menu_collapser.on('click', '.collapse-button', function(e)
  57. {
  58. e.preventDefault();
  59. $menu.slideToggle($options.animSpeed, $options.easingEffect);
  60. });
  61. this.resizeMenu({ data: { el: this.element, options: this.options } });
  62. $(window).on('resize', { el: this.element, options: this.options }, this.resizeMenu);
  63. $(window).trigger("resize");
  64. },
  65. resizeMenu: function(event)
  66. {
  67. var $window = $(window),
  68. $options = event.data.options,
  69. $menu = $(event.data.el),
  70. $menu_collapser = $('body').find('.menu-collapser');
  71. if (oldWindowWidth == $window.width()) {
  72. return;
  73. }
  74. oldWindowWidth = $window.width();
  75. var windowWidth = $window.width();
  76. if(window["innerWidth"] !== undefined){
  77. if(window["innerWidth"] > windowWidth){
  78. windowWidth = window["innerWidth"];
  79. }
  80. }
  81. if(windowWidth != this.oldwidth){
  82. this.oldwidth = windowWidth;
  83. $menu.find('li').each(function()
  84. {
  85. if ($(this).has('ul').length)
  86. {
  87. if ($(this).has('.sub-collapser').length)
  88. {
  89. $(this).children('.sub-collapser i').html('&#9660;');
  90. }
  91. else
  92. {
  93. $(this).append('<span class="sub-collapser"><i>&#9660;</i></span>');
  94. }
  95. }
  96. $(this).children('ul').hide();
  97. $(this).find('.sub-collapser').removeClass('expanded').children('i').html('&#9660;');
  98. });
  99. if ($options.resizeWidth >= windowWidth)
  100. {
  101. if ($options.indentChildren)
  102. {
  103. $menu.find('ul').each(function()
  104. {
  105. var $depth = $(this).parents('ul').length;
  106. if (!$(this).children('li').children('a').has('i').length)
  107. {
  108. $(this).children('li').children('a').prepend(Plugin.prototype.indent($depth, $options));
  109. }
  110. });
  111. }
  112. $menu.find('li').has('ul').off('mouseenter mouseleave');
  113. $menu.addClass('collapsed').hide();
  114. $menu_collapser.show();
  115. }
  116. else
  117. {
  118. $menu.find('li').has('ul').on('mouseenter', function()
  119. {
  120. $(this).find('>ul').stop().slideDown($options.animSpeed, $options.easingEffect);
  121. })
  122. .on('mouseleave', function()
  123. {
  124. $(this).find('>ul').stop().slideUp($options.animSpeed, $options.easingEffect);
  125. });
  126. $menu.find('li > a > i').remove();
  127. $menu.removeClass('collapsed').show();
  128. $menu_collapser.hide();
  129. }
  130. }
  131. },
  132. indent: function(num, options)
  133. {
  134. var $indent = '';
  135. for (var i=0; i < num; i++)
  136. {
  137. $indent += options.childrenIndenter;
  138. }
  139. return '<i>'+$indent+'</i>';
  140. }
  141. };
  142. $.fn[pluginName] = function ( options )
  143. {
  144. return this.each(function ()
  145. {
  146. if (!$.data(this, "plugin_" + pluginName))
  147. {
  148. $.data(this, "plugin_" + pluginName,
  149. new Plugin( this, options ));
  150. }
  151. });
  152. };
  153. })( jQuery, window, document );