Skip to content

Commit f69ebaf

Browse files
Qtype_drawline: display middle label
1 parent 2652200 commit f69ebaf

File tree

10 files changed

+98
-40
lines changed

10 files changed

+98
-40
lines changed

amd/build/form.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/form.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/line.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/line.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/question.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/question.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/form.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ define(['jquery', 'core/dragdrop', 'qtype_drawlines/line'], function($, dragDrop
3232
function LineManager(lineNo) {
3333
this.lineNo = lineNo;
3434
this.svgEl = null;
35-
this.line = Line.make(this.getCoordinatesFromForm(this.lineNo), this.getLabel(), this.getLineType());
35+
this.line = Line.make(this.getCoordinatesFromForm(this.lineNo), this.getLineType(), this.getLabel());
3636
this.updateCoordinatesFromForm();
3737
}
3838

@@ -138,11 +138,10 @@ define(['jquery', 'core/dragdrop', 'qtype_drawlines/line'], function($, dragDrop
138138
*/
139139
LineManager.prototype.updateLabel = function() {
140140
var label = this.getLabel();
141-
if (this.line.labelstart !== label[0] || this.line.labelend !== label[1]) {
142-
this.line.labelstart = label[0];
143-
this.line.labelend = label[1];
144-
this.updateSvgEl();
145-
}
141+
this.line.labelstart = label[0];
142+
this.line.labelmiddle = label[1];
143+
this.line.labelend = label[2];
144+
this.updateSvgEl();
146145
};
147146

148147
/**
@@ -162,6 +161,7 @@ define(['jquery', 'core/dragdrop', 'qtype_drawlines/line'], function($, dragDrop
162161
LineManager.prototype.getLabel = function() {
163162
return [
164163
drawlinesForm.getFormValue('labelstart', [this.lineNo]),
164+
drawlinesForm.getFormValue('labelmiddle', [this.lineNo]),
165165
drawlinesForm.getFormValue('labelend', [this.lineNo])
166166
];
167167
};
@@ -189,14 +189,14 @@ define(['jquery', 'core/dragdrop', 'qtype_drawlines/line'], function($, dragDrop
189189
// Then comes the move handle followed by the edit handles.
190190
var i = 0;
191191
for (i = 0; i < handles.moveHandles.length; ++i) {
192-
this.svgEl.childNodes[5 + i].setAttribute('cx', handles.moveHandles[i].x);
193-
this.svgEl.childNodes[5 + i].setAttribute('cy', handles.moveHandles[i].y);
192+
this.svgEl.childNodes[6 + i].setAttribute('cx', handles.moveHandles[i].x);
193+
this.svgEl.childNodes[6 + i].setAttribute('cy', handles.moveHandles[i].y);
194194
}
195195

196196
// Edit handles.
197197
for (i = 0; i < handles.editHandles.length; ++i) {
198-
this.svgEl.childNodes[7 + i].setAttribute('x', handles.editHandles[i].x - 6);
199-
this.svgEl.childNodes[7 + i].setAttribute('y', handles.editHandles[i].y - 6);
198+
this.svgEl.childNodes[8 + i].setAttribute('x', handles.editHandles[i].x - 6);
199+
this.svgEl.childNodes[8 + i].setAttribute('y', handles.editHandles[i].y - 6);
200200
}
201201
};
202202

@@ -571,6 +571,7 @@ define(['jquery', 'core/dragdrop', 'qtype_drawlines/line'], function($, dragDrop
571571
break;
572572

573573
case 'labelstart':
574+
case 'labelmiddle':
574575
case 'labelend':
575576
dropZone.updateLabel();
576577
break;

amd/src/line.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,19 @@ define(function() {
9494
/**
9595
* Line constructor. Class to represent the different types of drop zone shapes.
9696
*
97-
* @param {String} [labelstart] start label of a line.
9897
* @param {int} [x1] centre X1.
9998
* @param {int} [y1] centre Y1.
10099
* @param {int} [startRadius] startRadius.
101-
* @param {String} [labelend] end label of a line.
102100
* @param {int} [x2] centre X2.
103101
* @param {int} [y2] centre Y2.
104102
* @param {int} [endRadius] endRadius.
105103
* @param {String} [lineType] Line type.
104+
* @param {String} [labelstart] start label of a line.
105+
* @param {String} [labelmiddle] middle label of a line.
106+
* @param {String} [labelend] end label of a line.
106107
* @constructor
107108
*/
108-
function Line(labelstart, x1, y1, startRadius, labelend, x2, y2, endRadius, lineType) {
109-
this.labelstart = labelstart;
110-
this.labelend = labelend;
109+
function Line(x1, y1, startRadius, x2, y2, endRadius, lineType, labelstart, labelmiddle, labelend) {
111110
this.x1 = x1;
112111
this.y1 = y1;
113112

@@ -121,6 +120,10 @@ define(function() {
121120
this.endRadius = endRadius;
122121

123122
this.lineType = lineType;
123+
124+
this.labelstart = labelstart;
125+
this.labelmiddle = labelmiddle;
126+
this.labelend = labelend;
124127
}
125128
Line.prototype = new Line();
126129

@@ -178,12 +181,15 @@ define(function() {
178181

179182
// Set start and end label attributes.
180183
svgEl.childNodes[3].textContent = this.labelstart;
181-
svgEl.childNodes[3].setAttribute('x', this.centre1.x);
182-
svgEl.childNodes[3].setAttribute('y', parseInt(this.centre1.y) + 20);
184+
this.adjustTextPosition(svgEl, svgEl.childNodes[3], this.centre1.x, parseInt(this.centre1.y));
183185

184-
svgEl.childNodes[4].textContent = this.labelend;
185-
svgEl.childNodes[4].setAttribute('x', this.centre2.x);
186-
svgEl.childNodes[4].setAttribute('y', parseInt(this.centre2.y) + 20);
186+
svgEl.childNodes[4].textContent = this.labelmiddle;
187+
let middlex = Math.abs((this.centre1.x + this.centre2.x) / 2);
188+
let middley = Math.abs((this.centre1.y + this.centre2.y) / 2);
189+
this.adjustTextPosition(svgEl, svgEl.childNodes[4], middlex, middley);
190+
191+
svgEl.childNodes[5].textContent = this.labelend;
192+
this.adjustTextPosition(svgEl, svgEl.childNodes[5], this.centre2.x, parseInt(this.centre2.y));
187193

188194
// If the svg g element is already placed in dropzone, then add the keyboard support.
189195
var svgClass = svgEl.getAttribute('class');
@@ -193,6 +199,41 @@ define(function() {
193199
}
194200
};
195201

202+
/**
203+
* Update svg line attributes.
204+
*
205+
* @param {SVGElement} [svgEl] the g element of the SVG.
206+
* @param {SVGElement} [svgTextEl] the text node of the SVG.
207+
* @param {int} [linex] coordinate of the line.
208+
* @param {int} [liney] coordinate of the line.
209+
*/
210+
Line.prototype.adjustTextPosition = function(svgEl, svgTextEl, linex, liney) {
211+
// SVG container dimensions
212+
let parentSVG = svgEl.parentNode;
213+
const svgWidth = parentSVG.getAttribute('width');
214+
const svgHeight = parentSVG.getAttribute('height');
215+
const padding = 20;
216+
217+
// Text element dimensions.
218+
const bbox = svgTextEl.getBBox();
219+
const textWidth = bbox.width;
220+
221+
svgTextEl.setAttribute('x', linex);
222+
svgTextEl.setAttribute('y', liney + padding);
223+
224+
// Recalculate the position of x and y coordinates of text, to make sure the text content is fully displayed.
225+
if (linex < textWidth / 2) {
226+
svgTextEl.setAttribute('x', Math.abs(textWidth / 2));
227+
} else if ((linex + (textWidth / 2)) > svgWidth) {
228+
svgTextEl.setAttribute('x', Math.abs(svgWidth - (textWidth / 2)));
229+
}
230+
231+
if (liney + padding > svgHeight) {
232+
// Adjust if the line is very near to the bottom of the svg.
233+
svgTextEl.setAttribute('y', liney - padding);
234+
}
235+
};
236+
196237
/**
197238
* Update svg line attributes.
198239
*
@@ -607,6 +648,7 @@ define(function() {
607648
var endcirleEl = createSvgElement(svgEl, 'circle');
608649
endcirleEl.setAttribute('class', 'endcircle shape');
609650
createSvgElement(svgEl, 'text').setAttribute('class', 'labelstart shapeLabel');
651+
createSvgElement(svgEl, 'text').setAttribute('class', 'labelmiddle shapeLabel');
610652
createSvgElement(svgEl, 'text').setAttribute('class', 'labelend shapeLabel');
611653
return svgEl;
612654
}
@@ -627,15 +669,16 @@ define(function() {
627669
/**
628670
* Line constructor. Class to represent the different types of drop zone shapes.
629671
*
630-
* @param {String} [labelstart] start label of a line.
631672
* @param {int} [x1] centre X1.
632673
* @param {int} [y1] centre Y1.
633674
* @param {int} [startRadius] startRadius.
634-
* @param {String} [labelend] end label of a line.
635675
* @param {int} [x2] centre X2.
636676
* @param {int} [y2] centre Y2.
637677
* @param {int} [endRadius] endRadius.
638678
* @param {String} [lineType] Line type.
679+
* @param {String} [labelstart] start label of a line.
680+
* @param {String} [labelmiddle] middle label of a line.
681+
* @param {String} [labelend] end label of a line.
639682
* @constructor
640683
*/
641684
Line: Line,
@@ -652,20 +695,21 @@ define(function() {
652695
/**
653696
* Make a line of the given type.
654697
*
655-
* @param {Array} linecoordinates in the format (x,y;radius).
656-
* @param {Array} labels Start and end labels of a line.
657-
* @param {String} lineType The linetype (e.g., linesinglearrow, linedoublearrows, ...).
698+
* @param {Array} [linecoordinates] in the format (x,y;radius).
699+
* @param {String} [lineType] The linetype (e.g., linesinglearrow, linedoublearrows, ...).
700+
* @param {Array} [labels] Start, middle and end labels of a line.
658701
* @return {Line} the new line.
659702
*/
660-
make: function(linecoordinates, labels, lineType) {
703+
make: function(linecoordinates, lineType, labels) {
661704
// Line coordinates are in the format (x,y;radius).
662705
var startcoordinates = linecoordinates[0].split(';');
663706
var endcoordinates = linecoordinates[1].split(';');
664707
var linestartbits = startcoordinates[0].split(',');
665708
var lineendbits = endcoordinates[0].split(',');
666709

667-
return new Line(labels[0], parseInt(linestartbits[0]), parseInt(linestartbits[1]), parseInt(startcoordinates[1]),
668-
labels[1], parseInt(lineendbits[0]), parseInt(lineendbits[1]), parseInt(endcoordinates[1]), lineType);
710+
return new Line(parseInt(linestartbits[0]), parseInt(linestartbits[1]), parseInt(startcoordinates[1]),
711+
parseInt(lineendbits[0]), parseInt(lineendbits[1]), parseInt(endcoordinates[1]), lineType,
712+
labels[0], labels[1], labels[2]);
669713
},
670714

671715
/**
@@ -676,8 +720,9 @@ define(function() {
676720
* @return {line} the similar line of a different linetype.
677721
*/
678722
getSimilar: function(lineType, line) {
679-
return new Line(line.labelstart, parseInt(line.x1), parseInt(line.y1), parseInt(line.startRadius),
680-
line.labelend, parseInt(line.x2), parseInt(line.y2), parseInt(line.endRadius), lineType);
723+
return new Line(parseInt(line.x1), parseInt(line.y1), parseInt(line.startRadius),
724+
parseInt(line.x2), parseInt(line.y2), parseInt(line.endRadius), lineType,
725+
line.labelstart, line.labelmiddle, line.labelend);
681726
}
682727
};
683728
});

amd/src/question.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,16 @@ define([
152152
endcoordinates = coords[1] + ';10';
153153
this.lines[line] = Line.make(
154154
[startcoordinates, endcoordinates],
155-
[questionLines[line].labelstart, questionLines[line].labelend],
156-
questionLines[line].type
155+
questionLines[line].type,
156+
[questionLines[line].labelstart, questionLines[line].labelmiddle, questionLines[line].labelend]
157157
);
158158
this.addToSvg(line, dropzoneSvg);
159159
} else {
160160
// Need to be added to draghomeSvg.
161161
this.lines[line] = Line.make(
162162
[startcoordinates, endcoordinates],
163-
[questionLines[line].labelstart, questionLines[line].labelend],
164-
questionLines[line].type
163+
questionLines[line].type,
164+
[questionLines[line].labelstart, questionLines[line].labelmiddle, questionLines[line].labelend]
165165
);
166166
this.addToSvg(line, draghomeSvg);
167167
}

tests/behat/edit.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ Feature: Test editing an draw lines question
5757
And I press "id_submitbutton"
5858
Then I should see "Drawline edited"
5959

60+
@javascript
61+
Scenario: Editing DrawLines question labels should rellect in svg
62+
Given I am on the "Drawlines to edit" "core_question > edit" page logged in as teacher
63+
And I should see "Editing a Draw lines question"
64+
And I expand all fieldsets
65+
When I set the field "id_labelstart_0" to "new label start"
66+
And I set the field "id_labelmiddle_0" to "new label middle"
67+
And I set the field "id_labelend_0" to "new label end"
68+
Then "//*[name()='svg']/*[name()='g' and @data-dropzone-no='0']/*[name()='text'][1][contains(text(), 'new label start')]" "xpath_element" should exist
69+
And "//*[name()='svg']/*[name()='g' and @data-dropzone-no='0']/*[name()='text'][2][contains(text(), 'new label middle')]" "xpath_element" should exist
70+
And "//*[name()='svg']/*[name()='g' and @data-dropzone-no='0']/*[name()='text'][3][contains(text(), 'new label end')]" "xpath_element" should exist
71+
6072
@javascript @_file_upload
6173
Scenario: Validate the background image size for Draw lines question
6274
Given I am on the "Drawlines to edit" "core_question > edit" page logged in as teacher

0 commit comments

Comments
 (0)