diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 00000000..14721879 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,20 @@ +/* eslint-env node */ + +module.exports = { + plugins: [ 'metafizzy' ], + extends: 'plugin:metafizzy/browser', + env: { + browser: true, + commonjs: true, + }, + parserOptions: { + ecmaVersion: 2018, + }, + globals: { + InfiniteScroll: 'readonly', + QUnit: 'readonly', + define: 'readonly', + }, + rules: { + }, +}; diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000..8351c193 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +14 diff --git a/gulpfile.js b/gulpfile.js index 5377ae61..e716c358 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,55 +1,55 @@ -/*jshint node: true, strict: false */ +/* eslint-env node */ -var fs = require('fs'); -var gulp = require('gulp'); -var rename = require('gulp-rename'); -var replace = require('gulp-replace'); +let fs = require('fs'); +let gulp = require('gulp'); +let rename = require('gulp-rename'); +let replace = require('gulp-replace'); // ----- hint ----- // -var jshint = require('gulp-jshint'); +let jshint = require('gulp-jshint'); gulp.task( 'hint-js', function() { return gulp.src('js/**/*.js') .pipe( jshint() ) .pipe( jshint.reporter('default') ); -}); +} ); gulp.task( 'hint-test', function() { return gulp.src('test/unit/*.js') .pipe( jshint() ) .pipe( jshint.reporter('default') ); -}); +} ); gulp.task( 'hint-task', function() { return gulp.src('gulpfile.js') .pipe( jshint() ) .pipe( jshint.reporter('default') ); -}); +} ); -var jsonlint = require('gulp-json-lint'); +let jsonlint = require('gulp-json-lint'); gulp.task( 'jsonlint', function() { - return gulp.src( '*.json' ) + return gulp.src('*.json') .pipe( jsonlint() ) .pipe( jsonlint.report('verbose') ); -}); +} ); -gulp.task( 'hint', [ 'hint-js', 'hint-test', 'hint-task', 'jsonlint' ]); +gulp.task( 'hint', [ 'hint-js', 'hint-test', 'hint-task', 'jsonlint' ] ); // -------------------------- RequireJS makes pkgd -------------------------- // -var gutil = require('gulp-util'); -var chalk = require('chalk'); -var rjsOptimize = require('gulp-requirejs-optimize'); +let gutil = require('gulp-util'); +let chalk = require('chalk'); +let rjsOptimize = require('gulp-requirejs-optimize'); // regex for banner comment -var reBannerComment = new RegExp('^\\s*(?:\\/\\*[\\s\\S]*?\\*\\/)\\s*'); +let reBannerComment = new RegExp('^\\s*(?:\\/\\*[\\s\\S]*?\\*\\/)\\s*'); function getBanner() { - var src = fs.readFileSync( 'js/index.js', 'utf8' ); - var matches = src.match( reBannerComment ); - var banner = matches[0].replace( 'Infinite Scroll', 'Infinite Scroll PACKAGED' ); + let src = fs.readFileSync( 'js/index.js', 'utf8' ); + let matches = src.match( reBannerComment ); + let banner = matches[0].replace( 'Infinite Scroll', 'Infinite Scroll PACKAGED' ); return banner; } @@ -58,8 +58,8 @@ function addBanner( str ) { } gulp.task( 'requirejs', function() { - var banner = getBanner(); - var definitionRE = /define\(\s*'infinite-scroll\/js\/index'(.|\n)+\],\s*factory/; + let banner = getBanner(); + let definitionRE = /define\(\s*'infinite-scroll\/js\/index'(.|\n)+\],\s*factory/; // HACK src is not needed // should refactor rjsOptimize to produce src return gulp.src('js/index.js') @@ -69,12 +69,12 @@ gulp.task( 'requirejs', function() { include: [ 'jquery-bridget/jquery-bridget', 'infinite-scroll/js/index', - 'imagesloaded/imagesloaded' + 'imagesloaded/imagesloaded', ], paths: { 'infinite-scroll': '../', - jquery: 'empty:' - } + jquery: 'empty:', + }, }) ) // munge AMD definition .pipe( replace( definitionRE, function( definition ) { @@ -82,39 +82,38 @@ gulp.task( 'requirejs', function() { return definition.replace( "'infinite-scroll/js/index',", '' ) // use explicit file paths, './history' -> 'infinite-scroll/js/history' .replace( /'.\//g, "'infinite-scroll/js/" ); - }) ) + } ) ) // add banner .pipe( addBanner( banner ) ) .pipe( rename('infinite-scroll.pkgd.js') ) .pipe( gulp.dest('dist') ); -}); - +} ); // ----- uglify ----- // -var uglify = require('gulp-uglify'); +let uglify = require('gulp-uglify'); gulp.task( 'uglify', [ 'requirejs' ], function() { - var banner = getBanner(); + let banner = getBanner(); 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') ); -}); +} ); // ----- version ----- // // set version in source files -var minimist = require('minimist'); +let minimist = require('minimist'); // use gulp version -t 1.2.3 gulp.task( 'version', function() { - var args = minimist( process.argv.slice(3) ); - var version = args.t; - if ( !version || !/\d\.\d\.\d/.test( version ) ) { + let args = minimist( process.argv.slice( 3 ) ); + let version = args.t; + if ( !version || !( /\d\.\d\.\d/ ).test( version ) ) { gutil.log( 'invalid version: ' + chalk.red( version ) ); return; } @@ -127,11 +126,11 @@ gulp.task( 'version', function() { gulp.src('package.json') .pipe( replace( /"version": "\d\.\d+\.\d+"/, '"version": "' + version + '"' ) ) .pipe( gulp.dest('.') ); -}); +} ); // ----- default ----- // gulp.task( 'default', [ 'hint', 'uglify', -]); +] ); diff --git a/js/button.js b/js/button.js index 106e398a..1b7042f1 100644 --- a/js/button.js +++ b/js/button.js @@ -1,7 +1,6 @@ // button ( function( window, factory ) { // universal module definition - /* globals define, module, require */ if ( typeof define == 'function' && define.amd ) { // AMD define( [ @@ -9,20 +8,20 @@ 'fizzy-ui-utils/utils', ], function( InfiniteScroll, utils ) { return factory( window, InfiniteScroll, utils ); - }); + } ); } else if ( typeof module == 'object' && module.exports ) { // CommonJS module.exports = factory( - window, - require('./core'), - require('fizzy-ui-utils') + window, + require('./core'), + require('fizzy-ui-utils'), ); } else { // browser global factory( - window, - window.InfiniteScroll, - window.fizzyUIUtils + window, + window.InfiniteScroll, + window.fizzyUIUtils, ); } @@ -31,10 +30,9 @@ // InfiniteScroll.defaults.button = null; InfiniteScroll.create.button = function() { - var buttonElem = utils.getQueryElement( this.options.button ); + let buttonElem = utils.getQueryElement( this.options.button ); if ( buttonElem ) { this.button = new InfiniteScrollButton( buttonElem, this ); - return; } }; @@ -85,4 +83,4 @@ InfiniteScroll.Button = InfiniteScrollButton; return InfiniteScroll; -})); +} ) ); diff --git a/js/core.js b/js/core.js index 75c320a5..ddd1462c 100644 --- a/js/core.js +++ b/js/core.js @@ -1,39 +1,38 @@ // core ( function( window, factory ) { // universal module definition - /* globals define, module, require */ if ( typeof define == 'function' && define.amd ) { // AMD define( [ 'ev-emitter/ev-emitter', 'fizzy-ui-utils/utils', - ], function( EvEmitter, utils) { + ], function( EvEmitter, utils ) { return factory( window, EvEmitter, utils ); - }); + } ); } else if ( typeof module == 'object' && module.exports ) { // CommonJS module.exports = factory( - window, - require('ev-emitter'), - require('fizzy-ui-utils') + window, + require('ev-emitter'), + require('fizzy-ui-utils'), ); } else { // browser global window.InfiniteScroll = factory( - window, - window.EvEmitter, - window.fizzyUIUtils + window, + window.EvEmitter, + window.fizzyUIUtils, ); } }( window, function factory( window, EvEmitter, utils ) { -var jQuery = window.jQuery; +let jQuery = window.jQuery; // internal store of all InfiniteScroll intances -var instances = {}; +let instances = {}; function InfiniteScroll( element, options ) { - var queryElem = utils.getQueryElement( element ); + let queryElem = utils.getQueryElement( element ); if ( !queryElem ) { console.error( 'Bad element for InfiniteScroll: ' + ( queryElem || element ) ); @@ -42,7 +41,7 @@ function InfiniteScroll( element, options ) { element = queryElem; // do not initialize twice on same element if ( element.infiniteScrollGUID ) { - var instance = instances[ element.infiniteScrollGUID ]; + let instance = instances[ element.infiniteScrollGUID ]; instance.option( options ); return instance; } @@ -70,19 +69,19 @@ InfiniteScroll.defaults = { InfiniteScroll.create = {}; InfiniteScroll.destroy = {}; -var proto = InfiniteScroll.prototype; +let proto = InfiniteScroll.prototype; // inherit EvEmitter utils.extend( proto, EvEmitter.prototype ); // -------------------------- -------------------------- // // globally unique identifiers -var GUID = 0; +let GUID = 0; proto.create = function() { // create core // add id for InfiniteScroll.data - var id = this.guid = ++GUID; + let id = this.guid = ++GUID; this.element.infiniteScrollGUID = id; // expando instances[ id ] = this; // associate via id // properties @@ -90,7 +89,7 @@ proto.create = function() { this.loadCount = 0; this.updateGetPath(); // bail if getPath not set, or returns falsey #776 - var hasPath = this.getPath && this.getPath(); + let hasPath = this.getPath && this.getPath(); if ( !hasPath ) { console.error('Disabling InfiniteScroll'); return; @@ -99,7 +98,7 @@ proto.create = function() { this.log( 'initialized', [ this.element.className ] ); this.callOnInit(); // create features - for ( var method in InfiniteScroll.create ) { + for ( let method in InfiniteScroll.create ) { InfiniteScroll.create[ method ].call( this ); } }; @@ -110,7 +109,7 @@ proto.option = function( opts ) { // call onInit option, used for binding events on init proto.callOnInit = function() { - var onInit = this.options.onInit; + let onInit = this.options.onInit; if ( onInit ) { onInit.call( this, this ); } @@ -120,7 +119,7 @@ proto.callOnInit = function() { proto.dispatchEvent = function( type, event, args ) { this.log( type, args ); - var emitArgs = event ? [ event ].concat( args ) : args; + let emitArgs = event ? [ event ].concat( args ) : args; this.emitEvent( type, emitArgs ); // trigger jQuery event if ( !jQuery || !this.$element ) { @@ -128,17 +127,18 @@ proto.dispatchEvent = function( type, event, args ) { } // namespace jQuery event type += '.infiniteScroll'; - var $event = type; + let $event = type; if ( event ) { // create jQuery event - var jQEvent = jQuery.Event( event ); + /* eslint-disable-next-line new-cap */ + let jQEvent = jQuery.Event( event ); jQEvent.type = type; $event = jQEvent; } this.$element.trigger( $event, args ); }; -var loggers = { +let loggers = { initialized: function( className ) { return 'on ' + className; }, @@ -170,8 +170,8 @@ proto.log = function( type, args ) { if ( !this.options.debug ) { return; } - var message = '[InfiniteScroll] ' + type; - var logger = loggers[ type ]; + let message = '[InfiniteScroll] ' + type; + let logger = loggers[ type ]; if ( logger ) { message += '. ' + logger.apply( this, args ); } @@ -182,12 +182,12 @@ proto.log = function( type, args ) { proto.updateMeasurements = function() { this.windowHeight = window.innerHeight; - var rect = this.element.getBoundingClientRect(); + let rect = this.element.getBoundingClientRect(); this.top = rect.top + window.pageYOffset; }; proto.updateScroller = function() { - var elementScroll = this.options.elementScroll; + let elementScroll = this.options.elementScroll; if ( !elementScroll ) { // default, use window this.scroller = window; @@ -197,26 +197,26 @@ proto.updateScroller = function() { this.scroller = elementScroll === true ? this.element : utils.getQueryElement( elementScroll ); if ( !this.scroller ) { - throw 'Unable to find elementScroll: ' + elementScroll; + throw new Error( 'Unable to find elementScroll: ' + elementScroll ); } }; // -------------------------- page path -------------------------- // proto.updateGetPath = function() { - var optPath = this.options.path; + let optPath = this.options.path; if ( !optPath ) { console.error( 'InfiniteScroll path option required. Set as: ' + optPath ); return; } // function - var type = typeof optPath; + let type = typeof optPath; if ( type == 'function' ) { this.getPath = optPath; return; } // template string: '/pages/{{#}}.html' - var templateMatch = type == 'string' && optPath.match('{{#}}'); + let templateMatch = type == 'string' && optPath.match('{{#}}'); if ( templateMatch ) { this.updateGetPathTemplate( optPath ); return; @@ -228,17 +228,17 @@ proto.updateGetPath = function() { proto.updateGetPathTemplate = function( optPath ) { // set getPath with template string this.getPath = function() { - var nextIndex = this.pageIndex + 1; + let nextIndex = this.pageIndex + 1; return optPath.replace( '{{#}}', nextIndex ); }.bind( this ); // get pageIndex from location // convert path option into regex to look for pattern in location - // escape query (?) in url, allows for parsing GET parameters - var regexString = optPath + // escape query (?) in url, allows for parsing GET parameters + let regexString = optPath .replace( /(\\\?|\?)/, '\\?' ) .replace( '{{#}}', '(\\d\\d?\\d?)' ); - var templateRe = new RegExp( regexString ); - var match = location.href.match( templateRe ); + let templateRe = new RegExp( regexString ); + let match = location.href.match( templateRe ); if ( match ) { this.pageIndex = parseInt( match[1], 10 ); @@ -246,7 +246,7 @@ proto.updateGetPathTemplate = function( optPath ) { } }; -var pathRegexes = [ +let pathRegexes = [ // WordPress & Tumblr - example.com/page/2 // Jekyll - example.com/page2 /^(.*?\/?page\/?)(\d\d?\d?)(.*?$)/, @@ -258,21 +258,23 @@ var pathRegexes = [ proto.updateGetPathSelector = function( optPath ) { // parse href of link: '.next-page-link' - var hrefElem = document.querySelector( optPath ); + let hrefElem = document.querySelector( optPath ); if ( !hrefElem ) { console.error( 'Bad InfiniteScroll path option. Next link not found: ' + optPath ); return; } - var href = hrefElem.getAttribute('href'); + let href = hrefElem.getAttribute('href'); // try matching href to pathRegexes patterns - var pathParts, regex; - for ( var i=0; href && i < pathRegexes.length; i++ ) { - regex = pathRegexes[i]; - var match = href.match( regex ); - if ( match ) { - pathParts = match.slice(1); // remove first part - break; + let pathParts, regex; + if ( href ) { + for ( let i = 0; i < pathRegexes.length; i++ ) { + regex = pathRegexes[i]; + let match = href.match( regex ); + if ( match ) { + pathParts = match.slice( 1 ); // remove first part + break; + } } } if ( !pathParts ) { @@ -281,7 +283,7 @@ proto.updateGetPathSelector = function( optPath ) { } this.isPathSelector = true; // flag for checkLastPage() this.getPath = function() { - var nextIndex = this.pageIndex + 1; + let nextIndex = this.pageIndex + 1; return pathParts[0] + nextIndex + pathParts[2]; }.bind( this ); // get pageIndex from href @@ -290,17 +292,17 @@ proto.updateGetPathSelector = function( optPath ) { }; proto.updateGetAbsolutePath = function() { - var path = this.getPath(); + let path = this.getPath(); // path doesn't start with http or / - var isAbsolute = path.match( /^http/ ) || path.match( /^\// ); + let isAbsolute = path.match( /^http/ ) || path.match( /^\// ); if ( isAbsolute ) { this.getAbsolutePath = this.getPath; return; } - var pathname = location.pathname; + let pathname = location.pathname; // query parameter #829. example.com/?pg=2 - var isQuery = path.match( /^\?/ ); + let isQuery = path.match( /^\?/ ); if ( isQuery ) { this.getAbsolutePath = function() { return pathname + this.getPath(); @@ -309,7 +311,7 @@ proto.updateGetAbsolutePath = function() { } // /foo/bar/index.html => /foo/bar - var directory = pathname.substring( 0, pathname.lastIndexOf('/') ); + let directory = pathname.substring( 0, pathname.lastIndexOf('/') ); this.getAbsolutePath = function() { return directory + '/' + this.getPath(); }; @@ -319,7 +321,7 @@ proto.updateGetAbsolutePath = function() { // hide navigation InfiniteScroll.create.hideNav = function() { - var nav = utils.getQueryElement( this.options.hideNav ); + let nav = utils.getQueryElement( this.options.hideNav ); if ( !nav ) { return; } @@ -338,7 +340,7 @@ InfiniteScroll.destroy.hideNav = function() { proto.destroy = function() { this.allOff(); // remove all event listeners // call destroy methods - for ( var method in InfiniteScroll.destroy ) { + for ( let method in InfiniteScroll.destroy ) { InfiniteScroll.destroy[ method ].call( this ); } @@ -355,12 +357,12 @@ proto.destroy = function() { // https://remysharp.com/2010/07/21/throttling-function-calls InfiniteScroll.throttle = function( fn, threshold ) { threshold = threshold || 200; - var last, timeout; + let last, timeout; return function() { - var now = +new Date(); - var args = arguments; - var trigger = function() { + let now = +new Date(); + let args = arguments; + let trigger = function() { last = now; fn.apply( this, args ); }.bind( this ); @@ -376,13 +378,13 @@ InfiniteScroll.throttle = function( fn, threshold ) { InfiniteScroll.data = function( elem ) { elem = utils.getQueryElement( elem ); - var id = elem && elem.infiniteScrollGUID; + let id = elem && elem.infiniteScrollGUID; return id && instances[ id ]; }; // set internal jQuery, for Webpack + jQuery v3 -InfiniteScroll.setJQuery = function( $ ) { - jQuery = $; +InfiniteScroll.setJQuery = function( jqry ) { + jQuery = jqry; }; // -------------------------- setup -------------------------- // @@ -400,4 +402,4 @@ if ( jQuery && jQuery.bridget ) { return InfiniteScroll; -})); +} ) ); diff --git a/js/history.js b/js/history.js index 8525d07e..903097f8 100644 --- a/js/history.js +++ b/js/history.js @@ -1,7 +1,6 @@ // history ( function( window, factory ) { // universal module definition - /* globals define, module, require */ if ( typeof define == 'function' && define.amd ) { // AMD define( [ @@ -9,31 +8,31 @@ 'fizzy-ui-utils/utils', ], function( InfiniteScroll, utils ) { return factory( window, InfiniteScroll, utils ); - }); + } ); } else if ( typeof module == 'object' && module.exports ) { // CommonJS module.exports = factory( - window, - require('./core'), - require('fizzy-ui-utils') + window, + require('./core'), + require('fizzy-ui-utils'), ); } else { // browser global factory( - window, - window.InfiniteScroll, - window.fizzyUIUtils + window, + window.InfiniteScroll, + window.fizzyUIUtils, ); } }( window, function factory( window, InfiniteScroll, utils ) { -var proto = InfiniteScroll.prototype; +let proto = InfiniteScroll.prototype; InfiniteScroll.defaults.history = 'replace'; // InfiniteScroll.defaults.historyTitle = false; -var link = document.createElement('a'); +let link = document.createElement('a'); // ----- create/destroy ----- // @@ -43,9 +42,10 @@ InfiniteScroll.create.history = function() { } // check for same origin link.href = this.getAbsolutePath(); - // MS Edge does not have origin on link https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12236493/ - var linkOrigin = link.origin || link.protocol + '//' + link.host; - var isSameOrigin = linkOrigin == location.origin; + // MS Edge does not have origin on link + // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12236493/ + let linkOrigin = link.origin || link.protocol + '//' + link.host; + let isSameOrigin = linkOrigin == location.origin; if ( !isSameOrigin ) { console.error( '[InfiniteScroll] cannot set history with different origin: ' + link.origin + ' on ' + location.origin + @@ -71,7 +71,7 @@ proto.createHistoryAppend = function() { top: 0, path: location.href, title: document.title, - } + }, ]; this.scrollPageIndex = 0; // events @@ -83,7 +83,7 @@ proto.createHistoryAppend = function() { }; proto.bindHistoryAppendEvents = function( isBind ) { - var addRemove = isBind ? 'addEventListener' : 'removeEventListener'; + let addRemove = isBind ? 'addEventListener' : 'removeEventListener'; this.scroller[ addRemove ]( 'scroll', this.scrollHistoryHandler ); window[ addRemove ]( 'unload', this.unloadHandler ); }; @@ -94,7 +94,7 @@ proto.createHistoryPageLoad = function() { InfiniteScroll.destroy.history = proto.destroyHistory = function() { - var isHistoryAppend = this.options.history && this.options.append; + let isHistoryAppend = this.options.history && this.options.append; if ( isHistoryAppend ) { this.bindHistoryAppendEvents( false ); } @@ -107,8 +107,8 @@ proto.onAppendHistory = function( response, path, items ) { if ( !items || !items.length ) { return; } - var firstItem = items[0]; - var elemScrollY = this.getElementScrollY( firstItem ); + let firstItem = items[0]; + let elemScrollY = this.getElementScrollY( firstItem ); // resolve path link.href = path; // add page data to hash @@ -128,7 +128,7 @@ proto.getElementScrollY = function( elem ) { }; proto.getElementWindowScrollY = function( elem ) { - var rect = elem.getBoundingClientRect(); + let rect = elem.getBoundingClientRect(); return rect.top + window.pageYOffset; }; @@ -139,10 +139,10 @@ proto.getElementElementScrollY = function( elem ) { proto.onScrollHistory = function() { // cycle through positions, find biggest without going over - var scrollViewY = this.getScrollViewY(); - var pageIndex, page; - for ( var i=0; i < this.scrollPages.length; i++ ) { - var scrollPage = this.scrollPages[i]; + let scrollViewY = this.getScrollViewY(); + let pageIndex, page; + for ( let i = 0; i < this.scrollPages.length; i++ ) { + let scrollPage = this.scrollPages[i]; if ( scrollPage.top >= scrollViewY ) { break; } @@ -160,15 +160,15 @@ utils.debounceMethod( InfiniteScroll, 'onScrollHistory', 150 ); proto.getScrollViewY = function() { if ( this.options.elementScroll ) { - return this.scroller.scrollTop + this.scroller.clientHeight/2; + return this.scroller.scrollTop + this.scroller.clientHeight / 2; } else { - return window.pageYOffset + this.windowHeight/2; + return window.pageYOffset + this.windowHeight / 2; } }; proto.setHistory = function( title, path ) { - var optHistory = this.options.history; - var historyMethod = optHistory && history[ optHistory + 'State' ]; + let optHistory = this.options.history; + let historyMethod = optHistory && history[ optHistory + 'State' ]; if ( !historyMethod ) { return; } @@ -185,13 +185,13 @@ proto.setHistory = function( title, path ) { // scroll to top to prevent initial scroll-reset after page refresh // https://stackoverflow.com/a/18633915/182183 proto.onUnload = function() { - var pageIndex = this.scrollPageIndex; + let pageIndex = this.scrollPageIndex; if ( pageIndex === 0 ) { return; } // calculate where scroll position would be on refresh - var scrollPage = this.scrollPages[ pageIndex ]; - var scrollY = window.pageYOffset - scrollPage.top + this.top; + let scrollPage = this.scrollPages[ pageIndex ]; + let scrollY = window.pageYOffset - scrollPage.top + this.top; // disable scroll event before setting scroll #679 this.destroyHistory(); scrollTo( 0, scrollY ); @@ -208,4 +208,4 @@ proto.onPageLoadHistory = function( response, path ) { return InfiniteScroll; -})); +} ) ); diff --git a/js/index.js b/js/index.js index b010a736..5024526e 100644 --- a/js/index.js +++ b/js/index.js @@ -11,7 +11,6 @@ ( function( window, factory ) { // universal module definition - /* globals define, module, require */ if ( typeof define == 'function' && define.amd ) { // AMD define( [ @@ -25,15 +24,15 @@ } else if ( typeof module == 'object' && module.exports ) { // CommonJS module.exports = factory( - require('./core'), - require('./page-load'), - require('./scroll-watch'), - require('./history'), - require('./button'), - require('./status') + require('./core'), + require('./page-load'), + require('./scroll-watch'), + require('./history'), + require('./button'), + require('./status'), ); } -})( window, function factory( InfiniteScroll ) { +} )( window, function factory( InfiniteScroll ) { return InfiniteScroll; -}); +} ); diff --git a/js/page-load.js b/js/page-load.js index b3fc6ada..11d34668 100644 --- a/js/page-load.js +++ b/js/page-load.js @@ -1,31 +1,30 @@ // page-load ( function( window, factory ) { // universal module definition - /* globals define, module, require */ if ( typeof define == 'function' && define.amd ) { // AMD define( [ './core', ], function( InfiniteScroll ) { return factory( window, InfiniteScroll ); - }); + } ); } else if ( typeof module == 'object' && module.exports ) { // CommonJS module.exports = factory( - window, - require('./core') + window, + require('./core'), ); } else { // browser global factory( - window, - window.InfiniteScroll + window, + window.InfiniteScroll, ); } }( window, function factory( window, InfiniteScroll ) { -var proto = InfiniteScroll.prototype; +let proto = InfiniteScroll.prototype; // InfiniteScroll.defaults.append = false; InfiniteScroll.defaults.loadOnScroll = true; @@ -54,18 +53,18 @@ proto.loadNextPage = function() { return; } - var path = this.getAbsolutePath(); + let path = this.getAbsolutePath(); this.isLoading = true; - var onLoad = function( response ) { + let onLoad = function( response ) { this.onPageLoad( response, path ); }.bind( this ); - var onError = function( error ) { + let onError = function( error ) { this.onPageError( error, path ); }.bind( this ); - var onLast = function( response ) { + let onLast = function( response ) { this.lastPageReached( response, path ); }.bind( this ); @@ -86,16 +85,16 @@ proto.onPageLoad = function( response, path ) { }; proto.appendNextPage = function( response, path ) { - var optAppend = this.options.append; + let optAppend = this.options.append; // do not append json - var isDocument = this.options.responseType == 'document'; + let isDocument = this.options.responseType == 'document'; if ( !isDocument || !optAppend ) { return; } - var items = response.querySelectorAll( optAppend ); - var fragment = getItemsFragment( items ); - var appendReady = function () { + let items = response.querySelectorAll( optAppend ); + let fragment = getItemsFragment( items ); + let appendReady = function() { this.appendItems( items, fragment ); this.isLoading = false; this.dispatchEvent( 'append', null, [ response, path, items ] ); @@ -121,9 +120,11 @@ proto.appendItems = function( items, fragment ) { function getItemsFragment( items ) { // add items to fragment - var fragment = document.createDocumentFragment(); - for ( var i=0; items && i < items.length; i++ ) { - fragment.appendChild( items[i] ); + let fragment = document.createDocumentFragment(); + if ( items ) { + for ( let i = 0; i < items.length; i++ ) { + fragment.appendChild( items[i] ); + } } return fragment; } @@ -132,10 +133,10 @@ function getItemsFragment( items ) { //