Skip to content

Commit

Permalink
fix: disable mutation observing when not supported (michalsnik#405)
Browse files Browse the repository at this point in the history
Disable mutation observing and display a message when MutationObserver is not supported by the browser.

Closes michalsnik#404
  • Loading branch information
ncoden authored and michalsnik committed Oct 3, 2018
1 parent 2188804 commit ea374ae
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
16 changes: 14 additions & 2 deletions src/js/aos.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import styles from './../sass/aos.scss';
import throttle from 'lodash.throttle';
import debounce from 'lodash.debounce';

import observe from './libs/observer';
import observer from './libs/observer';

import detect from './helpers/detector';
import handleScroll from './helpers/handleScroll';
Expand Down Expand Up @@ -139,13 +139,25 @@ const init = function init(settings) {
// Create initial array with elements -> to be fullfilled later with prepare()
$aosElements = elements();

/**
* Disable mutation observing if not supported
*/
if (!options.disableMutationObserver && !observer.isSupported()) {
console.info(`
aos: MutationObserver is not supported on this browser,
code mutations observing has been disabled.
You may have to call "refreshHard()" by yourself.
`);
options.disableMutationObserver = true;
}

/**
* Observe [aos] elements
* If something is loaded by AJAX
* it'll refresh plugin automatically
*/
if (!options.disableMutationObserver) {
observe('[data-aos]', refreshHard);
observer.ready('[data-aos]', refreshHard);
}

/**
Expand Down
45 changes: 27 additions & 18 deletions src/js/libs/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,6 @@ function containsAOSNode(nodes) {
return false;
}

function ready(selector, fn) {
const doc = window.document;
const MutationObserver =
window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;

const observer = new MutationObserver(check);
callback = fn;

observer.observe(doc.documentElement, {
childList: true,
subtree: true,
removedNodes: true
});
}

function check(mutations) {
if (!mutations) return;

Expand All @@ -51,4 +34,30 @@ function check(mutations) {
});
}

export default ready;
function getMutationObserver() {
return (
window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver
);
}

function isSupported() {
return !!getMutationObserver();
}

function ready(selector, fn) {
const doc = window.document;
const MutationObserver = getMutationObserver();

const observer = new MutationObserver(check);
callback = fn;

observer.observe(doc.documentElement, {
childList: true,
subtree: true,
removedNodes: true
});
}

export default { isSupported, ready };

0 comments on commit ea374ae

Please sign in to comment.