Skip to content

Commit 92f72a2

Browse files
author
Philip Guo
committed
eliminate magic numbers in aligning arrows, so that it now
works on different browsers
1 parent b75d0eb commit 92f72a2

1 file changed

Lines changed: 55 additions & 26 deletions

File tree

‎v3/js/pytutor.js‎

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5555
*/
5656

5757

58-
var svgArrowPolygon = '0,3 12,3 12,0 18,5 12,10 12,7 0,7';
58+
var SVG_ARROW_POLYGON = '0,3 12,3 12,0 18,5 12,10 12,7 0,7';
59+
var SVG_ARROW_HEIGHT = 10; // must match height of SVG_ARROW_POLYGON
5960

6061
var curVisualizerID = 1; // global to uniquely identify each ExecutionVisualizer instance
6162

@@ -94,6 +95,11 @@ function ExecutionVisualizer(domRootID, dat, params) {
9495
curVisualizerID++;
9596

9697

98+
this.leftGutterSvgInitialized = false;
99+
this.arrowOffsetY = undefined;
100+
this.codeRowHeight = undefined;
101+
102+
97103
// cool, we can create a separate jsPlumb instance for each visualization:
98104
this.jsPlumbInstance = jsPlumb.getInstance({
99105
Endpoint: ["Dot", {radius:3}],
@@ -219,12 +225,12 @@ ExecutionVisualizer.prototype.render = function() {
219225

220226
myViz.domRootD3.select('svg#prevLegendArrowSVG')
221227
.append('polygon')
222-
.attr('points', svgArrowPolygon)
228+
.attr('points', SVG_ARROW_POLYGON)
223229
.attr('fill', lightArrowColor);
224230

225231
myViz.domRootD3.select('svg#curLegendArrowSVG')
226232
.append('polygon')
227-
.attr('points', svgArrowPolygon)
233+
.attr('points', SVG_ARROW_POLYGON)
228234
.attr('fill', darkArrowColor);
229235

230236

@@ -679,13 +685,13 @@ ExecutionVisualizer.prototype.renderPyCodeOutput = function() {
679685
myViz.domRootD3.select('svg#leftCodeGutterSVG')
680686
.append('polygon')
681687
.attr('id', 'prevLineArrow')
682-
.attr('points', svgArrowPolygon)
688+
.attr('points', SVG_ARROW_POLYGON)
683689
.attr('fill', 'white');
684690

685691
myViz.domRootD3.select('svg#leftCodeGutterSVG')
686692
.append('polygon')
687693
.attr('id', 'curLineArrow')
688-
.attr('points', svgArrowPolygon)
694+
.attr('points', SVG_ARROW_POLYGON)
689695
.attr('fill', 'white');
690696

691697

@@ -736,35 +742,58 @@ ExecutionVisualizer.prototype.renderPyCodeOutput = function() {
736742

737743
// This function is called every time the display needs to be updated
738744
ExecutionVisualizer.prototype.updateOutput = function() {
739-
var myViz = this; // to prevent confusion of 'this' inside of nested functions
740-
741745
assert(this.curTrace);
742746

747+
var myViz = this; // to prevent confusion of 'this' inside of nested functions
748+
743749
// there's no point in re-rendering if this pane isn't even visible in the first place!
744750
if (!myViz.domRoot.is(':visible')) {
745751
return;
746752
}
747753

748-
// set the gutter's height to match that of its parent
749-
// (we often can't do this earlier since the entire pane
750-
// might be invisible and hence returns a height of zero or NaN -- the exact format depends on browser)
754+
751755
var gutterSVG = myViz.domRoot.find('svg#leftCodeGutterSVG');
752-
gutterSVG.height(gutterSVG.parent().height());
753756

757+
// one-time initialization of the left gutter
758+
// (we often can't do this earlier since the entire pane
759+
// might be invisible and hence returns a height of zero or NaN
760+
// -- the exact format depends on browser)
761+
if (!myViz.leftGutterSvgInitialized) {
762+
// set the gutter's height to match that of its parent
763+
gutterSVG.height(gutterSVG.parent().height());
754764

755-
var tableTop = 5; // manually adjust this so that it looks good :)
765+
var firstRowOffsetY = myViz.domRoot.find('table#pyCodeOutput tr:first').offset().top;
766+
767+
// first take care of edge case when there's only one line ...
768+
myViz.codeRowHeight = myViz.domRoot.find('table#pyCodeOutput td.cod:first').height();
769+
770+
// ... then handle the (much more common) multi-line case ...
771+
// this weird contortion is necessary to get the accurate row height on Internet Explorer
772+
// (simpler methods work on all other major browsers, erghhhhhh!!!)
773+
if (this.codeOutputLines && this.codeOutputLines.length > 1) {
774+
var secondRowOffsetY = myViz.domRoot.find('table#pyCodeOutput tr:nth-child(2)').offset().top;
775+
myViz.codeRowHeight = secondRowOffsetY - firstRowOffsetY;
776+
}
756777

757-
// this weird contortion is necessary to get the accurate row height on Internet Explorer
758-
// (simpler methods work on all other major browsers, erghhhhhh!!!)
759-
760-
// first take care of edge case when there's only one line ...
761-
var rowHeight = myViz.domRoot.find('table#pyCodeOutput td.cod:first').height();
762-
// ... then handle the (much more common) multi-line case ...
763-
if (this.codeOutputLines && this.codeOutputLines.length > 1) {
764-
rowHeight = (myViz.domRoot.find('table#pyCodeOutput tr:nth-child(2)').offset().top -
765-
myViz.domRoot.find('table#pyCodeOutput tr:first').offset().top);
778+
assert(myViz.codeRowHeight > 0);
779+
780+
var gutterOffsetY = gutterSVG.offset().top;
781+
var teenyAdjustment = gutterOffsetY - firstRowOffsetY;
782+
783+
// super-picky detail to adjust the vertical alignment of arrows so that they line up
784+
// well with the pointed-to code text ...
785+
// (if you want to manually adjust tableTop, then ~5 is a reasonable number)
786+
myViz.arrowOffsetY = Math.floor((myViz.codeRowHeight / 2) - (SVG_ARROW_HEIGHT / 2)) - teenyAdjustment;
787+
788+
myViz.leftGutterSvgInitialized = true;
789+
790+
console.log(myViz.codeRowHeight);
766791
}
767792

793+
assert(myViz.arrowOffsetY !== undefined);
794+
assert(myViz.codeRowHeight !== undefined);
795+
assert(0 <= myViz.arrowOffsetY && myViz.arrowOffsetY <= myViz.codeRowHeight);
796+
768797
// call the callback if necessary (BEFORE rendering)
769798
if (this.params.updateOutputCallback) {
770799
this.params.updateOutputCallback(this);
@@ -861,8 +890,8 @@ ExecutionVisualizer.prototype.updateOutput = function() {
861890

862891
// on 'return' events, give a bit more of a vertical nudge to show that
863892
// the arrow is aligned with the 'bottom' of the line ...
864-
var prevVerticalNudge = prevIsReturn ? 10 : 0;
865-
var curVerticalNudge = curIsReturn ? 10 : 0;
893+
var prevVerticalNudge = prevIsReturn ? Math.floor(myViz.codeRowHeight / 2) : 0;
894+
var curVerticalNudge = curIsReturn ? Math.floor(myViz.codeRowHeight / 2) : 0;
866895

867896

868897
// edge case for the final instruction :0
@@ -873,15 +902,15 @@ ExecutionVisualizer.prototype.updateOutput = function() {
873902
}
874903
// otherwise have a smaller vertical nudge (to fit at bottom of display table)
875904
else {
876-
curVerticalNudge = 8;
905+
curVerticalNudge = curVerticalNudge - 2;
877906
}
878907
}
879908

880909
if (prevLineNumber) {
881910
gutterSVG.find('#prevLineArrow')
882911
.show()
883912
.attr('fill', lightArrowColor)
884-
.attr('transform', 'translate(0, ' + (((prevLineNumber - 1) * rowHeight) + tableTop + prevVerticalNudge) + ')');
913+
.attr('transform', 'translate(0, ' + (((prevLineNumber - 1) * myViz.codeRowHeight) + myViz.arrowOffsetY + prevVerticalNudge) + ')');
885914
}
886915
else {
887916
gutterSVG.find('#prevLineArrow').hide();
@@ -891,7 +920,7 @@ ExecutionVisualizer.prototype.updateOutput = function() {
891920
gutterSVG.find('#curLineArrow')
892921
.show()
893922
.attr('fill', darkArrowColor)
894-
.attr('transform', 'translate(0, ' + (((curLineNumber - 1) * rowHeight) + tableTop + curVerticalNudge) + ')');
923+
.attr('transform', 'translate(0, ' + (((curLineNumber - 1) * myViz.codeRowHeight) + myViz.arrowOffsetY + curVerticalNudge) + ')');
895924
}
896925
else {
897926
gutterSVG.find('#curLineArrow').hide();

0 commit comments

Comments
 (0)