update_input.js 12.8 KB
Newer Older
1
(function (requirejs, require, define) {
2
define([], function () {
3 4 5 6
    return {
        'check': check,
        'update': update
    };
7

8 9
    function update(state) {
        var draggables, tempObj;
10 11

        draggables = [];
12

13 14 15 16 17 18 19 20 21 22 23 24
        if (state.config.individualTargets === false) {
            (function (c1) {
                while (c1 < state.draggables.length) {
                    if (state.draggables[c1].x !== -1) {
                        tempObj = {};
                        tempObj[state.draggables[c1].id] = [
                            state.draggables[c1].x,
                            state.draggables[c1].y
                        ];
                        draggables.push(tempObj);
                        tempObj = null;
                    }
25

26
                    c1 += 1;
27
                }
28
            }(0));
29
        } else {
30 31 32
            (function (c1) {
                while (c1 < state.targets.length) {
                    (function (c2) {
33
                        while (c2 < state.targets[c1].draggableList.length) {
34
                            tempObj = {};
35 36 37 38 39 40

                            if (state.targets[c1].type === 'base') {
                                tempObj[state.targets[c1].draggableList[c2].id] = state.targets[c1].id;
                            } else {
                                addTargetRecursively(tempObj, state.targets[c1].draggableList[c2], state.targets[c1]);
                            }
41 42 43 44 45 46
                            draggables.push(tempObj);
                            tempObj = null;

                            c2 += 1;
                        }
                    }(0));
47

48
                    c1 += 1;
49
                }
50
            }(0));
51 52
        }

53 54 55 56 57 58 59 60 61 62 63 64
        $('#input_' + state.problemId).val(JSON.stringify(draggables));
    }

    function addTargetRecursively(tempObj, draggable, target) {
        if (target.type === 'base') {
            tempObj[draggable.id] = target.id;
        } else {
            tempObj[draggable.id] = {};
            tempObj[draggable.id][target.id] = {};

            addTargetRecursively(tempObj[draggable.id][target.id], target.draggableObj, target.draggableObj.onTarget);
        }
65
    }
66

67 68 69 70
    // Check if input has an answer from server. If yes, then position
    // all draggables according to answer.
    function check(state) {
        var inputElVal;
71

72
        inputElVal = $('#input_' + state.problemId).val();
73

74 75 76
        if (inputElVal.length === 0) {
            return false;
        }
77

78
        repositionDraggables(state, JSON.parse(inputElVal));
79

80 81
        return true;
    }
82

83 84 85 86 87 88 89
    function processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, depth, i) {
        var baseDraggableId, baseDraggable, baseTargetId, baseTarget,
            layeredDraggableId, layeredDraggable, layeredTargetId, layeredTarget,
            chain;

        if (depth === 0) {
            // We are at the lowest depth? The end.
90

91
            return;
92 93 94 95 96 97
        }

        if (answerSortedByDepth.hasOwnProperty(depth) === false) {
            // We have a depth that ts not valid, we decrease the depth by one.
            processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, depth - 1, 0);

98 99
            return;
        }
100

101 102 103
        if (answerSortedByDepth[depth].length <= i) {
            // We ran out of answers at this depth, go to the next depth down.
            processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, depth - 1, 0);
104

105 106
            return;
        }
107

108
        chain = answerSortedByDepth[depth][i];
109

110
        baseDraggableId = Object.keys(chain)[0];
111

112 113 114
        // This is a hack. For now we will work with depths 1 and 3.
        if (depth === 1) {
            baseTargetId = chain[baseDraggableId];
115

116 117
            layeredTargetId = null;
            layeredDraggableId = null;
118

119 120 121
            // createBaseDraggableOnTarget(state, baseDraggableId, baseTargetId);
        } else if (depth === 3) {
            layeredDraggableId = baseDraggableId;
122

123
            layeredTargetId = Object.keys(chain[layeredDraggableId])[0];
124

125
            baseDraggableId = Object.keys(chain[layeredDraggableId][layeredTargetId])[0];
126

127 128
            baseTargetId = chain[layeredDraggableId][layeredTargetId][baseDraggableId];
        }
129

130
        checkBaseDraggable();
131

132
        return;
133

134 135 136 137 138 139 140 141 142 143 144 145 146
        function checkBaseDraggable() {
            if ((baseDraggable = getById(state, 'draggables', baseDraggableId, null, false, baseTargetId)) === null) {
                createBaseDraggableOnTarget(state, baseDraggableId, baseTargetId, true, function () {
                    if ((baseDraggable = getById(state, 'draggables', baseDraggableId, null, false, baseTargetId)) === null) {
                        console.log('ERROR: Could not successfully create a base draggable on a base target.');
                    } else {
                        baseTarget = baseDraggable.onTarget;

                        if ((layeredTargetId === null) || (layeredDraggableId === null)) {
                            processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, depth, i + 1);
                        } else {
                            checklayeredDraggable();
                        }
147
                    }
148 149 150
                });
            } else {
                baseTarget = baseDraggable.onTarget;
151

152 153 154 155 156 157 158
                if ((layeredTargetId === null) || (layeredDraggableId === null)) {
                    processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, depth, i + 1);
                } else {
                    checklayeredDraggable();
                }
            }
        }
159

160 161 162 163 164 165 166
        function checklayeredDraggable() {
            if ((layeredDraggable = getById(state, 'draggables', layeredDraggableId, null, false, layeredTargetId, baseDraggableId, baseTargetId)) === null) {
                layeredDraggable = getById(state, 'draggables', layeredDraggableId);
                layeredTarget = null;
                baseDraggable.targetField.every(function (target) {
                    if (target.id === layeredTargetId) {
                        layeredTarget = target;
167 168
                    }

169 170 171 172 173 174 175 176 177
                    return true;
                });

                if ((layeredDraggable !== null) && (layeredTarget !== null)) {
                    layeredDraggable.moveDraggableTo('target', layeredTarget, function () {
                        processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, depth, i + 1);
                    });
                } else {
                    processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, depth, i + 1);
178
                }
179 180 181 182 183
            } else {
                processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, depth, i + 1);
            }
        }
    }
184

185 186 187 188 189
    function createBaseDraggableOnTarget(state, draggableId, targetId, reportError, funcCallback) {
        var draggable, target;

        if ((draggable = getById(state, 'draggables', draggableId)) === null) {
            if (reportError !== false) {
190
                console.log(
191 192 193 194
                    'ERROR: In answer there exists a ' +
                    'draggable ID "' + draggableId + '". No ' +
                    'draggable with this ID could be found.'
                );
195
            }
196 197 198 199 200 201

            return false;
        }

        if ((target = getById(state, 'targets', targetId)) === null) {
            if (reportError !== false) {
202
                console.log(
203 204 205 206 207 208 209 210 211 212 213 214
                    'ERROR: In answer there exists a target ' +
                    'ID "' + targetId + '". No target with this ' +
                    'ID could be found.'
                );
            }

            return false;
        }

        draggable.moveDraggableTo('target', target, funcCallback);

        return true;
215
    }
216

217 218 219 220
    function processAnswerPositions(state, answer) {
        var draggableId, draggable;

        (function (c1) {
221 222 223
            while (c1 < answer.length) {
                for (draggableId in answer[c1]) {
                    if (answer[c1].hasOwnProperty(draggableId) === false) {
224 225 226 227
                        continue;
                    }

                    if ((draggable = getById(state, 'draggables', draggableId)) === null) {
228
                        console.log(
229 230 231 232 233 234 235 236
                            'ERROR: In answer there exists a ' +
                            'draggable ID "' + draggableId + '". No ' +
                            'draggable with this ID could be found.'
                        );

                        continue;
                    }

237
                    draggable.moveDraggableTo('XY', {
238 239
                        'x': answer[c1][draggableId][0],
                        'y': answer[c1][draggableId][1]
240 241 242 243 244 245 246 247 248
                    });
                }

                c1 += 1;
            }
        }(0));
    }

    function repositionDraggables(state, answer) {
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
        var answerSortedByDepth, minDepth, maxDepth;

        answerSortedByDepth = {};
        minDepth = 1000;
        maxDepth = 0;

        answer.every(function (chain) {
            var depth;

            depth = findDepth(chain, 0);

            if (depth < minDepth) {
                minDepth = depth;
            }
            if (depth > maxDepth) {
                maxDepth = depth;
            }

            if (answerSortedByDepth.hasOwnProperty(depth) === false) {
                answerSortedByDepth[depth] = [];
            }

            answerSortedByDepth[depth].push(chain);

            return true;
        });

        if (answer.length === 0) {
277 278 279
            return;
        }

280 281
        // For now we support only one case.
        if ((minDepth < 1) || (maxDepth > 3)) {
282
            return;
283 284
        }

285
        if (state.config.individualTargets === true) {
286
            processAnswerTargets(state, answerSortedByDepth, minDepth, maxDepth, maxDepth, 0);
287 288 289 290
        } else if (state.config.individualTargets === false) {
            processAnswerPositions(state, answer);
        }
    }
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    function findDepth(tempObj, depth) {
        var i;

        if ($.isPlainObject(tempObj) === false) {
            return depth;
        }

        depth += 1;

        for (i in tempObj) {
            if (tempObj.hasOwnProperty(i) === true) {
                depth = findDepth(tempObj[i], depth);
            }
        }

        return depth;
    }

    function getById(state, type, id, fromTargetField, inContainer, targetId, baseDraggableId, baseTargetId) {
311 312
        return (function (c1) {
            while (c1 < state[type].length) {
313
                if (type === 'draggables') {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
                    if ((targetId !== undefined) && (inContainer === false) && (baseDraggableId !== undefined) && (baseTargetId !== undefined)) {
                        if (
                            (state[type][c1].id === id) &&
                            (state[type][c1].inContainer === false) &&
                            (state[type][c1].onTarget.id === targetId) &&
                            (state[type][c1].onTarget.type === 'on_drag') &&
                            (state[type][c1].onTarget.draggableObj.id === baseDraggableId) &&
                            (state[type][c1].onTarget.draggableObj.onTarget.id === baseTargetId)
                        ) {
                            return state[type][c1];
                        }
                    } else if ((targetId !== undefined) && (inContainer === false)) {
                        if (
                            (state[type][c1].id === id) &&
                            (state[type][c1].inContainer === false) &&
                            (state[type][c1].onTarget.id === targetId)
                        ) {
                            return state[type][c1];
                        }
                    } else {
                        if (inContainer === false) {
                            if ((state[type][c1].id === id) && (state[type][c1].inContainer === false)) {
                                return state[type][c1];
                            }
                        } else {
                            if ((state[type][c1].id === id) && (state[type][c1].inContainer === true)) {
                                return state[type][c1];
                            }
                        }
343 344
                    }
                } else { // 'targets'
345 346 347 348 349 350 351 352
                    if (fromTargetField === true) {
                        if ((state[type][c1].id === id) && (state[type][c1].type === 'on_drag')) {
                            return state[type][c1];
                        }
                    } else {
                        if ((state[type][c1].id === id) && (state[type][c1].type === 'base')) {
                            return state[type][c1];
                        }
353
                    }
354
                }
355

356
                c1 += 1;
357 358 359
            }

            return null;
360
        }(0));
361
    }
362
}); // End-of: define([], function () {
363
}(RequireJS.requirejs, RequireJS.require, RequireJS.define)); // End-of: (function (requirejs, require, define) {