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