hugo-theme-zzo/layouts/partials/script/pub-list-script.html

334 lines
11 KiB
HTML
Raw Normal View History

2020-02-05 17:23:27 +00:00
{{ $shave := resources.Get "js/shave.min.js" | resources.Fingerprint }}
<script defer src="{{ $shave.RelPermalink }}"></script>
2020-02-08 19:36:51 +00:00
{{ $masonry := resources.Get "js/masonry.pkgd.min.js" | resources.Fingerprint }}
<script defer src="{{ $masonry.RelPermalink }}"></script>
{{ $imagesloaded := resources.Get "js/imagesloaded.pkgd.min.js" | resources.Fingerprint }}
<script defer src="{{ $imagesloaded.RelPermalink }}"></script>
2020-02-05 17:23:27 +00:00
<script>
{{ $enableSearchHighlight := ($.Param "enableSearchHighlight") }}
var enableSearchHighlight = JSON.parse({{ $enableSearchHighlight | jsonify }});
{{ $isFirstSection := eq .Permalink .FirstSection.Permalink }}
var isFirstSection = JSON.parse({{ $isFirstSection | jsonify }});
2020-02-08 19:36:51 +00:00
{{ $tagsBaseURL := ("/tags/" | relLangURL) }}
var tagsBaseURL = JSON.parse({{ $tagsBaseURL | jsonify }});
2020-02-05 17:23:27 +00:00
document.addEventListener('DOMContentLoaded', function () {
var originHtmlText = JSON.stringify(document.querySelector('.pub__ul').innerHTML);
2020-02-05 17:23:27 +00:00
// search
2020-02-05 17:23:27 +00:00
var pubSearch = document.getElementById('pubSearch');
var pubList = document.querySelector('.pub__list');
var pubUl = document.querySelector('.pub__ul');
var pubResult = document.querySelector('.pub__result');
2020-02-05 17:23:27 +00:00
var makeLi = function(ulElem, obj) {
var li = document.createElement('li');
li.className = 'pub__item';
2020-02-08 19:36:51 +00:00
var titleLink = document.createElement('a');
titleLink.innerHTML = obj.title;
titleLink.setAttribute('href', obj.uri);
2020-02-05 17:23:27 +00:00
if (obj.booktitle) {
var span = document.createElement('span');
span.className = 'p2';
span.innerText = ' - ' + obj.booktitle;
2020-02-08 19:36:51 +00:00
titleLink.appendChild(span);
2020-02-05 17:23:27 +00:00
} else if (obj.shorttitle) {
var span = document.createElement('span');
span.className = 'p2';
span.innerText = ' - ' + obj.shorttitle;
2020-02-08 19:36:51 +00:00
titleLink.appendChild(span);
2020-02-05 17:23:27 +00:00
}
var metaDiv = document.createElement('div');
2020-02-08 19:36:51 +00:00
metaDiv.setAttribute('class', 'pub__metas');
2020-02-05 17:23:27 +00:00
if (obj.publishDate) {
var dateSpan = document.createElement('span');
dateSpan.className = 'caption pub__meta';
2020-02-06 02:18:23 +00:00
dateSpan.innerText = '📅 ' + obj.publishDate.slice(0, 10);
2020-02-05 17:23:27 +00:00
metaDiv.appendChild(dateSpan);
}
if (obj.authors) {
var authorsSpan = document.createElement('span');
authorsSpan.className = 'caption pub__meta';
authorsSpan.innerText = '✍️ ' + obj.authors.toString();
metaDiv.appendChild(authorsSpan);
}
if (obj.publication) {
var pubSpan = document.createElement('span');
pubSpan.className = 'caption pub__meta';
pubSpan.innerText = '📚 ' + obj.publication;
metaDiv.appendChild(pubSpan);
}
if (obj.ENTRYTYPE) {
var typeSpan = document.createElement('span');
typeSpan.className = 'caption pub__meta';
typeSpan.innerText = '🎯 ' + obj.ENTRYTYPE;
metaDiv.appendChild(typeSpan);
}
2020-02-08 19:36:51 +00:00
var tagsUl = null;
if (obj.tags && obj.tags.length) {
tagsUl = document.createElement('ul');
tagsUl.setAttribute('class', 'pub__tags caption');
tagsUl.innerHTML = '&nbsp;🏷️';
for (var i = 0; i < obj.tags.length; i++) {
var tagLi = document.createElement('li');
var tagLink = document.createElement('a');
tagLink.setAttribute('href', tagsBaseURL + obj.tags[i]);
tagLink.setAttribute('class', 'pub__tag');
tagLink.innerText = "#" + obj.tags[i];
tagLi.appendChild(tagLink);
tagsUl.appendChild(tagLi);
}
}
2020-02-05 17:23:27 +00:00
var descDiv = document.createElement('div');
descDiv.className = 'pub__summary';
if (obj.abstract) {
descDiv.innerHTML = obj.abstract.substr(0, 300);
}
2020-02-08 19:36:51 +00:00
li.appendChild(titleLink);
2020-02-05 17:23:27 +00:00
li.appendChild(metaDiv);
li.appendChild(descDiv);
2020-02-08 19:36:51 +00:00
if (tagsUl) {
metaDiv.appendChild(tagsUl);
}
2020-02-05 17:23:27 +00:00
ulElem.appendChild(li);
}
var makeHighlightLi = function (ulElem, obj) {
var li = document.createElement('li');
li.className = 'pub__item';
2020-02-08 19:36:51 +00:00
var titleLink = document.createElement('a');
titleLink.innerHTML = obj.item.title;
titleLink.setAttribute('href', obj.item.uri);
var descDiv = null;
if (obj.item.abstract) {
descDiv = document.createElement('div');
descDiv.innerHTML = obj.item.abstract;
descDiv.className = 'pub__summary';
}
if (obj.matches && obj.matches.length) {
for (var i = 0; i < obj.matches.length; i++) {
2020-02-08 19:36:51 +00:00
titleLink.innerHTML = obj.item.title;
if (obj.matches[i].key === 'title') {
2020-02-08 19:36:51 +00:00
titleLink.innerHTML = generateHighlightedText(obj.item.title, obj.matches[i].indices);
}
if (obj.item.booktitle) {
var span = document.createElement('span');
span.className = 'p2';
span.innerHTML = ' - ' + obj.item.booktitle;
2020-02-08 19:36:51 +00:00
titleLink.appendChild(span);
} else if (obj.item.shorttitle) {
var span = document.createElement('span');
span.className = 'p2';
span.innerHTML = ' - ' + obj.item.shorttitle;
2020-02-08 19:36:51 +00:00
titleLink.appendChild(span);
}
2020-02-08 19:36:51 +00:00
var metaDiv = document.createElement('div');
metaDiv.setAttribute('class', 'pub__metas');
if (obj.item.publishDate) {
var dateSpan = document.createElement('span');
dateSpan.className = 'caption pub__meta';
dateSpan.innerText = '📅 ' + obj.item.publishDate.slice(0, 10);
metaDiv.appendChild(dateSpan);
}
if (obj.item.authors) {
var authorsSpan = document.createElement('span');
authorsSpan.className = 'caption pub__meta';
authorsSpan.innerText = '✍️ ' + obj.item.authors.toString();
metaDiv.appendChild(authorsSpan);
}
if (obj.item.publication) {
var pubSpan = document.createElement('span');
pubSpan.className = 'caption pub__meta';
pubSpan.innerText = '📚 ' + obj.item.publication;
metaDiv.appendChild(pubSpan);
}
if (obj.item.ENTRYTYPE) {
var typeSpan = document.createElement('span');
typeSpan.className = 'caption pub__meta';
typeSpan.innerText = '🎯 ' + obj.item.ENTRYTYPE;
metaDiv.appendChild(typeSpan);
}
2020-02-08 19:36:51 +00:00
var tagsUl = null;
if (obj.item.tags && obj.item.tags.length) {
tagsUl = document.createElement('ul');
tagsUl.setAttribute('class', 'pub__tags caption');
tagsUl.innerHTML = '&nbsp;🏷️';
for (var j = 0; j < obj.item.tags.length; j++) {
var tagLi = document.createElement('li');
var tagLink = document.createElement('a');
tagLink.setAttribute('href', tagsBaseURL + obj.item.tags[j]);
tagLink.setAttribute('class', 'pub__tag');
tagLink.innerText = "#" + obj.item.tags[j];
tagLi.appendChild(tagLink);
tagsUl.appendChild(tagLi);
}
}
if (obj.matches[i].key === 'abstract') {
descDiv.innerHTML = generateHighlightedText(obj.matches[i].value, obj.matches[i].indices);
}
}
2020-02-08 19:36:51 +00:00
li.appendChild(titleLink);
if (metaDiv) {
li.appendChild(metaDiv);
}
if (descDiv) {
li.appendChild(descDiv);
}
2020-02-08 19:36:51 +00:00
if (tagsUl && metaDiv) {
metaDiv.appendChild(tagsUl);
}
ulElem.appendChild(li);
}
}
function generateHighlightedText(text, regions) {
if (!regions) {
return text;
}
var content = '', nextUnhighlightedRegionStartingIndex = 0;
regions.forEach(function (region) {
if (region[0] === region[1]) {
return null;
}
content += '' +
text.substring(nextUnhighlightedRegionStartingIndex, region[0]) +
'<span class="search__highlight">' +
text.substring(region[0], region[1] + 1) +
'</span>' +
'';
nextUnhighlightedRegionStartingIndex = region[1] + 1;
});
content += text.substring(nextUnhighlightedRegionStartingIndex);
return content;
};
pubSearch ?
2020-02-05 17:23:27 +00:00
pubSearch.addEventListener('input', function(e) {
var results = fuse.search(e.target.value);
2020-02-05 17:23:27 +00:00
if (enableSearchHighlight) {
renderSearchHighlightResults(e.target.value, results);
} else {
renderSearchResults(e.target.value, results);
}
}) : null;
function renderSearchResults(searchText, results) {
var originUl = document.querySelector('.pub__ul');
var newUl = document.createElement('ul');
newUl.setAttribute('class', 'pub__ul');
if (!searchText) {
2020-02-08 19:36:51 +00:00
if (isFirstSection) {
originUl.innerHTML = JSON.parse(originHtmlText);
var grid = document.querySelector('.pgrid');
var msnry = new Masonry(grid, {
itemSelector: '.pgrid-item',
columnWidth: '.pgrid-sizer',
percentPosition: true,
});
imagesLoaded(grid).on('progress', function () {
msnry.layout();
});
return null;
} else {
fuse ? fuse.list.forEach(function (item) {
makeLi(newUl, item);
}) : null;
}
pubResult ? pubResult.setAttribute('data-display', 'none') : null;
originUl ? originUl.setAttribute('data-display', 'block') : null;
} else if (results) {
2020-02-05 17:23:27 +00:00
if (results && results.length) {
results.forEach(function (result) {
makeLi(newUl, result);
2020-02-05 17:23:27 +00:00
});
pubResult ? pubResult.setAttribute('data-display', 'block') : null;
originUl ? originUl.setAttribute('data-display', 'none') : null;
}
}
originUl.parentNode.replaceChild(newUl, originUl);
}
function renderSearchHighlightResults(searchText, results) {
var originUl = document.querySelector('.pub__ul');
var newUl = document.createElement('ul');
newUl.setAttribute('class', 'pub__ul');
if (!searchText) {
if (isFirstSection) {
originUl.innerHTML = JSON.parse(originHtmlText);
2020-02-08 19:36:51 +00:00
var grid = document.querySelector('.pgrid');
var msnry = new Masonry(grid, {
itemSelector: '.pgrid-item',
columnWidth: '.pgrid-sizer',
percentPosition: true,
});
imagesLoaded(grid).on('progress', function () {
msnry.layout();
});
return null;
} else {
fuse ? fuse.list.forEach(function (item) {
makeLi(newUl, item);
}) : null;
}
pubResult ? pubResult.setAttribute('data-display', 'none') : null;
originUl ? originUl.setAttribute('data-display', 'block') : null;
} else if (results) {
if (results && results.length) {
results.forEach(function (result) {
makeHighlightLi(newUl, result);
2020-02-05 17:23:27 +00:00
});
pubResult ? pubResult.setAttribute('data-display', 'block') : null;
originUl ? originUl.setAttribute('data-display', 'none') : null;
2020-02-05 17:23:27 +00:00
}
}
originUl.parentNode.replaceChild(newUl, originUl);
}
2020-02-05 17:23:27 +00:00
// shave
shave('.pub__summary', 150);
});
</script>