diff --git a/app/assets/javascripts/discourse/app/lib/public-js-versions.js b/app/assets/javascripts/discourse/app/lib/public-js-versions.js index a8ced62efaa..f807c36c3f4 100644 --- a/app/assets/javascripts/discourse/app/lib/public-js-versions.js +++ b/app/assets/javascripts/discourse/app/lib/public-js-versions.js @@ -10,5 +10,5 @@ export const PUBLIC_JS_VERSIONS = { "diffhtml.min.js": "diffhtml/1.0.0-beta.20/diffhtml.min.js", "jquery.magnific-popup.min.js": "magnific-popup/1.1.0/jquery.magnific-popup.min.js", - "pikaday.js": "pikaday/1.8.0/pikaday.js", + "pikaday.js": "pikaday/1.8.2/pikaday.js", }; diff --git a/package.json b/package.json index 7ca44b41886..8366ebe15b9 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "magnific-popup": "1.1.0", "moment": "2.29.4", "moment-timezone": "0.5.39", - "pikaday": "1.8.0", + "pikaday": "1.8.2", "squoosh": "discourse/squoosh#dc9649d", "workbox-cacheable-response": "^4.3.1", "workbox-core": "^4.3.1", diff --git a/public/javascripts/pikaday/1.8.0/pikaday.js b/public/javascripts/pikaday/1.8.2/pikaday.js similarity index 93% rename from public/javascripts/pikaday/1.8.0/pikaday.js rename to public/javascripts/pikaday/1.8.2/pikaday.js index 8dae8382cde..61642454d18 100755 --- a/public/javascripts/pikaday/1.8.0/pikaday.js +++ b/public/javascripts/pikaday/1.8.2/pikaday.js @@ -99,8 +99,8 @@ isLeapYear = function(year) { - // solution by Matti Virkkunen: http://stackoverflow.com/a/4881951 - return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0; + // solution lifted from date.js (MIT license): https://github.com/datejs/Datejs + return ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0); }, getDaysInMonth = function(year, month) @@ -183,7 +183,7 @@ // automatically show/hide the picker on `field` focus (default `true` if `field` is set) bound: undefined, - // data-attribute on the input field with an aria assistance tekst (only applied when `bound` is set) + // data-attribute on the input field with an aria assistance text (only applied when `bound` is set) ariaLabel: 'Use the arrow keys to pick a date', // position of the datepicker, relative to the field (default to bottom & left) @@ -212,6 +212,10 @@ // first day of week (0: Sunday, 1: Monday etc) firstDay: 0, + // minimum number of days in the week that gets week number one + // default ISO 8601, week 01 is the week with the first Thursday (4) + firstWeekOfYearMinDays: 4, + // the default flag for moment's strict date parsing formatStrict: false, @@ -349,11 +353,35 @@ ''; }, - renderWeek = function (d, m, y) { - // Lifted from http://javascript.about.com/library/blweekyear.htm, lightly modified. - var onejan = new Date(y, 0, 1), - weekNum = Math.ceil((((new Date(y, m, d) - onejan) / 86400000) + onejan.getDay()+1)/7); - return '' + weekNum + ''; + isoWeek = function(date, firstWeekOfYearMinDays) { + // Ensure we're at the start of the day. + date.setHours(0, 0, 0, 0); + + // Thursday in current week decides the year because January 4th + // is always in the first week according to ISO8601. + var yearDay = date.getDate(), + weekDay = date.getDay(), + dayInFirstWeek = firstWeekOfYearMinDays, + dayShift = dayInFirstWeek - 1, // counting starts at 0 + daysPerWeek = 7, + prevWeekDay = function(day) { return (day + daysPerWeek - 1) % daysPerWeek; }; + + // Adjust to Thursday in week 1 and count number of weeks from date to week 1. + date.setDate(yearDay + dayShift - prevWeekDay(weekDay)); + + var jan4th = new Date(date.getFullYear(), 0, dayInFirstWeek), + msPerDay = 24 * 60 * 60 * 1000, + daysBetween = (date.getTime() - jan4th.getTime()) / msPerDay, + weekNum = 1 + Math.round((daysBetween - dayShift + prevWeekDay(jan4th.getDay())) / daysPerWeek); + + return weekNum; + }, + + renderWeek = function (d, m, y, firstWeekOfYearMinDays) { + var date = new Date(y, m, d), + week = hasMoment ? moment(date).isoWeek() : isoWeek(date, firstWeekOfYearMinDays); + + return '' + week + ''; }, renderRow = function(days, isRTL, pickWholeWeek, isRowSelected) @@ -393,7 +421,7 @@ for (arr = [], i = 0; i < 12; i++) { arr.push(''); } @@ -524,7 +552,6 @@ } break; case 37: - e.preventDefault(); self.adjustDate('subtract', 1); break; case 38: @@ -536,10 +563,26 @@ case 40: self.adjustDate('add', 7); break; + case 8: + case 46: + self.setDate(null); + break; } } }; + self._parseFieldValue = function() + { + if (opts.parse) { + return opts.parse(opts.field.value, opts.format); + } else if (hasMoment) { + var date = moment(opts.field.value, opts.format, opts.formatStrict); + return (date && date.isValid()) ? date.toDate() : null; + } else { + return new Date(Date.parse(opts.field.value)); + } + }; + self._onInputChange = function(e) { var date; @@ -547,15 +590,7 @@ if (e.firedBy === self) { return; } - if (opts.parse) { - date = opts.parse(opts.field.value, opts.format); - } else if (hasMoment) { - date = moment(opts.field.value, opts.format, opts.formatStrict); - date = (date && date.isValid()) ? date.toDate() : null; - } - else { - date = new Date(Date.parse(opts.field.value)); - } + date = self._parseFieldValue(); if (isDate(date)) { self.setDate(date); } @@ -640,11 +675,7 @@ addEvent(opts.field, 'change', self._onInputChange); if (!opts.defaultDate) { - if (hasMoment && opts.field.value) { - opts.defaultDate = moment(opts.field.value, opts.format).toDate(); - } else { - opts.defaultDate = new Date(Date.parse(opts.field.value)); - } + opts.defaultDate = self._parseFieldValue(); opts.setDefaultDate = true; } } @@ -825,6 +856,14 @@ } }, + /** + * clear and reset the date + */ + clear: function() + { + this.setDate(null); + }, + /** * change view to a specific date */ @@ -1004,9 +1043,8 @@ } } - randId = 'pika-title-' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 2); - for (var c = 0; c < opts.numberOfMonths; c++) { + randId = 'pika-title-' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 2); html += '
' + renderTitle(this, c, this.calendars[c].year, this.calendars[c].month, this.calendars[0].year, randId) + this.render(this.calendars[c].year, this.calendars[c].month, randId) + '
'; } @@ -1174,7 +1212,7 @@ if (++r === 7) { if (opts.showWeekNumber) { - row.unshift(renderWeek(i - before, month, year)); + row.unshift(renderWeek(i - before, month, year, opts.firstWeekOfYearMinDays)); } data.push(renderRow(row, opts.isRTL, opts.pickWholeWeek, isWeekSelected)); row = []; @@ -1213,9 +1251,12 @@ if (this._o.bound) { removeEvent(document, 'click', this._onClick); } - this.el.style.position = 'static'; // reset - this.el.style.left = 'auto'; - this.el.style.top = 'auto'; + + if (!this._o.container) { + this.el.style.position = 'static'; // reset + this.el.style.left = 'auto'; + this.el.style.top = 'auto'; + } addClass(this.el, 'is-hidden'); this._v = false; if (v !== undefined && typeof this._o.onClose === 'function') { diff --git a/yarn.lock b/yarn.lock index bd92478f1a8..f2774cead88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1998,10 +1998,10 @@ picomatch@^2.3.1: resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA== -pikaday@1.8.0: - version "1.8.0" - resolved "https://registry.yarnpkg.com/pikaday/-/pikaday-1.8.0.tgz#ce930e257042e852e6aadee1115e01554b2d71c5" - integrity sha512-SgGxMYX0NHj9oQnMaSyAipr2gOrbB4Lfs/TJTb6H6hRHs39/5c5VZi73Q8hr53+vWjdn6HzkWcj8Vtl3c9ziaA== +pikaday@1.8.2: + version "1.8.2" + resolved "https://registry.yarnpkg.com/pikaday/-/pikaday-1.8.2.tgz#72cc73fab7ccc068cbdf7dcaa1ce400fcfd894e3" + integrity sha512-TNtsE+34BIax3WtkB/qqu5uepV1McKYEgvL3kWzU7aqPCpMEN6rBF3AOwu4WCwAealWlBGobXny/9kJb49C1ew== pkg-dir@4.2.0: version "4.2.0"