@soleil-se/app-util 2.0.0 → 2.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,39 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [2.1.0] - 2020-02-14
8
+ ### Added
9
+ - App data import in client, `@soleil-api/webapp-util/app-data`.
10
+ - `getViewUri` to get the URI that also renders the page.
11
+ - `isOnline` to see if the app is online.
12
+
13
+ ### Changed
14
+ - `vue` is now an optional depedency.
15
+
16
+ ## [2.0.0] - 2020-02-12
17
+ ### Changed
18
+ - Now using `currentScript` to reference the script element the app is running in to pass data when using `renderApp`.
19
+ - `renderApp` is no longer using appName.
20
+ - `render` is now called directly in `./app_src/client/index.js`.
21
+ - `render` and `App` no longer needs to be exported from `./app_src/client/index.js`.
22
+
23
+ ## [1.2.4] - 2020-12-12
24
+ ### Fixed
25
+ - `TypeError: Cannot call method getIdentifier of null` when app is viewed in Addons.
26
+
27
+ ## [1.2.2] - 2019-05-20
28
+ ### Fixed
29
+ - Removed `_sitePage` from `currentPageId` in `getRouteUri`.
30
+
31
+ ## [1.2.1] - 2019-05-09
32
+ ### Added
33
+ - Added timestamp to WebApp script tag to prevent cache when uploading a new version.
34
+
35
+ ## [1.2.0] - 2019-05-07
36
+ ### Changed
37
+ - `getRouteUri` now returns the standalone route.
38
+ - HTML comment to not include this.name in `renderApp`.
39
+
package/README.md CHANGED
@@ -3,6 +3,9 @@ Utility functions for Webapps.
3
3
  ## Install
4
4
  `yarn add @soleil-se/webapp-util`
5
5
 
6
+ ## Changelog
7
+ [See changelog](CHANGELOG.md).
8
+
6
9
  ## Migration 1.x.x to 2.x.x
7
10
 
8
11
  Now using `currentScript` to reference the script element the app is running in to pass data.
@@ -56,19 +59,55 @@ import App from './App.vue';
56
59
 
57
60
  render(App);
58
61
  ```
59
- ### `getRouteUri(route)` ⇒ `String`
60
- Get URI for a route.
61
62
 
62
- **Returns**: `String` - URI for route.
63
+ ##### **App data**
64
+ If you need to use the data that is available for the App you can use `@soleil-se/webapp-util/app-data`.
65
+
66
+ For example when you need a route URI from the server:
67
+ ```javascript
68
+ import AppData from '@soleil-se/webapp-util/app-data';
69
+ import superagent from 'superagent';
70
+
71
+ const searchThings = async (query) => {
72
+ const { body } = await superagent
73
+ .get(AppData.searchRoute)
74
+ .query({ query });
75
+ return body;
76
+ };
77
+
78
+ export default searchThings;
79
+ ```
80
+
81
+ ## `getRouteUri(route)` ⇒ `String`
82
+ Get URI for a route, same as `getStandaloneUrl` in SiteVision template.
83
+ https://developer.sitevision.se/docs/webapps/template#h-Methods
84
+
85
+ **Returns**: <code>String</code> - URI for route.
63
86
 
64
87
  | Param | Type | Description |
65
88
  | --- | --- | --- |
66
- | route | `String` | A route. |
89
+ | route | <code>String</code> | A route. |
90
+
67
91
 
68
92
  #### Example
69
93
  ```javascript
70
94
  const routeUri = getRouteUri('/my-route');
71
95
  ```
96
+ ## `getViewUri(route)` ⇒ `String`
97
+ Get URI for a view, same as `getUrl` in SiteVision template.
98
+ https://developer.sitevision.se/docs/webapps/template#h-Methods
99
+
100
+ **Returns**: <code>String</code> - URI for view.
101
+
102
+ | Param | Type | Description |
103
+ | --- | --- | --- |
104
+ | route | <code>String</code> | A route. |
105
+
106
+ #### Example
107
+ ```javascript
108
+ const viewUri = getViewUri('/my-route');
109
+ ```
110
+
72
111
  ### `getResourceUri(resource)` ⇒ `String`
73
112
  Get URI for a resource.
74
113
 
@@ -78,6 +117,7 @@ Get URI for a resource.
78
117
  | --- | --- | --- |
79
118
  | resource | `String` | A resource. |
80
119
 
120
+
81
121
  #### Example
82
122
  ```javascript
83
123
  const resourceUri = getResourceUri('file/in/resource.png');
@@ -106,3 +146,12 @@ if(isOffline) {
106
146
  // Do something
107
147
  }
108
148
  ```
149
+
150
+ ### `isOnline`
151
+ If the webapp is running in online mode or not.
152
+
153
+ ```javascript
154
+ if(isOnline) {
155
+ // Do something
156
+ }
157
+ ```
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "@soleil/eslint-config-sitevision/client",
3
+ "root": true
4
+ }
@@ -0,0 +1,7 @@
1
+ import { parseAttribute } from '../attribute-util';
2
+
3
+ /**
4
+ * Get appData from the currentScript element.
5
+ * @returns {Object} AppData.
6
+ */
7
+ export default parseAttribute('data-app');
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "@soleil/eslint-config-sitevision/client",
3
+ "root": true
4
+ }
@@ -0,0 +1,22 @@
1
+ const { currentScript } = document;
2
+ /**
3
+ * JSON decode an attribute on the currentScript element.
4
+ * Use if attribute contains a JSON-object.
5
+ * @param {String} attribute Attribute to decode.
6
+ * @returns {Object} Decoded JSON object.
7
+ */
8
+ export const decodeAttribute = (attribute) => {
9
+ const encoded = currentScript.getAttribute(attribute);
10
+ if (encoded) {
11
+ return decodeURIComponent(encoded);
12
+ }
13
+ return undefined;
14
+ };
15
+
16
+ /**
17
+ * Parse an attribute on the currentScript element.
18
+ * Us if the attribute is a String.
19
+ * @param {String} attribute
20
+ * @returns {String} Parsed attribute value.
21
+ */
22
+ export const parseAttribute = (attribute) => JSON.parse(decodeAttribute(attribute));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soleil-se/app-util",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Utility functions for Webapps.",
5
5
  "main": "./src/index.js",
6
6
  "author": "Soleil AB",
@@ -14,5 +14,8 @@
14
14
  "dependencies": {
15
15
  "current-script-polyfill": "^1.0.0"
16
16
  },
17
+ "optionalDependencies": {
18
+ "vue": "^2.6.11"
19
+ },
17
20
  "devDependencies": {}
18
21
  }
@@ -1,3 +1,4 @@
1
1
  {
2
- "extends": "@soleil/eslint-config-sitevision/client"
2
+ "extends": "@soleil/eslint-config-sitevision/client",
3
+ "root": true
3
4
  }
@@ -1,17 +1,8 @@
1
1
  import 'current-script-polyfill';
2
2
  import Vue from 'vue';
3
3
 
4
- const { currentScript } = document;
5
-
6
- const decodeAttribute = (attribute) => {
7
- const encoded = currentScript.getAttribute(attribute);
8
- if (encoded) {
9
- return decodeURIComponent(encoded);
10
- }
11
- return undefined;
12
- };
13
-
14
- const parseAttribute = (attribute) => JSON.parse(decodeAttribute(attribute));
4
+ import { decodeAttribute } from '../attribute-util';
5
+ import appData from '../app-data';
15
6
 
16
7
  const offlineModeMixin = {
17
8
  mounted() {
@@ -32,12 +23,12 @@ const offlineModeMixin = {
32
23
  */
33
24
  export default function render(App) {
34
25
  const selector = decodeAttribute('data-selector');
35
- const options = parseAttribute('data-app');
26
+ const options = appData;
36
27
 
37
28
  Object.assign(App, options, {
38
29
  mixins: [offlineModeMixin],
39
30
  });
40
-
31
+
41
32
  const mountElementExists = !!document.querySelector(selector);
42
33
 
43
34
  if (mountElementExists) {
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import PortletContextUtil from 'PortletContextUtil';
2
+ import PropertyUtil from 'PropertyUtil';
2
3
  import VersionUtil from 'VersionUtil';
3
4
  import appInfo from 'appInfo';
4
5
  /* Underscore is provided by SiteVision */
@@ -7,8 +8,11 @@ import _ from 'underscore';
7
8
 
8
9
  /** If the webapp is running in offline mode or not. */
9
10
  export const isOffline = VersionUtil.getCurrentVersion() === VersionUtil.OFFLINE_VERSION;
11
+ /** If the webapp is running in online mode or not. */
12
+ export const isOnline = !isOffline;
10
13
 
11
- const portletId = PortletContextUtil.getCurrentPortlet().getIdentifier().replace('.', '_');
14
+ const currentPortlet = PortletContextUtil.getCurrentPortlet();
15
+ const portletId = currentPortlet ? currentPortlet.getIdentifier().replace('.', '_') : '';
12
16
 
13
17
  /**
14
18
  * Get URI for a resource.
@@ -82,7 +86,7 @@ export function renderApp(data, {
82
86
  }
83
87
 
84
88
  /**
85
- * Get URI for a route.
89
+ * Get URI for a route, same as `getStandaloneUrl` in SiteVision template.
86
90
  * @param {String} route A route.
87
91
  * @returns {String} URI for route.
88
92
  */
@@ -92,10 +96,22 @@ export function getRouteUri(route) {
92
96
  return `/appresource/${currentPageId}/${currentPortletId}/${route}`.replace(/\/\//g, '/');
93
97
  }
94
98
 
99
+ /**
100
+ * Get URI for a view, same as `getUrl` in SiteVision template.
101
+ * @param {String} route A route.
102
+ * @returns {String} URI for view.
103
+ */
104
+ export function getViewUri(route) {
105
+ const currentPageUri = PropertyUtil.getString(PortletContextUtil.getCurrentPage(), 'URI');
106
+ return `${currentPageUri}?sv.${portletId}.route=${encodeURI(route)}&sv.target=${portletId}`;
107
+ }
108
+
95
109
  export default {
96
110
  getRouteUri,
111
+ getViewUri,
97
112
  getResourceUri,
98
113
  renderApp,
99
114
  renderTemplate,
100
115
  isOffline,
116
+ isOnline,
101
117
  };