| Conditions | 2 |
| Paths | 2 |
| Total Lines | 120 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 69 | private function getElementJS(): string |
||
| 70 | { |
||
| 71 | $localeCode = $this->getLocaleCode(); |
||
| 72 | $id = $this->getAttribute('id'); |
||
| 73 | |||
| 74 | $altFormat = ($localeCode === 'en') ? 'F d, Y - H:i' : 'd F, Y - H:i'; |
||
| 75 | |||
| 76 | $js = "<script> |
||
| 77 | document.addEventListener('DOMContentLoaded', function () { |
||
| 78 | var input = document.getElementById('{$id}'); |
||
| 79 | if (!input) return; |
||
| 80 | |||
| 81 | function cap(s){ return s ? s.charAt(0).toUpperCase() + s.slice(1) : s; } |
||
| 82 | |||
| 83 | // Build a flatpickr locale object using Intl (no external l10n files) |
||
| 84 | function buildFlatpickrLocale(loc) { |
||
| 85 | try { |
||
| 86 | var fmtWeekLong = new Intl.DateTimeFormat(loc, { weekday: 'long' }); |
||
| 87 | var fmtWeekShort = new Intl.DateTimeFormat(loc, { weekday: 'short' }); |
||
| 88 | var fmtMonthLong = new Intl.DateTimeFormat(loc, { month: 'long' }); |
||
| 89 | var fmtMonthShort= new Intl.DateTimeFormat(loc, { month: 'short' }); |
||
| 90 | |||
| 91 | // Weekdays: flatpickr expects 0..6 starting on Sunday |
||
| 92 | var sun = new Date(Date.UTC(2020, 0, 5)); // a Sunday |
||
| 93 | var weekdaysLong = [], weekdaysShort = []; |
||
| 94 | for (var i=0;i<7;i++){ |
||
| 95 | var d = new Date(sun); d.setUTCDate(sun.getUTCDate()+i); |
||
| 96 | weekdaysLong.push(cap(fmtWeekLong.format(d))); |
||
| 97 | weekdaysShort.push(cap(fmtWeekShort.format(d))); |
||
| 98 | } |
||
| 99 | |||
| 100 | // Months 0..11 |
||
| 101 | var monthsLong = [], monthsShort = []; |
||
| 102 | for (var m=0;m<12;m++){ |
||
| 103 | var dm = new Date(Date.UTC(2020, m, 1)); |
||
| 104 | monthsLong.push(cap(fmtMonthLong.format(dm))); |
||
| 105 | monthsShort.push(cap(fmtMonthShort.format(dm))); |
||
| 106 | } |
||
| 107 | |||
| 108 | // First day of week (fallback to Monday if not available) |
||
| 109 | var firstDay = 1; // 0=Sun, 1=Mon |
||
| 110 | try { |
||
| 111 | if (window.Intl && Intl.Locale) { |
||
| 112 | var inf = new Intl.Locale(loc); |
||
| 113 | if (inf.weekInfo && inf.weekInfo.firstDay) { |
||
| 114 | firstDay = (inf.weekInfo.firstDay === 7) ? 0 : inf.weekInfo.firstDay; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } catch(e){} |
||
| 118 | |||
| 119 | return { |
||
| 120 | weekdays: { shorthand: weekdaysShort, longhand: weekdaysLong }, |
||
| 121 | months: { shorthand: monthsShort, longhand: monthsLong }, |
||
| 122 | firstDayOfWeek: firstDay, |
||
| 123 | weekAbbreviation: 'Wk', |
||
| 124 | rangeSeparator: ' – ', |
||
| 125 | time_24hr: true |
||
| 126 | }; |
||
| 127 | } catch(e) { |
||
| 128 | return 'en'; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | function initialize() { |
||
| 133 | try { |
||
| 134 | if (!window.flatpickr) return; |
||
| 135 | if (input._flatpickr) { input._flatpickr.destroy(); } |
||
| 136 | |||
| 137 | var loc = buildFlatpickrLocale('{$localeCode}'); |
||
| 138 | |||
| 139 | // Set as global default when possible |
||
| 140 | if (typeof flatpickr.localize === 'function' && typeof loc === 'object') { |
||
| 141 | flatpickr.localize(loc); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Also register under the key for string-based locale usage |
||
| 145 | if (flatpickr.l10ns && typeof loc === 'object') { |
||
| 146 | flatpickr.l10ns['{$localeCode}'] = loc; |
||
| 147 | } |
||
| 148 | |||
| 149 | var instance = flatpickr('#{$id}', { |
||
| 150 | locale: loc, |
||
| 151 | altInput: true, |
||
| 152 | altFormat: '{$altFormat}', |
||
| 153 | enableTime: true, |
||
| 154 | dateFormat: 'Y-m-d H:i', |
||
| 155 | time_24hr: true, |
||
| 156 | wrap: false, |
||
| 157 | onReady: function(selectedDates, dateStr, fp) { |
||
| 158 | const btn = document.createElement('button'); |
||
| 159 | btn.textContent = '".get_lang('Validate')."'; |
||
| 160 | btn.className = 'flatpickr-validate-btn'; |
||
| 161 | btn.type = 'button'; |
||
| 162 | btn.onclick = function(){ fp.close(); }; |
||
| 163 | fp.calendarContainer.appendChild(btn); |
||
| 164 | |||
| 165 | try { fp.redraw(); } catch(e){} |
||
| 166 | } |
||
| 167 | }); |
||
| 168 | |||
| 169 | // Ensure l10n is applied and redraw if needed |
||
| 170 | try { |
||
| 171 | if (instance && instance.l10n && typeof loc === 'object') { |
||
| 172 | Object.assign(instance.l10n, loc); |
||
| 173 | instance.redraw(); |
||
| 174 | } |
||
| 175 | } catch(e){} |
||
| 176 | |||
| 177 | // Debug (optional): |
||
| 178 | // console.log('[DateTimePicker]', '{$localeCode}', instance && instance.l10n); |
||
| 179 | } catch(e) { |
||
| 180 | console.error('[DateTimePicker] flatpickr init error', e); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | initialize(); |
||
| 185 | }); |
||
| 186 | </script>"; |
||
| 187 | |||
| 188 | return $js; |
||
| 189 | } |
||
| 262 |