1 YUI.add('moodle-availability_profile-form', function (Y, NAME) {
4 * JavaScript for form editing profile conditions.
6 * @module moodle-availability_profile-form
8 M.availability_profile = M.availability_profile || {};
11 * @class M.availability_profile.form
12 * @extends M.core_availability.plugin
14 M.availability_profile.form = Y.Object(M.core_availability.plugin);
17 * Groupings available for selection (alphabetical order).
22 M.availability_profile.form.profiles = null;
25 * Initialises this plugin.
28 * @param {Array} standardFields Array of objects with .field, .display
29 * @param {Array} customFields Array of objects with .field, .display
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>';
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>';
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>';
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>';
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);
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);
81 if (json.v !== undefined) {
82 node.one('input').set('value', json.v);
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();
95 var root = Y.one('#fitem_id_availabilityconditionsjson');
96 root.delegate('change', function() {
98 }, '.availability_profile select');
99 root.delegate('change', function() {
101 }, '.availability_profile input[name=value]');
107 M.availability_profile.form.fillValue = function(value, node) {
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);
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');
124 M.availability_profile.form.fillErrors = function(errors, node) {
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');
132 if (value.v !== undefined && /^\s*$/.test(value.v)) {
133 errors.push('availability_profile:error_setvalue');
138 }, '@VERSION@', {"requires": ["base", "node", "event", "moodle-core_availability-form"]});