Skip to content

Commit 90ecbe9

Browse files
committed
Initial version 2.0
1 parent 88d83f0 commit 90ecbe9

8 files changed

Lines changed: 41 additions & 17 deletions

File tree

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
## JavaScriptSnippet
2-
[Insert custom JavaScript to your page.]
2+
3+
Insert custom JavaScript to your Mendix pages, taking object attributes as input parameters.
34

45
## Features
5-
[feature highlights]
6+
7+
Insert JavaScript code which is executed when loading the widget.
8+
9+
Optionally add one or more attributes which can be used inside the JavaScript code.
10+
11+
Optionally for the attribute if it appears to be empty: fill an empty value.
612

713
## Usage
8-
[step by step instructions]
914

10-
## Demo project
11-
[link to sandbox]
15+
Place the widget anywhere (relevant) on the Mendix page. Insert JavaScript code that you want to run there.
16+
17+
Add attribute(s) that you may need inside the code.
18+
19+
Link used attributes inside the JavaScript code by using the `${VariableName}`.
1220

1321
## Issues, suggestions and feature requests
14-
[link to GitHub issues]
22+
23+
[link to GitHub issues](https://github.com/mrgroen/JavaScript-Snippet/issues)
1524

1625
## Development and contribution
1726

18-
1. Install NPM package dependencies by using: `npm install`. If you use NPM v7.x.x, which can be checked by executing `npm -v`, execute: `npm install --legacy-peer-deps`.
27+
1. Install NPM package dependencies by using: `npm install`. If you use NPM v7.x.x, which can be checked by executing
28+
`npm -v`, execute: `npm install --legacy-peer-deps`.
1929
1. Run `npm start` to watch for code changes. On every change:
2030
- the widget will be bundled;
2131
- the bundle will be included in a `dist` folder in the root directory of the project;
2232
- the bundle will be included in the `deployment` and `widgets` folder of the Mendix test project.
23-
24-
[specify contribution]

images/avatar.jpg

20.6 KB
Loading

images/screen1.png

25.9 KB
Loading

images/screen2.png

74.5 KB
Loading

images/screen3.png

99.6 KB
Loading

images/screen4.png

68.9 KB
Loading

src/JavaScriptSnippet.jsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import "./ui/JavaScriptSnippet.css";
22
import { createElement, useEffect, useState } from "react";
33

4-
export function JavaScriptSnippet({ attributeList, jsCode }) {
4+
export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
55
const self = this;
66
const [canRender, setCanRender] = useState(false);
77
const [javaScriptString, setJavaScriptString] = useState([]);
8+
const widgetName = rest.name || "";
89

910
function escape(htmlStr) {
1011
return htmlStr
@@ -26,7 +27,7 @@ export function JavaScriptSnippet({ attributeList, jsCode }) {
2627

2728
useEffect(() => {
2829
let JavaScriptStringArray = jsCode;
29-
if (attributeList) {
30+
if (attributeList.length) {
3031
attributeList.map(attr => {
3132
if (attr.jsAttribute.status === "available") {
3233
if (typeof attr.jsAttribute.value === "boolean") {
@@ -38,12 +39,23 @@ export function JavaScriptSnippet({ attributeList, jsCode }) {
3839
escape(attr.jsAttribute.value)
3940
);
4041
}
41-
return setJavaScriptString(JavaScriptStringArray);
42-
} else return null;
42+
}
43+
return null;
4344
});
4445
}
45-
setCanRender(true);
46-
}, [javaScriptString, attributeList, jsCode]);
46+
setJavaScriptString(JavaScriptStringArray);
47+
48+
if (!attributeList.length) {
49+
setCanRender(true);
50+
}
51+
52+
// clean-up function
53+
return () => {
54+
if (attributeList.length) {
55+
setCanRender(true);
56+
}
57+
};
58+
}, [attributeList, jsCode]);
4759

4860
if (canRender) {
4961
// JavaScript evaluation will be done in the context of the widget instance.
@@ -57,7 +69,11 @@ export function JavaScriptSnippet({ attributeList, jsCode }) {
5769
return null;
5870
} catch (error) {
5971
console.warn("Error while evaluating javascript input.");
60-
return <div className="alert alert-danger">Error while evaluating javascript input: {error}</div>;
72+
return (
73+
<div name={widgetName} className="alert alert-danger">
74+
Error while evaluating javascript input: {error}
75+
</div>
76+
);
6177
}
6278
} else return null;
6379
}

src/JavaScriptSnippet.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<propertyGroup caption="General">
1212
<property key="jsCode" type="string" multiline="true" required="false">
1313
<caption>JavaScript code</caption>
14-
<description/>
14+
<description>Note: special characters ( &amp; &#38; &gt; &lt; &#39; &#34; ) will be formatted.</description>
1515
</property>
1616
</propertyGroup>
1717
<propertyGroup caption="Attributes">

0 commit comments

Comments
 (0)