active_admin_date_range_preset 0.4.0 → 1.0.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.
- checksums.yaml +4 -4
- data/README.md +197 -124
- data/config/importmap.rb +2 -0
- data/lib/active_admin_date_range_preset/version.rb +1 -1
- data/lib/active_admin_date_range_preset.rb +22 -3
- data/vendor/assets/javascripts/active_admin_date_range_preset.js +292 -220
- data/vendor/assets/stylesheets/active_admin_date_range_preset.css +63 -0
- metadata +11 -4
- data/vendor/assets/stylesheets/active_admin_date_range_preset.scss +0 -50
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d3cae599dda0f084558bdfe20d9521b54a3cc21c0fce528268319ad244013987
|
|
4
|
+
data.tar.gz: 23c046e1ab9f57b901b0cd7f3beba397043dc4c22b5fab646d725b49e7d0a903
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5323afd071fb10ffcc929276b046ea7ebb7453328f613e3d15bb11aa738d7be2c086abf7c5afa5711e15bcbc757d1b68b076af37165831acb68d1d6bc9e4019a
|
|
7
|
+
data.tar.gz: e9ee89a404faaf6c11d4454b45b5957618b4e80d946acbd0fc52bb3b5082112515b90fc35a348682a9015a0c878f21af6348acb8030075ba7c3062fea708e342
|
data/README.md
CHANGED
|
@@ -12,24 +12,69 @@ This is how it looks like
|
|
|
12
12
|
<img width="1008" height="942" alt="combined" src="https://github.com/user-attachments/assets/cbe22410-078e-4f2b-894b-4457c13e3e22" />
|
|
13
13
|
|
|
14
14
|
|
|
15
|
+
|
|
15
16
|
## Compatibility
|
|
16
17
|
|
|
17
|
-
Active Admin 3.2 or newer,
|
|
18
|
-
newer. CI covers Active Admin 3.2 and 3.5 on Ruby 3.3, 3.4 and 4.0
|
|
19
|
-
Rails 8.0 and 8.1
|
|
18
|
+
Active Admin 3.2 or newer, including Active Admin 4. Rails 8.0 or newer, Ruby
|
|
19
|
+
3.3 or newer. CI covers Active Admin 3.2 and 3.5 on Ruby 3.3, 3.4 and 4.0
|
|
20
|
+
against Rails 8.0 and 8.1, and Active Admin 4.0.0.beta23 on Rails 8.0 and 8.1.
|
|
21
|
+
|
|
22
|
+
The plugin is one plain JavaScript file with no jQuery. Active Admin 3 loads it
|
|
23
|
+
through Sprockets, Active Admin 4 as an ES module from its import map. Both get
|
|
24
|
+
the same API, `window.ActiveAdminDateRangePreset`.
|
|
20
25
|
|
|
21
|
-
Version 0.4.0 raised the Rails floor from 7.1 to 8.0.
|
|
22
|
-
|
|
23
|
-
is tested. Stay on 0.3.x if you are still on Rails 7.
|
|
26
|
+
Version 0.4.0 raised the Rails floor from 7.1 to 8.0. Stay on 0.3.x if you are
|
|
27
|
+
still on Rails 7.
|
|
24
28
|
|
|
25
29
|
The Rails and Ruby floors are declared explicitly because Active Admin 3.2
|
|
26
30
|
still allows `railties >= 6.1` and Ruby `>= 2.6`, both long past end of life
|
|
27
31
|
and neither tested here.
|
|
28
32
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
## Upgrading from 0.x
|
|
34
|
+
|
|
35
|
+
The jQuery plugin `$.fn.date_range_ext_preset` is gone. Replace each call with
|
|
36
|
+
`ActiveAdminDateRangePreset.init`, which takes a CSS selector (or an element,
|
|
37
|
+
or a list of elements) and the same options:
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
// before
|
|
41
|
+
$('form.filter_form div.filter_date_range').date_range_ext_preset({ show_time: true });
|
|
42
|
+
$.fn.date_range_ext_preset.defaults.hours_offset = 3;
|
|
43
|
+
|
|
44
|
+
// after
|
|
45
|
+
document.addEventListener('DOMContentLoaded', function () {
|
|
46
|
+
ActiveAdminDateRangePreset.init('form.filter_form div.filter_date_range', { show_time: true });
|
|
47
|
+
});
|
|
48
|
+
ActiveAdminDateRangePreset.defaults.hours_offset = 3;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Every option keeps its name and meaning, `show_time` included. A call that
|
|
52
|
+
turned time on for some inputs becomes:
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
// before
|
|
56
|
+
$('.my_pairs').date_range_ext_preset({ show_time: true });
|
|
57
|
+
|
|
58
|
+
// after
|
|
59
|
+
document.addEventListener('DOMContentLoaded', function () {
|
|
60
|
+
ActiveAdminDateRangePreset.init('.my_pairs', { show_time: true });
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
To turn it on for every pair, including the ones set up automatically, set
|
|
65
|
+
the default instead:
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
ActiveAdminDateRangePreset.defaults.show_time = true;
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`show_time` still affects text inputs only. Native `date` and `datetime-local`
|
|
72
|
+
inputs get the format their `type` requires.
|
|
73
|
+
|
|
74
|
+
`gteq_input` and `lteq_input` are now CSS selectors or elements instead of
|
|
75
|
+
jQuery selectors. The markup and CSS classes are unchanged. The stylesheet is
|
|
76
|
+
plain CSS now, so `@import "active_admin_date_range_preset";` in
|
|
77
|
+
`active_admin.scss` keeps working.
|
|
33
78
|
|
|
34
79
|
## Installation
|
|
35
80
|
|
|
@@ -43,162 +88,173 @@ And then execute:
|
|
|
43
88
|
|
|
44
89
|
$ bundle install
|
|
45
90
|
|
|
46
|
-
|
|
91
|
+
### Active Admin 3 (Sprockets)
|
|
47
92
|
|
|
48
|
-
|
|
93
|
+
In `app/assets/javascripts/active_admin.js`:
|
|
49
94
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
```//= require active_admin_date_range_preset```
|
|
54
|
-
|
|
55
|
-
CSS
|
|
56
|
-
```@import "active_admin_date_range_preset";```
|
|
95
|
+
```javascript
|
|
96
|
+
//= require active_admin_date_range_preset
|
|
97
|
+
```
|
|
57
98
|
|
|
58
|
-
|
|
99
|
+
In `app/assets/stylesheets/active_admin.scss`:
|
|
59
100
|
|
|
60
|
-
|
|
101
|
+
```scss
|
|
102
|
+
@import "active_admin_date_range_preset";
|
|
103
|
+
```
|
|
61
104
|
|
|
62
|
-
|
|
105
|
+
### Active Admin 4 (import map)
|
|
63
106
|
|
|
64
|
-
|
|
107
|
+
The gem pins the module `active_admin_date_range_preset` into
|
|
108
|
+
`ActiveAdmin.importmap`. If your Active Admin has the `javascript_modules`
|
|
109
|
+
setting, the gem adds itself to it and pages import it automatically.
|
|
110
|
+
Otherwise, override `app/views/active_admin/_html_head.html.erb` (copy it from
|
|
111
|
+
Active Admin) and add after the `javascript_importmap_tags` line:
|
|
65
112
|
|
|
66
|
-
|
|
113
|
+
```erb
|
|
114
|
+
<%= javascript_import_module_tag "active_admin_date_range_preset" %>
|
|
115
|
+
```
|
|
67
116
|
|
|
68
|
-
|
|
117
|
+
If you use active_admin_theme 4, add the module to its list in an initializer
|
|
118
|
+
instead:
|
|
69
119
|
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
"@activeadmin-plugins/active_admin_date_range_preset": "0.3.1"
|
|
73
|
-
}
|
|
120
|
+
```ruby
|
|
121
|
+
ActiveAdminTheme.javascripts += %w[active_admin_date_range_preset]
|
|
74
122
|
```
|
|
75
|
-
and execute:
|
|
76
123
|
|
|
77
|
-
|
|
124
|
+
For the CSS, either link the stylesheet in the same `_html_head` override
|
|
125
|
+
(Propshaft serves it from the gem):
|
|
78
126
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
```javascript
|
|
82
|
-
import '@activeadmin-plugins/active_admin_date_range_preset';
|
|
127
|
+
```erb
|
|
128
|
+
<%= stylesheet_link_tag "active_admin_date_range_preset" %>
|
|
83
129
|
```
|
|
84
130
|
|
|
85
|
-
|
|
131
|
+
or import it into the Tailwind entry that builds `active_admin.css`, from the
|
|
132
|
+
npm package or from the gem's path (`bundle show active_admin_date_range_preset`):
|
|
86
133
|
|
|
87
134
|
```css
|
|
88
|
-
@import
|
|
135
|
+
@import "@activeadmin-plugins/active_admin_date_range_preset";
|
|
89
136
|
```
|
|
90
137
|
|
|
91
|
-
|
|
138
|
+
The menu follows Active Admin 4's dark mode (the `.dark` class on `<html>`).
|
|
92
139
|
|
|
93
|
-
|
|
140
|
+
### npm package
|
|
94
141
|
|
|
95
|
-
|
|
96
|
-
It is generated automatically on the first run (or via `bundle exec rake setup`) into
|
|
97
|
-
`spec/rails/rails-<version>/`, which is gitignored.
|
|
142
|
+
$ npm i @activeadmin-plugins/active_admin_date_range_preset
|
|
98
143
|
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
bundle exec rspec spec
|
|
144
|
+
```javascript
|
|
145
|
+
import '@activeadmin-plugins/active_admin_date_range_preset';
|
|
102
146
|
```
|
|
103
147
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
```sh
|
|
107
|
-
RAILS=8.0.0 AA=3.5.0 bundle install
|
|
108
|
-
RAILS=8.0.0 AA=3.5.0 bundle exec rspec spec
|
|
148
|
+
```css
|
|
149
|
+
@import '@activeadmin-plugins/active_admin_date_range_preset';
|
|
109
150
|
```
|
|
110
151
|
|
|
111
152
|
## Usage
|
|
112
153
|
|
|
113
|
-
|
|
154
|
+
### Forms
|
|
155
|
+
|
|
156
|
+
Put `datetime_preset_pair` on the wrapper of the first input of a pair. The
|
|
157
|
+
next input is the end of the range. Pairs with this class are set up on page
|
|
158
|
+
load.
|
|
114
159
|
|
|
115
160
|
```ruby
|
|
116
|
-
f.input :date_from, as: :
|
|
117
|
-
f.input :date_to, as: :
|
|
161
|
+
f.input :date_from, as: :string, wrapper_html: { class: 'datetime_preset_pair', data: { show_time: 'true' } }
|
|
162
|
+
f.input :date_to, as: :string
|
|
118
163
|
```
|
|
119
164
|
|
|
120
|
-
|
|
121
|
-
Main point is to set for first input-pair wrapper-class
|
|
165
|
+
The value format follows the input's `type`:
|
|
122
166
|
|
|
123
|
-
|
|
167
|
+
| Input | Value |
|
|
168
|
+
| --- | --- |
|
|
169
|
+
| `type="date"` | `2015-06-10` |
|
|
170
|
+
| `type="datetime-local"` | `2015-06-10T00:00`, with `:SS` when its `step` allows seconds |
|
|
171
|
+
| text | `2015-06-10`, or `2015-06-10 00:00:00` with `show_time` |
|
|
124
172
|
|
|
125
|
-
|
|
173
|
+
`data-show-time` overrides `show_time` for that pair, in both directions.
|
|
126
174
|
|
|
127
|
-
|
|
175
|
+
### Filters
|
|
128
176
|
|
|
129
|
-
|
|
177
|
+
A wrapper that holds both inputs uses its first and last input. Add the class
|
|
178
|
+
to a date_range filter:
|
|
130
179
|
|
|
180
|
+
```ruby
|
|
181
|
+
filter :created_at, as: :date_range, wrapper_html: { class: 'datetime_preset_pair' }
|
|
182
|
+
```
|
|
131
183
|
|
|
132
|
-
|
|
184
|
+
Or set up every date_range filter from JavaScript:
|
|
133
185
|
|
|
134
|
-
|
|
135
|
-
|
|
186
|
+
```javascript
|
|
187
|
+
// Active Admin 3
|
|
188
|
+
ActiveAdminDateRangePreset.init('form.filter_form div.filter_date_range');
|
|
189
|
+
// Active Admin 4
|
|
190
|
+
ActiveAdminDateRangePreset.init('form.filters-form div.date_range');
|
|
191
|
+
```
|
|
136
192
|
|
|
137
|
-
|
|
193
|
+
On Active Admin 4, put that call in a module script in your `_html_head`
|
|
194
|
+
override, after the import:
|
|
138
195
|
|
|
139
|
-
```
|
|
140
|
-
|
|
196
|
+
```erb
|
|
197
|
+
<script type="module">
|
|
198
|
+
import "active_admin_date_range_preset";
|
|
199
|
+
ActiveAdminDateRangePreset.init("form.filters-form div.date_range");
|
|
200
|
+
</script>
|
|
141
201
|
```
|
|
142
202
|
|
|
143
|
-
|
|
203
|
+
Active Admin 4's own date_range filter renders native date inputs, so the
|
|
204
|
+
presets write `YYYY-MM-DD` there.
|
|
144
205
|
|
|
145
|
-
|
|
206
|
+
### With active_admin_datetimepicker
|
|
207
|
+
|
|
208
|
+
[active_admin_datetimepicker](https://github.com/activeadmin-plugins/active_admin_datetimepicker)
|
|
209
|
+
range filters (`filter :time_start, as: :date_time_range`) render as
|
|
210
|
+
`div.filter_date_time_range` on Active Admin 3 and 4. Init them by that
|
|
211
|
+
selector, the same way on both:
|
|
146
212
|
|
|
147
213
|
```javascript
|
|
148
|
-
|
|
149
|
-
$('form.filter_form div.filter_date_time_range').date_range_ext_preset();
|
|
150
|
-
});
|
|
214
|
+
ActiveAdminDateRangePreset.init('form div.filter_date_time_range');
|
|
151
215
|
```
|
|
152
216
|
|
|
153
|
-
|
|
217
|
+
On Active Admin 4 their inputs are `datetime-local` (or `date` for a date
|
|
218
|
+
column), so the presets write `YYYY-MM-DDTHH:MM` (or `YYYY-MM-DD`). In forms,
|
|
219
|
+
put `datetime_preset_pair` on the first picker's `wrapper_html` as for any
|
|
220
|
+
other pair.
|
|
154
221
|
|
|
222
|
+
### Custom inputs
|
|
155
223
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
You can assign "Set range" almost to any input-text-pair filters/forms.
|
|
159
|
-
For example, you have complex form where input-pairs are not close to each other and not standard.
|
|
224
|
+
The link goes into the first `label` of the element you init. When the inputs
|
|
225
|
+
are elsewhere on the page, point at them:
|
|
160
226
|
|
|
161
227
|
```javascript
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
gteq_input: '.jquery_selector_to_second_input'
|
|
166
|
-
});
|
|
228
|
+
ActiveAdminDateRangePreset.init('.any_selector', {
|
|
229
|
+
gteq_input: '#date_from',
|
|
230
|
+
lteq_input: '#date_to'
|
|
167
231
|
});
|
|
168
232
|
```
|
|
169
|
-
".any_jquery_selector" is pointed to place where button "Set range" will appear.
|
|
170
|
-
Set lteq_input and gteq_input to point to inputs if they not near main selector.
|
|
171
233
|
|
|
234
|
+
Calling `init` again on the same element replaces its setup, so a call with
|
|
235
|
+
options after the automatic one wins.
|
|
172
236
|
|
|
173
|
-
|
|
237
|
+
### Test hooks
|
|
174
238
|
|
|
175
|
-
|
|
239
|
+
The "Set range" link carries `data-date-range-preset-toggle`, the menu
|
|
240
|
+
`data-date-range-preset-menu`, and each preset `data-date-range-preset` with its
|
|
241
|
+
key: `today`, `yesterday`, `week`, `month`, `last_week`, `last_month`, then
|
|
242
|
+
`custom_0`, `custom_1`, ... for `add_range` entries.
|
|
176
243
|
|
|
177
|
-
|
|
244
|
+
## Settings
|
|
178
245
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
});
|
|
183
|
-
```
|
|
184
|
-
Example how to set global settings. Write it before the `$(function () { ... })` block
|
|
246
|
+
Pass settings to `init` for some elements, or set them for every later `init`
|
|
247
|
+
on `ActiveAdminDateRangePreset.defaults`. Defaults are read each time a menu
|
|
248
|
+
opens, so they apply to pairs that were set up before you changed them.
|
|
185
249
|
|
|
186
250
|
```javascript
|
|
187
|
-
|
|
188
|
-
|
|
251
|
+
// End date is the same day, not the next one.
|
|
252
|
+
// Today true : 2015-06-12 - 2015-06-12
|
|
253
|
+
// Today false: 2015-06-12 - 2015-06-13
|
|
254
|
+
ActiveAdminDateRangePreset.defaults.date_to_human_readable = true;
|
|
189
255
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
```javascript
|
|
193
|
-
# End date will be full-day, not next.
|
|
194
|
-
# Today true : 2015-06-12 - 2015-06-12
|
|
195
|
-
# Today false: 2015-06-12 - 2015-06-13
|
|
196
|
-
$.fn.date_range_ext_preset.defaults.date_to_human_readable = true
|
|
197
|
-
|
|
198
|
-
# Display time
|
|
199
|
-
# Today: 2015-06-12 00:00:00 - 2015-06-13 00:00:00
|
|
200
|
-
# Today with human_readable=true: 2015-06-12 00:00:00 - 2015-16-12 23:59:59
|
|
201
|
-
$.fn.date_range_ext_preset.defaults.show_time = true
|
|
256
|
+
// Text inputs get the time too: 2015-06-12 00:00:00 - 2015-06-13 00:00:00
|
|
257
|
+
ActiveAdminDateRangePreset.defaults.show_time = true;
|
|
202
258
|
```
|
|
203
259
|
|
|
204
260
|
### date_to_human_readable
|
|
@@ -221,7 +277,8 @@ value: true/false
|
|
|
221
277
|
|
|
222
278
|
default: false
|
|
223
279
|
|
|
224
|
-
If true
|
|
280
|
+
If true, text inputs get date and time, usually 00:00:00. Native inputs ignore
|
|
281
|
+
it: their `type` decides.
|
|
225
282
|
|
|
226
283
|
### hours_offset
|
|
227
284
|
|
|
@@ -231,29 +288,45 @@ default: 0
|
|
|
231
288
|
|
|
232
289
|
To work correctly this plugin needs to detect current date-time. And it uses UTC. But if you need your local timezone or some other time-shift, your can set this option:
|
|
233
290
|
|
|
234
|
-
Example:
|
|
235
291
|
```javascript
|
|
236
|
-
|
|
292
|
+
ActiveAdminDateRangePreset.defaults.hours_offset = +3;
|
|
237
293
|
// or
|
|
238
|
-
|
|
294
|
+
ActiveAdminDateRangePreset.defaults.hours_offset = -3;
|
|
239
295
|
```
|
|
240
296
|
|
|
241
|
-
###
|
|
297
|
+
### add_range
|
|
242
298
|
|
|
243
299
|
```javascript
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
]
|
|
256
|
-
});
|
|
257
|
-
|
|
300
|
+
ActiveAdminDateRangePreset.init('form.filter_form div.filter_date_range', {
|
|
301
|
+
date_to_human_readable: true, // affects the end date
|
|
302
|
+
add_range: [
|
|
303
|
+
{
|
|
304
|
+
title: 'Last 30 days',
|
|
305
|
+
// date_to_human_readable moves the end date back a second, so end tomorrow:
|
|
306
|
+
start: new Date(new Date().setDate(new Date().getDate() - 29)),
|
|
307
|
+
end: new Date(new Date().setDate(new Date().getDate() + 1))
|
|
308
|
+
}
|
|
309
|
+
]
|
|
258
310
|
});
|
|
259
311
|
```
|
|
312
|
+
|
|
313
|
+
## Development
|
|
314
|
+
|
|
315
|
+
The specs run against a throwaway Rails app built by `spec/support/rails_template.rb`.
|
|
316
|
+
It is generated automatically on the first run (or via `bundle exec rake setup`) into
|
|
317
|
+
`spec/rails/rails-<version>-aa<version>/`, which is gitignored.
|
|
318
|
+
|
|
319
|
+
```sh
|
|
320
|
+
bundle install
|
|
321
|
+
bundle exec rspec spec
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Pick the versions under test with env vars. `AA=4.x` switches the bundle from
|
|
325
|
+
Sprockets to Propshaft, importmap-rails and Tailwind:
|
|
326
|
+
|
|
327
|
+
```sh
|
|
328
|
+
RAILS=8.0.0 AA=3.5.0 bundle install
|
|
329
|
+
RAILS=8.0.0 AA=3.5.0 bundle exec rspec spec
|
|
330
|
+
AA=4.0.0.beta23 bundle install
|
|
331
|
+
AA=4.0.0.beta23 bundle exec rspec spec
|
|
332
|
+
```
|
data/config/importmap.rb
ADDED
|
@@ -2,10 +2,29 @@ require "activeadmin"
|
|
|
2
2
|
require "active_admin_date_range_preset/version"
|
|
3
3
|
|
|
4
4
|
module ActiveAdminDateRangePreset
|
|
5
|
-
|
|
6
|
-
class Engine < ::Rails::Engine
|
|
5
|
+
JAVASCRIPT_MODULE = "active_admin_date_range_preset".freeze
|
|
7
6
|
|
|
7
|
+
def self.aa_v4?
|
|
8
|
+
Gem::Version.new(ActiveAdmin::VERSION) >= Gem::Version.new("4.0.0.beta1")
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module Rails
|
|
12
|
+
class Engine < ::Rails::Engine
|
|
13
|
+
# Active Admin 4 renders its own import map, so the module is pinned
|
|
14
|
+
# there. Propshaft serves it from vendor/assets/javascripts, which every
|
|
15
|
+
# engine adds to the asset paths.
|
|
16
|
+
initializer "active_admin_date_range_preset.importmap", after: "active_admin.importmap" do |app|
|
|
17
|
+
next unless ActiveAdminDateRangePreset.aa_v4? && app.config.respond_to?(:importmap)
|
|
18
|
+
|
|
19
|
+
ActiveAdmin.importmap.draw(root.join("config", "importmap.rb"))
|
|
8
20
|
end
|
|
9
|
-
end
|
|
10
21
|
|
|
22
|
+
# javascript_modules is pending upstream (activeadmin/activeadmin#9148).
|
|
23
|
+
config.after_initialize do
|
|
24
|
+
if ActiveAdmin.application.respond_to?(:javascript_modules)
|
|
25
|
+
ActiveAdmin.application.javascript_modules |= [JAVASCRIPT_MODULE]
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
11
30
|
end
|
|
@@ -1,245 +1,317 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
$.fn.date_range_ext_preset = function(options) {
|
|
7
|
-
// settings
|
|
8
|
-
options = options || {};
|
|
9
|
-
let opts = $.extend({}, $.fn.date_range_ext_preset.defaults, options);
|
|
10
|
-
|
|
11
|
-
// aditional functions
|
|
12
|
-
function num_with_leading_zero(num, digitsCount = 2) {
|
|
13
|
-
let s = num + '';
|
|
14
|
-
while (s.length < digitsCount) {
|
|
15
|
-
s = '0' + s;
|
|
16
|
-
}
|
|
17
|
-
return s;
|
|
18
|
-
}
|
|
1
|
+
// Plain script with no imports or exports, so the same file runs as a classic
|
|
2
|
+
// script (Sprockets on Active Admin 3) and as an ES module (importmap on
|
|
3
|
+
// Active Admin 4). Either way the API is window.ActiveAdminDateRangePreset.
|
|
4
|
+
(function () {
|
|
5
|
+
if (window.ActiveAdminDateRangePreset) { return; }
|
|
19
6
|
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
let str = date.getFullYear() + '-' + num_with_leading_zero(date.getMonth()+1) + '-' + num_with_leading_zero(date.getDate());
|
|
24
|
-
if (el_opts.show_time) {
|
|
25
|
-
str += (' ' + num_with_leading_zero(date.getHours()) + ':' + num_with_leading_zero(date.getMinutes()) + ':' + num_with_leading_zero(date.getSeconds()));
|
|
26
|
-
}
|
|
27
|
-
return str;
|
|
28
|
-
}
|
|
7
|
+
var defaults = {
|
|
8
|
+
// Manual global time shift, from UTC can be +/- number
|
|
9
|
+
hours_offset: 0,
|
|
29
10
|
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
11
|
+
// date_to_human_readable = true, then "date_to" consider as including full day without last second
|
|
12
|
+
// For example Today will be:
|
|
13
|
+
// true
|
|
14
|
+
// 2015-06-10 - 2015-06-10
|
|
15
|
+
// 2015-06-10 00:00:00 - 2015-06-10 23:59:59
|
|
16
|
+
// false
|
|
17
|
+
// 2015-06-10 - 2015-06-11
|
|
18
|
+
// 2015-06-10 00:00:00 - 2015-06-11 00:00:00
|
|
19
|
+
date_to_human_readable: false,
|
|
36
20
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
21
|
+
// Display time or not on text inputs: 2015-06-10 vs 2015-06-10 00:00:00
|
|
22
|
+
show_time: false,
|
|
23
|
+
|
|
24
|
+
// Array of additional ranges: { title: 'Last 30 days', start: Date, end: Date }
|
|
25
|
+
add_range: []
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
var PRESETS = [
|
|
29
|
+
{ key: 'today', title: 'Today', range: function (now) {
|
|
30
|
+
var start = day(now, 0);
|
|
31
|
+
return [start, day(start, 1)];
|
|
32
|
+
} },
|
|
33
|
+
{ key: 'yesterday', title: 'Yesterday', range: function (now) {
|
|
34
|
+
return [day(now, -1), day(now, 0)];
|
|
35
|
+
} },
|
|
36
|
+
{ key: 'week', title: 'This Week', range: function (now) {
|
|
37
|
+
var start = day(now, -daysSinceMonday(now));
|
|
38
|
+
return [start, day(start, 7)];
|
|
39
|
+
} },
|
|
40
|
+
{ key: 'month', title: 'This Month', range: function (now) {
|
|
41
|
+
return [
|
|
42
|
+
new Date(now.getFullYear(), now.getMonth(), 1),
|
|
43
|
+
new Date(now.getFullYear(), now.getMonth() + 1, 1)
|
|
44
|
+
];
|
|
45
|
+
} },
|
|
46
|
+
{ key: 'last_week', title: 'Last Week', range: function (now) {
|
|
47
|
+
var end = day(now, -daysSinceMonday(now));
|
|
48
|
+
return [day(end, -7), end];
|
|
49
|
+
} },
|
|
50
|
+
{ key: 'last_month', title: 'Last Month', range: function (now) {
|
|
51
|
+
return [
|
|
52
|
+
new Date(now.getFullYear(), now.getMonth() - 1, 1),
|
|
53
|
+
new Date(now.getFullYear(), now.getMonth(), 1)
|
|
54
|
+
];
|
|
55
|
+
} }
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
function day(date, shift) {
|
|
59
|
+
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + shift);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// getDay() counts from Sunday, so a plain `- getDay() + 1` lands on
|
|
63
|
+
// tomorrow for Sundays and hands back the week that has not started yet.
|
|
64
|
+
function daysSinceMonday(date) {
|
|
65
|
+
return (date.getDay() + 6) % 7;
|
|
66
|
+
}
|
|
41
67
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
68
|
+
function pad(num) {
|
|
69
|
+
return (num < 10 ? '0' : '') + num;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Native date inputs only accept ISO values, so the input's type picks the
|
|
73
|
+
// format; text inputs keep the format the plugin has always written.
|
|
74
|
+
function formatFor(input, date, opts) {
|
|
75
|
+
var ymd = date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate());
|
|
76
|
+
var hm = pad(date.getHours()) + ':' + pad(date.getMinutes());
|
|
77
|
+
var hms = hm + ':' + pad(date.getSeconds());
|
|
78
|
+
if (input.type === 'date') { return ymd; }
|
|
79
|
+
if (input.type === 'datetime-local') { return ymd + 'T' + (allowsSeconds(input) ? hms : hm); }
|
|
80
|
+
return opts.show_time ? ymd + ' ' + hms : ymd;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The default step of a datetime-local input is 60 seconds, and a value with
|
|
84
|
+
// seconds then fails validation and blocks the form submit.
|
|
85
|
+
function allowsSeconds(input) {
|
|
86
|
+
var step = input.getAttribute('step');
|
|
87
|
+
return step === 'any' || (step !== null && Number(step) % 60 !== 0);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// "Now" in UTC shifted by hours_offset, as a local Date whose fields hold
|
|
91
|
+
// those wall-clock values.
|
|
92
|
+
function shiftedNow(opts) {
|
|
93
|
+
var now = new Date();
|
|
94
|
+
var utc = new Date(
|
|
46
95
|
now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
|
|
47
96
|
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(),
|
|
48
97
|
now.getUTCMilliseconds()
|
|
49
98
|
);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// PLUGIN BEGIN
|
|
54
|
-
return this.each(function(i, el) {
|
|
55
|
-
let $this = $(el);
|
|
56
|
-
|
|
57
|
-
// Re-initialising an element replaces its wiring instead of adding to
|
|
58
|
-
// it. The auto-init below was dead from jQuery 3 onwards, so hosts
|
|
59
|
-
// worked around it by calling the plugin themselves; now that it works
|
|
60
|
-
// again both run, and without this the label grows a second "Set range"
|
|
61
|
-
// whose handler tears down the first one's popup.
|
|
62
|
-
//
|
|
63
|
-
// Replacing rather than skipping matters: the auto-init registers its
|
|
64
|
-
// ready callback before anything a host adds to active_admin.js, so
|
|
65
|
-
// bailing out on the second call would silently drop the host's
|
|
66
|
-
// options. Reading the link itself rather than a data flag keeps the
|
|
67
|
-
// guard and the thing it guards from drifting apart across clone() and
|
|
68
|
-
// Turbo cache restores, which copy the node but not jQuery's data.
|
|
69
|
-
$this.find('a.btn_timerange').remove();
|
|
70
|
-
$this.off('click.dateRangeExtPreset');
|
|
71
|
-
|
|
72
|
-
// Per element, because data-show-time is read off the element itself.
|
|
73
|
-
// Writing it back into the shared opts let one pair's setting reach
|
|
74
|
-
// every other pair in the same call, and formatDate reads the flag at
|
|
75
|
-
// click time rather than here, so the leak was not even order-dependent.
|
|
76
|
-
let el_opts = $.extend({}, opts);
|
|
77
|
-
|
|
78
|
-
// The attribute is authoritative in both directions when present. Only
|
|
79
|
-
// ever assigning true made it impossible for one element to opt out of
|
|
80
|
-
// a show_time the call or the defaults had turned on.
|
|
81
|
-
if (typeof $this.data('show-time') != 'undefined') {
|
|
82
|
-
el_opts.show_time = $this.data('show-time').toString() == 'true';
|
|
83
|
-
}
|
|
99
|
+
return new Date(utc.getTime() + opts.hours_offset * 60 * 60 * 1000);
|
|
100
|
+
}
|
|
84
101
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
lteq_input = $this.next().find('input');
|
|
91
|
-
}
|
|
102
|
+
function setValue(input, value) {
|
|
103
|
+
input.value = value;
|
|
104
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
105
|
+
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
106
|
+
}
|
|
92
107
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
108
|
+
// Also takes a jQuery object, which hosts migrating from the jQuery plugin
|
|
109
|
+
// still pass.
|
|
110
|
+
function resolveInput(option) {
|
|
111
|
+
if (!option) { return null; }
|
|
112
|
+
if (typeof option === 'string') { return document.querySelector(option); }
|
|
113
|
+
return option.jquery ? option[0] || null : option;
|
|
114
|
+
}
|
|
99
115
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
116
|
+
function fieldInputs(el) {
|
|
117
|
+
return el.querySelectorAll('input:not([type="hidden"]):not([type="submit"])');
|
|
118
|
+
}
|
|
103
119
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
120
|
+
// A wrapper holding both inputs (a date_range filter) uses its first and
|
|
121
|
+
// last. A Formtastic .datetime_preset_pair is two wrappers with one input
|
|
122
|
+
// each, so the "to" input lives in the next wrapper. Only that class looks
|
|
123
|
+
// there: any other element must not write into an unrelated next field.
|
|
124
|
+
function detectInputs(el, options) {
|
|
125
|
+
var inputs = fieldInputs(el);
|
|
126
|
+
var gteq = inputs[0];
|
|
127
|
+
var lteq = inputs[inputs.length - 1];
|
|
128
|
+
var next = el.nextElementSibling;
|
|
129
|
+
// A lone pair input with no partner gets only the start, not both ends.
|
|
130
|
+
if (inputs.length < 2 && el.classList.contains('datetime_preset_pair')) {
|
|
131
|
+
lteq = (next && fieldInputs(next)[0]) || null;
|
|
132
|
+
}
|
|
133
|
+
return [resolveInput(options.gteq_input) || gteq, resolveInput(options.lteq_input) || lteq];
|
|
134
|
+
}
|
|
110
135
|
|
|
111
|
-
|
|
112
|
-
|
|
136
|
+
var closeOpenMenu = function () {};
|
|
137
|
+
|
|
138
|
+
function menuItems(opts) {
|
|
139
|
+
var items = PRESETS.map(function (preset) {
|
|
140
|
+
return { key: preset.key, className: 'btn_' + preset.key, title: preset.title, range: preset.range };
|
|
141
|
+
});
|
|
142
|
+
opts.add_range.forEach(function (range, i) {
|
|
143
|
+
items.push({ key: 'custom_' + i, className: 'btn_date_range_' + i, title: range.title, range: function () {
|
|
144
|
+
return [day(range.start, 0), day(range.end, 0)];
|
|
145
|
+
} });
|
|
146
|
+
});
|
|
147
|
+
return items;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function openMenu(toggle, opts, fill) {
|
|
151
|
+
closeOpenMenu();
|
|
152
|
+
|
|
153
|
+
var items = menuItems(opts);
|
|
154
|
+
var menu = document.createElement('div');
|
|
155
|
+
menu.className = 'block_timerange';
|
|
156
|
+
menu.setAttribute('data-date-range-preset-menu', '');
|
|
157
|
+
menu.setAttribute('role', 'menu');
|
|
158
|
+
|
|
159
|
+
items.forEach(function (item) {
|
|
160
|
+
var row = document.createElement('div');
|
|
161
|
+
var button = document.createElement('span');
|
|
162
|
+
button.className = item.className;
|
|
163
|
+
button.setAttribute('data-date-range-preset', item.key);
|
|
164
|
+
button.setAttribute('role', 'menuitem');
|
|
165
|
+
button.tabIndex = 0;
|
|
166
|
+
button.textContent = item.title;
|
|
167
|
+
row.appendChild(button);
|
|
168
|
+
menu.appendChild(row);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
var box = toggle.getBoundingClientRect();
|
|
172
|
+
menu.style.minWidth = box.width + 'px';
|
|
173
|
+
menu.style.top = (box.top + window.scrollY) + 'px';
|
|
174
|
+
menu.style.left = (box.left + window.scrollX) + 'px';
|
|
113
175
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
176
|
+
function choose(target) {
|
|
177
|
+
var button = target.closest('[data-date-range-preset]');
|
|
178
|
+
if (!button) { return; }
|
|
179
|
+
var key = button.getAttribute('data-date-range-preset');
|
|
180
|
+
var item = items.filter(function (it) { return it.key === key; })[0];
|
|
181
|
+
closeOpenMenu();
|
|
182
|
+
fill(item.range(shiftedNow(opts)));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
menu.addEventListener('click', function (e) { choose(e.target); });
|
|
186
|
+
menu.addEventListener('keydown', function (e) {
|
|
187
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
188
|
+
e.preventDefault();
|
|
189
|
+
choose(e.target);
|
|
190
|
+
toggle.focus();
|
|
191
|
+
} else if (e.key === 'Escape') {
|
|
192
|
+
closeOpenMenu();
|
|
193
|
+
toggle.focus();
|
|
194
|
+
} else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
117
195
|
e.preventDefault();
|
|
196
|
+
var buttons = Array.prototype.slice.call(menu.querySelectorAll('[data-date-range-preset]'));
|
|
197
|
+
var step = e.key === 'ArrowDown' ? 1 : -1;
|
|
198
|
+
var at = buttons.indexOf(document.activeElement);
|
|
199
|
+
// With no preset focused, ArrowUp starts from the bottom.
|
|
200
|
+
if (at === -1) { at = step === 1 ? -1 : 0; }
|
|
201
|
+
buttons[(at + step + buttons.length) % buttons.length].focus();
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Tabbing out of the menu closes it. A null relatedTarget is a click on
|
|
206
|
+
// something unfocusable, which onOutsideClick already handles.
|
|
207
|
+
menu.addEventListener('focusout', function (e) {
|
|
208
|
+
if (e.relatedTarget && !menu.contains(e.relatedTarget)) { closeOpenMenu(); }
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// No stopPropagation: this listener only watches for a click that should
|
|
212
|
+
// close the menu. Active Admin delegates Clear Filters and the has_many
|
|
213
|
+
// buttons from <document>, so swallowing the event would break them.
|
|
214
|
+
function onOutsideClick(e) {
|
|
215
|
+
if (!menu.contains(e.target)) { closeOpenMenu(); }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
closeOpenMenu = function () {
|
|
219
|
+
menu.remove();
|
|
220
|
+
document.body.removeEventListener('click', onOutsideClick);
|
|
221
|
+
toggle.setAttribute('aria-expanded', 'false');
|
|
222
|
+
closeOpenMenu = function () {};
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// Append and bind in the same tick, so the presets are live as soon as
|
|
226
|
+
// the menu is in the document.
|
|
227
|
+
document.body.appendChild(menu);
|
|
228
|
+
document.body.addEventListener('click', onOutsideClick);
|
|
229
|
+
|
|
230
|
+
// A link at the right edge of a sidebar would push the menu off screen.
|
|
231
|
+
var overflow = menu.getBoundingClientRect().right - document.documentElement.clientWidth;
|
|
232
|
+
if (overflow > 0) { menu.style.left = Math.max(0, box.left + window.scrollX - overflow) + 'px'; }
|
|
233
|
+
toggle.setAttribute('aria-expanded', 'true');
|
|
234
|
+
|
|
235
|
+
// The menu sits at the end of <body>, out of the tab order near the link.
|
|
236
|
+
var first = menu.querySelector('[data-date-range-preset]');
|
|
237
|
+
if (first) { first.focus(); }
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Read at click time: on Active Admin 4 the module, and so the auto-init,
|
|
241
|
+
// runs before a host module that imports it can set defaults.
|
|
242
|
+
function currentOptions(el, options) {
|
|
243
|
+
// api.defaults, so a host that replaces the whole object is still heard.
|
|
244
|
+
var opts = Object.assign({}, api.defaults, options);
|
|
245
|
+
opts.add_range = opts.add_range || [];
|
|
246
|
+
|
|
247
|
+
// The attribute is authoritative in both directions when present.
|
|
248
|
+
var showTime = el.getAttribute('data-show-time');
|
|
249
|
+
if (showTime !== null) { opts.show_time = showTime === 'true'; }
|
|
250
|
+
return opts;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function initElement(el, options) {
|
|
254
|
+
// Re-initialising replaces the wiring instead of adding to it, so a host
|
|
255
|
+
// call with options after the auto-init wins. The listener lives on the
|
|
256
|
+
// link, so dropping the old link drops the old wiring with it.
|
|
257
|
+
el.querySelectorAll('[data-date-range-preset-toggle]').forEach(function (link) { link.remove(); });
|
|
258
|
+
|
|
259
|
+
var inputs = detectInputs(el, options);
|
|
260
|
+
var gteq = inputs[0];
|
|
261
|
+
var lteq = inputs[1];
|
|
262
|
+
|
|
263
|
+
var toggle = document.createElement('a');
|
|
264
|
+
toggle.href = '#';
|
|
265
|
+
toggle.className = 'btn_timerange';
|
|
266
|
+
toggle.textContent = 'Set range';
|
|
267
|
+
toggle.setAttribute('data-date-range-preset-toggle', '');
|
|
268
|
+
toggle.setAttribute('aria-haspopup', 'menu');
|
|
269
|
+
toggle.setAttribute('aria-expanded', 'false');
|
|
270
|
+
|
|
271
|
+
var label = el.querySelector('label');
|
|
272
|
+
if (label) {
|
|
273
|
+
label.classList.add('datetime_preset_filter_label');
|
|
274
|
+
label.appendChild(toggle);
|
|
275
|
+
} else {
|
|
276
|
+
el.insertBefore(toggle, el.firstChild);
|
|
277
|
+
}
|
|
118
278
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
// On a non-document collection the method is deprecated anyway.
|
|
129
|
-
let container = $('<div style="min-width: '+e.target.offsetWidth+'px; top: '+(e.target.offsetTop)+'px; left: '+(e.target.offsetLeft)+'px" class="block_timerange">' +
|
|
130
|
-
'<div><span class="btn_today">Today</span></div>' +
|
|
131
|
-
'<div><span class="btn_yesterday">Yesterday</span></div>' +
|
|
132
|
-
'<div><span class="btn_week">This Week</span></div>' +
|
|
133
|
-
'<div><span class="btn_month">This Month</span></div>' +
|
|
134
|
-
'<div><span class="btn_last_week">Last Week</span></div>' +
|
|
135
|
-
'<div><span class="btn_last_month">Last Month</span></div>' +
|
|
136
|
-
additional_items_html +
|
|
137
|
-
'</div>'
|
|
138
|
-
).appendTo('body');
|
|
139
|
-
|
|
140
|
-
// additional ranges
|
|
141
|
-
opts.add_range.forEach(function(el, i) {
|
|
142
|
-
container.on('click.CalendarRangeSet', '.btn_date_range_' + i, function(e) {
|
|
143
|
-
unbindClickEventBlockTimerange();
|
|
144
|
-
let start = new Date(el['start'].getFullYear(), el['start'].getMonth(), el['start'].getDate());
|
|
145
|
-
let end = new Date(el['end'].getFullYear(), el['end'].getMonth(), el['end'].getDate());
|
|
146
|
-
fillInputs(start, end)
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
// Today
|
|
151
|
-
container.on('click.CalendarRangeSet', '.btn_today', function(e) {
|
|
152
|
-
unbindClickEventBlockTimerange();
|
|
153
|
-
let start = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate());
|
|
154
|
-
let end = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate() + 1);
|
|
155
|
-
fillInputs(start, end);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
// Yesterday
|
|
159
|
-
container.on('click.CalendarRangeSet', '.btn_yesterday', function(e) {
|
|
160
|
-
unbindClickEventBlockTimerange();
|
|
161
|
-
let start = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate() - 1);
|
|
162
|
-
let end = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate());
|
|
163
|
-
fillInputs(start, end);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
// Week
|
|
167
|
-
container.on('click.CalendarRangeSet', '.btn_week', function(e) {
|
|
168
|
-
unbindClickEventBlockTimerange();
|
|
169
|
-
let start = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate() - days_since_monday(datetime));
|
|
170
|
-
let end = new Date(start.getFullYear(), start.getMonth(), start.getDate() + 7);
|
|
171
|
-
fillInputs(start, end);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
// Month
|
|
175
|
-
container.on('click.CalendarRangeSet', '.btn_month', function(e) {
|
|
176
|
-
unbindClickEventBlockTimerange();
|
|
177
|
-
let start = new Date(datetime.getFullYear(), datetime.getMonth(), 1);
|
|
178
|
-
let end = new Date(datetime.getFullYear(), datetime.getMonth() + 1, 1);
|
|
179
|
-
fillInputs(start, end);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
// Last Week
|
|
183
|
-
container.on('click.CalendarRangeSet', '.btn_last_week', function(e) {
|
|
184
|
-
unbindClickEventBlockTimerange();
|
|
185
|
-
let end = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate() - days_since_monday(datetime));
|
|
186
|
-
let start = new Date(end.getFullYear(), end.getMonth(), end.getDate() - 7);
|
|
187
|
-
fillInputs(start, end);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
// Last Month
|
|
191
|
-
container.on('click.CalendarRangeSet', '.btn_last_month', function(e) {
|
|
192
|
-
unbindClickEventBlockTimerange();
|
|
193
|
-
let end = new Date(datetime.getFullYear(), datetime.getMonth(), 1);
|
|
194
|
-
let start = new Date(end.getFullYear(), end.getMonth() - 1, 1);
|
|
195
|
-
fillInputs(start, end);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// Outer
|
|
199
|
-
// No stopPropagation here: this listener only watches for a click
|
|
200
|
-
// that should close the popup. Active Admin delegates Clear Filters
|
|
201
|
-
// and the has_many buttons from <document>, which is upstream of
|
|
202
|
-
// <body>, so swallowing the event would break them for as long as
|
|
203
|
-
// the popup is open.
|
|
204
|
-
$('body').on('click.CalendarRangeSet', function(e) {
|
|
205
|
-
if ($(e.target).closest('.block_timerange').length == 0) {
|
|
206
|
-
unbindClickEventBlockTimerange();
|
|
207
|
-
}
|
|
208
|
-
});
|
|
279
|
+
toggle.addEventListener('click', function (e) {
|
|
280
|
+
e.preventDefault();
|
|
281
|
+
e.stopPropagation();
|
|
282
|
+
var opts = currentOptions(el, options);
|
|
283
|
+
openMenu(toggle, opts, function (range) {
|
|
284
|
+
var end = range[1];
|
|
285
|
+
if (opts.date_to_human_readable) { end = new Date(end.getTime() - 1000); }
|
|
286
|
+
if (gteq) { setValue(gteq, formatFor(gteq, range[0], opts)); }
|
|
287
|
+
if (lteq) { setValue(lteq, formatFor(lteq, end, opts)); }
|
|
209
288
|
});
|
|
210
289
|
});
|
|
211
290
|
}
|
|
212
291
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
292
|
+
function toElements(target) {
|
|
293
|
+
if (typeof target === 'string') { return Array.prototype.slice.call(document.querySelectorAll(target)); }
|
|
294
|
+
if (target instanceof Element) { return [target]; }
|
|
295
|
+
return Array.prototype.slice.call(target || []);
|
|
296
|
+
}
|
|
216
297
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
// 2015-06-10 - 2015-06-11
|
|
224
|
-
// 2015-06-10 00:00:00 - 2015-06-11 00:00:00
|
|
225
|
-
date_to_human_readable: false,
|
|
298
|
+
// target: a CSS selector, an Element, or a list of Elements.
|
|
299
|
+
function init(target, options) {
|
|
300
|
+
var elements = toElements(target);
|
|
301
|
+
elements.forEach(function (el) { initElement(el, options || {}); });
|
|
302
|
+
return elements;
|
|
303
|
+
}
|
|
226
304
|
|
|
227
|
-
|
|
228
|
-
|
|
305
|
+
var api = { defaults: defaults, init: init };
|
|
306
|
+
window.ActiveAdminDateRangePreset = api;
|
|
229
307
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
// {
|
|
233
|
-
// title: 'Last 30 days',
|
|
234
|
-
// start: new Date().setDate((new Date()).getDate() - 30)
|
|
235
|
-
// end: new Date()
|
|
236
|
-
// }
|
|
237
|
-
add_range: []
|
|
308
|
+
function autoInit() {
|
|
309
|
+
init('.datetime_preset_pair');
|
|
238
310
|
}
|
|
239
|
-
});
|
|
240
311
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
312
|
+
if (document.readyState === 'loading') {
|
|
313
|
+
document.addEventListener('DOMContentLoaded', autoInit);
|
|
314
|
+
} else {
|
|
315
|
+
autoInit();
|
|
316
|
+
}
|
|
317
|
+
})();
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/* Plain CSS: Sprockets serves it to Active Admin 3 and a Tailwind entry can
|
|
2
|
+
@import it on Active Admin 4. Dark mode follows Active Admin 4's .dark class. */
|
|
3
|
+
|
|
4
|
+
form label.datetime_preset_filter_label .btn_timerange {
|
|
5
|
+
float: right;
|
|
6
|
+
margin: -2px 8px 0 0;
|
|
7
|
+
text-decoration: none;
|
|
8
|
+
border-bottom: 1px dashed;
|
|
9
|
+
line-height: 1.4em;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
form label.datetime_preset_filter_label .btn_timerange:hover {
|
|
13
|
+
border-bottom: 0 none;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/* A form pair gets Active Admin's full label column, wide enough that the
|
|
17
|
+
float above parks the link far from the inputs it drives. Put it on its own
|
|
18
|
+
line under the label text; `display: table` keeps it shrink-to-fit. Sidebar
|
|
19
|
+
filters are divs, not lis, and keep the float. */
|
|
20
|
+
li.datetime_preset_pair > label.datetime_preset_filter_label .btn_timerange {
|
|
21
|
+
float: none;
|
|
22
|
+
display: table;
|
|
23
|
+
margin: 2px 0 0 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.block_timerange {
|
|
27
|
+
position: absolute;
|
|
28
|
+
z-index: 50;
|
|
29
|
+
display: block;
|
|
30
|
+
border: 1px solid #000;
|
|
31
|
+
padding: 5px 10px;
|
|
32
|
+
text-align: center;
|
|
33
|
+
background-color: #f4f4f4;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.block_timerange > div {
|
|
37
|
+
margin-top: 10px;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.block_timerange > div:last-child {
|
|
41
|
+
margin-bottom: 10px;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.block_timerange > div span {
|
|
45
|
+
color: #2e86cc;
|
|
46
|
+
cursor: pointer;
|
|
47
|
+
text-decoration: none;
|
|
48
|
+
font-size: 14px;
|
|
49
|
+
border-bottom: 1px dashed;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.block_timerange > div span:hover {
|
|
53
|
+
border-bottom: 0 none;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.block_timerange:where(.dark, .dark *) {
|
|
57
|
+
border-color: #4b5563;
|
|
58
|
+
background-color: #1f2937;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.block_timerange:where(.dark, .dark *) > div span {
|
|
62
|
+
color: #60a5fa;
|
|
63
|
+
}
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_admin_date_range_preset
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gena M.
|
|
@@ -13,16 +13,22 @@ dependencies:
|
|
|
13
13
|
name: activeadmin
|
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
|
15
15
|
requirements:
|
|
16
|
-
- - "
|
|
16
|
+
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
18
|
version: '3.2'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 5.0.0.a
|
|
19
22
|
type: :runtime
|
|
20
23
|
prerelease: false
|
|
21
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
25
|
requirements:
|
|
23
|
-
- - "
|
|
26
|
+
- - ">="
|
|
24
27
|
- !ruby/object:Gem::Version
|
|
25
28
|
version: '3.2'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: 5.0.0.a
|
|
26
32
|
- !ruby/object:Gem::Dependency
|
|
27
33
|
name: railties
|
|
28
34
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -61,10 +67,11 @@ extra_rdoc_files: []
|
|
|
61
67
|
files:
|
|
62
68
|
- LICENSE.txt
|
|
63
69
|
- README.md
|
|
70
|
+
- config/importmap.rb
|
|
64
71
|
- lib/active_admin_date_range_preset.rb
|
|
65
72
|
- lib/active_admin_date_range_preset/version.rb
|
|
66
73
|
- vendor/assets/javascripts/active_admin_date_range_preset.js
|
|
67
|
-
- vendor/assets/stylesheets/active_admin_date_range_preset.
|
|
74
|
+
- vendor/assets/stylesheets/active_admin_date_range_preset.css
|
|
68
75
|
homepage: https://github.com/workgena/active_admin_date_range_preset
|
|
69
76
|
licenses:
|
|
70
77
|
- MIT
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
form label.datetime_preset_filter_label .btn_timerange {
|
|
2
|
-
float: right;
|
|
3
|
-
margin: -2px 8px 0 0;
|
|
4
|
-
text-decoration: none;
|
|
5
|
-
border-bottom: 1px dashed;
|
|
6
|
-
line-height: 1.4em;
|
|
7
|
-
&:hover {
|
|
8
|
-
border-bottom: 0 none;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// A form pair gets Active Admin's full label column, which is wide enough
|
|
13
|
-
// that the float above parks the link at the far right of it, away from the
|
|
14
|
-
// inputs it drives. Put it on its own line under the label text instead.
|
|
15
|
-
// `display: table` keeps it shrink-to-fit, so the dashed underline stays
|
|
16
|
-
// under the words rather than stretching the whole column.
|
|
17
|
-
//
|
|
18
|
-
// The sidebar filter keeps the float: there the label and the link share a
|
|
19
|
-
// narrow panel and right alignment is what the plugin has always shown.
|
|
20
|
-
.datetime_preset_pair > label.datetime_preset_filter_label .btn_timerange {
|
|
21
|
-
float: none;
|
|
22
|
-
display: table;
|
|
23
|
-
margin: 2px 0 0 0;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
.block_timerange {
|
|
27
|
-
position: absolute;
|
|
28
|
-
display: block;
|
|
29
|
-
border: 1px solid #000;
|
|
30
|
-
padding: 5px 10px 5px 10px;
|
|
31
|
-
text-align: center;
|
|
32
|
-
background-color: #f4f4f4;
|
|
33
|
-
>div {
|
|
34
|
-
margin-top: 10px;
|
|
35
|
-
&:last-child {
|
|
36
|
-
margin-bottom: 10px;
|
|
37
|
-
}
|
|
38
|
-
span {
|
|
39
|
-
color: #2e86cc;
|
|
40
|
-
cursor: pointer;
|
|
41
|
-
text-decoration: none;
|
|
42
|
-
font-size: 14px;
|
|
43
|
-
border-bottom: 1px dashed;
|
|
44
|
-
&:hover {
|
|
45
|
-
border-bottom: 0 none;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|