active_admin_date_range_preset 0.3.1 → 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 +5 -5
- data/README.md +226 -83
- 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 +317 -0
- data/vendor/assets/stylesheets/active_admin_date_range_preset.css +63 -0
- metadata +37 -28
- data/.idea/vcs.xml +0 -6
- data/Gemfile +0 -4
- data/Rakefile +0 -1
- data/active_admin_date_range_preset.gemspec +0 -24
- data/screen/step_1.jpg +0 -0
- data/screen/step_2.jpg +0 -0
- data/screen/step_2_1.png +0 -0
- data/screen/step_2_2.png +0 -0
- data/vendor/assets/javascripts/active_admin_date_range_preset.coffee +0 -191
- data/vendor/assets/stylesheets/active_admin_date_range_preset.scss +0 -36
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
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
|
@@ -1,16 +1,80 @@
|
|
|
1
|
+
[](https://badge.fury.io/rb/active_admin_date_range_preset)
|
|
2
|
+
[](https://badge.fury.io/js/@activeadmin-plugins%2Factive_admin_date_range_preset)
|
|
3
|
+

|
|
4
|
+
[](https://github.com/activeadmin-plugins/active_admin_date_range_preset/actions/workflows/ci.yml)
|
|
5
|
+
|
|
1
6
|
# active_admin_date_range_preset
|
|
2
7
|
|
|
3
8
|
Preset links for ActiveAdmin date_range inputs in sidebar filters in forms
|
|
4
9
|
|
|
5
10
|
This is how it looks like
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
<img width="1008" height="942" alt="combined" src="https://github.com/user-attachments/assets/cbe22410-078e-4f2b-894b-4457c13e3e22" />
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Compatibility
|
|
17
|
+
|
|
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.
|
|
8
21
|
|
|
9
|
-
|
|
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`.
|
|
10
25
|
|
|
11
|
-
|
|
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.
|
|
12
28
|
|
|
13
|
-
|
|
29
|
+
The Rails and Ruby floors are declared explicitly because Active Admin 3.2
|
|
30
|
+
still allows `railties >= 6.1` and Ruby `>= 2.6`, both long past end of life
|
|
31
|
+
and neither tested here.
|
|
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.
|
|
14
78
|
|
|
15
79
|
## Installation
|
|
16
80
|
|
|
@@ -24,111 +88,173 @@ And then execute:
|
|
|
24
88
|
|
|
25
89
|
$ bundle install
|
|
26
90
|
|
|
27
|
-
|
|
91
|
+
### Active Admin 3 (Sprockets)
|
|
92
|
+
|
|
93
|
+
In `app/assets/javascripts/active_admin.js`:
|
|
28
94
|
|
|
29
|
-
|
|
95
|
+
```javascript
|
|
96
|
+
//= require active_admin_date_range_preset
|
|
97
|
+
```
|
|
30
98
|
|
|
31
|
-
|
|
99
|
+
In `app/assets/stylesheets/active_admin.scss`:
|
|
32
100
|
|
|
33
|
-
|
|
34
|
-
|
|
101
|
+
```scss
|
|
102
|
+
@import "active_admin_date_range_preset";
|
|
103
|
+
```
|
|
35
104
|
|
|
36
|
-
|
|
37
|
-
```@import "active_admin_date_range_preset";```
|
|
105
|
+
### Active Admin 4 (import map)
|
|
38
106
|
|
|
39
|
-
|
|
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:
|
|
40
112
|
|
|
41
|
-
|
|
113
|
+
```erb
|
|
114
|
+
<%= javascript_import_module_tag "active_admin_date_range_preset" %>
|
|
115
|
+
```
|
|
42
116
|
|
|
43
|
-
in
|
|
117
|
+
If you use active_admin_theme 4, add the module to its list in an initializer
|
|
118
|
+
instead:
|
|
44
119
|
|
|
45
120
|
```ruby
|
|
46
|
-
|
|
47
|
-
f.input :date_to, as: :date_time_picker
|
|
121
|
+
ActiveAdminTheme.javascripts += %w[active_admin_date_range_preset]
|
|
48
122
|
```
|
|
49
123
|
|
|
50
|
-
|
|
51
|
-
|
|
124
|
+
For the CSS, either link the stylesheet in the same `_html_head` override
|
|
125
|
+
(Propshaft serves it from the gem):
|
|
52
126
|
|
|
53
|
-
```
|
|
127
|
+
```erb
|
|
128
|
+
<%= stylesheet_link_tag "active_admin_date_range_preset" %>
|
|
129
|
+
```
|
|
54
130
|
|
|
55
|
-
|
|
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`):
|
|
56
133
|
|
|
57
|
-
|
|
134
|
+
```css
|
|
135
|
+
@import "@activeadmin-plugins/active_admin_date_range_preset";
|
|
136
|
+
```
|
|
58
137
|
|
|
59
|
-
|
|
138
|
+
The menu follows Active Admin 4's dark mode (the `.dark` class on `<html>`).
|
|
60
139
|
|
|
140
|
+
### npm package
|
|
61
141
|
|
|
62
|
-
|
|
142
|
+
$ npm i @activeadmin-plugins/active_admin_date_range_preset
|
|
63
143
|
|
|
64
|
-
|
|
65
|
-
|
|
144
|
+
```javascript
|
|
145
|
+
import '@activeadmin-plugins/active_admin_date_range_preset';
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```css
|
|
149
|
+
@import '@activeadmin-plugins/active_admin_date_range_preset';
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Usage
|
|
153
|
+
|
|
154
|
+
### Forms
|
|
66
155
|
|
|
67
|
-
|
|
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.
|
|
68
159
|
|
|
69
160
|
```ruby
|
|
70
|
-
|
|
161
|
+
f.input :date_from, as: :string, wrapper_html: { class: 'datetime_preset_pair', data: { show_time: 'true' } }
|
|
162
|
+
f.input :date_to, as: :string
|
|
71
163
|
```
|
|
72
164
|
|
|
73
|
-
|
|
165
|
+
The value format follows the input's `type`:
|
|
74
166
|
|
|
75
|
-
|
|
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` |
|
|
76
172
|
|
|
77
|
-
|
|
78
|
-
$(document).on('ready', function(){
|
|
79
|
-
$('form.filter_form div.filter_date_time_range').date_range_ext_preset();
|
|
80
|
-
});
|
|
81
|
-
```
|
|
173
|
+
`data-show-time` overrides `show_time` for that pair, in both directions.
|
|
82
174
|
|
|
83
|
-
|
|
175
|
+
### Filters
|
|
84
176
|
|
|
177
|
+
A wrapper that holds both inputs uses its first and last input. Add the class
|
|
178
|
+
to a date_range filter:
|
|
85
179
|
|
|
86
|
-
|
|
180
|
+
```ruby
|
|
181
|
+
filter :created_at, as: :date_range, wrapper_html: { class: 'datetime_preset_pair' }
|
|
182
|
+
```
|
|
87
183
|
|
|
88
|
-
|
|
89
|
-
For example, you have complex form where input-pairs are not close to each other and not standard.
|
|
184
|
+
Or set up every date_range filter from JavaScript:
|
|
90
185
|
|
|
91
186
|
```javascript
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
});
|
|
97
|
-
});
|
|
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');
|
|
98
191
|
```
|
|
99
|
-
".any_jquery_selector" is pointed to place where button "Set range" will appear.
|
|
100
|
-
Set lteq_input and gteq_input to point to inputs if they not near main selector.
|
|
101
192
|
|
|
193
|
+
On Active Admin 4, put that call in a module script in your `_html_head`
|
|
194
|
+
override, after the import:
|
|
102
195
|
|
|
103
|
-
|
|
196
|
+
```erb
|
|
197
|
+
<script type="module">
|
|
198
|
+
import "active_admin_date_range_preset";
|
|
199
|
+
ActiveAdminDateRangePreset.init("form.filters-form div.date_range");
|
|
200
|
+
</script>
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Active Admin 4's own date_range filter renders native date inputs, so the
|
|
204
|
+
presets write `YYYY-MM-DD` there.
|
|
104
205
|
|
|
105
|
-
|
|
206
|
+
### With active_admin_datetimepicker
|
|
106
207
|
|
|
107
|
-
|
|
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:
|
|
108
212
|
|
|
109
213
|
```javascript
|
|
110
|
-
|
|
111
|
-
setting_name: "setting_value"
|
|
112
|
-
});
|
|
214
|
+
ActiveAdminDateRangePreset.init('form div.filter_date_time_range');
|
|
113
215
|
```
|
|
114
|
-
|
|
216
|
+
|
|
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.
|
|
221
|
+
|
|
222
|
+
### Custom inputs
|
|
223
|
+
|
|
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:
|
|
115
226
|
|
|
116
227
|
```javascript
|
|
117
|
-
|
|
228
|
+
ActiveAdminDateRangePreset.init('.any_selector', {
|
|
229
|
+
gteq_input: '#date_from',
|
|
230
|
+
lteq_input: '#date_to'
|
|
231
|
+
});
|
|
118
232
|
```
|
|
119
233
|
|
|
120
|
-
|
|
234
|
+
Calling `init` again on the same element replaces its setup, so a call with
|
|
235
|
+
options after the automatic one wins.
|
|
236
|
+
|
|
237
|
+
### Test hooks
|
|
238
|
+
|
|
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.
|
|
243
|
+
|
|
244
|
+
## Settings
|
|
245
|
+
|
|
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.
|
|
121
249
|
|
|
122
250
|
```javascript
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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;
|
|
127
255
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
# Today with human_readable=true: 2015-06-12 00:00:00 - 2015-16-12 23:59:59
|
|
131
|
-
$.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;
|
|
132
258
|
```
|
|
133
259
|
|
|
134
260
|
### date_to_human_readable
|
|
@@ -151,7 +277,8 @@ value: true/false
|
|
|
151
277
|
|
|
152
278
|
default: false
|
|
153
279
|
|
|
154
|
-
If true
|
|
280
|
+
If true, text inputs get date and time, usually 00:00:00. Native inputs ignore
|
|
281
|
+
it: their `type` decides.
|
|
155
282
|
|
|
156
283
|
### hours_offset
|
|
157
284
|
|
|
@@ -161,29 +288,45 @@ default: 0
|
|
|
161
288
|
|
|
162
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:
|
|
163
290
|
|
|
164
|
-
Example:
|
|
165
291
|
```javascript
|
|
166
|
-
|
|
292
|
+
ActiveAdminDateRangePreset.defaults.hours_offset = +3;
|
|
167
293
|
// or
|
|
168
|
-
|
|
169
|
-
```
|
|
294
|
+
ActiveAdminDateRangePreset.defaults.hours_offset = -3;
|
|
295
|
+
```
|
|
170
296
|
|
|
171
|
-
###
|
|
297
|
+
### add_range
|
|
172
298
|
|
|
173
299
|
```javascript
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
]
|
|
186
|
-
});
|
|
187
|
-
|
|
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
|
+
]
|
|
188
310
|
});
|
|
189
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
|
|
@@ -0,0 +1,317 @@
|
|
|
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; }
|
|
6
|
+
|
|
7
|
+
var defaults = {
|
|
8
|
+
// Manual global time shift, from UTC can be +/- number
|
|
9
|
+
hours_offset: 0,
|
|
10
|
+
|
|
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,
|
|
20
|
+
|
|
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
|
+
}
|
|
67
|
+
|
|
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(
|
|
95
|
+
now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
|
|
96
|
+
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(),
|
|
97
|
+
now.getUTCMilliseconds()
|
|
98
|
+
);
|
|
99
|
+
return new Date(utc.getTime() + opts.hours_offset * 60 * 60 * 1000);
|
|
100
|
+
}
|
|
101
|
+
|
|
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
|
+
}
|
|
107
|
+
|
|
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
|
+
}
|
|
115
|
+
|
|
116
|
+
function fieldInputs(el) {
|
|
117
|
+
return el.querySelectorAll('input:not([type="hidden"]):not([type="submit"])');
|
|
118
|
+
}
|
|
119
|
+
|
|
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
|
+
}
|
|
135
|
+
|
|
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';
|
|
175
|
+
|
|
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') {
|
|
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
|
+
}
|
|
278
|
+
|
|
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)); }
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
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
|
+
}
|
|
297
|
+
|
|
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
|
+
}
|
|
304
|
+
|
|
305
|
+
var api = { defaults: defaults, init: init };
|
|
306
|
+
window.ActiveAdminDateRangePreset = api;
|
|
307
|
+
|
|
308
|
+
function autoInit() {
|
|
309
|
+
init('.datetime_preset_pair');
|
|
310
|
+
}
|
|
311
|
+
|
|
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,43 +1,62 @@
|
|
|
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.
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
13
|
+
name: activeadmin
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - "
|
|
16
|
+
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
20
|
-
|
|
18
|
+
version: '3.2'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 5.0.0.a
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '3.2'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: 5.0.0.a
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: railties
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '8.0'
|
|
39
|
+
type: :runtime
|
|
21
40
|
prerelease: false
|
|
22
41
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
42
|
requirements:
|
|
24
|
-
- - "
|
|
43
|
+
- - ">="
|
|
25
44
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
45
|
+
version: '8.0'
|
|
27
46
|
- !ruby/object:Gem::Dependency
|
|
28
47
|
name: rake
|
|
29
48
|
requirement: !ruby/object:Gem::Requirement
|
|
30
49
|
requirements:
|
|
31
|
-
- - "
|
|
50
|
+
- - ">="
|
|
32
51
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
52
|
+
version: '0'
|
|
34
53
|
type: :development
|
|
35
54
|
prerelease: false
|
|
36
55
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
56
|
requirements:
|
|
38
|
-
- - "
|
|
57
|
+
- - ">="
|
|
39
58
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
59
|
+
version: '0'
|
|
41
60
|
description: Integrate useful fast links to set date ranges in to ActiveAdmin, for
|
|
42
61
|
example today range, week range, month range
|
|
43
62
|
email:
|
|
@@ -46,25 +65,17 @@ executables: []
|
|
|
46
65
|
extensions: []
|
|
47
66
|
extra_rdoc_files: []
|
|
48
67
|
files:
|
|
49
|
-
- ".idea/vcs.xml"
|
|
50
|
-
- Gemfile
|
|
51
68
|
- LICENSE.txt
|
|
52
69
|
- README.md
|
|
53
|
-
-
|
|
54
|
-
- active_admin_date_range_preset.gemspec
|
|
70
|
+
- config/importmap.rb
|
|
55
71
|
- lib/active_admin_date_range_preset.rb
|
|
56
72
|
- lib/active_admin_date_range_preset/version.rb
|
|
57
|
-
-
|
|
58
|
-
-
|
|
59
|
-
- screen/step_2_1.png
|
|
60
|
-
- screen/step_2_2.png
|
|
61
|
-
- vendor/assets/javascripts/active_admin_date_range_preset.coffee
|
|
62
|
-
- vendor/assets/stylesheets/active_admin_date_range_preset.scss
|
|
73
|
+
- vendor/assets/javascripts/active_admin_date_range_preset.js
|
|
74
|
+
- vendor/assets/stylesheets/active_admin_date_range_preset.css
|
|
63
75
|
homepage: https://github.com/workgena/active_admin_date_range_preset
|
|
64
76
|
licenses:
|
|
65
77
|
- MIT
|
|
66
78
|
metadata: {}
|
|
67
|
-
post_install_message:
|
|
68
79
|
rdoc_options: []
|
|
69
80
|
require_paths:
|
|
70
81
|
- lib
|
|
@@ -72,16 +83,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
72
83
|
requirements:
|
|
73
84
|
- - ">="
|
|
74
85
|
- !ruby/object:Gem::Version
|
|
75
|
-
version: '
|
|
86
|
+
version: '3.3'
|
|
76
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
88
|
requirements:
|
|
78
89
|
- - ">="
|
|
79
90
|
- !ruby/object:Gem::Version
|
|
80
91
|
version: '0'
|
|
81
92
|
requirements: []
|
|
82
|
-
|
|
83
|
-
rubygems_version: 2.6.12
|
|
84
|
-
signing_key:
|
|
93
|
+
rubygems_version: 3.6.9
|
|
85
94
|
specification_version: 4
|
|
86
95
|
summary: date_range_preset extension for ActiveAdmin
|
|
87
96
|
test_files: []
|
data/.idea/vcs.xml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
require "bundler/gem_tasks"
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
require 'active_admin_date_range_preset/version'
|
|
5
|
-
|
|
6
|
-
Gem::Specification.new do |spec|
|
|
7
|
-
spec.name = "active_admin_date_range_preset"
|
|
8
|
-
spec.version = ActiveAdminDateRangePreset::VERSION
|
|
9
|
-
spec.authors = ["Gena M."]
|
|
10
|
-
spec.email = ["workgena@gmail.com"]
|
|
11
|
-
|
|
12
|
-
spec.summary = %q{date_range_preset extension for ActiveAdmin}
|
|
13
|
-
spec.description = %q{Integrate useful fast links to set date ranges in to ActiveAdmin, for example today range, week range, month range}
|
|
14
|
-
spec.homepage = "https://github.com/workgena/active_admin_date_range_preset"
|
|
15
|
-
spec.license = "MIT"
|
|
16
|
-
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
18
|
-
spec.bindir = "bin"
|
|
19
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
20
|
-
spec.require_paths = ["lib"]
|
|
21
|
-
|
|
22
|
-
spec.add_development_dependency "bundler", "~> 1.8"
|
|
23
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
|
24
|
-
end
|
data/screen/step_1.jpg
DELETED
|
Binary file
|
data/screen/step_2.jpg
DELETED
|
Binary file
|
data/screen/step_2_1.png
DELETED
|
Binary file
|
data/screen/step_2_2.png
DELETED
|
Binary file
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
(($) ->
|
|
2
|
-
# options:
|
|
3
|
-
# gteq_input: jQuery-selecotr for input date_from
|
|
4
|
-
# lteq_input: jQuery-selecotr for input date_to
|
|
5
|
-
# hours_offset: Int number - hours +/- to correct time
|
|
6
|
-
$.fn.date_range_ext_preset = (options)->
|
|
7
|
-
|
|
8
|
-
# settings
|
|
9
|
-
options = options || {}
|
|
10
|
-
opts = $.extend( {}, $.fn.date_range_ext_preset.defaults, options );
|
|
11
|
-
|
|
12
|
-
# aditional functions
|
|
13
|
-
num_with_leading_zero = (num, digits_count = 2)->
|
|
14
|
-
s = num + ''
|
|
15
|
-
s = '0' + s while (s.length < digits_count)
|
|
16
|
-
return s
|
|
17
|
-
|
|
18
|
-
# formated date YYYY-MM-DD, with converting to UTC
|
|
19
|
-
# note: getMonth Returns the month (from 0-11), so we do +1
|
|
20
|
-
formatDate = (date)->
|
|
21
|
-
str = date.getFullYear() + '-' + num_with_leading_zero(date.getMonth()+1) + '-' + num_with_leading_zero(date.getDate())
|
|
22
|
-
if opts.show_time
|
|
23
|
-
str += ( ' ' + num_with_leading_zero(date.getHours()) + ':' + num_with_leading_zero(date.getMinutes()) + ':' + num_with_leading_zero(date.getSeconds()) )
|
|
24
|
-
return str
|
|
25
|
-
|
|
26
|
-
unbindClickEventBlockTimerange = ->
|
|
27
|
-
$('.block_timerange').remove()
|
|
28
|
-
$('body').off('click.CalendarRangeSet')
|
|
29
|
-
|
|
30
|
-
# local datetime now
|
|
31
|
-
now = new Date()
|
|
32
|
-
# UTC datetime now
|
|
33
|
-
now_utc = new Date(
|
|
34
|
-
now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
|
|
35
|
-
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(),
|
|
36
|
-
now.getUTCMilliseconds()
|
|
37
|
-
)
|
|
38
|
-
# datetime object, UTC with hours offset
|
|
39
|
-
datetime = new Date(now_utc.getTime() + opts.hours_offset * 60 * 60 * 1000)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
# PLUGIN BEGIN
|
|
43
|
-
return this.each (i, el) ->
|
|
44
|
-
$this = $(el)
|
|
45
|
-
|
|
46
|
-
if typeof $this.data('show-time') != 'undefined' && $this.data('show-time').toString() == 'true'
|
|
47
|
-
console.log $this.data('show-time')
|
|
48
|
-
opts.show_time = true
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
# detect inputs
|
|
52
|
-
if options.lteq_input
|
|
53
|
-
lteq_input = $(options.lteq_input)
|
|
54
|
-
else
|
|
55
|
-
lteq_input = if $this.hasClass('datetime_preset_pair') then $this.next().find('input') else $this.find('input:last')
|
|
56
|
-
if options.gteq_input
|
|
57
|
-
gteq_input = $(options.gteq_input)
|
|
58
|
-
else
|
|
59
|
-
gteq_input = if $this.hasClass('datetime_preset_pair') then $this.find('input') else $this.find('input:first')
|
|
60
|
-
|
|
61
|
-
# filter modifying
|
|
62
|
-
main_btn_html = '<a href="#" class="btn_timerange">Set range</a>'
|
|
63
|
-
$this.find('label').addClass('datetime_preset_filter_label').append(main_btn_html)
|
|
64
|
-
|
|
65
|
-
# helper
|
|
66
|
-
fillInputs = (start, end)->
|
|
67
|
-
gteq_input.val(formatDate(start) )
|
|
68
|
-
if opts.date_to_human_readable
|
|
69
|
-
end.setTime( end.getTime() - 1000 )
|
|
70
|
-
lteq_input.val( formatDate(end) )
|
|
71
|
-
|
|
72
|
-
$this.on('click', '.btn_timerange', (e)->
|
|
73
|
-
unbindClickEventBlockTimerange()
|
|
74
|
-
e.stopPropagation()
|
|
75
|
-
e.preventDefault()
|
|
76
|
-
|
|
77
|
-
additional_items_html = ''
|
|
78
|
-
opts.add_range.forEach (el, i)->
|
|
79
|
-
additional_items_html += '<div><span class="btn_date_range_' + i + '">' + el['title'] + '</span></div>'
|
|
80
|
-
|
|
81
|
-
$('body').append('<div style="min-width: '+e.target.offsetWidth+'px; top: '+(e.target.offsetTop)+'px; left: '+(e.target.offsetLeft)+'px" class="block_timerange">' +
|
|
82
|
-
'<div><span class="btn_today">Today</span></div>' +
|
|
83
|
-
'<div><span class="btn_yesterday">Yesterday</span></div>' +
|
|
84
|
-
'<div><span class="btn_week">This Week</span></div>' +
|
|
85
|
-
'<div><span class="btn_month">This Month</span></div>' +
|
|
86
|
-
'<div><span class="btn_last_week">Last Week</span></div>' +
|
|
87
|
-
'<div><span class="btn_last_month">Last Month</span></div>' +
|
|
88
|
-
additional_items_html +
|
|
89
|
-
'</div>'
|
|
90
|
-
).ready(->
|
|
91
|
-
container = $(this).find('.block_timerange')
|
|
92
|
-
|
|
93
|
-
# additional ranges
|
|
94
|
-
opts.add_range.forEach (el, i)->
|
|
95
|
-
$(container).on('click.CalendarRangeSet', '.btn_date_range_' + i, (e)->
|
|
96
|
-
unbindClickEventBlockTimerange()
|
|
97
|
-
start = new Date(el['start'].getFullYear(), el['start'].getMonth(), el['start'].getDate())
|
|
98
|
-
end = new Date(el['end'].getFullYear(), el['end'].getMonth(), el['end'].getDate())
|
|
99
|
-
fillInputs(start, end)
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
# Today
|
|
103
|
-
$(container).on('click.CalendarRangeSet', '.btn_today', (e)->
|
|
104
|
-
unbindClickEventBlockTimerange()
|
|
105
|
-
start = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate())
|
|
106
|
-
end = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate() + 1)
|
|
107
|
-
fillInputs(start, end)
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
# Yesterday
|
|
111
|
-
$(container).on('click.CalendarRangeSet', '.btn_yesterday', (e)->
|
|
112
|
-
unbindClickEventBlockTimerange()
|
|
113
|
-
start = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate() - 1)
|
|
114
|
-
end = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate())
|
|
115
|
-
fillInputs(start, end)
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
# Week
|
|
119
|
-
$(container).on('click.CalendarRangeSet', '.btn_week', (e)->
|
|
120
|
-
unbindClickEventBlockTimerange()
|
|
121
|
-
start = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate() - datetime.getDay() + 1)
|
|
122
|
-
end = new Date(start.getFullYear(), start.getMonth(), start.getDate() + 7)
|
|
123
|
-
fillInputs(start, end)
|
|
124
|
-
)
|
|
125
|
-
|
|
126
|
-
# Month
|
|
127
|
-
$(container).on('click.CalendarRangeSet', '.btn_month', (e)->
|
|
128
|
-
unbindClickEventBlockTimerange()
|
|
129
|
-
start = new Date(datetime.getFullYear(), datetime.getMonth(), 1)
|
|
130
|
-
end = new Date(datetime.getFullYear(), datetime.getMonth()+1, 1)
|
|
131
|
-
fillInputs(start, end)
|
|
132
|
-
)
|
|
133
|
-
|
|
134
|
-
# Last Week
|
|
135
|
-
$(container).on('click.CalendarRangeSet', '.btn_last_week', (e)->
|
|
136
|
-
unbindClickEventBlockTimerange()
|
|
137
|
-
end = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate() - datetime.getDay() + 1)
|
|
138
|
-
start = new Date(end.getFullYear(), end.getMonth(), end.getDate() - 7)
|
|
139
|
-
fillInputs(start, end)
|
|
140
|
-
)
|
|
141
|
-
|
|
142
|
-
# Last Month
|
|
143
|
-
$(container).on('click.CalendarRangeSet', '.btn_last_month', (e)->
|
|
144
|
-
unbindClickEventBlockTimerange()
|
|
145
|
-
end = new Date(datetime.getFullYear(), datetime.getMonth(), 1)
|
|
146
|
-
start = new Date(end.getFullYear(), end.getMonth() - 1, 1)
|
|
147
|
-
fillInputs(start, end)
|
|
148
|
-
)
|
|
149
|
-
|
|
150
|
-
# Outer
|
|
151
|
-
$('body').on('click.CalendarRangeSet', (e)->
|
|
152
|
-
e.stopPropagation()
|
|
153
|
-
if $(e.target).closest('.block_timerange').length == 0
|
|
154
|
-
unbindClickEventBlockTimerange()
|
|
155
|
-
)
|
|
156
|
-
)
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
$.fn.date_range_ext_preset.defaults = {
|
|
160
|
-
# Manual global time shift, from UTC can be +/- number
|
|
161
|
-
hours_offset: 0,
|
|
162
|
-
|
|
163
|
-
# date_to_human_readable = true, then "date_to" consider as including full day without last second
|
|
164
|
-
# For example Today will be:
|
|
165
|
-
# true
|
|
166
|
-
# 2015-06-10 - 2015-06-10
|
|
167
|
-
# 2015-06-10 00:00:00 - 2015-06-10 23:59:59
|
|
168
|
-
# false
|
|
169
|
-
# 2015-06-10 - 2015-06-11
|
|
170
|
-
# 2015-06-10 00:00:00 - 2015-06-11 00:00:00
|
|
171
|
-
date_to_human_readable: false,
|
|
172
|
-
|
|
173
|
-
# Display time or not: 2015-06-10 vs 2015-06-10 00:00:00
|
|
174
|
-
show_time: false,
|
|
175
|
-
|
|
176
|
-
# Array of addition ranges
|
|
177
|
-
# example:
|
|
178
|
-
# {
|
|
179
|
-
# title: 'Last 30 days',
|
|
180
|
-
# start: new Date().setDate((new Date()).getDate() - 30)
|
|
181
|
-
# end: new Date()
|
|
182
|
-
# }
|
|
183
|
-
add_range: []
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
) jQuery
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
$(document).on 'ready', ->
|
|
190
|
-
# Init in forms
|
|
191
|
-
$('.datetime_preset_pair').date_range_ext_preset()
|
|
@@ -1,36 +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
|
-
.block_timerange {
|
|
13
|
-
position: absolute;
|
|
14
|
-
display: block;
|
|
15
|
-
border: 1px solid #000;
|
|
16
|
-
padding: 5px 10px 5px 10px;
|
|
17
|
-
text-align: center;
|
|
18
|
-
background-color: #f4f4f4;
|
|
19
|
-
>div {
|
|
20
|
-
margin-top: 10px;
|
|
21
|
-
&:last-child {
|
|
22
|
-
margin-bottom: 10px;
|
|
23
|
-
}
|
|
24
|
-
span {
|
|
25
|
-
color: #2e86cc;
|
|
26
|
-
cursor: pointer;
|
|
27
|
-
text-decoration: none;
|
|
28
|
-
font-size: 14px;
|
|
29
|
-
border-bottom: 1px dashed;
|
|
30
|
-
&:hover {
|
|
31
|
-
border-bottom: 0 none;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|