This commit is contained in:
Andrew Nicols 2019-02-11 15:11:20 +08:00
commit 232906959a
4 changed files with 86 additions and 3 deletions

File diff suppressed because one or more lines are too long

View file

@ -115,7 +115,6 @@ define(['jquery', 'core/dragdrop', 'core/key_codes'], function($, dragDrop, keys
this.cloneDrags(); this.cloneDrags();
this.positionDragsAndDrops(); this.positionDragsAndDrops();
M.util.js_complete('qtype_ddimageortext-init-' + this.containerId); M.util.js_complete('qtype_ddimageortext-init-' + this.containerId);
}; };
/** /**
@ -269,6 +268,28 @@ define(['jquery', 'core/dragdrop', 'core/key_codes'], function($, dragDrop, keys
.addClass('placed inplace' + place) .addClass('placed inplace' + place)
.offset(root.find('.dropzone.place' + place).offset()); .offset(root.find('.dropzone.place' + place).offset());
}); });
this.bgImage().data('prev-top', bgPosition.top).data('prev-left', bgPosition.left);
};
/**
* Check to see if the background image has moved. If so, refresh the layout.
*/
DragDropOntoImageQuestion.prototype.fixLayoutIfBackgroundMoved = function() {
var bgImage = this.bgImage(),
bgPosition = bgImage.offset(),
prevTop = bgImage.data('prev-top'),
prevLeft = bgImage.data('prev-left');
if (prevLeft === undefined || prevTop === undefined) {
// Question is not set up yet. Nothing to do.
return;
}
if (prevTop === bgPosition.top && prevLeft === bgPosition.left) {
// Things have not moved.
return;
}
// We need to reposition things.
this.positionDragsAndDrops();
}; };
/** /**
@ -698,6 +719,7 @@ define(['jquery', 'core/dragdrop', 'core/key_codes'], function($, dragDrop, keys
'.que.ddimageortext:not(.qtype_ddimageortext-readonly) .dropzones .dropzone', '.que.ddimageortext:not(.qtype_ddimageortext-readonly) .dropzones .dropzone',
questionManager.handleKeyPress); questionManager.handleKeyPress);
$(window).on('resize', questionManager.handleWindowResize); $(window).on('resize', questionManager.handleWindowResize);
setTimeout(questionManager.fixLayoutIfThingsMoved, 100);
}, },
/** /**
@ -734,6 +756,24 @@ define(['jquery', 'core/dragdrop', 'core/key_codes'], function($, dragDrop, keys
} }
}, },
/**
* Sometimes, despite our best efforts, things change in a way that cannot
* be specifically caught (e.g. dock expanding or collapsing in Boost).
* Therefore, we need to periodically check everything is in the right position.
*/
fixLayoutIfThingsMoved: function() {
for (var containerId in questionManager.questions) {
if (questionManager.questions.hasOwnProperty(containerId)) {
questionManager.questions[containerId].fixLayoutIfBackgroundMoved();
}
}
// We use setTimeout after finishing work, rather than setInterval,
// in case positioning things is slow. We want 100 ms gap
// between executions, not what setInterval does.
setTimeout(questionManager.fixLayoutIfThingsMoved, 100);
},
/** /**
* Given an event, work out which question it effects. * Given an event, work out which question it effects.
* @param {Event} e the event. * @param {Event} e the event.

File diff suppressed because one or more lines are too long

View file

@ -181,6 +181,10 @@ define(['jquery', 'core/dragdrop', 'qtype_ddmarker/shapes', 'core/key_codes'], f
}); });
this.repositionDropZones(); this.repositionDropZones();
var bgImage = this.bgImage(),
bgPosition = bgImage.offset();
bgImage.data('prev-top', bgPosition.top).data('prev-left', bgPosition.left);
}; };
/** /**
@ -487,6 +491,26 @@ define(['jquery', 'core/dragdrop', 'qtype_ddmarker/shapes', 'core/key_codes'], f
this.repositionDrags(); this.repositionDrags();
}; };
/**
* Check to see if the background image has moved. If so, refresh the layout.
*/
DragDropMarkersQuestion.prototype.fixLayoutIfBackgroundMoved = function() {
var bgImage = this.bgImage(),
bgPosition = bgImage.offset(),
prevTop = bgImage.data('prev-top'),
prevLeft = bgImage.data('prev-left');
if (prevLeft === undefined || prevTop === undefined) {
// Question is not set up yet. Nothing to do.
return;
}
if (prevTop === bgPosition.top && prevLeft === bgPosition.left) {
// Things have not moved.
return;
}
// We need to reposition things.
this.repositionDrags();
};
/** /**
* Singleton that tracks all the DragDropToTextQuestions on this page, and deals * Singleton that tracks all the DragDropToTextQuestions on this page, and deals
* with event dispatching. * with event dispatching.
@ -533,6 +557,7 @@ define(['jquery', 'core/dragdrop', 'qtype_ddmarker/shapes', 'core/key_codes'], f
'.que.ddmarker:not(.qtype_ddmarker-readonly) div.dragitems .dragitem', '.que.ddmarker:not(.qtype_ddmarker-readonly) div.dragitems .dragitem',
questionManager.handleKeyPress); questionManager.handleKeyPress);
$(window).on('resize', questionManager.handleWindowResize); $(window).on('resize', questionManager.handleWindowResize);
setTimeout(questionManager.fixLayoutIfThingsMoved, 100);
}, },
/** /**
@ -569,6 +594,24 @@ define(['jquery', 'core/dragdrop', 'qtype_ddmarker/shapes', 'core/key_codes'], f
} }
}, },
/**
* Sometimes, despite our best efforts, things change in a way that cannot
* be specifically caught (e.g. dock expanding or collapsing in Boost).
* Therefore, we need to periodically check everything is in the right position.
*/
fixLayoutIfThingsMoved: function() {
for (var containerId in questionManager.questions) {
if (questionManager.questions.hasOwnProperty(containerId)) {
questionManager.questions[containerId].fixLayoutIfBackgroundMoved();
}
}
// We use setTimeout after finishing work, rather than setInterval,
// in case positioning things is slow. We want 100 ms gap
// between executions, not what setInterval does.
setTimeout(questionManager.fixLayoutIfThingsMoved, 100);
},
/** /**
* Given an event, work out which question it effects. * Given an event, work out which question it effects.
* @param {Event} e the event. * @param {Event} e the event.