Skip to content

Commit b959f44

Browse files
committed
include daterangepicker patch to fix a cosmetic bug in the highlighting.
This commit can be spirited away at some point if/when the patch is merged upstream. Signed-off-by: Adam Warner <[email protected]>
1 parent 8ff56a4 commit b959f44

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

queries.lp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ mg.include('scripts/lua/header_authenticated.lp','r')
197197

198198
<script src="<?=pihole.fileversion('vendor/bootstrap-select/bootstrap-select.min.js')?>"></script>
199199
<script src="<?=pihole.fileversion('vendor/daterangepicker/daterangepicker.min.js')?>"></script>
200+
<script src="<?=pihole.fileversion('vendor/daterangepicker/daterangepicker-patches.js')?>"></script>
200201
<script src="<?=pihole.fileversion('scripts/js/queries.js')?>"></script>
201202
<script src="<?=pihole.fileversion('scripts/js/ip-address-sorting.js')?>"></script>
202203

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Pi-hole patches for daterangepicker
3+
*
4+
* This file contains patches applied to the daterangepicker library
5+
* to make it compatible with Pi-hole's requirements.
6+
*
7+
* Load this file AFTER daterangepicker.min.js
8+
*
9+
* Patches:
10+
* - Fixed calculateChosenLabel() chained equality bug that caused "Custom Range"
11+
* to always be highlighted even when a predefined range was selected.
12+
*
13+
* Upstream PR: https://github.com/Wernfried/daterangepicker/pull/1
14+
*/
15+
16+
(function() {
17+
'use strict';
18+
19+
// Wait for jQuery and daterangepicker to be available
20+
if (typeof jQuery === 'undefined' || !jQuery.fn.daterangepicker) {
21+
console.error('Pi-hole daterangepicker patches: jQuery or daterangepicker not loaded');
22+
return;
23+
}
24+
25+
// Patch the calculateChosenLabel method in the DateRangePicker constructor's prototype
26+
// We need to intercept during initialization to patch the instance
27+
const originalDateRangePicker = jQuery.fn.daterangepicker;
28+
29+
jQuery.fn.daterangepicker = function(options, callback) {
30+
// Call the original constructor
31+
const result = originalDateRangePicker.call(this, options, callback);
32+
33+
// Get the daterangepicker instance
34+
const picker = this.data('daterangepicker');
35+
36+
if (picker && picker.calculateChosenLabel) {
37+
38+
// Replace with patched version
39+
picker.calculateChosenLabel = function() {
40+
// If selected range from calendar matches any custom range, then highlight it
41+
var customRange = true;
42+
var i = 0;
43+
for (var range in this.ranges) {
44+
var unit = this.timePicker ? 'hour' : 'day';
45+
if (this.timePicker) {
46+
if (this.timePickerOpts.showMinutes) {
47+
unit = 'minute';
48+
} else if (this.timePickerOpts.showSeconds) {
49+
unit = 'second';
50+
}
51+
}
52+
// PATCHED: Use .equals() and && instead of chained ==
53+
if (this.startDate.startOf(unit).equals(this.ranges[range][0].startOf(unit)) &&
54+
this.endDate.startOf(unit).equals(this.ranges[range][1].startOf(unit))) {
55+
// END PATCH
56+
customRange = false;
57+
this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key');
58+
break;
59+
}
60+
i++;
61+
}
62+
if (customRange) {
63+
if (this.showCustomRangeLabel) {
64+
this.chosenLabel = this.container.find('.ranges li:last').addClass('active').attr('data-range-key');
65+
} else {
66+
this.chosenLabel = null;
67+
}
68+
this.showCalendars();
69+
}
70+
};
71+
}
72+
73+
return result;
74+
};
75+
76+
// Copy over any properties from the original function
77+
jQuery.fn.daterangepicker.defaultOptions = originalDateRangePicker.defaultOptions;
78+
79+
console.log('Pi-hole daterangepicker patches applied');
80+
81+
})();

0 commit comments

Comments
 (0)