Browse files

Formatting

  • Loading branch information...
1 parent 46887eb commit 2b2fede553b4d3c0773321faa3e18ca723e49e66 @tefra committed Jul 9, 2013
Showing with 38 additions and 36 deletions.
  1. +32 −30 src/jquery.navgoco.js
  2. +2 −2 src/jquery.navgoco.min.js
  3. +4 −4 test/navgoco.js
View
62 src/jquery.navgoco.js
@@ -1,5 +1,5 @@
/*
- * jQuery Navgoco Menus Plugin v0.1.2 (2013-07-09)
+ * jQuery Navgoco Menus Plugin v0.1.3 (2013-07-09)
* https://github.com/tefra/navgoco
*
* Copyright (c) 2013 Chris T (@tefra)
@@ -61,27 +61,30 @@
links.append(self.options.caret);
}
links.on('click', function(e) {
+ e.stopPropagation();
e.preventDefault();
- var sub = $(this).next();
- var isOpen = sub.is(":visible");
+ var sub = $(this).next(),
+ isOpen = sub.is(":visible");
+
self._toggle(sub, !isOpen);
self._save();
});
},
/**
- * Accepts a JQuery Element and a boolean flag. If flag is false it removes from the
- * parent li the `open` css class and slides up the sub-menu. If flag is open it adds to
- * the parent li the `open` css class and slides down the menu. If accordion mode is on all
+ * Accepts a JQuery Element and a boolean flag. If flag is false it removes the `open` css
+ * class from the parent li and slides up the sub-menu. If flag is open it adds the `open`
+ * css class to the parent li and slides down the menu. If accordion mode is on all
* sub-menus except the direct parent tree will close. Internally an object with the menus
* states is maintained for later save duty.
*
* @param {Element} sub
* @param {Boolean} open
*/
_toggle: function(sub, open) {
- var self = this;
- var idx = sub.attr('data-index');
- var parent = sub.parent();
+ var self = this,
+ idx = sub.attr('data-index'),
+ parent = sub.parent();
+
if (open) {
parent.addClass(self.options.openClass);
sub.slideDown(self.options.slide);
@@ -91,9 +94,10 @@
var allowed = self.state = self._parents(sub);
allowed[idx] = self.state[idx] = 1;
- self.$el.find("ul:visible").each(function() {
- var sub = $(this);
- var idx = sub.attr('data-index');
+ self.$el.find('ul').filter(':visible').each(function() {
+ var sub = $(this),
+ idx = sub.attr('data-index');
+
if (!allowed.hasOwnProperty(idx)) {
self._toggle(sub, false);
}
@@ -104,8 +108,6 @@
sub.slideUp(self.options.slide);
self.state[idx] = 0;
}
-
-
},
/**
* Returns all parents of a sub-menu. When obj is true It returns an object with indexes for
@@ -117,13 +119,14 @@
* @returns {Object}
*/
_parents: function(sub, obj) {
- var result = {};
- var parent = sub.parent();
- var parents = parent.parents('ul');
+ var result = {},
+ parent = sub.parent(),
+ parents = parent.parents('ul');
parents.each(function() {
- var par = $(this);
- var idx = $(this).attr('data-index');
+ var par = $(this),
+ idx = par.attr('data-index');
+
if (!idx) {
return false;
}
@@ -170,19 +173,19 @@
* @param {Boolean} open
*/
toggle: function(open) {
- var self = this;
- var length = arguments.length;
+ var self = this,
+ length = arguments.length;
if (length <= 1) {
self.$el.find('ul').each(function() {
var sub = $(this);
self._toggle(sub, open);
});
} else {
- var idx;
- var list = {};
- var args = Array.prototype.slice.call(arguments, 1);
- length -= 1;
+ var idx,
+ list = {},
+ args = Array.prototype.slice.call(arguments, 1);
+ length--;
for (var i = 0; i < length; i++) {
idx = args[i];
@@ -204,7 +207,6 @@
self._toggle(list[idx], open);
}
}
-
self._save();
},
/**
@@ -225,17 +227,17 @@
*/
$.fn.navgoco = function(options) {
if (typeof options === 'string' && options.charAt(0) !== '_' && options !== 'init') {
- var callback = true;
- var args = Array.prototype.slice.call(arguments, 1);
+ var callback = true,
+ args = Array.prototype.slice.call(arguments, 1);
} else {
options = $.extend({}, $.fn.navgoco.defaults, options || {});
if (!$.cookie) {
options.save = false;
}
}
return this.each(function(idx) {
- var $this = $(this);
- var obj = $this.data('navgoco');
+ var $this = $(this),
+ obj = $this.data('navgoco');
if (!obj) {
obj = new Plugin(this, callback ? $.fn.navgoco.defaults : options, idx);
View
4 src/jquery.navgoco.min.js
@@ -1,8 +1,8 @@
/*
- * jQuery Navgoco Menus Plugin v0.1.2 (2013-07-09)
+ * jQuery Navgoco Menus Plugin v0.1.3 (2013-07-09)
* https://github.com/tefra/navgoco
*
* Copyright (c) 2013 Chris T (@tefra)
* BSD - https://github.com/tefra/navgoco/blob/master/LICENSE-BSD
*/
-!function(a){"use strict";var b=function(b,c,d){return this.el=b,this.$el=a(b),this.options=c,this.uuid=this.$el.attr("id")?this.$el.attr("id"):d,this.state={},this.init(),this};b.prototype={init:function(){var b=this;b._load(),b.$el.find("ul").each(function(c){var d=a(this);d.attr("data-index",c),b.options.save&&b.state.hasOwnProperty(c)?(d.parent().addClass(b.options.openClass),d.show()):d.parent().hasClass(b.options.openClass)?(d.show(),b.state[c]=1):d.hide()});var c=b.$el.find("li:has(ul) > a");b.options.caret&&c.append(b.options.caret),c.on("click",function(c){c.preventDefault();var d=a(this).next(),e=d.is(":visible");b._toggle(d,!e),b._save()})},_toggle:function(b,c){var d=this,e=b.attr("data-index"),f=b.parent();if(c){if(f.addClass(d.options.openClass),b.slideDown(d.options.slide),d.state[e]=1,d.options.accordion){var g=d.state=d._parents(b);g[e]=d.state[e]=1,d.$el.find("ul:visible").each(function(){var b=a(this),c=b.attr("data-index");g.hasOwnProperty(c)||d._toggle(b,!1)})}}else f.removeClass(d.options.openClass),b.slideUp(d.options.slide),d.state[e]=0},_parents:function(b,c){var d={},e=b.parent(),f=e.parents("ul");return f.each(function(){var b=a(this),e=a(this).attr("data-index");return e?(d[e]=c?b:1,void 0):!1}),d},_save:function(){if(this.options.save){var b={};for(var d in this.state)1===this.state[d]&&(b[d]=1);c[this.uuid]=this.state=b,a.cookie(this.options.cookie.name,JSON.stringify(c),this.options.cookie)}},_load:function(){if(this.options.save){if(null===c){var b=a.cookie(this.options.cookie.name);c=b?JSON.parse(b):{}}this.state=c.hasOwnProperty(this.uuid)?c[this.uuid]:{}}},toggle:function(b){var c=this,d=arguments.length;if(1>=d)c.$el.find("ul").each(function(){var d=a(this);c._toggle(d,b)});else{var e,f={},g=Array.prototype.slice.call(arguments,1);d-=1;for(var h=0;d>h;h++){e=g[h];var i=c.$el.find('ul[data-index="'+e+'"]').first();if(i&&(f[e]=i,b)){var j=c._parents(i,!0);for(var k in j)f.hasOwnProperty(k)||(f[k]=j[k])}}for(e in f)c._toggle(f[e],b)}c._save()},destroy:function(){a.removeData(this.$el),this.$el.find("li:has(ul) > a").unbind("click")}},a.fn.navgoco=function(c){if("string"==typeof c&&"_"!==c.charAt(0)&&"init"!==c)var d=!0,e=Array.prototype.slice.call(arguments,1);else c=a.extend({},a.fn.navgoco.defaults,c||{}),a.cookie||(c.save=!1);return this.each(function(f){var g=a(this),h=g.data("navgoco");h||(h=new b(this,d?a.fn.navgoco.defaults:c,f),g.data("navgoco",h)),d&&h[c].apply(h,e)})};var c=null;a.fn.navgoco.defaults={caret:'<span class="caret"></span>',accordion:!1,openClass:"open",save:!0,cookie:{name:"navgoco",expires:!1,path:"/"},slide:{duration:400,easing:"swing"}}}(jQuery);
+!function(a){"use strict";var b=function(b,c,d){return this.el=b,this.$el=a(b),this.options=c,this.uuid=this.$el.attr("id")?this.$el.attr("id"):d,this.state={},this.init(),this};b.prototype={init:function(){var b=this;b._load(),b.$el.find("ul").each(function(c){var d=a(this);d.attr("data-index",c),b.options.save&&b.state.hasOwnProperty(c)?(d.parent().addClass(b.options.openClass),d.show()):d.parent().hasClass(b.options.openClass)?(d.show(),b.state[c]=1):d.hide()});var c=b.$el.find("li:has(ul) > a");b.options.caret&&c.append(b.options.caret),c.on("click",function(c){c.stopPropagation(),c.preventDefault();var d=a(this).next(),e=d.is(":visible");b._toggle(d,!e),b._save()})},_toggle:function(b,c){var d=this,e=b.attr("data-index"),f=b.parent();if(c){if(f.addClass(d.options.openClass),b.slideDown(d.options.slide),d.state[e]=1,d.options.accordion){var g=d.state=d._parents(b);g[e]=d.state[e]=1,d.$el.find("ul").filter(":visible").each(function(){var b=a(this),c=b.attr("data-index");g.hasOwnProperty(c)||d._toggle(b,!1)})}}else f.removeClass(d.options.openClass),b.slideUp(d.options.slide),d.state[e]=0},_parents:function(b,c){var d={},e=b.parent(),f=e.parents("ul");return f.each(function(){var b=a(this),e=b.attr("data-index");return e?(d[e]=c?b:1,void 0):!1}),d},_save:function(){if(this.options.save){var b={};for(var d in this.state)1===this.state[d]&&(b[d]=1);c[this.uuid]=this.state=b,a.cookie(this.options.cookie.name,JSON.stringify(c),this.options.cookie)}},_load:function(){if(this.options.save){if(null===c){var b=a.cookie(this.options.cookie.name);c=b?JSON.parse(b):{}}this.state=c.hasOwnProperty(this.uuid)?c[this.uuid]:{}}},toggle:function(b){var c=this,d=arguments.length;if(1>=d)c.$el.find("ul").each(function(){var d=a(this);c._toggle(d,b)});else{var e,f={},g=Array.prototype.slice.call(arguments,1);d--;for(var h=0;d>h;h++){e=g[h];var i=c.$el.find('ul[data-index="'+e+'"]').first();if(i&&(f[e]=i,b)){var j=c._parents(i,!0);for(var k in j)f.hasOwnProperty(k)||(f[k]=j[k])}}for(e in f)c._toggle(f[e],b)}c._save()},destroy:function(){a.removeData(this.$el),this.$el.find("li:has(ul) > a").unbind("click")}},a.fn.navgoco=function(c){if("string"==typeof c&&"_"!==c.charAt(0)&&"init"!==c)var d=!0,e=Array.prototype.slice.call(arguments,1);else c=a.extend({},a.fn.navgoco.defaults,c||{}),a.cookie||(c.save=!1);return this.each(function(f){var g=a(this),h=g.data("navgoco");h||(h=new b(this,d?a.fn.navgoco.defaults:c,f),g.data("navgoco",h)),d&&h[c].apply(h,e)})};var c=null;a.fn.navgoco.defaults={caret:'<span class="caret"></span>',accordion:!1,openClass:"open",save:!0,cookie:{name:"navgoco",expires:!1,path:"/"},slide:{duration:400,easing:"swing"}}}(jQuery);
View
8 test/navgoco.js
@@ -1,6 +1,8 @@
(function($) {
- var menu, first, li;
- var html = arrayToUl(links);
+ var menu,
+ first,
+ li,
+ html = arrayToUl(links);
function getVisible() {
var result = [];
@@ -11,7 +13,6 @@
}
module('#navgoco', {
- // This will run before each test in this module.
setup: function() {
menu = $('<ul>' + html + '</ul>');
menu.appendTo($('#qunit-fixture'));
@@ -24,7 +25,6 @@
test('chainability', function() {
strictEqual(menu.navgoco(), menu, 'should be chainable');
-
});
test('caret', function() {

0 comments on commit 2b2fede

Please sign in to comment.