administrate-field-lazy_belongs_to 0.1.4 → 0.2.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/.idea/administrate-field-lazy_belongs_to.iml +1 -1
- data/CHANGELOG.md +6 -0
- data/README.md +13 -1
- data/app/assets/javascripts/administrate-field-lazy_belongs_to/components/lazy_belongs_to.js +27 -3
- data/app/assets/stylesheets/administrate-field-lazy_belongs_to/components/lazy_belongs_to.css +4 -4
- data/app/views/fields/lazy_belongs_to/_form.html.erb +4 -4
- data/lib/administrate/field/lazy_belongs_to/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 697cb0a185adc3a2c73fa841b27d3098731e88458e547b2cb97e9c0c25269687
|
4
|
+
data.tar.gz: f5002c9dddc93db61943c89f87b0c268cf70faf96f14d2a9719f6a63c5df2cef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd2bd5a9b03e47ba74983ef4f5d21c88b4bd3ee2781472b32f9c5e6532438c856594af556c8ba6f431cda2a57bbf95aec3a6aebace26e8c48aff37c152a25b83
|
7
|
+
data.tar.gz: 5ee96484f520e9de3090490040c74da16c6cbccbb81917c07b3ffe42dd260a9d7aed67c945dacd75bebf43b52a4e0da6f622a923cfbe4e67d17bb90de883e7d2
|
@@ -47,7 +47,7 @@
|
|
47
47
|
<orderEntry type="library" scope="PROVIDED" name="nokogiri (v1.10.1, ruby-2.5.3-p105) [gem]" level="application" />
|
48
48
|
<orderEntry type="library" scope="PROVIDED" name="rack (v2.0.6, ruby-2.5.3-p105) [gem]" level="application" />
|
49
49
|
<orderEntry type="library" scope="PROVIDED" name="rack-test (v1.1.0, ruby-2.5.3-p105) [gem]" level="application" />
|
50
|
-
<orderEntry type="library" scope="PROVIDED" name="rails (v5.2.
|
50
|
+
<orderEntry type="library" scope="PROVIDED" name="rails (v5.2.3, ruby-2.5.3-p105) [gem]" level="application" />
|
51
51
|
<orderEntry type="library" scope="PROVIDED" name="rails-dom-testing (v2.0.3, ruby-2.5.3-p105) [gem]" level="application" />
|
52
52
|
<orderEntry type="library" scope="PROVIDED" name="rails-html-sanitizer (v1.0.4, ruby-2.5.3-p105) [gem]" level="application" />
|
53
53
|
<orderEntry type="library" scope="PROVIDED" name="railties (v5.2.2.1, ruby-2.5.3-p105) [gem]" level="application" />
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -27,7 +27,10 @@ Or install it yourself as:
|
|
27
27
|
## Usage
|
28
28
|
|
29
29
|
This field needs a bit of setup, unless you're using [XPBytes/administrate-base_controller](https://github.com/XPBytes/administrate-base_controller),
|
30
|
-
in order to lazily collect the values you're looking for
|
30
|
+
in order to lazily collect the values you're looking for. If you _are_ using the `BaseController`, you only need to
|
31
|
+
set-up the [fields](#fields).
|
32
|
+
|
33
|
+
### Controller
|
31
34
|
|
32
35
|
You need to have a route that yields an array of objects with at least the value and label you want to show. The best
|
33
36
|
way is to re-use everything administrate offers you, which is the default behaviour of `Administrate::BaseController`:
|
@@ -54,6 +57,15 @@ end
|
|
54
57
|
This way, your already existing route also is available in JSON. You may optimize this by only use the id and value
|
55
58
|
field: `resources.to_json(fields: %i[id name])`.
|
56
59
|
|
60
|
+
You could also use the dashboard display:
|
61
|
+
```ruby
|
62
|
+
render json: resources.map do |resource|
|
63
|
+
{ id: resource.id, name: dashboard.display_resource(resource) }
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
### Fields
|
68
|
+
|
57
69
|
The rest of the setup is as easy as any other field, except that you need to tell the field where to query the results,
|
58
70
|
which key it should use for the value (defaults to `id`) and the label (defaults to `name`).
|
59
71
|
|
data/app/assets/javascripts/administrate-field-lazy_belongs_to/components/lazy_belongs_to.js
CHANGED
@@ -63,6 +63,11 @@ function bindLazyBelongsTos() {
|
|
63
63
|
})
|
64
64
|
|
65
65
|
select.setAttribute('size', "" + Math.max(2, Math.min(Number(select.getAttribute('data-max-size')), rs.length)))
|
66
|
+
|
67
|
+
// Deselect if there was nothing selected
|
68
|
+
if (!currentValue) {
|
69
|
+
select.selectedIndex = -1
|
70
|
+
}
|
66
71
|
})
|
67
72
|
.catch(error => {
|
68
73
|
if (error.name === 'AbortError') {
|
@@ -91,14 +96,33 @@ function bindLazyBelongsTos() {
|
|
91
96
|
}
|
92
97
|
})
|
93
98
|
|
99
|
+
function pickValue(value, label) {
|
100
|
+
target.value = value
|
101
|
+
button.textContent = label
|
102
|
+
hidePopout()
|
103
|
+
}
|
104
|
+
|
105
|
+
select.addEventListener('click', (e) => {
|
106
|
+
if (e.target.value && e.target.value === e.currentTarget.value) {
|
107
|
+
pickValue(
|
108
|
+
e.target.value,
|
109
|
+
e.target.textContent
|
110
|
+
)
|
111
|
+
|
112
|
+
e.stopImmediatePropagation()
|
113
|
+
return
|
114
|
+
}
|
115
|
+
})
|
116
|
+
|
94
117
|
select.addEventListener('change', (e) => {
|
95
118
|
if (!e.currentTarget.value) {
|
96
119
|
return
|
97
120
|
}
|
98
121
|
|
99
|
-
|
100
|
-
|
101
|
-
|
122
|
+
pickValue(
|
123
|
+
e.currentTarget.value,
|
124
|
+
e.currentTarget.selectedOptions[0].textContent
|
125
|
+
)
|
102
126
|
})
|
103
127
|
|
104
128
|
button.removeAttribute('disabled')
|
data/app/assets/stylesheets/administrate-field-lazy_belongs_to/components/lazy_belongs_to.css
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
.field-unit--lazy-belongs-to .
|
1
|
+
.field-unit--lazy-belongs-to .lazy_belongs_to__popout {
|
2
2
|
display: none;
|
3
3
|
background-color: white;
|
4
|
-
box-shadow: rgba(13, 22, 38, 0.1)
|
4
|
+
box-shadow: rgba(13, 22, 38, 0.1) 0 0 0 1px, rgba(13, 22, 38, 0.1) 0 4px 11px;
|
5
5
|
margin-top: 8px;
|
6
6
|
position: absolute;
|
7
7
|
z-index: 2;
|
8
8
|
border-radius: 4px;
|
9
9
|
}
|
10
10
|
|
11
|
-
.field-unit--lazy-belongs-to .
|
11
|
+
.field-unit--lazy-belongs-to .lazy_belongs_to__popout.active {
|
12
12
|
display: block;
|
13
13
|
}
|
14
14
|
|
15
|
-
.field-unit--lazy-belongs-to .
|
15
|
+
.field-unit--lazy-belongs-to .lazy_belongs_to__popout_search {
|
16
16
|
height: 100%;
|
17
17
|
width: auto;
|
18
18
|
line-height: 38px;
|
@@ -31,17 +31,17 @@ Instead of the default, this only loads the results lazily
|
|
31
31
|
>
|
32
32
|
<%= f.hidden_field(field.permitted_attribute) %>
|
33
33
|
|
34
|
-
<button type="button" disabled="disabled" class="button"><%= field.current_value %></button>
|
34
|
+
<button type="button" disabled="disabled" class="button lazy_belongs_to__button"><%= field.current_value %></button>
|
35
35
|
|
36
|
-
<div class="popout" data-target="popout">
|
37
|
-
<input type="search" class="
|
36
|
+
<div class="popout lazy_belongs_to__popout" data-target="popout">
|
37
|
+
<input type="search" class="lazy_belongs_to__popout_search" placeholder="Search..." aria-label="Search input">
|
38
38
|
<i class="icon">
|
39
39
|
<svg width="18" height="18" viewBox="0 0 24 24" focusable="false" role="presentation">
|
40
40
|
<path d="M16.436 15.085l3.94 4.01a1 1 0 0 1-1.425 1.402l-3.938-4.006a7.5 7.5 0 1 1 1.423-1.406zM10.5 16a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11z" fill="currentColor" fill-rule="evenodd"></path>
|
41
41
|
</svg>
|
42
42
|
</i>
|
43
43
|
<output class="results" data-target="output">
|
44
|
-
<select data-max-size="<%= field.size %>">
|
44
|
+
<select data-max-size="<%= field.size %>" aria-label="Select a result">
|
45
45
|
<option></option>
|
46
46
|
</select>
|
47
47
|
</output>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: administrate-field-lazy_belongs_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derk-Jan Karrenbeld
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: administrate
|
@@ -140,8 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
140
|
- !ruby/object:Gem::Version
|
141
141
|
version: '0'
|
142
142
|
requirements: []
|
143
|
-
|
144
|
-
rubygems_version: 2.7.6
|
143
|
+
rubygems_version: 3.0.3
|
145
144
|
signing_key:
|
146
145
|
specification_version: 4
|
147
146
|
summary: A belongs to-like field that lazily loads candidates from a custom endpoint.
|