active_admin_filters_visibility 1.3.0 → 2.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 +110 -119
- data/app/assets/stylesheets/active_admin_filters_visibility.css +132 -0
- data/app/javascript/active_admin_filters_visibility.js +310 -0
- data/config/importmap.rb +3 -0
- data/lib/active_admin_filters_visibility/version.rb +1 -1
- data/lib/active_admin_filters_visibility.rb +20 -0
- metadata +10 -8
- data/app/assets/javascripts/active_admin_filters_visibility.js +0 -517
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ef2f0a0b40aeab41223bab4bdbf833331ff90dd8a42be7594cc40a671f7d7933
|
|
4
|
+
data.tar.gz: 0d301b7a25542654538e829dc4ef5876b4a1d8b1e6bf591c5fdd610f3559883d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7fd066e68a3ec246acc07d945ca2b408120951310e4c649619e3e54582059d88bfd6c3b405d98cd8c6c2aa0be83eecb39a64c9c0a61859406d13e4d435d321b7
|
|
7
|
+
data.tar.gz: c003498a330c464ff2fe298c8befa69a2ec24abc0ab12263a08fd8b8241aa4241491da30df5bed273ec491534a1455bd8b5682b6371fe03b5da8310d27689c5a
|
data/README.md
CHANGED
|
@@ -4,174 +4,165 @@
|
|
|
4
4
|
|
|
5
5
|
# ActiveAdminFiltersVisibility
|
|
6
6
|
|
|
7
|
-
ActiveAdmin
|
|
8
|
-
Useful when page has many filters, but admin user needs only some of them.
|
|
9
|
-
Every filter saves its state using browser's LocalStorage.
|
|
7
|
+
Lets an admin hide filters in the ActiveAdmin filters sidebar, and optionally reorder them. Useful when a page has many filters and each admin needs only some of them. The choice is saved per page in the browser's localStorage.
|
|
10
8
|
|
|
11
|
-

|
|
10
|
+

|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
Version 2 supports ActiveAdmin 4 only. For ActiveAdmin 3, use version 1.x. See [Upgrading from 1.x](#upgrading-from-1x).
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
It works with stock ActiveAdmin 4 and with [active_admin_theme](https://github.com/activeadmin-plugins/active_admin_theme) 4, which wraps the filters sidebar in a panel: the toggle goes into the panel header.
|
|
16
15
|
|
|
17
16
|
## Install
|
|
18
17
|
|
|
19
|
-
In Gemfile add
|
|
20
|
-
|
|
21
18
|
```ruby
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
##### Using assets via Sprockets
|
|
26
|
-
in the
|
|
27
|
-
```
|
|
28
|
-
app/assets/javascript/active_admin.coffee
|
|
19
|
+
gem 'active_admin_filters_visibility'
|
|
29
20
|
```
|
|
30
21
|
|
|
31
|
-
|
|
22
|
+
### JavaScript
|
|
32
23
|
|
|
33
|
-
|
|
34
|
-
#= require active_admin_filters_visibility
|
|
35
|
-
```
|
|
24
|
+
The gem pins the `active_admin_filters_visibility` module into `ActiveAdmin.importmap`, the import map ActiveAdmin pages render. ActiveAdmin pages then need to import it. Pick the route that matches your app:
|
|
36
25
|
|
|
37
|
-
|
|
26
|
+
- **ActiveAdmin with the `javascript_modules` setting** ([activeadmin/activeadmin#9148](https://github.com/activeadmin/activeadmin/pull/9148)): nothing to do. The gem adds itself to that setting.
|
|
27
|
+
- **active_admin_theme 4**: add the module to the theme's list in an initializer:
|
|
38
28
|
|
|
39
|
-
|
|
29
|
+
```ruby
|
|
30
|
+
ActiveAdminTheme.javascripts += %w[active_admin_filters_visibility]
|
|
31
|
+
```
|
|
40
32
|
|
|
41
|
-
|
|
33
|
+
- **Otherwise**: copy `app/views/active_admin/_html_head.html.erb` from ActiveAdmin into your app and add this line after `javascript_importmap_tags`:
|
|
42
34
|
|
|
43
|
-
|
|
35
|
+
```erb
|
|
36
|
+
<%= javascript_import_module_tag "active_admin_filters_visibility" %>
|
|
37
|
+
```
|
|
44
38
|
|
|
45
|
-
|
|
39
|
+
**jsbundling hosts** install the npm package and import it from the ActiveAdmin entry file:
|
|
46
40
|
|
|
47
|
-
|
|
41
|
+
```sh
|
|
42
|
+
npm install @activeadmin-plugins/active_admin_filters_visibility
|
|
43
|
+
```
|
|
48
44
|
|
|
49
|
-
```
|
|
50
|
-
"
|
|
51
|
-
"@activeadmin-plugins/active_admin_filters_visibility": "1.2.0"
|
|
52
|
-
}
|
|
45
|
+
```javascript
|
|
46
|
+
import "@activeadmin-plugins/active_admin_filters_visibility"
|
|
53
47
|
```
|
|
54
|
-
and execute:
|
|
55
48
|
|
|
56
|
-
|
|
49
|
+
The module sets itself up on `#filters_sidebar_section` when the page loads.
|
|
57
50
|
|
|
58
|
-
|
|
51
|
+
### CSS
|
|
59
52
|
|
|
60
|
-
|
|
61
|
-
import '@activeadmin-plugins/active_admin_filters_visibility';
|
|
62
|
-
```
|
|
53
|
+
The styles are plain CSS. Import them into the Tailwind entry file that builds ActiveAdmin's stylesheet (usually `app/assets/stylesheets/active_admin.css`), after `@import "tailwindcss"`.
|
|
63
54
|
|
|
64
|
-
|
|
55
|
+
From the npm package:
|
|
65
56
|
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
$('#filters_sidebar_section').activeAdminFiltersVisibility();
|
|
69
|
-
});
|
|
57
|
+
```css
|
|
58
|
+
@import "@activeadmin-plugins/active_admin_filters_visibility/css";
|
|
70
59
|
```
|
|
71
60
|
|
|
72
|
-
|
|
61
|
+
Or from the installed gem. Its path differs between machines, so write it into a gitignored file before each CSS build instead of committing it:
|
|
73
62
|
|
|
74
|
-
```
|
|
75
|
-
|
|
63
|
+
```sh
|
|
64
|
+
echo "@import \"$(bundle show active_admin_filters_visibility)/app/assets/stylesheets/active_admin_filters_visibility.css\";" > app/assets/stylesheets/active_admin_filters_visibility.generated.css
|
|
76
65
|
```
|
|
77
66
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
```javascript
|
|
82
|
-
{
|
|
83
|
-
sidebarUniqId: function() {
|
|
84
|
-
return window.location.pathname;
|
|
85
|
-
},
|
|
86
|
-
icon: '☶',
|
|
87
|
-
iconClass: '',
|
|
88
|
-
iconStyle: '',
|
|
89
|
-
skipDefaultCss: false,
|
|
90
|
-
title: 'Visibility:',
|
|
91
|
-
ordering: false,
|
|
92
|
-
orderHint: 'Drag&Drop to reorder filters',
|
|
93
|
-
resetButtonTitle: 'Reset'
|
|
94
|
-
}
|
|
67
|
+
```css
|
|
68
|
+
@import "./active_admin_filters_visibility.generated.css";
|
|
95
69
|
```
|
|
96
70
|
|
|
97
|
-
|
|
98
|
-
Or you can set class("iconClass") for icon or inline styles("iconStyle").
|
|
71
|
+
Dark mode follows the `.dark` class on `<html>`, as ActiveAdmin 4 does. With active_admin_theme the colors follow the theme's `--aat-*` custom properties.
|
|
99
72
|
|
|
100
|
-
|
|
101
|
-
In case you want to use custom CSS, default styling can be ignored:
|
|
102
|
-
set ```skipDefaultCss``` to ```true```
|
|
73
|
+
## Configuration
|
|
103
74
|
|
|
75
|
+
Change `defaults` from a module that ActiveAdmin pages import. The plugin sets itself up on `DOMContentLoaded`, after every module has run, so the new values apply:
|
|
104
76
|
|
|
105
|
-
|
|
77
|
+
```javascript
|
|
78
|
+
import { defaults } from "active_admin_filters_visibility"
|
|
106
79
|
|
|
107
|
-
|
|
80
|
+
defaults.ordering = true
|
|
81
|
+
defaults.title = "Filtres visibles"
|
|
82
|
+
```
|
|
108
83
|
|
|
84
|
+
| Option | Default | Description |
|
|
85
|
+
| --- | --- | --- |
|
|
86
|
+
| `autoInit` | `true` | Set up the filters sidebar on page load. Set to `false` to call `setup()` yourself. |
|
|
87
|
+
| `selector` | `"#filters_sidebar_section"` | The sidebar section to set up on page load. |
|
|
88
|
+
| `storageKey` | `() => window.location.pathname` | The key the state is saved under. One key per page by default. |
|
|
89
|
+
| `ordering` | `false` | Let admins reorder filters by drag and drop, or with Alt+Up and Alt+Down on a focused filter. |
|
|
90
|
+
| `icon` | sliders SVG | HTML for the toggle button. |
|
|
91
|
+
| `buttonLabel` | `"Choose visible filters"` | The toggle's `aria-label` and tooltip. |
|
|
92
|
+
| `title` | `"Visible filters"` | The panel title. |
|
|
93
|
+
| `resetLabel` | `"Reset"` | The reset button text. |
|
|
94
|
+
| `orderHint` | `"Drag filters, or press Alt+Up or Alt+Down on one, to reorder them"` | The hint shown when ordering is on. |
|
|
109
95
|
|
|
110
|
-
|
|
111
|
-
Change text in options: ```title```, ```orderHint``` and ```resetButtonTitle```
|
|
96
|
+
To set up a section by hand, pass options for that call only:
|
|
112
97
|
|
|
98
|
+
```javascript
|
|
99
|
+
import { defaults, setup } from "active_admin_filters_visibility"
|
|
113
100
|
|
|
114
|
-
|
|
101
|
+
defaults.autoInit = false
|
|
102
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
103
|
+
setup(document.getElementById("filters_sidebar_section"), { ordering: true })
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Filters with a value stay visible
|
|
115
108
|
|
|
116
|
-
|
|
117
|
-
This was made for preventing unpredictable filtering: for example when user came to page by some link with predefined filtering params.
|
|
118
|
-
All default filters types(string, numeric, date range, etc.) are implemented.
|
|
119
|
-
But if you have some specific filter, you need to add custom handler.
|
|
109
|
+
A hidden filter that has a value when the page loads stays visible, for example when an admin follows a link with filter params. Otherwise the list would be filtered by something the admin cannot see.
|
|
120
110
|
|
|
121
|
-
|
|
122
|
-
And you want to consider "1" as default value and allow hiding it.
|
|
123
|
-
In this case you should add uniq class to wrapper.
|
|
111
|
+
The plugin detects a value by the filter's type, which is a class on the filter wrapper (`select`, `string`, `date_range`, ...). Select and boolean filters count a selected option with a value. Every other type counts a filled text field or a checked box. To change that for one filter, give its wrapper a class and register a detector for it. A detector you register wins over the built-in ones, whatever the class order:
|
|
124
112
|
|
|
125
113
|
```ruby
|
|
126
|
-
filter :id, wrapper_html: { class: '
|
|
114
|
+
filter :id, wrapper_html: { class: 'customized-id' }
|
|
127
115
|
```
|
|
128
116
|
|
|
129
117
|
```javascript
|
|
130
|
-
|
|
131
|
-
// elWrapper is jQuery element "div.filter_form_field.filter_customized_id"
|
|
132
|
-
// if not "1" then consider this filter as active, and return TRUE to prevent hiding
|
|
133
|
-
$.fn.activeAdminFiltersVisibility.registerFilterType('customized_id', function(elWrapper) {
|
|
134
|
-
var inputValue = elWrapper.find('input').val();
|
|
135
|
-
return inputValue != '1';
|
|
136
|
-
});
|
|
137
|
-
```
|
|
118
|
+
import { registerFilterType } from "active_admin_filters_visibility"
|
|
138
119
|
|
|
120
|
+
// Return true when the filter has a value. "1" counts as empty here.
|
|
121
|
+
registerFilterType("customized-id", (wrapper) => {
|
|
122
|
+
const value = wrapper.querySelector("input:not([type=hidden])").value
|
|
123
|
+
return value !== "" && value !== "1"
|
|
124
|
+
})
|
|
125
|
+
```
|
|
139
126
|
|
|
140
127
|
### Saving state
|
|
141
128
|
|
|
142
|
-
|
|
143
|
-
If you need to save this in cookies or user profile, you should write your own implementation - rewrite "$.fn.activeAdminFiltersVisibility.storage".
|
|
144
|
-
For example:
|
|
129
|
+
The state is saved in localStorage under `active_admin_filters_visibility:<storageKey>`, as JSON: `{"hidden": ["q_name_input"], "order": [...]}`. Filters are identified by the id of their wrapper (`q_<attribute>_input`). To save it elsewhere, replace the `storage` methods:
|
|
145
130
|
|
|
146
131
|
```javascript
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
132
|
+
import { storage } from "active_admin_filters_visibility"
|
|
133
|
+
|
|
134
|
+
storage.load = (key) => myStore.read(key) || {}
|
|
135
|
+
storage.save = (key, state) => myStore.write(key, state)
|
|
136
|
+
storage.clear = (key) => myStore.delete(key)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Test hooks
|
|
140
|
+
|
|
141
|
+
The markup the plugin adds carries `data-*` attributes, so tests do not depend on CSS classes:
|
|
142
|
+
|
|
143
|
+
- `data-filters-visibility` on the sidebar section once it is set up.
|
|
144
|
+
- `data-filters-visibility-header` on the header that holds the toggle.
|
|
145
|
+
- `data-filters-visibility-toggle` on the toggle button. It also has `aria-expanded`, and `data-filters-visibility-active` while any filter is hidden.
|
|
146
|
+
- `data-filters-visibility-panel` on the panel.
|
|
147
|
+
- `data-filters-visibility-item="<wrapper id>"` on each row and `data-filters-visibility-checkbox="<wrapper id>"` on its checkbox.
|
|
148
|
+
- `data-filters-visibility-reset` on the reset button.
|
|
149
|
+
- `data-filters-visibility-hidden` on a hidden filter wrapper.
|
|
150
|
+
|
|
151
|
+
## Upgrading from 1.x
|
|
152
|
+
|
|
153
|
+
- ActiveAdmin 4 only. ActiveAdmin 3 apps stay on 1.x.
|
|
154
|
+
- No jQuery and no Sprockets. The asset is an ES module loaded through ActiveAdmin's import map or the npm package, and it sets itself up: remove the `$('#filters_sidebar_section').activeAdminFiltersVisibility()` call.
|
|
155
|
+
- Options moved from the jQuery call to `defaults`. `iconClass`, `iconStyle` and `skipDefaultCss` are gone: style the toggle with CSS, or leave the stylesheet out. `sidebarUniqId` is now `storageKey`. `$.fn.activeAdminFiltersVisibility.registerFilterType` and `.storage` are now the `registerFilterType` export and the `storage` object.
|
|
156
|
+
- The styles are a stylesheet you import into the ActiveAdmin Tailwind build. They are no longer injected at runtime.
|
|
157
|
+
- Saved state uses a new key and format, and identifies filters by wrapper id instead of label text. Hidden filters and custom orders saved by 1.x are not carried over.
|
|
158
|
+
- Reset is always shown and no longer reloads the page.
|
|
159
|
+
- Filter type detectors are keyed by any class on the wrapper, such as Formtastic's type (`select`) or a class you set, instead of a `filter_<type>` class.
|
|
160
|
+
|
|
161
|
+
## Development
|
|
162
|
+
|
|
163
|
+
```sh
|
|
164
|
+
bundle exec rspec
|
|
165
|
+
BUNDLE_GEMFILE=gemfiles/active_admin_theme.gemfile bundle exec rspec
|
|
177
166
|
```
|
|
167
|
+
|
|
168
|
+
The second run uses active_admin_theme from git. Set `ACTIVE_ADMIN_THEME_PATH` to use a local checkout. The specs build the dummy app's stylesheet with the standalone Tailwind CLI from `tailwindcss-ruby`.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Styles for active_admin_filters_visibility. Plain CSS: import it into the
|
|
3
|
+
* ActiveAdmin Tailwind entry file after `@import "tailwindcss"`.
|
|
4
|
+
*
|
|
5
|
+
* Colors are custom properties. They follow active_admin_theme's `--aat-*`
|
|
6
|
+
* colors when the theme is present and fall back to ActiveAdmin 4's grays.
|
|
7
|
+
* Dark mode keys on the `.dark` class, as ActiveAdmin 4 does.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
.filters-visibility-header,
|
|
11
|
+
.filters-visibility-panel {
|
|
12
|
+
--filters-visibility-text: var(--aat-text, #374151);
|
|
13
|
+
--filters-visibility-muted: var(--aat-text-muted, #6b7280);
|
|
14
|
+
--filters-visibility-border: var(--aat-border, #e5e7eb);
|
|
15
|
+
--filters-visibility-surface: var(--aat-surface-2, #f9fafb);
|
|
16
|
+
--filters-visibility-hover: var(--aat-surface-hover, #f3f4f6);
|
|
17
|
+
--filters-visibility-accent: var(--aat-link, #2563eb);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
:where(.dark, .dark *):is(.filters-visibility-header, .filters-visibility-panel) {
|
|
21
|
+
--filters-visibility-text: var(--aat-text, #e5e7eb);
|
|
22
|
+
--filters-visibility-muted: var(--aat-text-muted, #9ca3af);
|
|
23
|
+
--filters-visibility-border: var(--aat-border, #374151);
|
|
24
|
+
--filters-visibility-surface: var(--aat-surface-2, #1f2937);
|
|
25
|
+
--filters-visibility-hover: var(--aat-surface-hover, #374151);
|
|
26
|
+
--filters-visibility-accent: var(--aat-link, #60a5fa);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.filters-visibility-header {
|
|
30
|
+
display: flex;
|
|
31
|
+
align-items: center;
|
|
32
|
+
justify-content: space-between;
|
|
33
|
+
gap: 0.5rem;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.filters-visibility-toggle {
|
|
37
|
+
display: inline-flex;
|
|
38
|
+
flex: none;
|
|
39
|
+
align-items: center;
|
|
40
|
+
justify-content: center;
|
|
41
|
+
width: 1.75rem;
|
|
42
|
+
height: 1.75rem;
|
|
43
|
+
padding: 0;
|
|
44
|
+
border: 0;
|
|
45
|
+
border-radius: 0.25rem;
|
|
46
|
+
background: transparent;
|
|
47
|
+
color: var(--filters-visibility-muted);
|
|
48
|
+
font: inherit;
|
|
49
|
+
cursor: pointer;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.filters-visibility-toggle:hover,
|
|
53
|
+
.filters-visibility-toggle[aria-expanded="true"] {
|
|
54
|
+
background-color: var(--filters-visibility-hover);
|
|
55
|
+
color: var(--filters-visibility-text);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.filters-visibility-toggle:focus-visible {
|
|
59
|
+
outline: 2px solid var(--filters-visibility-accent);
|
|
60
|
+
outline-offset: 1px;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* Some filters are hidden. */
|
|
64
|
+
.filters-visibility-toggle-active,
|
|
65
|
+
.filters-visibility-toggle-active:hover {
|
|
66
|
+
color: var(--filters-visibility-accent);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.filters-visibility-panel {
|
|
70
|
+
margin-bottom: 1rem;
|
|
71
|
+
padding: 0.5rem 0.625rem;
|
|
72
|
+
border: 1px solid var(--filters-visibility-border);
|
|
73
|
+
border-radius: 0.375rem;
|
|
74
|
+
background-color: var(--filters-visibility-surface);
|
|
75
|
+
color: var(--filters-visibility-text);
|
|
76
|
+
font-size: 0.875rem;
|
|
77
|
+
font-weight: normal;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.filters-visibility-panel[hidden],
|
|
81
|
+
.filters-form-field[data-filters-visibility-hidden] {
|
|
82
|
+
display: none;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.filters-visibility-panel-header {
|
|
86
|
+
display: flex;
|
|
87
|
+
align-items: center;
|
|
88
|
+
justify-content: space-between;
|
|
89
|
+
gap: 0.5rem;
|
|
90
|
+
margin-bottom: 0.375rem;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.filters-visibility-reset {
|
|
94
|
+
padding: 0;
|
|
95
|
+
border: 0;
|
|
96
|
+
background: transparent;
|
|
97
|
+
color: var(--filters-visibility-accent);
|
|
98
|
+
font: inherit;
|
|
99
|
+
cursor: pointer;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.filters-visibility-reset:hover {
|
|
103
|
+
text-decoration: underline;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.filters-visibility-list {
|
|
107
|
+
margin: 0;
|
|
108
|
+
padding: 0;
|
|
109
|
+
list-style: none;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.filters-visibility-item label {
|
|
113
|
+
display: flex;
|
|
114
|
+
align-items: center;
|
|
115
|
+
gap: 0.375rem;
|
|
116
|
+
padding: 0.125rem 0;
|
|
117
|
+
cursor: pointer;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.filters-visibility-item[draggable="true"] label {
|
|
121
|
+
cursor: grab;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.filters-visibility-item[data-filters-visibility-dragging] {
|
|
125
|
+
opacity: 0.5;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.filters-visibility-hint {
|
|
129
|
+
margin: 0.375rem 0 0;
|
|
130
|
+
color: var(--filters-visibility-muted);
|
|
131
|
+
font-size: 0.75rem;
|
|
132
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
// Lets an admin hide, show and (optionally) reorder individual filters in the
|
|
2
|
+
// ActiveAdmin 4 filters sidebar. The choice is kept per page in localStorage.
|
|
3
|
+
|
|
4
|
+
const STORAGE_PREFIX = "active_admin_filters_visibility:"
|
|
5
|
+
|
|
6
|
+
const ICON =
|
|
7
|
+
'<svg aria-hidden="true" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
|
|
8
|
+
'<path d="M4 6h10M18 6h2M4 12h4M12 12h8M4 18h12M20 18h0"/><circle cx="16" cy="6" r="2"/><circle cx="10" cy="12" r="2"/><circle cx="18" cy="18" r="2"/></svg>'
|
|
9
|
+
|
|
10
|
+
export const defaults = {
|
|
11
|
+
// Set to false to call setup() yourself.
|
|
12
|
+
autoInit: true,
|
|
13
|
+
selector: "#filters_sidebar_section",
|
|
14
|
+
// One stored state per page.
|
|
15
|
+
storageKey: () => window.location.pathname,
|
|
16
|
+
ordering: false,
|
|
17
|
+
icon: ICON,
|
|
18
|
+
buttonLabel: "Choose visible filters",
|
|
19
|
+
title: "Visible filters",
|
|
20
|
+
resetLabel: "Reset",
|
|
21
|
+
orderHint: "Drag filters, or press Alt+Up or Alt+Down on one, to reorder them"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const storage = {
|
|
25
|
+
load(key) {
|
|
26
|
+
try {
|
|
27
|
+
const state = JSON.parse(window.localStorage.getItem(STORAGE_PREFIX + key))
|
|
28
|
+
return state && typeof state === "object" ? state : {}
|
|
29
|
+
} catch {
|
|
30
|
+
return {}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
save(key, state) {
|
|
34
|
+
try {
|
|
35
|
+
window.localStorage.setItem(STORAGE_PREFIX + key, JSON.stringify(state))
|
|
36
|
+
} catch {
|
|
37
|
+
// Storage full or disabled: the choice lasts until the page reloads.
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
clear(key) {
|
|
41
|
+
try {
|
|
42
|
+
window.localStorage.removeItem(STORAGE_PREFIX + key)
|
|
43
|
+
} catch {
|
|
44
|
+
// Same as save.
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// A filter that has a value is never hidden on page load, so a link with
|
|
50
|
+
// filter params cannot filter by something the admin cannot see. Detectors
|
|
51
|
+
// are keyed by a class on the filter wrapper: Formtastic's input type
|
|
52
|
+
// ("select", "string", ...) or a class set with `wrapper_html`.
|
|
53
|
+
const hasEnteredValue = (wrapper) =>
|
|
54
|
+
Array.from(wrapper.querySelectorAll("input, textarea")).some((input) => {
|
|
55
|
+
if (input.disabled || ["hidden", "submit", "button", "reset"].includes(input.type)) return false
|
|
56
|
+
if (input.type === "checkbox" || input.type === "radio") return input.checked && input.value !== ""
|
|
57
|
+
return input.value !== ""
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
const hasSelectedValue = (wrapper) =>
|
|
61
|
+
Array.from(wrapper.querySelectorAll("select")).some((select) =>
|
|
62
|
+
Array.from(select.selectedOptions).some((option) => option.value !== "")
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
export const filterTypes = {
|
|
66
|
+
default: hasEnteredValue,
|
|
67
|
+
select: hasSelectedValue,
|
|
68
|
+
boolean: hasSelectedValue
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const builtInTypes = new Set(Object.keys(filterTypes))
|
|
72
|
+
|
|
73
|
+
export function registerFilterType(name, detector) {
|
|
74
|
+
filterTypes[name] = detector
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// A detector registered for a class beats Formtastic's type class, whatever
|
|
78
|
+
// the class order on the wrapper.
|
|
79
|
+
function isFilled(wrapper) {
|
|
80
|
+
const names = Array.from(wrapper.classList).filter((klass) => klass !== "default" && Object.hasOwn(filterTypes, klass))
|
|
81
|
+
const name = names.find((klass) => !builtInTypes.has(klass)) || names[0]
|
|
82
|
+
return Boolean((filterTypes[name] || filterTypes.default)(wrapper))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function labelFor(wrapper) {
|
|
86
|
+
const label = wrapper.querySelector("label")
|
|
87
|
+
const text = label ? (label.firstChild?.nodeType === Node.TEXT_NODE ? label.firstChild.textContent : label.textContent) : ""
|
|
88
|
+
return text.trim() || wrapper.id
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function sameList(a, b) {
|
|
92
|
+
return a.length === b.length && a.every((value, index) => value === b[index])
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The header the toggle goes into. Stock ActiveAdmin 4 titles the section with
|
|
96
|
+
// h3.filters-form-title. A layout that wraps sidebar sections in a panel (such
|
|
97
|
+
// as active_admin_theme 4) hides that title and shows the panel's instead.
|
|
98
|
+
function findHeader(section, form) {
|
|
99
|
+
const panel = form.closest(".panel")
|
|
100
|
+
const panelTitle = panel && section.contains(panel) && panel.querySelector(":scope > .panel-title")
|
|
101
|
+
return panelTitle || section.querySelector(".filters-form-title")
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function setup(section, options = {}) {
|
|
105
|
+
if (!section || section.hasAttribute("data-filters-visibility")) return null
|
|
106
|
+
|
|
107
|
+
const settings = { ...defaults, ...options }
|
|
108
|
+
const form = section.querySelector("form.filters-form") || section.querySelector("form")
|
|
109
|
+
const header = form && findHeader(section, form)
|
|
110
|
+
if (!header) return null
|
|
111
|
+
|
|
112
|
+
const fields = Array.from(form.querySelectorAll(".filters-form-field[id]")).filter(
|
|
113
|
+
(wrapper) => wrapper.parentElement === form
|
|
114
|
+
)
|
|
115
|
+
if (fields.length === 0) return null
|
|
116
|
+
|
|
117
|
+
section.setAttribute("data-filters-visibility", "")
|
|
118
|
+
const key = settings.storageKey()
|
|
119
|
+
const byId = new Map(fields.map((wrapper) => [wrapper.id, wrapper]))
|
|
120
|
+
const initialOrder = fields.map((wrapper) => wrapper.id)
|
|
121
|
+
const marker = fields[fields.length - 1].nextSibling
|
|
122
|
+
|
|
123
|
+
const saved = storage.load(key)
|
|
124
|
+
let hidden = (Array.isArray(saved.hidden) ? saved.hidden : []).filter((id) => byId.has(id))
|
|
125
|
+
let order = Array.isArray(saved.order) ? saved.order.filter((id) => byId.has(id)) : []
|
|
126
|
+
if (order.length) order = order.concat(initialOrder.filter((id) => !order.includes(id)))
|
|
127
|
+
|
|
128
|
+
const save = () => {
|
|
129
|
+
const state = { hidden }
|
|
130
|
+
if (settings.ordering && order.length && !sameList(order, initialOrder)) state.order = order
|
|
131
|
+
if (state.hidden.length || state.order) {
|
|
132
|
+
storage.save(key, state)
|
|
133
|
+
} else {
|
|
134
|
+
storage.clear(key)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Toggle button in the header.
|
|
139
|
+
const panelId = `filters-visibility-panel-${section.id || "filters"}`
|
|
140
|
+
const toggle = document.createElement("button")
|
|
141
|
+
toggle.type = "button"
|
|
142
|
+
toggle.className = "filters-visibility-toggle"
|
|
143
|
+
toggle.setAttribute("data-filters-visibility-toggle", "")
|
|
144
|
+
toggle.setAttribute("aria-expanded", "false")
|
|
145
|
+
toggle.setAttribute("aria-controls", panelId)
|
|
146
|
+
toggle.setAttribute("aria-label", settings.buttonLabel)
|
|
147
|
+
toggle.title = settings.buttonLabel
|
|
148
|
+
toggle.innerHTML = settings.icon
|
|
149
|
+
header.classList.add("filters-visibility-header")
|
|
150
|
+
header.setAttribute("data-filters-visibility-header", "")
|
|
151
|
+
header.appendChild(toggle)
|
|
152
|
+
|
|
153
|
+
// Settings panel above the form.
|
|
154
|
+
const panel = document.createElement("div")
|
|
155
|
+
panel.id = panelId
|
|
156
|
+
panel.className = "filters-visibility-panel"
|
|
157
|
+
panel.setAttribute("data-filters-visibility-panel", "")
|
|
158
|
+
panel.hidden = true
|
|
159
|
+
|
|
160
|
+
const bar = document.createElement("div")
|
|
161
|
+
bar.className = "filters-visibility-panel-header"
|
|
162
|
+
const title = document.createElement("strong")
|
|
163
|
+
title.textContent = settings.title
|
|
164
|
+
const reset = document.createElement("button")
|
|
165
|
+
reset.type = "button"
|
|
166
|
+
reset.className = "filters-visibility-reset"
|
|
167
|
+
reset.setAttribute("data-filters-visibility-reset", "")
|
|
168
|
+
reset.textContent = settings.resetLabel
|
|
169
|
+
bar.append(title, reset)
|
|
170
|
+
|
|
171
|
+
const list = document.createElement("ul")
|
|
172
|
+
list.className = "filters-visibility-list"
|
|
173
|
+
panel.append(bar, list)
|
|
174
|
+
|
|
175
|
+
if (settings.ordering) {
|
|
176
|
+
const hint = document.createElement("p")
|
|
177
|
+
hint.className = "filters-visibility-hint"
|
|
178
|
+
hint.textContent = settings.orderHint
|
|
179
|
+
panel.appendChild(hint)
|
|
180
|
+
}
|
|
181
|
+
form.parentElement.insertBefore(panel, form)
|
|
182
|
+
|
|
183
|
+
const items = new Map()
|
|
184
|
+
for (const id of initialOrder) {
|
|
185
|
+
const item = document.createElement("li")
|
|
186
|
+
item.className = "filters-visibility-item"
|
|
187
|
+
item.setAttribute("data-filters-visibility-item", id)
|
|
188
|
+
const label = document.createElement("label")
|
|
189
|
+
const checkbox = document.createElement("input")
|
|
190
|
+
checkbox.type = "checkbox"
|
|
191
|
+
checkbox.value = id
|
|
192
|
+
checkbox.setAttribute("data-filters-visibility-checkbox", id)
|
|
193
|
+
if (settings.ordering) checkbox.setAttribute("aria-keyshortcuts", "Alt+ArrowUp Alt+ArrowDown")
|
|
194
|
+
label.append(checkbox, document.createTextNode(` ${labelFor(byId.get(id))}`))
|
|
195
|
+
item.appendChild(label)
|
|
196
|
+
if (settings.ordering) item.draggable = true
|
|
197
|
+
items.set(id, item)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const setFieldHidden = (id, value) => {
|
|
201
|
+
const wrapper = byId.get(id)
|
|
202
|
+
wrapper.hidden = value
|
|
203
|
+
wrapper.toggleAttribute("data-filters-visibility-hidden", value)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const renderOrder = () => {
|
|
207
|
+
const ids = order.length ? order : initialOrder
|
|
208
|
+
for (const id of ids) {
|
|
209
|
+
list.appendChild(items.get(id))
|
|
210
|
+
form.insertBefore(byId.get(id), marker)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const renderState = () => {
|
|
215
|
+
for (const [id, item] of items) {
|
|
216
|
+
item.querySelector("input").checked = !hidden.includes(id)
|
|
217
|
+
}
|
|
218
|
+
toggle.toggleAttribute("data-filters-visibility-active", hidden.length > 0)
|
|
219
|
+
toggle.classList.toggle("filters-visibility-toggle-active", hidden.length > 0)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
for (const id of initialOrder) setFieldHidden(id, hidden.includes(id) && !isFilled(byId.get(id)))
|
|
223
|
+
if (settings.ordering) renderOrder()
|
|
224
|
+
else for (const id of initialOrder) list.appendChild(items.get(id))
|
|
225
|
+
renderState()
|
|
226
|
+
|
|
227
|
+
toggle.addEventListener("click", () => {
|
|
228
|
+
panel.hidden = !panel.hidden
|
|
229
|
+
toggle.setAttribute("aria-expanded", String(!panel.hidden))
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
list.addEventListener("change", (event) => {
|
|
233
|
+
const id = event.target.getAttribute("data-filters-visibility-checkbox")
|
|
234
|
+
if (!id) return
|
|
235
|
+
hidden = event.target.checked ? hidden.filter((other) => other !== id) : hidden.concat(id)
|
|
236
|
+
setFieldHidden(id, !event.target.checked)
|
|
237
|
+
renderState()
|
|
238
|
+
save()
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
reset.addEventListener("click", () => {
|
|
242
|
+
hidden = []
|
|
243
|
+
order = []
|
|
244
|
+
storage.clear(key)
|
|
245
|
+
for (const id of initialOrder) setFieldHidden(id, false)
|
|
246
|
+
renderOrder()
|
|
247
|
+
renderState()
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
if (settings.ordering) {
|
|
251
|
+
let dragged = null
|
|
252
|
+
list.addEventListener("dragstart", (event) => {
|
|
253
|
+
dragged = event.target.closest("[data-filters-visibility-item]")
|
|
254
|
+
if (!dragged) return
|
|
255
|
+
event.dataTransfer.effectAllowed = "move"
|
|
256
|
+
event.dataTransfer.setData("text/plain", dragged.getAttribute("data-filters-visibility-item"))
|
|
257
|
+
dragged.setAttribute("data-filters-visibility-dragging", "")
|
|
258
|
+
})
|
|
259
|
+
list.addEventListener("dragover", (event) => {
|
|
260
|
+
if (dragged) event.preventDefault()
|
|
261
|
+
})
|
|
262
|
+
list.addEventListener("drop", (event) => {
|
|
263
|
+
const target = event.target.closest("[data-filters-visibility-item]")
|
|
264
|
+
if (!dragged || !target || target === dragged) return
|
|
265
|
+
event.preventDefault()
|
|
266
|
+
const rect = target.getBoundingClientRect()
|
|
267
|
+
const after = event.clientY > rect.top + rect.height / 2
|
|
268
|
+
list.insertBefore(dragged, after ? target.nextSibling : target)
|
|
269
|
+
order = Array.from(list.children).map((item) => item.getAttribute("data-filters-visibility-item"))
|
|
270
|
+
renderOrder()
|
|
271
|
+
save()
|
|
272
|
+
})
|
|
273
|
+
list.addEventListener("dragend", () => {
|
|
274
|
+
dragged?.removeAttribute("data-filters-visibility-dragging")
|
|
275
|
+
dragged = null
|
|
276
|
+
})
|
|
277
|
+
list.addEventListener("keydown", (event) => {
|
|
278
|
+
const step = { ArrowUp: -1, ArrowDown: 1 }[event.key]
|
|
279
|
+
const item = event.target.closest("[data-filters-visibility-item]")
|
|
280
|
+
if (!event.altKey || !step || !item) return
|
|
281
|
+
event.preventDefault()
|
|
282
|
+
const ids = order.length ? order.slice() : initialOrder.slice()
|
|
283
|
+
const from = ids.indexOf(item.getAttribute("data-filters-visibility-item"))
|
|
284
|
+
const to = from + step
|
|
285
|
+
if (to < 0 || to >= ids.length) return
|
|
286
|
+
ids.splice(to, 0, ids.splice(from, 1)[0])
|
|
287
|
+
order = ids
|
|
288
|
+
renderOrder()
|
|
289
|
+
save()
|
|
290
|
+
event.target.focus()
|
|
291
|
+
})
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return { section, toggle, panel }
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function autoSetup() {
|
|
298
|
+
if (defaults.autoInit) document.querySelectorAll(defaults.selector).forEach((section) => setup(section))
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Wait for DOMContentLoaded even when the DOM is already parsed: module
|
|
302
|
+
// scripts run before it fires, so a host module that changes `defaults` after
|
|
303
|
+
// importing this one still gets its settings applied. setup() runs once per
|
|
304
|
+
// section, so the load fallback is harmless.
|
|
305
|
+
if (document.readyState === "complete") {
|
|
306
|
+
window.setTimeout(autoSetup, 0)
|
|
307
|
+
} else {
|
|
308
|
+
document.addEventListener("DOMContentLoaded", autoSetup, { once: true })
|
|
309
|
+
window.addEventListener("load", autoSetup, { once: true })
|
|
310
|
+
}
|
data/config/importmap.rb
ADDED
|
@@ -2,8 +2,28 @@ require 'activeadmin'
|
|
|
2
2
|
require 'active_admin_filters_visibility/version'
|
|
3
3
|
|
|
4
4
|
module ActiveAdminFiltersVisibility
|
|
5
|
+
JAVASCRIPT_MODULE = 'active_admin_filters_visibility'
|
|
6
|
+
|
|
5
7
|
module Rails
|
|
6
8
|
class Engine < ::Rails::Engine
|
|
9
|
+
# ActiveAdmin renders its own import map, ActiveAdmin.importmap, so the
|
|
10
|
+
# module is pinned there. A host without importmap-rails bundles the npm
|
|
11
|
+
# package instead.
|
|
12
|
+
initializer 'active_admin_filters_visibility.importmap', after: 'active_admin.importmap' do |app|
|
|
13
|
+
next unless app.config.respond_to?(:importmap)
|
|
14
|
+
|
|
15
|
+
ActiveAdmin.importmap.draw(root.join('config', 'importmap.rb'))
|
|
16
|
+
app.config.assets.paths << root.join('app/javascript') if app.config.respond_to?(:assets)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# javascript_modules comes from activeadmin/activeadmin#9148. Without it,
|
|
20
|
+
# or without importmap-rails (nothing pinned), the host imports the
|
|
21
|
+
# module itself (see README).
|
|
22
|
+
config.after_initialize do |app|
|
|
23
|
+
if app.config.respond_to?(:importmap) && ActiveAdmin.application.respond_to?(:javascript_modules)
|
|
24
|
+
ActiveAdmin.application.javascript_modules |= [JAVASCRIPT_MODULE]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
7
27
|
end
|
|
8
28
|
end
|
|
9
29
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_admin_filters_visibility
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gena M.
|
|
@@ -15,22 +15,22 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version:
|
|
18
|
+
version: 4.0.0.beta1
|
|
19
19
|
- - "<"
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
|
-
version:
|
|
21
|
+
version: 5.0.0.a
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
25
25
|
requirements:
|
|
26
26
|
- - ">="
|
|
27
27
|
- !ruby/object:Gem::Version
|
|
28
|
-
version:
|
|
28
|
+
version: 4.0.0.beta1
|
|
29
29
|
- - "<"
|
|
30
30
|
- !ruby/object:Gem::Version
|
|
31
|
-
version:
|
|
32
|
-
description: extension for activeadmin gem to hide
|
|
33
|
-
|
|
31
|
+
version: 5.0.0.a
|
|
32
|
+
description: extension for activeadmin gem to hide and reorder filters in the ActiveAdmin
|
|
33
|
+
4 filters sidebar
|
|
34
34
|
email:
|
|
35
35
|
- workgena@gmail.com
|
|
36
36
|
executables: []
|
|
@@ -39,7 +39,9 @@ extra_rdoc_files: []
|
|
|
39
39
|
files:
|
|
40
40
|
- LICENSE
|
|
41
41
|
- README.md
|
|
42
|
-
- app/assets/
|
|
42
|
+
- app/assets/stylesheets/active_admin_filters_visibility.css
|
|
43
|
+
- app/javascript/active_admin_filters_visibility.js
|
|
44
|
+
- config/importmap.rb
|
|
43
45
|
- lib/active_admin_filters_visibility.rb
|
|
44
46
|
- lib/active_admin_filters_visibility/version.rb
|
|
45
47
|
homepage: https://github.com/workgena/active_admin_filters_visibility
|
|
@@ -1,517 +0,0 @@
|
|
|
1
|
-
(function($) {
|
|
2
|
-
|
|
3
|
-
// Plugin definition
|
|
4
|
-
$.fn.activeAdminFiltersVisibility = function(options) {
|
|
5
|
-
var settings = $.extend({}, $.fn.activeAdminFiltersVisibility.defaults, options || {});
|
|
6
|
-
|
|
7
|
-
var storageKey = settings.sidebarUniqId(),
|
|
8
|
-
storage = $.fn.activeAdminFiltersVisibility.storage(storageKey),
|
|
9
|
-
panel = $(this);
|
|
10
|
-
|
|
11
|
-
if (panel.length > 1) {
|
|
12
|
-
console.log('Cant be applied to multiple sidebars');
|
|
13
|
-
return panel;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
function diffArray(arr1, arr2) {
|
|
18
|
-
return arr1.concat(arr2).filter(function (val) {
|
|
19
|
-
if (!(arr1.includes(val) && arr2.includes(val)))
|
|
20
|
-
return val;
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
function findFilterWrapperByLabelText(labelText) {
|
|
27
|
-
return panel.find(".filter_form_field label").filter(function() {
|
|
28
|
-
var nodeText = $(this)[0].childNodes[0].textContent;
|
|
29
|
-
return nodeText === labelText;
|
|
30
|
-
}).closest('.filter_form_field');
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function buildCheckboxesHtml() {
|
|
34
|
-
var html = '';
|
|
35
|
-
|
|
36
|
-
var labels = panel.find('.filter_form_field label:first-child').map(function() {
|
|
37
|
-
var text = $(this)[0].childNodes[0].textContent.trim()
|
|
38
|
-
$(this).closest('.filter_form_field').attr('data-pseudo-id', encodeURIComponent(text));
|
|
39
|
-
return text;
|
|
40
|
-
}).get();
|
|
41
|
-
|
|
42
|
-
var orderedLabels = storage.getOrder();
|
|
43
|
-
|
|
44
|
-
//diffArray
|
|
45
|
-
if (orderedLabels.length > 0) {
|
|
46
|
-
if (diffArray(labels, orderedLabels).length) {
|
|
47
|
-
storage.reset();
|
|
48
|
-
} else {
|
|
49
|
-
labels = orderedLabels;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
$.each(labels, function(i, text) {
|
|
54
|
-
var checked = storage.has(text) ? '' : 'checked="checked"';
|
|
55
|
-
html += '<div><label data-pseudo-id="'+encodeURIComponent(text)+'"><input type="checkbox" value="'+text+'" ' + checked +'> ' + text + '</label></div>';
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return html;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function renderNotice() {
|
|
62
|
-
var btn = panel.find('.filters-visibility-button');
|
|
63
|
-
if (storage.any()) {
|
|
64
|
-
btn.addClass('active');
|
|
65
|
-
} else {
|
|
66
|
-
btn.removeClass('active');
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function render() {
|
|
71
|
-
// Button
|
|
72
|
-
panel.find('h3:first')
|
|
73
|
-
.append('<span class="filters-visibility-button" class="' + settings.iconClass + '" style="' + settings.iconStyle + '">' + settings.icon + '</span>');
|
|
74
|
-
|
|
75
|
-
// Settings Panel
|
|
76
|
-
panel.find('.panel_contents').prepend(
|
|
77
|
-
'<div class="filters-visibility-panel">' +
|
|
78
|
-
'<div><strong>' + settings.title + '</strong></div>' +
|
|
79
|
-
buildCheckboxesHtml() +
|
|
80
|
-
'</div>'
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
// Hide filters
|
|
84
|
-
$.each(storage.all(), function(i, labelText) {
|
|
85
|
-
var elWrapper = findFilterWrapperByLabelText(labelText);
|
|
86
|
-
if ( false == isFilterFilled(elWrapper) ) {
|
|
87
|
-
elWrapper.hide();
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// Mark button if some filters are hidden
|
|
92
|
-
renderNotice();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
function hide(labelText) {
|
|
98
|
-
findFilterWrapperByLabelText(labelText).hide();
|
|
99
|
-
storage.add(labelText);
|
|
100
|
-
renderNotice();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function show(labelText) {
|
|
104
|
-
findFilterWrapperByLabelText(labelText).show();
|
|
105
|
-
storage.remove(labelText);
|
|
106
|
-
renderNotice();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// prevent hiding filter if it is no empty (triggers only on page render)
|
|
110
|
-
function isFilterFilled(elWrapper) {
|
|
111
|
-
if (elWrapper.length == 0) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
var klasses = elWrapper.attr('class').split(' ');
|
|
115
|
-
|
|
116
|
-
var filterTypeName = $.grep(klasses, function(v) {
|
|
117
|
-
return v != 'filter_form_field' && /^filter_/.test(v)
|
|
118
|
-
})[0].replace(/^filter_/, "");
|
|
119
|
-
|
|
120
|
-
function _detectIsFilled(filterTypeName, elWrapper) {
|
|
121
|
-
var functionOrString = $.fn.activeAdminFiltersVisibility.filtersTypesDefinitions[filterTypeName];
|
|
122
|
-
|
|
123
|
-
if (typeof functionOrString == 'string' && functionOrString != filterTypeName) {
|
|
124
|
-
return $.fn.activeAdminFiltersVisibility.filtersTypesDefinitions[functionOrString].call(this, elWrapper);
|
|
125
|
-
} else if (typeof functionOrString == 'function') {
|
|
126
|
-
return functionOrString.call(this, elWrapper);
|
|
127
|
-
} else {
|
|
128
|
-
return $.fn.activeAdminFiltersVisibility.filtersTypesDefinitions['default'].call(this, elWrapper);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return _detectIsFilled(filterTypeName, elWrapper);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
// apply styles
|
|
137
|
-
if (settings.skipDefaultCss == false) {
|
|
138
|
-
$.fn.activeAdminFiltersVisibility.css();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
panel.on('click', '.filters-visibility-button', function() {
|
|
143
|
-
panel.find('.filters-visibility-panel').toggle();
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
panel.on('change', '.filters-visibility-panel input[type=checkbox]', function() {
|
|
147
|
-
var $this = $(this),
|
|
148
|
-
labelText = $this.parent().text().trim();
|
|
149
|
-
|
|
150
|
-
if ($this.prop('checked')) {
|
|
151
|
-
show(labelText);
|
|
152
|
-
} else {
|
|
153
|
-
hide(labelText);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
render();
|
|
158
|
-
|
|
159
|
-
if (settings.ordering) {
|
|
160
|
-
$.fn.activeAdminFiltersVisibility.applyOrder(panel, storage);
|
|
161
|
-
var resetButtonHtml = '<a href="#" class="filters-visibility-reset-btn">' + settings.resetButtonTitle + '</a>';
|
|
162
|
-
panel.find('.filters-visibility-panel div:first')
|
|
163
|
-
.append(resetButtonHtml)
|
|
164
|
-
.after('<div><i>' + settings.orderHint + '</i></div>');
|
|
165
|
-
|
|
166
|
-
panel.on('click', '.filters-visibility-reset-btn', function(e) {
|
|
167
|
-
e.preventDefault();
|
|
168
|
-
storage.reset();
|
|
169
|
-
window.location.reload();
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return panel;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
// Ordering
|
|
177
|
-
$.fn.activeAdminFiltersVisibility.applyOrder = function(panel, storage) {
|
|
178
|
-
|
|
179
|
-
// re-render sidebar filters(even if they are not visible, show/hide event is independent and knows nothing about order)
|
|
180
|
-
function renderReorderedFilters() {
|
|
181
|
-
var wrapper = panel.find('form');
|
|
182
|
-
var order = storage.getOrder();
|
|
183
|
-
|
|
184
|
-
var existingOrder = panel.find('.filter_form_field label:first-child').map(function() {
|
|
185
|
-
return $(this)[0].childNodes[0].textContent.trim();
|
|
186
|
-
}).get();
|
|
187
|
-
|
|
188
|
-
if (order.length > 0 && JSON.stringify(order) != JSON.stringify(existingOrder)) {
|
|
189
|
-
var pos = 0;
|
|
190
|
-
$.each(order, function(i, text) {
|
|
191
|
-
var textEncoded = encodeURIComponent(text);
|
|
192
|
-
var el = wrapper.find('div[data-pseudo-id="' + textEncoded + '"]');
|
|
193
|
-
if (el.length > 0) {
|
|
194
|
-
var elOnThisPosition = wrapper.find('.filter_form_field').eq(pos);
|
|
195
|
-
if (elOnThisPosition.data('pseudo-id') != textEncoded) {
|
|
196
|
-
wrapper[0].insertBefore(el[0], elOnThisPosition[0]);
|
|
197
|
-
}
|
|
198
|
-
pos++;
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
var isOrdered = storage.getOrder().length > 0;
|
|
206
|
-
|
|
207
|
-
// define draggable elements
|
|
208
|
-
|
|
209
|
-
panel.find('.filters-visibility-panel label').attr('draggable', 'true');
|
|
210
|
-
|
|
211
|
-
// Attach events
|
|
212
|
-
panel.on('drop', '.filters-visibility-panel label', function(e) {
|
|
213
|
-
e.preventDefault();
|
|
214
|
-
var drag_el_id = e.originalEvent.dataTransfer.getData("text"),
|
|
215
|
-
drag_el = panel.find('label[data-pseudo-id="' + drag_el_id + '"]'),
|
|
216
|
-
drop_el = $(this),
|
|
217
|
-
wrapper = panel.find('.filters-visibility-panel');
|
|
218
|
-
|
|
219
|
-
if (drag_el != drop_el) {
|
|
220
|
-
wrapper[0].insertBefore(drag_el.parent()[0], drop_el.parent()[0]);
|
|
221
|
-
|
|
222
|
-
var orderedList = wrapper.find('label').map(function() {
|
|
223
|
-
return $(this).text().trim();
|
|
224
|
-
}).get();
|
|
225
|
-
|
|
226
|
-
storage.setOrder(orderedList);
|
|
227
|
-
|
|
228
|
-
renderReorderedFilters();
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
panel.on('dragover', '.filters-visibility-panel', function(e) {
|
|
233
|
-
e.preventDefault();
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
panel.find('.filters-visibility-panel label').on('dragstart', function(e) {
|
|
237
|
-
var key = $(this).data('pseudo-id');
|
|
238
|
-
e.originalEvent.dataTransfer.setData("text", key);
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
// Apply Order
|
|
243
|
-
if (isOrdered) {
|
|
244
|
-
// Apply reorder !!!
|
|
245
|
-
renderReorderedFilters();
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return panel;
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
// Plugin Storage method
|
|
254
|
-
$.fn.activeAdminFiltersVisibility.storage = function(storageUniqId) {
|
|
255
|
-
// https://github.com/tsironis/lockr
|
|
256
|
-
var Lockr = function(){
|
|
257
|
-
if (!Array.prototype.indexOf) {
|
|
258
|
-
Array.prototype.indexOf = function(elt)
|
|
259
|
-
{
|
|
260
|
-
var len = this.length >>> 0;
|
|
261
|
-
|
|
262
|
-
var from = Number(arguments[1]) || 0;
|
|
263
|
-
from = (from < 0)
|
|
264
|
-
? Math.ceil(from)
|
|
265
|
-
: Math.floor(from);
|
|
266
|
-
if (from < 0)
|
|
267
|
-
from += len;
|
|
268
|
-
|
|
269
|
-
for (; from < len; from++)
|
|
270
|
-
{
|
|
271
|
-
if (from in this &&
|
|
272
|
-
this[from] === elt)
|
|
273
|
-
return from;
|
|
274
|
-
}
|
|
275
|
-
return -1;
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
var Lockr = {};
|
|
280
|
-
|
|
281
|
-
Lockr.prefix = "";
|
|
282
|
-
|
|
283
|
-
Lockr._getPrefixedKey = function(key, options) {
|
|
284
|
-
options = options || {};
|
|
285
|
-
|
|
286
|
-
if (options.noPrefix) {
|
|
287
|
-
return key;
|
|
288
|
-
} else {
|
|
289
|
-
return this.prefix + key;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
Lockr.set = function (key, value, options) {
|
|
295
|
-
var query_key = this._getPrefixedKey(key, options);
|
|
296
|
-
|
|
297
|
-
try {
|
|
298
|
-
localStorage.setItem(query_key, JSON.stringify({"data": value}));
|
|
299
|
-
} catch (e) {
|
|
300
|
-
if (console) console.warn("Lockr didn't successfully save the '{"+ key +": "+ value +"}' pair, because the localStorage is full.");
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
Lockr.get = function (key, missing, options) {
|
|
305
|
-
var query_key = this._getPrefixedKey(key, options),
|
|
306
|
-
value;
|
|
307
|
-
|
|
308
|
-
try {
|
|
309
|
-
value = JSON.parse(localStorage.getItem(query_key));
|
|
310
|
-
} catch (e) {
|
|
311
|
-
if(localStorage[query_key]) {
|
|
312
|
-
value = {data: localStorage.getItem(query_key)};
|
|
313
|
-
} else{
|
|
314
|
-
value = null;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
if(value === null) {
|
|
318
|
-
return missing;
|
|
319
|
-
} else if (typeof value === 'object' && typeof value.data !== 'undefined') {
|
|
320
|
-
return value.data;
|
|
321
|
-
} else {
|
|
322
|
-
return missing;
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
Lockr.sadd = function(key, value, options) {
|
|
327
|
-
var query_key = this._getPrefixedKey(key, options),
|
|
328
|
-
json;
|
|
329
|
-
|
|
330
|
-
var values = Lockr.smembers(key);
|
|
331
|
-
|
|
332
|
-
if (values.indexOf(value) > -1) {
|
|
333
|
-
return null;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
try {
|
|
337
|
-
values.push(value);
|
|
338
|
-
json = JSON.stringify({"data": values});
|
|
339
|
-
localStorage.setItem(query_key, json);
|
|
340
|
-
} catch (e) {
|
|
341
|
-
console.log(e);
|
|
342
|
-
if (console) console.warn("Lockr didn't successfully add the "+ value +" to "+ key +" set, because the localStorage is full.");
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
|
|
346
|
-
Lockr.smembers = function(key, options) {
|
|
347
|
-
var query_key = this._getPrefixedKey(key, options),
|
|
348
|
-
value;
|
|
349
|
-
|
|
350
|
-
try {
|
|
351
|
-
value = JSON.parse(localStorage.getItem(query_key));
|
|
352
|
-
} catch (e) {
|
|
353
|
-
value = null;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
if (value === null)
|
|
357
|
-
return [];
|
|
358
|
-
else
|
|
359
|
-
return (value.data || []);
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
Lockr.sismember = function(key, value, options) {
|
|
363
|
-
return Lockr.smembers(key).indexOf(value) > -1;
|
|
364
|
-
};
|
|
365
|
-
|
|
366
|
-
Lockr.keys = function() {
|
|
367
|
-
var keys = [];
|
|
368
|
-
var allKeys = Object.keys(localStorage);
|
|
369
|
-
|
|
370
|
-
if (Lockr.prefix.length === 0) {
|
|
371
|
-
return allKeys;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
allKeys.forEach(function (key) {
|
|
375
|
-
if (key.indexOf(Lockr.prefix) !== -1) {
|
|
376
|
-
keys.push(key.replace(Lockr.prefix, ''));
|
|
377
|
-
}
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
return keys;
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
Lockr.getAll = function () {
|
|
384
|
-
var keys = Lockr.keys();
|
|
385
|
-
return keys.map(function (key) {
|
|
386
|
-
return Lockr.get(key);
|
|
387
|
-
});
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
Lockr.srem = function(key, value, options) {
|
|
391
|
-
var query_key = this._getPrefixedKey(key, options),
|
|
392
|
-
json,
|
|
393
|
-
index;
|
|
394
|
-
|
|
395
|
-
var values = Lockr.smembers(key, value);
|
|
396
|
-
|
|
397
|
-
index = values.indexOf(value);
|
|
398
|
-
|
|
399
|
-
if (index > -1)
|
|
400
|
-
values.splice(index, 1);
|
|
401
|
-
|
|
402
|
-
json = JSON.stringify({"data": values});
|
|
403
|
-
|
|
404
|
-
try {
|
|
405
|
-
localStorage.setItem(query_key, json);
|
|
406
|
-
} catch (e) {
|
|
407
|
-
if (console) console.warn("Lockr couldn't remove the "+ value +" from the set "+ key);
|
|
408
|
-
}
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
Lockr.rm = function (key) {
|
|
412
|
-
localStorage.removeItem(key);
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
Lockr.flush = function () {
|
|
416
|
-
if (Lockr.prefix.length) {
|
|
417
|
-
Lockr.keys().forEach(function(key) {
|
|
418
|
-
localStorage.removeItem(Lockr._getPrefixedKey(key));
|
|
419
|
-
});
|
|
420
|
-
} else {
|
|
421
|
-
localStorage.clear();
|
|
422
|
-
}
|
|
423
|
-
};
|
|
424
|
-
return Lockr;
|
|
425
|
-
|
|
426
|
-
}();
|
|
427
|
-
|
|
428
|
-
var storageIdForOrder = '__ordered__' + storageUniqId;
|
|
429
|
-
|
|
430
|
-
return {
|
|
431
|
-
setOrder: function(orderedList) {
|
|
432
|
-
Lockr.set(storageIdForOrder, orderedList);
|
|
433
|
-
},
|
|
434
|
-
getOrder: function() {
|
|
435
|
-
return Lockr.get(storageIdForOrder) || [];
|
|
436
|
-
},
|
|
437
|
-
reset: function() {
|
|
438
|
-
Lockr.rm(storageIdForOrder);
|
|
439
|
-
Lockr.rm(storageUniqId);
|
|
440
|
-
},
|
|
441
|
-
add: function(key) {
|
|
442
|
-
Lockr.sadd(storageUniqId, key);
|
|
443
|
-
},
|
|
444
|
-
remove: function(key) {
|
|
445
|
-
Lockr.srem(storageUniqId, key);
|
|
446
|
-
},
|
|
447
|
-
has: function(key) {
|
|
448
|
-
return Lockr.sismember(storageUniqId, key);
|
|
449
|
-
},
|
|
450
|
-
all: function() {
|
|
451
|
-
return Lockr.smembers(storageUniqId);
|
|
452
|
-
},
|
|
453
|
-
any: function() {
|
|
454
|
-
return this.all().length > 0;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
// Style
|
|
461
|
-
$.fn.activeAdminFiltersVisibility.css = function() {
|
|
462
|
-
var lines = [];
|
|
463
|
-
|
|
464
|
-
lines.push('.filters-visibility-button { margin: 0 5px 0 5px; padding: 0 4px 0 4px; background-color: rgba(0, 0, 0, 0.05); float: right; cursor: pointer; }');
|
|
465
|
-
lines.push('.filters-visibility-button.active { color: #007ab8; }');
|
|
466
|
-
lines.push('.filters-visibility-panel { display: none; border-left: 10px solid rgba(0, 0, 0, 0.1); background-color: rgba(0, 0, 0, 0.02); margin-bottom: 10px; }');
|
|
467
|
-
lines.push('.filters-visibility-panel > div { margin-left: 5px; }');
|
|
468
|
-
lines.push('.filters-visibility-panel > div:first-child { display: flex; }');
|
|
469
|
-
lines.push('.filters-visibility-panel .filters-visibility-reset-btn { display: inline-block; margin-left: auto; }');
|
|
470
|
-
|
|
471
|
-
var sheet = document.createElement('style');
|
|
472
|
-
sheet.type = 'text/css';
|
|
473
|
-
sheet.innerHTML = lines.join(' ');
|
|
474
|
-
document.body.appendChild(sheet);
|
|
475
|
-
};
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
// Prevent filter from being hidden this filter is was used for filtering data
|
|
479
|
-
$.fn.activeAdminFiltersVisibility.filtersTypesDefinitions = {
|
|
480
|
-
'default': function (elWrapper) {
|
|
481
|
-
var notEmptyInput = elWrapper.find('input[id!=""][type!="hidden"][value][value!=""]');
|
|
482
|
-
return notEmptyInput.length > 0;
|
|
483
|
-
}
|
|
484
|
-
};
|
|
485
|
-
|
|
486
|
-
$.fn.activeAdminFiltersVisibility.registerFilterType = function(name, filterTypeDefinitionFunc) {
|
|
487
|
-
$.fn.activeAdminFiltersVisibility.filtersTypesDefinitions[name] = filterTypeDefinitionFunc;
|
|
488
|
-
};
|
|
489
|
-
|
|
490
|
-
$.fn.activeAdminFiltersVisibility.registerFilterType('check_boxes', function(elWrapper) {
|
|
491
|
-
return elWrapper.find('input[id!=""][type!="hidden"][value][value!=""]:checked').length > 0;
|
|
492
|
-
});
|
|
493
|
-
|
|
494
|
-
$.fn.activeAdminFiltersVisibility.registerFilterType('select', function(elWrapper) {
|
|
495
|
-
return elWrapper.find('select[id]:first').val().length > 0;
|
|
496
|
-
});
|
|
497
|
-
|
|
498
|
-
$.fn.activeAdminFiltersVisibility.registerFilterType('boolean', 'select');
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
// Plugin defaults
|
|
503
|
-
$.fn.activeAdminFiltersVisibility.defaults = {
|
|
504
|
-
sidebarUniqId: function() {
|
|
505
|
-
return window.location.pathname;
|
|
506
|
-
},
|
|
507
|
-
icon: '☶',
|
|
508
|
-
iconClass: '',
|
|
509
|
-
iconStyle: '',
|
|
510
|
-
skipDefaultCss: false,
|
|
511
|
-
ordering: false,
|
|
512
|
-
title: 'Visibility:',
|
|
513
|
-
orderHint: 'Drag&Drop to reorder filters',
|
|
514
|
-
resetButtonTitle: 'Reset'
|
|
515
|
-
};
|
|
516
|
-
|
|
517
|
-
})(jQuery);
|