Skip to content

Instantly share code, notes, and snippets.

@adardesign
Created June 1, 2012 15:00
Show Gist options
  • Save adardesign/2852745 to your computer and use it in GitHub Desktop.
Save adardesign/2852745 to your computer and use it in GitHub Desktop.
//toggleListMoreView
adrma.toggleListMoreView = {
defaults: {
limit: 4,
toggledEleClassName: "toggledHiddenList",
togglerEle: "<span>",
togglerClassName: "toggleMore",
text: ["more...", "less..."],
wrapThem:false,
wrapEle:"div",
addTriggerInside:false,
documentClickClose:false
},
init: function (config) {
var self = this;
for(var i = 0; i < config.length; i++) {
var thisConfig = $.extend({}, this.defaults, config[i]);
this.buildToggler(thisConfig);
}
},
buildToggler:function(thisConfig){
var self = this;
$(thisConfig.selector).each(function (i, e) {
var listParent = $(this),
list = listParent.children();
if(list.length <= thisConfig.limit) {
return
}
var filteredList = list.filter(function (i) {
return i > thisConfig.limit
})
if(thisConfig.wrapThem){
filteredList.wrapAll("<"+thisConfig.wrapEle+" class="+thisConfig.toggledEleClassName+"/>");
}else{
filteredList.addClass(thisConfig.toggledEleClassName);
}
var moreLink = $(thisConfig.togglerEle, {
"class": thisConfig.togglerClassName,
text: thisConfig.text[0]
}).on("click", function (evt, triggered) {
self.triggerToggle(listParent,thisConfig, triggered);
evt.stopPropagation();
}).appendTo(listParent);
thisConfig.addTriggerInside && (function(){
var clonedMore = moreLink.clone(true);
listParent.find("." + thisConfig.toggledEleClassName).append(clonedMore);
})();
})
},
triggerToggle:function(listParent,thisConfig, triggered){
var self = this;
if(triggered){
}
listParent.find("." + thisConfig.toggledEleClassName).slideToggle(100);
listParent.find("." + thisConfig.togglerClassName).html(function (i, h) {
if(h === thisConfig.text[0]){
self.attachDocClick(thisConfig);
return thisConfig.text[1]
} else {
return thisConfig.text[0]
}
})
//if(!thisConfig.documentClickClose){ return }
},
attachDocClick:function(thisConfig){
var self = this;
if(self.attachedDocClick){
return false;
}
thisConfig.documentClickClose && (function(){
setTimeout(function(){
$(document).on("click.toggleListMoreView", function(evnt){
evnt.stopPropagation()
self.documentClose(evnt, self);
//return false;
});
},10)
self.attachedDocClick = true;
})();
},
documentClose:function(evnt, self){
var eTarget = $(evnt.target);
$("."+self.defaults.toggledEleClassName+":visible").find("."+self.defaults.togglerClassName).trigger("click", ["triggered"]);
//evnt.stopPropagation();
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment