1 |
efrain |
1 |
/**
|
|
|
2 |
* JavaScript for form editing group conditions.
|
|
|
3 |
*
|
|
|
4 |
* @module moodle-availability_group-form
|
|
|
5 |
*/
|
|
|
6 |
M.availability_group = M.availability_group || {};
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* @class M.availability_group.form
|
|
|
10 |
* @extends M.core_availability.plugin
|
|
|
11 |
*/
|
|
|
12 |
M.availability_group.form = Y.Object(M.core_availability.plugin);
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Groups available for selection (alphabetical order).
|
|
|
16 |
*
|
|
|
17 |
* @property groups
|
|
|
18 |
* @type Array
|
|
|
19 |
*/
|
|
|
20 |
M.availability_group.form.groups = null;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Initialises this plugin.
|
|
|
24 |
*
|
|
|
25 |
* @method initInner
|
|
|
26 |
* @param {Array} groups Array of objects containing groupid => name
|
|
|
27 |
*/
|
|
|
28 |
M.availability_group.form.initInner = function(groups) {
|
|
|
29 |
this.groups = groups;
|
|
|
30 |
};
|
|
|
31 |
|
|
|
32 |
M.availability_group.form.getNode = function(json) {
|
|
|
33 |
// Create HTML structure.
|
|
|
34 |
var html = '<label><span class="pr-3">' + M.util.get_string('title', 'availability_group') + '</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 |
'<option value="any">' + M.util.get_string('anygroup', 'availability_group') + '</option>';
|
|
|
39 |
for (var i = 0; i < this.groups.length; i++) {
|
|
|
40 |
var group = this.groups[i];
|
|
|
41 |
// String has already been escaped using format_string.
|
|
|
42 |
html += '<option value="' + group.id + '" data-visibility="' + group.visibility + '">' + group.name + '</option>';
|
|
|
43 |
}
|
|
|
44 |
html += '</select></span></label>';
|
|
|
45 |
var node = Y.Node.create('<span class="d-flex flex-wrap align-items-center">' + html + '</span>');
|
|
|
46 |
|
|
|
47 |
var select = node.one('select[name=id]');
|
|
|
48 |
|
|
|
49 |
select.on('change', function(e) {
|
|
|
50 |
var value = e.target.get('value');
|
|
|
51 |
// Find the visibility of the selected group.
|
|
|
52 |
var visibility = e.target.one('option[value=' + value + ']').get('dataset').visibility;
|
|
|
53 |
|
|
|
54 |
var event;
|
|
|
55 |
if (visibility > 0) {
|
|
|
56 |
event = 'availability:privateRuleSet';
|
|
|
57 |
} else {
|
|
|
58 |
event = 'availability:privateRuleUnset';
|
|
|
59 |
}
|
|
|
60 |
node.fire(event, {plugin: 'group'});
|
|
|
61 |
});
|
|
|
62 |
|
|
|
63 |
// Set initial values (leave default 'choose' if creating afresh).
|
|
|
64 |
if (json.creating === undefined) {
|
|
|
65 |
if (json.id !== undefined) {
|
|
|
66 |
var option = select.one('option[value=' + json.id + ']');
|
|
|
67 |
if (option) {
|
|
|
68 |
select.set('value', '' + json.id);
|
|
|
69 |
var visibility = option.get('dataset').visibility;
|
|
|
70 |
if (visibility > 0) {
|
|
|
71 |
// Defer firing the event, to allow event bubbling to be set up in M.core_availability.form.
|
|
|
72 |
window.setTimeout(function() {
|
|
|
73 |
node.fire('availability:privateRuleSet', {plugin: 'group'});
|
|
|
74 |
}, 0);
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
} else if (json.id === undefined) {
|
|
|
78 |
node.one('select[name=id]').set('value', 'any');
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// Add event handlers (first time only).
|
|
|
83 |
if (!M.availability_group.form.addedEvents) {
|
|
|
84 |
M.availability_group.form.addedEvents = true;
|
|
|
85 |
var root = Y.one('.availability-field');
|
|
|
86 |
root.delegate('change', function() {
|
|
|
87 |
// Just update the form fields.
|
|
|
88 |
M.core_availability.form.update();
|
|
|
89 |
}, '.availability_group select');
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return node;
|
|
|
93 |
};
|
|
|
94 |
|
|
|
95 |
M.availability_group.form.fillValue = function(value, node) {
|
|
|
96 |
var selected = node.one('select[name=id]').get('value');
|
|
|
97 |
if (selected === 'choose') {
|
|
|
98 |
value.id = 'choose';
|
|
|
99 |
} else if (selected !== 'any') {
|
|
|
100 |
value.id = parseInt(selected, 10);
|
|
|
101 |
}
|
|
|
102 |
};
|
|
|
103 |
|
|
|
104 |
M.availability_group.form.fillErrors = function(errors, node) {
|
|
|
105 |
var value = {};
|
|
|
106 |
this.fillValue(value, node);
|
|
|
107 |
|
|
|
108 |
// Check group item id.
|
|
|
109 |
if (value.id && value.id === 'choose') {
|
|
|
110 |
errors.push('availability_group:error_selectgroup');
|
|
|
111 |
}
|
|
|
112 |
};
|