Commit 79f5e98d by Valera Rozuvan Committed by Alexander Kryklia

Drag and Drop: refactoring + preparing for reuse of draggables.

parent e47f0e45
...@@ -26,7 +26,7 @@ define(['logme'], function (logme) { ...@@ -26,7 +26,7 @@ define(['logme'], function (logme) {
state.baseImageEl.attr( state.baseImageEl.attr(
'src', 'src',
state.config.base_image state.config.baseImage
); );
state.baseImageEl.load(function () { state.baseImageEl.load(function () {
baseImageElContainer.css('width', this.width); baseImageElContainer.css('width', this.width);
...@@ -43,11 +43,11 @@ define(['logme'], function (logme) { ...@@ -43,11 +43,11 @@ define(['logme'], function (logme) {
}); });
state.baseImageEl.error(function () { state.baseImageEl.error(function () {
logme( logme(
'ERROR: Image "' + state.config.base_image + '" was not found!' 'ERROR: Image "' + state.config.baseImage + '" was not found!'
); );
baseImageElContainer.html( baseImageElContainer.html(
'<span style="color: red;">' + '<span style="color: red;">' +
'ERROR: Image "' + state.config.base_image + '" was not found!' + 'ERROR: Image "' + state.config.baseImage + '" was not found!' +
'</span>' '</span>'
); );
baseImageElContainer.appendTo(state.containerEl); baseImageElContainer.appendTo(state.containerEl);
......
...@@ -10,8 +10,6 @@ define(['logme', 'update_input'], function (logme, updateInput) { ...@@ -10,8 +10,6 @@ define(['logme', 'update_input'], function (logme, updateInput) {
}; };
function init(state) { function init(state) {
logme('Draggables.init; state = ', state);
state.draggables = []; state.draggables = [];
state.numDraggablesInSlider = 0; state.numDraggablesInSlider = 0;
state.currentMovingDraggable = null; state.currentMovingDraggable = null;
...@@ -65,12 +63,136 @@ define(['logme', 'update_input'], function (logme, updateInput) { ...@@ -65,12 +63,136 @@ define(['logme', 'update_input'], function (logme, updateInput) {
} }
} }
function processDraggable(state, obj, objIndex) { function moveDraggableToXY(newPosition) {
var draggableObj; var self, offset;
if (this.hasLoaded === false) {
self = this;
setTimeout(function () {
self.moveDraggableToXY(newPosition);
}, 50);
return;
}
offset = 0;
if (this.state.config.targetOutline === true) {
offset = 1;
}
this.inContainer = false;
this.containerEl.hide();
this.iconEl.detach();
this.iconEl.css('background-color', this.iconElBGColor);
this.iconEl.css('padding-left', this.iconElPadding);
this.iconEl.css('padding-right', this.iconElPadding);
this.iconEl.css('border', this.iconElBorder);
this.iconEl.css('width', this.iconWidth);
this.iconEl.css('height', this.iconHeight);
this.iconEl.css(
'left',
newPosition.x - this.iconWidth * 0.5 + offset - this.iconElLeftOffset
);
this.iconEl.css(
'top',
newPosition.y - this.iconHeight * 0.5 + offset
);
this.iconEl.appendTo(this.state.baseImageEl.parent());
if (this.labelEl !== null) {
this.labelEl.detach();
this.labelEl.css('background-color', this.state.config.labelBgColor);
this.labelEl.css('padding-left', 8);
this.labelEl.css('padding-right', 8);
this.labelEl.css('border', '1px solid black');
this.labelEl.css(
'left',
newPosition.x - this.labelWidth * 0.5 + offset - 9 // Account for padding, border.
);
this.labelEl.css(
'top',
newPosition.y - this.iconHeight * 0.5 + this.iconHeight + 5 + offset
);
this.labelEl.appendTo(this.state.baseImageEl.parent());
}
this.x = newPosition.x;
this.y = newPosition.y;
this.state.numDraggablesInSlider -= 1;
this.state.updateArrowOpacity();
}
function moveDraggableToTarget(target) {
var self, offset;
logme('processDraggable; state = ', state); if (this.hasLoaded === false) {
self = this;
logme('Processing draggable #' + objIndex); setTimeout(function () {
self.moveDraggableToTarget(target);
}, 50);
return;
}
offset = 0;
if (this.state.config.targetOutline === true) {
offset = 1;
}
this.inContainer = false;
this.containerEl.hide();
this.iconEl.detach();
this.iconEl.css('background-color', this.iconElBGColor);
this.iconEl.css('padding-left', this.iconElPadding);
this.iconEl.css('padding-right', this.iconElPadding);
this.iconEl.css('border', this.iconElBorder);
this.iconEl.css('width', this.iconWidth);
this.iconEl.css('height', this.iconHeight);
this.iconEl.css(
'left',
target.offset.left + 0.5 * target.w - this.iconWidth * 0.5 + offset - this.iconElLeftOffset
);
this.iconEl.css(
'top',
target.offset.top + 0.5 * target.h - this.iconHeight * 0.5 + offset
);
this.iconEl.appendTo(this.state.baseImageEl.parent());
if (this.labelEl !== null) {
this.labelEl.detach();
this.labelEl.css('background-color', this.state.config.labelBgColor);
this.labelEl.css('padding-left', 8);
this.labelEl.css('padding-right', 8);
this.labelEl.css('border', '1px solid black');
this.labelEl.css(
'left',
target.offset.left + 0.5 * target.w - this.labelWidth * 0.5 + offset - 9 // Account for padding, border.
);
this.labelEl.css(
'top',
target.offset.top + 0.5 * target.h + this.iconHeight * 0.5 + 5 + offset
);
this.labelEl.appendTo(this.state.baseImageEl.parent());
}
this.onTarget = target;
target.draggable.push(this.id);
if (target.numTextEl !== null) {
target.updateNumTextEl();
}
this.state.numDraggablesInSlider -= 1;
this.state.updateArrowOpacity();
}
function processDraggable(state, obj, objIndex) {
var draggableObj;
draggableObj = { draggableObj = {
'zIndex': objIndex, 'zIndex': objIndex,
...@@ -91,7 +213,10 @@ define(['logme', 'update_input'], function (logme, updateInput) { ...@@ -91,7 +213,10 @@ define(['logme', 'update_input'], function (logme, updateInput) {
'checkIfOnTarget': checkIfOnTarget, 'checkIfOnTarget': checkIfOnTarget,
'snapToTarget': snapToTarget, 'snapToTarget': snapToTarget,
'correctZIndexes': correctZIndexes, 'correctZIndexes': correctZIndexes,
'moveBackToSlider': moveBackToSlider 'moveBackToSlider': moveBackToSlider,
'moveDraggableToTarget': moveDraggableToTarget,
'moveDraggableToXY': moveDraggableToXY
}; };
draggableObj.containerEl = $( draggableObj.containerEl = $(
...@@ -334,7 +459,7 @@ define(['logme', 'update_input'], function (logme, updateInput) { ...@@ -334,7 +459,7 @@ define(['logme', 'update_input'], function (logme, updateInput) {
this.iconHeight * 0.5 this.iconHeight * 0.5
); );
this.iconEl.appendTo( this.iconEl.appendTo(
state.baseImageEl.parent() this.state.baseImageEl.parent()
); );
if (this.labelEl !== null) { if (this.labelEl !== null) {
...@@ -447,7 +572,7 @@ define(['logme', 'update_input'], function (logme, updateInput) { ...@@ -447,7 +572,7 @@ define(['logme', 'update_input'], function (logme, updateInput) {
this.mousePressed = false; this.mousePressed = false;
positionIE = this.iconEl.position(); positionIE = this.iconEl.position();
if (this.state.individualTargets === true) { if (this.state.config.individualTargets === true) {
if (this.checkIfOnTarget(positionIE) === true) { if (this.checkIfOnTarget(positionIE) === true) {
this.correctZIndexes(); this.correctZIndexes();
} else { } else {
...@@ -486,7 +611,7 @@ define(['logme', 'update_input'], function (logme, updateInput) { ...@@ -486,7 +611,7 @@ define(['logme', 'update_input'], function (logme, updateInput) {
} }
this.state.updateArrowOpacity(); this.state.updateArrowOpacity();
updateInput(this.state); updateInput.update(this.state);
} }
function removeObjIdFromTarget() { function removeObjIdFromTarget() {
...@@ -532,7 +657,7 @@ define(['logme', 'update_input'], function (logme, updateInput) { ...@@ -532,7 +657,7 @@ define(['logme', 'update_input'], function (logme, updateInput) {
// (with an ID different from the one we are checking // (with an ID different from the one we are checking
// against), then go to next target. // against), then go to next target.
if ( if (
(this.state.config.one_per_target === true) && (this.state.config.onePerTarget === true) &&
(target.draggable.length === 1) && (target.draggable.length === 1) &&
(target.draggable[0] !== this.id) (target.draggable[0] !== this.id)
) { ) {
...@@ -652,7 +777,7 @@ define(['logme', 'update_input'], function (logme, updateInput) { ...@@ -652,7 +777,7 @@ define(['logme', 'update_input'], function (logme, updateInput) {
function correctZIndexes() { function correctZIndexes() {
var draggablesInMe, c1, c2, highestZIndex; var draggablesInMe, c1, c2, highestZIndex;
if (this.state.individualTargets === true) { if (this.state.config.individualTargets === true) {
if (this.onTarget.draggable.length > 0) { if (this.onTarget.draggable.length > 0) {
draggablesInMe = []; draggablesInMe = [];
......
...@@ -65,11 +65,11 @@ define( ...@@ -65,11 +65,11 @@ define(
Scroller(state); Scroller(state);
Draggables.init(state); Draggables.init(state);
logme('After Draggables.init(state); state = ', state);
// Update the input element, checking first that it is not filled with // Update the input element, checking first that it is not filled with
// an answer from the server. // an answer from the server.
updateInput(state, true); if (updateInput.check(state) === false) {
updateInput.update(state);
}
}()); }());
} }
}); });
......
...@@ -45,7 +45,7 @@ define(['logme'], function (logme) { ...@@ -45,7 +45,7 @@ define(['logme'], function (logme) {
event.preventDefault(); event.preventDefault();
}); });
if (state.config.one_per_target === false) { if (state.config.onePerTarget === false) {
numTextEl = $( numTextEl = $(
'<div ' + '<div ' +
'style=" ' + 'style=" ' +
...@@ -86,7 +86,7 @@ define(['logme'], function (logme) { ...@@ -86,7 +86,7 @@ define(['logme'], function (logme) {
'updateNumTextEl': updateNumTextEl 'updateNumTextEl': updateNumTextEl
}; };
if (state.config.one_per_target === false) { if (state.config.onePerTarget === false) {
numTextEl.appendTo(state.baseImageEl.parent()); numTextEl.appendTo(state.baseImageEl.parent());
numTextEl.mousedown(function (event) { numTextEl.mousedown(function (event) {
event.preventDefault(); event.preventDefault();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment