PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.8
VikBooking Hotel Booking Engine & PMS v1.6.8
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / layouts / sidepanel / notificationscenter.php

notificationscenter.php in VikBooking Hotel Booking Engine & PMS 1.6.8, at admin/layouts/sidepanel/notificationscenter.php

289 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage com_vikbooking
5 * @author Alessio Gaggii - E4J srl
6 * @copyright Copyright (C) 2024 E4J srl. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 * @link https://vikwp.com
9 */
10
11 defined('ABSPATH') or die('No script kiddies please!');
12
13 /**
14 * Obtain vars from arguments received in the layout file.
15 * This layout file should be called once at most per page.
16 *
17 * @var string $vbo_page the name of the current View in VBO.
18 * @var string $btn_trigger the CSS selector of the button that opens the panel.
19 * @var int $badge_count the total number of unread notifications.
20 * @var string $badge_c_attr the handler data attribute for the badge counter.
21 * @var string $badge_r_attr the handler data attribute for the readable badge counter.
22 */
23 extract($displayData);
24
25 ?>
26
27 <div class="vbo-notifications-center-wrap vbo-notifications-center-off">
28 <div class="vbo-notifications-center-inner"></div>
29 </div>
30
31 <script type="text/javascript">
32
33 /**
34 * @var bool
35 */
36 var vbo_notifs_center_on = false;
37
38 /**
39 * @var string
40 */
41 var vbo_notifs_center_last_keydown = null;
42
43 /**
44 * @var number
45 */
46 var vbo_notifs_center_widget_js_id = 0;
47
48 jQuery(function() {
49
50 /**
51 * Register the button trigger click handler.
52 */
53 jQuery('<?php echo $btn_trigger; ?>').on('click', function() {
54 let wrapper = jQuery('.vbo-notifications-center-wrap');
55 let suggest_push = (VBOCore.notificationsEnabled() === false);
56
57 if (!vbo_notifs_center_widget_js_id) {
58 vbo_notifs_center_widget_js_id = Math.floor(Math.random() * 100000);
59 }
60
61 if (wrapper.hasClass('vbo-notifications-center-off')) {
62 // show notifications center
63 vbo_notifs_center_on = true;
64
65 wrapper.removeClass('vbo-notifications-center-off');
66 wrapper.addClass('vbo-notifications-center-on');
67
68 // build loading string
69 let vbo_notifs_center_loading_html = '<div class="vbo-notifications-center-loading">' + "\n";
70 vbo_notifs_center_loading_html += '<span><?php VikBookingIcons::e('circle-notch', 'fa-spin fa-fw fa-3x'); ?></span>' + "\n";
71 vbo_notifs_center_loading_html += '</div>' + "\n";
72
73 // display loading
74 wrapper
75 .find('.vbo-notifications-center-inner')
76 .html(vbo_notifs_center_loading_html);
77
78 // render admin-widget
79 VBOCore.renderAdminWidget('notifications_center', {
80 _modalRendering: 1,
81 _modalJsId: vbo_notifs_center_widget_js_id,
82 _options: {
83 inMenu: true,
84 suggestPush: suggest_push,
85 },
86 }).then((content) => {
87 // populate content
88 wrapper
89 .find('.vbo-notifications-center-inner')
90 .html(content);
91 }).catch((error) => {
92 // display error
93 alert(error);
94 });
95 } else {
96 // hide notifications center
97 vbo_notifs_center_on = false;
98
99 wrapper.removeClass('vbo-notifications-center-on');
100 wrapper.addClass('vbo-notifications-center-off');
101
102 // destroy the wrapper content
103 wrapper
104 .find('.vbo-notifications-center-inner')
105 .html('');
106
107 // emit the dismissed event
108 VBOCore.emitEvent(VBOCore.options.widget_modal_dismissed + vbo_notifs_center_widget_js_id);
109 }
110 });
111
112 // register listener for esc key pressed to close the notifications center
113 jQuery(document).keyup(function(e) {
114 if (!vbo_notifs_center_on) {
115 return;
116 }
117 if ((e.key && e.key === "Escape") || (e.keyCode && e.keyCode == 27)) {
118 jQuery('<?php echo $btn_trigger; ?>').trigger('click');
119 }
120 });
121
122 // register shortcut for toggling the notifications center (CMD + K, CMD + C)
123 window.addEventListener('keydown', (e) => {
124 e = e || window.event;
125 if (!e.key) {
126 return;
127 }
128 if (e.key === 'k' && e.metaKey) {
129 // set special key listener for sequences of combos
130 e.preventDefault();
131 vbo_notifs_center_last_keydown = 'k';
132 return;
133 }
134 if (vbo_notifs_center_last_keydown === 'k' && e.metaKey && e.key === 'c') {
135 // trigger event to change color scheme preferences
136 e.preventDefault();
137 // unset special key
138 vbo_notifs_center_last_keydown = '';
139 // toggle notifications center
140 jQuery('<?php echo $btn_trigger; ?>').trigger('click');
141 return;
142 }
143 // always unset last key if this point gets reached
144 vbo_notifs_center_last_keydown = '';
145 }, true);
146
147 /**
148 * Listen to the global event for updating the badge counter. This listener will never have to be removed.
149 */
150 document.addEventListener('vbo-badge-count', (e) => {
151 if (!e || !e.detail || !e.detail.hasOwnProperty('badge_count') || isNaN(e.detail['badge_count'])) {
152 return;
153 }
154
155 // get the trigger button element
156 let btn_trigger = document
157 .querySelector('<?php echo $btn_trigger; ?>');
158
159 // get the current badge counter value
160 let badge_count_now = parseInt(btn_trigger.getAttribute('<?php echo $badge_c_attr; ?>'));
161
162 // get the new badge counter value
163 let badge_count_new = parseInt(e.detail['badge_count']);
164
165 // update badge attributes
166 if (badge_count_new <= 0) {
167 // no notifications to be read
168 btn_trigger.setAttribute('<?php echo $badge_c_attr; ?>', 0);
169 btn_trigger.setAttribute('<?php echo $badge_r_attr; ?>', '');
170 } else {
171 // update badge counter
172 btn_trigger.setAttribute('<?php echo $badge_c_attr; ?>', badge_count_new);
173 btn_trigger.setAttribute('<?php echo $badge_r_attr; ?>', (badge_count_new > 99 ? '99+' : badge_count_new));
174 }
175
176 // check if we had new notifications
177 if (badge_count_new > badge_count_now) {
178 // add shaking class
179 btn_trigger.classList.add('shaking');
180
181 // remove shaking class after some time
182 setTimeout(() => {
183 btn_trigger.classList.remove('shaking');
184 }, 2000);
185 }
186
187 // check if support for Web App badge is available
188 if (typeof navigator.setAppBadge !== 'undefined') {
189 if (badge_count_new > 0) {
190 navigator.setAppBadge(badge_count_new);
191 } else {
192 navigator.clearAppBadge();
193 }
194 }
195 });
196
197 /**
198 * Listen to the event for reloading the badge counter. This listener will never have to be removed.
199 * Reloading the badge counter is helpful when clicking on a Push notification from the ServiceWorker.
200 */
201 document.addEventListener('vbo-badge-count-reload', (e) => {
202 // the widget method to call
203 var call_method = 'countUnreadNotifications';
204
205 // make a request to the admin-widget "notifications center" to count the unread notifications
206 VBOCore.doAjax(
207 "<?php echo VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=exec_admin_widget'); ?>",
208 {
209 widget_id: 'notifications_center',
210 call: call_method,
211 return: 1,
212 },
213 (response) => {
214 try {
215 var obj_res = typeof response === 'string' ? JSON.parse(response) : response;
216 if (!obj_res.hasOwnProperty(call_method) || typeof obj_res[call_method] !== 'object') {
217 console.error('Unexpected JSON response', obj_res);
218 return false;
219 }
220
221 // scan all the events to dispatch
222 for (let ev_name in obj_res[call_method]) {
223 if (!obj_res[call_method].hasOwnProperty(ev_name)) {
224 continue;
225 }
226 // emit the event
227 VBOCore.emitEvent(ev_name, {
228 badge_count: obj_res[call_method][ev_name],
229 });
230 }
231 } catch(err) {
232 console.error('could not parse JSON response', err, response);
233 }
234 },
235 (error) => {
236 // silently log the error
237 console.error(error);
238 }
239 );
240 });
241
242 /**
243 * Listen to the event for reading notifications matching certain criterias.
244 * This listener will never have to be removed.
245 */
246 document.addEventListener('vbo-nc-read-notifications', (e) => {
247 if (!e || !e.detail || !e.detail.hasOwnProperty('criteria') || !e.detail['criteria']) {
248 return;
249 }
250
251 // the widget method to call
252 var call_method = 'readMatchingNotifications';
253
254 // make a request to the admin-widget "notifications center" to read any matching notification
255 VBOCore.doAjax(
256 "<?php echo VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=exec_admin_widget'); ?>",
257 {
258 widget_id: 'notifications_center',
259 call: call_method,
260 return: 1,
261 criteria: e.detail['criteria'],
262 },
263 (response) => {
264 try {
265 var obj_res = typeof response === 'string' ? JSON.parse(response) : response;
266 if (!obj_res.hasOwnProperty(call_method) || typeof obj_res[call_method] !== 'object') {
267 console.error('Unexpected JSON response', obj_res);
268 return false;
269 }
270
271 if (obj_res[call_method].hasOwnProperty('read_count') && obj_res[call_method]['read_count']) {
272 // trigger the event to reload the badge count after having read some notifications
273 VBOCore.emitEvent('vbo-badge-count-reload');
274 }
275 } catch(err) {
276 console.error('could not parse JSON response', err, response);
277 }
278 },
279 (error) => {
280 // silently log the error
281 console.error(error);
282 }
283 );
284 });
285
286 });
287
288 </script>
289