MOBILE-3438 question: ddimageortext infinite elements not positioned

main
Pau Ferrer Ocaña 2020-05-29 14:46:05 +02:00
parent 294fbadf6f
commit d58e8f3352
1 changed files with 26 additions and 28 deletions

View File

@ -133,10 +133,8 @@ export class AddonQtypeDdImageOrTextQuestion {
const dragNode = this.doc.cloneNewDragItem(i, dragItemNo);
i++;
if (!this.readOnly) {
// Make the item draggable.
this.draggableForQuestion(dragNode, group, choice);
}
// If the draggable item needs to be created more than once, create the rest of copies.
if (dragNode.classList.contains('infinite')) {
@ -146,9 +144,8 @@ export class AddonQtypeDdImageOrTextQuestion {
while (dragsToCreate > 0) {
const newDragNode = this.doc.cloneNewDragItem(i, dragItemNo);
i++;
if (!this.readOnly) {
this.draggableForQuestion(newDragNode, group, choice);
}
dragsToCreate--;
}
}
@ -336,6 +333,7 @@ export class AddonQtypeDdImageOrTextQuestion {
drag.setAttribute('group', String(group));
drag.setAttribute('choice', String(choice));
if (!this.readOnly) {
// Listen to click events.
drag.addEventListener('click', (e) => {
e.preventDefault();
@ -348,6 +346,7 @@ export class AddonQtypeDdImageOrTextQuestion {
}
});
}
}
/**
* Function called when a drop zone is clicked.
@ -391,14 +390,9 @@ export class AddonQtypeDdImageOrTextQuestion {
getUnplacedChoiceForDrop(choice: number, drop: HTMLElement): HTMLElement {
const dragItems = this.getChoicesForDrop(choice, drop);
for (let x = 0; x < dragItems.length; x++) {
const dragItem = dragItems[x];
if (this.readOnly || (!dragItem.classList.contains('placed') && !dragItem.classList.contains('beingdragged'))) {
return dragItem;
}
}
return null;
return dragItems.find((dragItem) => {
return (!dragItem.classList.contains('placed') && !dragItem.classList.contains('beingdragged'));
}) || null;
}
/**
@ -534,7 +528,7 @@ export class AddonQtypeDdImageOrTextQuestion {
if (originInputId && originInputId != targetInputId) {
// Remove it from the previous place.
const originInputNode = <HTMLInputElement> this.doc.topNode().querySelector('input#' + originInputId);
originInputNode.setAttribute('value', '');
originInputNode.setAttribute('value', '0');
}
// Now position the draggable and set it to the input.
@ -543,7 +537,10 @@ export class AddonQtypeDdImageOrTextQuestion {
drag.style.top = position[1] - 1 + 'px';
drag.classList.add('placed');
if (drag.getAttribute('choice')) {
inputNode.setAttribute('value', drag.getAttribute('choice'));
}
drag.setAttribute('inputid', targetInputId);
}
@ -579,7 +576,7 @@ export class AddonQtypeDdImageOrTextQuestion {
const inputId = drag.getAttribute('inputid');
if (inputId) {
const inputNode = <HTMLInputElement> this.doc.topNode().querySelector('input#' + inputId);
inputNode.setAttribute('value', '');
inputNode.setAttribute('value', '0');
}
// Move the element to its original position.
@ -656,6 +653,7 @@ export class AddonQtypeDdImageOrTextQuestion {
if (choice > 0) {
const dragItem = this.getUnplacedChoiceForDrop(choice, dropZone);
if (dragItem !== null) {
this.placeDragInDrop(dragItem, dropZone);
}