Blacklight = function () {
var buffer = new Array();
return {
onLoad: function (func) {
buffer.push(func);
},
activate: function () {
for (var i = 0; i < buffer.length; i++) {
buffer[i].call();
}
},
listeners: function () {
var listeners = [];
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();
});
});
$('.no-js').removeClass('no-js').addClass('js');
/*global Bloodhound */
Blacklight.onLoad(function () {
'use strict';
$('[data-autocomplete-enabled="true"]').each(function () {
var $el = $(this);
if ($el.hasClass('tt-hint')) {
return;
}
var suggestUrl = $el.data().autocompletePath;
var terms = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: suggestUrl + '?q=%QUERY',
wildcard: '%QUERY'
}
});
terms.initialize();
$el.typeahead({
hint: true,
highlight: true,
minLength: 2
}, {
name: 'terms',
displayKey: 'term',
source: terms.ttAdapter()
});
});
});
(function ($) {
//change form submit toggle to checkbox
Blacklight.doBookmarkToggleBehavior = function () {
if (typeof Blacklight.do_bookmark_toggle_behavior == 'function') {
console.warn("do_bookmark_toggle_behavior is deprecated. Use doBookmarkToggleBehavior instead.");
return Blacklight.do_bookmark_toggle_behavior();
}
$(Blacklight.doBookmarkToggleBehavior.selector).blCheckboxSubmit({
// cssClass is added to elements added, plus used for id base
cssClass: 'toggle-bookmark',
success: function (checked, response) {
if (response.bookmarks) {
$('[data-role=bookmark-counter]').text(response.bookmarks.count);
}
}
});
};
Blacklight.doBookmarkToggleBehavior.selector = 'form.bookmark-toggle';
Blacklight.onLoad(function () {
Blacklight.doBookmarkToggleBehavior();
});
})(jQuery);
Blacklight.onLoad(function () {
// 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.
document.querySelectorAll('button.collapse-toggle').forEach(button => {
button.addEventListener('click', () => {
event.target.focus();
});
});
});
/* A JQuery plugin (should this be implemented as a widget instead? not sure)
that will convert a "toggle" form, with single submit button to add/remove
something, like used for Bookmarks, into an AJAXy checkbox instead.
Apply to a form. Does require certain assumption about the form:
1) The same form 'action' href must be used for both ADD and REMOVE
actions, with the different being the hidden input name="_method"
being set to "put" or "delete" -- that's the Rails method to pretend
to be doing a certain HTTP verb. So same URL, PUT to add, DELETE
to remove. This plugin assumes that.
Plus, the form this is applied to should provide a data-doc-id
attribute (HTML5-style doc-*) that contains the id/primary key
of the object in question -- used by plugin for a unique value for
DOM id's.
Uses HTML for a checkbox compatible with Bootstrap 3.
Pass in options for your class name and labels:
$("form.something").blCheckboxSubmit({
//cssClass is added to elements added, plus used for id base
cssClass: "toggle_my_kinda_form",
error: function() {
#optional callback
},
success: function(after_success_check_state) {
#optional callback
}
});
*/
(function ($) {
$.fn.blCheckboxSubmit = function (argOpts) {
this.each(function () {
var options = $.extend({}, $.fn.blCheckboxSubmit.defaults, argOpts);
var form = $(this);
form.children().hide(); //We're going to use the existing form to actually send our add/removes
//This works conveneintly because the exact same action href is used
//for both bookmarks/$doc_id. But let's take out the irrelevant parts
//of the form to avoid any future confusion.
form.find('input[type=submit]').remove(); //View needs to set data-doc-id so we know a unique value
//for making DOM id
var uniqueId = form.attr('data-doc-id') || Math.random(); // if form is currently using method delete to change state,
// then checkbox is currently checked
var checked = form.find('input[name=_method][value=delete]').length != 0;
var checkbox = $('').addClass(options.cssClass).attr('id', options.cssClass + '_' + uniqueId);
var label = $('