als-layout 5.0.1 → 5.2.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.
package/docs/#.md ADDED
@@ -0,0 +1,19 @@
1
+ # als-layout Documentation
2
+
3
+ `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.
4
+
5
+ The `als-layout` library is versatile, suitable for:
6
+ - Building dynamic web pages that require frequent updates to their metadata, styles, or scripts.
7
+ - Creating templating systems where multiple page layouts share similar structures but differ in content or styling.
8
+ - Developing web applications that need to dynamically adjust their UI based on user interactions or data changes.
9
+
10
+ ## Installation and Adding
11
+ To use the `als-layout` library in your project, you can install it via npm and then include it in your JavaScript files:
12
+
13
+ ```bash
14
+ npm i als-layout
15
+ ```
16
+
17
+ ```js
18
+ const Layout = require('als-layout')
19
+ ```
@@ -0,0 +1,37 @@
1
+ ## Change Log
2
+ * V5.2.0
3
+ * options object on clone, cloned too (remove the reference and deep cloning the object)
4
+ * V5.1.0
5
+ * status and end methods removed
6
+ * status and end methods added to als-view
7
+ * V5.0.0
8
+ * constructor changed
9
+ * options object parameter instead separated parameters
10
+ * options.logger
11
+ * bugs fixed
12
+ * some refactoring
13
+ * minifiying error catching
14
+ * status and end methods added
15
+ * V4.2.0
16
+ * link method changed
17
+ * `link(href, attributes = { rel: "stylesheet", type: "text/css" })`
18
+ * no version method
19
+ * no version parameter in image, link,script
20
+ * script
21
+ * script in footer added one after other in right order
22
+
23
+ * V4.1.0
24
+ * Render removed
25
+
26
+ * V4.0.0
27
+ * All code rebuilded and refactored
28
+ * No als-simple-css for style
29
+ * No charset method
30
+ * minifying for style and inner scripts
31
+ * updated render version
32
+ * render as element's method instead layout's method
33
+
34
+ * V3.0.0
35
+ * render switched to als-render
36
+ * updated bug with meta tags after body
37
+
@@ -0,0 +1,47 @@
1
+ ## Basic Usage
2
+
3
+ ### Initialization
4
+ 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.
5
+
6
+ ```js
7
+ const Layout = require('als-layout');
8
+ const layout = new Layout();
9
+ ```
10
+
11
+ ### Adding Different Elements
12
+ 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:
13
+
14
+ ```js
15
+ const layout = new Layout()
16
+ .viewport() // default width=device-width, initial-scale=1.0
17
+ .title('Test title') // adding/updating title and meta[og:title]
18
+ .favicon('/favicon.png') // adding/updating link[rel=icon][type=image/x-icon] with new href
19
+ .keywords(['some', 'keyword']) // adding/updating meta[name=keywords]. not adding existing keywords
20
+ .image('/main-image.png', '1.5') // adding/updating meta - og:image, twitter:image, twitter:card
21
+ .description('Cool site') // adding/updating meta og:description, twitter:description, and description tag
22
+ .url('/some', 'http://site.com') // adding/updating meta[og:url] and link[rel="canonical"]
23
+ .style('body {margin:0; background-color:whitesmoke;}', true) // adding css styles to existing/new style tag. Second parameter is minified (default=false).
24
+ .link('/styles.css') // adding link[rel=stylesheet] if such href not exists
25
+ .script({src:'/app.js'}, '', true) // set script with src to head if such src not exists
26
+ .script({}, 'console.log("hello world")', false) // set script with script code to footer
27
+
28
+ // Accessors for document parts
29
+ layout.body // getter for body element (if not exists, created)
30
+ layout.head // getter for head element (if not exists, created)
31
+ layout.html // getter for html Element (if not exists, created)
32
+
33
+ // Outputs
34
+ layout.rawHtml // raw HTML of the document
35
+ layout.clone // creates a new layout object clone for current object
36
+ ```
37
+
38
+ ### onload
39
+
40
+ By adding onload attribute, you can run scripts for each element after dom content has loaded.
41
+
42
+ Example how it works:
43
+ ```js
44
+ const layout = new Layout().viewport().title('On load').onload()
45
+ layout.body.innerHTML = /*html*/`<div onload="this.innerHTML = 'new content'">original content</div>`
46
+ ```
47
+
@@ -0,0 +1,19 @@
1
+ ## Cloning Functionality
2
+
3
+ ### What is Cloning and Why is it Necessary?
4
+ 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.
5
+
6
+ ### How to Use Cloning
7
+ 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.
8
+
9
+ ```js
10
+ const newLayout = layout.clone;
11
+ ```
12
+
13
+ ### Benefits of Cloning
14
+ - **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.
15
+ - **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.
16
+ - **Isolation:** Changes made to a cloned `Layout` do not affect the original, ensuring that each instance can be modified independently based on specific requirements.
17
+
18
+ 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.
19
+
@@ -0,0 +1,48 @@
1
+ ## Advanced Usage
2
+
3
+ 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:
4
+
5
+ ```js
6
+ const Layout = require('als-layout')
7
+
8
+ // Starting with a basic HTML template and specifying the host for URL methods
9
+ const raw = /*html*/`<html></html>`
10
+ const host = 'http://example.com';
11
+ const options = {
12
+ logger,
13
+ host,
14
+ minified=false
15
+ }
16
+ const layout = new Layout(raw, options).lang('fr')
17
+ console.log(layout.rawHtml)
18
+ // <!DOCTYPE html><html lang="fr"><head></p></head><body></body></html>
19
+
20
+ // Cloning the initial layout to create a specialized page
21
+ const homePage = layout.clone
22
+ homeAutoReload = layout.clone
23
+ homePage.title('Home page')
24
+ homePage.body.innerHTML = /*html*/`<h1>Home page</h1>`
25
+ console.log(homePage.rawHtml)
26
+ // <!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>
27
+
28
+ // Adding script that reloads the page every minute
29
+ homeAutoReload.script({}, 'setTimeout(function() { window.location.reload(); }, 60000);', false)
30
+ console.log(homeAutoReload.rawHtml)
31
+ // <!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>
32
+
33
+ // Demonstrating dynamic stylesheet linkage with versioning
34
+ homePage.link('/css/main.css')
35
+ console.log(homePage.rawHtml)
36
+ // Includes link to the stylesheet with version parameter to ensure fresh cache
37
+ ```
38
+
39
+ In this example:
40
+
41
+ - We start with a basic HTML template and use the `lang` method to set the language.
42
+ - 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.
43
+ - We manipulate the `body` of the `homePage` to include custom HTML.
44
+ - We add a script to `homeAutoReload` that sets up an automatic page reload, showcasing how to insert JavaScript dynamically.
45
+ - We dynamically add a versioned link to a stylesheet in the `homePage`, demonstrating control over caching and resource management.
46
+
47
+ 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.
48
+
package/docs/4-api.md ADDED
@@ -0,0 +1,88 @@
1
+ ## API
2
+
3
+ ### Constructor
4
+
5
+ #### `new Layout(html: string, options: object)`
6
+
7
+ Creates a new `Layout` instance.
8
+
9
+ - **html**: The initial HTML document string.
10
+ - **options**: Configuration options for the layout instance.
11
+ - **minified**: Boolean (default: `false`). Determines if inline CSS and JavaScript should be minified.
12
+ - **logger**: Function (default: `console`). Function for errors in minifiying and url method.
13
+ - **host**: String (defult:`undefined`). String for url method.
14
+
15
+
16
+ ### Properties
17
+
18
+ #### `layout.rawHtml`
19
+
20
+ Returns the inner HTML of the document.
21
+
22
+ #### `layout.clone`
23
+
24
+ Creates a clone of the current layout instance, preserving `options`.
25
+
26
+ ### Methods
27
+
28
+ #### `lang(lang: string): this`
29
+
30
+ Sets the `lang` attribute on the `<html>` element.
31
+
32
+ #### `onload(): this`
33
+
34
+ Adds an `onload` script to the document. If called multiple times, the script is only added once.
35
+
36
+ #### `title(title: string): this`
37
+
38
+ Sets the document title and creates an Open Graph title meta tag.
39
+
40
+ #### `description(description: string): this`
41
+
42
+ Adds description meta tags for SEO and social platforms.
43
+
44
+ #### `favicon(href: string): this`
45
+
46
+ Sets or updates the favicon URL.
47
+
48
+ #### `meta(props: object): this`
49
+
50
+ Adds or updates a `<meta>` tag with specified attributes.
51
+
52
+ - **props**: An object where each key-value pair corresponds to a meta attribute.
53
+
54
+ #### `keywords(keywords: array): this`
55
+
56
+ Sets or appends keywords in the `content` attribute of a `<meta name="keywords">` tag.
57
+
58
+ #### `viewport(viewport: string): this`
59
+
60
+ Sets the viewport meta tag. Default: `width=device-width, initial-scale=1.0`.
61
+
62
+ #### `image(image: string): this`
63
+
64
+ Sets meta tags for Open Graph and Twitter cards for an image.
65
+
66
+ #### `style(styles: string, minified: boolean = this.options.minified): this`
67
+
68
+ Adds inline CSS to the document. If `minified` is true, the CSS is minified.
69
+
70
+ - **styles**: CSS string.
71
+ - **minified**: Boolean (optional).
72
+
73
+ #### `url(url: string, host: string = this.URL): this`
74
+
75
+ Sets the canonical URL and Open Graph URL meta tags.
76
+
77
+ #### `script(attrs: object = {}, innerHTML: string = '', head: boolean = true, minified: boolean = this.options.minified): this`
78
+
79
+ Adds a `<script>` tag to the document.
80
+
81
+ - **attrs**: Attributes for the `<script>` tag.
82
+ - **innerHTML**: Inline JavaScript (optional).
83
+ - **head**: Boolean (default: `true`). If `false`, the script is added to `<body>`.
84
+ - **minified**: Boolean. If true, inline JavaScript is minified.
85
+
86
+ #### `link(href: string, attributes: object = { rel: "stylesheet", type: "text/css" }): this`
87
+
88
+ Adds a `<link>` tag for external stylesheets.
package/lib/layout.js CHANGED
@@ -2,6 +2,7 @@ const { Document, SingleNode } = require('als-document');
2
2
  const script = require('./script');
3
3
  const style = require('./style');
4
4
  const onloadScript = require('./onload');
5
+ const deepClone = require('als-deep-clone')
5
6
 
6
7
  class Layout extends Document {
7
8
  constructor(html, options = {}) {
@@ -9,11 +10,10 @@ class Layout extends Document {
9
10
  this.options = options
10
11
  this.logger = options.logger || console
11
12
  this.root = this.html
12
- this.statusCode = 200
13
13
  }
14
14
 
15
15
  get rawHtml() { return this.innerHTML }
16
- get clone() { return new this.constructor(new Document(this, this.URL), this.options) }
16
+ get clone() { return new this.constructor(new Document(this, this.URL), deepClone(this.options)) }
17
17
  lang(lang) { this.html.setAttribute('lang', lang); return this }
18
18
 
19
19
  onload() {
@@ -113,17 +113,6 @@ class Layout extends Document {
113
113
  this.head.insert(2, linkElement)
114
114
  return this
115
115
  }
116
-
117
- status(statusCode) { this.statusCode = statusCode; return this }
118
-
119
- end(res,statusCode = this.statusCode, rawHtml = this.rawHtml) {
120
- res.writeHead(statusCode);
121
- if (res.set) res.set('Content-Type', 'text/html') // for Express
122
- else res.setHeader('Content-Type', 'text/html') // for http
123
- res.end(rawHtml)
124
- return this
125
- }
126
-
127
116
  }
128
117
 
129
118
  module.exports = Layout
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "als-layout",
3
- "version": "5.0.1",
3
+ "version": "5.2.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "node --test --experimental-test-coverage",
@@ -11,6 +11,7 @@
11
11
  "license": "MIT",
12
12
  "description": "Html layout constructor",
13
13
  "dependencies": {
14
+ "als-deep-clone": "^1.0.0",
14
15
  "als-document": "^1.4.0",
15
16
  "uglify-js": "^3.19.2",
16
17
  "uglifycss": "^0.0.29"
package/readme.md CHANGED
@@ -19,6 +19,11 @@ const Layout = require('als-layout')
19
19
  ```
20
20
 
21
21
  ## Change Log
22
+ * V5.2.0
23
+ * options object on clone, cloned too (remove the reference and deep cloning the object)
24
+ * V5.1.0
25
+ * status and end methods removed
26
+ * status and end methods added to als-view
22
27
  * V5.0.0
23
28
  * constructor changed
24
29
  * options object parameter instead separated parameters
@@ -77,8 +82,6 @@ const layout = new Layout()
77
82
  .link('/styles.css') // adding link[rel=stylesheet] if such href not exists
78
83
  .script({src:'/app.js'}, '', true) // set script with src to head if such src not exists
79
84
  .script({}, 'console.log("hello world")', false) // set script with script code to footer
80
- .status(200)
81
- .end(response);
82
85
 
83
86
  // Accessors for document parts
84
87
  layout.body // getter for body element (if not exists, created)
@@ -98,7 +101,9 @@ Example how it works:
98
101
  ```js
99
102
  const layout = new Layout().viewport().title('On load').onload()
100
103
  layout.body.innerHTML = /*html*/`<div onload="this.innerHTML = 'new content'">original content</div>`
101
- ```
104
+ ```
105
+
106
+
102
107
  ## Cloning Functionality
103
108
 
104
109
  ### What is Cloning and Why is it Necessary?
@@ -166,6 +171,7 @@ In this example:
166
171
  - We dynamically add a versioned link to a stylesheet in the `homePage`, demonstrating control over caching and resource management.
167
172
 
168
173
  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.
174
+
169
175
 
170
176
  ## API
171
177
 
@@ -255,11 +261,3 @@ Adds a `<script>` tag to the document.
255
261
  #### `link(href: string, attributes: object = { rel: "stylesheet", type: "text/css" }): this`
256
262
 
257
263
  Adds a `<link>` tag for external stylesheets.
258
-
259
- #### `status(statusCode: number): this`
260
-
261
- Sets the HTTP status code for the response.
262
-
263
- #### `end(res: object): this`
264
-
265
- Sends the HTML response with the specified `statusCode` and sets `Content-Type` to `text/html`.