2 * Provides interface for users to edit availability settings on the
3 * module/section editing form.
5 * The system works using this JavaScript plus form.js files inside each
8 * The overall concept is that data is held in a textarea in the form in JSON
9 * format. This JavaScript converts the textarea into a set of controls
10 * generated here and by the relevant plugins.
12 * (Almost) all data is held directly by the state of the HTML controls, and
13 * can be updated to the form field by calling the 'update' method, which
14 * this code and the plugins call if any HTML control changes.
16 * @module moodle-core_availability-form
18 M.core_availability = M.core_availability || {};
21 * Core static functions for availability settings in editing form.
23 * @class M.core_availability.form
26 M.core_availability.form = {
28 * Object containing installed plugins. They are indexed by plugin name.
36 * Availability field (textarea).
44 * Main div that replaces the availability field.
52 * Object that represents the root of the tree.
55 * @type M.core_availability.List
60 * Counter used when creating anything that needs an id.
68 * The 'Restrict by group' button if present.
70 * @property restrictByGroup
73 restrictByGroup: null,
76 * Called to initialise the system when the page loads. This method will
77 * also call the init method for each plugin.
81 init: function(pluginParams) {
83 for (var plugin in pluginParams) {
84 var params = pluginParams[plugin];
85 var pluginClass = M[params[0]].form;
86 pluginClass.init.apply(pluginClass, params);
89 // Get the availability field, hide it, and replace with the main div.
90 this.field = Y.one('#id_availabilityconditionsjson');
91 this.field.setAttribute('aria-hidden', 'true');
92 // The fcontainer class here is inappropriate, but is necessary
93 // because otherwise it is impossible to make Behat work correctly on
94 // these controls as Behat incorrectly decides they're a moodleform
95 // textarea. IMO Behat should not know about moodleforms at all and
96 // should look purely at HTML elements on the page, but until it is
97 // fixed to do this or fixed in some other way to only detect moodleform
98 // elements that specifically match what those elements should look like,
99 // then there is no good solution.
100 this.mainDiv = Y.Node.create('<div class="availability-field fcontainer"></div>');
101 this.field.insert(this.mainDiv, 'after');
103 // Get top-level tree as JSON.
104 var value = this.field.get('value');
108 data = Y.JSON.parse(value);
110 // If the JSON data is not valid, treat it as empty.
111 this.field.set('value', '');
114 this.rootList = new M.core_availability.List(data, true);
115 this.mainDiv.appendChild(this.rootList.node);
117 // Update JSON value after loading (to reflect any changes that need
118 // to be made to make it valid).
120 this.rootList.renumber();
122 // Mark main area as dynamically updated.
123 this.mainDiv.setAttribute('aria-live', 'polite');
125 // Listen for form submission - to avoid having our made-up fields
126 // submitted, we need to disable them all before submit.
127 this.field.ancestor('form').on('submit', function() {
128 this.mainDiv.all('input,textarea,select').set('disabled', true);
131 // If the form has group mode and/or grouping options, there is a
132 // 'add restriction' button there.
133 this.restrictByGroup = Y.one('#restrictbygroup');
134 if (this.restrictByGroup) {
135 this.restrictByGroup.on('click', this.addRestrictByGroup, this);
136 var groupmode = Y.one('#id_groupmode');
137 var groupingid = Y.one('#id_groupingid');
139 groupmode.on('change', this.updateRestrictByGroup, this);
142 groupingid.on('change', this.updateRestrictByGroup, this);
144 this.updateRestrictByGroup();
149 * Called at any time to update the hidden field value.
151 * This should be called whenever any value changes in the form settings.
156 // Convert tree to value.
157 var jsValue = this.rootList.getValue();
159 // Store any errors (for form reporting) in 'errors' value if present.
161 this.rootList.fillErrors(errors);
162 if (errors.length !== 0) {
163 jsValue.errors = errors;
166 // Set into hidden form field, JS-encoded.
167 this.field.set('value', Y.JSON.stringify(jsValue));
169 // Also update the restrict by group button if present.
170 this.updateRestrictByGroup();
174 * Updates the status of the 'restrict by group' button (enables or disables
175 * it) based on current availability restrictions and group/grouping settings.
177 updateRestrictByGroup: function() {
178 if (!this.restrictByGroup) {
182 // If the root list is anything other than the default 'and' type, disable.
183 if (this.rootList.getValue().op !== '&') {
184 this.restrictByGroup.set('disabled', true);
188 // If there's already a group restriction, disable it.
189 var alreadyGot = this.rootList.hasItemOfType('group') ||
190 this.rootList.hasItemOfType('grouping');
192 this.restrictByGroup.set('disabled', true);
196 // If the groupmode and grouping id aren't set, disable it.
197 var groupmode = Y.one('#id_groupmode');
198 var groupingid = Y.one('#id_groupingid');
199 if ((!groupmode || Number(groupmode.get('value')) === 0) &&
200 (!groupingid || Number(groupingid.get('value')) === 0)) {
201 this.restrictByGroup.set('disabled', true);
205 this.restrictByGroup.set('disabled', false);
209 * Called when the user clicks on the 'restrict by group' button. This is
210 * a special case that adds a group or grouping restriction.
212 * By default this restriction is not shown which makes it similar to the
214 * @param e Button click event
216 addRestrictByGroup: function(e) {
217 // If you don't prevent default, it submits the form for some reason.
220 // Add the condition.
221 var groupingid = Y.one('#id_groupingid');
223 if (groupingid && Number(groupingid.get('value')) !== 0) {
224 // Add a grouping restriction if one is specified.
225 newChild = new M.core_availability.Item(
226 {type: 'grouping', id: Number(groupingid.get('value'))}, true);
228 // Otherwise just add a group restriction.
229 newChild = new M.core_availability.Item({type: 'group'}, true);
233 this.rootList.addChild(newChild);
235 this.rootList.renumber();
236 this.rootList.updateHtml();
242 * Base object for plugins. Plugins should use Y.Object to extend this class.
244 * @class M.core_availability.plugin
247 M.core_availability.plugin = {
249 * True if users are allowed to add items of this plugin at the moment.
257 * Called (from PHP) to initialise the plugin. Should usually not be
258 * overridden by child plugin.
261 * @param {String} component Component name e.g. 'availability_date'
263 init: function(component, allowAdd, params) {
264 var name = component.replace(/^availability_/, '');
265 this.allowAdd = allowAdd;
266 M.core_availability.form.plugins[name] = this;
267 this.initInner.apply(this, params);
271 * Init method for plugin to override. (Default does nothing.)
273 * This method will receive any parameters defined in frontend.php
274 * get_javascript_init_params.
279 initInner: function() {
284 * Gets a YUI node representing the controls for this plugin on the form.
286 * Must be implemented by sub-object; default throws an exception.
289 * @return {Y.Node} YUI node
291 getNode: function() {
292 throw 'getNode not implemented';
296 * Fills in the value from this plugin's controls into a value object,
297 * which will later be converted to JSON and stored in the form field.
299 * Must be implemented by sub-object; default throws an exception.
302 * @param {Object} value Value object (to be written to)
303 * @param {Y.Node} node YUI node (same one returned from getNode)
305 fillValue: function() {
306 throw 'fillValue not implemented';
310 * Fills in any errors from this plugin's controls. If there are any
311 * errors, push them into the supplied array.
313 * Errors are Moodle language strings in format component:string, e.g.
314 * 'availability_date:error_date_past_end_of_world'.
316 * The default implementation does nothing.
319 * @param {Array} errors Array of errors (push new errors here)
320 * @param {Y.Node} node YUI node (same one returned from getNode)
322 fillErrors: function() {
327 * Focuses the first thing in the plugin after it has been added.
329 * The default implementation uses a simple algorithm to identify the
330 * first focusable input/select and then focuses it.
332 focusAfterAdd: function(node) {
333 var target = node.one('input:not([disabled]),select:not([disabled])');
340 * Maintains a list of children and settings for how they are combined.
342 * @class M.core_availability.List
344 * @param {Object} json Decoded JSON value
345 * @param {Boolean} [false] root True if this is root level list
346 * @param {Boolean} [false] root True if parent is root level list
348 M.core_availability.List = function(json, root, parentRoot) {
349 // Set default value for children. (You can't do this in the prototype
350 // definition, or it ends up sharing the same array between all of them.)
353 if (root !== undefined) {
356 // Create DIV structure (without kids).
357 this.node = Y.Node.create('<div class="availability-list"><h3 class="accesshide"></h3>' +
358 '<div class="availability-inner">' +
359 '<div class="availability-header m-b-1"><span>' +
360 M.util.get_string('listheader_sign_before', 'availability') + '</span>' +
361 ' <label><span class="accesshide">' + M.util.get_string('label_sign', 'availability') +
362 ' </span><select class="availability-neg custom-select m-x-1"' +
363 ' title="' + M.util.get_string('label_sign', 'availability') + '">' +
364 '<option value="">' + M.util.get_string('listheader_sign_pos', 'availability') + '</option>' +
365 '<option value="!">' + M.util.get_string('listheader_sign_neg', 'availability') + '</option></select></label> ' +
366 '<span class="availability-single">' + M.util.get_string('listheader_single', 'availability') + '</span>' +
367 '<span class="availability-multi">' + M.util.get_string('listheader_multi_before', 'availability') +
368 ' <label><span class="accesshide">' + M.util.get_string('label_multi', 'availability') + ' </span>' +
369 '<select class="availability-op custom-select m-x-1"' +
370 ' title="' + M.util.get_string('label_multi', 'availability') + '"><option value="&">' +
371 M.util.get_string('listheader_multi_and', 'availability') + '</option>' +
372 '<option value="|">' + M.util.get_string('listheader_multi_or', 'availability') + '</option></select></label> ' +
373 M.util.get_string('listheader_multi_after', 'availability') + '</span></div>' +
374 '<div class="availability-children"></div>' +
375 '<div class="availability-none"><span class="p-x-1">' + M.util.get_string('none', 'moodle') + '</span></div>' +
376 '<div class="clearfix m-t-1"></div>' +
377 '<div class="availability-button"></div></div><div class="clearfix"></div></div>');
379 this.node.addClass('availability-childlist d-sm-flex align-items-center');
381 this.inner = this.node.one('> .availability-inner');
385 // If it's the root, add an eye icon as first thing in header.
386 if (json && json.show !== undefined) {
389 this.eyeIcon = new M.core_availability.EyeIcon(false, shown);
390 this.node.one('.availability-header').get('firstChild').insert(
391 this.eyeIcon.span, 'before');
392 } else if (parentRoot) {
393 // When the parent is root, add an eye icon before the main list div.
394 if (json && json.showc !== undefined) {
397 this.eyeIcon = new M.core_availability.EyeIcon(false, shown);
398 this.inner.insert(this.eyeIcon.span, 'before');
402 // If it's not the root, add a delete button to the 'none' option.
403 // You can only delete lists when they have no children so this will
404 // automatically appear at the correct time.
405 var deleteIcon = new M.core_availability.DeleteIcon(this);
406 var noneNode = this.node.one('.availability-none');
407 noneNode.appendChild(document.createTextNode(' '));
408 noneNode.appendChild(deleteIcon.span);
410 // Also if it's not the root, none is actually invalid, so add a label.
411 noneNode.appendChild(Y.Node.create('<span class="m-t-1 label label-warning">' +
412 M.util.get_string('invalid', 'availability') + '</span>'));
415 // Create the button and add it.
416 var button = Y.Node.create('<button type="button" class="btn btn-secondary m-t-1">' +
417 M.util.get_string('addrestriction', 'availability') + '</button>');
418 button.on("click", function() {
421 this.node.one('div.availability-button').appendChild(button);
424 // Set operator from JSON data.
428 this.node.one('.availability-neg').set('value', '');
432 this.node.one('.availability-neg').set('value', '!');
438 this.node.one('.availability-op').set('value', '&');
442 this.node.one('.availability-op').set('value', '|');
446 // Construct children.
447 for (var i = 0; i < json.c.length; i++) {
448 var child = json.c[i];
449 if (this.root && json && json.showc !== undefined) {
450 child.showc = json.showc[i];
453 if (child.type !== undefined) {
455 newItem = new M.core_availability.Item(child, this.root);
458 newItem = new M.core_availability.List(child, false, this.root);
460 this.addChild(newItem);
464 // Add update listeners to the dropdowns.
465 this.node.one('.availability-neg').on('change', function() {
466 // Update hidden field and HTML.
467 M.core_availability.form.update();
470 this.node.one('.availability-op').on('change', function() {
471 // Update hidden field.
472 M.core_availability.form.update();
476 // Update HTML to hide unnecessary parts.
481 * Adds a child to the end of the list (in HTML and stored data).
485 * @param {M.core_availability.Item|M.core_availability.List} newItem Child to add
487 M.core_availability.List.prototype.addChild = function(newItem) {
488 if (this.children.length > 0) {
489 // Create connecting label (text will be filled in later by updateHtml).
490 this.inner.one('.availability-children').appendChild(Y.Node.create(
491 '<div class="availability-connector">' +
492 '<span class="label"></span>' +
495 // Add item to array and to HTML.
496 this.children.push(newItem);
497 this.inner.one('.availability-children').appendChild(newItem.node);
501 * Focuses something after a new list is added.
503 * @method focusAfterAdd
505 M.core_availability.List.prototype.focusAfterAdd = function() {
506 this.inner.one('button').focus();
510 * Checks whether this list uses the individual show icons or the single one.
512 * (Basically, AND and the equivalent NOT OR list can have individual show icons
513 * so that you hide the activity entirely if a user fails one condition, but
514 * may display it with information about the condition if they fail a different
515 * one. That isn't possible with OR and NOT AND because for those types, there
516 * is not really a concept of which single condition caused the user to fail
519 * Method can only be called on the root list.
521 * @method isIndividualShowIcons
522 * @return {Boolean} True if using the individual icons
524 M.core_availability.List.prototype.isIndividualShowIcons = function() {
526 throw 'Can only call this on root list';
528 var neg = this.node.one('.availability-neg').get('value') === '!';
529 var isor = this.node.one('.availability-op').get('value') === '|';
530 return (!neg && !isor) || (neg && isor);
534 * Renumbers the list and all children.
537 * @param {String} parentNumber Number to use in heading for this list
539 M.core_availability.List.prototype.renumber = function(parentNumber) {
540 // Update heading for list.
541 var headingParams = {count: this.children.length};
543 if (parentNumber === undefined) {
544 headingParams.number = '';
547 headingParams.number = parentNumber + ':';
548 prefix = parentNumber + '.';
550 var heading = M.util.get_string('setheading', 'availability', headingParams);
551 this.node.one('> h3').set('innerHTML', heading);
554 for (var i = 0; i < this.children.length; i++) {
555 var child = this.children[i];
556 child.renumber(prefix + (i + 1));
561 * Updates HTML for the list based on the current values, for example showing
562 * the 'None' text if there are no children.
566 M.core_availability.List.prototype.updateHtml = function() {
567 // Control children appearing or not appearing.
568 if (this.children.length > 0) {
569 this.inner.one('> .availability-children').removeAttribute('aria-hidden');
570 this.inner.one('> .availability-none').setAttribute('aria-hidden', 'true');
571 this.inner.one('> .availability-header').removeAttribute('aria-hidden');
572 if (this.children.length > 1) {
573 this.inner.one('.availability-single').setAttribute('aria-hidden', 'true');
574 this.inner.one('.availability-multi').removeAttribute('aria-hidden');
576 this.inner.one('.availability-single').removeAttribute('aria-hidden');
577 this.inner.one('.availability-multi').setAttribute('aria-hidden', 'true');
580 this.inner.one('> .availability-children').setAttribute('aria-hidden', 'true');
581 this.inner.one('> .availability-none').removeAttribute('aria-hidden');
582 this.inner.one('> .availability-header').setAttribute('aria-hidden', 'true');
585 // For root list, control eye icons.
587 var showEyes = this.isIndividualShowIcons();
590 for (var i = 0; i < this.children.length; i++) {
591 var child = this.children[i];
593 child.eyeIcon.span.removeAttribute('aria-hidden');
595 child.eyeIcon.span.setAttribute('aria-hidden', 'true');
599 // Single icon is the inverse.
601 this.eyeIcon.span.setAttribute('aria-hidden', 'true');
603 this.eyeIcon.span.removeAttribute('aria-hidden');
607 // Update connector text.
609 if (this.inner.one('.availability-op').get('value') === '&') {
610 connectorText = M.util.get_string('and', 'availability');
612 connectorText = M.util.get_string('or', 'availability');
614 this.inner.all('> .availability-children > .availability-connector span.label').each(function(span) {
615 span.set('innerHTML', connectorText);
620 * Deletes a descendant item (Item or List). Called when the user clicks a
623 * This is a recursive function.
625 * @method deleteDescendant
626 * @param {M.core_availability.Item|M.core_availability.List} descendant Item to delete
627 * @return {Boolean} True if it was deleted
629 M.core_availability.List.prototype.deleteDescendant = function(descendant) {
630 // Loop through children.
631 for (var i = 0; i < this.children.length; i++) {
632 var child = this.children[i];
633 if (child === descendant) {
634 // Remove from internal array.
635 this.children.splice(i, 1);
636 var target = child.node;
637 // Remove one of the connector nodes around target (if any left).
638 if (this.children.length > 0) {
639 if (target.previous('.availability-connector')) {
640 target.previous('.availability-connector').remove();
642 target.next('.availability-connector').remove();
645 // Remove target itself.
646 this.inner.one('> .availability-children').removeChild(target);
647 // Update the form and the list HTML.
648 M.core_availability.form.update();
650 // Focus add button for this list.
651 this.inner.one('> .availability-button').one('button').focus();
653 } else if (child instanceof M.core_availability.List) {
655 var found = child.deleteDescendant(descendant);
666 * Shows the 'add restriction' dialogue box.
670 M.core_availability.List.prototype.clickAdd = function() {
671 var content = Y.Node.create('<div>' +
672 '<ul class="list-unstyled container-fluid"></ul>' +
673 '<div class="availability-buttons mdl-align">' +
674 '<button type="button" class="btn btn-secondary">' + M.util.get_string('cancel', 'moodle') +
675 '</button></div></div>');
676 var cancel = content.one('button');
678 // Make a list of all the dialog options.
679 var dialogRef = {dialog: null};
680 var ul = content.one('ul');
681 var li, id, button, label;
682 for (var type in M.core_availability.form.plugins) {
683 // Plugins might decide not to display their add button.
684 if (!M.core_availability.form.plugins[type].allowAdd) {
687 // Add entry for plugin.
688 li = Y.Node.create('<li class="clearfix row"></li>');
689 id = 'availability_addrestriction_' + type;
690 button = Y.Node.create('<div class="col-6"><button type="button" class="btn btn-secondary w-100"' +
691 'id="' + id + '">' + M.util.get_string('title', 'availability_' + type) + '</button></div>');
692 button.on('click', this.getAddHandler(type, dialogRef), this);
693 li.appendChild(button);
694 label = Y.Node.create('<div class="col-6"><label for="' + id + '">' +
695 M.util.get_string('description', 'availability_' + type) + '</label></div>');
696 li.appendChild(label);
699 // Extra entry for lists.
700 li = Y.Node.create('<li class="clearfix row"></li>');
701 id = 'availability_addrestriction_list_';
702 button = Y.Node.create('<div class="col-6"><button type="button" class="btn btn-secondary w-100"' +
703 'id="' + id + '">' + M.util.get_string('condition_group', 'availability') + '</button></div>');
704 button.on('click', this.getAddHandler(null, dialogRef), this);
705 li.appendChild(button);
706 label = Y.Node.create('<div class="col-6"><label for="' + id + '">' +
707 M.util.get_string('condition_group_info', 'availability') + '</label></div>');
708 li.appendChild(label);
712 headerContent: M.util.get_string('addrestriction', 'availability'),
713 bodyContent: content,
714 additionalBaseClass: 'availability-dialogue',
720 dialogRef.dialog = new M.core.dialogue(config);
721 dialogRef.dialog.show();
722 cancel.on('click', function() {
723 dialogRef.dialog.destroy();
724 // Focus the button they clicked originally.
725 this.inner.one('> .availability-button').one('button').focus();
730 * Gets an add handler function used by the dialogue to add a particular item.
732 * @method getAddHandler
733 * @param {String|Null} type Type name of plugin or null to add lists
734 * @param {Object} dialogRef Reference to object that contains dialog
735 * @param {M.core.dialogue} dialogRef.dialog Dialog object
736 * @return {Function} Add handler function to call when adding that thing
738 M.core_availability.List.prototype.getAddHandler = function(type, dialogRef) {
742 // Create an Item object to represent the child.
743 newItem = new M.core_availability.Item({type: type, creating: true}, this.root);
745 // Create a new List object to represent the child.
746 newItem = new M.core_availability.List({c: [], showc: true}, false, this.root);
749 this.addChild(newItem);
750 // Update the form and list HTML.
751 M.core_availability.form.update();
752 M.core_availability.form.rootList.renumber();
755 dialogRef.dialog.destroy();
756 newItem.focusAfterAdd();
761 * Gets the value of the list ready to convert to JSON and fill form field.
764 * @return {Object} Value of list suitable for use in JSON
766 M.core_availability.List.prototype.getValue = function() {
767 // Work out operator from selects.
769 value.op = this.node.one('.availability-neg').get('value') +
770 this.node.one('.availability-op').get('value');
772 // Work out children from list.
775 for (i = 0; i < this.children.length; i++) {
776 value.c.push(this.children[i].getValue());
779 // Work out show/showc for root level.
781 if (this.isIndividualShowIcons()) {
783 for (i = 0; i < this.children.length; i++) {
784 value.showc.push(!this.children[i].eyeIcon.isHidden());
787 value.show = !this.eyeIcon.isHidden();
794 * Checks whether this list has any errors (incorrect user input). If so,
795 * an error string identifier in the form langfile:langstring should be pushed
796 * into the errors array.
799 * @param {Array} errors Array of errors so far
801 M.core_availability.List.prototype.fillErrors = function(errors) {
802 // List with no items is an error (except root).
803 if (this.children.length === 0 && !this.root) {
804 errors.push('availability:error_list_nochildren');
807 for (var i = 0; i < this.children.length; i++) {
808 this.children[i].fillErrors(errors);
813 * Checks whether the list contains any items of the given type name.
815 * @method hasItemOfType
816 * @param {String} pluginType Required plugin type (name)
817 * @return {Boolean} True if there is one
819 M.core_availability.List.prototype.hasItemOfType = function(pluginType) {
821 for (var i = 0; i < this.children.length; i++) {
822 var child = this.children[i];
823 if (child instanceof M.core_availability.List) {
825 if (child.hasItemOfType(pluginType)) {
829 if (child.pluginType === pluginType) {
838 * Eye icon for this list (null if none).
841 * @type M.core_availability.EyeIcon
843 M.core_availability.List.prototype.eyeIcon = null;
846 * True if list is special root level list.
851 M.core_availability.List.prototype.root = false;
854 * Array containing children (Lists or Items).
857 * @type M.core_availability.List[]|M.core_availability.Item[]
859 M.core_availability.List.prototype.children = null;
862 * HTML outer node for list.
867 M.core_availability.List.prototype.node = null;
870 * HTML node for inner div that actually is the displayed list.
875 M.core_availability.List.prototype.inner = null;
879 * Represents a single condition.
881 * @class M.core_availability.Item
883 * @param {Object} json Decoded JSON value
884 * @param {Boolean} root True if this item is a child of the root list.
886 M.core_availability.Item = function(json, root) {
887 this.pluginType = json.type;
888 if (M.core_availability.form.plugins[json.type] === undefined) {
889 // Handle undefined plugins.
891 this.pluginNode = Y.Node.create('<div class="availability-warning">' +
892 M.util.get_string('missingplugin', 'availability') + '</div>');
895 this.plugin = M.core_availability.form.plugins[json.type];
896 this.pluginNode = this.plugin.getNode(json);
898 // Add a class with the plugin Frankenstyle name to make CSS easier in plugin.
899 this.pluginNode.addClass('availability_' + json.type);
902 this.node = Y.Node.create('<div class="availability-item d-sm-flex align-items-center"><h3 class="accesshide"></h3></div>');
904 // Add eye icon if required. This icon is added for root items, but may be
905 // hidden depending on the selected list operator.
908 if (json.showc !== undefined) {
911 this.eyeIcon = new M.core_availability.EyeIcon(true, shown);
912 this.node.appendChild(this.eyeIcon.span);
915 // Add plugin controls.
916 this.pluginNode.addClass('availability-plugincontrols');
917 this.node.appendChild(this.pluginNode);
919 // Add delete button for node.
920 var deleteIcon = new M.core_availability.DeleteIcon(this);
921 this.node.appendChild(deleteIcon.span);
923 // Add the invalid marker (empty).
924 this.node.appendChild(document.createTextNode(' '));
925 this.node.appendChild(Y.Node.create('<span class="label label-warning"/>'));
929 * Obtains the value of this condition, which will be serialized into JSON
930 * format and stored in the form.
933 * @return {Object} JavaScript object containing value of this item
935 M.core_availability.Item.prototype.getValue = function() {
936 var value = {'type': this.pluginType};
938 this.plugin.fillValue(value, this.pluginNode);
944 * Checks whether this condition has any errors (incorrect user input). If so,
945 * an error string identifier in the form langfile:langstring should be pushed
946 * into the errors array.
949 * @param {Array} errors Array of errors so far
951 M.core_availability.Item.prototype.fillErrors = function(errors) {
952 var before = errors.length;
955 this.plugin.fillErrors(errors, this.pluginNode);
957 // Unknown plugin is an error
958 errors.push('core_availability:item_unknowntype');
960 // If any errors were added, add the marker to this item.
961 var errorLabel = this.node.one('> .label-warning');
962 if (errors.length !== before && !errorLabel.get('firstChild')) {
963 errorLabel.appendChild(document.createTextNode(M.util.get_string('invalid', 'availability')));
964 } else if (errors.length === before && errorLabel.get('firstChild')) {
965 errorLabel.get('firstChild').remove();
970 * Renumbers the item.
973 * @param {String} number Number to use in heading for this item
975 M.core_availability.Item.prototype.renumber = function(number) {
976 // Update heading for item.
977 var headingParams = {number: number};
979 headingParams.type = M.util.get_string('title', 'availability_' + this.pluginType);
981 headingParams.type = '[' + this.pluginType + ']';
983 headingParams.number = number + ':';
984 var heading = M.util.get_string('itemheading', 'availability', headingParams);
985 this.node.one('> h3').set('innerHTML', heading);
989 * Focuses something after a new item is added.
991 * @method focusAfterAdd
993 M.core_availability.Item.prototype.focusAfterAdd = function() {
994 this.plugin.focusAfterAdd(this.pluginNode);
1000 * @property pluginType
1003 M.core_availability.Item.prototype.pluginType = null;
1006 * Object representing plugin form controls.
1011 M.core_availability.Item.prototype.plugin = null;
1014 * Eye icon for item.
1017 * @type M.core_availability.EyeIcon
1019 M.core_availability.Item.prototype.eyeIcon = null;
1022 * HTML node for item.
1027 M.core_availability.Item.prototype.node = null;
1030 * Inner part of node that is owned by plugin.
1032 * @property pluginNode
1035 M.core_availability.Item.prototype.pluginNode = null;
1039 * Eye icon (to control show/hide of the activity if the user fails a condition).
1041 * There are individual eye icons (show/hide control for a single condition) and
1042 * 'all' eye icons (show/hide control that applies to the entire item, whatever
1043 * reason it fails for). This is necessary because the individual conditions
1044 * don't make sense for OR and AND NOT lists.
1046 * @class M.core_availability.EyeIcon
1048 * @param {Boolean} individual True if the icon is controlling a single condition
1049 * @param {Boolean} shown True if icon is initially in shown state
1051 M.core_availability.EyeIcon = function(individual, shown) {
1052 this.individual = individual;
1053 this.span = Y.Node.create('<a class="availability-eye col-form-label" href="#" role="button">');
1054 var icon = Y.Node.create('<img />');
1055 this.span.appendChild(icon);
1057 // Set up button text and icon.
1058 var suffix = individual ? '_individual' : '_all',
1059 setHidden = function() {
1060 var hiddenStr = M.util.get_string('hidden' + suffix, 'availability');
1061 icon.set('src', M.util.image_url('i/show', 'core'));
1062 icon.set('alt', hiddenStr);
1063 this.span.set('title', hiddenStr + ' \u2022 ' +
1064 M.util.get_string('show_verb', 'availability'));
1066 setShown = function() {
1067 var shownStr = M.util.get_string('shown' + suffix, 'availability');
1068 icon.set('src', M.util.image_url('i/hide', 'core'));
1069 icon.set('alt', shownStr);
1070 this.span.set('title', shownStr + ' \u2022 ' +
1071 M.util.get_string('hide_verb', 'availability'));
1074 setShown.call(this);
1076 setHidden.call(this);
1079 // Update when button is clicked.
1080 var click = function(e) {
1082 if (this.isHidden()) {
1083 setShown.call(this);
1085 setHidden.call(this);
1087 M.core_availability.form.update();
1089 this.span.on('click', click, this);
1090 this.span.on('key', click, 'up:32', this);
1091 this.span.on('key', function(e) {
1093 }, 'down:32', this);
1097 * True if this eye icon is an individual one (see above).
1099 * @property individual
1102 M.core_availability.EyeIcon.prototype.individual = false;
1105 * YUI node for the span that contains this icon.
1110 M.core_availability.EyeIcon.prototype.span = null;
1113 * Checks the current state of the icon.
1116 * @return {Boolean} True if this icon is set to 'hidden'
1118 M.core_availability.EyeIcon.prototype.isHidden = function() {
1119 var suffix = this.individual ? '_individual' : '_all',
1120 compare = M.util.get_string('hidden' + suffix, 'availability');
1121 return this.span.one('img').get('alt') === compare;
1126 * Delete icon (to delete an Item or List).
1128 * @class M.core_availability.DeleteIcon
1130 * @param {M.core_availability.Item|M.core_availability.List} toDelete Thing to delete
1132 M.core_availability.DeleteIcon = function(toDelete) {
1133 this.span = Y.Node.create('<a class="d-inline-block col-form-label availability-delete p-x-1" href="#" title="' +
1134 M.util.get_string('delete', 'moodle') + '" role="button">');
1135 var img = Y.Node.create('<img src="' + M.util.image_url('t/delete', 'core') +
1136 '" alt="' + M.util.get_string('delete', 'moodle') + '" />');
1137 this.span.appendChild(img);
1138 var click = function(e) {
1140 M.core_availability.form.rootList.deleteDescendant(toDelete);
1141 M.core_availability.form.rootList.renumber();
1143 this.span.on('click', click, this);
1144 this.span.on('key', click, 'up:32', this);
1145 this.span.on('key', function(e) {
1147 }, 'down:32', this);
1151 * YUI node for the span that contains this icon.
1156 M.core_availability.DeleteIcon.prototype.span = null;