Repository navigation
Expand file tree
/
Copy pathsyntax-error-bubble.js
More file actions
311 lines (257 loc) · 12.8 KB
/
Copy pathsyntax-error-bubble.js
File metadata and controls
311 lines (257 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/// extracted from opt-frontend.js
var activateSyntaxErrorSurvey = false; // true;
// domID is the ID of the element to attach to (without the leading '#' sign)
function SyntaxErrorSurveyBubble(parentViz, domID) {
this.parentViz = parentViz;
this.domID = domID;
this.hashID = '#' + domID;
this.my = 'left center';
this.at = 'right center';
this.qtipHidden = false; // is there a qtip object present but hidden? (TODO: kinda confusing)
}
SyntaxErrorSurveyBubble.prototype.destroyQTip = function() {
$(this.hashID).qtip('destroy');
}
SyntaxErrorSurveyBubble.prototype.redrawCodelineBubble = function() {
var myVisualizer = optCommon.getVisualizer();
if (myVisualizer.isOutputLineVisibleForBubbles(this.domID)) {
if (this.qtipHidden) {
$(this.hashID).qtip('show');
}
else {
$(this.hashID).qtip('reposition');
}
this.qtipHidden = false;
}
else {
$(this.hashID).qtip('hide');
this.qtipHidden = true;
}
}
SyntaxErrorSurveyBubble.prototype.qTipContentID = function() {
return '#ui-tooltip-' + this.domID + '-content';
}
SyntaxErrorSurveyBubble.prototype.qTipID = function() {
return '#ui-tooltip-' + this.domID;
}
// created on 2015-04-18
function experimentalPopUpSyntaxErrorSurvey() {
if (prevExecutionExceptionObjLst.length > 0) {
// work with the most recent entry
var prevExecutionExceptionObj = prevExecutionExceptionObjLst[prevExecutionExceptionObjLst.length - 1];
var offendingLine = prevExecutionExceptionObj.killerException.line;
if (offendingLine === undefined) {
return; // get out early!
}
// if we've switched languages between the previous error and this
// run, then DON'T pop up a survey since the point is moot anyhow;
// there's no point in asking the question when the language has
// changed :)
var curState = optCommon.getAppState();
if (prevExecutionExceptionObj.myAppState.py != curState.py) {
return;
}
// make sure jquery.qtip has been imported
var myVisualizer = optCommon.getVisualizer();
var codelineIDs = [];
$.each(myVisualizer.domRoot.find('#pyCodeOutput .cod'), function(i, e) {
// hacky!
var domID = $(e).attr('id');
var lineRE = new RegExp('cod' + String(offendingLine) + '$'); // $ for end-of-line match
if (lineRE.test(domID)) {
codelineIDs.push($(e).attr('id'));
}
});
// should find only 1 match, or else something is wonky, maybe
// because the code changed so much that the line number in question
// is no longer available
if (codelineIDs.length === 1) {
var codLineId = codelineIDs[0];
var bub = new SyntaxErrorSurveyBubble(myVisualizer, codLineId);
// if pyCodeOutputDiv is narrower than the current line, then
// adjust the x position of the pop-up bubble accordingly to be
// flush with the right of pyCodeOutputDiv
var pcodWidth = myVisualizer.domRoot.find('#pyCodeOutputDiv').width();
var codLineWidth = myVisualizer.domRoot.find('#' + codLineId).parent().width(); // get enclosing 'tr'
var adjustX = 0; // default
// actually nix this for now to keep things simple ...
//if (pcodWidth < codLineWidth) {
// adjustX = pcodWidth - codLineWidth; // should be negative!
//}
// destroy then create a new tip:
bub.destroyQTip();
$(bub.hashID).qtip($.extend({}, pytutor.qtipShared, {
content: ' ', // can't be empty!
id: bub.domID,
position: {
my: bub.my,
at: bub.at,
adjust: {
x: adjustX,
},
effect: null, // disable all cutesy animations
},
style: {
classes: 'ui-tooltip-pgbootstrap ui-tooltip-pgbootstrap-RED'
}
}));
// need to set both max-width and width() ...
$(bub.qTipID()).css('max-width', '350px').width('350px');
var myUuid = optCommon.supports_html5_storage() ? localStorage.getItem('opt_uuid') : '';
// Wording of the survey bubble:
/*
var version = 'v1'; // deployed on 2015-04-19, revoked on 2015-04-20
var surveyBubbleHTML = '<div id="syntaxErrBubbleContents">\
<div id="syntaxErrHeader">You just fixed the following error:</div>\
<div id="syntaxErrCodeDisplay"></div>\
<div id="syntaxErrMsg"></div>\
<div id="syntaxErrQuestion">\
If you think this message wasn\'t helpful, what would have been the best error message for you here?<br/>\
<input type="text" id="syntaxErrTxtInput" size=60 maxlength=150/><br/>\
<button id="syntaxErrSubmitBtn" type="button">Submit</button>\
<button id="syntaxErrCloseBtn" type="button">Close</button>\
</div>\
</div>'
*/
/*
var version = 'v2'; // deployed on 2015-04-20, revoked on 2015-09-08
var surveyBubbleHTML = '<div id="syntaxErrBubbleContents">\
<div id="syntaxErrHeader">You just fixed the following error:</div>\
<div id="syntaxErrCodeDisplay"></div>\
<div id="syntaxErrMsg"></div>\
<div id="syntaxErrQuestion">\
If you think this message wasn\'t helpful, what would have been the best error message for you here?<br/>\
<input type="text" id="syntaxErrTxtInput" size=60 maxlength=150/><br/>\
<button id="syntaxErrSubmitBtn" type="button">Submit</button>\
<button id="syntaxErrCloseBtn" type="button">Close</button>\
<a href="#" id="syntaxErrHideAllLink">Hide all pop-ups</a>\
</div>\
</div>'
*/
var version = 'v3'; // deployed on 2015-09-08
var surveyBubbleHTML = '<div id="syntaxErrBubbleContents">\
<div id="syntaxErrHeader">You just fixed the following error:</div>\
<div id="syntaxErrCodeDisplay"></div>\
<div id="syntaxErrMsg"></div>\
<div id="syntaxErrQuestion">\
Please help us improve error messages for future users.\
If you think the above message wasn\'t helpful, what would have been the best message for you here?<br/>\
<input type="text" id="syntaxErrTxtInput" size=60 maxlength=150/><br/>\
<button id="syntaxErrSubmitBtn" type="button">Submit</button>\
<button id="syntaxErrCloseBtn" type="button">Close</button>\
<a href="#" id="syntaxErrHideAllLink">Hide all of these pop-ups</a>\
</div>\
</div>'
$(bub.qTipContentID()).html(surveyBubbleHTML);
// unbind first, then bind a new one
myVisualizer.domRoot.find('#pyCodeOutputDiv')
.unbind('scroll')
.scroll(function() {
bub.redrawCodelineBubble();
});
$(bub.qTipContentID() + ' #syntaxErrSubmitBtn').click(function() {
var res = $(bub.qTipContentID() + ' #syntaxErrTxtInput').val();
var resObj = {appState: optCommon.getAppState(),
exc: prevExecutionExceptionObj, // note that prevExecutionExceptionObjLst is BLOWN AWAY by now
opt_uuid: myUuid,
reply: res,
type: 'submit',
v: version};
//console.log(resObj);
$.get('syntax_err_survey.py', {arg: JSON.stringify(resObj)}, function(dat) {});
bub.destroyQTip();
});
$(bub.qTipContentID() + ' #syntaxErrCloseBtn').click(function() {
// grab the value anyways in case the learner wrote something decent ...
var res = $(bub.qTipContentID() + ' #syntaxErrTxtInput').val();
var resObj = {appState: optCommon.getAppState(),
exc: prevExecutionExceptionObj, // note that prevExecutionExceptionObjLst is BLOWN AWAY by now
opt_uuid: myUuid,
reply: res,
type: 'close',
v: version};
//console.log(resObj);
$.get('syntax_err_survey.py', {arg: JSON.stringify(resObj)}, function(dat) {});
bub.destroyQTip();
});
$(bub.qTipContentID() + ' #syntaxErrHideAllLink').click(function() {
// grab the value anyways in case the learner wrote something decent ...
var res = $(bub.qTipContentID() + ' #syntaxErrTxtInput').val();
var resObj = {appState: optCommon.getAppState(),
exc: prevExecutionExceptionObj, // note that prevExecutionExceptionObjLst is BLOWN AWAY by now
opt_uuid: myUuid,
reply: res,
type: 'killall',
v: version};
activateSyntaxErrorSurvey = false; // global!
//console.log(resObj);
$.get('syntax_err_survey.py', {arg: JSON.stringify(resObj)}, function(dat) {});
bub.destroyQTip();
return false; // otherwise the 'a href' will trigger a page reload, ergh!
});
var bubbleAceEditor = ace.edit('syntaxErrCodeDisplay');
// set the size and value ASAP to get alignment working well ...
bubbleAceEditor.setOptions({minLines: 1, maxLines: 5}); // keep this SMALL
bubbleAceEditor.setValue(prevExecutionExceptionObj.myAppState.code.rtrim() /* kill trailing spaces */,
-1 /* do NOT select after setting text */);
var s = bubbleAceEditor.getSession();
// tab -> 4 spaces
s.setTabSize(4);
s.setUseSoftTabs(true);
// disable extraneous indicators:
s.setFoldStyle('manual'); // no code folding indicators
s.getDocument().setNewLineMode('unix'); // canonicalize all newlines to unix format
bubbleAceEditor.setHighlightActiveLine(false);
bubbleAceEditor.setShowPrintMargin(false);
bubbleAceEditor.setBehavioursEnabled(false);
bubbleAceEditor.setFontSize('10px');
bubbleAceEditor.$blockScrolling = Infinity; // kludgy to shut up weird warnings
$('#syntaxErrCodeDisplay').css('width', '320px');
$('#syntaxErrCodeDisplay').css('height', '90px'); // VERY IMPORTANT so that it works on I.E., ugh!
// don't do real-time syntax checks:
// https://github.com/ajaxorg/ace/wiki/Syntax-validation
s.setOption("useWorker", false);
var lang = prevExecutionExceptionObj.myAppState.py;
var mod = 'python';
if (lang === 'java') {
mod = 'java';
} else if (lang === 'js') {
mod = 'javascript';
} else if (lang === 'ts') {
mod = 'typescript';
} else if (lang === 'ruby') {
mod = 'ruby';
} else if (lang === 'c' || lang === 'cpp') {
mod = 'c_cpp';
}
s.setMode("ace/mode/" + mod);
bubbleAceEditor.setReadOnly(true);
s.setAnnotations([{row: offendingLine - 1 /* zero-indexed */,
column: null, /* for TS typechecking */
type: 'error',
text: prevExecutionExceptionObj.killerException.exception_msg}]);
// scroll down to the line where the error occurred, trying to center it
// by subtracing 3 from it (which should center it, assuming we're
// displaying 5 lines of context)
// TODO: maybe use the 'center' parameter of scrollToLine to make
// it automatically look centered, instead of this hack:
if ((offendingLine - 3) > 0) {
(bubbleAceEditor as any /* TS too strict */).scrollToLine(offendingLine - 3);
}
// don't forget htmlspecialchars
$("#syntaxErrMsg").html(pytutor.htmlspecialchars(prevExecutionExceptionObj.killerException.exception_msg));
bub.redrawCodelineBubble(); // do an initial redraw to align everything
//globalBub = bub; // for debugging
// log an event whenever this bubble is show (i.e., an 'impression')
// NB: it might actually be hidden if it appears on a line that
// isn't initially visible to the user, but whatevers ...
var impressionObj = {appState: optCommon.getAppState(),
exceptionLst: prevExecutionExceptionObjLst,
opt_uuid: myUuid,
type: 'show',
v: version};
//console.log(impressionObj);
$.get('syntax_err_survey.py', {arg: JSON.stringify(impressionObj)}, function(dat) {});
}
}
}