Skip to content

Commit 18b33a4

Browse files
committed
Initial commit
0 parents  commit 18b33a4

File tree

7 files changed

+308
-0
lines changed

7 files changed

+308
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
yarn.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Gilbert Pellegrom
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Laravel Vue Pagination
2+
A Vue.js pagination component for Laravel paginators that works with Bootstrap.
3+
4+
## Requirements
5+
6+
* [Vue.js](https://vuejs.org/) 2.x
7+
* [Laravel](http://laravel.com/docs/) 5.x
8+
* [Bootstrap](http://getbootstrap.com/) 3.x
9+
10+
## Usage
11+
12+
Register the component:
13+
14+
```
15+
Vue.component('pagination', require('laravel-vue-pagination'));
16+
```
17+
18+
Use the component:
19+
20+
```
21+
<pagination ref="paginator" :data="laravelData"></pagination>
22+
```
23+
24+
```
25+
Vue.component('example-component', {
26+
27+
data() {
28+
return {
29+
// Our data object that holds the Laravel paginator data
30+
laravelData: {},
31+
}
32+
},
33+
34+
created() {
35+
// Set up the pagination event listener
36+
this.$refs.paginator.$on('pagination-change-page', this.getResults);
37+
38+
// Fetch initial results
39+
this.getResults();
40+
},
41+
42+
methods: {
43+
// Our method to GET results from a Laravel endpoint
44+
getResults(page) {
45+
if (typeof page === 'undefined') {
46+
page = 1;
47+
}
48+
49+
// Using vue-resource as an example
50+
this.$http.get('example/results')
51+
.then(response => {
52+
return response.json();
53+
}).then(data => {
54+
this.laravelData = data;
55+
});
56+
}
57+
}
58+
59+
});
60+
```
61+
62+
## API
63+
64+
### Props
65+
66+
| Name | Type | Description |
67+
| --- | --- | --- |
68+
| `data` | Object | An object containing the structure of a [Laravel paginator](https://laravel.com/docs/5.3/pagination) response. See below for default value. |
69+
70+
```
71+
{
72+
current_page: 1,
73+
data: [],
74+
from: 1,
75+
last_page: 1,
76+
next_page_url: null,
77+
per_page: 10,
78+
prev_page_url: null,
79+
to: 1,
80+
total: 0,
81+
}
82+
```
83+
84+
### Events
85+
86+
| Name | Description |
87+
| --- | --- |
88+
| `pagination-change-page` | Triggered when a user changes page. Passes the new `page` index as a parameter. |
89+
90+
## Credits
91+
92+
Laravel Vue Pagination was created by [Gilbert Pellegrom](http://gilbert.pellegrom.me) from
93+
[Dev7studios](http://dev7studios.co). Released under the MIT license.

karma.conf.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Karma configuration
2+
module.exports = function(config) {
3+
config.set({
4+
5+
// base path that will be used to resolve all patterns (eg. files, exclude)
6+
basePath: '',
7+
8+
// frameworks to use
9+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
10+
frameworks: ['jasmine', 'browserify'],
11+
12+
// list of files / patterns to load in the browser
13+
files: [
14+
'test/**/*.js'
15+
],
16+
17+
// list of files to exclude
18+
exclude: [
19+
],
20+
21+
// preprocess matching files before serving them to the browser
22+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
23+
preprocessors: {
24+
'test/**/*.spec.js': ['browserify']
25+
},
26+
27+
// test results reporter to use
28+
// possible values: 'dots', 'progress'
29+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
30+
reporters: ['spec'],
31+
32+
// web server port
33+
port: 9876,
34+
35+
// enable / disable colors in the output (reporters and logs)
36+
colors: true,
37+
38+
// level of logging
39+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
40+
logLevel: config.LOG_INFO,
41+
42+
// enable / disable watching file and executing tests whenever any file changes
43+
autoWatch: true,
44+
45+
// start these browsers
46+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
47+
browsers: ['PhantomJS'],
48+
49+
// Continuous Integration mode
50+
// if true, Karma captures browsers, runs the tests and exits
51+
singleRun: true,
52+
53+
// Browserify config
54+
browserify: {
55+
transform: [['babelify', { presets: ['es2015'] }], ['aliasify', { aliases: { 'vue': 'vue/dist/vue.js' }}]],
56+
debug: true
57+
}
58+
})
59+
}

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "laravel-vue-pagination",
3+
"version": "1.0.0",
4+
"description": "Vue.js pagination component for Laravel paginators and Bootstrap styles",
5+
"author": "Gilbert Pellegrom <[email protected]>",
6+
"homepage": "https://github.com/gilbitron/laravel-vue-pagination",
7+
"license": "MIT",
8+
"keywords": [
9+
"vue",
10+
"component",
11+
"bootstrap",
12+
"laravel",
13+
"pagination"
14+
],
15+
"main": "src/laravel-vue-pagination.js",
16+
"scripts": {
17+
"test": "karma start karma.conf.js",
18+
"test-watch": "karma start karma.conf.js --no-single-run"
19+
},
20+
"devDependencies": {
21+
"aliasify": "^2.1.0",
22+
"babel-preset-es2015": "^6.18.0",
23+
"babelify": "^7.3.0",
24+
"browserify": "^13.3.0",
25+
"jasmine-core": "^2.5.2",
26+
"karma": "^1.3.0",
27+
"karma-browserify": "^5.1.0",
28+
"karma-jasmine": "^1.1.0",
29+
"karma-phantomjs-launcher": "^1.0.2",
30+
"karma-spec-reporter": "^0.0.26",
31+
"phantomjs-prebuilt": "^2.1.14",
32+
"vue": "^2.1.8",
33+
"watchify": "^3.8.0"
34+
}
35+
}

src/laravel-vue-pagination.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = {
2+
props: {
3+
data: {
4+
type: Object,
5+
default() {
6+
return {
7+
current_page: 1,
8+
data: [],
9+
from: 1,
10+
last_page: 1,
11+
next_page_url: null,
12+
per_page: 10,
13+
prev_page_url: null,
14+
to: 1,
15+
total: 0,
16+
}
17+
}
18+
}
19+
},
20+
21+
template: '<ul class="pagination" v-if="data.total > data.per_page">' +
22+
' <li v-if="data.prev_page_url">' +
23+
' <a href="#" aria-label="Previous" @click.prevent="selectPage(--data.current_page)"><span aria-hidden="true">&laquo;</span></a>' +
24+
' </li>' +
25+
' <li v-for="n in data.last_page" :class="{ \'active\': n == data.current_page }"><a href="#" @click.prevent="selectPage(n)">{{ n }}</a></li>' +
26+
' <li v-if="data.next_page_url">' +
27+
' <a href="#" aria-label="Next" @click.prevent="selectPage(++data.current_page)"><span aria-hidden="true">&raquo;</span></a>' +
28+
' </li>' +
29+
'</ul>',
30+
31+
methods: {
32+
selectPage(page) {
33+
this.$emit('pagination-change-page', page);
34+
}
35+
}
36+
};

test/laravel-vue-pagination.spec.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const Vue = require('vue');
2+
const LaravelVuePagination = require('../src/laravel-vue-pagination');
3+
4+
function getComponent(Component, propsData) {
5+
const Ctor = Vue.extend(Component);
6+
return new Ctor({ propsData }).$mount();
7+
}
8+
9+
var exampleData = {
10+
current_page: 1,
11+
data: [
12+
{ id: 1 },
13+
{ id: 2 },
14+
{ id: 3 },
15+
{ id: 4 },
16+
{ id: 5 },
17+
],
18+
from: 1,
19+
last_page: 3,
20+
next_page_url: 'http://example.com/page/2',
21+
per_page: 2,
22+
prev_page_url: null,
23+
to: 3,
24+
total: 5,
25+
};
26+
27+
describe('LaravelVuePagination', function() {
28+
it('has correct DOM structure', function() {
29+
const vm = getComponent(LaravelVuePagination, {
30+
data: exampleData
31+
});
32+
33+
expect(vm.$el.nodeName).toBe('UL');
34+
expect(vm.$el.getElementsByTagName('li').length).toBe(4);
35+
expect(vm.$el.getElementsByTagName('li')[0].classList).toContain('active');
36+
});
37+
38+
it('has correct DOM structure when on page 2', function() {
39+
exampleData.current_page = 2;
40+
exampleData.next_page_url = 'http://example.com/page/1';
41+
exampleData.prev_page_url = 'http://example.com/page/3';
42+
43+
const vm = getComponent(LaravelVuePagination, {
44+
data: exampleData
45+
});
46+
47+
expect(vm.$el.getElementsByTagName('li').length).toBe(5);
48+
expect(vm.$el.getElementsByTagName('li')[2].classList).toContain('active');
49+
});
50+
51+
it('emits correct event', function() {
52+
const vm = getComponent(LaravelVuePagination, {
53+
data: exampleData
54+
});
55+
56+
vm.$on('pagination-change-page', function (page) {
57+
expect(page).toBe(2);
58+
});
59+
60+
vm.selectPage(2);
61+
});
62+
});

0 commit comments

Comments
 (0)