Last active
October 1, 2015 03:18
-
-
Save ashleydw/1909395 to your computer and use it in GitHub Desktop.
Thinkle Mootools Paginator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Paginating through elements with MooTools | |
* | |
* Use: | |
* new ThinklePaginator(el, options); | |
* | |
* @link www.needathinkle.com/tumblr/18254885740 | |
* @version 2 | |
* @author Ashley White, www.needathinkle.com | |
* @license @license http://creativecommons.org/licenses/by-sa/3.0/ | |
*/ | |
var ThinklePaginator = new Class({ | |
Implements: Options, | |
options: { | |
limit: 10, | |
controls: 'bottom', | |
href: window.location.pathname, | |
current: 1 | |
}, | |
container: null, | |
elements: [], | |
pages: [], | |
current: 0, | |
initialize: function(container, options) { | |
this.setOptions(options); | |
this.container = container; | |
this.elements = this.container.getChildren(); | |
//Pages | |
for(var i = 0; i < this.elements.length; i+=this.options.limit) { | |
var children = this.elements.slice(i, i+this.options.limit); | |
var inner = new Element('div', { | |
'class': 'paginateBoxPage', | |
'style': i == 0?'display:block':'display:none' | |
}).wraps(children[0]); | |
children.each(function(item, j) { | |
if(j !== 0) | |
inner.grab(item); | |
}); | |
this.pages.push(inner); | |
} | |
this.controls(this.options.controls); | |
this.showPage(this.options.current); | |
}, | |
controls: function(el) { | |
if(!el || !this.container) | |
return; | |
//Controls | |
var ul = new Element('ul', { | |
'class': 'paginateBoxUl' | |
}); | |
for(var i = 1; i <= this.pages.length; i++) { | |
ul.adopt(new Element('li', { | |
'class': i == 1?'active':'' | |
}).adopt(new Element('a', { | |
href: this.options.href+'#'+i, | |
text: i | |
}))); | |
} | |
ul.addEvent('click:relay(a)', function(e) { | |
this.showPage(e.target.get('text')); | |
}.bind(this)); | |
if(el == 'top' || el == 'bottom') { | |
ul.inject(this.container, el); | |
} else | |
el.adopt(ul); | |
}, | |
showPage: function(page) { | |
page = page-1; | |
if(page == this.current) | |
return; | |
var current = this.pages[this.current]; | |
var newpage = this.pages[page]; | |
if(newpage) { | |
current.fade('out').setStyle('display', 'none'); | |
newpage.fade('in').setStyle('display', 'block'); | |
this.container.getElements('ul.paginateBoxUl').each(function(item) { | |
var active = item.getElement('li.active'); | |
if(active) | |
active.removeClass('active'); | |
item.getElement(':nth-child('+(page+1)+')').addClass('active'); | |
}); | |
} | |
this.current = page; | |
}, | |
next: function() { | |
this.showPage(this.current-1); | |
}, | |
previous: function() { | |
this.showPage(this.current+1); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment