main.js 3.61 KB
Newer Older
1 2
(function(requirejs, require, define) {
    define(
3
    ['js/capa/drag_and_drop/state',
4 5 6 7
     'js/capa/drag_and_drop/config_parser', 'js/capa/drag_and_drop/container',
     'js/capa/drag_and_drop/base_image', 'js/capa/drag_and_drop/scroller',
     'js/capa/drag_and_drop/draggables', 'js/capa/drag_and_drop/targets',
     'js/capa/drag_and_drop/update_input'],
8 9
    function(State, configParser, Container, BaseImage, Scroller, Draggables, Targets, updateInput) {
        return Main;
10

11
        function Main() {
12 13 14 15
        // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/every
        //
        // Array.prototype.every is a recent addition to the ECMA-262 standard; as such it may not be present in
        // other implementations of the standard.
16 17 18
            if (!Array.prototype.every) {
                Array.prototype.every = function(fun /* , thisp */) {
                    var thisp, t, len, i;
19

20 21 22
                    if (this == null) {
                        throw new TypeError();
                    }
23

24 25 26 27 28
                    t = Object(this);
                    len = t.length >>> 0;
                    if (typeof fun != 'function') {
                        throw new TypeError();
                    }
29

30
                    thisp = arguments[1];
31

32 33 34 35
                    for (i = 0; i < len; i++) {
                        if (i in t && !fun.call(thisp, t[i], i, t)) {
                            return false;
                        }
36 37
                    }

38 39 40
                    return true;
                };
            }
41

42 43
            $('.drag_and_drop_problem_div').each(processProblem);
        }
44 45

    // $(value) - get the element of the entire problem
46 47
        function processProblem(index, value) {
            var problemId, config, state;
48

49
            if ($(value).attr('data-problem-processed') === 'true') {
50 51 52
            // This problem was already processed by us before, so we will
            // skip it.

53 54 55
                return;
            }
            $(value).attr('data-problem-processed', 'true');
56

57 58 59
            problemId = $(value).attr('data-plain-id');
            if (typeof problemId !== 'string') {
                console.log('ERROR: Could not find the ID of the problem DOM element.');
60

61 62
                return;
            }
63

64 65 66 67 68
            try {
                config = JSON.parse($('#drag_and_drop_json_' + problemId).html());
            } catch (err) {
                console.log('ERROR: Could not parse the JSON configuration options.');
                console.log('Error message: "' + err.message + '".');
69

70 71
                return;
            }
72

73
            state = State(problemId);
74

75 76
            if (configParser(state, config) !== true) {
                console.log('ERROR: Could not make sense of the JSON configuration options.');
77

78 79
                return;
            }
80

81 82
            Container(state);
            BaseImage(state);
83

84 85 86
            (function addContent() {
                if (state.baseImageLoaded !== true) {
                    setTimeout(addContent, 50);
87

88 89
                    return;
                }
90

91 92 93
                Targets.initializeBaseTargets(state);
                Scroller(state);
                Draggables.init(state);
94

95
                state.updateArrowOpacity();
96

97 98
            // Update the input element, checking first that it is not filled with
            // an answer from the server.
99 100 101 102 103 104
                if (updateInput.check(state) === false) {
                    updateInput.update(state);
                }
            }());
        }
    }); // End-of: define(
105
}(RequireJS.requirejs, RequireJS.require, RequireJS.define)); // End-of: (function (requirejs, require, define) {