@soleil-se/app-util 1.0.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/README.md +44 -0
- package/dist/index.js +48 -0
- package/package.json +13 -0
- package/src/index.js +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Webapp Util
|
|
2
|
+
## Install
|
|
3
|
+
`yarn add @soleil-se/webapp-util`
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
```javascript
|
|
7
|
+
import { renderVue, getRouteUri, getResourceUri, renderTemplate, isOffline } from '@soleil-se/webapp-util';
|
|
8
|
+
```
|
|
9
|
+
### `renderVue`
|
|
10
|
+
Prints bootstrap code for a Vue application.
|
|
11
|
+
`name` has to match the name of the Vue application.
|
|
12
|
+
`data` is an object that will be available in `$options`.
|
|
13
|
+
`noScript` is a HTML-string that will be printed when JavaScript is not available.
|
|
14
|
+
```javascript
|
|
15
|
+
router.get('/', (req, res) => {
|
|
16
|
+
res.send(renderVue('AppName', data, noScript));
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
### `getRouteUri`
|
|
20
|
+
Get URI for a route.
|
|
21
|
+
```javascript
|
|
22
|
+
const routeUri = getRouteUri('/my-route');
|
|
23
|
+
```
|
|
24
|
+
### `getResourceUri`
|
|
25
|
+
Get URI for a resource.
|
|
26
|
+
```javascript
|
|
27
|
+
const resourceUri = getResourceUri('file/in/resource.png');
|
|
28
|
+
```
|
|
29
|
+
### `renderTemplate`
|
|
30
|
+
Renders a Underscore template to a string.
|
|
31
|
+
```javascript
|
|
32
|
+
const string = renderTemplate('<div><%= foo %></div>', {
|
|
33
|
+
foo: 'bar',
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
### `isOffline`
|
|
37
|
+
```javascript
|
|
38
|
+
if(isOffline) {
|
|
39
|
+
// Do something
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import PropertyUtil from 'PropertyUtil';
|
|
2
|
+
import PortletContextUtil from 'PortletContextUtil';
|
|
3
|
+
import VersionUtil from 'VersionUtil';
|
|
4
|
+
import app from 'app';
|
|
5
|
+
import _ from 'underscore';
|
|
6
|
+
|
|
7
|
+
export var isOffline = VersionUtil.getCurrentVersion() === VersionUtil.OFFLINE_VERSION;
|
|
8
|
+
|
|
9
|
+
export function getResourceUri(resource) {
|
|
10
|
+
return ("/webapp-files/" + (app.webAppId) + "/" + (app.webAppVersion) + "/" + resource);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function renderTemplate(template, values) {
|
|
14
|
+
if ( values === void 0 ) values = {};
|
|
15
|
+
|
|
16
|
+
if (typeof template === 'function') {
|
|
17
|
+
return template(values);
|
|
18
|
+
}
|
|
19
|
+
return _.template(template)(values);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function renderVue(name, data, noScript) {
|
|
23
|
+
if ( noScript === void 0 ) noScript = '';
|
|
24
|
+
|
|
25
|
+
var options = Object.assign({}, {webapp: app, isOffline: isOffline}, data);
|
|
26
|
+
|
|
27
|
+
var vueUri = getResourceUri('vue/index.js');
|
|
28
|
+
var html = "<!-- Generated by WebappUtil.renderVue() -->\n<div data-vue-name=\"" + name + "\" data-vue-portlet=\"" + (app.portletId) + "\">" + noScript + "</div>\n<script src=\"" + vueUri + "\"></script>\n<script type=\"text/javascript\">\n vueApps['" + name + "'].render('" + name + "', '" + (app.portletId) + "', " + (JSON.stringify(options)) + ");\n</script>";
|
|
29
|
+
return html;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getRouteUri(route) {
|
|
33
|
+
var currentPortlet = PortletContextUtil.getCurrentPortlet();
|
|
34
|
+
if (currentPortlet) {
|
|
35
|
+
var currentPortletId = currentPortlet.getIdentifier();
|
|
36
|
+
var currentPageUri = PropertyUtil.getString(PortletContextUtil.getCurrentPage(), 'URI');
|
|
37
|
+
return (currentPageUri + "?sv." + currentPortletId + ".route=" + (encodeURI(route)) + "&sv.target=" + currentPortletId);
|
|
38
|
+
}
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default {
|
|
43
|
+
getRouteUri: getRouteUri,
|
|
44
|
+
getResourceUri: getResourceUri,
|
|
45
|
+
renderVue: renderVue,
|
|
46
|
+
renderTemplate: renderTemplate,
|
|
47
|
+
isOffline: isOffline,
|
|
48
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@soleil-se/app-util",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"author": "Soleil AB",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"private": false,
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "npx buble ./src/index.js --output ./dist/index.js --no modules --objectAssign=Object.assign"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {},
|
|
12
|
+
"devDependencies": {}
|
|
13
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import PropertyUtil from 'PropertyUtil';
|
|
2
|
+
import PortletContextUtil from 'PortletContextUtil';
|
|
3
|
+
import VersionUtil from 'VersionUtil';
|
|
4
|
+
import app from 'app';
|
|
5
|
+
import _ from 'underscore';
|
|
6
|
+
|
|
7
|
+
export const isOffline = VersionUtil.getCurrentVersion() === VersionUtil.OFFLINE_VERSION;
|
|
8
|
+
|
|
9
|
+
export function getResourceUri(resource) {
|
|
10
|
+
return `/webapp-files/${app.webAppId}/${app.webAppVersion}/${resource}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function renderTemplate(template, values = {}) {
|
|
14
|
+
if (typeof template === 'function') {
|
|
15
|
+
return template(values);
|
|
16
|
+
}
|
|
17
|
+
return _.template(template)(values);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function renderVue(name, data, noScript = '') {
|
|
21
|
+
const options = { webapp: app, isOffline, ...data };
|
|
22
|
+
|
|
23
|
+
const vueUri = getResourceUri('vue/index.js');
|
|
24
|
+
const html = `<!-- Generated by WebappUtil.renderVue() -->
|
|
25
|
+
<div data-vue-name="${name}" data-vue-portlet="${app.portletId}">${noScript}</div>
|
|
26
|
+
<script src="${vueUri}"></script>
|
|
27
|
+
<script type="text/javascript">
|
|
28
|
+
vueApps['${name}'].render('${name}', '${app.portletId}', ${JSON.stringify(options)});
|
|
29
|
+
</script>`;
|
|
30
|
+
return html;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getRouteUri(route) {
|
|
34
|
+
const currentPortlet = PortletContextUtil.getCurrentPortlet();
|
|
35
|
+
if (currentPortlet) {
|
|
36
|
+
const currentPortletId = currentPortlet.getIdentifier();
|
|
37
|
+
const currentPageUri = PropertyUtil.getString(PortletContextUtil.getCurrentPage(), 'URI');
|
|
38
|
+
return `${currentPageUri}?sv.${currentPortletId}.route=${encodeURI(route)}&sv.target=${currentPortletId}`;
|
|
39
|
+
}
|
|
40
|
+
return '';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export default {
|
|
44
|
+
getRouteUri,
|
|
45
|
+
getResourceUri,
|
|
46
|
+
renderVue,
|
|
47
|
+
renderTemplate,
|
|
48
|
+
isOffline,
|
|
49
|
+
};
|