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 profile conditions.
3
 *
4
 * @module moodle-availability_profile-form
5
 */
6
M.availability_profile = M.availability_profile || {};
7
 
8
/**
9
 * @class M.availability_profile.form
10
 * @extends M.core_availability.plugin
11
 */
12
M.availability_profile.form = Y.Object(M.core_availability.plugin);
13
 
14
/**
15
 * Groupings available for selection (alphabetical order).
16
 *
17
 * @property profiles
18
 * @type Array
19
 */
20
M.availability_profile.form.profiles = null;
21
 
22
/**
23
 * Initialises this plugin.
24
 *
25
 * @method initInner
26
 * @param {Array} standardFields Array of objects with .field, .display
27
 * @param {Array} customFields Array of objects with .field, .display
28
 */
29
M.availability_profile.form.initInner = function(standardFields, customFields) {
30
    this.standardFields = standardFields;
31
    this.customFields = customFields;
32
};
33
 
34
M.availability_profile.form.getNode = function(json) {
35
    // Create HTML structure.
36
    var html = '<span class="availability-group"><label><span class="pr-3">' +
37
            M.util.get_string('conditiontitle', 'availability_profile') + '</span> ' +
38
            '<select name="field" class="custom-select">' +
39
            '<option value="choose">' + M.util.get_string('choosedots', 'moodle') + '</option>';
40
    var fieldInfo;
41
    for (var i = 0; i < this.standardFields.length; i++) {
42
        fieldInfo = this.standardFields[i];
43
        // String has already been escaped using format_string.
44
        html += '<option value="sf_' + fieldInfo.field + '">' + fieldInfo.display + '</option>';
45
    }
46
    for (i = 0; i < this.customFields.length; i++) {
47
        fieldInfo = this.customFields[i];
48
        // String has already been escaped using format_string.
49
        html += '<option value="cf_' + fieldInfo.field + '">' + fieldInfo.display + '</option>';
50
    }
51
    html += '</select></label> <label><span class="accesshide">' + M.util.get_string('label_operator', 'availability_profile') +
52
            ' </span><select name="op" title="' + M.util.get_string('label_operator', 'availability_profile') + '"' +
53
                     ' class="custom-select">';
54
    var operators = ['isequalto', 'contains', 'doesnotcontain', 'startswith', 'endswith',
55
            'isempty', 'isnotempty'];
56
    for (i = 0; i < operators.length; i++) {
57
        html += '<option value="' + operators[i] + '">' +
58
                M.util.get_string('op_' + operators[i], 'availability_profile') + '</option>';
59
    }
60
    html += '</select></label> <label><span class="accesshide">' + M.util.get_string('label_value', 'availability_profile') +
61
            '</span><input name="value" type="text" class="form-control" style="width: 10em" title="' +
62
            M.util.get_string('label_value', 'availability_profile') + '"/></label></span>';
63
    var node = Y.Node.create('<span class="d-flex flex-wrap align-items-center">' + html + '</span>');
64
 
65
    // Set initial values if specified.
66
    if (json.sf !== undefined &&
67
            node.one('select[name=field] > option[value=sf_' + json.sf + ']')) {
68
        node.one('select[name=field]').set('value', 'sf_' + json.sf);
69
    } else if (json.cf !== undefined &&
70
            node.one('select[name=field] > option[value=cf_' + json.cf + ']')) {
71
        node.one('select[name=field]').set('value', 'cf_' + json.cf);
72
    }
73
    if (json.op !== undefined &&
74
            node.one('select[name=op] > option[value=' + json.op + ']')) {
75
        node.one('select[name=op]').set('value', json.op);
76
        if (json.op === 'isempty' || json.op === 'isnotempty') {
77
            node.one('input[name=value]').set('disabled', true);
78
        }
79
    }
80
    if (json.v !== undefined) {
81
        node.one('input').set('value', json.v);
82
    }
83
 
84
    // Add event handlers (first time only).
85
    if (!M.availability_profile.form.addedEvents) {
86
        M.availability_profile.form.addedEvents = true;
87
        var updateForm = function(input) {
88
            var ancestorNode = input.ancestor('span.availability_profile');
89
            var op = ancestorNode.one('select[name=op]');
90
            var novalue = (op.get('value') === 'isempty' || op.get('value') === 'isnotempty');
91
            ancestorNode.one('input[name=value]').set('disabled', novalue);
92
            M.core_availability.form.update();
93
        };
94
        var root = Y.one('.availability-field');
95
        root.delegate('change', function() {
96
             updateForm(this);
97
        }, '.availability_profile select');
98
        root.delegate('change', function() {
99
             updateForm(this);
100
        }, '.availability_profile input[name=value]');
101
    }
102
 
103
    return node;
104
};
105
 
106
M.availability_profile.form.fillValue = function(value, node) {
107
    // Set field.
108
    var field = node.one('select[name=field]').get('value');
109
    if (field.substr(0, 3) === 'sf_') {
110
        value.sf = field.substr(3);
111
    } else if (field.substr(0, 3) === 'cf_') {
112
        value.cf = field.substr(3);
113
    }
114
 
115
    // Operator and value
116
    value.op = node.one('select[name=op]').get('value');
117
    var valueNode = node.one('input[name=value]');
118
    if (!valueNode.get('disabled')) {
119
        value.v = valueNode.get('value');
120
    }
121
};
122
 
123
M.availability_profile.form.fillErrors = function(errors, node) {
124
    var value = {};
125
    this.fillValue(value, node);
126
 
127
    // Check profile item id.
128
    if (value.sf === undefined && value.cf === undefined) {
129
        errors.push('availability_profile:error_selectfield');
130
    }
131
    if (value.v !== undefined && /^\s*$/.test(value.v)) {
132
        errors.push('availability_profile:error_setvalue');
133
    }
134
};