Bug 30708: Rebase - Use name instead of url for router-links
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / cookieconsent.js
1 (function () {
2     // Has the user previously consented, establish our
3     // initial state
4     let selected = [];
5     let existingConsent = '';
6     // The presence of a 'cookieConsent' local storage item indicates
7     // that previous consent has been set. If the value is empty, then
8     // no consent to non-essential cookies was given
9     const hasStoredConsent = localStorage.getItem('cookieConsent');
10     getExistingConsent();
11
12     // The consent bar may not be in the DOM if it is not being used
13     const consentBar = $('#cookieConsentBar');
14     if (!consentBar) {
15         return;
16     }
17     if (hasStoredConsent === null) {
18         showConsentBar();
19     }
20     addButtonHandlers();
21
22     // When the modal is opened, populate our state based on currently
23     // selected values
24     $('#cookieConsentModal').on('shown.bs.modal', function () {
25         initialiseSelected();
26     });
27
28     // Initialise existing consent based on local storage
29     function getExistingConsent() {
30         existingConsent = localStorage.getItem('cookieConsent') ?
31             localStorage.getItem('cookieConsent') :
32             [];
33     }
34
35     function showConsentBar() {
36         const consentBar = $('#cookieConsentBar');
37         const langmenu = $('#changelanguage');
38         if(langmenu) {
39             const height = langmenu.height();
40             consentBar.css('bottom', height);
41         };
42         consentBar.attr('aria-hidden', 'false');
43         consentBar.show();
44     }
45
46     function hideConsentBar() {
47         const consentBar = $('#cookieConsentBar');
48         consentBar.attr('aria-hidden', 'true');
49         consentBar.hide();
50     }
51
52     // Hides the appropriate consent container, depending on what
53     // is currently visible
54     function hideContainer() {
55         if ($('#cookieConsentModal').hasClass('in')) {
56             $('#cookieConsentModal').modal('hide');
57         } else {
58             hideConsentBar();
59         }
60     }
61
62     // Initialise our state of selected item and enable/disable
63     // the "Accept selected" button appropriately
64     function initialiseSelected() {
65         selected = [];
66         $('.consentCheckbox').each(function () {
67             const val = $(this).val();
68             if (existingConsent.indexOf(val) > -1 ) {
69                 $(this).prop('checked', true);
70                 selected.push(val);
71             } else {
72                 $(this).prop('checked', false);
73             }
74         });
75         enableDisableSelectedButton();
76     }
77
78     // Maintain our state of selected items and enable/disable
79     // the "Accept selected" button appropriately
80     function maintainSelected() {
81         selected = [];
82         $('.consentCheckbox:checked').each(function () {
83             selected.push($(this).val());
84         });
85         enableDisableSelectedButton();
86     }
87
88     // Set the enabled / disabled state of the
89     // "Accept selected button based on the checkbox
90     // states
91     function enableDisableSelectedButton() {
92         $('#consentAcceptSelected').prop(
93             'disabled',
94             selected.length === 0
95         );
96     }
97
98     function runConsentedCode() {
99         getExistingConsent();
100         $('.consentCode').each(function () {
101             // The user has explicitly consented to this code, or the
102             // code doesn't require consent in the staff view
103             if (
104                 existingConsent.indexOf($(this).data('consent-id')) > -1 ||
105                 !$(this).data('requires-consent')
106             ) {
107                 const code = atob($(this).data('consent-code'));
108                 const func = Function(code);
109                 func();
110             } else {
111                 // This code doesn't have consent to run, we may need to remove
112                 // any cookies it has previously set
113                 const matchPattern = $(this).data('consent-match-pattern');
114                 const cookieDomain = $(this).data('consent-cookie-domain');
115                 const cookiePath = $(this).data('consent-cookie-path');
116                 if (matchPattern.length > 0) {
117                     const regex = new RegExp(matchPattern);
118                     const allCookies = document.cookie.split('; ');
119                     allCookies.forEach(function (cookie) {
120                         const name = cookie.split('=')[0];
121                         if (regex.test(name)) {
122                             document.cookie = name + '=; expires=Thu, 01 Jan 1970 00: 00: 01 GMT; domain=' + cookieDomain + '; path=' + cookiePath;
123                         }
124                     });
125
126                 }
127             }
128         });
129     }
130
131     function addButtonHandlers() {
132         // "Accept all" handler
133         $('.consentAcceptAll').on('click', function(e) {
134             e.preventDefault();
135             let toSave = [];
136             $('.consentCheckbox').each(function () {
137                 const val = $(this).val();
138                 toSave.push(val);
139             });
140             localStorage.setItem('cookieConsent', toSave);
141             hideContainer();
142             runConsentedCode();
143         });
144
145         // "Accept essential" handler
146         $('.consentAcceptEssential').on('click', function(e) {
147             e.preventDefault();
148             localStorage.setItem('cookieConsent', []);
149             hideContainer();
150             runConsentedCode();
151         });
152
153         // "Accept selected" handler
154         $('#consentAcceptSelected').on('click', function(e) {
155             e.preventDefault();
156             const toSave = selected.length > 0 ? selected : [];
157             localStorage.setItem('cookieConsent', toSave);
158             hideContainer();
159             runConsentedCode();
160         });
161
162         // "Cancel" handler
163         $('.consentCloseModal').on('click', function(e) {
164             e.preventDefault();
165             hideContainer();
166             showConsentBar();
167         });
168
169         // "More information" handler
170         $('#consentMoreInfo, #viewCookieConsents').on(
171             'click',
172             function (e) {
173                 e.preventDefault();
174                 hideConsentBar();
175                 // Ensure we're up to date with the existing consent
176                 getExistingConsent();
177                 // Prevent the modal from being closed with anything
178                 // but the buttons
179                 $('#cookieConsentModal')
180                     .modal({
181                         backdrop: 'static',
182                         keyboard: false,
183                         focus: true,
184                         show: true
185                     });
186             }
187         );
188         $('.consentCheckbox').on('click', function () {
189             maintainSelected();
190         });
191     }
192
193     // On page load, run any code that has been given consent
194     runConsentedCode();
195 })();