Skip to content

Commit b373c8d

Browse files
committed
Merge pull request #2 from t-fitz/add-external-files
Add the ability to use external html and javascript files
2 parents f508fd5 + eb7f808 commit b373c8d

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

src/HTMLSnippet/HTMLSnippet.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</icon>
88

99
<properties>
10+
11+
<!-- content type -->
1012
<property key="contenttype" type="enumeration" defaultValue="html">
1113
<caption>Content Type</caption>
1214
<category>Behavior</category>
@@ -17,11 +19,22 @@
1719
<enumerationValue key="js">JavaScript</enumerationValue>
1820
</enumerationValues>
1921
</property>
20-
<property key="contents" type="translatableString" multiline="true">
22+
23+
<!-- html/js string -->
24+
<property key="contents" type="translatableString" multiline="true" required="false">
2125
<caption>Contents</caption>
2226
<category>Behavior</category>
2327
<description>The HTML or Javascript to embed.</description>
2428
</property>
29+
30+
<!-- html/js file path -->
31+
<property key="contentsPath" type="string" required="false">
32+
<caption>External File</caption>
33+
<category>Behavior</category>
34+
<description>The path to the HTML or Javascript you want to add. The root folder is theme folder. Will override the Contents if used.</description>
35+
</property>
36+
37+
<!-- description of what the html/js should do -->
2538
<property key="documentation" type="string" multiline="true" required="false">
2639
<caption>Documentation</caption>
2740
<category>Common</category>

src/HTMLSnippet/widget/HTMLSnippet.js

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
require([
55
'dojo/_base/declare', 'mxui/widget/_WidgetBase',
6-
'mxui/dom', 'dojo/dom-style', 'dojo/dom-attr', 'dojo/html'
7-
], function (declare, _WidgetBase, dom, domStyle, domAttr, html) {
6+
'mxui/dom', 'dojo/dom-style', 'dojo/dom-attr', 'dojo/dom-construct', 'dijit/layout/LinkPane'
7+
], function (declare, _WidgetBase, dom, domStyle, domAttr, domConstruct, linkPane) {
88

99
'use strict';
1010

@@ -18,23 +18,54 @@ require([
1818
postCreate: function () {
1919
console.log(this.id + '.postCreate');
2020

21-
switch (this.contenttype) {
21+
var external = this.contentsPath != '' ? true : false;
22+
23+
switch (this.contenttype) {
2224
case 'html':
23-
domStyle.set(this.domNode, {
24-
'height': 'auto',
25-
'width': '100%',
26-
'outline': 0
27-
});
2825

29-
domAttr.set(this.domNode, 'style', this.style); //might override height and width
30-
html.set(this.domNode, this.contents);
26+
if (external)
27+
{
28+
new linkPane(
29+
{
30+
preload: true,
31+
loadingMessage: "",
32+
href: this.contentsPath,
33+
onDownloadError: function(){ console.log("Error loading html path");}
34+
}).placeAt(this.domNode.id).startup();
35+
}
36+
else
37+
{
38+
domStyle.set(this.domNode, {
39+
'height': 'auto',
40+
'width': '100%',
41+
'outline': 0
42+
});
43+
44+
domAttr.set(this.domNode, 'style', this.style); // might override height and width
45+
domConstruct.place(this.contents, this.domNode, "only");
46+
}
47+
3148
break;
3249
case 'js':
33-
try {
34-
eval(this.contents);
35-
} catch (e) {
36-
html.set(this.domNode, "Error while evaluating JavaScript: " + e);
37-
}
50+
51+
if (external)
52+
{
53+
var scriptNode = document.createElement("script"),
54+
intDate = +new Date();
55+
56+
scriptNode.type = "text/javascript";
57+
scriptNode.src = this.contentsPath + "?v=" + intDate.toString();
58+
59+
domConstruct.place(scriptNode, this.domNode, "only");
60+
}
61+
else
62+
{
63+
try {
64+
eval(this.contents);
65+
} catch (e) {
66+
domConstruct.place("Error while evaluating JavaScript: " + e, this.domNode, "only");
67+
}
68+
}
3869
break;
3970
}
4071
}

0 commit comments

Comments
 (0)