Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"browser": true,
"unused": true,
"undef": true,
"esversion": 9,
"globals": {
"console": false
}
Expand Down
104 changes: 69 additions & 35 deletions dist/infinite-scroll.pkgd.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ var proto = InfiniteScroll.prototype;
InfiniteScroll.defaults.loadOnScroll = true;
InfiniteScroll.defaults.checkLastPage = true;
InfiniteScroll.defaults.responseType = 'document';
InfiniteScroll.defaults.method = 'GET';
// InfiniteScroll.defaults.prefill = false;
// InfiniteScroll.defaults.outlayer = null;

Expand Down Expand Up @@ -1037,7 +1038,7 @@ proto.loadNextPage = function() {
this.lastPageReached( response, path );
}.bind( this );

request( path, this.options.responseType, onLoad, onError, onLast );
request( path, this.options.responseType, onLoad, onError, onLast , this.options.method, this.options.params );
this.dispatchEvent( 'request', null, [ path ] );
};

Expand Down Expand Up @@ -1233,36 +1234,69 @@ proto.stopPrefill = function() {
this.off( 'append', this.prefill );
};

// -------------------------- request -------------------------- //

function request( url, responseType, onLoad, onError, onLast ) {
var req = new XMLHttpRequest();
req.open( 'GET', url, true );
// set responseType document to return DOM
req.responseType = responseType || '';
proto.method = function () {
this.log('method');
};

// set X-Requested-With header to check that is ajax request
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
// -------------------------- request -------------------------- //
function request( url, responseType, onLoad, onError, onLast, method, params ) {
if (typeof params === 'function') {
params = params();
}

req.onload = function() {
if ( req.status == 200 ) {
onLoad( req.response );
} else if ( req.status == 204 ) {
onLast( req.response );
} else {
// not 200 OK, error
var error = new Error( req.statusText );
onError( error );
if (typeof params === 'object') {
if (params instanceof HTMLFormElement) {
params = new FormData(params);
} else {
params = Object.keys(params).map(function(key) {
return key + '=' + params[key];
}).join('&');
}
}
// console.log(method);
fetch(url, {
method: method,
headers: { 'Content-type': 'application/x-www-form-urlencoded' },
credentials: 'include',
body: params
}).then(function(response) {
var responseData;
// console.log(responseType);
switch (responseType) {
case 'arraybuffer':
responseData = response.arrayBuffer();
break;
case 'blob':
responseData = response.blob();
break;
case 'json':
responseData = response.json();
break;
case 'document':
responseData = response.text().then(
function(res) {
var doc = document.implementation.createHTMLDocument('');
doc.open();doc.write(res);doc.close();
return Promise.resolve(doc);
});
break;
case 'text':
default:
responseData = response.text();
break;
}
};

// Handle network errors
req.onerror = function() {
var error = new Error( 'Network error requesting ' + url );
onError( error );
};
if (200 === response.status) {
return responseData.then(function(res){ onLoad(res);});
}
if (204 === response.status) {
return responseData.then(function(res){ onLast(res);});
}
var error = new Error(response.statusText);
return onError(error);

req.send();
}).catch(function() {
return onError(new Error("Network error requesting " + url));
});
}

// -------------------------- -------------------------- //
Expand Down Expand Up @@ -1803,7 +1837,7 @@ return InfiniteScroll;
}));

/*!
* Infinite Scroll v3.0.6
* Infinite Scroll v1.2.3
* Automatically add next page
*
* Licensed GPLv3 for open source use
Expand All @@ -1818,13 +1852,13 @@ return InfiniteScroll;
/* globals define, module, require */
if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
'infinite-scroll/js/core',
'infinite-scroll/js/page-load',
'infinite-scroll/js/scroll-watch',
'infinite-scroll/js/history',
'infinite-scroll/js/button',
'infinite-scroll/js/status',
define( 'infinite-scroll/js/index',[
'./core',
'./page-load',
'./scroll-watch',
'./history',
'./button',
'./status',
], factory );
} else if ( typeof module == 'object' && module.exports ) {
// CommonJS
Expand Down
20 changes: 11 additions & 9 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ gulp.task( 'jsonlint', function() {
.pipe( jsonlint.report('verbose') );
});

gulp.task( 'hint', [ 'hint-js', 'hint-test', 'hint-task', 'jsonlint' ]);
gulp.task( 'hint', gulp.series( 'hint-js', 'hint-test', 'hint-task', 'jsonlint' ));

// -------------------------- RequireJS makes pkgd -------------------------- //

var gutil = require('gulp-util');
var log = require('fancy-log');
var chalk = require('chalk');
var rjsOptimize = require('gulp-requirejs-optimize');

Expand Down Expand Up @@ -94,16 +94,18 @@ gulp.task( 'requirejs', function() {

var uglify = require('gulp-uglify');

gulp.task( 'uglify', [ 'requirejs' ], function() {
gulp.task( 'uglify-min', function() {
var banner = getBanner();
gulp.src('dist/infinite-scroll.pkgd.js')
return gulp.src('dist/infinite-scroll.pkgd.js')
.pipe( uglify() )
// add banner
.pipe( addBanner( banner ) )
.pipe( rename('infinite-scroll.pkgd.min.js') )
.pipe( gulp.dest('dist') );
});

gulp.task( 'uglify', gulp.series( 'requirejs' , 'uglify-min'));

// ----- version ----- //

// set version in source files
Expand All @@ -115,10 +117,10 @@ gulp.task( 'version', function() {
var args = minimist( process.argv.slice(3) );
var version = args.t;
if ( !version || !/\d\.\d\.\d/.test( version ) ) {
gutil.log( 'invalid version: ' + chalk.red( version ) );
log( 'invalid version: ' + chalk.red( version ) );
return;
}
gutil.log( 'ticking version to ' + chalk.green( version ) );
log( 'ticking version to ' + chalk.green( version ) );

gulp.src('js/index.js')
.pipe( replace( /Infinite Scroll v\d\.\d+\.\d+/, 'Infinite Scroll v' + version ) )
Expand All @@ -131,7 +133,7 @@ gulp.task( 'version', function() {

// ----- default ----- //

gulp.task( 'default', [
gulp.task( 'default', gulp.series(
'hint',
'uglify',
]);
'uglify'
));
88 changes: 61 additions & 27 deletions js/page-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var proto = InfiniteScroll.prototype;
InfiniteScroll.defaults.loadOnScroll = true;
InfiniteScroll.defaults.checkLastPage = true;
InfiniteScroll.defaults.responseType = 'document';
InfiniteScroll.defaults.method = 'GET';
// InfiniteScroll.defaults.prefill = false;
// InfiniteScroll.defaults.outlayer = null;

Expand Down Expand Up @@ -69,7 +70,7 @@ proto.loadNextPage = function() {
this.lastPageReached( response, path );
}.bind( this );

request( path, this.options.responseType, onLoad, onError, onLast );
request( path, this.options.responseType, onLoad, onError, onLast , this.options.method, this.options.params );
this.dispatchEvent( 'request', null, [ path ] );
};

Expand Down Expand Up @@ -265,36 +266,69 @@ proto.stopPrefill = function() {
this.off( 'append', this.prefill );
};

proto.method = function () {
this.log('method');
};

// -------------------------- request -------------------------- //
function request( url, responseType, onLoad, onError, onLast, method, params ) {
if (typeof params === 'function') {
params = params();
}

function request( url, responseType, onLoad, onError, onLast ) {
var req = new XMLHttpRequest();
req.open( 'GET', url, true );
// set responseType document to return DOM
req.responseType = responseType || '';

// set X-Requested-With header to check that is ajax request
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

req.onload = function() {
if ( req.status == 200 ) {
onLoad( req.response );
} else if ( req.status == 204 ) {
onLast( req.response );
} else {
// not 200 OK, error
var error = new Error( req.statusText );
onError( error );
if (typeof params === 'object') {
if (params instanceof HTMLFormElement) {
params = new FormData(params);
} else {
params = Object.keys(params).map(function(key) {
return key + '=' + params[key];
}).join('&');
}
}
// console.log(method);
fetch(url, {
method: method,
headers: { 'Content-type': 'application/x-www-form-urlencoded' },
credentials: 'include',
body: params
}).then(function(response) {
var responseData;
// console.log(responseType);
switch (responseType) {
case 'arraybuffer':
responseData = response.arrayBuffer();
break;
case 'blob':
responseData = response.blob();
break;
case 'json':
responseData = response.json();
break;
case 'document':
responseData = response.text().then(
function(res) {
var doc = document.implementation.createHTMLDocument('');
doc.open();doc.write(res);doc.close();
return Promise.resolve(doc);
});
break;
case 'text':
default:
responseData = response.text();
break;
}
};

// Handle network errors
req.onerror = function() {
var error = new Error( 'Network error requesting ' + url );
onError( error );
};
if (200 === response.status) {
return responseData.then(function(res){ onLoad(res);});
}
if (204 === response.status) {
return responseData.then(function(res){ onLast(res);});
}
var error = new Error(response.statusText);
return onError(error);

req.send();
}).catch(function() {
return onError(new Error("Network error requesting " + url));
});
}

// -------------------------- -------------------------- //
Expand Down
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@
"fizzy-ui-utils": "^2.0.5"
},
"devDependencies": {
"@babel/core": "^7.7.7",
"bower": "^1.8.8",
"chalk": "^0.5.1",
"gulp": "^3.9.1",
"gulp-jshint": "^1.12.0",
"gulp-json-lint": "0.0.1",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4",
"fancy-log": "^1.3.3",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-jshint": "^2.1.0",
"gulp-json-lint": "^0.1.0",
"gulp-rename": "^1.4.0",
"gulp-replace": "^1.0.0",
"gulp-requirejs-optimize": "github:metafizzy/gulp-requirejs-optimize",
"gulp-uglify": "^1.5.4",
"gulp-util": "^3.0.8",
"gulp-uglify": "^3.0.2",
"imagesloaded": "^4.1.3",
"jquery": "^3.2.1",
"jquery-bridget": "^2.0.1",
"jshint": "^2.11.0-rc1",
"masonry-layout": "^4.2.0",
"minimist": "^1.2.0",
"qunitjs": "^2.3.2"
"qunit": "^2.9.3"
},
"scripts": {
"test": "echo \"View test/ in browser\" && exit 1"
Expand Down
1 change: 1 addition & 0 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"devel": true,
"unused": true,
"undef": true,
"esversion": 9,
"globals": {
"InfiniteScroll": false,
"QUnit": false
Expand Down
9 changes: 8 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<!-- unit tests -->
<script src="unit/path.js"></script>
<script src="unit/page-load.js"></script>
<script src="unit/page-load-method.js"></script>
<script src="unit/page-load-error.js"></script>
<script src="unit/page-index.js"></script>
<script src="unit/check-last-page.js"></script>
Expand Down Expand Up @@ -57,6 +58,12 @@ <h2>path</h2>
<div class="demo demo--path"></div>
<p><a class="path-next-link" href="/area51/selector/page10.html">Next</a></p>

<h2>page load - method</h2>
<div class="demo demo--page-load-method">
<div class="post">page 1, post 1</div>
</div>
<p><a class="page-load-method-next-link" href="page/2-method.html">Next</a></p>

<h2>page load</h2>
<div class="demo demo--page-load">
<div class="post">page 1, post 1</div>
Expand Down Expand Up @@ -126,4 +133,4 @@ <h2>outlayer</h2>
<footer class="footer"></footer>

</body>
</html>
</html>
Loading