@@ -8,11 +8,19 @@ define([
8
8
"dojo/_base/lang" ,
9
9
"dojo/html" ,
10
10
"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
+ ) {
12
21
"use strict" ;
13
22
14
23
return declare ( "HTMLSnippet.widget.HTMLSnippet" , [ _WidgetBase ] , {
15
-
16
24
// Set in Modeler
17
25
contenttype : "html" ,
18
26
contents : "" ,
@@ -43,20 +51,22 @@ define([
43
51
case "html" :
44
52
if ( external ) {
45
53
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 ( ) ;
53
63
} else if ( ! this . encloseHTMLWithDiv ) {
54
64
html . set ( this . domNode , this . contents ) ;
55
65
} else {
56
66
domStyle . set ( this . domNode , {
57
- " height" : "auto" ,
58
- " width" : "100%" ,
59
- " outline" : 0
67
+ height : "auto" ,
68
+ width : "100%" ,
69
+ outline : 0
60
70
} ) ;
61
71
62
72
domAttr . set ( this . domNode , "style" , this . style ) ; // might override height and width
@@ -74,14 +84,13 @@ define([
74
84
intDate = + new Date ( ) ;
75
85
76
86
scriptNode . type = "text/javascript" ;
77
- scriptNode . src = this . contentsPath + "?v=" + intDate . toString ( ) ;
87
+ scriptNode . src =
88
+ this . contentsPath + "?v=" + intDate . toString ( ) ;
78
89
79
90
domConstruct . place ( scriptNode , this . domNode , "only" ) ;
80
91
} else {
81
92
if ( this . contenttype === "jsjQuery" ) {
82
- require ( [
83
- "HTMLSnippet/lib/jquery-3.3.1"
84
- ] , lang . hitch ( this , this . evalJs ) ) ;
93
+ this . _evalJQueryCode ( ) ;
85
94
} else {
86
95
this . evalJs ( ) ;
87
96
}
@@ -117,7 +126,11 @@ define([
117
126
_setupEvents : function ( ) {
118
127
logger . debug ( this . id + "._setupEvents" ) ;
119
128
if ( this . onclickmf ) {
120
- this . connect ( this . domNode , "click" , this . _executeMicroflow ) ;
129
+ this . connect (
130
+ this . domNode ,
131
+ "click" ,
132
+ this . _executeMicroflow
133
+ ) ;
121
134
}
122
135
} ,
123
136
@@ -129,27 +142,76 @@ define([
129
142
params . applyto = "selection" ;
130
143
params . guids = [ this . contextObj . getGuid ( ) ] ;
131
144
}
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
+ }
136
156
} ,
137
- error : function ( error ) {
138
- logger . error ( this . id + error ) ;
139
- }
140
- } , this ) ;
157
+ this
158
+ ) ;
141
159
}
142
160
} ,
143
161
144
162
evalJs : function ( ) {
145
163
logger . debug ( this . id + ".evalJS" ) ;
146
164
try {
147
165
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 ) ;
150
168
}
151
169
} ,
152
170
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 = / w i n d o w .\j Q u e r y / g;
186
+ var jqueryIdRegex2 = / w i n d o w .\$ / 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
+
153
215
_executeCallback : function ( cb , from ) {
154
216
logger . debug ( this . id + "._executeCallback" + ( from ? " from " + from : "" ) ) ;
155
217
if ( cb && typeof cb === "function" ) {
0 commit comments