You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
3.6 KiB
79 lines
3.6 KiB
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
|
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
|
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
|
|
import { MutationObs, isElement } from './dom';
|
|
import { warnNoMutationObserverSupport } from './warn';
|
|
/**
|
|
* Observe a DOM element changes, falls back to eventListener mode
|
|
* @param {Element} el The DOM element to observe
|
|
* @param {Function} callback callback to be called on change
|
|
* @param {object} [options={childList: true, subtree: true}] observe options
|
|
* @see https://stackoverflow.com/questions/3219758
|
|
*/
|
|
|
|
var observeDom = function observeDom(el, callback, options)
|
|
/* istanbul ignore next: difficult to test in JSDOM */
|
|
{
|
|
// Handle cases where we might be passed a Vue instance
|
|
el = el ? el.$el || el : null; // Early exit when we have no element
|
|
|
|
/* istanbul ignore next: difficult to test in JSDOM */
|
|
|
|
if (!isElement(el)) {
|
|
return null;
|
|
} // Exit and throw a warning when `MutationObserver` isn't available
|
|
|
|
|
|
if (warnNoMutationObserverSupport('observeDom')) {
|
|
return null;
|
|
} // Define a new observer
|
|
|
|
|
|
var obs = new MutationObs(function (mutations) {
|
|
var changed = false; // A mutation can contain several change records, so we loop
|
|
// through them to see what has changed
|
|
// We break out of the loop early if any "significant" change
|
|
// has been detected
|
|
|
|
for (var i = 0; i < mutations.length && !changed; i++) {
|
|
// The mutation record
|
|
var mutation = mutations[i]; // Mutation type
|
|
|
|
var type = mutation.type; // DOM node (could be any DOM node type - HTMLElement, Text, comment, etc.)
|
|
|
|
var target = mutation.target; // Detect whether a change happened based on type and target
|
|
|
|
if (type === 'characterData' && target.nodeType === Node.TEXT_NODE) {
|
|
// We ignore nodes that are not TEXT (i.e. comments, etc.)
|
|
// as they don't change layout
|
|
changed = true;
|
|
} else if (type === 'attributes') {
|
|
changed = true;
|
|
} else if (type === 'childList' && (mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0)) {
|
|
// This includes HTMLElement and text nodes being
|
|
// added/removed/re-arranged
|
|
changed = true;
|
|
}
|
|
} // We only call the callback if a change that could affect
|
|
// layout/size truly happened
|
|
|
|
|
|
if (changed) {
|
|
callback();
|
|
}
|
|
}); // Have the observer observe foo for changes in children, etc
|
|
|
|
obs.observe(el, _objectSpread({
|
|
childList: true,
|
|
subtree: true
|
|
}, options)); // We return a reference to the observer so that `obs.disconnect()`
|
|
// can be called if necessary
|
|
// To reduce overhead when the root element is hidden
|
|
|
|
return obs;
|
|
};
|
|
|
|
export default observeDom;
|