Debian Codegraph @

extensions/Cite/modules/ve-cite/ve.dm.MWDocumentReferences.js

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * @copyright 2024 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
5 * @license MIT
6 */
7
8 /**
9 * A facade providing a simplified and safe interface to Cite `ref` and
10 * `references` tags in a document.
11 *
12 * @constructor
13 * @mixes OO.EventEmitter
14 * @param {ve.dm.Document} doc The document that reference tags will be embedded in.
15 */
16 ve.dm.MWDocumentReferences = function VeDmMWDocumentReferences( doc ) {
17 // Mixin constructors
18 OO.EventEmitter.call( this );
19
20 // Properties
21 this.doc = doc;
22 /**
23 * Holds the information calculated for each group.
24 *
25 * @member {Object.<string, ve.dm.MWGroupReferences>}
26 */
27 this.cachedByGroup = {};
28
29 doc.getInternalList().connect( this, { update: 'updateGroups' } );
30 this.updateAllGroups();
31 };
32
33 /* Inheritance */
34
35 OO.mixinClass( ve.dm.MWDocumentReferences, OO.EventEmitter );
36
37 /* Methods */
38
39 /**
40 * Singleton MWDocumentReferences for a document.
41 *
42 * @param {ve.dm.Document} doc Source document associated with the
43 * singleton. May be a fragment in which case we only look at refs included in
44 * the fragment.
45 *
46 * @return {ve.dm.MWDocumentReferences} Singleton docRefs
47 */
48 ve.dm.MWDocumentReferences.static.refsForDoc = function ( doc ) {
49 let docRefs;
50 // Only use cache if we're working with the full document.
51 if ( !doc.getOriginalDocument() ) {
52 docRefs = doc.extCiteDocumentReferences;
53 }
54 if ( docRefs === undefined ) {
55 docRefs = new ve.dm.MWDocumentReferences( doc );
56 }
57 if ( !doc.getOriginalDocument() ) {
58 doc.extCiteDocumentReferences = docRefs;
59 }
60 return docRefs;
61 };
62
63 /**
64 * @private
65 */
66 ve.dm.MWDocumentReferences.prototype.updateAllGroups = function () {
67 this.updateGroups( this.getAllGroupNames() );
68 };
69
70 /**
71 * @private
72 * @param {string[]} groupsChanged A list of group names which have changed in
73 * this transaction
74 */
75 ve.dm.MWDocumentReferences.prototype.updateGroups = function ( groupsChanged ) {
76 groupsChanged.forEach( ( groupName ) => this.updateGroup( groupName ) );
77 };
78
79 /**
80 * @private
81 * @param {string[]} groupName Name of the reference group which needs to be
82 * updated, with prefix
83 */
84 ve.dm.MWDocumentReferences.prototype.updateGroup = function ( groupName ) {
85 const nodeGroup = this.doc.getInternalList().getNodeGroup( groupName );
86 this.cachedByGroup[ groupName ] = ve.dm.MWGroupReferences.static.makeGroupRefs( nodeGroup );
87 };
88
89 /**
90 * @param {string} groupName with or without prefix
91 * @return {ve.dm.MWGroupReferences}
92 */
93 ve.dm.MWDocumentReferences.prototype.getGroupRefs = function ( groupName ) {
94 return this.cachedByGroup[ groupName.startsWith( 'mwReference/' ) ? groupName : 'mwReference/' + groupName ] ||
95 new ve.dm.MWGroupReferences();
96 };
97
98 ve.dm.MWDocumentReferences.prototype.getAllGroupNames = function () {
99 return Object.keys( this.doc.getInternalList().getNodeGroups() );
100 };
101
102 ve.dm.MWDocumentReferences.prototype.hasRefs = function () {
103 return this.getAllGroupNames().some( ( groupName ) => !this.getGroupRefs( groupName ).isEmpty() );
104 };
105
106 /**
107 * Return a formatted number, in the content script, with no separators.
108 *
109 * Partial clone of mw.language.convertNumber .
110 *
111 * @param {number} num
112 * @return {string}
113 */
114 ve.dm.MWDocumentReferences.static.contentLangDigits = function ( num ) {
115 const contentLang = mw.config.get( 'wgContentLanguage' );
116 const digitLookup = mw.config.get( 'wgTranslateNumerals' ) &&
117 mw.language.getData( contentLang, 'digitTransformTable' );
118 const numString = String( num );
119 if ( !digitLookup ) {
120 return numString;
121 }
122 return numString.split( '' ).map( ( numChar ) => digitLookup[ numChar ] ).join( '' );
123 };
124
125 /**
126 * @deprecated Should be refactored to store formatted index numbers as a simple
127 * property on each CE ref node after document transaction.
128 * @param {string} groupName Ref group without prefix
129 * @param {string} listKey Ref key with prefix
130 * @return {string} Rendered index number string which can be used as a footnote
131 * marker or reflist item number.
132 */
133 ve.dm.MWDocumentReferences.prototype.getIndexLabel = function ( groupName, listKey ) {
134 return this.getGroupRefs( groupName ).getIndexLabel( listKey );
135 };