Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove jQuery from the citation modal (except fomantic) #30008

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions web_src/js/features/citation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import $ from 'jquery';
import {getCurrentLocale} from '../utils.js';

const {pageData} = window.config;

async function initInputCitationValue($citationCopyApa, $citationCopyBibtex) {
async function initInputCitationValue(citationCopyApa, citationCopyBibtex) {
const [{Cite, plugins}] = await Promise.all([
import(/* webpackChunkName: "citation-js-core" */'@citation-js/core'),
import(/* webpackChunkName: "citation-js-formats" */'@citation-js/plugin-software-formats'),
Expand All @@ -14,56 +15,57 @@ async function initInputCitationValue($citationCopyApa, $citationCopyBibtex) {
config.constants.fieldTypes.doi = ['field', 'literal'];
config.constants.fieldTypes.version = ['field', 'literal'];
const citationFormatter = new Cite(citationFileContent);
const lang = document.documentElement.lang || 'en-US';
const lang = getCurrentLocale() || 'en-US';
const apaOutput = citationFormatter.format('bibliography', {template: 'apa', lang});
const bibtexOutput = citationFormatter.format('bibtex', {lang});
$citationCopyBibtex.attr('data-text', bibtexOutput);
$citationCopyApa.attr('data-text', apaOutput);
citationCopyBibtex.setAttribute('data-text', bibtexOutput);
citationCopyApa.setAttribute('data-text', apaOutput);
}

export async function initCitationFileCopyContent() {
const defaultCitationFormat = 'apa'; // apa or bibtex

if (!pageData.citationFileContent) return;

const $citationCopyApa = $('#citation-copy-apa');
const $citationCopyBibtex = $('#citation-copy-bibtex');
const $inputContent = $('#citation-copy-content');
const citationCopyApa = document.getElementById('citation-copy-apa');
const citationCopyBibtex = document.getElementById('citation-copy-bibtex');
const inputContent = document.getElementById('citation-copy-content');

if ((!citationCopyApa && !citationCopyBibtex) || !inputContent) return;

if ((!$citationCopyApa.length && !$citationCopyBibtex.length) || !$inputContent.length) return;
const updateUi = () => {
const isBibtex = (localStorage.getItem('citation-copy-format') || defaultCitationFormat) === 'bibtex';
const copyContent = (isBibtex ? $citationCopyBibtex : $citationCopyApa).attr('data-text');

$inputContent.val(copyContent);
$citationCopyBibtex.toggleClass('primary', isBibtex);
$citationCopyApa.toggleClass('primary', !isBibtex);
const copyContent = (isBibtex ? citationCopyBibtex : citationCopyApa).getAttribute('data-text');
inputContent.value = copyContent;
citationCopyBibtex.classList.toggle('primary', isBibtex);
citationCopyApa.classList.toggle('primary', !isBibtex);
};

$('#cite-repo-button').on('click', async (e) => {
document.getElementById('cite-repo-button')?.addEventListener('click', async (e) => {
const dropdownBtn = e.target.closest('.ui.dropdown.button');
dropdownBtn.classList.add('is-loading');

try {
try {
await initInputCitationValue($citationCopyApa, $citationCopyBibtex);
await initInputCitationValue(citationCopyApa, citationCopyBibtex);
} catch (e) {
console.error(`initCitationFileCopyContent error: ${e}`, e);
return;
}
updateUi();

$citationCopyApa.on('click', () => {
citationCopyApa.addEventListener('click', () => {
localStorage.setItem('citation-copy-format', 'apa');
updateUi();
});
$citationCopyBibtex.on('click', () => {

citationCopyBibtex.addEventListener('click', () => {
localStorage.setItem('citation-copy-format', 'bibtex');
updateUi();
});

$inputContent.on('click', () => {
$inputContent.trigger('select');
inputContent.addEventListener('click', () => {
inputContent.select();
silverwind marked this conversation as resolved.
Show resolved Hide resolved
});
} finally {
dropdownBtn.classList.remove('is-loading');
Expand Down