Skip to content

Commit 06ed415

Browse files
author
Osama Najjar
committed
wrap Jquery code in its own context
1 parent 7e808e5 commit 06ed415

File tree

1 file changed

+89
-27
lines changed

1 file changed

+89
-27
lines changed

src/HTMLSnippet/widget/HTMLSnippet.js

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ define([
88
"dojo/_base/lang",
99
"dojo/html",
1010
"dijit/layout/LinkPane"
11-
], function (declare, _WidgetBase, domStyle, domAttr, domConstruct, lang, html, LinkPane) {
11+
], function (
12+
declare,
13+
_WidgetBase,
14+
domStyle,
15+
domAttr,
16+
domConstruct,
17+
lang,
18+
html,
19+
LinkPane
20+
) {
1221
"use strict";
1322

1423
return declare("HTMLSnippet.widget.HTMLSnippet", [_WidgetBase], {
15-
1624
// Set in Modeler
1725
contenttype: "html",
1826
contents: "",
@@ -43,20 +51,22 @@ define([
4351
case "html":
4452
if (external) {
4553
new LinkPane({
46-
preload: true,
47-
loadingMessage: "",
48-
href: this.contentsPath,
49-
onDownloadError: function () {
50-
console.log("Error loading html path");
51-
}
52-
}).placeAt(this.domNode.id).startup();
54+
preload: true,
55+
loadingMessage: "",
56+
href: this.contentsPath,
57+
onDownloadError: function () {
58+
console.log("Error loading html path");
59+
}
60+
})
61+
.placeAt(this.domNode.id)
62+
.startup();
5363
} else if (!this.encloseHTMLWithDiv) {
5464
html.set(this.domNode, this.contents);
5565
} else {
5666
domStyle.set(this.domNode, {
57-
"height": "auto",
58-
"width": "100%",
59-
"outline": 0
67+
height: "auto",
68+
width: "100%",
69+
outline: 0
6070
});
6171

6272
domAttr.set(this.domNode, "style", this.style); // might override height and width
@@ -74,14 +84,13 @@ define([
7484
intDate = +new Date();
7585

7686
scriptNode.type = "text/javascript";
77-
scriptNode.src = this.contentsPath + "?v=" + intDate.toString();
87+
scriptNode.src =
88+
this.contentsPath + "?v=" + intDate.toString();
7889

7990
domConstruct.place(scriptNode, this.domNode, "only");
8091
} else {
8192
if (this.contenttype === "jsjQuery") {
82-
require([
83-
"HTMLSnippet/lib/jquery-3.3.1"
84-
], lang.hitch(this, this.evalJs));
93+
this._evalJQueryCode();
8594
} else {
8695
this.evalJs();
8796
}
@@ -117,7 +126,11 @@ define([
117126
_setupEvents: function () {
118127
logger.debug(this.id + "._setupEvents");
119128
if (this.onclickmf) {
120-
this.connect(this.domNode, "click", this._executeMicroflow);
129+
this.connect(
130+
this.domNode,
131+
"click",
132+
this._executeMicroflow
133+
);
121134
}
122135
},
123136

@@ -129,27 +142,76 @@ define([
129142
params.applyto = "selection";
130143
params.guids = [this.contextObj.getGuid()];
131144
}
132-
mx.ui.action(this.onclickmf, {
133-
params: params,
134-
callback: function (obj) {
135-
logger.debug(this.id + " (executed microflow successfully).");
145+
mx.ui.action(
146+
this.onclickmf, {
147+
params: params,
148+
callback: function (obj) {
149+
logger.debug(
150+
this.id + " (executed microflow successfully)."
151+
);
152+
},
153+
error: function (error) {
154+
logger.error(this.id + error);
155+
}
136156
},
137-
error: function (error) {
138-
logger.error(this.id + error);
139-
}
140-
}, this);
157+
this
158+
);
141159
}
142160
},
143161

144162
evalJs: function () {
145163
logger.debug(this.id + ".evalJS");
146164
try {
147165
eval(this.contents + "\r\n//# sourceURL=" + this.id + ".js");
148-
} catch (e) {
149-
domConstruct.place("<div class=\"alert alert-danger\">Error while evaluating javascript input: " + e + "</div>", this.domNode, "only");
166+
} catch (error) {
167+
this._handleError(error);
150168
}
151169
},
152170

171+
_evalJQueryCode: function () {
172+
logger.debug(this.id + "._evalJQueryCode");
173+
/*** load jQuery ***/
174+
require(["HTMLSnippet/lib/jquery-3.3.1"], lang.hitch(this, function (jquery_3_3_1) {
175+
try {
176+
177+
(function (snippetCode) {
178+
179+
/**
180+
* user's are get used to or might expect to have jQuery available globaly
181+
* and they will write their code according to that, and since we, in this widget, don't expose
182+
* jQuery globaly, we'll check user's code snippet if there is any attemption to access jQuery
183+
* from the global scope ( window ).
184+
*/
185+
var jqueryIdRegex1 = /window.\jQuery/g;
186+
var jqueryIdRegex2 = /window.\$/g;
187+
snippetCode = snippetCode.replace(jqueryIdRegex1, 'jQuery');
188+
snippetCode = snippetCode.replace(jqueryIdRegex2, '$');
189+
190+
snippetCode = "var jQuery, $; jQuery = $ = this.jquery;" + // make this jQuery version only accessible and availabe in the scope of this anonymous function
191+
snippetCode +
192+
"console.debug('your code snippet is evaluated and executed against JQuery version:'+ this.jquery.fn.jquery);";
193+
eval(snippetCode);
194+
}).call({
195+
jquery: jquery_3_3_1 // pass JQuery as the context of the immediate function which will wrap the code snippet
196+
}, this.contents); // pass the code snippet as an arg
197+
} catch (error) {
198+
this._handleError(error);
199+
}
200+
}));
201+
},
202+
203+
204+
_handleError: function (error) {
205+
logger.debug(this.id + "._handleError");
206+
domConstruct.place(
207+
'<div class="alert alert-danger">Error while evaluating javascript input: ' +
208+
error +
209+
"</div>",
210+
this.domNode,
211+
"only"
212+
);
213+
},
214+
153215
_executeCallback: function (cb, from) {
154216
logger.debug(this.id + "._executeCallback" + (from ? " from " + from : ""));
155217
if (cb && typeof cb === "function") {

0 commit comments

Comments
 (0)