MDL-62550 core_theme: remove unused #fgroup_ and #fitem_ rules
[moodle.git] / admin / tool / dataprivacy / amd / src / effective_retention_period.js
CommitLineData
5efc1f9e
DM
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.
12//
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/>.
15
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 */
24define(['jquery'],
25 function($) {
26
27 var SELECTORS = {
28 PURPOSE_SELECT: '#id_purposeid',
8b451d25
DM
29 RETENTION_FIELD_BOOST: '#id_error_retention_current',
30 RETENTION_FIELD_CLEAN: '#fitem_id_retention_current [data-fieldtype=static]',
5efc1f9e
DM
31 };
32
33 /**
34 * Constructor for the retention period display.
35 *
36 * @param {Array} purposeRetentionPeriods Associative array of purposeids with effective retention period at this context
37 */
38 var EffectiveRetentionPeriod = function(purposeRetentionPeriods) {
39 this.purposeRetentionPeriods = purposeRetentionPeriods;
40 this.registerEventListeners();
41 };
42
43 /**
44 * Removes the current 'change' listeners.
45 *
46 * Useful when a new form is loaded.
47 */
48 var removeListeners = function() {
49 $(SELECTORS.PURPOSE_SELECT).off('change');
50 };
51
52 /**
53 * @var {Array} purposeRetentionPeriods
54 * @private
55 */
56 EffectiveRetentionPeriod.prototype.purposeRetentionPeriods = [];
57
58 /**
59 * Add purpose change listeners.
60 *
61 * @method registerEventListeners
62 */
63 EffectiveRetentionPeriod.prototype.registerEventListeners = function() {
64
65 $(SELECTORS.PURPOSE_SELECT).on('change', function(ev) {
66 var selected = $(ev.currentTarget).val();
67 var selectedPurpose = this.purposeRetentionPeriods[selected];
68
8b451d25
DM
69 var cleanSelector = $(SELECTORS.RETENTION_FIELD_CLEAN);
70 if (cleanSelector.length > 0) {
71 cleanSelector.text(selectedPurpose);
72 } else {
73 var boostSelector = $(SELECTORS.RETENTION_FIELD_BOOST);
74 var retentionField = boostSelector.siblings();
5efc1f9e 75 if (retentionField.length > 0) {
8b451d25 76 retentionField.text(selectedPurpose);
5efc1f9e 77 }
8b451d25 78 }
5efc1f9e
DM
79
80 }.bind(this));
81 };
82
83 return /** @alias module:tool_dataprivacy/effective_retention_period */ {
84 init: function(purposeRetentionPeriods) {
85 // Remove previously attached listeners.
86 removeListeners();
87 return new EffectiveRetentionPeriod(purposeRetentionPeriods);
88 }
89 };
90 }
91);
92