MDL-62550 core_privacy: remove theme specific js from retention js
[moodle.git] / admin / tool / dataprivacy / amd / src / effective_retention_period.js
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/>.
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: '#fitem_id_retention_current [data-fieldtype=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];
67                 $(SELECTORS.RETENTION_FIELD).text(selectedPurpose);
68             }.bind(this));
69         };
71         return /** @alias module:tool_dataprivacy/effective_retention_period */ {
72             init: function(purposeRetentionPeriods) {
73                 // Remove previously attached listeners.
74                 removeListeners();
75                 return new EffectiveRetentionPeriod(purposeRetentionPeriods);
76             }
77         };
78     }
79 );