|
1 | | -import $ from 'jquery' |
2 | | - |
3 | 1 | export function queryByHook (hook, container) { |
4 | | - return $(`[data-hook~=${hook}]`, container) |
| 2 | + const context = container || document |
| 3 | + return Array.from(context.querySelectorAll(`[data-hook~=${hook}]`)) |
5 | 4 | } |
6 | 5 |
|
7 | 6 | export function queryByComponent (component, container) { |
8 | | - return $(`[data-component~=${component}]`, container) |
| 7 | + const context = container || document |
| 8 | + return Array.from(context.querySelectorAll(`[data-component~=${component}]`)) |
9 | 9 | } |
10 | 10 |
|
11 | 11 | export function setContent (container, content) { |
12 | | - return container.empty().append(content) |
| 12 | + const containerList = ensureArray(container) |
| 13 | + const contentString = Array.isArray(content) ? content.join('\n') : content |
| 14 | + containerList.forEach((container) => container.innerHTML = contentString) |
13 | 15 | } |
14 | 16 |
|
15 | 17 | // Meant to mimic Jekyll's slugify function |
@@ -37,20 +39,48 @@ export function createDatasetFilters (filters) { |
37 | 39 |
|
38 | 40 | // Collapses a bootstrap list-group to only show a few items by default |
39 | 41 | // Number of items to show can be specified in [data-show] attribute or passed as param |
40 | | -export function collapseListGroup (container, show) { |
41 | | - if (!show) show = container.data('show') || 5 |
| 42 | +export function collapseListGroup (container, numToShow) { |
| 43 | + if (!numToShow) numToShow = +container.dataset.show || 5 |
| 44 | + |
| 45 | + const hideFromIndex = numToShow + 1 |
| 46 | + const itemsToHideQuery = `.list-group-item:nth-of-type(n+${hideFromIndex}):not(.active)` |
| 47 | + const itemsToHide = Array.from(container.querySelectorAll(itemsToHideQuery)) |
42 | 48 |
|
43 | | - const itemsToHide = $('.list-group-item:gt(' + (show - 1) + '):not(.active)', container) |
44 | 49 | if (itemsToHide.length) { |
45 | | - itemsToHide.hide() |
46 | | - |
47 | | - const showMoreButton = $('<a href="#" class="list-group-item">Show ' + itemsToHide.length + ' more...</a>') |
48 | | - showMoreButton.on('click', function (e) { |
49 | | - itemsToHide.show() |
50 | | - $(this).off('click') |
51 | | - $(this).remove() |
52 | | - e.preventDefault() |
53 | | - }) |
54 | | - container.append(showMoreButton) |
| 50 | + hide(itemsToHide) |
| 51 | + |
| 52 | + const showMoreButton = createShowMoreButton(itemsToHide) |
| 53 | + container.appendChild(showMoreButton) |
55 | 54 | } |
56 | 55 | } |
| 56 | + |
| 57 | +export function param (obj) { |
| 58 | + const params = new URLSearchParams(obj) |
| 59 | + return params.toString() |
| 60 | +} |
| 61 | + |
| 62 | +export function hide (els) { |
| 63 | + ensureArray(els).forEach((el) => el.style.display = 'none') |
| 64 | +} |
| 65 | + |
| 66 | +export function show (els) { |
| 67 | + ensureArray(els).forEach((el) => el.style.display = '') |
| 68 | +} |
| 69 | + |
| 70 | +function ensureArray (item) { |
| 71 | + return Array.isArray(item) ? item : [item] |
| 72 | +} |
| 73 | + |
| 74 | +function createShowMoreButton (hiddenItems) { |
| 75 | + const el = document.createElement('a') |
| 76 | + el.classList.add('list-group-item') |
| 77 | + el.setAttribute('href', '#') |
| 78 | + el.textContent = `Show ${hiddenItems.length} more...` |
| 79 | + el.addEventListener('click', function (event) { |
| 80 | + show(hiddenItems) |
| 81 | + this.remove() |
| 82 | + event.preventDefault() |
| 83 | + }, {once: true}) |
| 84 | + |
| 85 | + return el |
| 86 | +} |
0 commit comments