main.js 2.4 KB
Newer Older
1 2 3 4 5 6
// Wrapper for RequireJS. It will make the standard requirejs(), require(), and
// define() functions from Require JS available inside the anonymous function.
//
// See https://edx-wiki.atlassian.net/wiki/display/LMS/Integration+of+Require+JS+into+the+system
(function (requirejs, require, define) {

7
define(
8 9
    ['logme', 'state', 'config_parser', 'container', 'target', 'scroller', 'draggables'],
    function (logme, State, configParser, Container, Target, Scroller, Draggables) {
10 11 12
    return Main;

    function Main() {
13
        $('.drag_and_drop_problem_div').each(processProblem);
14 15 16 17
    }

    // $(value) - get the element of the entire problem
    function processProblem(index, value) {
18
        var problemId, imageDir, config, state;
19

20 21 22 23 24 25 26 27
        if ($(value).attr('data-problem-processed') === 'true') {
            // This problem was already processed by us before, so we will
            // skip it.

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

28 29
        problemId = $(value).attr('data-plain-id');
        if (typeof problemId !== 'string') {
30 31 32 33 34
            logme('ERROR: Could not find the ID of the problem DOM element.');

            return;
        }

35
        imageDir = $(value).attr('data-course-folder');
36 37
        if (typeof imageDir !== 'string') {
            logme('ERROR: Could not find the name of the image directory.');
38 39 40 41 42

            return;
        }

        try {
43
            config = JSON.parse($('#drag_and_drop_json_' + problemId).html());
44 45 46 47 48 49 50 51 52
        } catch (err) {
            logme('ERROR: Could not parse the JSON configuration options.');
            logme('Error message: "' + err.message + '".');

            return;
        }

        state = State(problemId);

53 54 55 56 57
        if (configParser(config, imageDir, state) !== true) {
            logme('ERROR: Could not make sense of the JSON configuration options.');

            return;
        }
58

59
        Container(state);
60
        Target(state);
61
        Scroller(state);
62 63
        Draggables(state);

64 65
        logme('config', config);
        logme('state', state);
66 67 68 69 70 71 72 73
    }
});

// End of wrapper for RequireJS. As you can see, we are passing
// namespaced Require JS variables to an anonymous function. Within
// it, you can use the standard requirejs(), require(), and define()
// functions as if they were in the global namespace.
}(RequireJS.requirejs, RequireJS.require, RequireJS.define)); // End-of: (function (requirejs, require, define)