PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.5
VikBooking Hotel Booking Engine & PMS v1.6.5
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 / helpers / widgets / sticky_notes.php

sticky_notes.php in VikBooking Hotel Booking Engine & PMS 1.6.5, at admin/helpers/widgets/sticky_notes.php

738 lines 26.7 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 - Extensionsforjoomla.com
6 * @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. 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 * Class handler for admin widget "sticky notes". This widget has settings.
15 *
16 * @since 1.14.0 (J) - 1.4.0 (WP)
17 */
18 class VikBookingAdminWidgetStickyNotes extends VikBookingAdminWidget
19 {
20 /**
21 * The instance counter of this widget. Since we do not load individual parameters
22 * for each widget's instance, we use a static counter to determine its settings.
23 *
24 * @var int
25 */
26 protected static $instance_counter = -1;
27
28 /**
29 * We detect the Operating System through the browser useer agent
30 * to display different information about the shortcuts to use.
31 *
32 * @var string
33 */
34 protected $isMac = false;
35
36 /**
37 * Class constructor will define the widget name and identifier.
38 */
39 public function __construct()
40 {
41 // call parent constructor
42 parent::__construct();
43
44 // declare name, description and identifier
45 $this->widgetName = JText::translate('VBO_W_STICKYN_TITLE');
46 $this->widgetDescr = JText::translate('VBO_W_STICKYN_DESCR');
47 $this->widgetId = basename(__FILE__, '.php');
48
49 /**
50 * Define widget and icon and style name.
51 *
52 * @since 1.15.0 (J) - 1.5.0 (WP)
53 */
54 $this->widgetIcon = '<i class="' . VikBookingIcons::i('thumbtack') . '"></i>';
55 $this->widgetStyleName = 'yellow';
56
57 // load widget's settings
58 $this->widgetSettings = $this->loadSettings();
59
60 // determine if the operating system is MacOS or iOS
61 $ua = JFactory::getApplication()->input->server->getString('HTTP_USER_AGENT', '');
62 if (stripos($ua, 'windows') === false && (stripos($ua, 'mac') !== false || stripos($ua, 'iphone') !== false || stripos($ua, 'ipad') !== false)) {
63 $this->isMac = true;
64 }
65 }
66
67 /**
68 * Custom method for this widget only to update one sticky note.
69 * The method is called by the admin controller through an AJAX request.
70 * The visibility should be public, it should not exit the process, and
71 * any content sent to output will be returned to the AJAX response.
72 *
73 * @param int $force_note_index the new index can be forced to insert a new note.
74 * @param int $force_instance the widget's instance to force.
75 * @param string $force_txt the text of the sticky note to force.
76 *
77 * @return void it may output a log with the new widget instance assigned.
78 */
79 public function updateStickyNote($force_note_index = null, $force_instance = null, $force_txt = null)
80 {
81 $note_instance = VikRequest::getInt('note_instance', -1, 'request');
82 $note_txt = VikRequest::getString('note_txt', '', 'request', VIKREQUEST_ALLOWRAW);
83 $note_index = VikRequest::getInt('note_index', 0, 'request');
84
85 if ($force_note_index !== null && $force_note_index >= 0) {
86 // use the forced index instead
87 $note_index = $force_note_index;
88 }
89
90 if ($force_instance !== null && $force_instance >= 0) {
91 // use the forced instance instead
92 $note_instance = $force_instance;
93 }
94
95 if (is_string($force_txt) && strlen($force_txt)) {
96 // use the forced text instead
97 $note_txt = $force_txt;
98 }
99
100 // make sure the settings of the widget are an array
101 if (!is_array($this->widgetSettings)) {
102 $this->widgetSettings = array();
103 }
104
105 // create the new sticky note object
106 $username = $this->getLoggedUserName();
107 $sticky_note = new stdClass;
108 $sticky_note->html = $note_txt;
109 $sticky_note->ts = time();
110 if (!empty($username)) {
111 $sticky_note->user = $username;
112 }
113
114 // eventually append debugging result
115 $result_log = '';
116
117 // eventually append the new widget instance assigned for newly added widgets via AJAX
118 $new_instance_response = '';
119
120 if (!count($this->widgetSettings)) {
121 // push the first sticky note as the first instance of this widget
122 $this->widgetSettings = array(
123 array($sticky_note)
124 );
125 $result_log = 'pushing the first sticky note as the first instance of the widget';
126 $new_instance_response = '[instance=0]';
127 } elseif (isset($this->widgetSettings[$note_instance])) {
128 // this instance was saved already
129 if (isset($this->widgetSettings[$note_instance][$note_index])) {
130 // we already have this exact sticky note index, so we replace it
131 $this->widgetSettings[$note_instance][$note_index] = $sticky_note;
132 $result_log = 'replacing requested note index in existing widget instance';
133 } else {
134 // we push a new sticky note
135 array_push($this->widgetSettings[$note_instance], $sticky_note);
136 $result_log = 'adding a new note to the existing widget instance';
137 }
138 } else {
139 // this is a new instance of the widget, which has settings already - push the new instance and the new sticky note
140 array_push($this->widgetSettings, array($sticky_note));
141 $result_log = 'pushing a new instance and note to the settings';
142 $new_instance_response = '[instance=' . (count($this->widgetSettings) - 1) . ']';
143 }
144
145 // update widget's settings
146 $this->updateSettings(json_encode($this->widgetSettings));
147
148 echo 'e4j.ok' . (!empty($result_log) ? '(' . $result_log . ')' : '') . $new_instance_response;
149 }
150
151 /**
152 * Custom method for this widget only to delete one sticky note.
153 * The method is called by the admin controller through an AJAX request.
154 * The visibility should be public, it should not exit the process, and
155 * any content sent to output will be returned to the AJAX response.
156 */
157 public function deleteStickyNote()
158 {
159 $note_instance = VikRequest::getInt('note_instance', -1, 'request');
160 $note_index = VikRequest::getInt('note_index', 0, 'request');
161
162 // make sure the settings of the widget are an array
163 if (!is_array($this->widgetSettings)) {
164 echo 'e4j.error.no settings found';
165 return;
166 }
167
168 // make sure the instance of the widget exists
169 if (!isset($this->widgetSettings[$note_instance]) || !is_array($this->widgetSettings[$note_instance])) {
170 echo 'e4j.error.instance not found';
171 return;
172 }
173
174 // make sure the index of the note exists
175 if (!isset($this->widgetSettings[$note_instance][$note_index])) {
176 echo 'e4j.error.note index not found, maybe it was never updated before';
177 return;
178 }
179
180 // splice the array to remove the requested note
181 array_splice($this->widgetSettings[$note_instance], $note_index, 1);
182
183 // update widget's settings
184 $this->updateSettings(json_encode($this->widgetSettings));
185
186 echo 'e4j.ok';
187 }
188
189 /**
190 * Custom method for this widget only to sort one sticky note.
191 * The method is called by the admin controller through an AJAX request.
192 * The visibility should be public, it should not exit the process, and
193 * any content sent to output will be returned to the AJAX response.
194 */
195 public function sortStickyNote()
196 {
197 $note_instance = VikRequest::getInt('note_instance', -1, 'request');
198 $note_index_new = VikRequest::getInt('note_index_new', 0, 'request');
199 $note_index_old = VikRequest::getInt('note_index_old', 0, 'request');
200
201 if ($note_index_new == $note_index_old) {
202 // nothing to do, as notes can be sorted only within the same instance
203 echo 'e4j.error.same position given for sticky note';
204 return;
205 }
206
207 // make sure the settings of the widget are an array
208 if (!is_array($this->widgetSettings)) {
209 echo 'e4j.error.no settings found';
210 return;
211 }
212
213 // make sure the instance of the widget exists
214 if (!isset($this->widgetSettings[$note_instance]) || !is_array($this->widgetSettings[$note_instance])) {
215 echo 'e4j.error.instance not found';
216 return;
217 }
218
219 // make sure the old index of the note exists
220 if (!isset($this->widgetSettings[$note_instance][$note_index_old])) {
221 // check if this note was moved before getting saved, so as soon as it was added
222 if (count($this->widgetSettings[$note_instance]) == $note_index_old) {
223 // append the newly created (empty) note before moving it
224 $this->updateStickyNote($note_index_old, $note_instance);
225 // reload the widget's settings after they have been updated with the new note
226 $this->widgetSettings = $this->loadSettings();
227 // proceed below with moving the sticky note
228 } else {
229 // unable to proceed
230 echo 'e4j.error.original note index not found, maybe it was never updated before';
231 return;
232 }
233 }
234
235 // make sure the new index can fit
236 if ($note_index_new > (count($this->widgetSettings[$note_instance]) - 1)) {
237 echo 'e4j.error.new index exceeds the highest position available';
238 return;
239 }
240
241 // move the sticky note from the old index to the new index
242 $extracted = array_splice($this->widgetSettings[$note_instance], $note_index_old, 1);
243 array_splice($this->widgetSettings[$note_instance], $note_index_new, 0, $extracted);
244
245 // update widget's settings
246 $this->updateSettings(json_encode($this->widgetSettings));
247
248 echo 'e4j.ok';
249 }
250
251 /**
252 * Preload the necessary assets.
253 *
254 * @return void
255 */
256 public function preload()
257 {
258 // JS lang def
259 JText::script('VBO_STICKYN_TITLE');
260 JText::script('VBO_STICKYN_TEXT');
261 JText::script('VBO_STICKYN_TEXT2');
262 JText::script('VBO_STICKYN_CUSTOMURI');
263 JText::script('VBO_WIDGETS_CONFRMELEM');
264 }
265
266 public function render(VBOMultitaskData $data = null)
267 {
268 // increase widget's instance counter
269 static::$instance_counter++;
270
271 // check whether the widget is being rendered via AJAX when adding it through the customizer
272 $is_ajax = $this->isAjaxRendering();
273
274 // check whether we are in the multitask panel
275 $is_multitask = $this->isMultitaskRendering();
276
277 // check whether the widget requires settings
278 $needs_settings = !$is_ajax;
279 $data_instance = !$is_ajax ? static::$instance_counter : '-1';
280 if ($is_multitask && $is_ajax) {
281 $guess_inst_counter = $this->guessMultitaskStickyInstance($data);
282 if ($guess_inst_counter !== false) {
283 // force the loading of the guessed widget instance's settings
284 static::$instance_counter = $guess_inst_counter;
285 $data_instance = $guess_inst_counter;
286 $needs_settings = true;
287 }
288 }
289
290 /**
291 * This widget requires the jQuery UI Sortable library, but we do not preload it
292 * in order to save resources. We only load this JS asset when the widget is saved.
293 * The page Dashboard always loads the Sortable library, but not the Multitask panel.
294 *
295 * @since 1.15.0 (J) - 1.5.0 (WP)
296 */
297 if (!$is_ajax) {
298 // load assets (with no "preloading")
299 JHtml::fetch('script', VBO_SITE_URI.'resources/jquery-ui.sortable.min.js');
300 }
301
302 // generate a unique ID for the sticky notes wrapper instance
303 $wrapper_instance = !$is_ajax ? static::$instance_counter : rand();
304 $wrapper_id = 'vbo-widget-sticky-' . $wrapper_instance;
305
306 // load the settings for this specific instance of the widget
307 $instance_settings = array();
308 if ($needs_settings && is_array($this->widgetSettings) && isset($this->widgetSettings[static::$instance_counter])) {
309 $instance_settings = is_array($this->widgetSettings[static::$instance_counter]) ? $this->widgetSettings[static::$instance_counter] : array();
310 }
311
312 ?>
313 <div class="vbo-admin-widget-wrapper">
314 <div class="vbo-admin-widget-head">
315 <h4><?php echo $this->widgetIcon; ?> <span><?php echo $this->widgetName; ?></span></h4>
316 <div class="btn-toolbar pull-right vbo-btn-toolbar-hastext">
317 <span class="vbo-sticky-shortcuts-help"<?php echo count($instance_settings) ? ' style="display: none;"' : ''; ?>>
318 <?php
319 echo $this->vbo_app->createPopover(array(
320 'title' => JText::translate('VBO_W_STICKYN_HELP_TITLE'),
321 'content' => JText::translate(($this->isMac ? 'VBO_W_STICKYN_HELP_DESCR_MAC' : 'VBO_W_STICKYN_HELP_DESCR')),
322 'icon_class' => VikBookingIcons::i('keyboard'),
323 'placement' => 'left'
324 ));
325 ?>
326 </span>
327 </div>
328 </div>
329 <div id="<?php echo $wrapper_id; ?>" class="vbo-admin-widget-sticky-notes-wrap" data-instance="<?php echo $data_instance; ?>">
330 <ul class="vbo-admin-widget-sticky-notes-list">
331 <?php
332 foreach ($instance_settings as $k => $sticky_note) {
333 ?>
334 <li class="vbo-sticky-note">
335 <div class="vbo-sticky-note-cmds">
336 <span class="vbo-sticky-note-cmd-drag"><?php VikBookingIcons::e('ellipsis-v'); ?></span>
337 <span class="vbo-sticky-note-cmd-trash" onclick="vboWidgetStickyNoteDelete(this);"><?php VikBookingIcons::e('trash'); ?></span>
338 </div>
339 <div contenteditable="true" spellcheck="false" class="vbo-widget-sticky-canvas">
340 <?php echo $sticky_note->html; ?>
341 </div>
342 <div class="vbo-sticky-note-sign">
343 <span class="vbo-sticky-note-sign-dt"><?php echo date(str_replace("/", $this->datesep, $this->df).' H:i', $sticky_note->ts); ?></span>
344 <?php
345 if (!empty($sticky_note->user)) {
346 ?>
347 <span class="vbo-sticky-note-sign-user"><?php echo $sticky_note->user; ?></span>
348 <?php
349 }
350 ?>
351 </div>
352 </li>
353 <?php
354 }
355 ?>
356 <li class="vbo-sticky-note-add">
357 <div class="vbo-sticky-note-add-inner" onclick="vboWidgetStickyNoteAdd(this);">
358 <span><?php VikBookingIcons::e('plus-circle'); ?></span>
359 </div>
360 </li>
361 </ul>
362 </div>
363 </div>
364
365 <script type="text/javascript">
366 // declare global variables for notes sorting
367 var vbo_stickynote_initial_pos = null;
368
369 jQuery(function() {
370
371 // register input event listener for each sticky note
372 var stickies<?php echo $wrapper_instance; ?> = document.querySelectorAll('#<?php echo $wrapper_id; ?> .vbo-widget-sticky-canvas');
373 for (var i = 0; i < stickies<?php echo $wrapper_instance; ?>.length; i++) {
374 stickies<?php echo $wrapper_instance; ?>[i].addEventListener('input', VBOCore.debounceEvent(vboWidgetStickyNoteUpdateTxt, 750));
375 }
376
377 if (typeof jQuery.fn.sortable !== 'undefined') {
378 /**
379 * Make all sticky notes sortable. Do not use .disableSelection() or this
380 * will break all [contenteditable] elements and their focus/selection events.
381 */
382 jQuery('#<?php echo $wrapper_id; ?> .vbo-admin-widget-sticky-notes-list').sortable({
383 cursor: 'move',
384 handle: '.vbo-sticky-note-cmd-drag',
385 items: 'li.vbo-sticky-note',
386 revert: false,
387 start: function(event, ui) {
388 // update sticky note initial position
389 vbo_stickynote_initial_pos = jQuery('#<?php echo $wrapper_id; ?>').find('.vbo-sticky-note').index(jQuery(ui.item));
390 },
391 update: function(event, ui) {
392 // get sticky note new position
393 var now_note = jQuery(ui.item);
394 var new_note_index = jQuery('#<?php echo $wrapper_id; ?>').find('.vbo-sticky-note').index(now_note);
395 var widget_instance = now_note.closest('.vbo-admin-widget-sticky-notes-wrap').attr('data-instance');
396
397 if (vbo_stickynote_initial_pos === null) {
398 return;
399 }
400
401 // the widget method to call
402 var call_method = 'sortStickyNote';
403
404 // make a silent request to remove the sticky note
405 VBOCore.doAjax(
406 "<?php echo $this->getExecWidgetAjaxUri(); ?>",
407 {
408 widget_id: "<?php echo $this->getIdentifier(); ?>",
409 call: call_method,
410 note_index_old: vbo_stickynote_initial_pos,
411 note_index_new: new_note_index,
412 note_instance: widget_instance,
413 tmpl: "component"
414 },
415 function(response) {
416 // unset global note position var
417 vbo_stickynote_initial_pos = null;
418 try {
419 var obj_res = typeof response === 'string' ? JSON.parse(response) : response;
420 if (!obj_res.hasOwnProperty(call_method)) {
421 console.error('Unexpected JSON response', obj_res);
422 }
423 } catch(err) {
424 console.error('could not parse JSON response', err, response);
425 }
426 },
427 function(error) {
428 // unset global note position var
429 vbo_stickynote_initial_pos = null;
430 console.error(error);
431 }
432 );
433 }
434 });
435 } else {
436 // hide sortable handler
437 jQuery('#<?php echo $wrapper_id; ?> .vbo-admin-widget-sticky-notes-list').find('.vbo-sticky-note-cmd-drag').hide();
438 }
439
440 });
441 </script>
442
443 <?php
444 if (static::$instance_counter === 0 || $is_ajax) {
445 ?>
446 <script type="text/javascript">
447 jQuery(function() {
448
449 /**
450 * Add event listener to keydown for shortcuts during typing on contenteditable elements.
451 */
452 document.onkeydown = function(e) {
453 e = e || window.event;
454 var active_el = document.activeElement;
455 var exec_cmd = null;
456 var exec_val = null;
457 if ((!e.metaKey && !e.ctrlKey) || !active_el || !active_el.hasAttribute('contenteditable')) {
458 return;
459 }
460
461 if (e.keyCode == 66) {
462 // CMD + B detected
463 exec_cmd = 'bold';
464 } else if (e.keyCode == 85) {
465 // CMD + U detected
466 exec_cmd = 'underline';
467 } else if (e.keyCode == 73) {
468 // CMD + I detected
469 exec_cmd = 'italic';
470 } else if (e.keyCode == 83) {
471 // CMD + S
472 exec_cmd = 'strikeThrough';
473 } else if (e.keyCode == 72 || e.keyCode == 84) {
474 // CMD + H || CMD + T detected
475 exec_cmd = 'formatBlock';
476 exec_val = 'h2';
477 } else if (e.keyCode == 80) {
478 // CMD + P
479 exec_cmd = 'formatBlock';
480 exec_val = 'p';
481 } else if (e.keyCode == 79 || e.keyCode == 78) {
482 // CMD + O || CMD + N detected
483 exec_cmd = 'insertOrderedList';
484 } else if (e.keyCode == 76) {
485 // CMD + L
486 if (window.getSelection()) {
487 var range = window.getSelection().getRangeAt(0);
488 exec_val = range.toString();
489 if (exec_val && exec_val.length) {
490 // some text is selected
491 if (exec_val.match(/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i)) {
492 // URI selected, set command to create a link
493 exec_cmd = 'createLink';
494 } else {
495 // just some plain text was selected, ask for a custom URI
496 e.preventDefault();
497 var link_uri = prompt(Joomla.JText._('VBO_STICKYN_CUSTOMURI'), '<?php echo JUri::root(); ?>');
498 if (link_uri != null && link_uri != '' && link_uri.indexOf('http') >= 0) {
499 exec_val = '<a href="' + link_uri + '">' + exec_val + '</a>';
500 exec_cmd = 'insertHTML';
501 }
502 }
503 } else {
504 // no text selected, create an unordered list
505 exec_val = null;
506 exec_cmd = 'insertUnorderedList';
507 }
508 } else {
509 // cannot access text selection, create an unordered list also in this case
510 exec_val = null;
511 exec_cmd = 'insertUnorderedList';
512 }
513 } else if (e.keyCode == 77 && window.getSelection()) {
514 // CMD + M
515 var txtsel = window.getSelection().getRangeAt(0).toString();
516 if (txtsel && txtsel.length && txtsel.indexOf('<') === 0 && txtsel.substr(-1, 1) == '>') {
517 // HTML tag selected, convert it from plain text to HTML code
518 if (txtsel.indexOf('><') >= 0) {
519 // hack for converting icons, where empty tags are not parsed correctly
520 txtsel = txtsel.replace('><', '> <');
521 }
522 exec_val = txtsel + ' ';
523 exec_cmd = 'insertHTML';
524 }
525 }
526
527 if (exec_cmd !== null) {
528 e.preventDefault();
529 document.execCommand(exec_cmd, false, exec_val);
530 active_el.focus();
531
532 return false;
533 }
534 }
535
536 /**
537 * Listen to mousedown event for clicks on links inside the elements
538 * with contenteditable, otherwise links in sticky notes won't open.
539 */
540 jQuery(document.body).on('mousedown', '.vbo-widget-sticky-canvas[contenteditable]', function(e) {
541 var elem = jQuery(e.target);
542 if (elem.is('a')) {
543 var goto = elem.attr('href');
544 if (goto && goto.length && goto.indexOf('http') >= 0) {
545 e.preventDefault();
546 window.open(goto, '_blank');
547 return false;
548 }
549 }
550 });
551 });
552 </script>
553
554 <script type="text/javascript">
555 function vboWidgetStickyNoteAdd(elem) {
556 // display the help for the shortcuts instructions
557 jQuery(elem).closest('.vbo-admin-widget-wrapper').find('.vbo-sticky-shortcuts-help').fadeIn();
558 // sticky note default HTML placeholder
559 var sticky_placeholder = '<h2>' + Joomla.JText._('VBO_STICKYN_TITLE') + '</h2>' + "\n";
560 sticky_placeholder += '<p>' + Joomla.JText._('VBO_STICKYN_TEXT') + '</p>' + "\n";
561 sticky_placeholder += '<p>' + Joomla.JText._('VBO_STICKYN_TEXT2') + '</p>' + "\n";
562 // build new sticky note HTML
563 var html_sticky_new = '<div class="vbo-sticky-note-cmds">';
564 html_sticky_new += ' <span class="vbo-sticky-note-cmd-drag"><?php VikBookingIcons::e('ellipsis-v'); ?></span>';
565 html_sticky_new += ' <span class="vbo-sticky-note-cmd-trash" onclick="vboWidgetStickyNoteDelete(this);"><?php VikBookingIcons::e('trash'); ?></span>';
566 html_sticky_new += '</div>';
567 html_sticky_new += '<div contenteditable="true" spellcheck="false" class="vbo-widget-sticky-canvas">';
568 html_sticky_new += sticky_placeholder;
569 html_sticky_new += '</div>';
570
571 // build new element and add HTML to it
572 var sticky_new = document.createElement('li');
573 sticky_new.setAttribute('class', 'vbo-sticky-note');
574 sticky_new.innerHTML = html_sticky_new;
575
576 // attach listener for input event
577 sticky_new.addEventListener('input', VBOCore.debounceEvent(vboWidgetStickyNoteUpdateTxt, 750));
578
579 // add new element to the document, before the button to add new sticky notes
580 var append_to = jQuery(elem).closest('.vbo-admin-widget-sticky-notes-list').find('.vbo-sticky-note-add');
581 append_to.before(jQuery(sticky_new));
582
583 /**
584 * The input event is immediately triggered so that the newly added note will be saved to avoid
585 * problems when like adding two notes before even typing some text, and then moving/removing them.
586 */
587 sticky_new.dispatchEvent(new Event('input'));
588 }
589
590 function vboWidgetStickyNoteDelete(elem) {
591 var note_elem = jQuery(elem);
592 var note_index = note_elem.closest('.vbo-admin-widget-sticky-notes-list').find('.vbo-sticky-note').index(note_elem.closest('.vbo-sticky-note'));
593 var widget_instance = note_elem.closest('.vbo-admin-widget-sticky-notes-wrap').attr('data-instance');
594
595 var confirm_lbl = Joomla.JText._('VBO_WIDGETS_CONFRMELEM');
596 confirm_lbl = confirm_lbl.length ? confirm_lbl : 'Continue?';
597 if (confirm(confirm_lbl)) {
598 // the widget method to call
599 var call_method = 'deleteStickyNote';
600
601 // make a silent request to remove the sticky note
602 VBOCore.doAjax(
603 "<?php echo $this->getExecWidgetAjaxUri(); ?>",
604 {
605 widget_id: "<?php echo $this->getIdentifier(); ?>",
606 call: call_method,
607 note_index: note_index,
608 note_instance: widget_instance,
609 tmpl: "component"
610 },
611 function(response) {
612 try {
613 var obj_res = typeof response === 'string' ? JSON.parse(response) : response;
614 if (!obj_res.hasOwnProperty(call_method)) {
615 console.error('Unexpected JSON response', obj_res);
616 }
617 } catch(err) {
618 console.error('could not parse JSON response', err, response);
619 }
620 },
621 function(error) {
622 console.error(error);
623 }
624 );
625
626 // remove the sticky note from the document
627 note_elem.closest('li.vbo-sticky-note').remove();
628 }
629 }
630
631 function vboWidgetStickyNoteUpdateTxt(event) {
632 var note_elem = jQuery(this);
633 // display the help for the shortcuts instructions
634 note_elem.closest('.vbo-admin-widget-wrapper').find('.vbo-sticky-shortcuts-help').fadeIn();
635 // element "this" may be different depending on how the event was triggered
636 if (note_elem.find('.vbo-widget-sticky-canvas').length) {
637 var note_txt = note_elem.find('.vbo-widget-sticky-canvas').html();
638 } else {
639 var note_txt = note_elem.html();
640 }
641 var note_index = note_elem.closest('.vbo-admin-widget-sticky-notes-list').find('.vbo-sticky-note').index(note_elem.closest('.vbo-sticky-note'));
642 var widget_instance = note_elem.closest('.vbo-admin-widget-sticky-notes-wrap').attr('data-instance');
643
644 // the widget method to call
645 var call_method = 'updateStickyNote';
646
647 // make a silent request to update the sticky note details
648 VBOCore.doAjax(
649 "<?php echo $this->getExecWidgetAjaxUri(); ?>",
650 {
651 widget_id: "<?php echo $this->getIdentifier(); ?>",
652 call: call_method,
653 note_txt: note_txt,
654 note_index: note_index,
655 note_instance: widget_instance,
656 tmpl: "component"
657 },
658 function(response) {
659 try {
660 var obj_res = typeof response === 'string' ? JSON.parse(response) : response;
661 if (!obj_res.hasOwnProperty(call_method)) {
662 console.error('Unexpected JSON response', obj_res);
663 } else {
664 // response for the method updateStickyNote() may contain the new instance given to the widget
665 if (widget_instance < 0 && obj_res[call_method].indexOf('[instance=') >= 0) {
666 // extract the new instance assigned
667 var resp_left = obj_res[call_method].split('[instance=');
668 var widget_instance_new = resp_left[1].split(']')[0];
669 if (widget_instance_new && widget_instance_new.length) {
670 // update widget's instance
671 note_elem.closest('.vbo-admin-widget-sticky-notes-wrap').attr('data-instance', widget_instance_new);
672 }
673 }
674 }
675 } catch(err) {
676 console.error('could not parse JSON response', err, response);
677 }
678 },
679 function(error) {
680 console.error(error);
681 }
682 );
683 }
684 </script>
685 <?php
686 }
687 }
688
689 /**
690 * Protected method to guess if the widget being rendered via AJAX in the
691 * multitask panel should load specific settings. Should be called when
692 * settings have been loaded, and if AJAX + multitask rendering is detected.
693 * It's assumed that the AJAX rendering of a multitask widget takes place one
694 * second before the AJAX event that updates the multitask map to push the widget.
695 *
696 * @param VBOMultitaskData $data the data object injected to the widget.
697 *
698 * @return bool|int false on failure, guessed settings index otherwise.
699 */
700 protected function guessMultitaskStickyInstance(VBOMultitaskData $data = null)
701 {
702 if (!is_array($this->widgetSettings) || !count($this->widgetSettings)) {
703 // nothing to guess if this widget has got no saved settings
704 return false;
705 }
706
707 if (!is_object($data)) {
708 // multitask data object must be set
709 return false;
710 }
711
712 // the page must be set in the multitask object
713 $vbo_page = $data->getPage();
714 if (empty($vbo_page)) {
715 // nothing to guess if no current page set
716 return false;
717 }
718
719 // get the map for the current page
720 $page_map = VikBooking::getAdminWidgetsInstance()->getMultitaskingMap($vbo_page, $whole = false);
721 if (!is_array($page_map) || !count($page_map)) {
722 // the multitask panel of this page has got no widgets saved, return the first index for settings
723 return 0;
724 }
725
726 // count how many widgets of this type are already on this page
727 $guessed_index = 0;
728 foreach ($page_map as $widget_type) {
729 if ($widget_type == $this->getIdentifier()) {
730 $guessed_index++;
731 }
732 }
733
734 // return the guessed index, which will load the next hypothetical instance
735 return $guessed_index;
736 }
737 }
738