MDL-61899 tool_dataprivacy: Subject access requests tool
[moodle.git] / admin / tool / dataprivacy / amd / src / effective_retention_period.js
blobc73fb05e884f5004bf3a6570e552e98b64a1d974
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * Module to update the displayed retention period.
18  *
19  * @module     tool_dataprivacy/effective_retention_period
20  * @package    tool_dataprivacy
21  * @copyright  2018 David Monllao
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 define(['jquery'],
25     function($) {
27         var SELECTORS = {
28             PURPOSE_SELECT: '#id_purposeid',
29             RETENTION_FIELD: '.form-control-static',
30         };
32         /**
33          * Constructor for the retention period display.
34          *
35          * @param {Array} purposeRetentionPeriods Associative array of purposeids with effective retention period at this context
36          */
37         var EffectiveRetentionPeriod = function(purposeRetentionPeriods) {
38             this.purposeRetentionPeriods = purposeRetentionPeriods;
39             this.registerEventListeners();
40         };
42         /**
43          * Removes the current 'change' listeners.
44          *
45          * Useful when a new form is loaded.
46          */
47         var removeListeners = function() {
48             $(SELECTORS.PURPOSE_SELECT).off('change');
49         };
51         /**
52          * @var {Array} purposeRetentionPeriods
53          * @private
54          */
55         EffectiveRetentionPeriod.prototype.purposeRetentionPeriods = [];
57         /**
58          * Add purpose change listeners.
59          *
60          * @method registerEventListeners
61          */
62         EffectiveRetentionPeriod.prototype.registerEventListeners = function() {
64             $(SELECTORS.PURPOSE_SELECT).on('change', function(ev) {
65                 var selected = $(ev.currentTarget).val();
66                 var selectedPurpose = this.purposeRetentionPeriods[selected];
68                 $(SELECTORS.RETENTION_FIELD).each(function() {
69                     var node = $(this);
70                     // Sucky way to select the text, but no id for static fields :(.
71                     var retentionField = node.siblings('#id_error_retention_current');
72                     if (retentionField.length > 0) {
73                         node.text(selectedPurpose);
74                     }
75                 });
77             }.bind(this));
78         };
80         return /** @alias module:tool_dataprivacy/effective_retention_period */ {
81             init: function(purposeRetentionPeriods) {
82                 // Remove previously attached listeners.
83                 removeListeners();
84                 return new EffectiveRetentionPeriod(purposeRetentionPeriods);
85             }
86         };
87     }