1.2.69 • Published 9 months ago

@cehlers88/ceutils v1.2.69

Weekly downloads
21
License
ISC
Repository
github
Last release
9 months ago

CEUTILS - 23.08.2024

Installation

npm install @cehlers88/ceutils

DOM-Utilities

After installation, the DOM-Library can be used via the following import:

import {domLib} from "@cehlers88/ceutils";

Element-Methods

By using the DOM-Library, a number of new element methods are automatically available:

appendChilds(nodes:Elements[]):Element

Adds a list of elements as child elements to the current element. Example:

const nodes = [
    document.createTextNode("Hello"),
    document.createTextNode("World")
];
document.body.appendChilds(nodes);

createChild(tagName:string, properties:{key:string:any}):Element

Creates a new element and adds it as a child element to the current element. Example:

// Creates a new div element with the ID "myDiv" and the CSS class "myClass" and adds it as a child element to the body
document.body.createChild("div", {id: "myDiv", class: "myClass"}); // (see Library-Functions/createElement for all possible properties)

getParentByCondition(condition:(element:Element)=>boolean):?Element

Returns the first parent element that meets the defined condition. If no parent element is found, "undefined" is returned. Example:

// Returns the first parent element that has the tag name "div"
const result = document.body.getParentByCondition(element => element.tagName.toUpperCase() === "DIV");

getParentWithClass(className:string):?Element

Returns the first parent element that has the specified CSS class. If no parent element is found, "undefined" is returned. Example:

// Returns the first parent element that has the CSS class "myClass"
const result = document.body.getParentWithClass("myClass");

removeAllChilds():Element

Removes all child elements of the current element. Example:

document.body.removeAllChilds();

Library-Functions

All functions of the DOM-Library are accessible through the "domLib" object.

appendChilds(parent:Element, nodes:Elements[]):Element

Adds a list of elements as child elements to the specified parent element. Example:

domLib.appendChilds(document.body, [domLib.createElement("div"), domLib.createElement("div")]);

createElement(tagName:string, properties:{key:string:any}):Element

Creates a new element with the specified properties and returns it. Example:

// Creates a new div element with the ID "myDiv" and the CSS class "myClass"
const element = domLib.createElement("div", {id: "myDiv", class: "myClass"});
// It can now be appended to a parent element using appendChild:
document.body.appendChild(element);

Example 2:

// Creates a new div element with the ID "myDiv", the CSS class "myClass", a child span with text and adds it as a child element to the body
document.body.appendChild(domLib.createElement("div", {
    id: "myDiv", 
    class: "myClass",
    childNodes: [
        domLib.createElement("span", {innerText: "Hello World"})
    ]
}));
// Add button with click event listener to the body
document.body.appendChild(domLib.createElement("button", {
    innerText: "Click me!",
    onClick: () => {
        console.log("Button clicked!");
    }
}));

// Or do it in one step:
document.body.appendChilds([ // ...s !
  domLib.createElement("div", {
    id: "myDiv",
    class: "myClass",
    childNodes: [
      domLib.createElement("span", {innerText: "Hello World"})
    ]
  }),
  domLib.createElement("button", {
    innerText: "Click me!",
    onClick: () => {
      console.log("Button clicked!");
    }
  })
]);

The following properties can be defined optionally:

  • "childNodes": an array of elements to be added as child elements.
  • "data": an object with data to be added as "data-*" attributes.
  • "innerText": the text content of the element.
  • "onBlur": an event listener for the "blur" event.
  • "onChange": an event listener for the "change" event.
  • "onClick": an event listener for the "click" event.
  • "onFocus": an event listener for the "focus" event.
  • "onKeyDown": an event listener for the "keydown" event.
  • "onKeyUp": an event listener for the "keyup" event.
  • "onSubmit": an event listener for the "submit" event.
  • "onLoad": an event listener for the "load" event.
  • "onMouseOver": an event listener for the "mouseover" event.
  • "onMouseDown": an event listener for the "mousedown" event.
  • "onMouseEnter": an event listener for the "mouseenter" event.
  • "onMouseLeave": an event listener for the "mouseleave" event.
  • "onMouseUp": an event listener for the "mouseup" event.
  • "onMouseMove": an event listener for the "mousemove" event.
  • "onMouseOut": an event listener for the "mouseout" event.
  • as well as all common element attributes.

if there is a property that is not listed here, it can be added as an attribute to the element:

const element = domLib.createElement("div", {id: "myDiv", class: "myClass"});
element.setAttribute("missing-attribute", "myValue");
// same as event listeners:
element.addEventListener("missing-event", () => {
    // do something cool
});

getElement(elementNeedle:string):any

Returns the element(s) that match(es) the specified selector. Example:

// Returns the element with the ID "myDiv"
const element_Example01 = domLib.getElement("myDiv"); // means: document.getElementById("myDiv")
const element_Example02 = domLib.getElement("#myDiv"); // means: document.getElementById("myDiv") too
const element_Example03 = domLib.getElement(".myDiv"); // means: document.getElementsByClassName("myDiv")
const element_Example04 = domLib.getElement("?.myDiv"); // means: document.querySelector(".myDiv")
const element_Example05 = domLib.getElement("?*.myDiv"); // means: document.querySelectorAll(".myDiv")

getParentWithClass(element:Element, className:string):?Element

Returns the first parent element that has the specified CSS class. If no parent element is found, "undefined" is returned. Example:

// Returns the first parent element that has the CSS class "myClass"
const result = domLib.getParentWithClass(document.body, "myClass");

removeAllChilds(element:Element):Element

Removes all child elements of the specified element. Example:

domLib.removeAllChilds(document.body);

Components

CachedDataProvider

The "CachedDataProvider" reduces the number of API calls by caching the data and retrieving it from the cache when needed. Each instance of a CachedDataProvider object has its own cache that can be manually cleared.

Usage

// Create a new instance of CachedDataProvider
import CachedDataProvider from "@cehlers88/ceutils/build/Provider/CachedDataProvider";
const dataProvider = new CachedDataProvider();

// Using the dataProvider to fetch data
dataProvider.fetch({
  url: "https://xyz.net",
  evalJson: true, // Optional
  method: "GET", // Optional
  data: { // Optional
  }
}).then(v=>{
    // handle response data
});

// Clear the cache
dataProvider.clearCache();

ComponentStarter

Register service

service.js

import ComponentBaseService from "./ComponentBaseService";
export default class MyService extends ComponentBaseService {
...
    getName() {
        return "MyService";
    }
...
}

app.js

import ComponentStarter from "./src/service/ComponentStarter";
import MyService from "./service"

const componentStarter = new ComponentStarter();
componentStarter.registerComponent(MyService);

Start React-Component

import ComponentStarter from "./src/service/ComponentStarter";
const test = new ComponentStarter();
test.registerComponent()
...
    <script >
    
    </script>
...

Eventhandler

- addListener
- dispatch
- setErrorHandler
- on

Interfaces

  • ICachedDataProviderFetchOptions

    • url:string
    • evalJson:?boolean
    • method:?string
    • data:?{key:string:any}
  • IDataEntry

    • key:string
    • value:any
1.2.69

9 months ago

1.2.68

10 months ago

1.2.67

11 months ago

1.2.60

1 year ago

1.2.63

12 months ago

1.2.64

11 months ago

1.2.61

1 year ago

1.2.62

12 months ago

1.2.65

11 months ago

1.2.66

11 months ago

1.2.59

2 years ago

1.2.58

2 years ago

1.2.41

2 years ago

1.2.42

2 years ago

1.2.40

2 years ago

1.2.45

2 years ago

1.2.46

2 years ago

1.2.43

2 years ago

1.2.44

2 years ago

1.2.49

2 years ago

1.2.47

2 years ago

1.2.48

2 years ago

1.2.52

2 years ago

1.2.53

2 years ago

1.2.50

2 years ago

1.2.51

2 years ago

1.2.56

2 years ago

1.2.57

2 years ago

1.2.54

2 years ago

1.2.55

2 years ago

1.2.38

2 years ago

1.2.39

2 years ago

1.2.34

3 years ago

1.2.35

3 years ago

1.2.36

3 years ago

1.2.37

3 years ago

1.2.27

3 years ago

1.2.28

3 years ago

1.2.25

3 years ago

1.2.26

3 years ago

1.2.29

3 years ago

1.2.30

3 years ago

1.2.31

3 years ago

1.2.32

3 years ago

1.2.33

3 years ago

1.2.23

3 years ago

1.2.24

3 years ago

1.2.21

3 years ago

1.2.22

3 years ago

1.2.18

3 years ago

1.2.19

3 years ago

1.2.20

3 years ago

1.2.16

4 years ago

1.2.17

4 years ago

1.2.9

4 years ago

1.2.12

4 years ago

1.2.13

4 years ago

1.2.10

4 years ago

1.2.11

4 years ago

1.2.14

4 years ago

1.2.15

4 years ago

1.2.8

4 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.4-2

5 years ago

1.2.4-1

5 years ago

1.2.4-0

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago