1
1
import $ from 'jquery' ;
2
+ import { getCurrentLocale } from '../utils.js' ;
2
3
3
4
const { pageData} = window . config ;
4
5
5
- async function initInputCitationValue ( $ citationCopyApa, $ citationCopyBibtex) {
6
+ async function initInputCitationValue ( citationCopyApa , citationCopyBibtex ) {
6
7
const [ { Cite, plugins} ] = await Promise . all ( [
7
8
import ( /* webpackChunkName: "citation-js-core" */ '@citation-js/core' ) ,
8
9
import ( /* webpackChunkName: "citation-js-formats" */ '@citation-js/plugin-software-formats' ) ,
@@ -14,56 +15,57 @@ async function initInputCitationValue($citationCopyApa, $citationCopyBibtex) {
14
15
config . constants . fieldTypes . doi = [ 'field' , 'literal' ] ;
15
16
config . constants . fieldTypes . version = [ 'field' , 'literal' ] ;
16
17
const citationFormatter = new Cite ( citationFileContent ) ;
17
- const lang = document . documentElement . lang || 'en-US' ;
18
+ const lang = getCurrentLocale ( ) || 'en-US' ;
18
19
const apaOutput = citationFormatter . format ( 'bibliography' , { template : 'apa' , lang} ) ;
19
20
const bibtexOutput = citationFormatter . format ( 'bibtex' , { lang} ) ;
20
- $ citationCopyBibtex. attr ( 'data-text' , bibtexOutput ) ;
21
- $ citationCopyApa. attr ( 'data-text' , apaOutput ) ;
21
+ citationCopyBibtex . setAttribute ( 'data-text' , bibtexOutput ) ;
22
+ citationCopyApa . setAttribute ( 'data-text' , apaOutput ) ;
22
23
}
23
24
24
25
export async function initCitationFileCopyContent ( ) {
25
26
const defaultCitationFormat = 'apa' ; // apa or bibtex
26
27
27
28
if ( ! pageData . citationFileContent ) return ;
28
29
29
- const $citationCopyApa = $ ( '#citation-copy-apa' ) ;
30
- const $citationCopyBibtex = $ ( '#citation-copy-bibtex' ) ;
31
- const $inputContent = $ ( '#citation-copy-content' ) ;
30
+ const citationCopyApa = document . getElementById ( 'citation-copy-apa' ) ;
31
+ const citationCopyBibtex = document . getElementById ( 'citation-copy-bibtex' ) ;
32
+ const inputContent = document . getElementById ( 'citation-copy-content' ) ;
33
+
34
+ if ( ( ! citationCopyApa && ! citationCopyBibtex ) || ! inputContent ) return ;
32
35
33
- if ( ( ! $citationCopyApa . length && ! $citationCopyBibtex . length ) || ! $inputContent . length ) return ;
34
36
const updateUi = ( ) => {
35
37
const isBibtex = ( localStorage . getItem ( 'citation-copy-format' ) || defaultCitationFormat ) === 'bibtex' ;
36
- const copyContent = ( isBibtex ? $citationCopyBibtex : $citationCopyApa ) . attr ( 'data-text' ) ;
37
-
38
- $inputContent . val ( copyContent ) ;
39
- $citationCopyBibtex . toggleClass ( 'primary' , isBibtex ) ;
40
- $citationCopyApa . toggleClass ( 'primary' , ! isBibtex ) ;
38
+ const copyContent = ( isBibtex ? citationCopyBibtex : citationCopyApa ) . getAttribute ( 'data-text' ) ;
39
+ inputContent . value = copyContent ;
40
+ citationCopyBibtex . classList . toggle ( 'primary' , isBibtex ) ;
41
+ citationCopyApa . classList . toggle ( 'primary' , ! isBibtex ) ;
41
42
} ;
42
43
43
- $ ( '# cite-repo-button') . on ( 'click' , async ( e ) => {
44
+ document . getElementById ( ' cite-repo-button') ?. addEventListener ( 'click' , async ( e ) => {
44
45
const dropdownBtn = e . target . closest ( '.ui.dropdown.button' ) ;
45
46
dropdownBtn . classList . add ( 'is-loading' ) ;
46
47
47
48
try {
48
49
try {
49
- await initInputCitationValue ( $ citationCopyApa, $ citationCopyBibtex) ;
50
+ await initInputCitationValue ( citationCopyApa , citationCopyBibtex ) ;
50
51
} catch ( e ) {
51
52
console . error ( `initCitationFileCopyContent error: ${ e } ` , e ) ;
52
53
return ;
53
54
}
54
55
updateUi ( ) ;
55
56
56
- $ citationCopyApa. on ( 'click' , ( ) => {
57
+ citationCopyApa . addEventListener ( 'click' , ( ) => {
57
58
localStorage . setItem ( 'citation-copy-format' , 'apa' ) ;
58
59
updateUi ( ) ;
59
60
} ) ;
60
- $citationCopyBibtex . on ( 'click' , ( ) => {
61
+
62
+ citationCopyBibtex . addEventListener ( 'click' , ( ) => {
61
63
localStorage . setItem ( 'citation-copy-format' , 'bibtex' ) ;
62
64
updateUi ( ) ;
63
65
} ) ;
64
66
65
- $ inputContent. on ( 'click' , ( ) => {
66
- $ inputContent. trigger ( ' select' ) ;
67
+ inputContent . addEventListener ( 'click' , ( ) => {
68
+ inputContent . select ( ) ;
67
69
} ) ;
68
70
} finally {
69
71
dropdownBtn . classList . remove ( 'is-loading' ) ;
0 commit comments