blacklight-frontend
Advanced tools
Comparing version 8.0.0-pre.4 to 8.0.0
@@ -7,55 +7,2 @@ (function (global, factory) { | ||
const Blacklight = function() { | ||
const buffer = new Array; | ||
return { | ||
onLoad: function(func) { | ||
buffer.push(func); | ||
}, | ||
activate: function() { | ||
for(let i = 0; i < buffer.length; i++) { | ||
buffer[i].call(); | ||
} | ||
}, | ||
listeners: function () { | ||
const listeners = []; | ||
if (typeof Turbo !== 'undefined') { | ||
listeners.push('turbo:load', 'turbo:frame-load'); | ||
} else if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) { | ||
// Turbolinks 5 | ||
if (Turbolinks.BrowserAdapter) { | ||
listeners.push('turbolinks:load'); | ||
} else { | ||
// Turbolinks < 5 | ||
listeners.push('page:load', 'DOMContentLoaded'); | ||
} | ||
} else { | ||
listeners.push('DOMContentLoaded'); | ||
} | ||
return listeners; | ||
} | ||
}; | ||
}(); | ||
// turbolinks triggers page:load events on page transition | ||
// If app isn't using turbolinks, this event will never be triggered, no prob. | ||
Blacklight.listeners().forEach(function(listener) { | ||
document.addEventListener(listener, function() { | ||
Blacklight.activate(); | ||
}); | ||
}); | ||
Blacklight.onLoad(function () { | ||
const elem = document.querySelector('.no-js'); | ||
// The "no-js" class may already have been removed because this function is | ||
// run on every turbo:load event, in that case, it won't find an element. | ||
if (!elem) return; | ||
elem.classList.remove('no-js'); | ||
elem.classList.add('js'); | ||
}); | ||
/* Converts a "toggle" form, with single submit button to add/remove | ||
@@ -142,28 +89,22 @@ something, like used for Bookmarks, into an AJAXy checkbox instead. | ||
const BookmarkToggle = (() => { | ||
// change form submit toggle to checkbox | ||
Blacklight.doBookmarkToggleBehavior = function() { | ||
document.addEventListener('click', (e) => { | ||
if (e.target.matches('[data-checkboxsubmit-target="checkbox"]')) { | ||
const form = e.target.closest('form'); | ||
if (form) new CheckboxSubmit(form).clicked(e); | ||
} | ||
}); | ||
}; | ||
Blacklight.doBookmarkToggleBehavior.selector = 'form.bookmark-toggle'; | ||
const BookmarkToggle = (e) => { | ||
if (e.target.matches('[data-checkboxsubmit-target="checkbox"]')) { | ||
const form = e.target.closest('form'); | ||
if (form) new CheckboxSubmit(form).clicked(e); | ||
} | ||
}; | ||
Blacklight.doBookmarkToggleBehavior(); | ||
})(); | ||
document.addEventListener('click', BookmarkToggle); | ||
const ButtonFocus = (() => { | ||
document.addEventListener('click', (e) => { | ||
// Button clicks should change focus. As of 10/3/19, Firefox for Mac and | ||
// Safari both do not set focus to a button on button click. | ||
// See https://zellwk.com/blog/inconsistent-button-behavior/ for background information | ||
if (e.target.matches('[data-toggle="collapse"]') || e.target.matches('[data-bs-toggle="collapse"]')) { | ||
e.target.focus(); | ||
} | ||
}); | ||
})(); | ||
const ButtonFocus = (e) => { | ||
// Button clicks should change focus. As of 10/3/19, Firefox for Mac and | ||
// Safari both do not set focus to a button on button click. | ||
// See https://zellwk.com/blog/inconsistent-button-behavior/ for background information | ||
if (e.target.matches('[data-toggle="collapse"]') || e.target.matches('[data-bs-toggle="collapse"]')) { | ||
e.target.focus(); | ||
} | ||
}; | ||
document.addEventListener('click', ButtonFocus); | ||
/* | ||
@@ -203,3 +144,3 @@ The blacklight modal plugin can display some interactions inside a Bootstrap | ||
<div class="modal-header"> | ||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> | ||
<button type="button" class="close" data-bl-dismiss="modal" aria-hidden="true">×</button> | ||
<h3 class="modal-title">Request Placed</h3> | ||
@@ -215,3 +156,3 @@ </div> | ||
<div class="modal-footer"> | ||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> | ||
<button type="button" class="btn btn-secondary" data-bl-dismiss="modal">Close</button> | ||
</div> | ||
@@ -227,10 +168,4 @@ </div> | ||
const Modal = (() => { | ||
// We keep all our data in Blacklight.modal object. | ||
// Create lazily if someone else created first. | ||
if (Blacklight.modal === undefined) { | ||
Blacklight.modal = {}; | ||
} | ||
const modal = {}; | ||
const modal = Blacklight.modal; | ||
// a Bootstrap modal div that should be already on the page hidden | ||
@@ -255,11 +190,10 @@ modal.modalSelector = '#blacklight-modal'; | ||
// Called on fatal failure of ajax load, function returns content | ||
// to show to user in modal. Right now called only for extreme | ||
// network errors. | ||
modal.onFailure = function (jqXHR, textStatus, errorThrown) { | ||
console.error('Server error:', this.url, jqXHR.status, errorThrown); | ||
// to show to user in modal. Right now called only for network errors. | ||
modal.onFailure = function (error) { | ||
console.error('Server error:', this.url, error); | ||
const contents = `<div class="modal-header"> | ||
<div class="modal-title">There was a problem with your request.</div> | ||
<button type="button" class="blacklight-modal-close btn-close close" data-dismiss="modal" data-bs-dismiss="modal" aria-label="Close"> | ||
<span aria-hidden="true">×</span> | ||
<button type="button" class="blacklight-modal-close btn-close close" data-bl-dismiss="modal" aria-label="Close"> | ||
<span aria-hidden="true" class="visually-hidden">×</span> | ||
</button> | ||
@@ -269,3 +203,3 @@ </div> | ||
<p>Expected a successful response from the server, but got an error</p> | ||
<pre>${this.type} ${this.url}\n${jqXHR.status}: ${errorThrown}</pre> | ||
<pre>${this.url}\n${error}</pre> | ||
</div>`; | ||
@@ -279,6 +213,12 @@ | ||
// Add the passed in contents to the modal and display it. | ||
modal.receiveAjax = function (contents) { | ||
modal.receiveAjax = function (contents) { | ||
const domparser = new DOMParser(); | ||
const dom = domparser.parseFromString(contents, "text/html"); | ||
const elements = dom.querySelectorAll(`${modal.containerSelector} > *`); | ||
// If there is a containerSelector on the document, use its children. | ||
let elements = dom.querySelectorAll(`${modal.containerSelector} > *`); | ||
if (elements.length == 0) { | ||
// If the containerSelector wasn't found, use the whole document | ||
elements = dom.body.childNodes; | ||
} | ||
document.querySelector(`${modal.modalSelector} .modal-content`).replaceChildren(...elements); | ||
@@ -309,5 +249,5 @@ | ||
document.addEventListener('click', (e) => { | ||
if (e.target.matches(`${modal.triggerLinkSelector}, ${modal.preserveLinkSelector}`)) | ||
if (e.target.closest(`${modal.triggerLinkSelector}, ${modal.preserveLinkSelector}`)) | ||
modal.modalAjaxLinkClick(e); | ||
else if (e.target.matches('[data-bl-dismiss="modal"]')) | ||
else if (e.target.closest('[data-bl-dismiss="modal"]')) | ||
modal.hide(); | ||
@@ -318,3 +258,3 @@ }); | ||
modal.hide = function (el) { | ||
const dom = document.querySelector(Blacklight.modal.modalSelector); | ||
const dom = document.querySelector(modal.modalSelector); | ||
@@ -326,3 +266,3 @@ if (!dom.open) return | ||
modal.show = function(el) { | ||
const dom = document.querySelector(Blacklight.modal.modalSelector); | ||
const dom = document.querySelector(modal.modalSelector); | ||
@@ -334,60 +274,111 @@ if (dom.open) return | ||
modal.setupModal(); | ||
return modal; | ||
})(); | ||
const SearchContext = (() => { | ||
Blacklight.doSearchContextBehavior = function() { | ||
document.addEventListener('click', (e) => { | ||
if (e.target.matches('[data-context-href]')) { | ||
Blacklight.handleSearchContextMethod.call(e.target, e); | ||
} | ||
}); | ||
}; | ||
const SearchContext = (e) => { | ||
if (e.target.matches('[data-context-href]')) { | ||
SearchContext.handleSearchContextMethod.call(e.target, e); | ||
} | ||
}; | ||
Blacklight.csrfToken = () => document.querySelector('meta[name=csrf-token]')?.content; | ||
Blacklight.csrfParam = () => document.querySelector('meta[name=csrf-param]')?.content; | ||
SearchContext.csrfToken = () => document.querySelector('meta[name=csrf-token]')?.content; | ||
SearchContext.csrfParam = () => document.querySelector('meta[name=csrf-param]')?.content; | ||
// this is the Rails.handleMethod with a couple adjustments, described inline: | ||
// first, we're attaching this directly to the event handler, so we can check for meta-keys | ||
Blacklight.handleSearchContextMethod = function(event) { | ||
const link = this; | ||
// this is the Rails.handleMethod with a couple adjustments, described inline: | ||
// first, we're attaching this directly to the event handler, so we can check for meta-keys | ||
SearchContext.handleSearchContextMethod = function(event) { | ||
const link = this; | ||
// instead of using the normal href, we need to use the context href instead | ||
let href = link.getAttribute('data-context-href'); | ||
let target = link.getAttribute('target'); | ||
let csrfToken = Blacklight.csrfToken(); | ||
let csrfParam = Blacklight.csrfParam(); | ||
let form = document.createElement('form'); | ||
form.method = 'post'; | ||
form.action = href; | ||
// instead of using the normal href, we need to use the context href instead | ||
let href = link.getAttribute('data-context-href'); | ||
let target = link.getAttribute('target'); | ||
let csrfToken = SearchContext.csrfToken(); | ||
let csrfParam = SearchContext.csrfParam(); | ||
let form = document.createElement('form'); | ||
form.method = 'post'; | ||
form.action = href; | ||
let formContent = `<input name="_method" value="post" type="hidden" /> | ||
<input name="redirect" value="${link.getAttribute('href')}" type="hidden" />`; | ||
let formContent = `<input name="_method" value="post" type="hidden" /> | ||
<input name="redirect" value="${link.getAttribute('href')}" type="hidden" />`; | ||
// check for meta keys.. if set, we should open in a new tab | ||
if(event.metaKey || event.ctrlKey) { | ||
target = '_blank'; | ||
} | ||
// check for meta keys.. if set, we should open in a new tab | ||
if(event.metaKey || event.ctrlKey) { | ||
target = '_blank'; | ||
} | ||
if (csrfParam !== undefined && csrfToken !== undefined) { | ||
formContent += `<input name="${csrfParam}" value="${csrfToken}" type="hidden" />`; | ||
} | ||
if (csrfParam !== undefined && csrfToken !== undefined) { | ||
formContent += `<input name="${csrfParam}" value="${csrfToken}" type="hidden" />`; | ||
} | ||
// Must trigger submit by click on a button, else "submit" event handler won't work! | ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit | ||
formContent += '<input type="submit" />'; | ||
// Must trigger submit by click on a button, else "submit" event handler won't work! | ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit | ||
formContent += '<input type="submit" />'; | ||
if (target) { form.setAttribute('target', target); } | ||
if (target) { form.setAttribute('target', target); } | ||
form.style.display = 'none'; | ||
form.innerHTML = formContent; | ||
document.body.appendChild(form); | ||
form.querySelector('[type="submit"]').click(); | ||
form.style.display = 'none'; | ||
form.innerHTML = formContent; | ||
document.body.appendChild(form); | ||
form.querySelector('[type="submit"]').click(); | ||
event.preventDefault(); | ||
event.preventDefault(); | ||
}; | ||
document.addEventListener('click', SearchContext); | ||
const Blacklight = function() { | ||
const buffer = new Array; | ||
return { | ||
onLoad: function(func) { | ||
buffer.push(func); | ||
}, | ||
activate: function() { | ||
for(let i = 0; i < buffer.length; i++) { | ||
buffer[i].call(); | ||
} | ||
}, | ||
listeners: function () { | ||
const listeners = []; | ||
if (typeof Turbo !== 'undefined') { | ||
listeners.push('turbo:load', 'turbo:frame-load'); | ||
} else if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) { | ||
// Turbolinks 5 | ||
if (Turbolinks.BrowserAdapter) { | ||
listeners.push('turbolinks:load'); | ||
} else { | ||
// Turbolinks < 5 | ||
listeners.push('page:load', 'DOMContentLoaded'); | ||
} | ||
} else { | ||
listeners.push('DOMContentLoaded'); | ||
} | ||
return listeners; | ||
} | ||
}; | ||
}(); | ||
Blacklight.doSearchContextBehavior(); | ||
})(); | ||
// turbolinks triggers page:load events on page transition | ||
// If app isn't using turbolinks, this event will never be triggered, no prob. | ||
Blacklight.listeners().forEach(function(listener) { | ||
document.addEventListener(listener, function() { | ||
Blacklight.activate(); | ||
}); | ||
}); | ||
Blacklight.onLoad(function () { | ||
const elem = document.querySelector('.no-js'); | ||
// The "no-js" class may already have been removed because this function is | ||
// run on every turbo:load event, in that case, it won't find an element. | ||
if (!elem) return; | ||
elem.classList.remove('no-js'); | ||
elem.classList.add('js'); | ||
}); | ||
const index = { | ||
@@ -398,2 +389,3 @@ BookmarkToggle, | ||
SearchContext, | ||
Core: Blacklight, | ||
onLoad: Blacklight.onLoad | ||
@@ -400,0 +392,0 @@ }; |
{ | ||
"name": "blacklight-frontend", | ||
"version": "8.0.0-pre.4", | ||
"version": "8.0.0", | ||
"description": "[![Build Status](https://travis-ci.com/projectblacklight/blacklight.png?branch=main)](https://travis-ci.com/projectblacklight/blacklight) [![Gem Version](https://badge.fury.io/rb/blacklight.png)](http://badge.fury.io/rb/blacklight) [![Coverage Status](https://coveralls.io/repos/github/projectblacklight/blacklight/badge.svg?branch=main)](https://coveralls.io/github/projectblacklight/blacklight?branch=main)", | ||
"main": "app/assets/javascripts/blacklight", | ||
"module": "app/assets/javascripts/blacklight/blacklight.esm.js", | ||
"scripts": { | ||
"js-compile-bundle": "rollup --config rollup.config.js --sourcemap" | ||
"prepare": "rollup --config rollup.config.js --sourcemap && ESM=true rollup --config rollup.config.js --sourcemap" | ||
}, | ||
@@ -14,3 +15,4 @@ "repository": { | ||
"files": [ | ||
"app/assets" | ||
"app/assets", | ||
"app/javascript" | ||
], | ||
@@ -24,3 +26,4 @@ "author": "", | ||
"devDependencies": { | ||
"rollup": "^2.60.0" | ||
"rollup": "^2.60.0", | ||
"rollup-plugin-includepaths": "^0.2.4" | ||
}, | ||
@@ -27,0 +30,0 @@ "browserslist": [ |
@@ -63,3 +63,3 @@ # Blacklight | ||
1. run `npm install` to download dependencies | ||
1. run `npm run js-compile-bundle` to build the bundle | ||
1. run `npm run prepare` to build the bundle | ||
1. run `npm publish` to push the javascript package to https://npmjs.org/package/blacklight-frontend | ||
@@ -66,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
117696
38
996
0
2
8