Skip to content

Commit 07f4a49

Browse files
committed
add a emitter class
1 parent da6bcf9 commit 07f4a49

19 files changed

+3996
-4007
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": [["es2015"]]
2+
"presets": ["es2015"]
33
}

.eslintrc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
"env": {
44
"browser": true,
5-
"node": true
5+
"node": true,
6+
"mocha": true
67
},
78

89
"extends": "airbnb-base/legacy",
910

1011
"parser": "babel-eslint",
1112

1213
"rules": {
13-
"strict": 0,
14-
15-
"no-console": 1
14+
"func-names": "off"
1615
}
1716

1817
}
File renamed without changes.

src/index.html renamed to examples/index.html

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
<title>Listview Example</title>
55

66
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700">
7-
<link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.min.css">
8-
<link rel="stylesheet" href="/node_modules/slickgrid/slick.grid.css">
9-
<link rel="stylesheet" href="/node_modules/slickgrid/css/smoothness/jquery-ui-1.11.3.custom.css">
107
<link rel="stylesheet" href="/css/grid.css">
118
<style>
129
.container {
@@ -111,16 +108,37 @@
111108
</div>
112109

113110
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.js"></script>
114-
<script src="/node_modules/slickgrid/lib/jquery-ui-1.11.3.js"></script>
115-
<script src="/node_modules/slickgrid/lib/jquery.event.drag-2.3.0.js"></script>
116-
<script src="/node_modules/slickgrid/slick.core.js"></script>
117-
<script src="/node_modules/slickgrid/slick.formatters.js"></script>
118-
<script src="/node_modules/slickgrid/slick.editors.js"></script>
119-
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
120111
<script src="/js/index.js"></script>
121112
<script type="text/javascript">
122113
$(function() {
123-
// Slick.createAjaxGrid();
114+
115+
var e = new Flow.Emitter();
116+
e.on('foo', () => console.log('foo fired'));
117+
e.trigger('foo');
118+
119+
var dataProvider = new Flow.AjaxDataProvider({
120+
url: 'http://192.168.60.167:3002/api.php',
121+
pagination: {
122+
pageSize: 25,
123+
pageParam: 'page',
124+
}
125+
});
126+
127+
var grid = new Flow.Grid({
128+
el: $('.grid'),
129+
dataProvider: dataProvider,
130+
editable: false,
131+
sortable: false,
132+
filterable: false,
133+
columns: [
134+
{ id: 'first_name', name: 'First name', field: 'first_name', width: 200 },
135+
{ id: 'last_name', name: 'Last name', field: 'last_name', width: 150 },
136+
{ id: 'title', name: 'Title', field: 'title', width: 250 },
137+
{ id: 'city', name: 'City', field: 'city', width: 150 },
138+
{ id: 'date', name: 'Date', field: 'date', width: 150 },
139+
{ id: 'amount', name: 'Amount', field: 'amount', width: 150 }
140+
]
141+
});
124142
});
125143
</script>
126144
</body>

gulpfile.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ const plumber = require('gulp-plumber');
66
const rollup = require('gulp-better-rollup');
77
const commonjs = require('rollup-plugin-commonjs');
88
const resolve = require('rollup-plugin-node-resolve');
9+
const babel = require('rollup-plugin-babel');
10+
const jasmine = require('gulp-jasmine');
911

1012
gulp.task('js', () => {
11-
gulp.src('./src/js/**/*.js')
13+
gulp.src('./src/js/index.js')
1214
.pipe(plumber())
1315
.pipe(rollup({
1416
plugins: [
@@ -19,15 +21,29 @@ gulp.task('js', () => {
1921
commonjs({
2022
include: './src/js',
2123
extensions: ['.js']
24+
}),
25+
babel({
26+
presets: [["es2015", { modules: false }]],
27+
exclude: 'node_modules/**',
28+
plugins: ['external-helpers'],
29+
externalHelpers: true
2230
})
2331
],
2432
format: 'umd',
25-
moduleName: 'Slick'
33+
moduleName: 'Flow'
2634
}))
2735
.pipe(gulp.dest('./dist/js'))
2836
.pipe(browserSync.stream());
2937
});
3038

39+
gulp.task('test', () => {
40+
gulp.src('spec/**')
41+
.pipe(plumber())
42+
.pipe(jasmine({
43+
config: 'spec/support/jasmine.json'
44+
}));
45+
});
46+
3147
gulp.task('eslint', function() {
3248
return gulp.src('./src/js/*.js')
3349
.pipe(plumber())
@@ -52,7 +68,7 @@ gulp.task('html', function() {
5268

5369
gulp.task('serve', ['html', 'eslint', 'js', 'sass'], function() {
5470
browserSync.init({
55-
server: ['./dist', './']
71+
server: ['./dist', './examples', './']
5672
});
5773

5874
gulp.watch('./src/js/*.js', ['eslint', 'js']);

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@
1818
"babel-core": "^6.24.0",
1919
"babel-eslint": "^7.2.1",
2020
"babel-preset-es2015": "^6.24.0",
21+
"babel-register": "^6.24.1",
2122
"browser-sync": "^2.18.8",
2223
"eslint-config-airbnb-base": "^11.1.3",
2324
"eslint-config-import": "^0.13.0",
25+
"eslint-plugin-import": "^2.2.0",
2426
"gulp": "^3.9.1",
27+
"gulp-babel": "^6.1.2",
2528
"gulp-better-rollup": "^1.1.1",
2629
"gulp-eslint": "^3.0.1",
30+
"gulp-jasmine": "^2.4.2",
2731
"gulp-plumber": "^1.1.0",
2832
"gulp-sass": "^3.1.0",
2933
"jquery": "^3.2.1",
3034
"rollup": "^0.41.6",
35+
"rollup-plugin-babel": "^2.7.1",
3136
"rollup-plugin-commonjs": "^8.0.2",
3237
"rollup-plugin-node-resolve": "^3.0.0"
3338
}

spec/emitter-spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Emitter from '../src/js/emitter';
2+
3+
describe('Emitter', () => {
4+
it(".on('foo')", () => {
5+
expect(true).toEqual(true);
6+
});
7+
});

spec/support/jasmine.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"spec_dir": "spec",
3+
"spec_files": [
4+
"**/*[sS]pec.js"
5+
],
6+
"helpers": [
7+
"../node_modules/babel-register/lib/node.js",
8+
"helpers/**/*.js"
9+
],
10+
"stopSpecOnExpectationFailure": false,
11+
"random": false
12+
}

src/js/ajax-data-provider.js

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,99 @@
1-
/* global Slick, $ */
2-
3-
/**
4-
* A sample AJAX data store implementation.
5-
* Right now, it's hooked up to load Hackernews stories, but can
6-
* easily be extended to support any JSONP-compatible backend that accepts paging parameters.
7-
*/
8-
function AjaxDataProvider() {
9-
// private
10-
var pageSize = 10;
11-
var page = 0;
12-
var totalCount = 0;
13-
var data = [];
14-
var url = 'http://192.168.60.167:3002/api.php';
15-
16-
var onDataLoaded = new Slick.Event();
17-
var onDataLoading = new Slick.Event();
18-
var onPaginationUpdated = new Slick.Event();
19-
var refreshHints = {};
20-
21-
function init() {
22-
if (data.length <= 0) {
23-
prepareData();
1+
/* eslint-disable */
2+
/* global $ */
3+
import Emitter from './emitter';
4+
import Pagination from './pagination';
5+
6+
class AjaxDataProvider extends Emitter {
7+
constructor() {
8+
super();
9+
10+
this.pageSize = 10;
11+
this.page = 0;
12+
this.totalCount = 0;
13+
this.data = [];
14+
15+
this.pagination = new Pagination({
16+
});
17+
18+
this.url = 'http://192.168.60.167:3002/api.php';
19+
this.refreshHints = {};
20+
if (this.data.length <= 0) {
21+
this.prepareData();
2422
}
2523
}
2624

27-
function setRefreshHints(hints) {
28-
refreshHints = hints;
25+
setRefreshHints(hints) {
26+
this.refreshHints = hints;
2927
}
3028

31-
function getRefreshHints() {
32-
return refreshHints;
29+
getRefreshHints() {
30+
return this.refreshHints;
3331
}
3432

35-
function isDataLoaded(from, to) {
36-
return data.slice(from, to)
37-
.filter(i => i).length > 0;
33+
isDataLoaded(from, to) {
34+
return this.data.slice(from, to)
35+
.filter(i => i).length > 0;
3836
}
3937

40-
function clearData() {
41-
data.splice(page, (page * pageSize));
38+
clearData() {
39+
this.data.splice(this.page, (this.page * this.pageSize));
4240
}
4341

44-
function onError() {
45-
throw new Error('Could not load page: ' + [page, page * pageSize].join('-'));
42+
onError() {
43+
throw new Error('Could not load page: ' + [
44+
this.page, this.page * this.pageSize
45+
].join('-'));
4646
}
4747

48-
function onSuccess(resp) {
49-
var from = page * pageSize;
48+
onSuccess(resp) {
49+
var from = this.page * this.pageSize;
5050
var to = from;
5151
if (resp.data.length > 0) {
5252
to = from + resp.data.length;
53-
data = data.concat(resp.data);
53+
this.data = this.data.concat(resp.data);
5454
}
5555

56-
onDataLoaded.notify({ from: from, to: to });
56+
this.emit('dataLoaded', {
57+
from: from, to: to
58+
});
5759
}
5860

59-
function prepareData() {
60-
onDataLoading.notify({ from: page, to: page * pageSize });
61+
prepareData() {
62+
this.emit('dataLoading', {
63+
from: this.page, to: this.page * this.pageSize
64+
});
6165

6266
$.ajax({
63-
url: url,
67+
url: this.url,
6468
type: 'get',
65-
data: { page: page, per_page: pageSize },
69+
data: { page: this.page, per_page: this.pageSize },
6670
dataType: 'json',
6771
cache: true,
68-
success: onSuccess,
69-
error: onError
72+
success: this.onSuccess.bind(this),
73+
error: this.onError.bind(this)
7074
});
7175
}
7276

73-
function getData() {
74-
return data;
77+
getData() {
78+
return this.data;
7579
}
7680

77-
function refresh(from, to) {
78-
clearData();
79-
getData(from, to);
81+
refresh(from, to) {
82+
this.clearData();
83+
this.getData(from, to);
8084
}
8185

82-
function getPagination() {
83-
var totalPages = page * pageSize;
86+
getPagination() {
87+
var totalPages = this.page * this.pageSize;
8488
return {
85-
page: page,
86-
pageSize: pageSize,
87-
totalCount: totalCount,
88-
totalPages: totalPages
89+
page: this.page,
90+
pageSize: this.pageSize,
91+
totalCount: this.totalCount,
92+
totalPages: this.totalPages
8993
};
9094
}
9195

92-
function setPaginationOptions(args) {
96+
setPaginationOptions(args) {
9397
var defaultPageSize = Math.max(0, Math.ceil(totalCount / pageSize) - 1);
9498

9599
if (args.pageSize !== undefined) {
@@ -98,40 +102,21 @@ function AjaxDataProvider() {
98102
}
99103

100104
if (args.page !== undefined) {
101-
page = Math.min(args.page, defaultPageSize);
105+
this.page = Math.min(args.page, defaultPageSize);
102106
}
103107

104108
onPaginationUpdated.notify(getPagination(), null, self);
105109

106-
refresh();
110+
this.refresh();
107111
}
108112

109-
function setPageSize(value) {
113+
setPageSize(value) {
110114
pageSize = value;
111115
}
112116

113-
function getPageSize() {
117+
getPageSize() {
114118
return pageSize;
115119
}
116-
117-
init();
118-
119-
return {
120-
getData: getData,
121-
isDataLoaded: isDataLoaded,
122-
prepareData: prepareData,
123-
refresh: refresh,
124-
setPageSize: setPageSize,
125-
getPageSize: getPageSize,
126-
getPagination: getPagination,
127-
setPaginationOptions: setPaginationOptions,
128-
setRefreshHints: setRefreshHints,
129-
getRefreshHints: getRefreshHints,
130-
131-
onDataLoaded: onDataLoaded,
132-
onDataLoading: onDataLoading,
133-
onPaginationUpdated: onPaginationUpdated
134-
};
135120
}
136121

137122
export default AjaxDataProvider;

0 commit comments

Comments
 (0)