@mediafly/js-ui 51.4.0 → 51.4.1-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/lib/constants/constants.js +2 -1
  2. package/lib/constants/constants.js.map +1 -1
  3. package/lib/date-input/date-input.js +33 -12
  4. package/lib/date-input/date-input.js.map +1 -1
  5. package/lib/date-input/validate-date.js +12 -8
  6. package/lib/date-input/validate-date.js.map +1 -1
  7. package/lib/date-time-zone-picker/date-time-zone-factory.js +62 -49
  8. package/lib/date-time-zone-picker/date-time-zone-factory.js.map +1 -1
  9. package/lib/date-time-zone-picker/date-time-zone-picker.html +6 -6
  10. package/lib/date-time-zone-picker/date-time-zone-picker.js +119 -59
  11. package/lib/date-time-zone-picker/date-time-zone-picker.js.map +1 -1
  12. package/lib/date-time-zone-picker/datepicker.html +2 -3
  13. package/lib/date-time-zone-picker/datepicker.js +79 -26
  14. package/lib/date-time-zone-picker/datepicker.js.map +1 -1
  15. package/lib/date-time-zone-picker/time-format-filter.js +8 -8
  16. package/lib/date-time-zone-picker/time-format-filter.js.map +1 -1
  17. package/lib/date-time-zone-picker/time-zone-picker.js +1 -1
  18. package/lib/date-time-zone-picker/timepicker.html +1 -2
  19. package/lib/date-time-zone-picker/timepicker.js +35 -40
  20. package/lib/date-time-zone-picker/timepicker.js.map +1 -1
  21. package/lib/time-input/time-input.js +27 -23
  22. package/lib/time-input/time-input.js.map +1 -1
  23. package/lib/time-input/validate-time.js +6 -3
  24. package/lib/time-input/validate-time.js.map +1 -1
  25. package/lib/tsconfig.deploy.tsbuildinfo +163 -25
  26. package/package.json +4 -4
  27. package/src/components/constants/constants.js +2 -1
  28. package/src/components/date-input/date-input.js +31 -12
  29. package/src/components/date-input/validate-date.js +14 -10
  30. package/src/components/date-time-zone-picker/date-time-zone-factory.ts +62 -49
  31. package/src/components/date-time-zone-picker/date-time-zone-picker.html +6 -6
  32. package/src/components/date-time-zone-picker/date-time-zone-picker.ts +131 -70
  33. package/src/components/date-time-zone-picker/datepicker.html +2 -3
  34. package/src/components/date-time-zone-picker/datepicker.ts +83 -26
  35. package/src/components/date-time-zone-picker/time-format-filter.ts +8 -8
  36. package/src/components/date-time-zone-picker/time-zone-picker.js +1 -1
  37. package/src/components/date-time-zone-picker/timepicker.html +1 -2
  38. package/src/components/date-time-zone-picker/timepicker.js +46 -47
  39. package/src/components/time-input/time-input.js +27 -24
  40. package/src/components/time-input/validate-time.js +6 -3
  41. package/src/index.spec.js +1 -1
  42. package/webpack.config.js +6 -0
  43. package/tsconfig.tsbuildinfo +0 -7221
@@ -1,6 +1,6 @@
1
1
  import angular from 'angular'
2
2
  import _ from 'lodash'
3
- import moment from 'moment'
3
+ import { DateTime } from 'luxon'
4
4
 
5
5
  angular.module('app').factory('dateTimeZoneFactory', function dateTimeZoneFactory() {
6
6
 
@@ -38,44 +38,58 @@ angular.module('app').factory('dateTimeZoneFactory', function dateTimeZoneFactor
38
38
  { value: 12, zone: 'NZST - New Zealand Standard Time', display: 'NZST' }
39
39
  ]
40
40
 
41
- // using string validate on the datepicker gives us more control w moment's loose parsing
41
+ // using string validate on the datepicker gives us more control with luxon's parsing
42
42
  const validDateFormats = [
43
- 'D-MMMM-YYYY',
44
- 'D-MM-YYYY',
45
- 'D-M-YYYY',
46
- 'MMMM-D-YYYY',
47
- 'MM-D-YYYY',
48
- 'M-D-YYYY',
49
- 'D/MMMM/YYYY',
50
- 'D/MM/YYYY',
51
- 'D/M/YYYY',
52
- 'MMMM/D/YYYY',
53
- 'MM/D/YYYY',
54
- 'M/D/YYYY',
55
- 'D.MMMM.YYYY',
56
- 'D.MM.YYYY',
57
- 'D.M.YYYY',
58
- 'MMMM.D.YYYY',
59
- 'MM.D.YYYY',
60
- 'M.D.YYYY',
61
- 'D-MMMM-YY',
62
- 'D-MM-YY',
63
- 'D-M-YY',
64
- 'MMMM-D-YY',
65
- 'MM-D-YY',
66
- 'M-D-YY',
67
- 'D/MMMM/YY',
68
- 'D/MM/YY',
69
- 'D/M/YY',
70
- 'MMMM/D/YY',
71
- 'MM/D/YY',
72
- 'M/D/YY',
73
- 'D.MMMM.YY',
74
- 'D.MM.YY',
75
- 'D.M.YY',
76
- 'MMMM.D.YY',
77
- 'MM.D.YY',
78
- 'M.D.YY'
43
+ // Full month name formats (MMMM)
44
+ 'd-MMMM-yyyy',
45
+ 'd-MM-yyyy',
46
+ 'd-M-yyyy',
47
+ 'MMMM-d-yyyy',
48
+ 'MM-d-yyyy',
49
+ 'M-d-yyyy',
50
+ 'd/MMMM/yyyy',
51
+ 'd/MM/yyyy',
52
+ 'd/M/yyyy',
53
+ 'MMMM/d/yyyy',
54
+ 'MM/d/yyyy',
55
+ 'M/d/yyyy',
56
+ 'd.MMMM.yyyy',
57
+ 'd.MM.yyyy',
58
+ 'd.M.yyyy',
59
+ 'MMMM.d.yyyy',
60
+ 'MM.d.yyyy',
61
+ 'M.d.yyyy',
62
+ 'd-MMMM-yy',
63
+ 'd-MM-yy',
64
+ 'd-M-yy',
65
+ 'MMMM-d-yy',
66
+ 'MM-d-yy',
67
+ 'M-d-yy',
68
+ 'd/MMMM/yy',
69
+ 'd/MM/yy',
70
+ 'd/M/yy',
71
+ 'MMMM/d/yy',
72
+ 'MM/d/yy',
73
+ 'M/d/yy',
74
+ 'd.MMMM.yy',
75
+ 'd.MM.yy',
76
+ 'd.M.yy',
77
+ 'MMMM.d.yy',
78
+ 'MM.d.yy',
79
+ 'M.d.yy',
80
+ // Abbreviated month name formats (MMM)
81
+ 'd-MMM-yyyy',
82
+ 'MMM-d-yyyy',
83
+ 'd/MMM/yyyy',
84
+ 'MMM/d/yyyy',
85
+ 'd.MMM.yyyy',
86
+ 'MMM.d.yyyy',
87
+ 'd-MMM-yy',
88
+ 'MMM-d-yy',
89
+ 'd/MMM/yy',
90
+ 'MMM/d/yy',
91
+ 'd.MMM.yy',
92
+ 'MMM.d.yy'
79
93
  ]
80
94
 
81
95
 
@@ -85,28 +99,27 @@ angular.module('app').factory('dateTimeZoneFactory', function dateTimeZoneFactor
85
99
 
86
100
  validDateFormats,
87
101
 
88
- getTimeZone(momentDate) {
102
+ getTimeZone(dateTime) {
89
103
 
90
- if (momentDate.isValid()) {
104
+ if (dateTime && dateTime.isValid) {
91
105
 
92
106
  // Get the timezone offset in hours and adjust the sign
93
- let zoneOffset = (momentDate.utcOffset() / 60)
107
+ let zoneOffset = dateTime.offset / 60
94
108
 
95
109
  // consider a shift for DST
96
- // must use isDST on a frest moment object -
97
- // the passed variable is parsedZone and thus will always return isDST false
98
- const isDST = moment(momentDate.format()).isDST()
110
+ // create a fresh DateTime to check if DST is in effect
111
+ const isDST = DateTime.fromISO(dateTime.toISO()).isInDST
99
112
 
100
- // at this point zoneOffest is our true offest from UTC 0
101
- // so for example if we are UTC -6 AND we are DST, then our moment is really "Mountain Time" which is is -7
113
+ // at this point zoneOffset is our true offset from UTC 0
114
+ // so for example if we are UTC -6 AND we are DST, then our dateTime is really "Mountain Time" which is -7
102
115
  // during the winter but is -6 during DST -- if we want to consistently show the label 'MST' for this date
103
- // we need to select -7 although the offset is -6 (or subtracting 1 from our actyual utc output)
116
+ // we need to select -7 although the offset is -6 (or subtracting 1 from our actual utc output)
104
117
  zoneOffset = (isDST) ? zoneOffset - 1 : zoneOffset
105
118
 
106
119
  // NOTE: The above DST adjustment is somewhat arbitrary.
107
120
  // isDST() cannot be globally accurate as it works purely off a date and an offset.
108
- // No actual timezone information is used except for assumptins made from the users current locale.
109
- // Recommendation to switch to moment's timezone.js in the future.
121
+ // No actual timezone information is used except for assumptions made from the users current locale.
122
+ // Recommendation to switch to luxon's timezone handling in the future.
110
123
 
111
124
  return this.getTimeZoneByOffset(zoneOffset)
112
125
  }
@@ -6,18 +6,18 @@
6
6
  input-id="{{ inputId }}"
7
7
  is-disabled="isDisabled"
8
8
  is-required="{{ isRequired }}"
9
- date-moment="dateMoment"
9
+ date-luxon="dateLuxon"
10
10
  on-open="pickerOpened()"
11
11
  on-date-change="updateDate(newDate)">
12
12
  </mfly:date-picker>
13
13
  </div>
14
14
 
15
- <div class="o-col-xs-7" ng-show="type === 'datetime' && dateMoment && !isDisabled">
15
+ <div class="o-col-xs-7" ng-show="type === 'datetime' && dateLuxon && !isDisabled">
16
16
  <mfly:time-picker
17
17
  input-id="{{ inputId }}Time"
18
- time-moment="timeMoment"
18
+ time-luxon="timeLuxon"
19
19
  ng-show="!isDisabled"
20
- on-time-change="updateTime()">
20
+ on-time-change="updateTime(newTime)">
21
21
  </mfly:time-picker>
22
22
 
23
23
  <mfly:time-zone-picker
@@ -28,9 +28,9 @@
28
28
  </div>
29
29
 
30
30
  <!-- If the time is disabled, show plain text instead of controls -->
31
- <div class="o-col-xs-7" ng-show="type === 'datetime' && dateMoment && isDisabled">
31
+ <div class="o-col-xs-7" ng-show="type === 'datetime' && dateLuxon && isDisabled">
32
32
  <div class="pull-left disabled">
33
- <span>{{ parentMoment.format('hh:mm a') }} {{ (timeZone) ? timeZone.display : '' }}</span>
33
+ <span>{{ parentLuxon.toFormat('hh:mm a') }} {{ (timeZone) ? timeZone.display : '' }}</span>
34
34
  </div>
35
35
  </div>
36
36
  </div>
@@ -1,5 +1,5 @@
1
1
  import angular from 'angular'
2
- import moment from 'moment'
2
+ import { DateTime } from 'luxon'
3
3
 
4
4
  // This type is necessary because the directive link function expects something
5
5
  // conforming to ng.IController, but we want the types from ng.INgModelController
@@ -23,73 +23,104 @@ angular.module('app').directive('mflyDateTimeZonePicker', ['dateTimeZoneFactory'
23
23
  template: require('./date-time-zone-picker.html'),
24
24
  link($scope: ScopeKeyValuePair, _element, _attr, ngModelCtrl: PatchedNgModelController) {
25
25
 
26
- // Init function creates moment dates from our utc string provided by the API
27
- // These moment dates are bound to the datepicker and timepicker (the
28
- // timezone object is also created but is not a moment)
29
- // Each component only alters its respective moment date, then tells this
26
+ // Init function creates DateTime dates from our utc string provided by the API
27
+ // These DateTime dates are bound to the datepicker and timepicker (the
28
+ // timezone object is also created but is not a DateTime)
29
+ // Each component only alters its respective DateTime date, then tells this
30
30
  // component when a change is made
31
31
  // When a change is made, this component combines the 3 datetime elements
32
32
  // and sends a utc string back to the api
33
33
  // ------------------------------------------------------------------
34
- function initParentMomentFromAPI(value) {
35
-
36
- // Create a base moment and bound copies for each child component.
37
- $scope.parentMoment = moment.parseZone(value)
38
-
39
- $scope.timeMoment = moment({ hour: $scope.parentMoment.hour(), minute: $scope.parentMoment.minute() })
40
- $scope.dateMoment = moment({ year: $scope.parentMoment.year(), month: $scope.parentMoment.month(), date: $scope.parentMoment.date() })
34
+ function initParentLuxonFromAPI(value) {
35
+
36
+ // Create a base DateTime and bound copies for each child component.
37
+ if (value instanceof Date) {
38
+ $scope.parentLuxon = DateTime.fromJSDate(value)
39
+ } else if (typeof value === 'string') {
40
+ // Parse with setZone: true to preserve the offset from the string
41
+ const dt = DateTime.fromISO(value, { setZone: true })
42
+ // Calculate offset in hours (including minutes as decimal)
43
+ const offset = dt.offset / 60
44
+ // Find the matching time zone object
45
+ const matchedZone = dateTimeZoneFactory.getTimeZoneByOffset(offset)
46
+ // If found, set the zone on the DateTime object
47
+ if (matchedZone) {
48
+ // Format offset as UTC±HH:MM for non-integer offsets
49
+ const absOffset = Math.abs(matchedZone.value)
50
+ const hours = Math.floor(absOffset)
51
+ const minutes = Math.round((absOffset - hours) * 60)
52
+ const sign = matchedZone.value >= 0 ? '+' : '-'
53
+ const zoneString =
54
+ minutes === 0
55
+ ? `UTC${sign}${hours}`
56
+ : `UTC${sign}${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
57
+ $scope.parentLuxon = dt.setZone(zoneString, { keepLocalTime: true })
58
+ $scope.timeZone = matchedZone
59
+ } else {
60
+ // Fallback: just use the parsed DateTime
61
+ $scope.parentLuxon = dt
62
+ $scope.timeZone = null
63
+ }
64
+ } else {
65
+ throw new Error(`Invalid value type ${typeof value} (${value})`)
66
+ }
41
67
 
42
- // The timezone component takes a custom timezone object instead of
43
- // a moment. The factory returns a timezone, adjusting for DST.
44
- $scope.timeZone = dateTimeZoneFactory.getTimeZone($scope.parentMoment)
68
+ $scope.timeLuxon = DateTime.fromObject({ hour: $scope.parentLuxon.hour, minute: $scope.parentLuxon.minute, second: 0, millisecond: 0 })
69
+ $scope.dateLuxon = DateTime.fromObject({ year: $scope.parentLuxon.year, month: $scope.parentLuxon.month, day: $scope.parentLuxon.day })
45
70
  }
46
71
 
47
- function initParentMomentFromDatePicker(newDate) {
72
+ function initParentLuxonFromDatePicker(newDate) {
48
73
  // when selecting a date from null or for the first time we need to
49
- // initialize the parentMoment
50
- $scope.parentMoment = moment()
51
-
52
- // the dateMoment is set by the datepicker, but we want to update the parent
53
- // moment to have the same date
54
- $scope.parentMoment.month(newDate.month())
55
- $scope.parentMoment.year(newDate.year())
56
- $scope.parentMoment.date(newDate.date())
57
-
58
- // we then init the timeMoment with a default of 12:00am
59
- $scope.timeMoment = moment('12:00 AM', 'hh:mm A')
60
-
61
- // parent moment will have the now, but should also inisially have 12:00am
62
- $scope.parentMoment.hour($scope.timeMoment.hour())
63
- $scope.parentMoment.minute($scope.timeMoment.minute())
64
-
65
- // and init the time zone from the users's local time zone as we did in the initFromAPI funciton
66
- $scope.timeZone = dateTimeZoneFactory.getTimeZone($scope.parentMoment)
74
+ // initialize the parentLuxon
75
+ $scope.parentLuxon = DateTime.now()
76
+
77
+ // the dateLuxon is set by the datepicker, but we want to update the parent
78
+ // luxon to have the same date
79
+ $scope.parentLuxon = $scope.parentLuxon.set({
80
+ month: newDate.month,
81
+ year: newDate.year,
82
+ day: newDate.day
83
+ })
84
+
85
+ // we then init the timeLuxon with a default of 12:00am
86
+ $scope.timeLuxon = DateTime.fromFormat('12:00 AM', 'hh:mm a')
87
+
88
+ // parent luxon will have the now, but should also initially have 12:00am
89
+ $scope.parentLuxon = $scope.parentLuxon.set({
90
+ hour: $scope.timeLuxon.hour,
91
+ minute: $scope.timeLuxon.minute,
92
+ second: 0,
93
+ millisecond: 0,
94
+ })
95
+
96
+ // and init the time zone from the users's local time zone as we did in the initFromAPI function
97
+ $scope.timeZone = dateTimeZoneFactory.getTimeZone($scope.parentLuxon)
67
98
  }
68
99
 
69
100
  // Formatter runs when incoming values come from the model - this is
70
- // where we initialize our moment.
71
- ngModelCtrl.$formatters.push(function(modelValue) {
101
+ // where we initialize our DateTime.
102
+ ngModelCtrl.$formatters.push(function (modelValue) {
72
103
 
73
104
  if (modelValue) {
74
- initParentMomentFromAPI(modelValue)
105
+ initParentLuxonFromAPI(modelValue)
75
106
  }
76
107
  else {
77
108
  // If we don't have a value, we should insure we clear the control
78
109
  // by passing NULL to the update function.
79
- $scope.dateMoment = null
110
+ $scope.dateLuxon = null
80
111
  }
81
112
 
82
113
  return modelValue
83
114
  })
84
115
 
85
- ngModelCtrl.$parsers.push(function(modelValue) {
116
+ ngModelCtrl.$parsers.push(function (modelValue) {
86
117
  return modelValue
87
118
  })
88
119
 
89
- function updateAPIFromMoment() {
120
+ function updateAPIFromLuxon() {
90
121
 
91
- if ($scope.parentMoment) {
92
- ngModelCtrl.$setViewValue($scope.parentMoment.format())
122
+ if ($scope.parentLuxon) {
123
+ ngModelCtrl.$setViewValue($scope.parentLuxon.toISO())
93
124
  }
94
125
  else {
95
126
  ngModelCtrl.$setViewValue(null)
@@ -97,60 +128,90 @@ angular.module('app').directive('mflyDateTimeZonePicker', ['dateTimeZoneFactory'
97
128
  }
98
129
 
99
130
 
100
- $scope.updateDate = function(newDate) {
101
- // This is fired when the datepicker has updated the date portion of the moment
102
- // Update the parentMoment with only the date change
131
+ $scope.updateDate = function (newDate) {
132
+ // This is fired when the datepicker has updated the date portion of the DateTime
133
+ // Update the parentLuxon with only the date change
103
134
  if (newDate) {
104
- if ($scope.parentMoment) {
105
- // if we have a parent moment update it
106
- $scope.parentMoment.month(newDate.month())
107
- $scope.parentMoment.year(newDate.year())
108
- $scope.parentMoment.date(newDate.date())
135
+ // Update dateLuxon to reflect the new date (needed for template visibility)
136
+ $scope.dateLuxon = newDate
137
+
138
+ if ($scope.parentLuxon) {
139
+ // if we have a parent luxon update it
140
+ $scope.parentLuxon = $scope.parentLuxon.set({
141
+ month: newDate.month,
142
+ year: newDate.year,
143
+ day: newDate.day
144
+ })
109
145
  }
110
146
  else {
111
- // if we do not have a parent moment b/c no previous date existed
112
- initParentMomentFromDatePicker(newDate)
147
+ // if we do not have a parent luxon b/c no previous date existed
148
+ initParentLuxonFromDatePicker(newDate)
113
149
  }
114
150
  }
115
151
  else {
116
152
  // we are clearing out a date
117
- $scope.parentMoment = ''
153
+ $scope.dateLuxon = null
154
+ $scope.parentLuxon = null
118
155
  }
119
156
 
120
- // changing between DST could alter our UTC offest
157
+ // changing between DST could alter our UTC offset
121
158
  // this adjustment happens in updateTimeZone which we can call here
122
- $scope.updateTimeZone($scope.timeZone.value)
159
+ if ($scope.timeZone) {
160
+ $scope.updateTimeZone($scope.timeZone.value)
161
+ }
162
+
163
+ updateAPIFromLuxon()
123
164
  }
124
165
 
125
166
 
126
- $scope.updateTime = function() {
127
- // This is fired when the timepicker has updated the time portion of the moment
128
- // Update the parentMoment with only the time change
129
- $scope.parentMoment.hour($scope.timeMoment.hour())
130
- $scope.parentMoment.minute($scope.timeMoment.minute())
167
+ $scope.updateTime = function (newTime) {
168
+ // This is fired when the timepicker has updated the time portion of the DateTime
169
+ // Update the parentLuxon with only the time change
170
+ $scope.parentLuxon = $scope.parentLuxon.set({
171
+ hour: newTime.hour,
172
+ minute: newTime.minute,
173
+ second: 0,
174
+ millisecond: 0,
175
+ })
131
176
 
132
- updateAPIFromMoment()
177
+ updateAPIFromLuxon()
133
178
  }
134
179
 
135
- $scope.updateTimeZone = function(zoneOffset) {
180
+ $scope.updateTimeZone = function (zoneOffset) {
136
181
 
137
- if ($scope.parentMoment !== '') {
182
+ if ($scope.parentLuxon !== '') {
138
183
 
139
- // The zone offest displays with a dst offset (see date-time-zone-factory for details)
184
+ // The zone offset displays with a dst offset (see date-time-zone-factory for details)
140
185
  // When we save back to the API we need to re-account for that offset
141
- // Create a moment to determine if we are dst
142
- const isDST = moment($scope.parentMoment.format()).isDST()
186
+ // Create a DateTime to determine if we are dst
187
+ const isDST = DateTime.fromISO($scope.parentLuxon.toISO()).isInDST
143
188
 
144
189
  // adjust the zoneOffset passed from the UI if the date is dst
145
190
  zoneOffset = (isDST) ? zoneOffset + 1 : zoneOffset
146
- // update the zone/offset change
147
- $scope.parentMoment.utcOffset(zoneOffset, true)
191
+
192
+ // Convert fractional hours to HH:MM format for Luxon
193
+ // Luxon expects UTC offsets in the format "UTC+HH:MM" or "UTC-HH:MM"
194
+ const sign = zoneOffset >= 0 ? '+' : '-'
195
+ const absOffset = Math.abs(zoneOffset)
196
+ const hours = Math.floor(absOffset)
197
+ const minutes = Math.round((absOffset - hours) * 60)
198
+
199
+ let zoneString
200
+ if (minutes === 0) {
201
+ // For whole hours, we can use the simple format like "UTC+3" or "UTC-5"
202
+ zoneString = `UTC${sign}${hours}`
203
+ } else {
204
+ // For fractional hours, we need the HH:MM format like "UTC+03:30" or "UTC-03:30"
205
+ zoneString = `UTC${sign}${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
206
+ }
207
+
208
+ $scope.parentLuxon = $scope.parentLuxon.setZone(zoneString, { keepLocalTime: true })
148
209
  }
149
210
 
150
- updateAPIFromMoment()
211
+ updateAPIFromLuxon()
151
212
  }
152
213
 
153
- $scope.pickerOpened = function() {
214
+ $scope.pickerOpened = function () {
154
215
  $scope.onOpen()
155
216
  }
156
217
 
@@ -6,10 +6,9 @@
6
6
  autocomplete="off"
7
7
  class="o-form-control"
8
8
  id="{{ inputId }}"
9
- mfly-datepicker="momentDate"
9
+ mfly-datepicker="luxonDate"
10
10
  name="mflyDatepicker"
11
- ng-blur="focusAddOn = false"
12
- ng-change="validateDate()"
11
+ ng-blur="onBlur()"
13
12
  ng-disabled="isDisabled"
14
13
  ng-focus="focusAddOn = true"
15
14
  ng-keydown="inputKeyDown($event)"
@@ -1,5 +1,5 @@
1
1
  import angular from 'angular'
2
- import moment from 'moment'
2
+ import { DateTime } from 'luxon'
3
3
  import Pikaday from 'pikaday'
4
4
  import { recursivelySetPristine } from '../form-validation-utils/form-validation-utils'
5
5
  import type { TranslateFactory } from '../translate/translate-factory'
@@ -9,7 +9,7 @@ interface PikadayInstance {
9
9
  }
10
10
 
11
11
  // Wrapper directive for pikaday datepicker component
12
- // This 3rd party component has a dependency on moment.js
12
+ // This 3rd party component has an optional dependency on moment.js that we no longer use
13
13
  angular.module('app').directive('mflyDatePicker', [
14
14
  'dateTimeZoneFactory',
15
15
  'translateFactory',
@@ -25,7 +25,7 @@ angular.module('app').directive('mflyDatePicker', [
25
25
  isDisabled: '=',
26
26
  isRequired: '@',
27
27
  format: '@',
28
- dateMoment: '=',
28
+ dateLuxon: '=',
29
29
  onDateChange: '&',
30
30
  onOpen: '&'
31
31
  },
@@ -37,7 +37,7 @@ angular.module('app').directive('mflyDatePicker', [
37
37
  }
38
38
 
39
39
  // if a "small" format is required we need to set it here
40
- const dateStringFormat = ($scope.format === 'short') ? 'D-MMM-YYYY' : 'D-MMMM-YYYY'
40
+ const dateStringFormat = ($scope.format === 'short') ? 'd-MMM-yyyy' : 'd-MMMM-yyyy'
41
41
 
42
42
  // The Pikaday component does some odd formatting at times
43
43
  // This flag helps avoid this anomaly when it occurs
@@ -51,10 +51,9 @@ angular.module('app').directive('mflyDatePicker', [
51
51
 
52
52
  // set up default options for the pikaday component
53
53
  const pikadayOptions = {
54
- field: inputElement[0],
55
- trigger: wrapperDiv[0],
54
+ field: wrapperDiv[0],
56
55
  firstDay: 1,
57
- format: dateStringFormat,
56
+ format: dateStringFormat.toUpperCase(), // Pikaday expects uppercase format
58
57
  yearRange: 10,
59
58
  setDefaultDate: true,
60
59
  defaultDate: '',
@@ -71,21 +70,37 @@ angular.module('app').directive('mflyDatePicker', [
71
70
  // Handler for when a date is 'selected' with the picker
72
71
  // selection happens on enter, blur, and by using the popup calendars
73
72
  onSelect() {
74
- // update the bound date moment
75
- $scope.dateMoment = picker.getMoment()
73
+ // update the bound date luxon - convert from JS Date to DateTime
74
+ const selectedDate = picker.getDate()
76
75
 
77
- // fire a change in the parent directive for the api
78
- $scope.onDateChange({ newDate: $scope.dateMoment })
76
+ // Safely handle the digest cycle
77
+ if ($scope.$root.$$phase !== '$digest' && $scope.$root.$$phase !== '$apply') {
78
+ $scope.$apply(() => { $scope.updateDate(selectedDate) })
79
+ } else {
80
+ $scope.updateDate(selectedDate)
81
+ }
79
82
  }
80
83
  }
81
84
 
82
85
  // Handle for the Pikaday component
83
86
  const picker = new Pikaday(pikadayOptions)
84
87
 
85
- $scope.$watch('dateMoment', function(newValue, oldValue) {
88
+ $scope.updateDate = function(newDate) {
89
+ $scope.dateLuxon = newDate ? DateTime.fromJSDate(newDate) : null
90
+ // fire a change in the parent directive for the api
91
+ $scope.onDateChange({ newDate: $scope.dateLuxon })
92
+
93
+ // Set the form control as dirty and valid when date changes
94
+ $scope.form?.validation?.mflyDatepicker?.$setDirty()
95
+ $scope.form?.validation?.mflyDatepicker?.$setValidity('invalidDate', true)
96
+ }
97
+
98
+ $scope.$watch('dateLuxon', function(newValue: any, oldValue) {
86
99
 
87
100
  if (newValue === oldValue) { // i.e., initializing
88
- picker.setMoment($scope.dateMoment)
101
+ if ($scope.dateLuxon) {
102
+ picker.setDate($scope.dateLuxon.toJSDate())
103
+ }
89
104
  recursivelySetPristine($scope.form.validation)
90
105
  return
91
106
  }
@@ -103,7 +118,9 @@ angular.module('app').directive('mflyDatePicker', [
103
118
 
104
119
  // Otherwise, it may be a legit change. For example, if the model was
105
120
  // programmatically changed from somewhere outside the component.
106
- picker.setMoment($scope.dateMoment, true) // true = do not trigger onSelect
121
+ picker.setDate(newValue.toJSDate(), true) // true = do not trigger onSelect
122
+ // Also update the input value to match our format
123
+ $scope.dateInputValue.date = newValue.toFormat(dateStringFormat)
107
124
  })
108
125
 
109
126
  // watch necessary to style preceding add-on element (CSS has no prev sibling selector)
@@ -119,37 +136,66 @@ angular.module('app').directive('mflyDatePicker', [
119
136
  })
120
137
 
121
138
 
122
- $scope.setPikadayMoment = function() {
139
+ $scope.setPikadayLuxon = function() {
123
140
 
124
- const tempMoment = moment($scope.dateInputValue.date, 'DD-MMMM-YYYY')
141
+ // Try parsing with any valid format, just like in validateDate
142
+ let tempDateTime
143
+ for (const format of dateTimeZoneFactory.validDateFormats) {
144
+ tempDateTime = DateTime.fromFormat($scope.dateInputValue.date, format)
145
+ if (tempDateTime.isValid) {
146
+ break
147
+ }
148
+ }
125
149
 
126
150
  // The pikaday will oddly format 2 digit years to 0003
127
- // The year is correct in the moment, but you can see the odd string when formatted
128
- // To prevent we will set the moment manually if the user is not backspacing/deleting
129
- if (!preventPikadayUpdate) {
130
- picker.setMoment(tempMoment)
151
+ // The year is correct in the dateTime, but you can see the odd string when formatted
152
+ // To prevent we will set the dateTime manually if the user is not backspacing/deleting
153
+ if (!preventPikadayUpdate && tempDateTime && tempDateTime.isValid) {
154
+ // Set the date without triggering onSelect to avoid circular updates
155
+ picker.setDate(tempDateTime.toJSDate(), true)
156
+
157
+ // Manually update the dateLuxon to trigger reformatting
158
+ const updateDateTime = () => {
159
+ $scope.dateLuxon = tempDateTime
160
+ $scope.onDateChange({ newDate: tempDateTime })
161
+ }
162
+
163
+ // Safely handle the digest cycle
164
+ if ($scope.$root.$$phase !== '$digest' && $scope.$root.$$phase !== '$apply') {
165
+ $scope.$apply(updateDateTime)
166
+ } else {
167
+ updateDateTime()
168
+ }
131
169
  } else {
132
170
  preventPikadayUpdate = false
133
171
  }
134
172
  }
135
173
 
136
- // called everytime the text input is changed manually
174
+ // called every time the text input is changed manually
137
175
  $scope.validateDate = function() {
138
176
  // pikaday interrupts the normal binding - manually update scope
139
177
  $scope.dateInputValue.date = angular.element(inputElement[0]).val()
140
178
 
141
179
  let isValid = true
142
180
  if ($scope.dateInputValue.date && $scope.dateInputValue.date.length > 0) {
143
- // attempt to create a date form value
144
- isValid = moment($scope.dateInputValue.date, dateTimeZoneFactory.validDateFormats, true).isValid()
181
+ // attempt to create a date from value using any valid format
182
+ isValid = false
183
+ for (const format of dateTimeZoneFactory.validDateFormats) {
184
+ const parsed = DateTime.fromFormat($scope.dateInputValue.date, format)
185
+ if (parsed.isValid) {
186
+ isValid = true
187
+ $scope.updateDate(parsed.toJSDate())
188
+ break
189
+ }
190
+ }
145
191
 
146
192
  if (isValid) {
147
- $scope.setPikadayMoment()
193
+ $scope.setPikadayLuxon()
148
194
  }
149
195
 
150
196
  } else if ($scope.dateInputValue.date.length === 0) {
151
- // clear the scope moment
152
- $scope.dateMoment = null
197
+ // clear the scope luxon
198
+ $scope.dateLuxon = null
153
199
  // update the parent component
154
200
  $scope.onDateChange({ newDate: null })
155
201
  } else {
@@ -166,6 +212,17 @@ angular.module('app').directive('mflyDatePicker', [
166
212
  }
167
213
  }
168
214
 
215
+ $scope.onBlur = function() {
216
+ // Validate the date
217
+ $scope.validateDate()
218
+
219
+ // Hide the Pikaday calendar popup
220
+ picker.hide()
221
+
222
+ // Update focus state
223
+ $scope.focusAddOn = false
224
+ }
225
+
169
226
  $scope.requiredText = translateFactory.instant('JSUI.REQUIRED')
170
227
  $scope.invalidDateText = translateFactory.instant('JSUI.INVALID_DATE')
171
228
  }