bullet_train-fields 1.3.1 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c6f5ed54111d8605cb024f35e2d22ebbc8796dc0ee1ea889ad0e7e19e9bbeaa
4
- data.tar.gz: a72c9b6c59084dba172f9d01752056eff99eae9b7c0f38eac296bc4a46f231b9
3
+ metadata.gz: 8d55793fe0b13a862e6df286add5f944e51ddac507d83e3771b35b839a123599
4
+ data.tar.gz: df7c6f3067c58ec7afeb9f673b2eab58bdcd53c8dbefd6d6da7a3fb44b1dbaa1
5
5
  SHA512:
6
- metadata.gz: 14b0856a9c3a33999713ed94ac519e92849c01b97a63f503f7913b3158294049c0d5f3ddd6ec72f39d3fa3f2e691d19210779d86d6bbc02b16514a8a689df067
7
- data.tar.gz: 4116f5e19cfc907ca0ee1f11637650fd8e3cb0be8a4f7c40102f34be46fb47efb2af9a07b49b79afd2c0ee1929f62e82a4db3bb28ad755b04630a2ccce3bc345
6
+ metadata.gz: b952ed07c8cc291358ce1195b8dbeb681b914c60eb359ef18a3ead154ee114538f7526e2a26eb1d05339534103a53e25b3e819a5fbd5c329068de6ad8a9e755f
7
+ data.tar.gz: 87aa8fa81f8bc2cbaf9dda4c590860d6ab7b3adfb570f2b0fcf34c7b61346f9d6bd275caf180ac970518de87c2f5c8f976ab979f13491883d1cc5e84ea9024c5
@@ -2,12 +2,22 @@ module Fields::DateAndTimeSupport
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  def assign_date_and_time(strong_params, attribute)
5
+ deprecator = ActiveSupport::Deprecation.new("2.0", "BulletTrain::Fields")
6
+ deprecator.deprecation_warning(
7
+ "assign_date_and_time",
8
+ "Please assign an ISO8601 datetime string as form field value instead and remove all assign_date_and_time assignments,
9
+ see https://ruby-doc.org/3.2.2/exts/date/DateTime.html"
10
+ )
5
11
  attribute = attribute.to_s
6
12
  time_zone_attribute = "#{attribute}_time_zone"
7
13
  if strong_params.dig(attribute).present?
8
- time_zone = ActiveSupport::TimeZone.new(strong_params[time_zone_attribute] || current_team.time_zone)
9
- strong_params.delete(time_zone_attribute)
10
- strong_params[attribute] = time_zone.strptime(strong_params[attribute], t("global.formats.date_and_time"))
14
+ begin
15
+ strong_params[attribute] = DateTime.iso8601(strong_params[attribute])
16
+ rescue ArgumentError
17
+ time_zone = ActiveSupport::TimeZone.new(strong_params[time_zone_attribute] || current_team.time_zone)
18
+ strong_params.delete(time_zone_attribute)
19
+ strong_params[attribute] = time_zone.strptime(strong_params[attribute], t("global.formats.date_and_time"))
20
+ end
11
21
  end
12
22
  end
13
23
  end
@@ -2,11 +2,21 @@ module Fields::DateSupport
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  def assign_date(strong_params, attribute)
5
+ deprecator = ActiveSupport::Deprecation.new("2.0", "BulletTrain::Fields")
6
+ deprecator.deprecation_warning(
7
+ "assign_date",
8
+ "Please assign an ISO8601 date string as field value instead and remove all assign_date assignments,
9
+ see https://ruby-doc.org/3.2.2/exts/date/Date.html"
10
+ )
5
11
  attribute = attribute.to_s
6
12
  if strong_params.dig(attribute).present?
7
- parsed_value = Chronic.parse(strong_params[attribute])
8
- return nil unless parsed_value
9
- strong_params[attribute] = parsed_value.to_date
13
+ begin
14
+ strong_params[attribute] = Date.iso8601(strong_params[attribute])
15
+ rescue ArgumentError
16
+ parsed_value = Chronic.parse(strong_params[attribute])
17
+ return nil unless parsed_value
18
+ strong_params[attribute] = parsed_value.to_date
19
+ end
10
20
  end
11
21
  end
12
22
  end
@@ -3,14 +3,18 @@ require("daterangepicker/daterangepicker.css");
3
3
 
4
4
  // requires jQuery, moment, might want to consider a vanilla JS alternative
5
5
  import 'daterangepicker';
6
+ import moment from 'moment-timezone'
6
7
 
7
8
  export default class extends Controller {
8
- static targets = [ "field", "clearButton", "currentTimeZoneWrapper", "timeZoneButtons", "timeZoneSelectWrapper", "timeZoneField" ]
9
+ static targets = [ "field", "displayField", "clearButton", "currentTimeZoneWrapper", "timeZoneButtons", "timeZoneSelectWrapper", "timeZoneField", "timeZoneSelect" ]
9
10
  static values = {
10
11
  includeTime: Boolean,
11
12
  defaultTimeZones: Array,
12
- cancelButtonLabel: { type: String, default: "Cancel" },
13
- applyButtonLabel: { type: String, default: "Apply" }
13
+ dateFormat: String,
14
+ timeFormat: String,
15
+ currentTimeZone: String,
16
+ isAmPm: Boolean,
17
+ pickerLocale: { type: Object, default: {} }
14
18
  }
15
19
 
16
20
  connect() {
@@ -26,13 +30,33 @@ export default class extends Controller {
26
30
  event.preventDefault()
27
31
 
28
32
  $(this.fieldTarget).val('')
33
+ $(this.displayFieldTarget).val('')
34
+ }
35
+
36
+ currentTimeZone(){
37
+ return (
38
+ ( this.hasTimeZoneSelectWrapperTarget && $(this.timeZoneSelectWrapperTarget).is(":visible") && this.timeZoneSelectTarget.value ) ||
39
+ ( this.hasTimeZoneFieldTarget && this.timeZoneFieldTarget.value ) ||
40
+ this.currentTimeZoneValue
41
+ )
29
42
  }
30
43
 
31
44
  applyDateToField(event, picker) {
32
- const format = this.includeTimeValue ? 'MM/DD/YYYY h:mm A' : 'MM/DD/YYYY'
33
- $(this.fieldTarget).val(picker.startDate.format(format))
45
+ const format = this.includeTimeValue ? this.timeFormatValue : this.dateFormatValue
46
+ const newTimeZone = this.currentTimeZone()
47
+ const momentVal = (
48
+ picker ?
49
+ moment(picker.startDate.toISOString()).tz(newTimeZone, true) :
50
+ moment.tz(moment(this.fieldTarget.value, "YYYY-MM-DDTHH:mm").format("YYYY-MM-DDTHH:mm"), newTimeZone)
51
+ )
52
+ const displayVal = momentVal.format(format)
53
+ const dataVal = this.includeTimeValue ? momentVal.toISOString(true) : momentVal.format('YYYY-MM-DD')
54
+ $(this.displayFieldTarget).val(displayVal)
55
+ $(this.fieldTarget).val(dataVal)
34
56
  // bubble up a change event when the input is updated for other listeners
35
- this.fieldTarget.dispatchEvent(new CustomEvent('change', { detail: { picker: picker }}))
57
+ if(picker){
58
+ this.displayFieldTarget.dispatchEvent(new CustomEvent('change', { detail: { picker: picker }}))
59
+ }
36
60
  }
37
61
 
38
62
  showTimeZoneButtons(event) {
@@ -43,15 +67,18 @@ export default class extends Controller {
43
67
  $(this.timeZoneButtonsTarget).toggleClass('hidden')
44
68
  }
45
69
 
70
+ // triggered on other click from the timezone buttons
46
71
  showTimeZoneSelectWrapper(event) {
47
72
  // don't follow the anchor
48
73
  event.preventDefault()
49
74
 
50
75
  $(this.timeZoneButtonsTarget).toggleClass('hidden')
51
-
52
76
  if (this.hasTimeZoneSelectWrapperTarget) {
53
77
  $(this.timeZoneSelectWrapperTarget).toggleClass('hidden')
54
78
  }
79
+ if(!["", null].includes(this.fieldTarget.value)){
80
+ $(this.displayFieldTarget).trigger("apply.daterangepicker");
81
+ }
55
82
  }
56
83
 
57
84
  resetTimeZoneUI(e) {
@@ -65,39 +92,70 @@ export default class extends Controller {
65
92
  }
66
93
  }
67
94
 
95
+ // triggered on selecting a new timezone using the buttons
68
96
  setTimeZone(event) {
69
97
  // don't follow the anchor
70
98
  event.preventDefault()
71
-
72
99
  const currentTimeZoneEl = this.currentTimeZoneWrapperTarget.querySelector('a')
73
- const {value} = event.target.dataset
74
-
75
- $(this.timeZoneFieldTarget).val(value)
76
- $(currentTimeZoneEl).text(value)
77
-
100
+ $(this.timeZoneFieldTarget).val(event.target.dataset.value)
101
+ $(currentTimeZoneEl).text(event.target.dataset.label)
78
102
  $('.time-zone-button').removeClass('button').addClass('button-alternative')
79
103
  $(event.target).removeClass('button-alternative').addClass('button')
104
+ this.resetTimeZoneUI()
105
+ if(!["", null].includes(this.fieldTarget.value)){
106
+ $(this.displayFieldTarget).trigger("apply.daterangepicker");
107
+ }
108
+ }
109
+
110
+ // triggered on selecting a new timezone from the timezone picker
111
+ selectTimeZoneChange(event) {
112
+ if(!["", null].includes(this.fieldTarget.value)){
113
+ $(this.displayFieldTarget).trigger("apply.daterangepicker");
114
+ }
115
+ }
80
116
 
117
+ // triggered on cancel click from the timezone picker
118
+ cancelSelect(event) {
119
+ event.preventDefault()
81
120
  this.resetTimeZoneUI()
121
+ if(!["", null].includes(this.fieldTarget.value)){
122
+ $(this.displayFieldTarget).trigger("apply.daterangepicker")
123
+ }
124
+ }
125
+
126
+ displayFieldChange(event) {
127
+ const newTimeZone = this.currentTimeZone()
128
+ const format = this.includeTimeValue ? this.timeFormatValue : this.dateFormatValue
129
+ const momentParsed = moment(this.displayFieldTarget.value, format, false)
130
+ if(momentParsed.isValid()){
131
+ const momentVal = moment.tz(momentParsed.format("YYYY-MM-DDTHH:mm"), newTimeZone)
132
+ const dataVal = this.includeTimeValue ? momentVal.toISOString(true) : momentVal.format('YYYY-MM-DD')
133
+ $(this.fieldTarget).val(dataVal)
134
+ } else {
135
+ // nullify field value when the display format is wrong
136
+ $(this.fieldTarget).val("")
137
+ }
82
138
  }
83
139
 
84
140
  initPluginInstance() {
85
- $(this.fieldTarget).daterangepicker({
141
+ const localeValues = this.pickerLocaleValue
142
+ const isAmPm = this.isAmPmValue
143
+ localeValues['format'] = this.includeTimeValue ? this.timeFormatValue : this.dateFormatValue
144
+
145
+ $(this.displayFieldTarget).daterangepicker({
86
146
  singleDatePicker: true,
87
147
  timePicker: this.includeTimeValue,
88
148
  timePickerIncrement: 5,
89
149
  autoUpdateInput: false,
90
- locale: {
91
- cancelLabel: this.cancelButtonLabelValue,
92
- applyLabel: this.applyButtonLabelValue,
93
- format: this.includeTimeValue ? 'MM/DD/YYYY h:mm A' : 'MM/DD/YYYY'
94
- }
150
+ locale: localeValues,
151
+ timePicker24Hour: !isAmPm,
95
152
  })
96
153
 
97
- $(this.fieldTarget).on('apply.daterangepicker', this.applyDateToField.bind(this))
98
- $(this.fieldTarget).on('cancel.daterangepicker', this.clearDate.bind(this))
154
+ $(this.displayFieldTarget).on('apply.daterangepicker', this.applyDateToField.bind(this))
155
+ $(this.displayFieldTarget).on('cancel.daterangepicker', this.clearDate.bind(this))
156
+ $(this.displayFieldTarget).on('input', this,this.displayFieldChange.bind(this));
99
157
 
100
- this.pluginMainEl = this.fieldTarget
158
+ this.pluginMainEl = this.displayFieldTarget
101
159
  this.plugin = $(this.pluginMainEl).data('daterangepicker') // weird
102
160
 
103
161
  // Init time zone select
@@ -113,7 +171,6 @@ export default class extends Controller {
113
171
  $(this.timeZoneSelect).on('change.select2', function(event) {
114
172
  const currentTimeZoneEl = self.currentTimeZoneWrapperTarget.querySelector('a')
115
173
  const {value} = event.target
116
-
117
174
  $(self.timeZoneFieldTarget).val(value)
118
175
  $(currentTimeZoneEl).text(value)
119
176
 
@@ -126,7 +183,6 @@ export default class extends Controller {
126
183
  } else {
127
184
  // deselect any selected button
128
185
  $('.time-zone-button').removeClass('button').addClass('button-alternative')
129
-
130
186
  selectedOptionTimeZoneButton.text(value)
131
187
  selectedOptionTimeZoneButton.attr('data-value', value).removeAttr('hidden')
132
188
  selectedOptionTimeZoneButton.removeClass(['hidden', 'button-alternative']).addClass('button')
@@ -139,10 +195,8 @@ export default class extends Controller {
139
195
 
140
196
  teardownPluginInstance() {
141
197
  if (this.plugin === undefined) { return }
142
-
143
198
  $(this.pluginMainEl).off('apply.daterangepicker')
144
199
  $(this.pluginMainEl).off('cancel.daterangepicker')
145
-
146
200
  // revert to original markup, remove any event listeners
147
201
  this.plugin.remove()
148
202
 
@@ -1,5 +1,5 @@
1
1
  module BulletTrain
2
2
  module Fields
3
- VERSION = "1.3.1"
3
+ VERSION = "1.3.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bullet_train-fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Culver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-16 00:00:00.000000000 Z
11
+ date: 2023-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: standard