Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates #1

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: cam
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
2 changes: 1 addition & 1 deletion dist/ajaxinate-min.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

196 changes: 121 additions & 75 deletions dist/ajaxinate.js
Original file line number Diff line number Diff line change
@@ -1,145 +1,191 @@
'use strict';

/* ===================================================================================== @preserve =
___ _ _ _
/ || | | | | |
\__ | | | | | | __
/ |/ |/_) |/ / \_/\/
\___/|__/| \_/|__/\__/ /\_/
|\
|/
Ajaxinate
version v2.0.11
https://github.com/Elkfox/Ajaxinate
Copyright (c) 2017 Elkfox Co Pty Ltd
https://elkfox.com
MIT License
================================================================================================= */

var Ajaxinate = function ajaxinateConstructor(config) {
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Ajaxinate = Ajaxinate;
/* @preserve
* https://github.com/Elkfox/Ajaxinate
* Copyright (c) 2017 Elkfox Co Pty Ltd (elkfox.com)
* MIT License (do not remove above copyright!)
*/

/* Configurable options;
*
* method: scroll or click
* container: selector of repeating content
* pagination: selector of pagination container
* offset: number of pixels before the bottom to start loading more on scroll
* loadingText: 'Loading', The text shown during when appending new content
* callback: null, callback function after new content is appended
*
* Usage;
*
* import {Ajaxinate} from 'ajaxinate';
*
* new Ajaxinate({
* offset: 5000,
* loadingText: 'Loading more...',
* });
*/

/* eslint-env browser */
function Ajaxinate(config) {
var settings = config || {};
/*
pagination: Selector of pagination container
method: [options are 'scroll', 'click']
container: Selector of repeating content
offset: 0, offset the number of pixels before the bottom to start loading more on scroll
loadingText: 'Loading', The text changed during loading
callback: null, function to callback after a new page is loaded
*/
var defaultSettings = {
pagination: '#AjaxinatePagination',

var defaults = {
method: 'scroll',
container: '#AjaxinateLoop',
container: '#AjaxinateContainer',
pagination: '#AjaxinatePagination',
offset: 0,
loadingText: 'Loading',
callback: null
};
// Merge configs
this.settings = Object.assign(defaultSettings, settings);

// Bind 'this' to applicable prototype functions
// Merge custom configs with defaults
this.settings = Object.assign(defaults, settings);

// Functions
this.addScrollListeners = this.addScrollListeners.bind(this);
this.addClickListener = this.addClickListener.bind(this);
this.checkIfPaginationInView = this.checkIfPaginationInView.bind(this);
this.stopMultipleClicks = this.stopMultipleClicks.bind(this);
this.preventMultipleClicks = this.preventMultipleClicks.bind(this);
this.removeClickListener = this.removeClickListener.bind(this);
this.removeScrollListener = this.removeScrollListener.bind(this);
this.removePaginationElement = this.removePaginationElement.bind(this);
this.destroy = this.destroy.bind(this);

// Set up our element selectors
// Selectors
this.containerElement = document.querySelector(this.settings.container);
this.paginationElement = document.querySelector(this.settings.pagination);

this.initialize();
};
}

Ajaxinate.prototype.initialize = function initializeTheCorrectFunctionsBasedOnTheMethod() {
// Find and initialise the correct function based on the method set in the config
if (this.containerElement) {
var initializers = {
click: this.addClickListener,
scroll: this.addScrollListeners
};
initializers[this.settings.method]();
Ajaxinate.prototype.initialize = function initialize() {
if (!this.containerElement) {
return;
}

var initializers = {
click: this.addClickListener,
scroll: this.addScrollListeners
};

initializers[this.settings.method]();
};

Ajaxinate.prototype.addScrollListeners = function addEventListenersForScrolling() {
if (this.paginationElement) {
document.addEventListener('scroll', this.checkIfPaginationInView);
window.addEventListener('resize', this.checkIfPaginationInView);
window.addEventListener('orientationchange', this.checkIfPaginationInView);
Ajaxinate.prototype.addScrollListeners = function addScrollListeners() {
if (!this.paginationElement) {
return;
}

document.addEventListener('scroll', this.checkIfPaginationInView);
window.addEventListener('resize', this.checkIfPaginationInView);
window.addEventListener('orientationchange', this.checkIfPaginationInView);
};

Ajaxinate.prototype.addClickListener = function addEventListenerForClicking() {
if (this.paginationElement) {
this.nextPageLinkElement = this.paginationElement.querySelector('a');
this.clickActive = true;
if (this.nextPageLinkElement !== null) {
this.nextPageLinkElement.addEventListener('click', this.stopMultipleClicks);
}
Ajaxinate.prototype.addClickListener = function addClickListener() {
if (!this.paginationElement) {
return;
}

this.nextPageLinkElement = this.paginationElement.querySelector('a');
this.clickActive = true;

if (typeof this.nextPageLinkElement !== 'undefined' && this.nextPageLinkElement !== null) {
this.nextPageLinkElement.addEventListener('click', this.preventMultipleClicks);
}
};

Ajaxinate.prototype.stopMultipleClicks = function handleClickEvent(event) {
Ajaxinate.prototype.preventMultipleClicks = function preventMultipleClicks(event) {
event.preventDefault();
if (this.clickActive) {
this.nextPageLinkElement.innerHTML = this.settings.loadingText;
this.nextPageUrl = this.nextPageLinkElement.href;
this.clickActive = false;
this.loadMore();

if (!this.clickActive) {
return;
}

this.nextPageLinkElement.innerText = this.settings.loadingText;
this.nextPageUrl = this.nextPageLinkElement.href;
this.clickActive = false;

this.loadMore();
};

Ajaxinate.prototype.checkIfPaginationInView = function handleScrollEvent() {
Ajaxinate.prototype.checkIfPaginationInView = function checkIfPaginationInView() {
var top = this.paginationElement.getBoundingClientRect().top - this.settings.offset;
var bottom = this.paginationElement.getBoundingClientRect().bottom + this.settings.offset;

if (top <= window.innerHeight && bottom >= 0) {
this.nextPageLinkElement = this.paginationElement.querySelector('a');
this.removeScrollListener();

if (this.nextPageLinkElement) {
this.nextPageLinkElement.innerHTML = this.settings.loadingText;
this.nextPageLinkElement.innerText = this.settings.loadingText;
this.nextPageUrl = this.nextPageLinkElement.href;

this.loadMore();
}
}
};

Ajaxinate.prototype.loadMore = function getTheHtmlOfTheNextPageWithAnAjaxRequest() {
Ajaxinate.prototype.loadMore = function loadMore() {
this.request = new XMLHttpRequest();

this.request.onreadystatechange = function success() {
if (this.request.readyState === 4 && this.request.status === 200) {
var newContainer = this.request.responseXML.querySelectorAll(this.settings.container)[0];
var newPagination = this.request.responseXML.querySelectorAll(this.settings.pagination)[0];
this.containerElement.insertAdjacentHTML('beforeend', newContainer.innerHTML);
if (!this.request.responseXML) {
return;
}
if (!this.request.readyState === 4 || !this.request.status === 200) {
return;
}

var newContainer = this.request.responseXML.querySelectorAll(this.settings.container)[0];
var newPagination = this.request.responseXML.querySelectorAll(this.settings.pagination)[0];

this.containerElement.insertAdjacentHTML('beforeend', newContainer.innerHTML);

if (typeof newPagination === 'undefined') {
this.removePaginationElement();
} else {
this.paginationElement.innerHTML = newPagination.innerHTML;

if (this.settings.callback && typeof this.settings.callback === 'function') {
this.settings.callback(this.request.responseXML);
}

this.initialize();
}
}.bind(this);

this.request.open('GET', this.nextPageUrl);
this.request.responseType = 'document';
this.request.send();
};

Ajaxinate.prototype.removeClickListener = function removeClickEventListener() {
this.nextPageLinkElement.addEventListener('click', this.stopMultipleClicks);
Ajaxinate.prototype.removeClickListener = function removeClickListener() {
this.nextPageLinkElement.removeEventListener('click', this.preventMultipleClicks);
};

Ajaxinate.prototype.removePaginationElement = function removePaginationElement() {
this.paginationElement.innerHTML = '';
this.destroy();
};

Ajaxinate.prototype.removeScrollListener = function removeScrollEventListener() {
Ajaxinate.prototype.removeScrollListener = function removeScrollListener() {
document.removeEventListener('scroll', this.checkIfPaginationInView);
window.removeEventListener('resize', this.checkIfPaginationInView);
window.removeEventListener('orientationchange', this.checkIfPaginationInView);
};

Ajaxinate.prototype.destroy = function removeEventListenersAndReturnThis() {
// This method is used to unbind event listeners from the DOM
// This function is called manually to destroy "this" Ajaxinate instance
Ajaxinate.prototype.destroy = function destroy() {
var destroyers = {
click: this.removeClickListener,
scroll: this.removeScrollListener
};

destroyers[this.settings.method]();

return this;
};
};

exports.default = Ajaxinate;
Loading