Skip to content

Commit 8b7aea7

Browse files
Add keyboard support to the preview page
1 parent 5963f76 commit 8b7aea7

File tree

8 files changed

+205
-114
lines changed

8 files changed

+205
-114
lines changed

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/Line.js

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,16 @@ define(function() {
282282
/**
283283
* Parse the coordinates from the string representation.
284284
*
285-
* @param {String} startcoordinates "x1,y1".
286-
* @param {String} endcoordinates "x1,y1".
285+
* @param {String} startcoordinates "x1,y1;radius".
286+
* @param {String} endcoordinates "x1,y1;radius".
287287
* @param {float} ratio .
288-
* @return {Point} the point. Throws an exception if input is not valid.
288+
* @return {boolean} True if the coordinates are parsed. False if the start and end coordinates are empty.
289+
* * Throws an exception if input point is not valid.
289290
*/
290291
Line.prototype.parse = function(startcoordinates, endcoordinates, ratio) {
292+
if (startcoordinates === '' || endcoordinates === '') {
293+
return false;
294+
}
291295
var startcoordinatesbits = startcoordinates.split(';');
292296
var endcoordinatesbits = endcoordinates.split(';');
293297
this.centre1 = Point.parse(startcoordinatesbits[0]);
@@ -309,14 +313,14 @@ define(function() {
309313
/**
310314
* Move the entire shape by this offset.
311315
*
312-
* @param {String} handleIndex which handle was moved.
316+
* @param {String} whichHandle which circle handle was moved, i.e., startcircle or endcircle.
313317
* @param {int} dx x offset.
314318
* @param {int} dy y offset.
315319
* @param {int} maxX ensure that after editing, the shape lies between 0 and maxX on the x-axis.
316320
* @param {int} maxY ensure that after editing, the shape lies between 0 and maxX on the y-axis.
317321
*/
318-
Line.prototype.move = function(handleIndex, dx, dy, maxX, maxY) {
319-
if (handleIndex === '0') {
322+
Line.prototype.move = function(whichHandle, dx, dy, maxX, maxY) {
323+
if (whichHandle === 'startcircle') {
320324
this.centre1.move(dx, dy);
321325
if (this.centre1.x < this.startRadius) {
322326
this.centre1.x = this.startRadius;
@@ -356,7 +360,7 @@ define(function() {
356360
};
357361

358362
/**
359-
* Move the entire line by this offset.
363+
* Move the line end points by this offset.
360364
*
361365
* @param {int} dx x offset.
362366
* @param {int} dy y offset.
@@ -414,28 +418,36 @@ define(function() {
414418

415419
/**
416420
* Move the g element between the dropzones and dragHomes.
417-
* @param {SVGElement} svgDragsHome Svg element containing the drags.
418-
* @param {SVGElement} svgDropZones Svg element containing the dropZone.
421+
* @param {String} eventType Whether it's a mouse event or a keyboard event.
419422
* @param {SVGElement} selectedElement The element selected for dragging.
420-
* @param {int} dropX
421-
* @param {int} dropY
423+
* @param {int|null} dropX Used by mouse events to calculate the svg to which it belongs.
424+
* @param {int|null} dropY
425+
* @param {String|null} whichSVG
422426
*/
423-
Line.prototype.addToDropZone = function(svgDragsHome, svgDropZones, selectedElement, dropX, dropY) {
424-
var maxY = 0;
425-
var dropzoneNo = selectedElement.getAttribute('data-dropzone-no');
426-
var classattributes = '';
427-
if (this.isInsideSVG(svgDragsHome, dropX, dropY)) {
428-
// Append the element to the second SVG
429-
// Get the height of the dropZone SVG.
427+
Line.prototype.addToDropZone = function(eventType, selectedElement, dropX, dropY, whichSVG) {
428+
var maxY = 0,
429+
dropzoneNo = selectedElement.getAttribute('data-dropzone-no'),
430+
classattributes = '',
431+
dropZone = false;
432+
var svgDragsHome = document.querySelector('svg.dragshome');
433+
var svgDropZones = document.querySelector('svg.dropzones');
434+
if (eventType === 'mouse') {
435+
dropZone = this.isInsideSVG(svgDragsHome, dropX, dropY);
436+
} else {
437+
dropZone = (whichSVG === 'DragsSVG');
438+
}
439+
if (dropZone) {
440+
// Append the element to the dropzone SVG.
441+
// Get the height of the dropZone SVG, to decide the position to where to drop the line.
430442
maxY = svgDropZones.height.baseVal.value;
431443
svgDropZones.appendChild(selectedElement);
432444
selectedElement.getAttribute('data-dropzone-no');
433445

446+
// Set tabindex to add keyevents to the circle movehandles.
447+
selectedElement.childNodes[1].setAttribute('tabindex', '0');
448+
selectedElement.childNodes[2].setAttribute('tabindex', '0');
449+
434450
// Caluculate the position of line drop.
435-
// this.centre1.y = maxY - (2 * this.startRadius) - (dropzoneNo * 50);
436-
// this.y1 = maxY - (2 * this.startRadius) - (dropzoneNo * 50);
437-
// this.centre2.y = maxY - (2 * this.endRadius) - (dropzoneNo * 50);
438-
// this.y2 = maxY - (2 * this.endRadius) - (dropzoneNo * 50);
439451
this.centre1.y = maxY - (2 * this.startRadius);
440452
this.y1 = maxY - (2 * this.startRadius);
441453
this.centre2.y = maxY - (2 * this.endRadius);
@@ -446,8 +458,8 @@ define(function() {
446458
classattributes = classattributes.replace('inactive', 'placed');
447459
selectedElement.setAttribute('class', classattributes);
448460

449-
} else if (this.isInsideSVG(svgDropZones, dropX, dropY)) {
450-
// Append the element to the first SVG (to ensure it stays in the same SVG if dropped there)
461+
} else {
462+
// Append the element to the draghmes SVG.
451463
svgDragsHome.appendChild(selectedElement);
452464

453465
// We want to drop the lines from the top, depending on the line number.
@@ -463,6 +475,9 @@ define(function() {
463475
classattributes = selectedElement.getAttribute('class');
464476
classattributes = classattributes.replace('placed', 'inactive');
465477
selectedElement.setAttribute('class', classattributes);
478+
// Set tabindex = -1, so the circle movehandles aren't focusable when in draghomes svg.
479+
selectedElement.childNodes[1].setAttribute('tabindex', '-1');
480+
selectedElement.childNodes[2].setAttribute('tabindex', '-1');
466481
}
467482
return '';
468483
};
@@ -583,9 +598,13 @@ define(function() {
583598
*/
584599
function createSvgShapeGroup(svg, tagName) {
585600
var svgEl = createSvgElement(svg, 'g');
586-
createSvgElement(svgEl, tagName).setAttribute('class', 'shape');
587-
createSvgElement(svgEl, 'circle').setAttribute('class', 'startcircle shape');
588-
createSvgElement(svgEl, 'circle').setAttribute('class', 'endcircle shape');
601+
var lineEl = createSvgElement(svgEl, tagName);
602+
lineEl.setAttribute('class', 'shape');
603+
lineEl.setAttribute('tabindex', '0');
604+
var startcircleEl = createSvgElement(svgEl, 'circle');
605+
startcircleEl.setAttribute('class', 'startcircle shape');
606+
var endcirleEl = createSvgElement(svgEl, 'circle');
607+
endcirleEl.setAttribute('class', 'endcircle shape');
589608
createSvgElement(svgEl, 'text').setAttribute('class', 'labelstart shapeLabel');
590609
createSvgElement(svgEl, 'text').setAttribute('class', 'labelend shapeLabel');
591610
return svgEl;

0 commit comments

Comments
 (0)