jquery.slicknav.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*!
  2. * SlickNav Responsive Mobile Menu v1.0.4
  3. * (c) 2015 Josh Cope
  4. * licensed under MIT
  5. */
  6. ;(function ($, document, window) {
  7. var
  8. // default settings object.
  9. defaults = {
  10. label: 'MENU',
  11. duplicate: true,
  12. duration: 200,
  13. easingOpen: 'swing',
  14. easingClose: 'swing',
  15. closedSymbol: '►',
  16. openedSymbol: '▼',
  17. prependTo: 'body',
  18. parentTag: 'a',
  19. closeOnClick: false,
  20. allowParentLinks: false,
  21. nestedParentLinks: true,
  22. showChildren: false,
  23. removeIds: false,
  24. removeClasses: false,
  25. brand: '',
  26. init: function () {},
  27. beforeOpen: function () {},
  28. beforeClose: function () {},
  29. afterOpen: function () {},
  30. afterClose: function () {}
  31. },
  32. mobileMenu = 'slicknav',
  33. prefix = 'slicknav';
  34. function Plugin(element, options) {
  35. this.element = element;
  36. // jQuery has an extend method which merges the contents of two or
  37. // more objects, storing the result in the first object. The first object
  38. // is generally empty as we don't want to alter the default options for
  39. // future instances of the plugin
  40. this.settings = $.extend({}, defaults, options);
  41. this._defaults = defaults;
  42. this._name = mobileMenu;
  43. this.init();
  44. }
  45. Plugin.prototype.init = function () {
  46. var $this = this,
  47. menu = $(this.element),
  48. settings = this.settings,
  49. iconClass,
  50. menuBar;
  51. // clone menu if needed
  52. if (settings.duplicate) {
  53. $this.mobileNav = menu.clone();
  54. //remove ids from clone to prevent css issues
  55. $this.mobileNav.removeAttr('id');
  56. $this.mobileNav.find('*').each(function (i, e) {
  57. $(e).removeAttr('id');
  58. });
  59. } else {
  60. $this.mobileNav = menu;
  61. // remove ids if set
  62. $this.mobileNav.removeAttr('id');
  63. $this.mobileNav.find('*').each(function (i, e) {
  64. $(e).removeAttr('id');
  65. });
  66. }
  67. // remove classes if set
  68. if (settings.removeClasses) {
  69. $this.mobileNav.removeAttr('class');
  70. $this.mobileNav.find('*').each(function (i, e) {
  71. $(e).removeAttr('class');
  72. });
  73. }
  74. // styling class for the button
  75. iconClass = prefix + '_icon';
  76. if (settings.label === '') {
  77. iconClass += ' ' + prefix + '_no-text';
  78. }
  79. if (settings.parentTag == 'a') {
  80. settings.parentTag = 'a href="#"';
  81. }
  82. // create menu bar
  83. $this.mobileNav.attr('class', prefix + '_nav');
  84. menuBar = $('<div class="' + prefix + '_menu"></div>');
  85. if (settings.brand !== '') {
  86. var brand = $('<div class="' + prefix + '_brand">'+settings.brand+'</div>');
  87. $(menuBar).append(brand);
  88. }
  89. $this.btn = $(
  90. ['<' + settings.parentTag + ' aria-haspopup="true" tabindex="0" class="' + prefix + '_btn ' + prefix + '_collapsed">',
  91. '<span class="' + prefix + '_menutxt">' + settings.label + '</span>',
  92. '<span class="' + iconClass + '">',
  93. '<span class="' + prefix + '_icon-bar"></span>',
  94. '<span class="' + prefix + '_icon-bar"></span>',
  95. '<span class="' + prefix + '_icon-bar"></span>',
  96. '</span>',
  97. '</' + settings.parentTag + '>'
  98. ].join('')
  99. );
  100. $(menuBar).append($this.btn);
  101. $(settings.prependTo).prepend(menuBar);
  102. menuBar.append($this.mobileNav);
  103. // iterate over structure adding additional structure
  104. var items = $this.mobileNav.find('li');
  105. $(items).each(function () {
  106. var item = $(this),
  107. data = {};
  108. data.children = item.children('ul').attr('role', 'menu');
  109. item.data('menu', data);
  110. // if a list item has a nested menu
  111. if (data.children.length > 0) {
  112. // select all text before the child menu
  113. // check for anchors
  114. var a = item.contents(),
  115. containsAnchor = false,
  116. nodes = [];
  117. $(a).each(function () {
  118. if (!$(this).is('ul')) {
  119. nodes.push(this);
  120. } else {
  121. return false;
  122. }
  123. if($(this).is("a")) {
  124. containsAnchor = true;
  125. }
  126. });
  127. var wrapElement = $(
  128. '<' + settings.parentTag + ' role="menuitem" aria-haspopup="true" tabindex="-1" class="' + prefix + '_item"/>'
  129. );
  130. // wrap item text with tag and add classes unless we are separating parent links
  131. if ((!settings.allowParentLinks || settings.nestedParentLinks) || !containsAnchor) {
  132. var $wrap = $(nodes).wrapAll(wrapElement).parent();
  133. $wrap.addClass(prefix+'_row');
  134. } else
  135. $(nodes).wrapAll('<span class="'+prefix+'_parent-link '+prefix+'_row"/>').parent();
  136. if (!settings.showChildren) {
  137. item.addClass(prefix+'_collapsed');
  138. } else {
  139. item.addClass(prefix+'_open');
  140. }
  141. item.addClass(prefix+'_parent');
  142. // create parent arrow. wrap with link if parent links and separating
  143. var arrowElement = $('<span class="'+prefix+'_arrow">'+(settings.showChildren?settings.openedSymbol:settings.closedSymbol)+'</span>');
  144. if (settings.allowParentLinks && !settings.nestedParentLinks && containsAnchor)
  145. arrowElement = arrowElement.wrap(wrapElement).parent();
  146. //append arrow
  147. $(nodes).last().after(arrowElement);
  148. } else if ( item.children().length === 0) {
  149. item.addClass(prefix+'_txtnode');
  150. }
  151. // accessibility for links
  152. item.children('a').attr('role', 'menuitem').click(function(event){
  153. //Ensure that it's not a parent
  154. if (settings.closeOnClick && !$(event.target).parent().closest('li').hasClass(prefix+'_parent')) {
  155. //Emulate menu close if set
  156. $($this.btn).click();
  157. }
  158. });
  159. //also close on click if parent links are set
  160. if (settings.closeOnClick && settings.allowParentLinks) {
  161. item.children('a').children('a').click(function (event) {
  162. //Emulate menu close
  163. $($this.btn).click();
  164. });
  165. item.find('.'+prefix+'_parent-link a:not(.'+prefix+'_item)').click(function(event){
  166. //Emulate menu close
  167. $($this.btn).click();
  168. });
  169. }
  170. });
  171. // structure is in place, now hide appropriate items
  172. $(items).each(function () {
  173. var data = $(this).data('menu');
  174. if (!settings.showChildren){
  175. $this._visibilityToggle(data.children, null, false, null, true);
  176. }
  177. });
  178. // finally toggle entire menu
  179. $this._visibilityToggle($this.mobileNav, null, false, 'init', true);
  180. // accessibility for menu button
  181. $this.mobileNav.attr('role','menu');
  182. // outline prevention when using mouse
  183. $(document).mousedown(function(){
  184. $this._outlines(false);
  185. });
  186. $(document).keyup(function(){
  187. $this._outlines(true);
  188. });
  189. // menu button click
  190. $($this.btn).click(function (e) {
  191. e.preventDefault();
  192. $this._menuToggle();
  193. });
  194. // click on menu parent
  195. $this.mobileNav.on('click', '.' + prefix + '_item', function (e) {
  196. e.preventDefault();
  197. $this._itemClick($(this));
  198. });
  199. // check for enter key on menu button and menu parents
  200. $($this.btn).keydown(function (e) {
  201. var ev = e || event;
  202. if(ev.keyCode == 13) {
  203. e.preventDefault();
  204. $this._menuToggle();
  205. }
  206. });
  207. $this.mobileNav.on('keydown', '.'+prefix+'_item', function(e) {
  208. var ev = e || event;
  209. if(ev.keyCode == 13) {
  210. e.preventDefault();
  211. $this._itemClick($(e.target));
  212. }
  213. });
  214. // allow links clickable within parent tags if set
  215. if (settings.allowParentLinks && settings.nestedParentLinks) {
  216. $('.'+prefix+'_item a').click(function(e){
  217. e.stopImmediatePropagation();
  218. });
  219. }
  220. };
  221. //toggle menu
  222. Plugin.prototype._menuToggle = function (el) {
  223. var $this = this;
  224. var btn = $this.btn;
  225. var mobileNav = $this.mobileNav;
  226. if (btn.hasClass(prefix+'_collapsed')) {
  227. btn.removeClass(prefix+'_collapsed');
  228. btn.addClass(prefix+'_open');
  229. } else {
  230. btn.removeClass(prefix+'_open');
  231. btn.addClass(prefix+'_collapsed');
  232. }
  233. btn.addClass(prefix+'_animating');
  234. $this._visibilityToggle(mobileNav, btn.parent(), true, btn);
  235. };
  236. // toggle clicked items
  237. Plugin.prototype._itemClick = function (el) {
  238. var $this = this;
  239. var settings = $this.settings;
  240. var data = el.data('menu');
  241. if (!data) {
  242. data = {};
  243. data.arrow = el.children('.'+prefix+'_arrow');
  244. data.ul = el.next('ul');
  245. data.parent = el.parent();
  246. //Separated parent link structure
  247. if (data.parent.hasClass(prefix+'_parent-link')) {
  248. data.parent = el.parent().parent();
  249. data.ul = el.parent().next('ul');
  250. }
  251. el.data('menu', data);
  252. }
  253. if (data.parent.hasClass(prefix+'_collapsed')) {
  254. data.arrow.html(settings.openedSymbol);
  255. data.parent.removeClass(prefix+'_collapsed');
  256. data.parent.addClass(prefix+'_open');
  257. data.parent.addClass(prefix+'_animating');
  258. $this._visibilityToggle(data.ul, data.parent, true, el);
  259. } else {
  260. data.arrow.html(settings.closedSymbol);
  261. data.parent.addClass(prefix+'_collapsed');
  262. data.parent.removeClass(prefix+'_open');
  263. data.parent.addClass(prefix+'_animating');
  264. $this._visibilityToggle(data.ul, data.parent, true, el);
  265. }
  266. };
  267. // toggle actual visibility and accessibility tags
  268. Plugin.prototype._visibilityToggle = function(el, parent, animate, trigger, init) {
  269. var $this = this;
  270. var settings = $this.settings;
  271. var items = $this._getActionItems(el);
  272. var duration = 0;
  273. if (animate) {
  274. duration = settings.duration;
  275. }
  276. if (el.hasClass(prefix+'_hidden')) {
  277. el.removeClass(prefix+'_hidden');
  278. //Fire beforeOpen callback
  279. if (!init) {
  280. settings.beforeOpen(trigger);
  281. }
  282. el.slideDown(duration, settings.easingOpen, function(){
  283. $(trigger).removeClass(prefix+'_animating');
  284. $(parent).removeClass(prefix+'_animating');
  285. //Fire afterOpen callback
  286. if (!init) {
  287. settings.afterOpen(trigger);
  288. }
  289. });
  290. el.attr('aria-hidden','false');
  291. items.attr('tabindex', '0');
  292. $this._setVisAttr(el, false);
  293. } else {
  294. el.addClass(prefix+'_hidden');
  295. //Fire init or beforeClose callback
  296. if (!init){
  297. settings.beforeClose(trigger);
  298. }
  299. el.slideUp(duration, this.settings.easingClose, function() {
  300. el.attr('aria-hidden','true');
  301. items.attr('tabindex', '-1');
  302. $this._setVisAttr(el, true);
  303. el.hide(); //jQuery 1.7 bug fix
  304. $(trigger).removeClass(prefix+'_animating');
  305. $(parent).removeClass(prefix+'_animating');
  306. //Fire init or afterClose callback
  307. if (!init){
  308. settings.afterClose(trigger);
  309. } else if (trigger == 'init'){
  310. settings.init();
  311. }
  312. });
  313. }
  314. };
  315. // set attributes of element and children based on visibility
  316. Plugin.prototype._setVisAttr = function(el, hidden) {
  317. var $this = this;
  318. // select all parents that aren't hidden
  319. var nonHidden = el.children('li').children('ul').not('.'+prefix+'_hidden');
  320. // iterate over all items setting appropriate tags
  321. if (!hidden) {
  322. nonHidden.each(function(){
  323. var ul = $(this);
  324. ul.attr('aria-hidden','false');
  325. var items = $this._getActionItems(ul);
  326. items.attr('tabindex', '0');
  327. $this._setVisAttr(ul, hidden);
  328. });
  329. } else {
  330. nonHidden.each(function(){
  331. var ul = $(this);
  332. ul.attr('aria-hidden','true');
  333. var items = $this._getActionItems(ul);
  334. items.attr('tabindex', '-1');
  335. $this._setVisAttr(ul, hidden);
  336. });
  337. }
  338. };
  339. // get all 1st level items that are clickable
  340. Plugin.prototype._getActionItems = function(el) {
  341. var data = el.data("menu");
  342. if (!data) {
  343. data = {};
  344. var items = el.children('li');
  345. var anchors = items.find('a');
  346. data.links = anchors.add(items.find('.'+prefix+'_item'));
  347. el.data('menu', data);
  348. }
  349. return data.links;
  350. };
  351. Plugin.prototype._outlines = function(state) {
  352. if (!state) {
  353. $('.'+prefix+'_item, .'+prefix+'_btn').css('outline','none');
  354. } else {
  355. $('.'+prefix+'_item, .'+prefix+'_btn').css('outline','');
  356. }
  357. };
  358. Plugin.prototype.toggle = function(){
  359. var $this = this;
  360. $this._menuToggle();
  361. };
  362. Plugin.prototype.open = function(){
  363. var $this = this;
  364. if ($this.btn.hasClass(prefix+'_collapsed')) {
  365. $this._menuToggle();
  366. }
  367. };
  368. Plugin.prototype.close = function(){
  369. var $this = this;
  370. if ($this.btn.hasClass(prefix+'_open')) {
  371. $this._menuToggle();
  372. }
  373. };
  374. $.fn[mobileMenu] = function ( options ) {
  375. var args = arguments;
  376. // Is the first parameter an object (options), or was omitted, instantiate a new instance
  377. if (options === undefined || typeof options === 'object') {
  378. return this.each(function () {
  379. // Only allow the plugin to be instantiated once due to methods
  380. if (!$.data(this, 'plugin_' + mobileMenu)) {
  381. // if it has no instance, create a new one, pass options to our plugin constructor,
  382. // and store the plugin instance in the elements jQuery data object.
  383. $.data(this, 'plugin_' + mobileMenu, new Plugin( this, options ));
  384. }
  385. });
  386. // If is a string and doesn't start with an underscore or 'init' function, treat this as a call to a public method.
  387. } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
  388. // Cache the method call to make it possible to return a value
  389. var returns;
  390. this.each(function () {
  391. var instance = $.data(this, 'plugin_' + mobileMenu);
  392. // Tests that there's already a plugin-instance and checks that the requested public method exists
  393. if (instance instanceof Plugin && typeof instance[options] === 'function') {
  394. // Call the method of our plugin instance, and pass it the supplied arguments.
  395. returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
  396. }
  397. });
  398. // If the earlier cached method gives a value back return the value, otherwise return this to preserve chainability.
  399. return returns !== undefined ? returns : this;
  400. }
  401. };
  402. }(jQuery, document, window));