PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
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 1.7.3 All 36 releases
vikbooking / site / resources / chat / task.js

task.js in VikBooking Hotel Booking Engine & PMS trunk, at site/resources/chat/task.js

451 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function($, w) {
2 'use strict';
3
4 const insideTaskManagementForm = () => {
5 return $('.vbo-managetask-modal-form').length ? true : false;
6 }
7
8 /***************
9 * SEE BOOKING *
10 ***************/
11
12 /**
13 * See task booking button action handler.
14 */
15 $(w).on('chat.task.booking.action', (event) => {
16 const [root, parentEvent, button, chat] = event.args;
17
18 // display booking details on a new widget
19 VBOCore.handleDisplayWidgetNotification({
20 widget_id: 'booking_details',
21 }, {
22 booking_id: button.booking,
23 modal_options: {
24 suffix: 'vbo-task-booking-details',
25 body_prepend: false,
26 enlargeable: false,
27 minimizeable: false,
28 },
29 });
30 });
31
32 /************
33 * SEE TASK *
34 ************/
35
36 /**
37 * See task button visibility handler.
38 */
39 $(w).on('chat.task.see.visible', async (event) => {
40 event.shouldDisplay = !insideTaskManagementForm();
41 });
42
43 /**
44 * See task button action handler.
45 */
46 $(w).on('chat.task.see.action', (event) => {
47 const [root, parentEvent, button, chat] = event.args;
48
49 // define the modal save button
50 const saveButton = $('<button></button>')
51 .attr('type', 'button')
52 .addClass('btn btn-success')
53 .text(Joomla.JText._('VBSAVE'))
54 .on('click', function() {
55 $(this).prop('disabled', true);
56
57 // start loading animation
58 VBOCore.emitEvent('vbo-tm-edittask-loading');
59
60 // get form data
61 const taskForm = new FormData(document.querySelector('#vbo-tm-task-manage-form'));
62
63 // build query parameters for the request
64 let qpRequest = new URLSearchParams(taskForm);
65
66 // make sure the request always includes the assignees query parameter, even if the list is empty
67 if (!qpRequest.has('data[assignees][]')) {
68 qpRequest.append('data[assignees][]', []);
69 }
70
71 // make sure the request always includes the tags query parameter, even if the list is empty
72 if (!qpRequest.has('data[tags][]')) {
73 qpRequest.append('data[tags][]', []);
74 }
75
76 qpRequest.append('task', 'taskmanager.updateTask');
77
78 // make the request
79 VBOCore.doAjax(
80 chat.data.environment.url,
81 qpRequest.toString(),
82 (resp) => {
83 // dismiss the modal
84 VBOCore.emitEvent('vbo-tm-edittask-dismiss');
85
86 // refresh the task manager filters, if any
87 VBOCore.emitEvent('vbo-tm-filters-changed', {
88 filters: typeof vboTmFilters !== 'undefined' ? vboTmFilters : {},
89 });
90 },
91 (error) => {
92 // display error message
93 alert(error.responseText);
94
95 // re-enable submit button
96 $(this).prop('disabled', false);
97
98 // stop loading
99 VBOCore.emitEvent('vbo-tm-edittask-loading');
100 }
101 );
102 });
103
104 // display modal
105 let modalBody = VBOCore.displayModal({
106 suffix: 'tm_edittask_modal',
107 title: Joomla.JText._('VBO_TASK') + ' #' + chat.data.environment.context.id,
108 extra_class: 'vbo-modal-rounded vbo-modal-taller vbo-modal-large',
109 body_prepend: true,
110 lock_scroll: true,
111 escape_dismiss: false,
112 footer_right: saveButton,
113 loading_event: 'vbo-tm-edittask-loading',
114 dismiss_event: 'vbo-tm-edittask-dismiss',
115 });
116
117 // start loading animation
118 VBOCore.emitEvent('vbo-tm-edittask-loading');
119
120 // make the request
121 VBOCore.doAjax(
122 chat.data.environment.url,
123 {
124 task: 'taskmanager.renderLayout',
125 type: 'tasks.managetask',
126 data: {
127 task_id: chat.data.environment.context.id,
128 form_id: 'vbo-tm-task-manage-form',
129 },
130 },
131 (resp) => {
132 // stop loading
133 VBOCore.emitEvent('vbo-tm-edittask-loading');
134
135 try {
136 // decode the response (if needed), and append the content to the modal body
137 let obj_res = typeof resp === 'string' ? JSON.parse(resp) : resp;
138 modalBody.append(obj_res['html']);
139 } catch (err) {
140 console.error('Error decoding the response', err, resp);
141 }
142 },
143 (error) => {
144 // display error message
145 alert(error.responseText);
146
147 // stop loading
148 VBOCore.emitEvent('vbo-tm-edittask-loading');
149 }
150 );
151 });
152
153 /************
154 * NEW TASK *
155 ************/
156
157 const chooseArea = (chat) => {
158 return new Promise((resolve, reject) => {
159 let areaSelected = false;
160
161 const pickArea = (event) => {
162 areaSelected = true;
163
164 // auto-close modal
165 VBOCore.emitEvent('vbo-tm-pickarea-dismiss');
166
167 setTimeout(() => {
168 resolve(event?.detail?.area);
169 }, 500);
170 }
171
172 // display modal
173 let modalBody = VBOCore.displayModal({
174 suffix: 'tm_areapicker_modal',
175 title: Joomla.JText._('VBO_PROJECTS_AREAS'),
176 extra_class: 'vbo-modal-rounded vbo-modal-nofooter',
177 body_prepend: true,
178 lock_scroll: true,
179 escape_dismiss: false,
180 loading_event: 'vbo-tm-pickarea-loading',
181 dismiss_event: 'vbo-tm-pickarea-dismiss',
182 onDismiss: () => {
183 document.removeEventListener('vbo-tm-area-id-selected', pickArea);
184
185 if (!areaSelected) {
186 reject('Task area selection aborted.');
187 }
188 }
189 });
190
191 // start loading animation
192 VBOCore.emitEvent('vbo-tm-pickarea-loading');
193
194 // make the request
195 VBOCore.doAjax(
196 chat.data.environment.url,
197 {
198 task: 'taskmanager.renderLayout',
199 type: 'tasks.selectarea',
200 },
201 (resp) => {
202 // stop loading
203 VBOCore.emitEvent('vbo-tm-pickarea-loading');
204
205 try {
206 // decode the response (if needed), and append the content to the modal body
207 let obj_res = typeof resp === 'string' ? JSON.parse(resp) : resp;
208 modalBody.append(obj_res['html']);
209 } catch (err) {
210 console.error('Error decoding the response', err, resp);
211 }
212
213 // observe area selection
214 document.addEventListener('vbo-tm-area-id-selected', pickArea, {
215 once: true,
216 });
217 },
218 (error) => {
219 // display error message
220 alert(error.responseText);
221
222 // stop loading
223 VBOCore.emitEvent('vbo-tm-pickarea-loading');
224 }
225 );
226 });
227 }
228
229 /**
230 * New task button visibility handler.
231 */
232 $(w).on('chat.task.new.visible', async (event) => {
233 event.shouldDisplay = !insideTaskManagementForm();
234 });
235
236 /**
237 * New task button action handler.
238 */
239 $(w).on('chat.task.new.action', async (event) => {
240 const [root, parentEvent, button, chat] = event.args;
241
242 let area = null;
243
244 try {
245 area = await chooseArea(chat);
246 } catch (err) {
247 if (err) {
248 console.warn(err);
249 }
250
251 // selection aborted
252 return;
253 }
254
255 // define the modal save button
256 let saveButton = jQuery('<button></button>')
257 .attr('type', 'button')
258 .addClass('btn btn-success')
259 .text(Joomla.JText._('VBSAVE'))
260 .on('click', function() {
261 $(this).prop('disabled', true);
262
263 // start loading animation
264 VBOCore.emitEvent('vbo-tm-newtask-loading');
265
266 // get form data
267 const taskForm = new FormData(document.querySelector('#vbo-tm-task-manage-form'));
268
269 // build query parameters for the request
270 let qpRequest = new URLSearchParams(taskForm);
271
272 qpRequest.append('task', 'taskmanager.createTask');
273
274 // make the request
275 VBOCore.doAjax(
276 chat.data.environment.url,
277 qpRequest.toString(),
278 (resp) => {
279 // dismiss the modal
280 VBOCore.emitEvent('vbo-tm-newtask-dismiss');
281
282 // refresh the task manager filters, if any
283 VBOCore.emitEvent('vbo-tm-filters-changed', {
284 filters: typeof vboTmFilters !== 'undefined' ? vboTmFilters : {},
285 });
286 },
287 (error) => {
288 // display error message
289 alert(error.responseText);
290
291 // re-enable submit button
292 $(this).prop('disabled', false);
293
294 // stop loading
295 VBOCore.emitEvent('vbo-tm-newtask-loading');
296 }
297 );
298 });
299
300 // display modal
301 let modalBody = VBOCore.displayModal({
302 suffix: 'tm_newtask_modal',
303 title: area.name + ' - ' + Joomla.JText._('VBO_NEW_TASK'),
304 extra_class: 'vbo-modal-rounded vbo-modal-taller vbo-modal-large',
305 body_prepend: true,
306 lock_scroll: true,
307 escape_dismiss: false,
308 footer_right: saveButton,
309 loading_event: 'vbo-tm-newtask-loading',
310 dismiss_event: 'vbo-tm-newtask-dismiss',
311 });
312
313 // start loading animation
314 VBOCore.emitEvent('vbo-tm-newtask-loading');
315
316 // make the request
317 VBOCore.doAjax(
318 chat.data.environment.url,
319 {
320 task: 'taskmanager.renderLayout',
321 type: 'tasks.managetask',
322 data: {
323 area_id: area.id,
324 form_id: 'vbo-tm-task-manage-form',
325 },
326 },
327 (resp) => {
328 // stop loading
329 VBOCore.emitEvent('vbo-tm-newtask-loading');
330
331 try {
332 // decode the response (if needed), and append the content to the modal body
333 let obj_res = typeof resp === 'string' ? JSON.parse(resp) : resp;
334 modalBody.append(obj_res['html']);
335 } catch (err) {
336 console.error('Error decoding the response', err, resp);
337 }
338 },
339 (error) => {
340 // display error message
341 alert(error.responseText);
342
343 // stop loading
344 VBOCore.emitEvent('vbo-tm-newtask-loading');
345 }
346 );
347 });
348
349 /*****************
350 * CHANGE STATUS *
351 *****************/
352
353 const changeStatus = (status, chat) => {
354 return new Promise((resolve, reject) => {
355 VBOCore.doAjax(
356 chat.data.environment.url,
357 {
358 task: 'taskmanager.updateTask',
359 data: {
360 id: chat.data.environment.context.id,
361 status_enum: status,
362 }
363 },
364 (resp) => {
365 resolve();
366 },
367 (error) => {
368 reject(error.responseText || error.statusText || 'Error');
369 }
370 );
371 });
372 }
373
374 /**
375 * Status task button icon handler.
376 */
377 $(w).on('chat.task.status.icon', (event) => {
378 const [root, menu, button, chat] = event.args;
379
380 // build button icon element
381 const btnIconEl = $('<span></span>')
382 .addClass('vbo-colortag-circle')
383 .addClass('vbo-tm-colortag-circle')
384 .addClass('vbo-tm-statustype-circle');
385
386 if (button?.color) {
387 btnIconEl.addClass(button.color);
388 }
389
390 event.displayIcon = btnIconEl;
391 });
392
393 /**
394 * Status task button disable handler.
395 */
396 $(w).on('chat.task.status.disabled', (event) => {
397 const [root, menu, button, chat] = event.args;
398
399 // iterate all the contextual menu buttons
400 $(root).vboContextMenu('buttons').forEach((btn) => {
401 // flag the button as disabled in case it is selected
402 if (button.id == btn.id && btn.selected) {
403 event.shouldDisable = true;
404 }
405 });
406 });
407
408 /**
409 * Status task button action handler.
410 */
411 $(w).on('chat.task.status.action', async (event) => {
412 const [root, parentEvent, button, chat] = event.args;
413
414 try {
415 // make request to change the status
416 await changeStatus(button.id, chat);
417
418 // obtain all the contextual menu buttons
419 const buttons = $(root).vboContextMenu('buttons');
420
421 buttons.forEach((btn) => {
422 // flag the clicked button as selected
423 btn.selected = button.id == btn.id;
424 });
425
426 // update buttons
427 $(root).vboContextMenu('buttons', buttons);
428 } catch (error) {
429 alert(error);
430 }
431 });
432
433 /**
434 * Status task button visibility handler.
435 */
436 $(w).on('chat.task.status.visible', async (event) => {
437 event.shouldDisplay = !insideTaskManagementForm();
438 });
439
440 /*****************
441 * BUTTON GROUPS *
442 *****************/
443
444 /**
445 * Task button group visibility handler.
446 */
447 $(w).on('chat.task.btngroup.visible', async (event) => {
448 event.shouldDisplay = !insideTaskManagementForm();
449 });
450
451 })(jQuery, window);