@soleil-se/app-util 3.0.3 → 4.0.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.
- package/CHANGELOG.md +18 -29
- package/MIGRATION.md +136 -0
- package/README.md +146 -266
- package/client/attribute-util/getCurrentScript.js +56 -0
- package/{attribute-util → client/attribute-util}/index.js +4 -1
- package/{svelte-client → client/svelte}/index.js +4 -8
- package/{render-vue → client/vue}/index.js +7 -8
- package/common/index.js +144 -0
- package/common/legacy/getRouteUri.js +19 -0
- package/common/legacy/getViewUri.js +21 -0
- package/docs/1.render.md +53 -0
- package/docs/2.svelte.md +52 -0
- package/docs/3.vue.md +19 -0
- package/docs/4.underscore.md +48 -0
- package/package.json +6 -8
- package/server/index.js +78 -0
- package/server/svelte/index.js +33 -0
- package/app-data/.eslintrc +0 -4
- package/app-data/index.js +0 -16
- package/attribute-util/.eslintrc +0 -4
- package/src/index.js +0 -162
- package/svelte-client/.eslintrc +0 -4
- package/svelte-server/index.js +0 -26
- /package/{render-vue → client}/.eslintrc +0 -0
package/README.md
CHANGED
|
@@ -1,329 +1,209 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@soleil-se/webapp-util@^4.0.0`
|
|
2
2
|
Utility functions for Webapps.
|
|
3
3
|
|
|
4
|
+
[GitHub](https://github.com/soleilit/server-monorepo/tree/master/packages/webapp-util)
|
|
5
|
+
[NPM](https://www.npmjs.com/package/@soleil-se/webapp-util)
|
|
6
|
+
|
|
4
7
|
<!-- TOC -->
|
|
5
8
|
|
|
9
|
+
- [Requirements](#requirements)
|
|
6
10
|
- [Install](#install)
|
|
7
|
-
- [
|
|
8
|
-
- [
|
|
9
|
-
- [
|
|
10
|
-
- [
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- [
|
|
14
|
-
|
|
15
|
-
- [
|
|
16
|
-
|
|
17
|
-
- [
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
- [Example](#example-4)
|
|
21
|
-
- [`isOffline`](#isoffline)
|
|
22
|
-
- [`isOnline`](#isonline)
|
|
23
|
-
- [`uniqueId`](#uniqueid)
|
|
24
|
-
- [AppData](#appdata)
|
|
25
|
-
- [Svelte](#svelte)
|
|
26
|
-
- [Server](#server)
|
|
27
|
-
- [`render(App, [data], [settings])` ⇒ `String`](#renderapp-data-settings-⇒-string)
|
|
28
|
-
- [`renderServer(App, [data])` ⇒ `String`](#renderserverapp-data-⇒-string)
|
|
29
|
-
- [`renderClient(App, [data], [settings])` ⇒ `String`](#renderclientapp-data-settings-⇒-string)
|
|
30
|
-
- [Client](#client)
|
|
31
|
-
- [`render(App)`](#renderapp)
|
|
11
|
+
- [Migration](#migration)
|
|
12
|
+
- [API](#api)
|
|
13
|
+
- [Constants](#constants)
|
|
14
|
+
- [Functions](#functions)
|
|
15
|
+
- [appId : <code>String</code>](#appid--codestringcode)
|
|
16
|
+
- [isOffline : <code>Boolean</code>](#isoffline--codebooleancode)
|
|
17
|
+
- [isOnline : <code>Boolean</code>](#isonline--codebooleancode)
|
|
18
|
+
- [getNamespace([prefix]) ⇒ <code>String</code>](#getnamespaceprefix-⇒-codestringcode)
|
|
19
|
+
- [getRouteUri(route) ⇒ <code>String</code>](#getrouteuriroute-⇒-codestringcode)
|
|
20
|
+
- [getViewUri(route) ⇒ <code>String</code>](#getviewuriroute-⇒-codestringcode)
|
|
21
|
+
- [getResourceUri(resource) ⇒ <code>String</code>](#getresourceuriresource-⇒-codestringcode)
|
|
22
|
+
- [getAppData([key]) ⇒ <code>\*</code> \| <code>Object</code>](#getappdatakey-⇒-code\code-\-codeobjectcode)
|
|
23
|
+
- [Rendering](#rendering)
|
|
32
24
|
|
|
33
25
|
<!-- /TOC -->
|
|
34
26
|
|
|
27
|
+
## Requirements
|
|
28
|
+
`@soleil-se/sv-gulp-build` 4 or later (Works best with ^4.3.0).
|
|
29
|
+
|
|
35
30
|
## Install
|
|
36
31
|
`yarn add @soleil-se/webapp-util`
|
|
37
32
|
|
|
38
|
-
##
|
|
39
|
-
|
|
33
|
+
## Migration
|
|
34
|
+
Migrating from version 2 or 3?
|
|
35
|
+
See [MIGRATION](./MIGRATION.md).
|
|
40
36
|
|
|
41
|
-
##
|
|
37
|
+
## API
|
|
38
|
+
All imports from base package are available both on the server and client.
|
|
42
39
|
|
|
43
|
-
|
|
40
|
+
### Constants
|
|
44
41
|
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
<dl>
|
|
43
|
+
<dt><a href="#appId">appId</a> : <code>String</code></dt>
|
|
44
|
+
<dd><p>DOM friendly unique identifier for the WebApp.</p>
|
|
45
|
+
</dd>
|
|
46
|
+
<dt><a href="#isOffline">isOffline</a> : <code>Boolean</code></dt>
|
|
47
|
+
<dd><p>If the WebApp is running in offline mode or not.</p>
|
|
48
|
+
</dd>
|
|
49
|
+
<dt><a href="#isOnline">isOnline</a> : <code>Boolean</code></dt>
|
|
50
|
+
<dd><p>If the WebApp is running in online mode or not.</p>
|
|
51
|
+
</dd>
|
|
52
|
+
</dl>
|
|
47
53
|
|
|
48
|
-
|
|
54
|
+
### Functions
|
|
49
55
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
Get
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
| [settings.defer] | <code>Boolean</code> | <code>true</code> | If the app script should be loaded after DOM is ready. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
68
|
-
| [settings.req] | <code>Object</code> | | The req object from SiteVision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
|
|
69
|
-
|
|
70
|
-
#### Example
|
|
71
|
-
In `app_src/server/index.js`.
|
|
72
|
-
```javascript
|
|
73
|
-
router.get('/', (req, res) => {
|
|
74
|
-
const data = { foo: 'bar' };
|
|
75
|
-
// Most common usage.
|
|
76
|
-
res.send(renderApp(data));
|
|
77
|
-
|
|
78
|
-
// With all settings.
|
|
79
|
-
res.send(renderApp(data, {
|
|
80
|
-
noScript: 'You need JS for this!',
|
|
81
|
-
selector: '#mount_me_here',
|
|
82
|
-
async: false,
|
|
83
|
-
defer: true,
|
|
84
|
-
req,
|
|
85
|
-
}));
|
|
86
|
-
});
|
|
87
|
-
```
|
|
88
|
-
##### **Vue**
|
|
89
|
-
In `app_src/client/index.js` or `app_src/main.js`.
|
|
90
|
-
```javascript
|
|
91
|
-
import render from '@soleil-se/webapp-util/render-vue';
|
|
92
|
-
import App from './App.vue';
|
|
56
|
+
<dl>
|
|
57
|
+
<dt><a href="#getNamespace">getNamespace([prefix])</a> ⇒ <code>String</code></dt>
|
|
58
|
+
<dd><p>Get a prefixed namespace unique for app.</p>
|
|
59
|
+
</dd>
|
|
60
|
+
<dt><a href="#getRouteUri">getRouteUri(route)</a> ⇒ <code>String</code></dt>
|
|
61
|
+
<dd><p>Get URI for a route, same as <code>getStandaloneUrl</code> in SiteVision template.</p>
|
|
62
|
+
</dd>
|
|
63
|
+
<dt><a href="#getViewUri">getViewUri(route)</a> ⇒ <code>String</code></dt>
|
|
64
|
+
<dd><p>Get URI for a view, same as <code>getUrl</code> in SiteVision template.</p>
|
|
65
|
+
</dd>
|
|
66
|
+
<dt><a href="#getResourceUri">getResourceUri(resource)</a> ⇒ <code>String</code></dt>
|
|
67
|
+
<dd><p>Get URI for a resource.</p>
|
|
68
|
+
</dd>
|
|
69
|
+
<dt><a href="#getAppData">getAppData([key])</a> ⇒ <code>*</code> | <code>Object</code></dt>
|
|
70
|
+
<dd><p>Get appData value or object.</p>
|
|
71
|
+
</dd>
|
|
72
|
+
</dl>
|
|
93
73
|
|
|
94
|
-
|
|
95
|
-
```
|
|
74
|
+
<a name="appId"></a>
|
|
96
75
|
|
|
97
|
-
###
|
|
98
|
-
|
|
99
|
-
https://developer.sitevision.se/docs/webapps/template#h-Methods
|
|
76
|
+
### appId : <code>String</code>
|
|
77
|
+
DOM friendly unique identifier for the WebApp.
|
|
100
78
|
|
|
101
|
-
|
|
79
|
+
```js
|
|
80
|
+
import { appId } from '@soleil-se/webapp-util';
|
|
102
81
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
82
|
+
console.log(appId); // For example: 12_682d461b1708a9bb1ea13efd
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
<a name="isOffline"></a>
|
|
86
|
+
|
|
87
|
+
### isOffline : <code>Boolean</code>
|
|
88
|
+
If the WebApp is running in offline mode or not.
|
|
106
89
|
|
|
90
|
+
```js
|
|
91
|
+
import { isOffline } from '@soleil-se/webapp-util';
|
|
107
92
|
|
|
108
|
-
|
|
109
|
-
```javascript
|
|
110
|
-
const routeUri = getRouteUri('/my-route');
|
|
93
|
+
console.log(isOffline); // true or false
|
|
111
94
|
```
|
|
112
|
-
### `getViewUri(route)` ⇒ `String`
|
|
113
|
-
Get URI for a view, same as `getUrl` in SiteVision template.
|
|
114
|
-
https://developer.sitevision.se/docs/webapps/template#h-Methods
|
|
115
95
|
|
|
116
|
-
|
|
96
|
+
<a name="isOnline"></a>
|
|
117
97
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
98
|
+
### isOnline : <code>Boolean</code>
|
|
99
|
+
If the WebApp is running in online mode or not.
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
import { isOnline } from '@soleil-se/webapp-util';
|
|
121
103
|
|
|
122
|
-
|
|
123
|
-
```javascript
|
|
124
|
-
const viewUri = getViewUri('/my-route');
|
|
104
|
+
console.log(isOnline); // true or false
|
|
125
105
|
```
|
|
126
106
|
|
|
127
|
-
###
|
|
128
|
-
Get
|
|
107
|
+
### getNamespace([prefix]) ⇒ <code>String</code>
|
|
108
|
+
Get a prefixed namespace unique for app.
|
|
129
109
|
|
|
130
|
-
**Returns**:
|
|
110
|
+
**Returns**: <code>String</code> - Prefixed namespace.
|
|
131
111
|
|
|
132
|
-
| Param | Type |
|
|
112
|
+
| Param | Type | Default |
|
|
133
113
|
| --- | --- | --- |
|
|
134
|
-
|
|
|
114
|
+
| [prefix] | <code>string</code> | <code>"'app'"</code> |
|
|
135
115
|
|
|
116
|
+
```js
|
|
117
|
+
import { getNamespace } from '@soleil-se/webapp-util';
|
|
136
118
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const resourceUri = getResourceUri('file/in/resource.png');
|
|
140
|
-
```
|
|
141
|
-
### `renderTemplate(template, [values])` ⇒ `String`
|
|
142
|
-
Renders a Underscore template and returns a string.
|
|
143
|
-
This function is also available inside the template.
|
|
144
|
-
|
|
145
|
-
**Returns**: `String` - Rendered template
|
|
146
|
-
|
|
147
|
-
| Param | Type | Default | Description |
|
|
148
|
-
| --- | --- | --- | --- |
|
|
149
|
-
| template | `String` | | Underscore template. |
|
|
150
|
-
| [values] | `Object` | `{}` | Values. |
|
|
151
|
-
|
|
152
|
-
#### Example
|
|
153
|
-
**Simple example**
|
|
154
|
-
```javascript
|
|
155
|
-
const string = renderTemplate('<div><%= foo %></div>', {
|
|
156
|
-
foo: 'bar',
|
|
157
|
-
});
|
|
158
|
-
```
|
|
159
|
-
**Multiple templates**
|
|
160
|
-
```html
|
|
161
|
-
/* views/item.html */
|
|
162
|
-
<li>
|
|
163
|
-
<%- name %>
|
|
164
|
-
</li>
|
|
165
|
-
```
|
|
166
|
-
```html
|
|
167
|
-
/* views/main.html */
|
|
168
|
-
<ul>
|
|
169
|
-
<% items.forEach(function(item) { %>
|
|
170
|
-
<%= renderTemplate(itemTemplate, item) %>
|
|
171
|
-
<% }); %>
|
|
172
|
-
</ul>
|
|
173
|
-
```
|
|
119
|
+
console.log(getNamespace());
|
|
120
|
+
// For example: app_12_682d461b1708a9bb1ea13efd
|
|
174
121
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
import mainTemplate from './views/main.html';
|
|
178
|
-
import itemTemplate from './views/item.html';
|
|
122
|
+
console.log(getNamespace('input'));
|
|
123
|
+
// For example: input_12_682d461b1708a9bb1ea13efd
|
|
179
124
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
> **NOTE**
|
|
184
|
-
> Remember that the second argument must be an object and that objects properties are accessed directly in any child templates!
|
|
185
|
-
### `isOffline`
|
|
186
|
-
If the webapp is running in offline mode or not.
|
|
187
|
-
This value is also passed to AppData.
|
|
188
|
-
|
|
189
|
-
```javascript
|
|
190
|
-
if(isOffline) {
|
|
191
|
-
// Do something
|
|
192
|
-
}
|
|
125
|
+
// If the app is in a decoration template the Portlet ID is the same for all instances, so the ID of the decorated node is used as well.
|
|
126
|
+
console.log(getNamespace('decoration'));
|
|
127
|
+
// For example: decoration_10_3871c02f1754f3aa8f9d4eb_12_70c3d424173b4900fc550e1c
|
|
193
128
|
```
|
|
194
129
|
|
|
195
|
-
|
|
196
|
-
If the webapp is running in online mode or not.
|
|
197
|
-
This value is also passed to AppData.
|
|
130
|
+
<a name="getRouteUri"></a>
|
|
198
131
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
// Do something
|
|
202
|
-
}
|
|
203
|
-
```
|
|
132
|
+
### getRouteUri(route) ⇒ <code>String</code>
|
|
133
|
+
Get URI for a route, same as `getStandaloneUrl` in SiteVision template.
|
|
204
134
|
|
|
205
|
-
|
|
206
|
-
A unique ID for the WebApp.
|
|
207
|
-
This value is also passed to AppData.
|
|
135
|
+
**Returns**: <code>String</code> - URI for route.
|
|
208
136
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
```javascript
|
|
213
|
-
import AppData from '@soleil-se/webapp-util/app-data';
|
|
137
|
+
| Param | Type | Description |
|
|
138
|
+
| --- | --- | --- |
|
|
139
|
+
| route | <code>String</code> | A route. |
|
|
214
140
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
```
|
|
141
|
+
```js
|
|
142
|
+
import { getRouteUri } from '@soleil-se/webapp-util';
|
|
218
143
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
```javascript
|
|
222
|
-
res.send(renderApp({ foo: 'bar' }))
|
|
144
|
+
console.log(getRouteUri('/items'));
|
|
145
|
+
// URI structure: /appresource/<pageId>/<portletId>/items
|
|
223
146
|
```
|
|
224
|
-
In app:
|
|
225
|
-
```javascript
|
|
226
|
-
import AppData from '@soleil-se/webapp-util/app-data';
|
|
227
147
|
|
|
228
|
-
|
|
148
|
+
<a name="getViewUri"></a>
|
|
229
149
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
console.log(foo);
|
|
233
|
-
```
|
|
150
|
+
### getViewUri(route) ⇒ <code>String</code>
|
|
151
|
+
Get URI for a view, same as `getUrl` in SiteVision template.
|
|
234
152
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
#### `render(App, [data], [settings])` ⇒ `String`
|
|
241
|
-
Get HTML string for rendering a universal Svelte application.
|
|
242
|
-
To be used together with `@soleil-se/webapp-util/svelte-client`.
|
|
243
|
-
|
|
244
|
-
**Returns**: <code>String</code> - HTML for rendering an application.
|
|
245
|
-
|
|
246
|
-
| Param | Type | Default | Description |
|
|
247
|
-
| --- | --- | --- | --- |
|
|
248
|
-
| [App] | <code>Svelte</code> | | Svelte application. |
|
|
249
|
-
| [data] | <code>Object</code> | <code>{}</code> | Server data or config that will be available from `@soleil-se/webapp-util/app-data` |
|
|
250
|
-
| [settings] | <code>Object</code> | <code>{}</code> | Settings object. |
|
|
251
|
-
| [settings.async] | <code>Boolean</code> | <code>false</code> | If the app script should be loaded asynchronously. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
252
|
-
| [settings.defer] | <code>Boolean</code> | <code>true</code> | If the app script should be loaded after DOM is ready. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
253
|
-
| [settings.req] | <code>Object</code> | | The req object from SiteVision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
|
|
254
|
-
|
|
255
|
-
In `app_src/server/index.js` or `app_src/index.js`.
|
|
256
|
-
```javascript
|
|
257
|
-
import router from 'router';
|
|
258
|
-
import { render } from '@soleil-se/webapp-util/svelte-server';
|
|
259
|
-
|
|
260
|
-
import App from './App.svelte';
|
|
261
|
-
|
|
262
|
-
router.get('/', (req, res) => {
|
|
263
|
-
const data = { foo: 'bar' };
|
|
264
|
-
res.send(renderSvelte(App, data));
|
|
265
|
-
});
|
|
266
|
-
```
|
|
153
|
+
**Returns**: <code>String</code> - URI for view.
|
|
154
|
+
|
|
155
|
+
| Param | Type | Description |
|
|
156
|
+
| --- | --- | --- |
|
|
157
|
+
| route | <code>String</code> | A route. |
|
|
267
158
|
|
|
268
|
-
|
|
269
|
-
|
|
159
|
+
```js
|
|
160
|
+
import { getViewUri } from '@soleil-se/webapp-util';
|
|
270
161
|
|
|
271
|
-
|
|
162
|
+
console.log(getViewUri('/items'));
|
|
163
|
+
// URI structure: ?sv.target=<id>&sv.<id>.route=/items
|
|
164
|
+
```
|
|
272
165
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
| [data] | <code>Object</code> | <code>{}</code> | Server data or config that will be available from `@soleil-se/webapp-util/app-data` |
|
|
166
|
+
<a name="getResourceUri"></a>
|
|
167
|
+
### getResourceUri(resource) ⇒ <code>String</code>
|
|
168
|
+
Get URI for a resource.
|
|
277
169
|
|
|
278
|
-
|
|
279
|
-
```javascript
|
|
280
|
-
import router from 'router';
|
|
281
|
-
import { renderServer } from '@soleil-se/webapp-util/svelte-server';
|
|
170
|
+
**Returns**: <code>String</code> - URI for a resource.
|
|
282
171
|
|
|
283
|
-
|
|
172
|
+
| Param | Type | Description |
|
|
173
|
+
| --- | --- | --- |
|
|
174
|
+
| resource | <code>String</code> | A resource. |
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
import { getResourceUri } from '@soleil-se/webapp-util';
|
|
284
178
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
res.send(renderServer(App, data));
|
|
288
|
-
});
|
|
179
|
+
console.log(getResourceUri('/image.png'));
|
|
180
|
+
// URI structure: /webapp-files/<webappname>/<webappversion>/image.png
|
|
289
181
|
```
|
|
290
182
|
|
|
291
|
-
|
|
292
|
-
Get HTML string for a client only rendered Svelte application.
|
|
293
|
-
Can be used together with `@soleil-se/webapp-util/svelte-client`.
|
|
183
|
+
<a name="getAppData"></a>
|
|
294
184
|
|
|
295
|
-
|
|
185
|
+
### getAppData([key]) ⇒ <code>\*</code> \| <code>Object</code>
|
|
186
|
+
Get appData value or object that is passed to app when rendering.
|
|
296
187
|
|
|
297
|
-
|
|
298
|
-
| --- | --- | --- | --- |
|
|
299
|
-
| [App] | <code>Svelte</code> | | Svelte application. |
|
|
300
|
-
| [data] | <code>Object</code> | <code>{}</code> | Server data or config that will be available from `@soleil-se/webapp-util/app-data` |
|
|
301
|
-
| [settings] | <code>Object</code> | <code>{}</code> | Settings object. |
|
|
302
|
-
| [settings.async] | <code>Boolean</code> | <code>false</code> | If the app script should be loaded asynchronously. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
303
|
-
| [settings.defer] | <code>Boolean</code> | <code>true</code> | If the app script should be loaded after DOM is ready. [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/) |
|
|
304
|
-
| [settings.req] | <code>Object</code> | | The req object from SiteVision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
|
|
188
|
+
**Returns**: <code>\*</code> \| <code>Object</code> - Value or object.
|
|
305
189
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
import { renderClient } from '@soleil-se/webapp-util/svelte-server';
|
|
190
|
+
| Param | Type | Description |
|
|
191
|
+
| --- | --- | --- |
|
|
192
|
+
| [key] | <code>String</code> | Key for value. |
|
|
310
193
|
|
|
311
|
-
|
|
194
|
+
```js
|
|
195
|
+
import { getAppData } from '@soleil-se/webapp-util';
|
|
312
196
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
});
|
|
197
|
+
// Get value with key
|
|
198
|
+
const myValue = getAppData('myValue');
|
|
199
|
+
// Or with destructuring
|
|
200
|
+
const { myValue } = getAppData();
|
|
317
201
|
```
|
|
318
202
|
|
|
319
|
-
|
|
320
|
-
`@soleil-se/webapp-util/svelte-client`
|
|
321
|
-
#### `render(App)`
|
|
322
|
-
In `app_src/client/index.js` or `app_src/main.js`.
|
|
323
|
-
```javascript
|
|
324
|
-
import { render } from '@soleil-se/webapp-util/svelte-client';
|
|
325
|
-
import App from './App.svelte';
|
|
203
|
+
## Rendering
|
|
326
204
|
|
|
327
|
-
render
|
|
328
|
-
|
|
205
|
+
* [Render](./docs/1.render.md)
|
|
206
|
+
* [Svelte](./docs/2.svelte.md)
|
|
207
|
+
* [Vue](./docs/3.vue.md)
|
|
208
|
+
* [Underscore](./docs/4.underscore.md)
|
|
329
209
|
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/* Scoped version of https://github.com/amiller-gh/currentScript-polyfill */
|
|
2
|
+
// document.currentScript polyfill by Adam Miller
|
|
3
|
+
// MIT license
|
|
4
|
+
|
|
5
|
+
export default function getCurrentScript() {
|
|
6
|
+
if (!('currentScript' in document)) {
|
|
7
|
+
// IE 8-10 support script readyState
|
|
8
|
+
// IE 11+ support stack trace
|
|
9
|
+
try {
|
|
10
|
+
throw new Error();
|
|
11
|
+
} catch (err) {
|
|
12
|
+
// Find the second match for the "at" string to get file src url from stack.
|
|
13
|
+
// Specifically works with the format of stack traces in IE.
|
|
14
|
+
let i = 0;
|
|
15
|
+
const stackDetails = (/.*at [^(]*\((.*):(.+):(.+)\)$/ig).exec(err.stack);
|
|
16
|
+
const scriptLocation = (stackDetails && stackDetails[1]) || false;
|
|
17
|
+
const line = (stackDetails && stackDetails[2]) || false;
|
|
18
|
+
const currentLocation = document.location.href.replace(document.location.hash, '');
|
|
19
|
+
let pageSource;
|
|
20
|
+
let inlineScriptSourceRegExp;
|
|
21
|
+
let inlineScriptSource;
|
|
22
|
+
const scripts = document.getElementsByTagName('script'); // Live NodeList collection
|
|
23
|
+
|
|
24
|
+
if (scriptLocation === currentLocation) {
|
|
25
|
+
pageSource = document.documentElement.outerHTML;
|
|
26
|
+
inlineScriptSourceRegExp = new RegExp(`(?:[^\\n]+?\\n){0,${line - 2}}[^<]*<script>([\\d\\D]*?)<\\/script>[\\d\\D]*`, 'i');
|
|
27
|
+
inlineScriptSource = pageSource.replace(inlineScriptSourceRegExp, '$1').trim();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (; i < scripts.length; i += 1) {
|
|
31
|
+
// If ready state is interactive, return the script tag
|
|
32
|
+
if (scripts[i].readyState === 'interactive') {
|
|
33
|
+
return scripts[i];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// If src matches, return the script tag
|
|
37
|
+
if (scripts[i].src === scriptLocation) {
|
|
38
|
+
return scripts[i];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// If inline source matches, return the script tag
|
|
42
|
+
if (
|
|
43
|
+
scriptLocation === currentLocation
|
|
44
|
+
&& scripts[i].innerHTML
|
|
45
|
+
&& scripts[i].innerHTML.trim() === inlineScriptSource
|
|
46
|
+
) {
|
|
47
|
+
return scripts[i];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// If no match, return null
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return document.currentScript;
|
|
56
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import getCurrentScript from './getCurrentScript';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* JSON decode an attribute on the currentScript element.
|
|
3
5
|
* Use if attribute contains a JSON-object.
|
|
@@ -5,7 +7,8 @@
|
|
|
5
7
|
* @returns {Object} Decoded JSON object.
|
|
6
8
|
*/
|
|
7
9
|
export const decodeAttribute = (attribute) => {
|
|
8
|
-
const
|
|
10
|
+
const currentScript = getCurrentScript();
|
|
11
|
+
const encoded = currentScript.getAttribute(attribute);
|
|
9
12
|
if (encoded) {
|
|
10
13
|
return decodeURIComponent(encoded);
|
|
11
14
|
}
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
import '
|
|
3
|
-
|
|
4
|
-
import { decodeAttribute, parseAttribute } from '../attribute-util';
|
|
5
|
-
import { setAppData } from '../app-data';
|
|
6
|
-
|
|
7
|
-
const props = parseAttribute('data-app');
|
|
8
|
-
setAppData(props);
|
|
2
|
+
import { decodeAttribute } from '../attribute-util';
|
|
3
|
+
import { getAppData } from '../../common';
|
|
9
4
|
|
|
10
5
|
/**
|
|
11
|
-
*
|
|
6
|
+
* Renders a Svelte application
|
|
12
7
|
* @param {Svelte} App Svelte application to be started.
|
|
13
8
|
*/
|
|
14
9
|
export function render(App) {
|
|
@@ -16,6 +11,7 @@ export function render(App) {
|
|
|
16
11
|
|
|
17
12
|
const mountElement = document.querySelector(selector);
|
|
18
13
|
const hydrate = mountElement.childElementCount > 0;
|
|
14
|
+
const props = getAppData();
|
|
19
15
|
if (mountElement) {
|
|
20
16
|
return new App({
|
|
21
17
|
hydrate,
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
/* eslint-disable-next-line import/no-unresolved */
|
|
2
3
|
import Vue from 'vue';
|
|
3
4
|
|
|
4
|
-
import { decodeAttribute
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
const options = parseAttribute('data-app');
|
|
8
|
-
setAppData(options);
|
|
5
|
+
import { decodeAttribute } from '../attribute-util';
|
|
6
|
+
import { getAppData, isOffline } from '../../common';
|
|
9
7
|
|
|
10
8
|
const offlineModeMixin = {
|
|
11
9
|
mounted() {
|
|
12
|
-
if (
|
|
10
|
+
if (isOffline) {
|
|
13
11
|
const $anchors = this.$el.querySelectorAll('a');
|
|
14
12
|
$anchors.forEach(($anchor) => {
|
|
15
13
|
$anchor.addEventListener('click', (e) => {
|
|
@@ -24,8 +22,9 @@ const offlineModeMixin = {
|
|
|
24
22
|
* Render a Vue application
|
|
25
23
|
* @param {Vue} App Vue application to be started.
|
|
26
24
|
*/
|
|
27
|
-
export
|
|
25
|
+
export function render(App) {
|
|
28
26
|
const selector = decodeAttribute('data-selector');
|
|
27
|
+
const options = getAppData();
|
|
29
28
|
|
|
30
29
|
Object.assign(App, options, {
|
|
31
30
|
mixins: [offlineModeMixin],
|