| 1 | 
           efrain | 
           1 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           2 | 
           //
  | 
        
        
            | 
            | 
           3 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           4 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           5 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           6 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           7 | 
           //
  | 
        
        
            | 
            | 
           8 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           9 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           10 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           11 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           12 | 
           //
  | 
        
        
            | 
            | 
           13 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           14 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           15 | 
              | 
        
        
            | 
            | 
           16 | 
           /**
  | 
        
        
            | 
            | 
           17 | 
            * JavaScript to make drag-drop into text questions work.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * Some vocabulary to help understand this code:
  | 
        
        
            | 
            | 
           20 | 
            *
  | 
        
        
            | 
            | 
           21 | 
            * The question text contains 'drops' - blanks into which the 'drags', the missing
  | 
        
        
            | 
            | 
           22 | 
            * words, can be put.
  | 
        
        
            | 
            | 
           23 | 
            *
  | 
        
        
            | 
            | 
           24 | 
            * The thing that can be moved into the drops are called 'drags'. There may be
  | 
        
        
            | 
            | 
           25 | 
            * multiple copies of the 'same' drag which does not really cause problems.
  | 
        
        
            | 
            | 
           26 | 
            * Each drag has a 'choice' number which is the value set on the drop's hidden
  | 
        
        
            | 
            | 
           27 | 
            * input when this drag is placed in a drop.
  | 
        
        
            | 
            | 
           28 | 
            *
  | 
        
        
            | 
            | 
           29 | 
            * These may be in separate 'groups', distinguished by colour.
  | 
        
        
            | 
            | 
           30 | 
            * Things can only interact with other things in the same group.
  | 
        
        
            | 
            | 
           31 | 
            * The groups are numbered from 1.
  | 
        
        
            | 
            | 
           32 | 
            *
  | 
        
        
            | 
            | 
           33 | 
            * The place where a given drag started from is called its 'home'.
  | 
        
        
            | 
            | 
           34 | 
            *
  | 
        
        
            | 
            | 
           35 | 
            * @module     qtype_ddwtos/ddwtos
  | 
        
        
            | 
            | 
           36 | 
            * @copyright  2018 The Open University
  | 
        
        
            | 
            | 
           37 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           38 | 
            * @since      3.6
  | 
        
        
            | 
            | 
           39 | 
            */
  | 
        
        
            | 
            | 
           40 | 
           define([
  | 
        
        
            | 
            | 
           41 | 
               'jquery',
  | 
        
        
            | 
            | 
           42 | 
               'core/dragdrop',
  | 
        
        
            | 
            | 
           43 | 
               'core/key_codes',
  | 
        
        
            | 
            | 
           44 | 
               'core_form/changechecker'
  | 
        
        
            | 
            | 
           45 | 
           ], function(
  | 
        
        
            | 
            | 
           46 | 
               $,
  | 
        
        
            | 
            | 
           47 | 
               dragDrop,
  | 
        
        
            | 
            | 
           48 | 
               keys,
  | 
        
        
            | 
            | 
           49 | 
               FormChangeChecker
  | 
        
        
            | 
            | 
           50 | 
           ) {
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
               "use strict";
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
               /**
  | 
        
        
            | 
            | 
           55 | 
                * Object to handle one drag-drop into text question.
  | 
        
        
            | 
            | 
           56 | 
                *
  | 
        
        
            | 
            | 
           57 | 
                * @param {String} containerId id of the outer div for this question.
  | 
        
        
            | 
            | 
           58 | 
                * @param {boolean} readOnly whether the question is being displayed read-only.
  | 
        
        
            | 
            | 
           59 | 
                * @constructor
  | 
        
        
            | 
            | 
           60 | 
                */
  | 
        
        
            | 
            | 
           61 | 
               function DragDropToTextQuestion(containerId, readOnly) {
  | 
        
        
            | 
            | 
           62 | 
                   this.containerId = containerId;
  | 
        
        
            | 
            | 
           63 | 
                   this.questionAnswer = {};
  | 
        
        
            | 
            | 
           64 | 
                   if (readOnly) {
  | 
        
        
            | 
            | 
           65 | 
                       this.getRoot().addClass('qtype_ddwtos-readonly');
  | 
        
        
            | 
            | 
           66 | 
                   }
  | 
        
        
            | 
            | 
           67 | 
                   this.resizeAllDragsAndDrops();
  | 
        
        
            | 
            | 
           68 | 
                   this.cloneDrags();
  | 
        
        
            | 
            | 
           69 | 
                   this.positionDrags();
  | 
        
        
            | 
            | 
           70 | 
               }
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
               /**
  | 
        
        
            | 
            | 
           73 | 
                * In each group, resize all the items to be the same size.
  | 
        
        
            | 
            | 
           74 | 
                */
  | 
        
        
            | 
            | 
           75 | 
               DragDropToTextQuestion.prototype.resizeAllDragsAndDrops = function() {
  | 
        
        
            | 
            | 
           76 | 
                   var thisQ = this;
  | 
        
        
            | 
            | 
           77 | 
                   this.getRoot().find('.answercontainer > div').each(function(i, node) {
  | 
        
        
            | 
            | 
           78 | 
                       thisQ.resizeAllDragsAndDropsInGroup(
  | 
        
        
            | 
            | 
           79 | 
                           thisQ.getClassnameNumericSuffix($(node), 'draggrouphomes'));
  | 
        
        
            | 
            | 
           80 | 
                   });
  | 
        
        
            | 
            | 
           81 | 
               };
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
               /**
  | 
        
        
            | 
            | 
           84 | 
                * In a given group, set all the drags and drops to be the same size.
  | 
        
        
            | 
            | 
           85 | 
                *
  | 
        
        
            | 
            | 
           86 | 
                * @param {int} group the group number.
  | 
        
        
            | 
            | 
           87 | 
                */
  | 
        
        
            | 
            | 
           88 | 
               DragDropToTextQuestion.prototype.resizeAllDragsAndDropsInGroup = function(group) {
  | 
        
        
            | 
            | 
           89 | 
                   var thisQ = this,
  | 
        
        
            | 
            | 
           90 | 
                       dragHomes = this.getRoot().find('.draggrouphomes' + group + ' span.draghome'),
  | 
        
        
            | 
            | 
           91 | 
                       maxWidth = 0,
  | 
        
        
            | 
            | 
           92 | 
                       maxHeight = 0;
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
                   // Find the maximum size of any drag in this groups.
  | 
        
        
            | 
            | 
           95 | 
                   dragHomes.each(function(i, drag) {
  | 
        
        
            | 
            | 
           96 | 
                       maxWidth = Math.max(maxWidth, Math.ceil(drag.offsetWidth));
  | 
        
        
            | 
            | 
           97 | 
                       maxHeight = Math.max(maxHeight, Math.ceil(0 + drag.offsetHeight));
  | 
        
        
            | 
            | 
           98 | 
                   });
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
                   // The size we will want to set is a bit bigger than this.
  | 
        
        
            | 
            | 
           101 | 
                   maxWidth += 8;
  | 
        
        
            | 
            | 
           102 | 
                   maxHeight += 2;
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
                   // Set each drag home to that size.
  | 
        
        
            | 
            | 
           105 | 
                   dragHomes.each(function(i, drag) {
  | 
        
        
            | 
            | 
           106 | 
                       thisQ.setElementSize(drag, maxWidth, maxHeight);
  | 
        
        
            | 
            | 
           107 | 
                   });
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
                   // Set each drop to that size.
  | 
        
        
            | 
            | 
           110 | 
                   this.getRoot().find('span.drop.group' + group).each(function(i, drop) {
  | 
        
        
            | 
            | 
           111 | 
                       thisQ.setElementSize(drop, maxWidth, maxHeight);
  | 
        
        
            | 
            | 
           112 | 
                   });
  | 
        
        
            | 
            | 
           113 | 
               };
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
               /**
  | 
        
        
            | 
            | 
           116 | 
                * Set a given DOM element to be a particular size.
  | 
        
        
            | 
            | 
           117 | 
                *
  | 
        
        
            | 
            | 
           118 | 
                * @param {HTMLElement} element
  | 
        
        
            | 
            | 
           119 | 
                * @param {int} width
  | 
        
        
            | 
            | 
           120 | 
                * @param {int} height
  | 
        
        
            | 
            | 
           121 | 
                */
  | 
        
        
            | 
            | 
           122 | 
               DragDropToTextQuestion.prototype.setElementSize = function(element, width, height) {
  | 
        
        
            | 
            | 
           123 | 
                   $(element).width(width).height(height).css('lineHeight', height + 'px');
  | 
        
        
            | 
            | 
           124 | 
               };
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
               /**
  | 
        
        
            | 
            | 
           127 | 
                * Invisible 'drag homes' are output by the renderer. These have the same properties
  | 
        
        
            | 
            | 
           128 | 
                * as the drag items but are invisible. We clone these invisible elements to make the
  | 
        
        
            | 
            | 
           129 | 
                * actual drag items.
  | 
        
        
            | 
            | 
           130 | 
                */
  | 
        
        
            | 
            | 
           131 | 
               DragDropToTextQuestion.prototype.cloneDrags = function() {
  | 
        
        
            | 
            | 
           132 | 
                   var thisQ = this;
  | 
        
        
            | 
            | 
           133 | 
                   thisQ.getRoot().find('span.draghome').each(function(index, draghome) {
  | 
        
        
            | 
            | 
           134 | 
                       var drag = $(draghome);
  | 
        
        
            | 
            | 
           135 | 
                       var placeHolder = drag.clone();
  | 
        
        
            | 
            | 
           136 | 
                       placeHolder.removeClass();
  | 
        
        
            | 
            | 
           137 | 
                       placeHolder.addClass('draghome choice' +
  | 
        
        
            | 
            | 
           138 | 
                           thisQ.getChoice(drag) + ' group' +
  | 
        
        
            | 
            | 
           139 | 
                           thisQ.getGroup(drag) + ' dragplaceholder');
  | 
        
        
            | 
            | 
           140 | 
                       drag.before(placeHolder);
  | 
        
        
            | 
            | 
           141 | 
                   });
  | 
        
        
            | 
            | 
           142 | 
               };
  | 
        
        
            | 
            | 
           143 | 
              | 
        
        
            | 
            | 
           144 | 
               /**
  | 
        
        
            | 
            | 
           145 | 
                * Update the position of drags.
  | 
        
        
            | 
            | 
           146 | 
                */
  | 
        
        
            | 
            | 
           147 | 
               DragDropToTextQuestion.prototype.positionDrags = function() {
  | 
        
        
            | 
            | 
           148 | 
                   var thisQ = this,
  | 
        
        
            | 
            | 
           149 | 
                       root = this.getRoot();
  | 
        
        
            | 
            | 
           150 | 
              | 
        
        
            | 
            | 
           151 | 
                   // First move all items back home.
  | 
        
        
            | 
            | 
           152 | 
                   root.find('span.draghome').not('.dragplaceholder').each(function(i, dragNode) {
  | 
        
        
            | 
            | 
           153 | 
                       var drag = $(dragNode),
  | 
        
        
            | 
            | 
           154 | 
                           currentPlace = thisQ.getClassnameNumericSuffix(drag, 'inplace');
  | 
        
        
            | 
            | 
           155 | 
                       drag.addClass('unplaced')
  | 
        
        
            | 
            | 
           156 | 
                           .removeClass('placed');
  | 
        
        
            | 
            | 
           157 | 
                       drag.removeAttr('tabindex');
  | 
        
        
            | 
            | 
           158 | 
                       if (currentPlace !== null) {
  | 
        
        
            | 
            | 
           159 | 
                           drag.removeClass('inplace' + currentPlace);
  | 
        
        
            | 
            | 
           160 | 
                       }
  | 
        
        
            | 
            | 
           161 | 
                   });
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
            | 
            | 
           163 | 
                   // Then place the once that should be placed.
  | 
        
        
            | 
            | 
           164 | 
                   root.find('input.placeinput').each(function(i, inputNode) {
  | 
        
        
            | 
            | 
           165 | 
                       var input = $(inputNode),
  | 
        
        
            | 
            | 
           166 | 
                           choice = input.val(),
  | 
        
        
            | 
            | 
           167 | 
                           place = thisQ.getPlace(input);
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
                       // Record the last known position of the drop.
  | 
        
        
            | 
            | 
           170 | 
                       var drop = root.find('.drop.place' + place),
  | 
        
        
            | 
            | 
           171 | 
                           dropPosition = drop.offset();
  | 
        
        
            | 
            | 
           172 | 
                       drop.data('prev-top', dropPosition.top).data('prev-left', dropPosition.left);
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
                       if (choice === '0') {
  | 
        
        
            | 
            | 
           175 | 
                           // No item in this place.
  | 
        
        
            | 
            | 
           176 | 
                           return;
  | 
        
        
            | 
            | 
           177 | 
                       }
  | 
        
        
            | 
            | 
           178 | 
              | 
        
        
            | 
            | 
           179 | 
                       // Get the unplaced drag.
  | 
        
        
            | 
            | 
           180 | 
                       var unplacedDrag = thisQ.getUnplacedChoice(thisQ.getGroup(input), choice);
  | 
        
        
            | 
            | 
           181 | 
                       // Get the clone of the drag.
  | 
        
        
            | 
            | 
           182 | 
                       var hiddenDrag = thisQ.getDragClone(unplacedDrag);
  | 
        
        
            | 
            | 
           183 | 
                       if (hiddenDrag.length) {
  | 
        
        
            | 
            | 
           184 | 
                           if (unplacedDrag.hasClass('infinite')) {
  | 
        
        
            | 
            | 
           185 | 
                               var noOfDrags = thisQ.noOfDropsInGroup(thisQ.getGroup(unplacedDrag));
  | 
        
        
            | 
            | 
           186 | 
                               var cloneDrags = thisQ.getInfiniteDragClones(unplacedDrag, false);
  | 
        
        
            | 
            | 
           187 | 
                               if (cloneDrags.length < noOfDrags) {
  | 
        
        
            | 
            | 
           188 | 
                                   var cloneDrag = unplacedDrag.clone();
  | 
        
        
            | 
            | 
           189 | 
                                   hiddenDrag.after(cloneDrag);
  | 
        
        
            | 
            | 
           190 | 
                                   questionManager.addEventHandlersToDrag(cloneDrag);
  | 
        
        
            | 
            | 
           191 | 
                               } else {
  | 
        
        
            | 
            | 
           192 | 
                                   hiddenDrag.addClass('active');
  | 
        
        
            | 
            | 
           193 | 
                               }
  | 
        
        
            | 
            | 
           194 | 
                           } else {
  | 
        
        
            | 
            | 
           195 | 
                               hiddenDrag.addClass('active');
  | 
        
        
            | 
            | 
           196 | 
                           }
  | 
        
        
            | 
            | 
           197 | 
                       }
  | 
        
        
            | 
            | 
           198 | 
                       // Send the drag to drop.
  | 
        
        
            | 
            | 
           199 | 
                       thisQ.sendDragToDrop(thisQ.getUnplacedChoice(thisQ.getGroup(input), choice), drop);
  | 
        
        
            | 
            | 
           200 | 
                   });
  | 
        
        
            | 
            | 
           201 | 
              | 
        
        
            | 
            | 
           202 | 
                   // Save the question answer.
  | 
        
        
            | 
            | 
           203 | 
                   thisQ.questionAnswer = thisQ.getQuestionAnsweredValues();
  | 
        
        
            | 
            | 
           204 | 
               };
  | 
        
        
            | 
            | 
           205 | 
              | 
        
        
            | 
            | 
           206 | 
               /**
  | 
        
        
            | 
            | 
           207 | 
                * Get the question answered values.
  | 
        
        
            | 
            | 
           208 | 
                *
  | 
        
        
            | 
            | 
           209 | 
                * @return {Object} Contain key-value with key is the input id and value is the input value.
  | 
        
        
            | 
            | 
           210 | 
                */
  | 
        
        
            | 
            | 
           211 | 
               DragDropToTextQuestion.prototype.getQuestionAnsweredValues = function() {
  | 
        
        
            | 
            | 
           212 | 
                   let result = {};
  | 
        
        
            | 
            | 
           213 | 
                   this.getRoot().find('input.placeinput').each((i, inputNode) => {
  | 
        
        
            | 
            | 
           214 | 
                       result[inputNode.id] = inputNode.value;
  | 
        
        
            | 
            | 
           215 | 
                   });
  | 
        
        
            | 
            | 
           216 | 
              | 
        
        
            | 
            | 
           217 | 
                   return result;
  | 
        
        
            | 
            | 
           218 | 
               };
  | 
        
        
            | 
            | 
           219 | 
              | 
        
        
            | 
            | 
           220 | 
               /**
  | 
        
        
            | 
            | 
           221 | 
                * Check if the question is being interacted or not.
  | 
        
        
            | 
            | 
           222 | 
                *
  | 
        
        
            | 
            | 
           223 | 
                * @return {boolean} Return true if the user has changed the question-answer.
  | 
        
        
            | 
            | 
           224 | 
                */
  | 
        
        
            | 
            | 
           225 | 
               DragDropToTextQuestion.prototype.isQuestionInteracted = function() {
  | 
        
        
            | 
            | 
           226 | 
                   const oldAnswer = this.questionAnswer;
  | 
        
        
            | 
            | 
           227 | 
                   const newAnswer = this.getQuestionAnsweredValues();
  | 
        
        
            | 
            | 
           228 | 
                   let isInteracted = false;
  | 
        
        
            | 
            | 
           229 | 
              | 
        
        
            | 
            | 
           230 | 
                   // First, check both answers have the same structure or not.
  | 
        
        
            | 
            | 
           231 | 
                   if (JSON.stringify(newAnswer) !== JSON.stringify(oldAnswer)) {
  | 
        
        
            | 
            | 
           232 | 
                       isInteracted = true;
  | 
        
        
            | 
            | 
           233 | 
                       return isInteracted;
  | 
        
        
            | 
            | 
           234 | 
                   }
  | 
        
        
            | 
            | 
           235 | 
                   // Check the values.
  | 
        
        
            | 
            | 
           236 | 
                   Object.keys(newAnswer).forEach(key => {
  | 
        
        
            | 
            | 
           237 | 
                       if (newAnswer[key] !== oldAnswer[key]) {
  | 
        
        
            | 
            | 
           238 | 
                           isInteracted = true;
  | 
        
        
            | 
            | 
           239 | 
                       }
  | 
        
        
            | 
            | 
           240 | 
                   });
  | 
        
        
            | 
            | 
           241 | 
              | 
        
        
            | 
            | 
           242 | 
                   return isInteracted;
  | 
        
        
            | 
            | 
           243 | 
               };
  | 
        
        
            | 
            | 
           244 | 
              | 
        
        
            | 
            | 
           245 | 
               /**
  | 
        
        
            | 
            | 
           246 | 
                * Handles the start of dragging an item.
  | 
        
        
            | 
            | 
           247 | 
                *
  | 
        
        
            | 
            | 
           248 | 
                * @param {Event} e the touch start or mouse down event.
  | 
        
        
            | 
            | 
           249 | 
                */
  | 
        
        
            | 
            | 
           250 | 
               DragDropToTextQuestion.prototype.handleDragStart = function(e) {
  | 
        
        
            | 
            | 
           251 | 
                   var thisQ = this,
  | 
        
        
            | 
            | 
           252 | 
                       drag = $(e.target).closest('.draghome');
  | 
        
        
            | 
            | 
           253 | 
              | 
        
        
            | 
            | 
           254 | 
                   var info = dragDrop.prepare(e);
  | 
        
        
            | 
            | 
           255 | 
                   if (!info.start || drag.hasClass('beingdragged')) {
  | 
        
        
            | 
            | 
           256 | 
                       return;
  | 
        
        
            | 
            | 
           257 | 
                   }
  | 
        
        
            | 
            | 
           258 | 
              | 
        
        
            | 
            | 
           259 | 
                   drag.addClass('beingdragged');
  | 
        
        
            | 
            | 
           260 | 
                   var currentPlace = this.getClassnameNumericSuffix(drag, 'inplace');
  | 
        
        
            | 
            | 
           261 | 
                   if (currentPlace !== null) {
  | 
        
        
            | 
            | 
           262 | 
                       this.setInputValue(currentPlace, 0);
  | 
        
        
            | 
            | 
           263 | 
                       drag.removeClass('inplace' + currentPlace);
  | 
        
        
            | 
            | 
           264 | 
                       var hiddenDrop = thisQ.getDrop(drag, currentPlace);
  | 
        
        
            | 
            | 
           265 | 
                       if (hiddenDrop.length) {
  | 
        
        
            | 
            | 
           266 | 
                           hiddenDrop.addClass('active');
  | 
        
        
            | 
            | 
           267 | 
                           drag.offset(hiddenDrop.offset());
  | 
        
        
            | 
            | 
           268 | 
                       }
  | 
        
        
            | 
            | 
           269 | 
                   } else {
  | 
        
        
            | 
            | 
           270 | 
                       var hiddenDrag = thisQ.getDragClone(drag);
  | 
        
        
            | 
            | 
           271 | 
                       if (hiddenDrag.length) {
  | 
        
        
            | 
            | 
           272 | 
                           if (drag.hasClass('infinite')) {
  | 
        
        
            | 
            | 
           273 | 
                               var noOfDrags = this.noOfDropsInGroup(this.getGroup(drag));
  | 
        
        
            | 
            | 
           274 | 
                               var cloneDrags = this.getInfiniteDragClones(drag, false);
  | 
        
        
            | 
            | 
           275 | 
                               if (cloneDrags.length < noOfDrags) {
  | 
        
        
            | 
            | 
           276 | 
                                   var cloneDrag = drag.clone();
  | 
        
        
            | 
            | 
           277 | 
                                   cloneDrag.removeClass('beingdragged');
  | 
        
        
            | 
            | 
           278 | 
                                   hiddenDrag.after(cloneDrag);
  | 
        
        
            | 
            | 
           279 | 
                                   questionManager.addEventHandlersToDrag(cloneDrag);
  | 
        
        
            | 
            | 
           280 | 
                                   drag.offset(cloneDrag.offset());
  | 
        
        
            | 
            | 
           281 | 
                               } else {
  | 
        
        
            | 
            | 
           282 | 
                                   hiddenDrag.addClass('active');
  | 
        
        
            | 
            | 
           283 | 
                                   drag.offset(hiddenDrag.offset());
  | 
        
        
            | 
            | 
           284 | 
                               }
  | 
        
        
            | 
            | 
           285 | 
                           } else {
  | 
        
        
            | 
            | 
           286 | 
                               hiddenDrag.addClass('active');
  | 
        
        
            | 
            | 
           287 | 
                               drag.offset(hiddenDrag.offset());
  | 
        
        
            | 
            | 
           288 | 
                           }
  | 
        
        
            | 
            | 
           289 | 
                       }
  | 
        
        
            | 
            | 
           290 | 
                   }
  | 
        
        
            | 
            | 
           291 | 
              | 
        
        
            | 
            | 
           292 | 
                   dragDrop.start(e, drag, function(x, y, drag) {
  | 
        
        
            | 
            | 
           293 | 
                       thisQ.dragMove(x, y, drag);
  | 
        
        
            | 
            | 
           294 | 
                   }, function(x, y, drag) {
  | 
        
        
            | 
            | 
           295 | 
                       thisQ.dragEnd(x, y, drag);
  | 
        
        
            | 
            | 
           296 | 
                   });
  | 
        
        
            | 
            | 
           297 | 
               };
  | 
        
        
            | 
            | 
           298 | 
              | 
        
        
            | 
            | 
           299 | 
               /**
  | 
        
        
            | 
            | 
           300 | 
                * Called whenever the currently dragged items moves.
  | 
        
        
            | 
            | 
           301 | 
                *
  | 
        
        
            | 
            | 
           302 | 
                * @param {Number} pageX the x position.
  | 
        
        
            | 
            | 
           303 | 
                * @param {Number} pageY the y position.
  | 
        
        
            | 
            | 
           304 | 
                * @param {jQuery} drag the item being moved.
  | 
        
        
            | 
            | 
           305 | 
                */
  | 
        
        
            | 
            | 
           306 | 
               DragDropToTextQuestion.prototype.dragMove = function(pageX, pageY, drag) {
  | 
        
        
            | 
            | 
           307 | 
                   var thisQ = this;
  | 
        
        
            | 
            | 
           308 | 
                   this.getRoot().find('span.group' + this.getGroup(drag)).not('.beingdragged').each(function(i, dropNode) {
  | 
        
        
            | 
            | 
           309 | 
                       var drop = $(dropNode);
  | 
        
        
            | 
            | 
           310 | 
                       if (thisQ.isPointInDrop(pageX, pageY, drop)) {
  | 
        
        
            | 
            | 
           311 | 
                           drop.addClass('valid-drag-over-drop');
  | 
        
        
            | 
            | 
           312 | 
                       } else {
  | 
        
        
            | 
            | 
           313 | 
                           drop.removeClass('valid-drag-over-drop');
  | 
        
        
            | 
            | 
           314 | 
                       }
  | 
        
        
            | 
            | 
           315 | 
                   });
  | 
        
        
            | 
            | 
           316 | 
               };
  | 
        
        
            | 
            | 
           317 | 
              | 
        
        
            | 
            | 
           318 | 
               /**
  | 
        
        
            | 
            | 
           319 | 
                * Called when user drops a drag item.
  | 
        
        
            | 
            | 
           320 | 
                *
  | 
        
        
            | 
            | 
           321 | 
                * @param {Number} pageX the x position.
  | 
        
        
            | 
            | 
           322 | 
                * @param {Number} pageY the y position.
  | 
        
        
            | 
            | 
           323 | 
                * @param {jQuery} drag the item being moved.
  | 
        
        
            | 
            | 
           324 | 
                */
  | 
        
        
            | 
            | 
           325 | 
               DragDropToTextQuestion.prototype.dragEnd = function(pageX, pageY, drag) {
  | 
        
        
            | 
            | 
           326 | 
                   var thisQ = this,
  | 
        
        
            | 
            | 
           327 | 
                       root = this.getRoot(),
  | 
        
        
            | 
            | 
           328 | 
                       placed = false;
  | 
        
        
            | 
            | 
           329 | 
                   root.find('span.group' + this.getGroup(drag)).not('.beingdragged').each(function(i, dropNode) {
  | 
        
        
            | 
            | 
           330 | 
                       if (placed) {
  | 
        
        
            | 
            | 
           331 | 
                           return false;
  | 
        
        
            | 
            | 
           332 | 
                       }
  | 
        
        
            | 
            | 
           333 | 
                       const dropZone = $(dropNode);
  | 
        
        
            | 
            | 
           334 | 
                       if (!thisQ.isPointInDrop(pageX, pageY, dropZone)) {
  | 
        
        
            | 
            | 
           335 | 
                           // Not this drop zone.
  | 
        
        
            | 
            | 
           336 | 
                           return true;
  | 
        
        
            | 
            | 
           337 | 
                       }
  | 
        
        
            | 
            | 
           338 | 
                       let drop = null;
  | 
        
        
            | 
            | 
           339 | 
                       if (dropZone.hasClass('placed')) {
  | 
        
        
            | 
            | 
           340 | 
                           // This is an placed drag item in a drop.
  | 
        
        
            | 
            | 
           341 | 
                           dropZone.removeClass('valid-drag-over-drop');
  | 
        
        
            | 
            | 
           342 | 
                           // Get the correct drop.
  | 
        
        
            | 
            | 
           343 | 
                           drop = thisQ.getDrop(drag, thisQ.getClassnameNumericSuffix(dropZone, 'inplace'));
  | 
        
        
            | 
            | 
           344 | 
                       } else {
  | 
        
        
            | 
            | 
           345 | 
                           // Empty drop.
  | 
        
        
            | 
            | 
           346 | 
                           drop = dropZone;
  | 
        
        
            | 
            | 
           347 | 
                       }
  | 
        
        
            | 
            | 
           348 | 
                       // Now put this drag into the drop.
  | 
        
        
            | 
            | 
           349 | 
                       drop.removeClass('valid-drag-over-drop');
  | 
        
        
            | 
            | 
           350 | 
                       thisQ.sendDragToDrop(drag, drop);
  | 
        
        
            | 
            | 
           351 | 
                       placed = true;
  | 
        
        
            | 
            | 
           352 | 
                       return false; // Stop the each() here.
  | 
        
        
            | 
            | 
           353 | 
                   });
  | 
        
        
            | 
            | 
           354 | 
                   if (!placed) {
  | 
        
        
            | 
            | 
           355 | 
                       this.sendDragHome(drag);
  | 
        
        
            | 
            | 
           356 | 
                   }
  | 
        
        
            | 
            | 
           357 | 
               };
  | 
        
        
            | 
            | 
           358 | 
              | 
        
        
            | 
            | 
           359 | 
               /**
  | 
        
        
            | 
            | 
           360 | 
                * Animate a drag item into a given place (or back home).
  | 
        
        
            | 
            | 
           361 | 
                *
  | 
        
        
            | 
            | 
           362 | 
                * @param {jQuery|null} drag the item to place. If null, clear the place.
  | 
        
        
            | 
            | 
           363 | 
                * @param {jQuery} drop the place to put it.
  | 
        
        
            | 
            | 
           364 | 
                */
  | 
        
        
            | 
            | 
           365 | 
               DragDropToTextQuestion.prototype.sendDragToDrop = function(drag, drop) {
  | 
        
        
            | 
            | 
           366 | 
                   // Send drag home if there is no place in drop.
  | 
        
        
            | 
            | 
           367 | 
                   if (this.getPlace(drop) === null) {
  | 
        
        
            | 
            | 
           368 | 
                       this.sendDragHome(drag);
  | 
        
        
            | 
            | 
           369 | 
                       return;
  | 
        
        
            | 
            | 
           370 | 
                   }
  | 
        
        
            | 
            | 
           371 | 
              | 
        
        
            | 
            | 
           372 | 
                   // Is there already a drag in this drop? if so, evict it.
  | 
        
        
            | 
            | 
           373 | 
                   var oldDrag = this.getCurrentDragInPlace(this.getPlace(drop));
  | 
        
        
            | 
            | 
           374 | 
                   if (oldDrag.length !== 0) {
  | 
        
        
            | 
            | 
           375 | 
                       var currentPlace = this.getClassnameNumericSuffix(oldDrag, 'inplace');
  | 
        
        
            | 
            | 
           376 | 
                       // When infinite group and there is already a drag in a drop, reject the exact clone in the same drop.
  | 
        
        
            | 
            | 
           377 | 
                       if (this.hasDropSameDrag(currentPlace, drop, oldDrag, drag)) {
  | 
        
        
            | 
            | 
           378 | 
                           this.sendDragHome(drag);
  | 
        
        
            | 
            | 
           379 | 
                           return;
  | 
        
        
            | 
            | 
           380 | 
                       }
  | 
        
        
            | 
            | 
           381 | 
                       var hiddenDrop = this.getDrop(oldDrag, currentPlace);
  | 
        
        
            | 
            | 
           382 | 
                       hiddenDrop.addClass('active');
  | 
        
        
            | 
            | 
           383 | 
                       oldDrag.addClass('beingdragged');
  | 
        
        
            | 
            | 
           384 | 
                       oldDrag.offset(hiddenDrop.offset());
  | 
        
        
            | 
            | 
           385 | 
                       this.sendDragHome(oldDrag);
  | 
        
        
            | 
            | 
           386 | 
                   }
  | 
        
        
            | 
            | 
           387 | 
              | 
        
        
            | 
            | 
           388 | 
                   if (drag.length === 0) {
  | 
        
        
            | 
            | 
           389 | 
                       this.setInputValue(this.getPlace(drop), 0);
  | 
        
        
            | 
            | 
           390 | 
                       if (drop.data('isfocus')) {
  | 
        
        
            | 
            | 
           391 | 
                           drop.focus();
  | 
        
        
            | 
            | 
           392 | 
                       }
  | 
        
        
            | 
            | 
           393 | 
                   } else {
  | 
        
        
            | 
            | 
           394 | 
                       // Prevent the drag item drop into two drop-zone.
  | 
        
        
            | 
            | 
           395 | 
                       if (this.getClassnameNumericSuffix(drag, 'inplace')) {
  | 
        
        
            | 
            | 
           396 | 
                           return;
  | 
        
        
            | 
            | 
           397 | 
                       }
  | 
        
        
            | 
            | 
           398 | 
              | 
        
        
            | 
            | 
           399 | 
                       this.setInputValue(this.getPlace(drop), this.getChoice(drag));
  | 
        
        
            | 
            | 
           400 | 
                       drag.removeClass('unplaced')
  | 
        
        
            | 
            | 
           401 | 
                           .addClass('placed inplace' + this.getPlace(drop));
  | 
        
        
            | 
            | 
           402 | 
                       drag.attr('tabindex', 0);
  | 
        
        
            | 
            | 
           403 | 
                       this.animateTo(drag, drop);
  | 
        
        
            | 
            | 
           404 | 
                   }
  | 
        
        
            | 
            | 
           405 | 
               };
  | 
        
        
            | 
            | 
           406 | 
              | 
        
        
            | 
            | 
           407 | 
               /**
  | 
        
        
            | 
            | 
           408 | 
                * When infinite group and there is already a drag in a drop, reject the exact clone in the same drop.
  | 
        
        
            | 
            | 
           409 | 
                *
  | 
        
        
            | 
            | 
           410 | 
                * @param {int} currentPlace  the position of the current drop.
  | 
        
        
            | 
            | 
           411 | 
                * @param {jQuery} drop the drop containing a drag.
  | 
        
        
            | 
            | 
           412 | 
                * @param {jQuery} oldDrag the drag already placed in drop.
  | 
        
        
            | 
            | 
           413 | 
                * @param {jQuery} drag the new drag which is exactly the same (clone) as oldDrag .
  | 
        
        
            | 
            | 
           414 | 
                * @returns {boolean}
  | 
        
        
            | 
            | 
           415 | 
                */
  | 
        
        
            | 
            | 
           416 | 
               DragDropToTextQuestion.prototype.hasDropSameDrag = function(currentPlace, drop, oldDrag, drag) {
  | 
        
        
            | 
            | 
           417 | 
                   if (drag.hasClass('infinite')) {
  | 
        
        
            | 
            | 
           418 | 
                       return drop.hasClass('place' + currentPlace) &&
  | 
        
        
            | 
            | 
           419 | 
                           this.getGroup(drag) === this.getGroup(drop) &&
  | 
        
        
            | 
            | 
           420 | 
                           this.getChoice(drag) === this.getChoice(oldDrag) &&
  | 
        
        
            | 
            | 
           421 | 
                           this.getGroup(drag) === this.getGroup(oldDrag);
  | 
        
        
            | 
            | 
           422 | 
                   }
  | 
        
        
            | 
            | 
           423 | 
                   return false;
  | 
        
        
            | 
            | 
           424 | 
               };
  | 
        
        
            | 
            | 
           425 | 
              | 
        
        
            | 
            | 
           426 | 
               /**
  | 
        
        
            | 
            | 
           427 | 
                * Animate a drag back to its home.
  | 
        
        
            | 
            | 
           428 | 
                *
  | 
        
        
            | 
            | 
           429 | 
                * @param {jQuery} drag the item being moved.
  | 
        
        
            | 
            | 
           430 | 
                */
  | 
        
        
            | 
            | 
           431 | 
               DragDropToTextQuestion.prototype.sendDragHome = function(drag) {
  | 
        
        
            | 
            | 
           432 | 
                   var currentPlace = this.getClassnameNumericSuffix(drag, 'inplace');
  | 
        
        
            | 
            | 
           433 | 
                   if (currentPlace !== null) {
  | 
        
        
            | 
            | 
           434 | 
                       drag.removeClass('inplace' + currentPlace);
  | 
        
        
            | 
            | 
           435 | 
                   }
  | 
        
        
            | 
            | 
           436 | 
                   drag.data('unplaced', true);
  | 
        
        
            | 
            | 
           437 | 
              | 
        
        
            | 
            | 
           438 | 
                   this.animateTo(drag, this.getDragHome(this.getGroup(drag), this.getChoice(drag)));
  | 
        
        
            | 
            | 
           439 | 
               };
  | 
        
        
            | 
            | 
           440 | 
              | 
        
        
            | 
            | 
           441 | 
               /**
  | 
        
        
            | 
            | 
           442 | 
                * Handles keyboard events on drops.
  | 
        
        
            | 
            | 
           443 | 
                *
  | 
        
        
            | 
            | 
           444 | 
                * Drops are focusable. Once focused, right/down/space switches to the next choice, and
  | 
        
        
            | 
            | 
           445 | 
                * left/up switches to the previous. Escape clear.
  | 
        
        
            | 
            | 
           446 | 
                *
  | 
        
        
            | 
            | 
           447 | 
                * @param {KeyboardEvent} e
  | 
        
        
            | 
            | 
           448 | 
                */
  | 
        
        
            | 
            | 
           449 | 
               DragDropToTextQuestion.prototype.handleKeyPress = function(e) {
  | 
        
        
            | 
            | 
           450 | 
                   var drop = $(e.target).closest('.drop');
  | 
        
        
            | 
            | 
           451 | 
                   if (drop.length === 0) {
  | 
        
        
            | 
            | 
           452 | 
                       var placedDrag = $(e.target);
  | 
        
        
            | 
            | 
           453 | 
                       var currentPlace = this.getClassnameNumericSuffix(placedDrag, 'inplace');
  | 
        
        
            | 
            | 
           454 | 
                       if (currentPlace !== null) {
  | 
        
        
            | 
            | 
           455 | 
                           drop = this.getDrop(placedDrag, currentPlace);
  | 
        
        
            | 
            | 
           456 | 
                       }
  | 
        
        
            | 
            | 
           457 | 
                   }
  | 
        
        
            | 
            | 
           458 | 
                   var currentDrag = this.getCurrentDragInPlace(this.getPlace(drop)),
  | 
        
        
            | 
            | 
           459 | 
                       nextDrag = $();
  | 
        
        
            | 
            | 
           460 | 
              | 
        
        
            | 
            | 
           461 | 
                   switch (e.keyCode) {
  | 
        
        
            | 
            | 
           462 | 
                       case keys.space:
  | 
        
        
            | 
            | 
           463 | 
                       case keys.arrowRight:
  | 
        
        
            | 
            | 
           464 | 
                       case keys.arrowDown:
  | 
        
        
            | 
            | 
           465 | 
                           nextDrag = this.getNextDrag(this.getGroup(drop), currentDrag);
  | 
        
        
            | 
            | 
           466 | 
                           break;
  | 
        
        
            | 
            | 
           467 | 
              | 
        
        
            | 
            | 
           468 | 
                       case keys.arrowLeft:
  | 
        
        
            | 
            | 
           469 | 
                       case keys.arrowUp:
  | 
        
        
            | 
            | 
           470 | 
                           nextDrag = this.getPreviousDrag(this.getGroup(drop), currentDrag);
  | 
        
        
            | 
            | 
           471 | 
                           break;
  | 
        
        
            | 
            | 
           472 | 
              | 
        
        
            | 
            | 
           473 | 
                       case keys.escape:
  | 
        
        
            | 
            | 
           474 | 
                           break;
  | 
        
        
            | 
            | 
           475 | 
              | 
        
        
            | 
            | 
           476 | 
                       default:
  | 
        
        
            | 
            | 
           477 | 
                           questionManager.isKeyboardNavigation = false;
  | 
        
        
            | 
            | 
           478 | 
                           return; // To avoid the preventDefault below.
  | 
        
        
            | 
            | 
           479 | 
                   }
  | 
        
        
            | 
            | 
           480 | 
              | 
        
        
            | 
            | 
           481 | 
                   if (nextDrag.length) {
  | 
        
        
            | 
            | 
           482 | 
                       nextDrag.data('isfocus', true);
  | 
        
        
            | 
            | 
           483 | 
                       nextDrag.addClass('beingdragged');
  | 
        
        
            | 
            | 
           484 | 
                       var hiddenDrag = this.getDragClone(nextDrag);
  | 
        
        
            | 
            | 
           485 | 
                       if (hiddenDrag.length) {
  | 
        
        
            | 
            | 
           486 | 
                           if (nextDrag.hasClass('infinite')) {
  | 
        
        
            | 
            | 
           487 | 
                               var noOfDrags = this.noOfDropsInGroup(this.getGroup(nextDrag));
  | 
        
        
            | 
            | 
           488 | 
                               var cloneDrags = this.getInfiniteDragClones(nextDrag, false);
  | 
        
        
            | 
            | 
           489 | 
                               if (cloneDrags.length < noOfDrags) {
  | 
        
        
            | 
            | 
           490 | 
                                   var cloneDrag = nextDrag.clone();
  | 
        
        
            | 
            | 
           491 | 
                                   cloneDrag.removeClass('beingdragged');
  | 
        
        
            | 
            | 
           492 | 
                                   cloneDrag.removeAttr('tabindex');
  | 
        
        
            | 
            | 
           493 | 
                                   hiddenDrag.after(cloneDrag);
  | 
        
        
            | 
            | 
           494 | 
                                   questionManager.addEventHandlersToDrag(cloneDrag);
  | 
        
        
            | 
            | 
           495 | 
                                   nextDrag.offset(cloneDrag.offset());
  | 
        
        
            | 
            | 
           496 | 
                               } else {
  | 
        
        
            | 
            | 
           497 | 
                                   hiddenDrag.addClass('active');
  | 
        
        
            | 
            | 
           498 | 
                                   nextDrag.offset(hiddenDrag.offset());
  | 
        
        
            | 
            | 
           499 | 
                               }
  | 
        
        
            | 
            | 
           500 | 
                           } else {
  | 
        
        
            | 
            | 
           501 | 
                               hiddenDrag.addClass('active');
  | 
        
        
            | 
            | 
           502 | 
                               nextDrag.offset(hiddenDrag.offset());
  | 
        
        
            | 
            | 
           503 | 
                           }
  | 
        
        
            | 
            | 
           504 | 
                       }
  | 
        
        
            | 
            | 
           505 | 
                   } else {
  | 
        
        
            | 
            | 
           506 | 
                       drop.data('isfocus', true);
  | 
        
        
            | 
            | 
           507 | 
                   }
  | 
        
        
            | 
            | 
           508 | 
              | 
        
        
            | 
            | 
           509 | 
                   e.preventDefault();
  | 
        
        
            | 
            | 
           510 | 
                   this.sendDragToDrop(nextDrag, drop);
  | 
        
        
            | 
            | 
           511 | 
               };
  | 
        
        
            | 
            | 
           512 | 
              | 
        
        
            | 
            | 
           513 | 
               /**
  | 
        
        
            | 
            | 
           514 | 
                * Choose the next drag in a group.
  | 
        
        
            | 
            | 
           515 | 
                *
  | 
        
        
            | 
            | 
           516 | 
                * @param {int} group which group.
  | 
        
        
            | 
            | 
           517 | 
                * @param {jQuery} drag current choice (empty jQuery if there isn't one).
  | 
        
        
            | 
            | 
           518 | 
                * @return {jQuery} the next drag in that group, or null if there wasn't one.
  | 
        
        
            | 
            | 
           519 | 
                */
  | 
        
        
            | 
            | 
           520 | 
               DragDropToTextQuestion.prototype.getNextDrag = function(group, drag) {
  | 
        
        
            | 
            | 
           521 | 
                   var choice,
  | 
        
        
            | 
            | 
           522 | 
                       numChoices = this.noOfChoicesInGroup(group);
  | 
        
        
            | 
            | 
           523 | 
              | 
        
        
            | 
            | 
           524 | 
                   if (drag.length === 0) {
  | 
        
        
            | 
            | 
           525 | 
                       choice = 1; // Was empty, so we want to select the first choice.
  | 
        
        
            | 
            | 
           526 | 
                   } else {
  | 
        
        
            | 
            | 
           527 | 
                       choice = this.getChoice(drag) + 1;
  | 
        
        
            | 
            | 
           528 | 
                   }
  | 
        
        
            | 
            | 
           529 | 
              | 
        
        
            | 
            | 
           530 | 
                   var next = this.getUnplacedChoice(group, choice);
  | 
        
        
            | 
            | 
           531 | 
                   while (next.length === 0 && choice < numChoices) {
  | 
        
        
            | 
            | 
           532 | 
                       choice++;
  | 
        
        
            | 
            | 
           533 | 
                       next = this.getUnplacedChoice(group, choice);
  | 
        
        
            | 
            | 
           534 | 
                   }
  | 
        
        
            | 
            | 
           535 | 
              | 
        
        
            | 
            | 
           536 | 
                   return next;
  | 
        
        
            | 
            | 
           537 | 
               };
  | 
        
        
            | 
            | 
           538 | 
              | 
        
        
            | 
            | 
           539 | 
               /**
  | 
        
        
            | 
            | 
           540 | 
                * Choose the previous drag in a group.
  | 
        
        
            | 
            | 
           541 | 
                *
  | 
        
        
            | 
            | 
           542 | 
                * @param {int} group which group.
  | 
        
        
            | 
            | 
           543 | 
                * @param {jQuery} drag current choice (empty jQuery if there isn't one).
  | 
        
        
            | 
            | 
           544 | 
                * @return {jQuery} the next drag in that group, or null if there wasn't one.
  | 
        
        
            | 
            | 
           545 | 
                */
  | 
        
        
            | 
            | 
           546 | 
               DragDropToTextQuestion.prototype.getPreviousDrag = function(group, drag) {
  | 
        
        
            | 
            | 
           547 | 
                   var choice;
  | 
        
        
            | 
            | 
           548 | 
              | 
        
        
            | 
            | 
           549 | 
                   if (drag.length === 0) {
  | 
        
        
            | 
            | 
           550 | 
                       choice = this.noOfChoicesInGroup(group);
  | 
        
        
            | 
            | 
           551 | 
                   } else {
  | 
        
        
            | 
            | 
           552 | 
                       choice = this.getChoice(drag) - 1;
  | 
        
        
            | 
            | 
           553 | 
                   }
  | 
        
        
            | 
            | 
           554 | 
              | 
        
        
            | 
            | 
           555 | 
                   var previous = this.getUnplacedChoice(group, choice);
  | 
        
        
            | 
            | 
           556 | 
                   while (previous.length === 0 && choice > 1) {
  | 
        
        
            | 
            | 
           557 | 
                       choice--;
  | 
        
        
            | 
            | 
           558 | 
                       previous = this.getUnplacedChoice(group, choice);
  | 
        
        
            | 
            | 
           559 | 
                   }
  | 
        
        
            | 
            | 
           560 | 
              | 
        
        
            | 
            | 
           561 | 
                   // Does this choice exist?
  | 
        
        
            | 
            | 
           562 | 
                   return previous;
  | 
        
        
            | 
            | 
           563 | 
               };
  | 
        
        
            | 
            | 
           564 | 
              | 
        
        
            | 
            | 
           565 | 
               /**
  | 
        
        
            | 
            | 
           566 | 
                * Animate an object to the given destination.
  | 
        
        
            | 
            | 
           567 | 
                *
  | 
        
        
            | 
            | 
           568 | 
                * @param {jQuery} drag the element to be animated.
  | 
        
        
            | 
            | 
           569 | 
                * @param {jQuery} target element marking the place to move it to.
  | 
        
        
            | 
            | 
           570 | 
                */
  | 
        
        
            | 
            | 
           571 | 
               DragDropToTextQuestion.prototype.animateTo = function(drag, target) {
  | 
        
        
            | 
            | 
           572 | 
                   var currentPos = drag.offset(),
  | 
        
        
            | 
            | 
           573 | 
                       targetPos = target.offset(),
  | 
        
        
            | 
            | 
           574 | 
                       thisQ = this;
  | 
        
        
            | 
            | 
           575 | 
              | 
        
        
            | 
            | 
           576 | 
                   M.util.js_pending('qtype_ddwtos-animate-' + thisQ.containerId);
  | 
        
        
            | 
            | 
           577 | 
                   // Animate works in terms of CSS position, whereas locating an object
  | 
        
        
            | 
            | 
           578 | 
                   // on the page works best with jQuery offset() function. So, to get
  | 
        
        
            | 
            | 
           579 | 
                   // the right target position, we work out the required change in
  | 
        
        
            | 
            | 
           580 | 
                   // offset() and then add that to the current CSS position.
  | 
        
        
            | 
            | 
           581 | 
                   drag.animate(
  | 
        
        
            | 
            | 
           582 | 
                       {
  | 
        
        
            | 
            | 
           583 | 
                           left: parseInt(drag.css('left')) + targetPos.left - currentPos.left,
  | 
        
        
            | 
            | 
           584 | 
                           top: parseInt(drag.css('top')) + targetPos.top - currentPos.top
  | 
        
        
            | 
            | 
           585 | 
                       },
  | 
        
        
            | 
            | 
           586 | 
                       {
  | 
        
        
            | 
            | 
           587 | 
                           duration: 'fast',
  | 
        
        
            | 
            | 
           588 | 
                           done: function() {
  | 
        
        
            | 
            | 
           589 | 
                               $('body').trigger('qtype_ddwtos-dragmoved', [drag, target, thisQ]);
  | 
        
        
            | 
            | 
           590 | 
                               M.util.js_complete('qtype_ddwtos-animate-' + thisQ.containerId);
  | 
        
        
            | 
            | 
           591 | 
                           }
  | 
        
        
            | 
            | 
           592 | 
                       }
  | 
        
        
            | 
            | 
           593 | 
                   );
  | 
        
        
            | 
            | 
           594 | 
               };
  | 
        
        
            | 
            | 
           595 | 
              | 
        
        
            | 
            | 
           596 | 
               /**
  | 
        
        
            | 
            | 
           597 | 
                * Detect if a point is inside a given DOM node.
  | 
        
        
            | 
            | 
           598 | 
                *
  | 
        
        
            | 
            | 
           599 | 
                * @param {Number} pageX the x position.
  | 
        
        
            | 
            | 
           600 | 
                * @param {Number} pageY the y position.
  | 
        
        
            | 
            | 
           601 | 
                * @param {jQuery} drop the node to check (typically a drop).
  | 
        
        
            | 
            | 
           602 | 
                * @return {boolean} whether the point is inside the node.
  | 
        
        
            | 
            | 
           603 | 
                */
  | 
        
        
            | 
            | 
           604 | 
               DragDropToTextQuestion.prototype.isPointInDrop = function(pageX, pageY, drop) {
  | 
        
        
            | 
            | 
           605 | 
                   var position = drop.offset();
  | 
        
        
            | 
            | 
           606 | 
                   return pageX >= position.left && pageX < position.left + drop.width()
  | 
        
        
            | 
            | 
           607 | 
                           && pageY >= position.top && pageY < position.top + drop.height();
  | 
        
        
            | 
            | 
           608 | 
               };
  | 
        
        
            | 
            | 
           609 | 
              | 
        
        
            | 
            | 
           610 | 
               /**
  | 
        
        
            | 
            | 
           611 | 
                * Set the value of the hidden input for a place, to record what is currently there.
  | 
        
        
            | 
            | 
           612 | 
                *
  | 
        
        
            | 
            | 
           613 | 
                * @param {int} place which place to set the input value for.
  | 
        
        
            | 
            | 
           614 | 
                * @param {int} choice the value to set.
  | 
        
        
            | 
            | 
           615 | 
                */
  | 
        
        
            | 
            | 
           616 | 
               DragDropToTextQuestion.prototype.setInputValue = function(place, choice) {
  | 
        
        
            | 
            | 
           617 | 
                   this.getRoot().find('input.placeinput.place' + place).val(choice);
  | 
        
        
            | 
            | 
           618 | 
               };
  | 
        
        
            | 
            | 
           619 | 
              | 
        
        
            | 
            | 
           620 | 
               /**
  | 
        
        
            | 
            | 
           621 | 
                * Get the outer div for this question.
  | 
        
        
            | 
            | 
           622 | 
                *
  | 
        
        
            | 
            | 
           623 | 
                * @returns {jQuery} containing that div.
  | 
        
        
            | 
            | 
           624 | 
                */
  | 
        
        
            | 
            | 
           625 | 
               DragDropToTextQuestion.prototype.getRoot = function() {
  | 
        
        
            | 
            | 
           626 | 
                   return $(document.getElementById(this.containerId));
  | 
        
        
            | 
            | 
           627 | 
               };
  | 
        
        
            | 
            | 
           628 | 
              | 
        
        
            | 
            | 
           629 | 
               /**
  | 
        
        
            | 
            | 
           630 | 
                * Get drag home for a given choice.
  | 
        
        
            | 
            | 
           631 | 
                *
  | 
        
        
            | 
            | 
           632 | 
                * @param {int} group the group.
  | 
        
        
            | 
            | 
           633 | 
                * @param {int} choice the choice number.
  | 
        
        
            | 
            | 
           634 | 
                * @returns {jQuery} containing that div.
  | 
        
        
            | 
            | 
           635 | 
                */
  | 
        
        
            | 
            | 
           636 | 
               DragDropToTextQuestion.prototype.getDragHome = function(group, choice) {
  | 
        
        
            | 
            | 
           637 | 
                   if (!this.getRoot().find('.draghome.dragplaceholder.group' + group + '.choice' + choice).is(':visible')) {
  | 
        
        
            | 
            | 
           638 | 
                       return this.getRoot().find('.draggrouphomes' + group +
  | 
        
        
            | 
            | 
           639 | 
                           ' span.draghome.infinite' +
  | 
        
        
            | 
            | 
           640 | 
                           '.choice' + choice +
  | 
        
        
            | 
            | 
           641 | 
                           '.group' + group);
  | 
        
        
            | 
            | 
           642 | 
                   }
  | 
        
        
            | 
            | 
           643 | 
                   return this.getRoot().find('.draghome.dragplaceholder.group' + group + '.choice' + choice);
  | 
        
        
            | 
            | 
           644 | 
               };
  | 
        
        
            | 
            | 
           645 | 
              | 
        
        
            | 
            | 
           646 | 
               /**
  | 
        
        
            | 
            | 
           647 | 
                * Get an unplaced choice for a particular group.
  | 
        
        
            | 
            | 
           648 | 
                *
  | 
        
        
            | 
            | 
           649 | 
                * @param {int} group the group.
  | 
        
        
            | 
            | 
           650 | 
                * @param {int} choice the choice number.
  | 
        
        
            | 
            | 
           651 | 
                * @returns {jQuery} jQuery wrapping the unplaced choice. If there isn't one, the jQuery will be empty.
  | 
        
        
            | 
            | 
           652 | 
                */
  | 
        
        
            | 
            | 
           653 | 
               DragDropToTextQuestion.prototype.getUnplacedChoice = function(group, choice) {
  | 
        
        
            | 
            | 
           654 | 
                   return this.getRoot().find('.draghome.group' + group + '.choice' + choice + '.unplaced').slice(0, 1);
  | 
        
        
            | 
            | 
           655 | 
               };
  | 
        
        
            | 
            | 
           656 | 
              | 
        
        
            | 
            | 
           657 | 
               /**
  | 
        
        
            | 
            | 
           658 | 
                * Get the drag that is currently in a given place.
  | 
        
        
            | 
            | 
           659 | 
                *
  | 
        
        
            | 
            | 
           660 | 
                * @param {int} place the place number.
  | 
        
        
            | 
            | 
           661 | 
                * @return {jQuery} the current drag (or an empty jQuery if none).
  | 
        
        
            | 
            | 
           662 | 
                */
  | 
        
        
            | 
            | 
           663 | 
               DragDropToTextQuestion.prototype.getCurrentDragInPlace = function(place) {
  | 
        
        
            | 
            | 
           664 | 
                   return this.getRoot().find('span.draghome.inplace' + place);
  | 
        
        
            | 
            | 
           665 | 
               };
  | 
        
        
            | 
            | 
           666 | 
              | 
        
        
            | 
            | 
           667 | 
               /**
  | 
        
        
            | 
            | 
           668 | 
                * Return the number of blanks in a given group.
  | 
        
        
            | 
            | 
           669 | 
                *
  | 
        
        
            | 
            | 
           670 | 
                * @param {int} group the group number.
  | 
        
        
            | 
            | 
           671 | 
                * @returns {int} the number of drops.
  | 
        
        
            | 
            | 
           672 | 
                */
  | 
        
        
            | 
            | 
           673 | 
               DragDropToTextQuestion.prototype.noOfDropsInGroup = function(group) {
  | 
        
        
            | 
            | 
           674 | 
                   return this.getRoot().find('.drop.group' + group).length;
  | 
        
        
            | 
            | 
           675 | 
               };
  | 
        
        
            | 
            | 
           676 | 
              | 
        
        
            | 
            | 
           677 | 
               /**
  | 
        
        
            | 
            | 
           678 | 
                * Return the number of choices in a given group.
  | 
        
        
            | 
            | 
           679 | 
                *
  | 
        
        
            | 
            | 
           680 | 
                * @param {int} group the group number.
  | 
        
        
            | 
            | 
           681 | 
                * @returns {int} the number of choices.
  | 
        
        
            | 
            | 
           682 | 
                */
  | 
        
        
            | 
            | 
           683 | 
               DragDropToTextQuestion.prototype.noOfChoicesInGroup = function(group) {
  | 
        
        
            | 
            | 
           684 | 
                   return this.getRoot().find('.draghome.group' + group).length;
  | 
        
        
            | 
            | 
           685 | 
               };
  | 
        
        
            | 
            | 
           686 | 
              | 
        
        
            | 
            | 
           687 | 
               /**
  | 
        
        
            | 
            | 
           688 | 
                * Return the number at the end of the CSS class name with the given prefix.
  | 
        
        
            | 
            | 
           689 | 
                *
  | 
        
        
            | 
            | 
           690 | 
                * @param {jQuery} node
  | 
        
        
            | 
            | 
           691 | 
                * @param {String} prefix name prefix
  | 
        
        
            | 
            | 
           692 | 
                * @returns {Number|null} the suffix if found, else null.
  | 
        
        
            | 
            | 
           693 | 
                */
  | 
        
        
            | 
            | 
           694 | 
               DragDropToTextQuestion.prototype.getClassnameNumericSuffix = function(node, prefix) {
  | 
        
        
            | 
            | 
           695 | 
                   var classes = node.attr('class');
  | 
        
        
            | 
            | 
           696 | 
                   if (classes !== undefined && classes !== '') {
  | 
        
        
            | 
            | 
           697 | 
                       var classesArr = classes.split(' ');
  | 
        
        
            | 
            | 
           698 | 
                       for (var index = 0; index < classesArr.length; index++) {
  | 
        
        
            | 
            | 
           699 | 
                           var patt1 = new RegExp('^' + prefix + '([0-9])+$');
  | 
        
        
            | 
            | 
           700 | 
                           if (patt1.test(classesArr[index])) {
  | 
        
        
            | 
            | 
           701 | 
                               var patt2 = new RegExp('([0-9])+$');
  | 
        
        
            | 
            | 
           702 | 
                               var match = patt2.exec(classesArr[index]);
  | 
        
        
            | 
            | 
           703 | 
                               return Number(match[0]);
  | 
        
        
            | 
            | 
           704 | 
                           }
  | 
        
        
            | 
            | 
           705 | 
                       }
  | 
        
        
            | 
            | 
           706 | 
                   }
  | 
        
        
            | 
            | 
           707 | 
                   return null;
  | 
        
        
            | 
            | 
           708 | 
               };
  | 
        
        
            | 
            | 
           709 | 
              | 
        
        
            | 
            | 
           710 | 
               /**
  | 
        
        
            | 
            | 
           711 | 
                * Get the choice number of a drag.
  | 
        
        
            | 
            | 
           712 | 
                *
  | 
        
        
            | 
            | 
           713 | 
                * @param {jQuery} drag the drag.
  | 
        
        
            | 
            | 
           714 | 
                * @returns {Number} the choice number.
  | 
        
        
            | 
            | 
           715 | 
                */
  | 
        
        
            | 
            | 
           716 | 
               DragDropToTextQuestion.prototype.getChoice = function(drag) {
  | 
        
        
            | 
            | 
           717 | 
                   return this.getClassnameNumericSuffix(drag, 'choice');
  | 
        
        
            | 
            | 
           718 | 
               };
  | 
        
        
            | 
            | 
           719 | 
              | 
        
        
            | 
            | 
           720 | 
               /**
  | 
        
        
            | 
            | 
           721 | 
                * Given a DOM node that is significant to this question
  | 
        
        
            | 
            | 
           722 | 
                * (drag, drop, ...) get the group it belongs to.
  | 
        
        
            | 
            | 
           723 | 
                *
  | 
        
        
            | 
            | 
           724 | 
                * @param {jQuery} node a DOM node.
  | 
        
        
            | 
            | 
           725 | 
                * @returns {Number} the group it belongs to.
  | 
        
        
            | 
            | 
           726 | 
                */
  | 
        
        
            | 
            | 
           727 | 
               DragDropToTextQuestion.prototype.getGroup = function(node) {
  | 
        
        
            | 
            | 
           728 | 
                   return this.getClassnameNumericSuffix(node, 'group');
  | 
        
        
            | 
            | 
           729 | 
               };
  | 
        
        
            | 
            | 
           730 | 
              | 
        
        
            | 
            | 
           731 | 
               /**
  | 
        
        
            | 
            | 
           732 | 
                * Get the place number of a drop, or its corresponding hidden input.
  | 
        
        
            | 
            | 
           733 | 
                *
  | 
        
        
            | 
            | 
           734 | 
                * @param {jQuery} node the DOM node.
  | 
        
        
            | 
            | 
           735 | 
                * @returns {Number} the place number.
  | 
        
        
            | 
            | 
           736 | 
                */
  | 
        
        
            | 
            | 
           737 | 
               DragDropToTextQuestion.prototype.getPlace = function(node) {
  | 
        
        
            | 
            | 
           738 | 
                   return this.getClassnameNumericSuffix(node, 'place');
  | 
        
        
            | 
            | 
           739 | 
               };
  | 
        
        
            | 
            | 
           740 | 
              | 
        
        
            | 
            | 
           741 | 
               /**
  | 
        
        
            | 
            | 
           742 | 
                * Get drag clone for a given drag.
  | 
        
        
            | 
            | 
           743 | 
                *
  | 
        
        
            | 
            | 
           744 | 
                * @param {jQuery} drag the drag.
  | 
        
        
            | 
            | 
           745 | 
                * @returns {jQuery} the drag's clone.
  | 
        
        
            | 
            | 
           746 | 
                */
  | 
        
        
            | 
            | 
           747 | 
               DragDropToTextQuestion.prototype.getDragClone = function(drag) {
  | 
        
        
            | 
            | 
           748 | 
                   return this.getRoot().find('.draggrouphomes' +
  | 
        
        
            | 
            | 
           749 | 
                       this.getGroup(drag) +
  | 
        
        
            | 
            | 
           750 | 
                       ' span.draghome' +
  | 
        
        
            | 
            | 
           751 | 
                       '.choice' + this.getChoice(drag) +
  | 
        
        
            | 
            | 
           752 | 
                       '.group' + this.getGroup(drag) +
  | 
        
        
            | 
            | 
           753 | 
                       '.dragplaceholder');
  | 
        
        
            | 
            | 
           754 | 
               };
  | 
        
        
            | 
            | 
           755 | 
              | 
        
        
            | 
            | 
           756 | 
               /**
  | 
        
        
            | 
            | 
           757 | 
                * Get infinite drag clones for given drag.
  | 
        
        
            | 
            | 
           758 | 
                *
  | 
        
        
            | 
            | 
           759 | 
                * @param {jQuery} drag the drag.
  | 
        
        
            | 
            | 
           760 | 
                * @param {Boolean} inHome in the home area or not.
  | 
        
        
            | 
            | 
           761 | 
                * @returns {jQuery} the drag's clones.
  | 
        
        
            | 
            | 
           762 | 
                */
  | 
        
        
            | 
            | 
           763 | 
               DragDropToTextQuestion.prototype.getInfiniteDragClones = function(drag, inHome) {
  | 
        
        
            | 
            | 
           764 | 
                   if (inHome) {
  | 
        
        
            | 
            | 
           765 | 
                       return this.getRoot().find('.draggrouphomes' +
  | 
        
        
            | 
            | 
           766 | 
                           this.getGroup(drag) +
  | 
        
        
            | 
            | 
           767 | 
                           ' span.draghome' +
  | 
        
        
            | 
            | 
           768 | 
                           '.choice' + this.getChoice(drag) +
  | 
        
        
            | 
            | 
           769 | 
                           '.group' + this.getGroup(drag) +
  | 
        
        
            | 
            | 
           770 | 
                           '.infinite').not('.dragplaceholder');
  | 
        
        
            | 
            | 
           771 | 
                   }
  | 
        
        
            | 
            | 
           772 | 
                   return this.getRoot().find('span.draghome' +
  | 
        
        
            | 
            | 
           773 | 
                       '.choice' + this.getChoice(drag) +
  | 
        
        
            | 
            | 
           774 | 
                       '.group' + this.getGroup(drag) +
  | 
        
        
            | 
            | 
           775 | 
                       '.infinite').not('.dragplaceholder');
  | 
        
        
            | 
            | 
           776 | 
               };
  | 
        
        
            | 
            | 
           777 | 
              | 
        
        
            | 
            | 
           778 | 
               /**
  | 
        
        
            | 
            | 
           779 | 
                * Get drop for a given drag and place.
  | 
        
        
            | 
            | 
           780 | 
                *
  | 
        
        
            | 
            | 
           781 | 
                * @param {jQuery} drag the drag.
  | 
        
        
            | 
            | 
           782 | 
                * @param {Integer} currentPlace the current place of drag.
  | 
        
        
            | 
            | 
           783 | 
                * @returns {jQuery} the drop's clone.
  | 
        
        
            | 
            | 
           784 | 
                */
  | 
        
        
            | 
            | 
           785 | 
               DragDropToTextQuestion.prototype.getDrop = function(drag, currentPlace) {
  | 
        
        
            | 
            | 
           786 | 
                   return this.getRoot().find('.drop.group' + this.getGroup(drag) + '.place' + currentPlace);
  | 
        
        
            | 
            | 
           787 | 
               };
  | 
        
        
            | 
            | 
           788 | 
              | 
        
        
            | 
            | 
           789 | 
               /**
  | 
        
        
            | 
            | 
           790 | 
                * Singleton that tracks all the DragDropToTextQuestions on this page, and deals
  | 
        
        
            | 
            | 
           791 | 
                * with event dispatching.
  | 
        
        
            | 
            | 
           792 | 
                *
  | 
        
        
            | 
            | 
           793 | 
                * @type {Object}
  | 
        
        
            | 
            | 
           794 | 
                */
  | 
        
        
            | 
            | 
           795 | 
               var questionManager = {
  | 
        
        
            | 
            | 
           796 | 
                   /**
  | 
        
        
            | 
            | 
           797 | 
                    * {boolean} used to ensure the event handlers are only initialised once per page.
  | 
        
        
            | 
            | 
           798 | 
                    */
  | 
        
        
            | 
            | 
           799 | 
                   eventHandlersInitialised: false,
  | 
        
        
            | 
            | 
           800 | 
              | 
        
        
            | 
            | 
           801 | 
                   /**
  | 
        
        
            | 
            | 
           802 | 
                    * {Object} ensures that the drag event handlers are only initialised once per question,
  | 
        
        
            | 
            | 
           803 | 
                    * indexed by containerId (id on the .que div).
  | 
        
        
            | 
            | 
           804 | 
                    */
  | 
        
        
            | 
            | 
           805 | 
                   dragEventHandlersInitialised: {},
  | 
        
        
            | 
            | 
           806 | 
              | 
        
        
            | 
            | 
           807 | 
                   /**
  | 
        
        
            | 
            | 
           808 | 
                    * {boolean} is keyboard navigation or not.
  | 
        
        
            | 
            | 
           809 | 
                    */
  | 
        
        
            | 
            | 
           810 | 
                   isKeyboardNavigation: false,
  | 
        
        
            | 
            | 
           811 | 
              | 
        
        
            | 
            | 
           812 | 
                   /**
  | 
        
        
            | 
            | 
           813 | 
                    * {DragDropToTextQuestion[]} all the questions on this page, indexed by containerId (id on the .que div).
  | 
        
        
            | 
            | 
           814 | 
                    */
  | 
        
        
            | 
            | 
           815 | 
                   questions: {},
  | 
        
        
            | 
            | 
           816 | 
              | 
        
        
            | 
            | 
           817 | 
                   /**
  | 
        
        
            | 
            | 
           818 | 
                    * Initialise questions.
  | 
        
        
            | 
            | 
           819 | 
                    *
  | 
        
        
            | 
            | 
           820 | 
                    * @param {String} containerId id of the outer div for this question.
  | 
        
        
            | 
            | 
           821 | 
                    * @param {boolean} readOnly whether the question is being displayed read-only.
  | 
        
        
            | 
            | 
           822 | 
                    */
  | 
        
        
            | 
            | 
           823 | 
                   init: function(containerId, readOnly) {
  | 
        
        
            | 
            | 
           824 | 
                       questionManager.questions[containerId] = new DragDropToTextQuestion(containerId, readOnly);
  | 
        
        
            | 
            | 
           825 | 
                       if (!questionManager.eventHandlersInitialised) {
  | 
        
        
            | 
            | 
           826 | 
                           questionManager.setupEventHandlers();
  | 
        
        
            | 
            | 
           827 | 
                           questionManager.eventHandlersInitialised = true;
  | 
        
        
            | 
            | 
           828 | 
                       }
  | 
        
        
            | 
            | 
           829 | 
                       if (!questionManager.dragEventHandlersInitialised.hasOwnProperty(containerId)) {
  | 
        
        
            | 
            | 
           830 | 
                           questionManager.dragEventHandlersInitialised[containerId] = true;
  | 
        
        
            | 
            | 
           831 | 
                           // We do not use the body event here to prevent the other event on Mobile device, such as scroll event.
  | 
        
        
            | 
            | 
           832 | 
                           var questionContainer = document.getElementById(containerId);
  | 
        
        
            | 
            | 
           833 | 
                           if (questionContainer.classList.contains('ddwtos') &&
  | 
        
        
            | 
            | 
           834 | 
                               !questionContainer.classList.contains('qtype_ddwtos-readonly')) {
  | 
        
        
            | 
            | 
           835 | 
                               // TODO: Convert all the jQuery selectors and events to native Javascript.
  | 
        
        
            | 
            | 
           836 | 
                               questionManager.addEventHandlersToDrag($(questionContainer).find('span.draghome'));
  | 
        
        
            | 
            | 
           837 | 
                           }
  | 
        
        
            | 
            | 
           838 | 
                       }
  | 
        
        
            | 
            | 
           839 | 
                   },
  | 
        
        
            | 
            | 
           840 | 
              | 
        
        
            | 
            | 
           841 | 
                   /**
  | 
        
        
            | 
            | 
           842 | 
                    * Set up the event handlers that make this question type work. (Done once per page.)
  | 
        
        
            | 
            | 
           843 | 
                    */
  | 
        
        
            | 
            | 
           844 | 
                   setupEventHandlers: function() {
  | 
        
        
            | 
            | 
           845 | 
                       $('body')
  | 
        
        
            | 
            | 
           846 | 
                           .on('keydown',
  | 
        
        
            | 
            | 
           847 | 
                               '.que.ddwtos:not(.qtype_ddwtos-readonly) span.drop',
  | 
        
        
            | 
            | 
           848 | 
                               questionManager.handleKeyPress)
  | 
        
        
            | 
            | 
           849 | 
                           .on('keydown',
  | 
        
        
            | 
            | 
           850 | 
                               '.que.ddwtos:not(.qtype_ddwtos-readonly) span.draghome.placed:not(.beingdragged)',
  | 
        
        
            | 
            | 
           851 | 
                               questionManager.handleKeyPress)
  | 
        
        
            | 
            | 
           852 | 
                           .on('qtype_ddwtos-dragmoved', questionManager.handleDragMoved);
  | 
        
        
            | 
            | 
           853 | 
                   },
  | 
        
        
            | 
            | 
           854 | 
              | 
        
        
            | 
            | 
           855 | 
                   /**
  | 
        
        
            | 
            | 
           856 | 
                    * Binding the drag/touch event again for newly created element.
  | 
        
        
            | 
            | 
           857 | 
                    *
  | 
        
        
            | 
            | 
           858 | 
                    * @param {jQuery} element Element to bind the event
  | 
        
        
            | 
            | 
           859 | 
                    */
  | 
        
        
            | 
            | 
           860 | 
                   addEventHandlersToDrag: function(element) {
  | 
        
        
            | 
            | 
           861 | 
                       // Unbind all the mousedown and touchstart events to prevent double binding.
  | 
        
        
            | 
            | 
           862 | 
                       element.unbind('mousedown touchstart');
  | 
        
        
            | 
            | 
           863 | 
                       element.on('mousedown touchstart', questionManager.handleDragStart);
  | 
        
        
            | 
            | 
           864 | 
                   },
  | 
        
        
            | 
            | 
           865 | 
              | 
        
        
            | 
            | 
           866 | 
                   /**
  | 
        
        
            | 
            | 
           867 | 
                    * Handle mouse down / touch start on drags.
  | 
        
        
            | 
            | 
           868 | 
                    * @param {Event} e the DOM event.
  | 
        
        
            | 
            | 
           869 | 
                    */
  | 
        
        
            | 
            | 
           870 | 
                   handleDragStart: function(e) {
  | 
        
        
            | 
            | 
           871 | 
                       e.preventDefault();
  | 
        
        
            | 
            | 
           872 | 
                       var question = questionManager.getQuestionForEvent(e);
  | 
        
        
            | 
            | 
           873 | 
                       if (question) {
  | 
        
        
            | 
            | 
           874 | 
                           question.handleDragStart(e);
  | 
        
        
            | 
            | 
           875 | 
                       }
  | 
        
        
            | 
            | 
           876 | 
                   },
  | 
        
        
            | 
            | 
           877 | 
              | 
        
        
            | 
            | 
           878 | 
                   /**
  | 
        
        
            | 
            | 
           879 | 
                    * Handle key down / press on drops.
  | 
        
        
            | 
            | 
           880 | 
                    * @param {KeyboardEvent} e
  | 
        
        
            | 
            | 
           881 | 
                    */
  | 
        
        
            | 
            | 
           882 | 
                   handleKeyPress: function(e) {
  | 
        
        
            | 
            | 
           883 | 
                       if (questionManager.isKeyboardNavigation) {
  | 
        
        
            | 
            | 
           884 | 
                           return;
  | 
        
        
            | 
            | 
           885 | 
                       }
  | 
        
        
            | 
            | 
           886 | 
                       questionManager.isKeyboardNavigation = true;
  | 
        
        
            | 
            | 
           887 | 
                       var question = questionManager.getQuestionForEvent(e);
  | 
        
        
            | 
            | 
           888 | 
                       if (question) {
  | 
        
        
            | 
            | 
           889 | 
                           question.handleKeyPress(e);
  | 
        
        
            | 
            | 
           890 | 
                       }
  | 
        
        
            | 
            | 
           891 | 
                   },
  | 
        
        
            | 
            | 
           892 | 
              | 
        
        
            | 
            | 
           893 | 
                   /**
  | 
        
        
            | 
            | 
           894 | 
                    * Given an event, work out which question it affects.
  | 
        
        
            | 
            | 
           895 | 
                    *
  | 
        
        
            | 
            | 
           896 | 
                    * @param {Event} e the event.
  | 
        
        
            | 
            | 
           897 | 
                    * @returns {DragDropToTextQuestion|undefined} The question, or undefined.
  | 
        
        
            | 
            | 
           898 | 
                    */
  | 
        
        
            | 
            | 
           899 | 
                   getQuestionForEvent: function(e) {
  | 
        
        
            | 
            | 
           900 | 
                       var containerId = $(e.currentTarget).closest('.que.ddwtos').attr('id');
  | 
        
        
            | 
            | 
           901 | 
                       return questionManager.questions[containerId];
  | 
        
        
            | 
            | 
           902 | 
                   },
  | 
        
        
            | 
            | 
           903 | 
              | 
        
        
            | 
            | 
           904 | 
                   /**
  | 
        
        
            | 
            | 
           905 | 
                    * Handle when drag moved.
  | 
        
        
            | 
            | 
           906 | 
                    *
  | 
        
        
            | 
            | 
           907 | 
                    * @param {Event} e the event.
  | 
        
        
            | 
            | 
           908 | 
                    * @param {jQuery} drag the drag
  | 
        
        
            | 
            | 
           909 | 
                    * @param {jQuery} target the target
  | 
        
        
            | 
            | 
           910 | 
                    * @param {DragDropToTextQuestion} thisQ the question.
  | 
        
        
            | 
            | 
           911 | 
                    */
  | 
        
        
            | 
            | 
           912 | 
                   handleDragMoved: function(e, drag, target, thisQ) {
  | 
        
        
            | 
            | 
           913 | 
                       drag.removeClass('beingdragged');
  | 
        
        
            | 
            | 
           914 | 
                       drag.css('top', '').css('left', '');
  | 
        
        
            | 
            | 
           915 | 
                       target.after(drag);
  | 
        
        
            | 
            | 
           916 | 
                       target.removeClass('active');
  | 
        
        
            | 
            | 
           917 | 
                       if (typeof drag.data('unplaced') !== 'undefined' && drag.data('unplaced') === true) {
  | 
        
        
            | 
            | 
           918 | 
                           drag.removeClass('placed').addClass('unplaced');
  | 
        
        
            | 
            | 
           919 | 
                           drag.removeAttr('tabindex');
  | 
        
        
            | 
            | 
           920 | 
                           drag.removeData('unplaced');
  | 
        
        
            | 
            | 
           921 | 
                           if (drag.hasClass('infinite') && thisQ.getInfiniteDragClones(drag, true).length > 1) {
  | 
        
        
            | 
            | 
           922 | 
                               thisQ.getInfiniteDragClones(drag, true).first().remove();
  | 
        
        
            | 
            | 
           923 | 
                           }
  | 
        
        
            | 
            | 
           924 | 
                       }
  | 
        
        
            | 
            | 
           925 | 
                       if (typeof drag.data('isfocus') !== 'undefined' && drag.data('isfocus') === true) {
  | 
        
        
            | 
            | 
           926 | 
                           drag.focus();
  | 
        
        
            | 
            | 
           927 | 
                           drag.removeData('isfocus');
  | 
        
        
            | 
            | 
           928 | 
                       }
  | 
        
        
            | 
            | 
           929 | 
                       if (typeof target.data('isfocus') !== 'undefined' && target.data('isfocus') === true) {
  | 
        
        
            | 
            | 
           930 | 
                           target.removeData('isfocus');
  | 
        
        
            | 
            | 
           931 | 
                       }
  | 
        
        
            | 
            | 
           932 | 
                       if (questionManager.isKeyboardNavigation) {
  | 
        
        
            | 
            | 
           933 | 
                           questionManager.isKeyboardNavigation = false;
  | 
        
        
            | 
            | 
           934 | 
                       }
  | 
        
        
            | 
            | 
           935 | 
                       if (thisQ.isQuestionInteracted()) {
  | 
        
        
            | 
            | 
           936 | 
                           // The user has interacted with the draggable items. We need to mark the form as dirty.
  | 
        
        
            | 
            | 
           937 | 
                           questionManager.handleFormDirty();
  | 
        
        
            | 
            | 
           938 | 
                           // Save the new answered value.
  | 
        
        
            | 
            | 
           939 | 
                           thisQ.questionAnswer = thisQ.getQuestionAnsweredValues();
  | 
        
        
            | 
            | 
           940 | 
                       }
  | 
        
        
            | 
            | 
           941 | 
                   },
  | 
        
        
            | 
            | 
           942 | 
              | 
        
        
            | 
            | 
           943 | 
                   /**
  | 
        
        
            | 
            | 
           944 | 
                    * Handle when the form is dirty.
  | 
        
        
            | 
            | 
           945 | 
                    */
  | 
        
        
            | 
            | 
           946 | 
                   handleFormDirty: function() {
  | 
        
        
            | 
            | 
           947 | 
                       const responseForm = document.getElementById('responseform');
  | 
        
        
            | 
            | 
           948 | 
                       FormChangeChecker.markFormAsDirty(responseForm);
  | 
        
        
            | 
            | 
           949 | 
                   }
  | 
        
        
            | 
            | 
           950 | 
               };
  | 
        
        
            | 
            | 
           951 | 
              | 
        
        
            | 
            | 
           952 | 
               /**
  | 
        
        
            | 
            | 
           953 | 
                * @alias module:qtype_ddwtos/ddwtos
  | 
        
        
            | 
            | 
           954 | 
                */
  | 
        
        
            | 
            | 
           955 | 
               return {
  | 
        
        
            | 
            | 
           956 | 
                   /**
  | 
        
        
            | 
            | 
           957 | 
                    * Initialise one drag-drop into text question.
  | 
        
        
            | 
            | 
           958 | 
                    *
  | 
        
        
            | 
            | 
           959 | 
                    * @param {String} containerId id of the outer div for this question.
  | 
        
        
            | 
            | 
           960 | 
                    * @param {boolean} readOnly whether the question is being displayed read-only.
  | 
        
        
            | 
            | 
           961 | 
                    */
  | 
        
        
            | 
            | 
           962 | 
                   init: questionManager.init
  | 
        
        
            | 
            | 
           963 | 
               };
  | 
        
        
            | 
            | 
           964 | 
           });
  |