als-layout 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,9 @@
1
1
  ## Change Log
2
+ * V2.3.0
3
+ * Default, component will not be included in $App, if not [publish] attribute
4
+ * When cloning, components and other staff cloned too
5
+ * Reference does not change
6
+
2
7
  * V2.2.0
3
8
  * fixed empty update function if no components for $App
4
9
  * if component function return string, it will element.innerHTML = string
@@ -9,8 +14,10 @@
9
14
  * lang method instead
10
15
  * no layout.cached
11
16
  * charset meta tag allready exists by default
17
+
12
18
  * V2.1.0
13
19
  * updated als-document version
14
20
  * [part] attribute for static components
15
21
  * onload() method
16
- * bugs fixed
22
+ * bugs fixed
23
+
package/docs/4-render.md CHANGED
@@ -52,7 +52,7 @@ layout.actions = {
52
52
  // Add buttons and the counter display to the body
53
53
  layout.body.innerHTML = /*html*/`
54
54
  <button onclick="$App.actions.increase()">Increase</button>
55
- <span component="counter"></span>
55
+ <span component="counter" publish></span>
56
56
  <button onclick="$App.actions.decrease()">Decrease</button>
57
57
  `
58
58
 
@@ -68,4 +68,4 @@ fs.writeFileSync('counter.html', rawHtml, 'utf-8')
68
68
 
69
69
  ### Advanced Rendering Details
70
70
  - **Component Indexing:** Each component is assigned a `componentIndex` during rendering, providing a unique index within its parent component.
71
- - **Part Attribute:** Using the `part` attribute in a component prevents it from being added to `$App.components`, unless it is nested within another component.
71
+ - **Publish Attribute:** Using the `publish` attribute in a component make it being added to `$App.components`, unless it is nested within another component.
package/lib/layout.js CHANGED
@@ -8,15 +8,17 @@ const {
8
8
  } = require('./elements/index')
9
9
 
10
10
  class Layout extends Document {
11
- constructor(html,url) {
11
+ constructor(html,url,doc = {}) {
12
12
  super(html,url)
13
- this.components = {}
14
- this.data = {}
15
- this.actions = {}
16
- this.utils = {}
13
+ const {components = {},data = {},actions = {},utils = {}} = doc
14
+ this.components = components
15
+ this.data = data
16
+ this.actions = actions
17
+ this.utils = utils
17
18
  this.root = this.html
18
19
  this.update = update
19
20
  }
21
+
20
22
  onload() {this.script({},onload); return this}
21
23
  lang(lang) {this.html.setAttribute('lang',lang); return this}
22
24
  version(v) { this.v = v; return this; }
@@ -32,7 +34,7 @@ class Layout extends Document {
32
34
  link(href, v = this.v) { return addLink(href, v, this) }
33
35
  viewport(newViewport = 'width=device-width, initial-scale=1.0') { return viewport(newViewport, this) }
34
36
  get rawHtml() {return this.innerHTML}
35
- get clone() {return new Layout(new Document(this),this.URL)}
37
+ get clone() {return new Layout(new Document(this),this.URL,this)}
36
38
  render() {
37
39
  const done = []
38
40
  this.update(this.root, done)
@@ -1,5 +1,4 @@
1
1
  function componentHierarchy(elements) {
2
-
3
2
  const entries = elements
4
3
  .map(el => ([el, el.getAttribute('component'), el.ancestors]))
5
4
  .sort((a, b) => a[2].length - b[2].length)
@@ -7,19 +6,13 @@ function componentHierarchy(elements) {
7
6
  const result = []
8
7
  while(entries.length > 0) {
9
8
  const [element, componentName, ancestors] = entries.shift()
10
- if(element.getAttribute('part') !== null) {
9
+ const partsInside = element.$$('[component]')
10
+ if(element.getAttribute('publish') === null) {
11
11
  element.removeAttribute('component')
12
- element.removeAttribute('part')
12
+ element.removeAttribute('publish')
13
13
  continue
14
14
  }
15
- const partsInside = element.$$('[component][part]')
16
- partsInside.forEach(element => { element.removeAttribute('part') });
17
- entries.forEach(entry => {
18
- if(entry[1] !== componentName) return
19
- if(entry[2].includes(element)) {
20
- throw new Error(`The component '${componentName}' acts as both a parent and a child, which may complicate rendering.`);
21
- }
22
- });
15
+ partsInside.forEach(element => { element.setAttribute('publish','') });
23
16
  if(!result.includes(componentName)) result.push(componentName)
24
17
  }
25
18
  return result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "als-layout",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "description": "Html layout constructor",
5
5
  "main": "index.js",
6
6
  "directories": {
package/readme.md CHANGED
@@ -5,9 +5,6 @@
5
5
  ### What is it?
6
6
  `als-layout` is a JavaScript library designed to simplify and enhance the process of constructing and managing web page layouts. It provides a comprehensive API for modifying HTML documents dynamically, allowing developers to add, update, and manipulate various elements such as meta tags, styles, scripts, and more.
7
7
 
8
- ### Why is it needed?
9
- Managing HTML document structures and their contents can often become repetitive and error-prone when done manually. `als-modular` offers a structured and reusable approach, reducing development time and improving code maintainability.
10
-
11
8
  ### What can you do with it and where can it be used?
12
9
  The `als-layout` library is versatile, suitable for:
13
10
  - Building dynamic web pages that require frequent updates to their metadata, styles, or scripts.
@@ -25,8 +22,16 @@ npm i als-layout
25
22
  ```js
26
23
  const Layout = require('als-layout')
27
24
  ```
28
-
25
+
29
26
  ## Change Log
27
+ * V2.4.0
28
+ * child components published issue fixed
29
+ * removed throwing error for recursive components
30
+ * V2.3.0
31
+ * Default, component will not be included in $App, if not [publish] attribute
32
+ * When cloning, components and other staff cloned too
33
+ * Reference does not change
34
+
30
35
  * V2.2.0
31
36
  * fixed empty update function if no components for $App
32
37
  * if component function return string, it will element.innerHTML = string
@@ -37,11 +42,14 @@ const Layout = require('als-layout')
37
42
  * lang method instead
38
43
  * no layout.cached
39
44
  * charset meta tag allready exists by default
45
+
40
46
  * V2.1.0
41
47
  * updated als-document version
42
48
  * [part] attribute for static components
43
49
  * onload() method
44
- * bugs fixed
50
+ * bugs fixed
51
+
52
+
45
53
  ## Basic Usage
46
54
 
47
55
  ### Initialization
@@ -90,7 +98,7 @@ Example how it works:
90
98
  ```js
91
99
  const layout = new Layout().charset().viewport().title('On load').onload()
92
100
  layout.body.innerHTML = /*html*/`<div onload="this.innerHTML = 'new content'">original content</div>`
93
- ```
101
+ ```
94
102
  ## Cloning Functionality
95
103
 
96
104
  ### What is Cloning and Why is it Necessary?
@@ -110,7 +118,7 @@ const newLayout = layout.clone;
110
118
 
111
119
  Cloning is particularly useful in scenarios where templates or base layouts are used repeatedly with slight variations, providing a robust and scalable solution for web page generation.
112
120
 
113
-
121
+
114
122
  ## Advanced Usage
115
123
 
116
124
  The `als-layout` library allows for sophisticated manipulation of web page layouts, providing robust tools for creating dynamic and complex web pages. Below is an advanced example demonstrating various capabilities of the library:
@@ -154,7 +162,7 @@ In this example:
154
162
 
155
163
  This advanced example illustrates how `als-layout` can be used to handle complex scenarios and requirements in web development, enhancing the flexibility and power at your disposal.
156
164
 
157
-
165
+
158
166
  ## Rendering
159
167
 
160
168
  Each instance of `Layout` comes equipped with a `render` method that compiles the HTML structure and embeds a JavaScript object to manage the page dynamically. This object, known as `window.$App`, allows for real-time interaction and updates within the page.
@@ -209,7 +217,7 @@ layout.actions = {
209
217
  // Add buttons and the counter display to the body
210
218
  layout.body.innerHTML = /*html*/`
211
219
  <button onclick="$App.actions.increase()">Increase</button>
212
- <span component="counter"></span>
220
+ <span component="counter" publish></span>
213
221
  <button onclick="$App.actions.decrease()">Decrease</button>
214
222
  `
215
223
 
@@ -225,4 +233,4 @@ fs.writeFileSync('counter.html', rawHtml, 'utf-8')
225
233
 
226
234
  ### Advanced Rendering Details
227
235
  - **Component Indexing:** Each component is assigned a `componentIndex` during rendering, providing a unique index within its parent component.
228
- - **Part Attribute:** Using the `part` attribute in a component prevents it from being added to `$App.components`, unless it is nested within another component.
236
+ - **Publish Attribute:** Using the `publish` attribute in a component make it being added to `$App.components`, unless it is nested within another component.
@@ -17,13 +17,13 @@ describe('build-$App Functionality', () => {
17
17
  const called = []
18
18
  const done = [
19
19
  {
20
- getAttribute: (attr) => attr === 'component' ? 'comp1' : null,
20
+ getAttribute: (attr) => attr === 'component' ? 'comp1' : attr === 'publish' ? '' : null,
21
21
  removeAttribute(att) {called.push(att)},
22
22
  $$(){return []},
23
23
  ancestors:2
24
24
  },
25
25
  {
26
- getAttribute: (attr) => attr === 'component' ? 'comp2' : null,
26
+ getAttribute: (attr) => attr === 'component' ? 'comp2' : attr === 'publish' ? '' : null,
27
27
  removeAttribute(att) {called.push(att)},
28
28
  $$(){return []},
29
29
  ancestors:2
@@ -13,7 +13,7 @@ function shuffleArray(array) {
13
13
 
14
14
  describe('Basic tests', () => {
15
15
  it('should build correct hierarchy', () => {
16
- const root = parseHTML(/*html*/`<div component="some">
16
+ const root = parseHTML(/*html*/`<div component="some" publish>
17
17
  <div>
18
18
  <div component="some1"></div>
19
19
  <div>
@@ -46,6 +46,6 @@ describe('Basic tests', () => {
46
46
  </div>
47
47
  </div>`)
48
48
 
49
- assert.throws(() => componentHierarchy(shuffleArray(root.$$('[component]'))))
49
+ assert.doesNotThrow(() => componentHierarchy(shuffleArray(root.$$('[component]'))))
50
50
  })
51
51
  })
@@ -111,7 +111,7 @@ describe('Component Updating', () => {
111
111
  element.insert(2, 'test1 success')
112
112
  }
113
113
  layout.data.test = 'hello'
114
- layout.body.innerHTML = /*html*/`<div component="test1">
114
+ layout.body.innerHTML = /*html*/`<div component="test1" publish>
115
115
  <div component="test2">failed</div>
116
116
  </div>`
117
117
 
@@ -132,8 +132,8 @@ describe('Component Updating', () => {
132
132
 
133
133
  it('should add part to $App if it is component`s descendant', () => {
134
134
  layout.body.innerHTML = /*html*/`
135
- <div component="parent">
136
- <div component="child" part></div>
135
+ <div component="parent" publish>
136
+ <div component="child"></div>
137
137
  </div>`;
138
138
  layout.components.parent = (element, $App) => {};
139
139
  layout.components.child = (element, $App) => {};
@@ -151,8 +151,8 @@ describe('Component Updating', () => {
151
151
 
152
152
  it('should not add part to $App if it has no component ancestor', () => {
153
153
  layout.body.innerHTML = /*html*/`
154
- <div component="comp1"></div>
155
- <div component="comp2" part></div>
154
+ <div component="comp1" publish></div>
155
+ <div component="comp2"></div>
156
156
  `;
157
157
  layout.components.comp1 = (element, $App) => {};
158
158
  layout.components.comp2 = (element, $App) => {};