项目作者: keleko34

项目描述 :
Heavily extends the ability of event listeners to allow for listening to any and all property changes, dom changes, style changes, and method calls
高级语言: JavaScript
项目地址: git://github.com/keleko34/pikantny.git
创建时间: 2017-10-26T11:13:53Z
项目社区:https://github.com/keleko34/pikantny

开源协议:MIT License

下载


Pikantny

Extending DOM event listening to include attributes, properties, and styles

NPM version Gitter

English - Español - Polski - Pусский - 中文

Table of contents

What is it?

This library allows you to use event listeners in such a manner as standard events to listen to changes from anything that happens on the DOM, from listening to when html changes to when a style gets set or even when a value on an input changes.

Installation

This libray can be installed using:

  • NPM : npm install pikantny --save
  • Bower : bower install pikantny --save
  • Yarn : yarn add pikantny

Getting started

The script can be loaded both in the head and in the body.
All functionality is automatically loaded as soon as the file is loaded.
Note: include this script before any other scripts for proper implementation

  1. <script src="/(node_modules|bower_modules)/pikantny/pikantny.min.js"></script>

To start using it is as simple as just using your standard listener method

Native

  1. var node = document.querySelector('selector')
  2. node.addEventListener('innerHTML', console.log);

jQuery

  1. $('selector').on('innerHTML', console.log);

when listening for propery events there are two different types of listeners, the pre DOM update listener and the post DOM update listener. By simply adding update to the end of any listener your event will fire post DOM update

  1. node.addEventListener('innerHTMLupdate', console.log);

Attributes

Attribute event listeners can be added to detect any changes in any attributes

  1. node.addEventListener('id', console.log);
  2. node.setAttribute('id','your-id');
  3. // or
  4. node.id = 'your-id';

Properties

Properties of an element also allow listening for any changes

  1. node.addEventListener('textContent', console.log);
  2. node.textContent = 'new-text';

Functions

Any element methods allow listening for their execution

  1. node.addEventListener('appendChild', console.log);
  2. node.appendChild(input);

Styles

Styles associated with the styles object or styles attribute also allow listening for any changes, each respective listener will fire if multiple are set in the style attribute

  1. node.addEventListener('color', console.log);
  2. node.style.color = '#000';
  3. // or
  4. node.setAttribute('style','color:#000;');

Inputs

Input value changes also allow listening for any changes and are IME compatible

  1. input.addEventListener('value', console.log);

Event object

The event object that is passed to each of these fired events allow for similar functionality as that of a standard DOM event listener

event.preventDefault()

When called from a pre DOM update event, this method can be used to prevent the DOM from updating

  1. // innerHTML, textContent, appendChild, etc
  2. node.addEventListener('html', function(e){ e.preventDefault(); });
  3. // input
  4. input.addEventListener('value', function(e){ e.preventDefault(); });

event.stop()

When called from a pre DOM update event, this method can be used to stop the post DOM update events from firing

  1. node.addEventListener('innerHTML', function(e){ e.stop(); });
  2. // this will not fire
  3. node.addEventListener('innerHTMLupdate', console.log);

event.stopPropogation()

When called no bubbled listeners after the current one will fire, including post DOM update

event.stopImmediatePropogation()

When called no listeners after the current one will fire, including post DOM update

event.action

This property shows the returning value of a executed function when looked at in a post DOM update event

event.value

Shows the value that is being set

event.oldValue

Shows the previous value of the item being set

All other event properties follow the same guideline as a standard Event object

Examples

HTML changes

Don’t allow an element to have any html changes

  1. var node = document.querySelector('selector');
  2. node.addEventListener('html',function(e){e.preventDefault();});

Validation

Validate inputs to see if a given value is allowed.
You can use return false; or event.preventDefault(); to stop the input from updating

  1. var input = document.querySelector('selector');
  2. input.addEventListener('value',function(e){ return /^[0-9A-Za-z]+$/.test(e.value); });

Debugging

This library supports dev console events panel, all events added will show up in this panel.

How to contribute

If You would like to contribute here are the steps

  1. Clone Repo: Pikantny Github Repo
  2. Install any necessary dev dependencies
  3. build the project npm run build
  4. test your changes don’t break anything npm test
  5. Make a pull request on github for your changes :)

License

You can view the license here: License