blacklight-frontend 9.2.0 → 9.3.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.
@@ -1,50 +1,52 @@
1
- import debounce from "blacklight-frontend/debounce";
1
+ import debounce from 'blacklight-frontend/debounce'
2
2
 
3
3
  const FacetSuggest = async (e) => {
4
4
  if (e.target.matches('.facet-suggest')) {
5
- const queryFragment = e.target.value?.trim();
6
- const facetField = e.target.dataset.facetField;
7
- const facetArea = document.querySelector('.facet-extended-list');
8
- const prevNextLinks = document.querySelectorAll('.prev_next_links');
5
+ const queryFragment = e.target.value?.trim()
6
+ const facetField = e.target.dataset.facetField
7
+ const facetArea = document.querySelector('.facet-extended-list')
8
+ const prevNextLinks = document.querySelectorAll('.prev_next_links')
9
9
 
10
- if (!facetField) { return; }
10
+ if (!facetField) {
11
+ return
12
+ }
11
13
 
12
14
  // Get the search params from the current query so the facet suggestions
13
15
  // can retain that context.
14
- const facetSearchContext = e.target.dataset.facetSearchContext;
15
- const url = new URL(facetSearchContext, window.location.origin);
16
+ const facetSearchContext = e.target.dataset.facetSearchContext
17
+ const url = new URL(facetSearchContext, window.location.origin)
16
18
 
17
19
  // Drop facet.page so a filtered suggestion list will always start on page 1
18
- url.searchParams.delete('facet.page');
20
+ url.searchParams.delete('facet.page')
19
21
  // add our queryFragment for facet filtering
20
- url.searchParams.append('query_fragment', queryFragment);
22
+ url.searchParams.append('query_fragment', queryFragment)
21
23
 
22
- const facetSearchParams = url.searchParams.toString();
23
- const basePathComponent = url.pathname.split('/')[1];
24
+ const facetSearchParams = url.searchParams.toString()
25
+ const basePathComponent = url.pathname.split('/')[1]
24
26
 
25
- const urlToFetch = `/${basePathComponent}/facet_suggest/${facetField}?${facetSearchParams}`;
27
+ const urlToFetch = `/${basePathComponent}/facet_suggest/${facetField}?${facetSearchParams}`
26
28
 
27
- const response = await fetch(urlToFetch);
29
+ const response = await fetch(urlToFetch)
28
30
  if (response.ok) {
29
- const blob = await response.blob()
30
- const text = await blob.text()
31
+ const blob = await response.blob()
32
+ const text = await blob.text()
31
33
 
32
- if (text && facetArea) {
33
- facetArea.innerHTML = text
34
- }
34
+ if (text && facetArea) {
35
+ facetArea.innerHTML = text
36
+ }
35
37
  }
36
38
 
37
39
  // Hide the prev/next links when a user enters text in the facet
38
40
  // suggestion input. They don't work with a filtered list.
39
- prevNextLinks.forEach(element => {
40
- element.classList.toggle('invisible', !!queryFragment);
41
- });
41
+ prevNextLinks.forEach((element) => {
42
+ element.classList.toggle('invisible', !!queryFragment)
43
+ })
42
44
 
43
45
  // Add a class to distinguish suggested facet values vs. regular.
44
- facetArea.classList.toggle('facet-suggestions', !!queryFragment);
46
+ facetArea.classList.toggle('facet-suggestions', !!queryFragment)
45
47
  }
46
- };
48
+ }
47
49
 
48
- document.addEventListener('input', debounce(FacetSuggest));
50
+ document.addEventListener('input', debounce(FacetSuggest))
49
51
 
50
52
  export default FacetSuggest
@@ -14,5 +14,5 @@ export default {
14
14
  Modal,
15
15
  SearchContext,
16
16
  Core,
17
- onLoad: Core.onLoad
17
+ onLoad: Core.onLoad,
18
18
  }
@@ -57,11 +57,11 @@ const Modal = (() => {
57
57
  const modal = {}
58
58
 
59
59
  // a Bootstrap modal div that should be already on the page hidden
60
- modal.modalSelector = '#blacklight-modal';
60
+ modal.modalSelector = '#blacklight-modal'
61
61
 
62
62
  // Trigger selectors identify forms or hyperlinks that should open
63
63
  // inside a modal dialog.
64
- modal.triggerLinkSelector = 'a[data-blacklight-modal~=trigger]';
64
+ modal.triggerLinkSelector = 'a[data-blacklight-modal~=trigger]'
65
65
 
66
66
  // preserve selectors identify forms or hyperlinks that, if activated already
67
67
  // inside a modal dialog, should have destinations remain inside the modal -- but
@@ -70,16 +70,17 @@ const Modal = (() => {
70
70
  // No need to repeat selectors from trigger selectors, those will already
71
71
  // be preserved. MUST be manually prefixed with the modal selector,
72
72
  // so they only apply to things inside a modal.
73
- modal.preserveLinkSelector = modal.modalSelector + ' a[data-blacklight-modal~=preserve]';
73
+ modal.preserveLinkSelector =
74
+ modal.modalSelector + ' a[data-blacklight-modal~=preserve]'
74
75
 
75
- modal.containerSelector = '[data-blacklight-modal~=container]';
76
+ modal.containerSelector = '[data-blacklight-modal~=container]'
76
77
 
77
78
  // Called on fatal failure of ajax load, function returns content
78
79
  // to show to user in modal. Right now called only for network errors.
79
80
  modal.onFailure = function (error) {
80
- console.error('Server error:', this.url, error);
81
+ console.error('Server error:', this.url, error)
81
82
 
82
- const contents = `<div class="modal-header">
83
+ const contents = `<div class="modal-header">
83
84
  <div class="modal-title">There was a problem with your request.</div>
84
85
  <button type="button" class="blacklight-modal-close btn-close" data-bl-dismiss="modal" aria-label="Close">
85
86
  </button>
@@ -89,17 +90,17 @@ const Modal = (() => {
89
90
  <pre>${this.url}\n${error}</pre>
90
91
  </div>`
91
92
 
92
- modal.target().querySelector('.modal-content').innerHTML = contents
93
+ modal.target().querySelector('.modal-content').innerHTML = contents
93
94
 
94
- modal.show();
95
+ modal.show()
95
96
  }
96
97
 
97
98
  // Add the passed in contents to the modal and display it.
98
99
  // We have specific handling so that scripts returned from the ajax call are executed.
99
100
  // This enables adding a script like recaptcha to prevent bots from sending emails.
100
101
  modal.receiveAjax = function (contents) {
101
- const domparser = new DOMParser();
102
- const dom = domparser.parseFromString(contents, "text/html")
102
+ const domparser = new DOMParser()
103
+ const dom = domparser.parseFromString(contents, 'text/html')
103
104
  // If there is a containerSelector on the document, use its children.
104
105
  let elements = dom.querySelectorAll(`${modal.containerSelector} > *`)
105
106
  const frag = document.createDocumentFragment()
@@ -113,13 +114,16 @@ const Modal = (() => {
113
114
  modal.target().querySelector('.modal-content').replaceChildren(frag)
114
115
 
115
116
  // send custom event with the modal dialog div as the target
116
- var e = new CustomEvent('loaded.blacklight.blacklight-modal', { bubbles: true, cancelable: true });
117
+ var e = new CustomEvent('loaded.blacklight.blacklight-modal', {
118
+ bubbles: true,
119
+ cancelable: true,
120
+ })
117
121
  modal.target().dispatchEvent(e)
118
122
 
119
123
  // if they did preventDefault, don't show the dialog
120
- if (e.defaultPrevented) return;
121
- modal.show();
122
- };
124
+ if (e.defaultPrevented) return
125
+ modal.show()
126
+ }
123
127
 
124
128
  // DOMParser doesn't allow scripts to be executed. This fixes that.
125
129
  modal.activateScripts = function (frag) {
@@ -131,86 +135,99 @@ const Modal = (() => {
131
135
  })
132
136
  }
133
137
 
134
- modal.modalAjaxLinkClick = function(e) {
135
- e.preventDefault();
138
+ modal.modalAjaxLinkClick = function (e) {
139
+ e.preventDefault()
136
140
  const href = e.target.closest('a').getAttribute('href')
137
- fetch(href, { headers: { 'X-Requested-With': 'XMLHttpRequest' }})
138
- .then(response => {
139
- if (!response.ok) {
140
- throw new TypeError("Request failed");
141
- }
142
- return response.text();
143
- })
144
- .then(data => modal.receiveAjax(data))
145
- .catch(error => modal.onFailure(error))
146
- };
147
-
148
- modal.setupModal = function() {
141
+ fetch(href, { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
142
+ .then((response) => {
143
+ if (!response.ok) {
144
+ throw new TypeError('Request failed')
145
+ }
146
+ return response.text()
147
+ })
148
+ .then((data) => modal.receiveAjax(data))
149
+ .catch((error) => modal.onFailure(error))
150
+ }
151
+
152
+ modal.setupModal = function () {
149
153
  // Register several click handlers in ONE event handler for efficiency
150
154
  //
151
155
  // * close button OR click on backdrop (modal.modalSelector) closes modal
152
156
  // * trigger and preserve link in modal functionality -- if somethign matches both trigger and
153
157
  // preserve, still only called once.
154
158
  document.addEventListener('click', (e) => {
155
- if (e.target.closest(`${modal.triggerLinkSelector}, ${modal.preserveLinkSelector}`))
159
+ if (
160
+ e.target.closest(
161
+ `${modal.triggerLinkSelector}, ${modal.preserveLinkSelector}`,
162
+ )
163
+ )
156
164
  modal.modalAjaxLinkClick(e)
157
- else if (e.target.matches(`${modal.modalSelector}`) || e.target.closest('[data-bl-dismiss="modal"]'))
165
+ else if (
166
+ e.target.matches(`${modal.modalSelector}`) ||
167
+ e.target.closest('[data-bl-dismiss="modal"]')
168
+ )
158
169
  modal.hide()
159
170
  })
160
171
 
161
172
  // Make sure user-agent dismissal of html 'dialog', etc `esc` key, triggers
162
173
  // our hide logic, including events and scroll restoration.
163
- const modalDom = modal.target();
174
+ const modalDom = modal.target()
164
175
  if (modalDom) {
165
176
  modal.target().addEventListener('cancel', (e) => {
166
- e.preventDefault(); // 'hide' will close the modal unless cancelled
177
+ e.preventDefault() // 'hide' will close the modal unless cancelled
167
178
 
168
- modal.hide();
169
- });
179
+ modal.hide()
180
+ })
170
181
  }
171
- };
182
+ }
172
183
 
173
- modal.hide = function (el) {
174
- const dom = modal.target();
184
+ modal.hide = function () {
185
+ const dom = modal.target()
175
186
 
176
187
  if (!dom.open) return
177
188
 
178
- var e = new CustomEvent('hide.blacklight.blacklight-modal', { bubbles: true, cancelable: true });
189
+ var e = new CustomEvent('hide.blacklight.blacklight-modal', {
190
+ bubbles: true,
191
+ cancelable: true,
192
+ })
179
193
  dom.dispatchEvent(e)
180
194
 
181
195
  dom.close()
182
196
 
183
197
  // Turn body scrolling back to what it was
184
- document.body.style["overflow"] = modal.originalBodyOverflow;
185
- document.body.style["padding-right"] = modal.originalBodyPaddingRight;
186
- modal.originalBodyOverflow = undefined;
187
- modal.originalBodyPaddingRight = undefined;
198
+ document.body.style['overflow'] = modal.originalBodyOverflow
199
+ document.body.style['padding-right'] = modal.originalBodyPaddingRight
200
+ modal.originalBodyOverflow = undefined
201
+ modal.originalBodyPaddingRight = undefined
188
202
  }
189
203
 
190
- modal.show = function(el) {
191
- const dom = modal.target();
204
+ modal.show = function () {
205
+ const dom = modal.target()
192
206
 
193
207
  if (dom.open) return
194
208
 
195
- var e = new CustomEvent('show.blacklight.blacklight-modal', { bubbles: true, cancelable: true });
209
+ var e = new CustomEvent('show.blacklight.blacklight-modal', {
210
+ bubbles: true,
211
+ cancelable: true,
212
+ })
196
213
  dom.dispatchEvent(e)
197
214
 
198
215
  dom.showModal()
199
216
 
200
217
  // Turn off body scrolling
201
- modal.originalBodyOverflow = document.body.style['overflow'];
202
- modal.originalBodyPaddingRight = document.body.style['padding-right'];
203
- document.body.style["overflow"] = "hidden"
204
- document.body.style["padding-right"] = "0px"
218
+ modal.originalBodyOverflow = document.body.style['overflow']
219
+ modal.originalBodyPaddingRight = document.body.style['padding-right']
220
+ document.body.style['overflow'] = 'hidden'
221
+ document.body.style['padding-right'] = '0px'
205
222
  }
206
223
 
207
- modal.target = function() {
208
- return document.querySelector(modal.modalSelector);
224
+ modal.target = function () {
225
+ return document.querySelector(modal.modalSelector)
209
226
  }
210
227
 
211
228
  modal.setupModal()
212
229
 
213
- return modal;
230
+ return modal
214
231
  })()
215
232
 
216
233
  export default Modal
@@ -5,50 +5,69 @@ const SearchContext = (e) => {
5
5
  }
6
6
  }
7
7
 
8
- SearchContext.csrfToken = () => document.querySelector('meta[name=csrf-token]')?.content
9
- SearchContext.csrfParam = () => document.querySelector('meta[name=csrf-param]')?.content
8
+ SearchContext.csrfToken = () =>
9
+ document.querySelector('meta[name=csrf-token]')?.content
10
+ SearchContext.csrfParam = () =>
11
+ document.querySelector('meta[name=csrf-param]')?.content
10
12
 
11
13
  // this is the Rails.handleMethod with a couple adjustments, described inline:
12
14
  // first, we're attaching this directly to the event handler, so we can check for meta-keys
13
- SearchContext.handleSearchContextMethod = function(event) {
15
+ SearchContext.handleSearchContextMethod = function (event) {
14
16
  const link = this
15
17
 
16
- // instead of using the normal href, we need to use the context href instead
17
- let href = link.getAttribute('data-context-href')
18
+ // instead of using the normal href, we need to use the context href instead.
19
+ // Only post to same-origin URLs, which also rules out javascript: URLs;
20
+ // otherwise fall through to the link's default behavior.
21
+ const contextUrl = new URL(
22
+ link.getAttribute('data-context-href'),
23
+ window.location.href,
24
+ )
25
+ if (contextUrl.origin !== window.location.origin) return
26
+
18
27
  let target = link.getAttribute('target')
19
28
  let csrfToken = SearchContext.csrfToken()
20
29
  let csrfParam = SearchContext.csrfParam()
21
30
  let form = document.createElement('form')
22
31
  form.method = 'post'
23
- form.action = href
32
+ form.action = contextUrl.href
24
33
 
34
+ const appendHiddenInput = (name, value) => {
35
+ const input = document.createElement('input')
36
+ input.type = 'hidden'
37
+ input.name = name
38
+ input.value = value
39
+ form.appendChild(input)
40
+ }
25
41
 
26
- let formContent = `<input name="_method" value="post" type="hidden" />
27
- <input name="redirect" value="${link.getAttribute('href')}" type="hidden" />`
42
+ appendHiddenInput('_method', 'post')
43
+ appendHiddenInput('redirect', link.getAttribute('href'))
28
44
 
29
45
  // check for meta keys.. if set, we should open in a new tab
30
- if(event.metaKey || event.ctrlKey) {
31
- form.dataset.turbo = "false";
32
- target = '_blank';
46
+ if (event.metaKey || event.ctrlKey) {
47
+ form.dataset.turbo = 'false'
48
+ target = '_blank'
33
49
  }
34
50
 
35
51
  if (csrfParam !== undefined && csrfToken !== undefined) {
36
- formContent += `<input name="${csrfParam}" value="${csrfToken}" type="hidden" />`
52
+ appendHiddenInput(csrfParam, csrfToken)
37
53
  }
38
54
 
39
55
  // Must trigger submit by click on a button, else "submit" event handler won't work!
40
56
  // https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit
41
- formContent += '<input type="submit" />'
57
+ const submit = document.createElement('input')
58
+ submit.type = 'submit'
59
+ form.appendChild(submit)
42
60
 
43
- if (target) { form.setAttribute('target', target); }
61
+ if (target) {
62
+ form.setAttribute('target', target)
63
+ }
44
64
 
45
65
  form.style.display = 'none'
46
- form.innerHTML = formContent
47
66
  document.body.appendChild(form)
48
- form.querySelector('[type="submit"]').click()
67
+ submit.click()
49
68
 
50
69
  event.preventDefault()
51
- };
70
+ }
52
71
 
53
72
  document.addEventListener('click', SearchContext)
54
73
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blacklight-frontend",
3
- "version": "9.2.0",
3
+ "version": "9.3.0",
4
4
  "description": "The frontend code and styles for Blacklight",
5
5
  "exports": {
6
6
  "./blacklight.esm.js": "./app/assets/javascripts/blacklight/blacklight.esm.js",
@@ -14,7 +14,9 @@
14
14
  "type": "module",
15
15
  "scripts": {
16
16
  "build": "sass ./app/assets/stylesheets/blacklight/build.scss:./app/assets/builds/blacklight.css --no-source-map --load-path=node_modules --quiet-deps",
17
- "prepare": "rollup --config rollup.config.js --sourcemap && ESM=true rollup --config rollup.config.js --sourcemap"
17
+ "prepare": "rollup --config rollup.config.js --sourcemap && ESM=true rollup --config rollup.config.js --sourcemap",
18
+ "format": "prettier --write \"app/javascript/**/*.js\" \"app/assets/config/**/*.js\" rollup.config.js",
19
+ "format:check": "prettier --check \"app/javascript/**/*.js\" \"app/assets/config/**/*.js\" rollup.config.js"
18
20
  },
19
21
  "repository": {
20
22
  "type": "git",
@@ -31,10 +33,11 @@
31
33
  },
32
34
  "homepage": "https://github.com/projectblacklight/blacklight#readme",
33
35
  "devDependencies": {
36
+ "bootstrap": "^5.3.9",
37
+ "prettier": "3.10.1",
34
38
  "rollup": "^4.24.0",
35
39
  "rollup-plugin-includepaths": "^0.2.4",
36
- "sass": "^1.80.3",
37
- "bootstrap": "^5.3.8"
40
+ "sass": "^1.80.3"
38
41
  },
39
42
  "browserslist": [
40
43
  "defaults",