@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/src/index.js DELETED
@@ -1,162 +0,0 @@
1
- import PortletContextUtil from 'PortletContextUtil';
2
- import PropertyUtil from 'PropertyUtil';
3
- import VersionUtil from 'VersionUtil';
4
- import appInfo from 'appInfo';
5
-
6
- /* Underscore is provided by SiteVision */
7
- /* eslint-disable-next-line import/no-extraneous-dependencies */
8
- import _ from 'underscore';
9
-
10
- /** If the webapp is running in offline mode or not. */
11
- export const isOffline = VersionUtil.getCurrentVersion() === VersionUtil.OFFLINE_VERSION;
12
- /** If the webapp is running in online mode or not. */
13
- export const isOnline = !isOffline;
14
-
15
- const currentPortlet = PortletContextUtil.getCurrentPortlet();
16
-
17
- const getUniqueId = () => {
18
- const id = (currentPortlet ? currentPortlet.getIdentifier() : appInfo['jcr:uuid']).replace('.', '_');
19
- const decoratedNode = PortletContextUtil.getCurrentDecoratedNode();
20
-
21
- if (decoratedNode) {
22
- return `${decoratedNode.getIdentifier().replace('.', '_')}_${id}`;
23
- }
24
- return id;
25
- };
26
-
27
-
28
- export const uniqueId = getUniqueId();
29
-
30
- /**
31
- * Get URI for a resource.
32
- * @param {String} resource A resource.
33
- * @returns {String} URI for a resource.
34
- */
35
- export function getResourceUri(resource) {
36
- return `/webapp-files/${appInfo.appIdentifier}/${appInfo.appVersion}/${resource}`;
37
- }
38
-
39
- /**
40
- * Renders a Underscore template and returns a string.
41
- * @param {String} template Underscore template.
42
- * @param {Object} [values={}] Values.
43
- * @returns {String} Rendered template
44
- */
45
- export function renderTemplate(template, values = {}) {
46
- if (typeof template === 'function') {
47
- return template({ ...values, renderTemplate });
48
- }
49
- return _.template(template)(({ ...values, renderTemplate }));
50
- }
51
-
52
- /**
53
- * Get a HTML string for rendering an application.
54
- * @param {Svelte} [App] Svelte application.
55
- * @param {Object} [data={}] Server config or data that will be available in the attribute.
56
- * `data-app` on currentScript. In Vue it will also be available in the `$options` object.
57
- * @param {Object} [settings={}] Settings object.
58
- * @param {String} [settings.noScript=''] HTML that will be rendered when JavaScript
59
- * is not available.
60
- * @param {String} [settings.selector=`[data-portlet-id="${portletId}"]`] Query selector for
61
- * where the app should be mounted.
62
- * @param {Boolean} [settings.async=false] If the app script should be loaded asynchronously.
63
- * [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
64
- * @param {Boolean} [settings.defer=true] If the app script should be loaded after DOM is ready.
65
- * [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
66
- * @param {Object} [settings.req] The req object from SiteVision, pass this to optimize browser
67
- * specific script loading if you have multiple instances of the app.
68
- * @returns {String} HTML for rendering an application.
69
- */
70
- export function renderApp(data, {
71
- noScript = '',
72
- html = '',
73
- selector = `[data-portlet-id="${uniqueId}"]`,
74
- async = false,
75
- defer = true,
76
- req,
77
- } = {}) {
78
- if (typeof data === 'string') {
79
- throw new Error('As of version 2 renderApp no longer needs the application name.');
80
- }
81
-
82
- const isIE = () => {
83
- if (!req) return true;
84
- const userAgent = req.header('user-agent');
85
- return /Trident\/|MSIE/.test(userAgent);
86
- };
87
-
88
- const getHtml = () => (html || (noScript ? `<noscript>${noScript}</noscript>` : ''));
89
-
90
- const appData = {
91
- ...data, isOffline, isOnline, uniqueId,
92
- };
93
-
94
- if (isOffline) {
95
- return `
96
- <div data-portlet-id="${uniqueId}">${getHtml()}</div>
97
- <script>
98
- window.svDocReady(function() {
99
- var targetElement = document.querySelector('[data-portlet-id="${uniqueId}"]');
100
- var script = document.createElement("script");
101
- script.src = "${getResourceUri('client/index.js')}?${appInfo.appImportDate}${uniqueId.replace('_', '')}";
102
- script.setAttribute("data-app", "${encodeURIComponent(JSON.stringify(appData))}");
103
- script.setAttribute("data-selector", "${encodeURIComponent(selector)}");
104
- targetElement.parentNode.insertBefore(script, targetElement.nextSibling);
105
- });
106
- </script>`;
107
- }
108
- return `
109
- <div data-portlet-id="${uniqueId}">${getHtml()}</div>
110
- <script
111
- src="${getResourceUri('client/index.js')}?${appInfo.appImportDate}${isIE() ? uniqueId.replace('_', '') : ''}"
112
- data-app="${encodeURIComponent(JSON.stringify(appData))}"
113
- data-selector="${encodeURIComponent(selector)}"
114
- ${async && !defer ? 'async' : ''}
115
- ${defer ? 'defer' : ''}>
116
- </script>`;
117
- }
118
-
119
- /**
120
- * Get URI for a route, same as `getStandaloneUrl` in SiteVision template.
121
- * @param {String} route A route.
122
- * @returns {String} URI for route.
123
- */
124
- export function getRouteUri(route) {
125
- const currentPage = PortletContextUtil.getCurrentPage();
126
- if (currentPage && currentPortlet) {
127
- const currentPageId = currentPage.getIdentifier().replace('_sitePage', '');
128
- const currentPortletId = currentPortlet.getIdentifier();
129
- return `/appresource/${currentPageId}/${currentPortletId}/${route}`.replace(/\/\//g, '/');
130
- }
131
- return '/';
132
- }
133
-
134
- /**
135
- * Get URI for a view, same as `getUrl` in SiteVision template.
136
- * @param {String} route A route.
137
- * @returns {String} URI for view.
138
- */
139
- export function getViewUri(route) {
140
- const currentPage = PortletContextUtil.getCurrentPage();
141
- if (currentPage && currentPortlet) {
142
- const currentPageId = currentPage.getIdentifier().replace('_sitePage', '');
143
- const currentPageUri = PropertyUtil.getString(currentPage, 'URI');
144
- const currentPortletId = PortletContextUtil.getCurrentPortlet().getIdentifier();
145
- if (isOffline) {
146
- return `/edit-offline/${currentPageId}?sv.target=${currentPortletId}&sv.${currentPortletId}.route=${encodeURI(route)}`;
147
- }
148
- return `${currentPageUri}?sv.target=${currentPortletId}&sv.${currentPortletId}.route=${encodeURI(route)}`;
149
- }
150
- const addonId = appInfo['jcr:uuid'];
151
- return `/edit-web-app-offline/${addonId}?sv.target=${addonId}&sv.${addonId}.route=${encodeURI(route)}`;
152
- }
153
-
154
- export default {
155
- getRouteUri,
156
- getViewUri,
157
- getResourceUri,
158
- renderApp,
159
- renderTemplate,
160
- isOffline,
161
- isOnline,
162
- };
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "@soleil/eslint-config-sitevision/client",
3
- "root": true
4
- }
@@ -1,26 +0,0 @@
1
- import appResource from 'appResource';
2
- import { setAppData } from '../app-data';
3
- import {
4
- renderApp, isOffline, isOnline, uniqueId,
5
- } from '../src';
6
-
7
- export function renderClient(data, settings) {
8
- return renderApp(data, settings);
9
- }
10
-
11
- export function renderServer(App, data) {
12
- const appData = {
13
- ...data, isOffline, isOnline, uniqueId,
14
- };
15
- setAppData(appData);
16
- const { html } = App.render(appData);
17
- return html;
18
- }
19
-
20
- export function render(App, data, settings) {
21
- const html = renderServer(App, data);
22
- if (appResource.getNode('client/index.js')) {
23
- return renderClient(data, { html, ...settings });
24
- }
25
- return html;
26
- }
File without changes