Skip to content

Commit 504e8c2

Browse files
committed
Add a way to ref widget element (this)
1 parent eb77f1b commit 504e8c2

7 files changed

Lines changed: 31 additions & 23 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Add attribute(s) that you may need inside the code.
2020

2121
Link used attributes inside the JavaScript code by using the `${VariableName}`.
2222

23+
Whenever using the `this` string inside the widget, it will be replaced by the widget unique classname.
24+
2325
## Issues, suggestions and feature requests
2426

2527
[link to GitHub issues](https://github.com/mrgroen/JavaScript-Snippet/issues)

images/screen2.png

13.3 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "javascriptsnippet",
33
"widgetName": "JavaScriptSnippet",
4-
"version": "2.0.0",
4+
"version": "2.1.0",
55
"description": "Insert custom JavaScript to your page.",
66
"copyright": "2022 Incentro",
77
"author": "Rob Duits",

src/JavaScriptSnippet.editorConfig.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@
4646
* @param {("web"|"desktop")} target
4747
* @returns {Properties}
4848
*/
49-
export function getProperties(values, defaultProperties, target) {
49+
50+
// export function getProperties(values, defaultProperties, target) {
5051
// Do the values manipulation here to control the visibility of properties in Studio and Studio Pro conditionally.
5152
/* Example
5253
if (values.myProperty === "custom") {
5354
delete defaultProperties.properties.myOtherProperty;
5455
}
5556
*/
56-
return defaultProperties;
57-
}
57+
// }
5858

5959
// /**
6060
// * @param {Object} values

src/JavaScriptSnippet.jsx

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

44
export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
55
const [canRender, setCanRender] = useState(false);
66
const [javaScriptString, setJavaScriptString] = useState([]);
77
const widgetName = rest.name || "";
8+
const uid = useState((Date.now().toString(36) + Math.random().toString(36).substring(2)));
89

910
function escape(htmlStr) {
1011
return htmlStr
@@ -13,7 +14,7 @@ export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
1314
.replace(/>/g, ">")
1415
.replace(/"/g, """)
1516
.replace(/'/g, "'");
16-
}
17+
};
1718

1819
// function unEscape(htmlStr) {
1920
// htmlStr = htmlStr.replace(/&lt;/g, "<");
@@ -53,6 +54,9 @@ export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
5354
}
5455
return null;
5556
});
57+
58+
JSArray = JSArray.split("this").join(`'${widgetName}_${uid[0]}'`);
59+
setJavaScriptString(JSArray);
5660
}
5761
setJavaScriptString(JSArray);
5862

@@ -74,10 +78,9 @@ export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
7478
try {
7579
// eslint-disable-next-line no-new-func
7680
Function(javaScriptString)();
77-
return null;
7881
} catch (error) {
79-
console.warn(`${widgetName}: Error while evaluating javascript input.
80-
${error}`);
82+
console.warn(`${widgetName}: Error while evaluating javascript input. ${error}`);
8183
}
82-
} else return null;
84+
}
85+
return <div className={`${widgetName}_${uid[0]}`}></div>;
8386
}

src/JavaScriptSnippet.xml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@
1717
<propertyGroup caption="General">
1818
<property key="jsCode" type="string" multiline="true" required="false">
1919
<caption>JavaScript code</caption>
20-
<description
21-
>Note: special characters ( &amp; &#38; &gt; &lt; &#39; &#34; ) will be formatted.</description>
20+
<description>Note: special characters ( &amp; &#38; &gt; &lt; &#39; &#34; ) will be formatted.
21+
22+
Whenever using the `this` string inside the widget, it will be replaced by the widget unique classname.</description>
23+
</property>
24+
<property key="tagUseRepeat" type="boolean" defaultValue="false">
25+
<caption>Repeat element</caption>
26+
<description>Repeat element for each item in data source.</description>
27+
</property>
28+
<property key="tagContentRepeatDataSource" type="datasource" required="true" isList="true">
29+
<caption>Data source</caption>
30+
<description />
2231
</property>
2332
</propertyGroup>
2433
<propertyGroup caption="Attributes">
@@ -29,17 +38,11 @@
2938
<propertyGroup caption="Object list group">
3039
<property key="jsVarName" type="string" required="true">
3140
<caption>Variable name</caption>
32-
<description>
33-
camelCaseName: Identifies the attribute value, this name should in the JavaScript
34-
Snippet code.
35-
</description>
41+
<description>camelCaseName: Identifies the attribute value, this name should in the JavaScript Snippet code.</description>
3642
</property>
3743
<property key="jsAttribute" type="attribute">
3844
<caption>Attribute</caption>
39-
<description>
40-
Value of this attribute will be used to replace ${camelCaseName} and passed as a string
41-
(also Boolean attributes).
42-
</description>
45+
<description>Value of this attribute will be used to replace ${camelCaseName} and passed as a string (also Boolean attributes).</description>
4346
<attributeTypes>
4447
<attributeType name="Boolean" />
4548
<attributeType name="DateTime" />

src/package.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<package xmlns="http://www.mendix.com/package/1.0/">
3-
<clientModule name="JavaScriptSnippet" version="2.0.0" xmlns="http://www.mendix.com/clientModule/1.0/">
3+
<clientModule name="JavaScriptSnippet" version="2.1.0" xmlns="http://www.mendix.com/clientModule/1.0/">
44
<widgetFiles>
5-
<widgetFile path="JavaScriptSnippet.xml"/>
5+
<widgetFile path="JavaScriptSnippet.xml" />
66
</widgetFiles>
77
<files>
8-
<file path="incentro/javascriptsnippet"/>
8+
<file path="incentro/javascriptsnippet" />
99
</files>
1010
</clientModule>
1111
</package>

0 commit comments

Comments
 (0)