als-layout 2.5.0 → 2.5.1

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/readme.md +0 -236
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "als-layout",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "description": "Html layout constructor",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -15,7 +15,7 @@
15
15
  "license": "ISC",
16
16
  "dependencies": {
17
17
  "als-css-parser": "^0.5.0",
18
- "als-document": "^1.4.1",
18
+ "als-document": "^1.4.0",
19
19
  "als-simple-css": "^9.1.0"
20
20
  }
21
21
  }
package/readme.md DELETED
@@ -1,236 +0,0 @@
1
- # als-layout Documentation
2
-
3
- ## Library Description
4
-
5
- ### What is it?
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
-
8
- ### What can you do with it and where can it be used?
9
- The `als-layout` library is versatile, suitable for:
10
- - Building dynamic web pages that require frequent updates to their metadata, styles, or scripts.
11
- - Creating templating systems where multiple page layouts share similar structures but differ in content or styling.
12
- - Developing web applications that need to dynamically adjust their UI based on user interactions or data changes.
13
- This library is particularly useful in environments where rapid development and modular design are prioritized.
14
-
15
- ## Installation and Adding
16
- To use the `als-layout` library in your project, you can install it via npm and then include it in your JavaScript files:
17
-
18
- ```bash
19
- npm i als-layout
20
- ```
21
-
22
- ```js
23
- const Layout = require('als-layout')
24
- ```
25
-
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
-
35
- * V2.2.0
36
- * fixed empty update function if no components for $App
37
- * if component function return string, it will element.innerHTML = string
38
- * Now each Layout extends Document (als-document)
39
- * layout.$ and layout.$$
40
- * Also $App.$ and $App.$$ on backend too
41
- * no langs validation any more
42
- * lang method instead
43
- * no layout.cached
44
- * charset meta tag allready exists by default
45
-
46
- * V2.1.0
47
- * updated als-document version
48
- * [part] attribute for static components
49
- * onload() method
50
- * bugs fixed
51
-
52
-
53
- ## Basic Usage
54
-
55
- ### Initialization
56
- To start using `als-layout`, you first need to create a new instance of `Layout`. This instance will serve as the foundation for building and modifying your web page.
57
-
58
- ```js
59
- const Layout = require('als-layout');
60
- const layout = new Layout();
61
- ```
62
-
63
- ### Adding Different Elements
64
- Once you have your `Layout` instance, you can easily add or modify various elements of your web page. Here are some examples of how you can use the library to customize your layout:
65
-
66
- ```js
67
- const layout = new Layout()
68
- .charset() // default UTF-8
69
- .viewport() // default width=device-width, initial-scale=1.0
70
- .title('Test title') // adding/updating title and meta[og:title]
71
- .favicon('/favicon.png') // adding/updating link[rel=icon][type=image/x-icon] with new href
72
- .keywords(['some', 'keyword']) // adding/updating meta[name=keywords]. not adding existing keywords
73
- .image('/main-image.png', '1.5') // adding/updating meta - og:image, twitter:image, twitter:card
74
- .description('Cool site') // adding/updating meta og:description, twitter:description, and description tag
75
- .version('1.0.0') // adds version parameter to link, script.src, and image
76
- .url('/some', 'http://site.com') // adding/updating meta[og:url] and link[rel="canonical"]
77
- .style([{body:{m:0, bgc:'whitesmoke'}}]) // adding as simple-css styles to existing/new style tag
78
- .style('body {margin:0; background-color:whitesmoke;}', true) // adding css styles to existing/new style tag. Second parameter is minified (default=false).
79
- .link('/styles.css', '2.0') // adding link[rel=stylesheet] if such href not exists
80
- .script({src:'/app.js'}, '', true, '3.0') // set script with src to head if such src not exists
81
- .script({}, 'console.log("hello world")', false) // set script with script code to footer
82
-
83
- // Accessors for document parts
84
- layout.body // getter for body element (if not exists, created)
85
- layout.head // getter for head element (if not exists, created)
86
- layout.html // getter for html Element (if not exists, created)
87
-
88
- // Outputs
89
- layout.rawHtml // raw HTML of the document
90
- layout.clone // creates a new layout object clone for current object
91
- ```
92
-
93
- ### onload
94
-
95
- By adding onload attribute, you can run scripts for each element after dom content has loaded.
96
-
97
- Example how it works:
98
- ```js
99
- const layout = new Layout().charset().viewport().title('On load').onload()
100
- layout.body.innerHTML = /*html*/`<div onload="this.innerHTML = 'new content'">original content</div>`
101
- ```
102
- ## Cloning Functionality
103
-
104
- ### What is Cloning and Why is it Necessary?
105
- Cloning in the `als-layout` library refers to creating a complete, independent copy of the existing `Layout` instance. This functionality is crucial when you need to generate multiple pages or versions of a page from a single base layout without affecting the original setup.
106
-
107
- ### How to Use Cloning
108
- To clone a `Layout` instance, simply use the `clone` method. This method creates a new `Layout` instance with the same properties and settings as the original, allowing for independent modifications without interference.
109
-
110
- ```js
111
- const newLayout = layout.clone;
112
- ```
113
-
114
- ### Benefits of Cloning
115
- - **Efficiency:** Cloning is highly efficient, especially for creating pages with similar structures but different content or styles. It avoids the overhead of reinitializing and reconfiguring a new `Layout` instance from scratch.
116
- - **Speed:** Cloning is fast, typically taking less than 20ms even for large pages. This makes it ideal for high-performance web applications that need to dynamically generate content.
117
- - **Isolation:** Changes made to a cloned `Layout` do not affect the original, ensuring that each instance can be modified independently based on specific requirements.
118
-
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.
120
-
121
-
122
- ## Advanced Usage
123
-
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:
125
-
126
- ```js
127
- const Layout = require('als-layout')
128
-
129
- // Starting with a basic HTML template and specifying the host for URL methods
130
- const raw = /*html*/`<html></html>`
131
- const host = 'http://example.com';
132
- const layout = new Layout(raw, host).lang('fr')
133
- console.log(layout.rawHtml)
134
- // <!DOCTYPE html><html lang="fr"><head></p></head><body></body></html>
135
-
136
- // Cloning the initial layout to create a specialized page
137
- const homePage = layout.clone
138
- homeAutoReload = layout.clone
139
- homePage.title('Home page')
140
- homePage.body.innerHTML = /*html*/`<h1>Home page</h1>`
141
- console.log(homePage.rawHtml)
142
- // <!DOCTYPE html><html lang="fr"><head><title>Home page</title><meta property="og:title" content="Home page"></head><body><h1>Home page</h1></body></html>
143
-
144
- // Adding script that reloads the page every minute
145
- homeAutoReload.script({}, 'setTimeout(function() { window.location.reload(); }, 60000);', false)
146
- console.log(homeAutoReload.rawHtml)
147
- // <!DOCTYPE html><html lang="fr"><head><title>Automatic Reload Page</title><meta property="og:title" content="Automatic Reload Page"></head><body><script>setTimeout(function() { window.location.reload(); }, 60000);</script></body></html>
148
-
149
- // Demonstrating dynamic stylesheet linkage with versioning
150
- const styleVersion = '1.1';
151
- homePage.link('/css/main.css', styleVersion)
152
- console.log(homePage.rawHtml)
153
- // Includes link to the stylesheet with version parameter to ensure fresh cache
154
- ```
155
-
156
- In this example:
157
- - We start with a basic HTML template and use the `lang` method to set the language.
158
- - We use the `clone` method to create two versions of the base layout: one for the home page and another that automatically reloads every minute.
159
- - We manipulate the `body` of the `homePage` to include custom HTML.
160
- - We add a script to `homeAutoReload` that sets up an automatic page reload, showcasing how to insert JavaScript dynamically.
161
- - We dynamically add a versioned link to a stylesheet in the `homePage`, demonstrating control over caching and resource management.
162
-
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.
164
-
165
-
166
- ## Rendering
167
-
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.
169
-
170
- ### Structure
171
- The layout object houses several key properties that facilitate dynamic content management:
172
-
173
- - `layout.data`: Stores the data that can be used across the page, such as state variables or configuration settings.
174
- - `layout.components`: Holds functions that define the behavior and rendering logic for components identified by specific attributes in the HTML.
175
- - `layout.utils`: Contains utility functions that can be used throughout the page for common tasks.
176
- - `layout.actions`: Methods that can be triggered by user interaction, often modifying `layout.data` and updating the page accordingly.
177
-
178
- ### The Render Method
179
- The `render` method processes all elements with a `component` attribute, dynamically generating their content and behavior based on the defined components. After rendering, the page includes the `window.$App` JavaScript object, which provides methods and properties to interact with the page elements and data:
180
-
181
- ```js
182
- window.$App = {
183
- data, // Access to the layout data object
184
- components, // Access to components functions
185
- utils, // Access to utility functions
186
- actions, // Access to action functions
187
- $(selector, parent = document), // Equivalent to querySelector
188
- $$(selector, parent = document), // Equivalent to querySelectorAll
189
- update(element) { // Updates the element if it has a component attribute
190
- // Update logic here
191
- }
192
- }
193
- ```
194
-
195
- ### Counter Example
196
- To demonstrate dynamic interaction, consider a counter that can be increased or decreased through user input:
197
-
198
- ```js
199
- const fs = require('fs')
200
- const Layout = require('als-layout')
201
-
202
- // Create and configure the layout
203
- const layout = new Layout().title('Counter')
204
- layout.data.counter = 0 // Initialize counter data
205
-
206
- // Define a component for displaying the counter
207
- layout.components.counter = function(element, $App) {
208
- element.innerHTML = `${$App.data.counter}`
209
- }
210
-
211
- // Define actions for increasing and decreasing the counter
212
- layout.actions = {
213
- increase: () => { $App.data.counter++; $App.update($App.$('[component=counter]')); },
214
- decrease: () => { $We render the html page to measure and write to a document.app.data.counter--; $Page updates are shown in real-time on the rendered HTML.$App.update($App.$('[component=counter]')); }
215
- }
216
-
217
- // Add buttons and the counter display to the body
218
- layout.body.innerHTML = /*html*/`
219
- <button onclick="$App.actions.increase()">Increase</button>
220
- <span component="counter" publish></span>
221
- <button onclick="$App.actions.decrease()">Decrease</button>
222
- `
223
-
224
- // Measure render time and generate HTML
225
- const time1 = performance.now()
226
- const rawHtml = layout.render()
227
- const time2 = performance.now()
228
- console.log(`${time2 - time1}ms`) // e.g., 1.0649ms
229
-
230
- // Write the output to a file
231
- fs.writeFileSync('counter.html', rawHtml, 'utf-8')
232
- ```
233
-
234
- ### Advanced Rendering Details
235
- - **Component Indexing:** Each component is assigned a `componentIndex` during rendering, providing a unique index within its parent 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.