administrate-field-lazy_belongs_to 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37517c5b81cbf7a693235f819ba70bcd42fab256bdb197e7c1609662ee7584cd
4
- data.tar.gz: d4d593e2ff2fe4b8202bd9606368ba180eb884ef17545354650630bc178b2c2c
3
+ metadata.gz: f95651ee7033646d6e9a40c0e8033bc574944b40b7fa53eb184bc4643f19acb8
4
+ data.tar.gz: 253836f8fb97ac521e0b5650379eb8ec3b9b64deb9482c5e6efe140295c1f683
5
5
  SHA512:
6
- metadata.gz: aaddd4c7eb6e7cf32134c1663b8abe39c11d14a5506a4847d14c1061a526a762f6e109f5d47810a9034cdf4771f08358f0f9e10ede0021a05bb55acdfdc40aaf
7
- data.tar.gz: 6cb45b4569704266d7cbe661d7c4aac68436ff1bcdd3569ad081711d1b5c2f7b3f623fd28182ee3c3616b72ddfdd273c639f3e7273745567271226351dbabf4b
6
+ metadata.gz: '08c4b2efdcfaab90cb9b421e825146e732b2fe6ca6574a3652e2000de70f736f60c56719802aa08de0520bb5483836f198edba001f76d95dcf6dfdb013b24cde'
7
+ data.tar.gz: af592318beb1fee05ada96708cd17c743fc1be5858e20303b8105f3c4fcd7483dcfd634dc2076620765f3d719640388d45b043a5297722f226edbf640bf817aa
@@ -13,6 +13,7 @@ function bindLazyBelongsTos() {
13
13
 
14
14
  let controller = undefined
15
15
  let lastResult = undefined
16
+ let lastDebounce = undefined
16
17
 
17
18
  function onQuery (event) {
18
19
  const value = event.currentTarget.value
@@ -22,47 +23,54 @@ function bindLazyBelongsTos() {
22
23
  controller.abort()
23
24
  }
24
25
 
26
+ if (lastDebounce) {
27
+ clearTimeout(lastDebounce)
28
+ }
29
+
25
30
  controller = new AbortController()
26
31
  const { signal } = controller
27
- console.log({ hidden: target.value, selected: select.value, query: value })
28
-
29
- fetch(options.url.replace('{q}', value).replace('%7Bq%7D', value), {
30
- signal,
31
- headers: { Accept: 'application/json' }
32
- })
33
- .then(r => r.json())
34
- .then(r => r.map(e => ({ value: e[options.value], label: e[options.label] })))
35
- .then(rs => {
36
- const currentResult = JSON.stringify((rs))
37
-
38
- if (lastResult && lastResult === currentResult) {
39
- return
40
- }
41
-
42
- lastResult = currentResult
43
-
44
- while (select.lastChild) {
45
- select.removeChild(select.lastChild);
46
- }
47
-
48
- const currentValue = target.value
49
-
50
- rs.forEach(r => {
51
- const option = document.createElement('option')
52
- option.setAttribute('value', r.value)
53
- option.innerText = r.label
54
- option.selected = currentValue === r.value
55
- select.appendChild(option)
56
- })
57
32
 
58
- select.setAttribute('size', "" + Math.max(2, Math.min(Number(select.getAttribute('data-max-size')), rs.length)))
59
- })
60
- .catch(error => {
61
- if (error.name === 'AbortError') {
62
- return /* ignore, this is an aborted promise */
63
- }
64
- console.error(error)
33
+ lastDebounce = setTimeout(() => {
34
+ lastDebounce = undefined
35
+
36
+ fetch(options.url.replace('{q}', value).replace('%7Bq%7D', value), {
37
+ signal,
38
+ headers: { Accept: 'application/json' }
65
39
  })
40
+ .then(r => r.json())
41
+ .then(r => r.map(e => ({ value: e[options.value], label: e[options.label] })))
42
+ .then(rs => {
43
+ const currentResult = JSON.stringify((rs))
44
+
45
+ if (lastResult && lastResult === currentResult) {
46
+ return
47
+ }
48
+
49
+ lastResult = currentResult
50
+
51
+ while (select.lastChild) {
52
+ select.removeChild(select.lastChild);
53
+ }
54
+
55
+ const currentValue = target.value
56
+
57
+ rs.forEach(r => {
58
+ const option = document.createElement('option')
59
+ option.setAttribute('value', r.value)
60
+ option.innerText = r.label
61
+ option.selected = currentValue === r.value
62
+ select.appendChild(option)
63
+ })
64
+
65
+ select.setAttribute('size', "" + Math.max(2, Math.min(Number(select.getAttribute('data-max-size')), rs.length)))
66
+ })
67
+ .catch(error => {
68
+ if (error.name === 'AbortError') {
69
+ return /* ignore, this is an aborted promise */
70
+ }
71
+ console.error(error)
72
+ })
73
+ }, 250)
66
74
  }
67
75
 
68
76
  function showPopout() {
@@ -1,7 +1,7 @@
1
1
  module Administrate
2
2
  module Field
3
3
  module LazyBelongsToVersion
4
- VERSION = '0.1.1'.freeze
4
+ VERSION = '0.1.2'.freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: administrate-field-lazy_belongs_to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld