Generates relations between DOM elements depending on their depth

栏目: IT技术 · 发布时间: 4年前

内容简介:It figures out relations between DOM elements without storing them or creating actual dom tree. Instead, it gets relationship based on element depth. In case you are studying any dom-tree, you can build entire branches and navigate through them using gener

@dflex/dom-gen

Generates relations between DOM elements depending on their depth.
npm install @dflex/dom-gen

It figures out relations between DOM elements without storing them or creating actual dom tree. Instead, it gets relationship based on element depth. In case you are studying any dom-tree, you can build entire branches and navigate through them using generated unique keys.

const domGen = new Generator()

domGen.getElmPointer(id: string, depth: number)

Returns pointer object refers to element relation with its unique keys and related index:

  • order: Object <elementOrder>

    self: number
    parent: number
    
  • keys: Object <relationKey>

    sK: string
    pK: string
    chK: string
    
import Generator from "@dflex/dom-gen";

const domGen = new Generator();

let pointer = domGen.getElmPointer("id-0", 0);

// pointer = {
//   keys: {
//     chK: null,
//     pK: "1-0",
//     sK: "0-0",
//   },
//   order: {
//     parent: 0,
//     self: 0,
//   },
// };

Internally Generator has build dom tree as following:

DOM-root
│
│───id-0 => order: { parent: 0, self: 0 } || keys: { chK: null , pK: "1-0", sK: "0-0" }

Adding more elements on the same level:

const domGen = new Generator();

const pointer1 = domGen.getElmPointer("id-1", 0);

// pointer1 = {
//   keys: {
//     chK: null,
//     pK: "1-0",
//     sK: "0-0",
//   },
//   order: {
//     parent: 0,
//     self: 1,
//   },
// };

const pointer2 = domGen.getElmPointer("id-2", 0);

// pointer2 = {
//   keys: {
//     chK: null,
//     pK: "1-0",
//     sK: "0-0",
//   },
//   order: {
//     parent: 0,
//     self: 2,
//   },
// };

And dom tree is:

DOM-root
│
│───id-0 => order: {parent: 0, self: 0} || keys: {chK: null, pK: "1-0", sK: "0-0"}
│
│───id-1 => order: {parent: 0, self: 1} || keys: {chK: null, pK: "1-0", sK: "0-0"}
│
│───id-2 => order: {parent: 0, self: 2} || keys: {chK: null, pK: "1-0", sK: "0-0"}

Note:id/0-2 all have same parent and siblings key. So any incoming parent will carry key 1-0 and exists in position 0 . And this goes also for any parent which means for any element you can go up↑ and down↓ .

Following the same logic we can go deeper:

const domGen = new Generator();

const pointer = domGen.getElmPointer("id-parent-1", 1);

// pointer = {
//   keys: {
//     chK: "0-0",
//     pK: "2-0",
//     sK: "1-0", // this key was generated previously in children level
//   },
//   order: {
//     parent: 0,
//     self: 0,
//   },
// };

And dom tree is with relational key is as following:

DOM-root
├───id-parent-1  => order: {parent: 0, self: 0} || keys: {chK: "0-0", pK: "2-0", sK: "1-0"}
    |
    │───id-0  => order: {parent: 0, self: 0} || keys: {chK: null, pK: "1-0", sK: "0-0"}
    │
    │───id-1 => ..
    │
    │───id-2 => ..

dom-gen doesn't store keys and ids. All the id's validations should be done outside Generator logic. Also, storing relationships should be done separately in a store.

Test

npm test

License

This project is licensed under the GPL-3.0 License


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C算法(第二卷:图算法)(第3版)

C算法(第二卷:图算法)(第3版)

塞德威克(Sedgewick Robert) / 周良忠 / 第1版 (2004年1月1日) / 2004-4 / 38.0

《C算法(第2卷)(图算法)(第3版)(中文版)》所讨论的图算法,都是实际中解决图问题的最重要的已知方法。《C算法(第2卷)(图算法)(第3版)(中文版)》的主要宗旨是让越来越多需要了解这些算法的人的能够掌握这些方法及基本原理。书中根据基本原理从基本住处开始循序渐进地讲解,然后再介绍一些经典方法,最后介绍仍在进行研究和发展的现代技术。精心挑选的实例、详尽的图示以及完整的实现代码与正文中的算法和应用......一起来看看 《C算法(第二卷:图算法)(第3版)》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具