script.php
108 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Layout variables |
| 16 | * ----------------- |
| 17 | * @param string suffix An optional suffix to support different configurations. |
| 18 | */ |
| 19 | extract($displayData); |
| 20 | |
| 21 | $suffix = isset($suffix) ? $suffix : ''; |
| 22 | |
| 23 | ?> |
| 24 | |
| 25 | <script> |
| 26 | |
| 27 | jQuery(function($) { |
| 28 | // handle main configuration nav buttons |
| 29 | $('.vaptabli').on('click', function() { |
| 30 | if ($(this).hasClass('vapconfigtabactive')) { |
| 31 | // pane already selected |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | $('.vaptabli').removeClass('vapconfigtabactive'); |
| 36 | $(this).addClass('vapconfigtabactive'); |
| 37 | |
| 38 | let tab = $(this).data('id'); |
| 39 | |
| 40 | $('.vaptabview').hide(); |
| 41 | $('#vaptabview' + tab).show(); |
| 42 | |
| 43 | /** |
| 44 | * Store active tab in a cookie and keep it there until the session expires. |
| 45 | * |
| 46 | * @since 1.7 |
| 47 | */ |
| 48 | document.cookie = 'vikappointments.config<?php echo $suffix; ?>.tab=' + tab + '; path=/'; |
| 49 | }); |
| 50 | |
| 51 | // create lambda to register the selected tab within a cookie |
| 52 | const cacheActiveTab = (pane, tab) => { |
| 53 | let paneId = $(pane).attr('id').replace(/^vaptabview/, ''); |
| 54 | let tabId = $(tab).data('id'); |
| 55 | |
| 56 | document.cookie = 'vikappointments.config<?php echo $suffix; ?>.tab' + paneId + '=' + tabId + '; path=/'; |
| 57 | }; |
| 58 | |
| 59 | // handle configuration panel nav buttons |
| 60 | $('.vaptabview .config-panel-subnav li').on('click', function() { |
| 61 | if ($(this).hasClass('active')) { |
| 62 | // pane already selected |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // back to parent tab |
| 67 | const pane = $(this).closest('.vaptabview'); |
| 68 | |
| 69 | pane.find('.config-panel-subnav li').removeClass('active'); |
| 70 | pane.find('.config-panel-tabview-inner').hide(); |
| 71 | |
| 72 | $(this).addClass('active'); |
| 73 | pane.find('.config-panel-tabview-inner') |
| 74 | .filter('[data-id="' + $(this).data('id') + '"]') |
| 75 | .show(); |
| 76 | |
| 77 | cacheActiveTab(pane, this); |
| 78 | }); |
| 79 | |
| 80 | // check if the URL requested a specific setting |
| 81 | if (document.location.hash) { |
| 82 | // get setting input (starts with the specified hash) |
| 83 | const input = $('*[name^="' + document.location.hash.replace(/^#/, '') + '"]').first(); |
| 84 | |
| 85 | // extract fieldset ID |
| 86 | const idFieldset = input.closest('.config-panel-tabview-inner[data-id]').data('id'); |
| 87 | |
| 88 | // find tab view to which the input belong |
| 89 | const tabView = input.closest('.vaptabview'); |
| 90 | |
| 91 | // extract tabView index from ID |
| 92 | const matches = tabView.attr('id').match(/^vaptabview(\d+)$/); |
| 93 | |
| 94 | if (matches && matches.length) { |
| 95 | // activate the tab view of the input |
| 96 | $('.vaptabli[data-id="' + matches[1] + '"]').trigger('click'); |
| 97 | // active the inner fieldset |
| 98 | $('.config-panel-subnav li[data-id="' + idFieldset + '"]').trigger('click'); |
| 99 | // set the focus to the input |
| 100 | $(input).focus(); |
| 101 | // animate to the input position |
| 102 | $('html, body').animate({ scrollTop: input.offset().top - 200 }); |
| 103 | } |
| 104 | } |
| 105 | }); |
| 106 | |
| 107 | </script> |
| 108 |