Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * JavaScript for form editing grouping conditions.
3
 *
4
 * @module moodle-availability_grouping-form
5
 */
6
M.availability_grouping = M.availability_grouping || {};
7
 
8
/**
9
 * @class M.availability_grouping.form
10
 * @extends M.core_availability.plugin
11
 */
12
M.availability_grouping.form = Y.Object(M.core_availability.plugin);
13
 
14
/**
15
 * Groupings available for selection (alphabetical order).
16
 *
17
 * @property groupings
18
 * @type Array
19
 */
20
M.availability_grouping.form.groupings = null;
21
 
22
/**
23
 * Initialises this plugin.
24
 *
25
 * @method initInner
26
 * @param {Array} groupings Array of objects containing groupingid => name
27
 */
28
M.availability_grouping.form.initInner = function(groupings) {
29
    this.groupings = groupings;
30
};
31
 
32
M.availability_grouping.form.getNode = function(json) {
33
    // Create HTML structure.
34
    var html = '<label><span class="pr-3">' + M.util.get_string('title', 'availability_grouping') + '</span> ' +
35
            '<span class="availability-group">' +
36
            '<select name="id" class="custom-select">' +
37
            '<option value="choose">' + M.util.get_string('choosedots', 'moodle') + '</option>';
38
    for (var i = 0; i < this.groupings.length; i++) {
39
        var grouping = this.groupings[i];
40
        // String has already been escaped using format_string.
41
        html += '<option value="' + grouping.id + '">' + grouping.name + '</option>';
42
    }
43
    html += '</select></span></label>';
44
    var node = Y.Node.create('<span class="d-flex flex-wrap align-items-center">' + html + '</span>');
45
 
46
    // Set initial value if specified.
47
    if (json.id !== undefined &&
48
            node.one('select[name=id] > option[value=' + json.id + ']')) {
49
        node.one('select[name=id]').set('value', '' + json.id);
50
    }
51
 
52
    // Add event handlers (first time only).
53
    if (!M.availability_grouping.form.addedEvents) {
54
        M.availability_grouping.form.addedEvents = true;
55
        var root = Y.one('.availability-field');
56
        root.delegate('change', function() {
57
            // Just update the form fields.
58
            M.core_availability.form.update();
59
        }, '.availability_grouping select');
60
    }
61
 
62
    return node;
63
};
64
 
65
M.availability_grouping.form.fillValue = function(value, node) {
66
    var selected = node.one('select[name=id]').get('value');
67
    if (selected === 'choose') {
68
        value.id = 'choose';
69
    } else {
70
        value.id = parseInt(selected, 10);
71
    }
72
};
73
 
74
M.availability_grouping.form.fillErrors = function(errors, node) {
75
    var value = {};
76
    this.fillValue(value, node);
77
 
78
    // Check grouping item id.
79
    if (value.id === 'choose') {
80
        errors.push('availability_grouping:error_selectgrouping');
81
    }
82
};