import { TypeVoidFunction } from '../types/voidFunction';
import { EventEmitter } from '../event';
import { IMutationObserverExtOption } from './types';
/**
* @typedef {String} MutationRecordType
* @alias MutationRecordType
* @memberof MutationObserver
* @see https://developer.mozilla.org/ko/docs/Web/API/MutationObserver#MutationObserverInit
* @property {String} childList
* @property {String} attributes
* @property {String} characterData
* @property {String} subtree
*/
/**
* @export
* @readonly
* @enum {MutationRecordType}
*/
export declare enum MutationRecordType {
childList = "childList",
attributes = "attributes",
characterData = "characterData",
subtree = "subtree"
}
/**
* MutationEvent Types
* @event MutationObserver#MUTATION_EVENTS
* @memberof MutationObserver
* @property {String} CHANGE_CHILD_LIST - childList 변경 시점
* @property {String} CHANGE_SUBTREE - subtree 변경 시점
* @property {String} CHANGE_ATTRIBUTES - attributes 변경 시점
* @property {String} CHANGE_CHARACTER_DATA - data 변경 시점
* @property {String} CHANGE - 기타 변경 시점
* @property {String} WILD_CARD - 모든 변경 시점
*/
/**
* @export
* @readonly
* @enum {MUTATION_EVENTS}
*/
export declare enum MUTATION_EVENTS {
CHANGE_CHILD_LIST = "MUTATION_OBERSERVER-EVENTS-CHANGE_CHILD_LIST",
CHANGE_SUBTREE = "MUTATION_OBERSERVER-EVENTS-CHANGE_SUBTREE",
CHANGE_ATTRIBUTES = "MUTATION_OBERSERVER-EVENTS-CHANGE_ATTRIBUTES",
CHANGE_CHARACTER_DATA = "MUTATION_OBERSERVER-EVENTS-CHANGE_CHARACTER_DATA",
CHANGE = "MUTATION_OBERSERVER-EVENTS-CHANGE",
WILD_CARD = "MUTATION_OBERSERVER-EVENTS-CHANGE_WILCD_CARD"
}
/**
* target 으로 설정된 element 의 변화를 감지
* @export
* @class MutationObserver
* @alias observer/MutationObserver
* @extends EventEmitter
* @see https://developer.mozilla.org/ko/docs/Web/API/MutationObserver
* @see https://github.com/webmodules/mutation-observer
* @see https://developer.mozilla.org/ko/docs/Web/API/MutationObserver#MutationObserverInit
* @param {Partial} option
* @param {HTMLElement} [option.target=null] target
* @param {Number} [option.debounce=300] debounce
* @param {Function|null} [option.callback={@link noop}] callback function
* @param {Object} [option.options={}]
* @param {Boolean} [option.options.childList]
* @param {Boolean} [option.options.attributes]
* @param {Boolean} [option.options.characterData]
* @param {Boolean} [option.options.subtree]
* @param {Boolean} [option.options.attributeOldValue]
* @param {Boolean} [option.options.characterDataOldValue]
* @param {Array.} [option.options.attributeFilter] [MDN attributeFilter]{@link https://developer.mozilla.org/en-US/docs/Web/API/MutationObserverInit/attributeFilter}
* @example
import { MutationObserver, MUTATION_EVENTS } from '@nonoll/code-snippet/observer';
const createElement = ({ tag = 'div', id = '', style = '', value = '', text = '' }) => {
const doc = window.document;
const target = doc.createElement(tag);
target.setAttribute('id', id);
target.setAttribute('style', style);
target.setAttribute('value', value);
if (text) {
target.textContent = text;
}
return target;
}
let observer;
const forExample = () => {
if (observer) {
console.log('already example');
return;
}
const doc = window.document;
const target = createElement({ id: 'example_target', style: 'border: 1px solid red' });
doc.body.appendChild(target);
const options = {
childList: true,
subtree: true
};
observer = new MutationObserver({ target, options });
observer.on(MUTATION_EVENTS.WILD_CARD, (type, values) => {
console.log('wildCard', type, values);
}).on(MUTATION_EVENTS.CHANGE_CHILD_LIST, values => {
console.log('childList', values);
});
observer.attach();
const attachButton = createElement({ tag: 'button', text: 'observer attach' });
const detachButton = createElement({ tag: 'button', text: 'observer detach' });
const appendButton = createElement({ tag: 'button', text: 'append' });
doc.body.appendChild(attachButton);
doc.body.appendChild(detachButton);
doc.body.appendChild(appendButton);
attachButton.addEventListener('click', e => {
e.preventDefault();
console.log('attachButton clicked');
if (!observer) {
return;
}
observer.attach();
});
detachButton.addEventListener('click', e => {
e.preventDefault();
console.log('detachButton clicked');
if (!observer) {
return;
}
observer.detach();
});
appendButton.addEventListener('click', e => {
e.preventDefault();
console.log('appendButton clicked');
if (!observer) {
return;
}
const input = createElement({ tag: 'input', value: `${+new Date()}` });
target.appendChild(input);
});
}
forExample();
*/
export declare class MutationObserver extends EventEmitter {
private target;
private callback;
private observer;
private options;
constructor({ target, debounce, callback, options }: Partial);
/**
* 이벤트 감지 설정
* @returns {MutationObserver}
* @memberof MutationObserver
* @example
observer.attach();
*/
attach(): MutationObserver;
/**
* 이벤트 감지 등록
* @override
* @param {string} eventName
* @param {TypeVoidFunction} [listener={@link noop}]
* @param {*} [context]
* @memberof MutationObserver
* @listens MutationObserver#MUTATION_EVENTS
* @returns {MutationObserver}
*/
on(eventName: string, /* istanbul ignore next: for noop */ listener?: TypeVoidFunction, context?: any): MutationObserver;
/**
* 변화 감지 이벤트 리스너
* @private
* @param {MutationRecord[]} mutations [MDN MutationRecord]{@link https://developer.mozilla.org/en-US/docs/Web/API/MutationRecord}
* @memberof MutationObserver
* @fires MutationObserver#MUTATION_EVENTS
*/
private onMutationObserverListener;
/**
* 이벤트 감지 해제
* @returns {MutationObserver}
* @memberof MutationObserver
* @example
observer.detach();
*/
detach(): MutationObserver;
/**
* destory
* @returns {MutationObserver}
* @memberof MutationObserver
* @example
observer.destroy();
*/
destroy(): MutationObserver;
}