Kinetic.TextToTextColumn = function(config){ Kinetic.Group.call(this, config); this.data = config.data; this.initExercise(); this.createExercise(); this.eventHandlers(); } Kinetic.TextToTextColumn.prototype = { createExercise: function(){ // columns var dropItemData = this.data.dropItems; this.dropItems = new Array(); for (var i = 0; i < dropItemData.length; i++){ this.dropItems[i] = new Kinetic.TextToTextColumnDropItem({ 'data': dropItemData[i], 'x': dropItemData[i].x, 'y': dropItemData[i].y }); this.add(this.dropItems[i]); } // drag item group this.itemGroup = new Kinetic.Group({ 'x': this.data.dragItemPositionArea.x, 'y': this.data.dragItemPositionArea.y }); this.add(this.itemGroup); // drag items var itemData = shuffleArray(this.data.dragItems); this.items = new Array(); var xPos = 0; var yPos = 0; var rowHeight = 0; for (var i = 0; i < itemData.length; i++){ this.items[i] = new Kinetic.DragItem({ 'data': itemData[i], 'x': xPos, 'draggable': true }); this.itemGroup.add(this.items[i]); if ((xPos + this.items[i].getWidth()) > this.data.dragItemPositionArea.width){ xPos = 0; yPos += rowHeight + 5; rowHeight = 0; } if (this.items[i].getHeight() > rowHeight){ rowHeight = this.items[i].getHeight(); } this.items[i].setX(xPos); this.items[i].setY(yPos); xPos += this.items[i].getWidth() + 5; } }, eventHandlers: function(){ var thisObject = this; for (var i = 0; i < this.items.length; i++){ this.items[i].on('dragstart', function(evt){ thisObject.dragStart(this, evt); }); this.items[i].on('dragend', function(evt){ thisObject.dragEnd(this, evt); }); } }, dragStart: function(thisObject, evt){ thisObject.moveToTop(); if (thisObject.dropItem != undefined){ thisObject.dropItem.removeItem(thisObject); thisObject.dropItem = null; } }, dragEnd: function(thisObject, evt){ var myCenterX = thisObject.getX() + this.itemGroup.getX() + (thisObject.getWidth() / 2); var myCenterY = thisObject.getY() + this.itemGroup.getY() + (thisObject.getHeight() / 2); // check drop items var hit = false; for (var i = 0; i < this.dropItems.length; i++){ var minX = this.dropItems[i].getX(); var minY = this.dropItems[i].getY(); var maxX = minX + this.dropItems[i].getWidth(); var maxY = minY + this.dropItems[i].getHeight(); if (myCenterX > minX && myCenterX < maxX && myCenterY > minY && myCenterY < maxY){ thisObject.dropItem = this.dropItems[i]; this.dropItems[i].addItem(thisObject); hit = true; break; } } if (!hit){ var oriX = thisObject.oriX; var oriY = thisObject.oriY; var tween = new Kinetic.Tween({ 'node': thisObject, 'x': oriX, 'y': oriY, 'duration': 0.2, 'easing': Kinetic.Easings.EaseInOut }); tween.play(); } }, checkAnswer: function(){ this.attempts -= 1; if (this.attempts == 0){ this.blockExercise(); } this.correct = true; var targetId = null; var dropItemId = null; var toMove = new Array(); for (var i = 0; i < this.items.length; i++){ targetId = this.items[i].data.target; dropItemId = null; if (this.items[i].dropItem != undefined){ dropItemId = this.items[i].dropItem.data.id; } if (targetId == dropItemId){ this.items[i].setCorrect(); } else { this.correct = false; this.items[i].setWrong(); // remove from wrong position if (dropItemId != null){ for (var j = 0; j < this.dropItems.length; j++){ if (this.dropItems[j].data.id == dropItemId){ this.dropItems[j].removeItem(this.items[i]); } } } // move to the right position (delayed) for (var j = 0; j < this.dropItems.length; j++){ if (this.dropItems[j].data.id == targetId){ toMove.push(new Array(this.dropItems[j], this.items[i])); } } } } window.setTimeout(function(){ for (var i = 0; i < toMove.length; i++){ toMove[i][0].addItem(toMove[i][1]); } }, 500); this.draw(); this.showFeedback(this.correct, this.attempts); } } Kinetic.Util.extend(Kinetic.TextToTextColumn, Kinetic.ExerciseType);