dom-selector-observer
v0.1.2
Published
Observation the attach, move and detach of elements in DOM
Downloads
7
Readme
dom-selector-observer
Observation the attach, move and detach of elements in DOM.
Install
npm install dom-selector-observer
Usage
import DomSelectorObserver from 'dom-selector-observer';
class MyComponent {
constructor(element) {
this.element = element;
// your code, for example, add event listeners
}
refresh() {
// your code, for example, update sizes
}
destroy() {
// your code, for example, remove event listeners
}
}
DomSelectorObserver.addAttachHandler('.my-component', function(element) {
element.myComponent = new MyComponent(element);
});
DomSelectorObserver.addMoveHandler('.my-component', function(element) {
element.myComponent.refresh();
});
DomSelectorObserver.addAttachHandler('.my-component', function(element) {
element.myComponent.destroy();
});
Documentation
Import types
There are several entry points for importing a library:
import DomSelectorObserver from 'dom-selector-observer'
- similarlywith-shims
;import DomSelectorObserver from 'dom-selector-observer/standard'
- easy import without polyfills for ie11;import DomSelectorObserver from 'dom-selector-observer/with-shims'
- import with shims for ie11;import DomSelectorObserver from 'dom-selector-observer/with-polyfills'
- import with polyfill for ie11;
Differences shims from polyfills you can read in polyshim package.
When directly include the script from the dist
folder to the browser, you can get an DomSelectorObserver instance via window.DomSelectorObserver.default
.
Methods
addAttachHandler(selector, handler)
- add handler when element match selector attached to DOMselector {String}
- CSS-selectorhandler {function(node)}
- handler functionnode {Element}
- attached element
removeAttachHandler(selector[, handler])
- remove handler added withaddAttachHandler()
selector {String}
- CSS-selectorhandler {function(node)}
- handler function
addMoveHandler(selector, handler)
- add handler when element match selector moved in DOMselector {String}
- CSS-selectorhandler {function(node)}
- handler functionnode {Element}
- moved element
removeMoveHandler(selector[, handler])
- remove handler added withaddMoveHandler()
selector {String}
- CSS-selectorhandler {function(node)}
- handler function
addDetachHandler(selector, handler)
- add handler when element match selector detached from DOMselector {String}
- CSS-selectorhandler {function(node)}
- handler functionnode {Element}
- detached element
removeDetachHandler(selector[, handler])
- remove handler added withaddDetachHandler()
selector {String}
- CSS-selectorhandler {function(node)}
- handler function
observe([processExisting])
- start observing DOMprocessExisting {boolean}
- process attach handlers for existing elements in DOM
unobserve()
- stop observing DOMsetShim([setMatches])
- sets shims for non-cross-browser methodssetMatches {Function|null}
- shim forElement.prototype.matches
Testing
To run tests, use:
npm test
If necessary, you can install launchers for other browsers and activate them in karma.conf.js
:
npm i --save-dev karma-ie-launcher
Browsers support
- Internet Explorer 11+
- Other modern browsers