|
| 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