Skip to content
Snippets Groups Projects
Commit 1b7f55f8 authored by Daniel Herzog's avatar Daniel Herzog
Browse files

Add basic files

parent 4c51ef6c
No related branches found
No related tags found
No related merge requests found
## 1.1.0 (2016-06-25)
General:
- Initial version released.
# Contributing
1. Fork this project
2. Create your feature branch `git checkout -b my-branch`
3. Commit your changes `git commit -am 'Add some feature'`
4. Push to the branch `git push origin my-new-feature`
5. Create a pull request with the following format:
```markdown
## Description
This pull request will be used to [fix|implement|...] the following [bugs|features|...].
## Tasks
- [ ] Uncompleted task nº1.
- [x] Completed task nº2.
- [x] Buy eggs.
- [ ] Fix that strange bug.
- [ ] Etc.
## References
- [Example reference](http://www.example.com)
- [Wabit season!](https://www.youtube.com/watch?v=17ocaZb-bGg)
```
The MIT License (MIT)
=====================
Copyright © `2016` `Daniel Herzog`
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
<!DOCTYPE html>
<html>
<head>
<title>jQuery paginate example</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="jquery-paginate.js"></script>
<style>
.page-navigation a {
margin: 0 2px;
display: inline-block;
padding: 3px 5px;
color: #ffffff;
background-color: #70b7ec;
border-radius: 5px;
text-decoration: none;
font-weight: bold;
}
.page-navigation a[data-selected] {
background-color: #3d9be0;
}
</style>
</head>
<body>
<table id='myTable'>
<thead>
<tr>
<td>Index</td>
<td>Value</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
// Add some examples to the page
for(var i = 0; i < 40; ++i)
$('#myTable > tbody').append('<tr><td>' + i + '</td><td>' + i.toString(16) + '</td></tr>');
// Paginate it
$('#myTable').paginate({
limit: 10,
onSelect: function(obj, page) {
console.log('Page ' + page + ' selected!' );
}
});
</script>
</body>
</html>
(function($) {
'use strict';
var Paginator = function() {
return {
// Attributes
obj: null,
options: null,
nav: null,
// Methods
build: function(obj, opts) {
this.obj = obj;
this.options = opts;
this._createNavigation();
this._setPage();
if(this.options.onCreate) this.options.onCreate(obj);
return this.obj;
},
_createNavigation: function() {
this._createNavigationWrapper();
this._createNavigationButtons();
this._appendNavigation();
this._addNavigationCallbacks();
},
_createNavigationWrapper: function() {
this.nav = $('<div>', {
class: this.options.navigationClass
});
},
_createNavigationButtons: function() {
// TODO: Add callbacks to buttons
// Add 'first' button
if(this.options.first)
this._createNavigationButton(this.options.firstText, {
'data-first': true
});
// Add 'previous' button
if(this.options.previous)
this._createNavigationButton(this.options.previousText, {
'data-direction': -1,
'data-previous': true
});
// Add page buttons
for(var i = 0; i < this._totalPages(); ++i)
this._createNavigationButton(this.options.pageToText(i), {
'data-page': i
});
// Add 'next' button
if(this.options.next)
this._createNavigationButton(this.options.nextText, {
'data-direction': 1,
'data-next': true
});
// Add 'last' button
if(this.options.last)
this._createNavigationButton(this.options.lastText, {
'data-last': true
});
},
_createNavigationButton: function(text, options) {
this.nav.append($('<a>', $.extend(options, { href: '#', text: text })));
},
_appendNavigation: function() {
// Add the content to the navigation block
if(this.options.navigationWrapper) this.options.navigationWrapper.append(this.nav);
// Add it after the table
else this.obj.after(this.nav);
},
_addNavigationCallbacks: function() {
var paginator = this;
paginator.nav.find('a').click(function(e) {
var direction = $(this).data('direction') * 1;
if($(this).data('first') !== undefined) {
paginator._setPage(0);
}
else if ($(this).data('page') !== undefined) {
paginator._setPage($(this).data('page') * 1);
}
else if ($(this).data('previous') !== undefined || $(this).data('next') !== undefined) {
var page = paginator._currentPage() + direction;
if(page >= 0 && page <= paginator._totalPages() - 1) {
paginator._setPage(page);
}
}
else if ($(this).data('last') !== undefined) {
paginator._setPage(paginator._totalPages() - 1);
}
if(paginator.options.onSelect) paginator.options.onSelect(paginator.obj, paginator._currentPage());
e.preventDefault();
return false;
});
},
_setPage: function(index) {
if(!index) index = this.options.initialPage;
// Hide all elements, and then show the current page.
this._rows().hide().slice(index * this.options.limit, (index + 1) * this.options.limit).show();
// Set the current button as active
this.nav.find('a').removeAttr('data-selected').siblings('a[data-page=' + index + ']')
.attr('data-selected', true);
},
_currentPage: function() {
return this.nav.find('a[data-selected=true]').data('page');
},
_totalRows: function() {
// Count the total rows of the selector
return this._rows().length;
},
_rows: function() {
return this.obj.find(this.options.childrenSelector);
},
_totalPages: function() {
return Math.floor(this._totalRows() / this.options.limit);
}
};
};
$.fn.paginate = function(options) {
switch(options) {
// Example of custom actions:
// case 'destroy': return pagination.destroy(this);
default: return Paginator().build(this, $.extend( {}, $.fn.paginate.defaults, options));
}
};
$.fn.paginate.defaults = {
limit: 20,
initialPage: 0,
previous: true,
previousText: '<',
next: true,
nextText: '>',
first: true,
firstText: '<<',
last: true,
lastText: '>>',
optional: true,
onCreate: null,
onSelect: null,
childrenSelector: 'tbody > tr',
navigationWrapper: null,
navigationClass: 'page-navigation',
pageToText: function(i) { return (i + 1).toString(); }
}
}(jQuery));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment