|
| 1 | +{# |
| 2 | + # --------------------------------------------------------------------- |
| 3 | + # |
| 4 | + # GLPI - Gestionnaire Libre de Parc Informatique |
| 5 | + # |
| 6 | + # http://glpi-project.org |
| 7 | + # |
| 8 | + # @copyright 2015-2023 Teclib' and contributors. |
| 9 | + # @copyright 2003-2014 by the INDEPNET Development Team. |
| 10 | + # @licence https://www.gnu.org/licenses/gpl-3.0.html |
| 11 | + # |
| 12 | + # --------------------------------------------------------------------- |
| 13 | + # |
| 14 | + # LICENSE |
| 15 | + # |
| 16 | + # This file is part of GLPI. |
| 17 | + # |
| 18 | + # This program is free software: you can redistribute it and/or modify |
| 19 | + # it under the terms of the GNU General Public License as published by |
| 20 | + # the Free Software Foundation, either version 3 of the License, or |
| 21 | + # (at your option) any later version. |
| 22 | + # |
| 23 | + # This program is distributed in the hope that it will be useful, |
| 24 | + # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | + # GNU General Public License for more details. |
| 27 | + # |
| 28 | + # You should have received a copy of the GNU General Public License |
| 29 | + # along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 30 | + # |
| 31 | + # --------------------------------------------------------------------- |
| 32 | + #} |
| 33 | + |
| 34 | +{% import 'components/form/fields_macros.html.twig' as fields %} |
| 35 | + |
| 36 | +{% set options = { |
| 37 | + 'rand': random() |
| 38 | +} %} |
| 39 | + |
| 40 | +{% if not timerange_presets %} |
| 41 | + {% set timerange_presets = { |
| 42 | + 'P1D': __('Last day'), |
| 43 | + 'P1W': __('Last 7 days'), |
| 44 | + 'P1M': __('Last 30 days'), |
| 45 | + 'P3M': __('Last quarter'), |
| 46 | + 'P1Y': __('Last year'), |
| 47 | + 'P1000Y': __('All time'), |
| 48 | + } %} |
| 49 | +{% endif %} |
| 50 | + |
| 51 | +{% if not format_presets %} |
| 52 | + {% set format_presets = { |
| 53 | + 'dynamic': __('Dynamic distribution'), |
| 54 | + 'daily': __('Daily'), |
| 55 | + 'weekly': __('Weekly'), |
| 56 | + 'monthly': __('Monthly'), |
| 57 | + 'yearly': __('Yearly'), |
| 58 | + } %} |
| 59 | +{% endif %} |
| 60 | + |
| 61 | +<style> |
| 62 | + .rotate-45 { |
| 63 | + transform: rotate(45deg); |
| 64 | + transition: transform 0.2s ease-in-out; |
| 65 | + } |
| 66 | + .rotate-90 { |
| 67 | + transform: rotate(90deg); |
| 68 | + transition: transform 0.2s ease-in-out; |
| 69 | + } |
| 70 | +</style> |
| 71 | + |
| 72 | +<div class="d-flex gap-2 w-full"> |
| 73 | + <div id="select_range_dropdown" class="dropdown"> |
| 74 | + <button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> |
| 75 | + <i class="ti ti-calendar me-2"></i> |
| 76 | + {{ start_date and end_date ? __('Custom range') ~ ': ' ~ start_date|split('T')[0] ~ ' - ' ~ end_date|split('T')[0] : timerange_presets[interval] }} |
| 77 | + </button> |
| 78 | + <ul class="dropdown-menu"> |
| 79 | + {% for key, value in timerange_presets %} |
| 80 | + <li> |
| 81 | + <span class="dropdown-item {{ interval == key and (not start_date or not end_date) ? 'active' }}" href="#" data-key="{{ key }}" onclick="update_date_preset_{{ options['rand'] }}(this)">{{ value }}</span> |
| 82 | + </li> |
| 83 | + {% endfor %} |
| 84 | + <li><hr class="dropdown-divider"></li> |
| 85 | + <li id="show_custom_range" class="d-flex align-items-center"> |
| 86 | + <span class="dropdown-item {{ start_date and end_date ? 'active' }}" href="#"> |
| 87 | + <i class="ti ti-plus {{ start_date and end_date ? 'rotate-45' : 'rotate-90' }}"></i> |
| 88 | + {{ __('Custom range') }} |
| 89 | + </span> |
| 90 | + </li> |
| 91 | + <li id="date_range_input" class="px-2 {{ not start_date or not end_date ? 'd-none' }}" style="width:11rem"> |
| 92 | + {{ fields.dateField('range_date', '', '', options|merge({ |
| 93 | + 'no_label': true, |
| 94 | + 'full_width': true, |
| 95 | + 'mb': 'my-2', |
| 96 | + })) }} |
| 97 | + <button class="btn btn-primary w-full mb-1" type="button" onclick="update_custom_date_range_{{ options['rand'] }}()">{{ __('Apply') }}</button> |
| 98 | + </li> |
| 99 | + </ul> |
| 100 | + </div> |
| 101 | + <div id="select_format_dropdown" class="dropdown"> |
| 102 | + <button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> |
| 103 | + <i class="ti ti-chart-dots-3 me-2"></i> |
| 104 | + {{ format_presets[format] }} |
| 105 | + </button> |
| 106 | + <ul class="dropdown-menu"> |
| 107 | + {% for key, value in format_presets %} |
| 108 | + <li> |
| 109 | + <span class="dropdown-item {{ format_presets[format] == value ? 'active' }}" href="#" data-key="{{ key }}" onclick="update_format_{{ options['rand'] }}(this)">{{ value }}</span> |
| 110 | + </li> |
| 111 | + {% endfor %} |
| 112 | + </ul> |
| 113 | + </div> |
| 114 | +</div> |
| 115 | + |
| 116 | +<script> |
| 117 | + var date_range_data = { |
| 118 | + 'format': '{{ format }}', |
| 119 | + 'interval': '{{ interval }}', |
| 120 | + 'start_date': '{{ start_date }}', |
| 121 | + 'end_date': '{{ end_date }}', |
| 122 | + } |
| 123 | +
|
| 124 | + function reloadTab_{{ options['rand'] }}() { |
| 125 | + params = 'date_format=' + date_range_data['format']; |
| 126 | +
|
| 127 | + if(date_range_data['start_date'] && date_range_data['end_date']) { |
| 128 | + params += '&date_start=' + date_range_data['start_date'] + '&date_end=' + date_range_data['end_date']; |
| 129 | + } else { |
| 130 | + params += '&date_interval=' + date_range_data['interval']; |
| 131 | + } |
| 132 | +
|
| 133 | + reloadTab(params); |
| 134 | + } |
| 135 | +
|
| 136 | + function update_date_preset_{{ options['rand'] }}(element) { |
| 137 | + if (!$(element).hasClass('active')) { |
| 138 | + date_range_data['interval'] = element.dataset.key; |
| 139 | + date_range_data['start_date'] = null; |
| 140 | + date_range_data['end_date'] = null; |
| 141 | +
|
| 142 | + $('#select_range_dropdown button.dropdown-toggle').html(element.innerHTML); |
| 143 | + reloadTab_{{ options['rand'] }}(); |
| 144 | + } |
| 145 | + } |
| 146 | + function update_format_{{ options['rand'] }}(element) { |
| 147 | + date_range_data['format'] = element.dataset.key; |
| 148 | +
|
| 149 | + $('#select_format_dropdown button.dropdown-toggle').html(element.innerHTML); |
| 150 | + reloadTab_{{ options['rand'] }}(); |
| 151 | + } |
| 152 | + function update_custom_date_range_{{ options['rand'] }}() { |
| 153 | + let range_date = $('#range-date_{{ options['rand'] }}')[0]; |
| 154 | + let start_date = range_date._flatpickr.selectedDates[0]; |
| 155 | + let end_date = range_date._flatpickr.selectedDates[1]; |
| 156 | +
|
| 157 | + if (start_date && end_date) { |
| 158 | + // Igore timezone offset |
| 159 | + date_range_data['start_date'] = new Date(start_date.getTime() - (start_date.getTimezoneOffset() * 60000)).toISOString(); |
| 160 | + date_range_data['end_date'] = new Date(end_date.getTime() - (end_date.getTimezoneOffset() * 60000)).toISOString(); |
| 161 | + date_range_data['interval'] = null; |
| 162 | +
|
| 163 | + let label = __('Custom Range') + ': '; |
| 164 | + label += date_range_data['start_date'].split('T')[0]; |
| 165 | + label += ' - ' + date_range_data['end_date'].split('T')[0]; |
| 166 | + $('#select_range_dropdown button.dropdown-toggle').html(label); |
| 167 | + reloadTab_{{ options['rand'] }}(); |
| 168 | + } |
| 169 | + } |
| 170 | +
|
| 171 | + $(document).ready(function () { |
| 172 | + let range_date = $('#range-date_{{ options['rand'] }}')[0]; |
| 173 | + let dropdownInst = new bootstrap.Dropdown($('#select_range_dropdown button.dropdown-toggle')); |
| 174 | +
|
| 175 | + $('#show_custom_range').click(function () { |
| 176 | + event.stopPropagation(); |
| 177 | + $('#date_range_input').toggleClass('d-none'); |
| 178 | +
|
| 179 | + if ($('#show_custom_range span i.ti-plus').hasClass('rotate-45')) { |
| 180 | + $('#show_custom_range span i.ti-plus').addClass('rotate-90'); |
| 181 | + $('#show_custom_range span i.ti-plus').removeClass('rotate-45'); |
| 182 | + } else { |
| 183 | + $('#show_custom_range span i.ti-plus').removeClass('rotate-90'); |
| 184 | + $('#show_custom_range span i.ti-plus').addClass('rotate-45'); |
| 185 | + } |
| 186 | + }); |
| 187 | +
|
| 188 | + range_date._flatpickr.config.mode = 'range'; |
| 189 | + range_date._flatpickr.config.onOpen.push(function () { |
| 190 | + dropdownInst._config.autoClose = false |
| 191 | + }); |
| 192 | + range_date._flatpickr.config.onClose.push(function () { |
| 193 | + dropdownInst._config.autoClose = true |
| 194 | + }); |
| 195 | +
|
| 196 | + {% if start_date and end_date %} |
| 197 | + range_date._flatpickr.setDate([new Date('{{ start_date }}'), new Date('{{ end_date }}')]); |
| 198 | + {% endif %} |
| 199 | + }); |
| 200 | +</script> |
0 commit comments