Skip to content

Commit

Permalink
Replace bootstrap-tagsinput with Tom Select
Browse files Browse the repository at this point in the history
bootstrap-tagsinput is unsupported and out of date. Tom Select supports
using a text input, has a similar method of loading remote data, and has
both Bootstrap 4 and 5 assets. Vendoring the assets for now, but if we
change the asset pipeline for Spotlight v4 perhaps we can remove that.
  • Loading branch information
taylor-steve committed Aug 2, 2024
1 parent ac477bc commit 1e85361
Show file tree
Hide file tree
Showing 32 changed files with 12,124 additions and 1,764 deletions.
5,879 changes: 5,317 additions & 562 deletions app/assets/javascripts/spotlight/spotlight.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/spotlight/spotlight.esm.js.map

Large diffs are not rendered by default.

5,879 changes: 5,317 additions & 562 deletions app/assets/javascripts/spotlight/spotlight.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/spotlight/spotlight.js.map

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions app/assets/stylesheets/spotlight/_catalog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,12 @@
}

form.edit_solr_document {
.bootstrap-tagsinput {
@extend .clearfix;
cursor: text;
}
.bg-warning.form-text {
font-size: 0.9em;
padding: 3px 6px;
}
}

.bootstrap-tagsinput {
display: block;
.twitter-typeahead {
width: auto;
}

.tt-input {
vertical-align: baseline !important;
}
}

.blacklight-catalog-edit, .blacklight-catalog-show {
.img-thumbnail {
@extend .col-md-6;
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/spotlight/_spotlight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@import "utilities";
@import "view_larger";
@import "accessibility";
@import "tom_select_fix";



Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/spotlight/_tom_select_fix.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Remove when this is fixed or we switch from sassc-rails: https://github.com/orchidjs/tom-select/issues/656
@function max($value, $value2) {
@return unquote('max(#{$value}, #{$value2})');
}
67 changes: 33 additions & 34 deletions app/javascript/spotlight/admin/exhibit_tag_autocomplete.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import TomSelect from 'tom-select';

export default class {
connect() {
$('[data-autocomplete-tag="true"]').each(function(_i, el) {
var $el = $(el);
// By default tags input binds on page ready to [data-role=tagsinput],
// however, that doesn't work with Turbolinks. So we init manually:
$el.tagsinput();

var tags = new Bloodhound({
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 100,
prefetch: {
url: $el.data('autocomplete-url'),
ttl: 1,
filter: function(list) {
// Let the dom know that the response has been returned
$el.attr('data-autocomplete-fetched', true);
return $.map(list, function(tag) { return { name: tag }; });
}
}
});

tags.initialize();
const tagOptions = {
closeAfterSelect: true,
create: true,
createOnBlur: true,
duplicates: false,
hideSelected: true,
labelField: 'name',
loadThrottle: 300,
maxOptions: 100,
persist: false,
plugins: ['remove_button'],
preload: true,
searchField: 'name',
valueField: 'name',
onItemAdd: function(value, item) {
this.control_input.value = '';
},
load: function(query, callback) {
fetch(this.input.dataset.autocompleteUrl)
.then(response => response.json())
.then(json => {
callback(json.map(tag => ({name: tag.trim()})));
}).catch(() => callback());
}
}

$el.tagsinput('input').typeahead({highlight: true, hint: false}, {
name: 'tags',
displayKey: 'name',
source: tags.ttAdapter()
}).bind('typeahead:selected', $.proxy(function (obj, datum) {
$el.tagsinput('add', datum.name);
$el.tagsinput('input').typeahead('val', '');
})).bind('blur', function() {
$el.tagsinput('add', $el.tagsinput('input').typeahead('val'));
$el.tagsinput('input').typeahead('val', '');
})
})
document.querySelectorAll('[data-autocomplete-tag="true"]').forEach(tagElement => {
// Handle leading spaces (e.g., 'Tag 1, Tag 2') or else the user can add what appear to be duplicate tags.
const items = tagElement.value.split(',').map(item => item.trim()).filter(Boolean);
const options = items.map(item => ({name: item}));
new TomSelect(tagElement, { ...tagOptions, items, options });
});
}
}
1 change: 0 additions & 1 deletion app/javascript/spotlight/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// These scripts are in the vendor directory
import 'nestable'
import 'parameterize'
import 'bootstrap-tagsinput'
import 'jquery.serializejson'
import 'leaflet-iiif'
import 'Leaflet.Editable'
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/spotlight/templates/spotlight.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import 'bootstrap-tagsinput';
@import 'spotlight/variables_bootstrap';
@import 'bootstrap';
@import 'sir-trevor/main';
@import 'leaflet';
@import 'spotlight/spotlight';
@import 'tom-select/tom-select.bootstrap4.min';
245 changes: 244 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1e85361

Please sign in to comment.