Debian Codegraph @

extensions/Cite/modules/ve-cite/ve.dm.MWGroupReferences.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 * Holds information about the refs from a single Cite group.
10 *
11 * This structure is persisted in memory until a document change affects a ref
12 * tag from this group, at which point it will be fully recalculated.
13 *
14 * @constructor
15 */
16 ve.dm.MWGroupReferences = function VeDmMWGroupReferences() {
17 // Mixin constructors
18 OO.EventEmitter.call( this );
19
20 // Properties
21 /**
22 * Lookup from listKey to a pair of integers which are the [major, minor] footnote numbers
23 * that will be rendered on the ref in some digit system. Note that top-level refs always
24 * have minor number `-1`.
25 *
26 * @member {Object.<string, number[]>}
27 */
28 this.footnoteNumberLookup = {};
29 /**
30 * Lookup from listKey to a rendered footnote number or subref number like "1.2", in the
31 * local content language.
32 *
33 * FIXME: push labeling to presentation code and drop from here.
34 *
35 * @member {Object.<string, string>}
36 */
37 this.footnoteLabelLookup = {};
38 /**
39 * Lookup from parent listKey to subrefs.
40 *
41 * @member {Object.<string, ve.dm.MWReferenceNode[]>}
42 */
43 this.subRefsByParent = {};
44
45 /** @private */
46 this.topLevelCounter = 1;
47 /**
48 * InternalList node group, or null if no such group exists.
49 *
50 * @member {Object|null}
51 * @private
52 */
53 this.nodeGroup = null;
54 };
55
56 /* Inheritance */
57
58 OO.initClass( ve.dm.MWGroupReferences );
59
60 /* Static Methods */
61
62 /**
63 * Rebuild information about this group of references.
64 *
65 * @param {Object} nodeGroup InternalList group object containing refs.
66 * @return {ve.dm.MWGroupReferences}
67 */
68 ve.dm.MWGroupReferences.static.makeGroupRefs = function ( nodeGroup ) {
69 const result = new ve.dm.MWGroupReferences();
70 result.nodeGroup = nodeGroup;
71
72 ( nodeGroup ? nodeGroup.indexOrder : [] )
73 .map( ( index ) => nodeGroup.firstNodes[ index ] )
74 // FIXME: debug null nodes
75 .filter( ( node ) => node && !node.getAttribute( 'placeholder' ) )
76 .forEach( ( node ) => {
77 const listKey = node.getAttribute( 'listKey' );
78 const extendsRef = node.getAttribute( 'extendsRef' );
79
80 if ( !extendsRef ) {
81 result.getOrAllocateTopLevelIndex( listKey );
82 } else {
83 result.addSubref( extendsRef, listKey, node );
84 }
85 } );
86
87 return result;
88 };
89
90 /* Methods */
91
92 /**
93 * @private
94 * @param {string} listKey Full key for the top-level ref
95 * @return {number[]} Allocated topLevelIndex
96 */
97 ve.dm.MWGroupReferences.prototype.getOrAllocateTopLevelIndex = function ( listKey ) {
98 if ( this.footnoteNumberLookup[ listKey ] === undefined ) {
99 const number = this.topLevelCounter++;
100 this.footnoteNumberLookup[ listKey ] = [ number, -1 ];
101 this.footnoteLabelLookup[ listKey ] = ve.dm.MWDocumentReferences.static.contentLangDigits( number );
102 }
103 return this.footnoteNumberLookup[ listKey ][ 0 ];
104 };
105
106 /**
107 * @private
108 * @param {string} parentKey Full key of the parent reference
109 * @param {string} listKey Full key of the subreference
110 * @param {ve.dm.MWReferenceNode} subrefNode Subref to add to internal tracking
111 */
112 ve.dm.MWGroupReferences.prototype.addSubref = function ( parentKey, listKey, subrefNode ) {
113 if ( this.subRefsByParent[ parentKey ] === undefined ) {
114 this.subRefsByParent[ parentKey ] = [];
115 }
116 this.subRefsByParent[ parentKey ].push( subrefNode );
117 const subrefIndex = this.subRefsByParent[ parentKey ].length;
118
119 const topLevelIndex = this.getOrAllocateTopLevelIndex( parentKey );
120 this.footnoteNumberLookup[ listKey ] = [ topLevelIndex, subrefIndex ];
121 this.footnoteLabelLookup[ listKey ] = ve.dm.MWDocumentReferences.static.contentLangDigits( topLevelIndex ) +
122 // FIXME: RTL, and customization of the separator like with mw:referencedBy
123 '.' + ve.dm.MWDocumentReferences.static.contentLangDigits( subrefIndex );
124 };
125
126 /**
127 * Check whether the group has any references.
128 *
129 * @return {boolean}
130 */
131 ve.dm.MWGroupReferences.prototype.isEmpty = function () {
132 // Use an internal shortcut, otherwise we could do something like
133 // !!nodes.indexOrder.length
134 return this.topLevelCounter === 1;
135 };
136
137 /**
138 * List all document references in the order they first appear, ignoring reuses
139 * and placeholders.
140 *
141 * @return {ve.dm.MWReferenceNode[]}
142 */
143 ve.dm.MWGroupReferences.prototype.getAllRefsInDocumentOrder = function () {
144 return Object.keys( this.footnoteNumberLookup )
145 .sort( ( aKey, bKey ) => this.footnoteNumberLookup[ aKey ][ 0 ] - this.footnoteNumberLookup[ bKey ][ 0 ] )
146 .map( ( listKey ) => this.nodeGroup.keyedNodes[ listKey ] )
147 .filter( ( nodes ) => !!nodes )
148 .map( ( nodes ) => nodes[ 0 ] );
149 };
150
151 /**
152 * List all reference listKeys in the order they appear in the reflist including
153 * named refs, unnamed refs, and those that don't resolve
154 *
155 * @return {string[]} Reference listKeys
156 */
157 ve.dm.MWGroupReferences.prototype.getTopLevelKeysInReflistOrder = function () {
158 return Object.keys( this.footnoteNumberLookup )
159 .sort( ( aKey, bKey ) => this.footnoteNumberLookup[ aKey ][ 0 ] - this.footnoteNumberLookup[ bKey ][ 0 ] )
160 // TODO: Function could be split here, if a use case is found for a list of
161 // all numbers including subrefs.
162 .filter( ( listKey ) => this.footnoteNumberLookup[ listKey ][ 1 ] === -1 );
163 };
164
165 /**
166 * Return the defining reference node for this key
167 *
168 * @see #getInternalModelNode
169 *
170 * @param {string} key in listKey format
171 * @return {ve.dm.MWReferenceNode|undefined}
172 */
173 ve.dm.MWGroupReferences.prototype.getRefNode = function ( key ) {
174 const keyedNodes = this.nodeGroup && this.nodeGroup.keyedNodes[ key ];
175 return keyedNodes && keyedNodes[ 0 ];
176 };
177
178 /**
179 * Return the internalList internal item if it exists.
180 *
181 * @see #getRefNode
182 *
183 * @param {string} key in listKey format
184 * @return {ve.dm.InternalItemNode|undefined}
185 */
186 ve.dm.MWGroupReferences.prototype.getInternalModelNode = function ( key ) {
187 const ref = this.getRefNode( key );
188 return ref && ref.getInternalItem();
189 };
190
191 /**
192 * Return document nodes for each usage of a ref key. This excludes usages
193 * under the `<references>` section, so note that nested references won't behave
194 * as expected. The reflist item for a ref is not counted as a reference,
195 * either.
196 *
197 * FIXME: Implement backlinks from within a nested ref within the footnote body.
198 *
199 * @param {string} key in listKey format
200 * @return {ve.dm.MWReferenceNode[]}
201 */
202 ve.dm.MWGroupReferences.prototype.getRefUsages = function ( key ) {
203 return ( this.nodeGroup && this.nodeGroup.keyedNodes[ key ] || [] )
204 .filter( ( node ) => !node.getAttribute( 'placeholder' ) &&
205 !node.findParent( ve.dm.MWReferencesListNode )
206 );
207 };
208
209 /**
210 * Get the total number of usages for a reference, including sub-references.
211 *
212 * @param {string} listKey Full key of the reference
213 * @return {number} Total usage count of main refs and subrefs
214 */
215 ve.dm.MWGroupReferences.prototype.getTotalUsageCount = function ( listKey ) {
216 const mainRefs = this.getRefUsages( listKey );
217 const mainRefsCount = mainRefs.length;
218
219 const subrefs = this.getSubrefs( listKey );
220 const subrefsCount = subrefs.length;
221
222 const totalUsageCount = mainRefsCount + subrefsCount;
223
224 return totalUsageCount;
225 };
226
227 /**
228 * @param {string} parentKey parent ref key
229 * @return {ve.dm.MWReferenceNode[]} List of subrefs for this parent
230 */
231 ve.dm.MWGroupReferences.prototype.getSubrefs = function ( parentKey ) {
232 return this.subRefsByParent[ parentKey ] || [];
233 };
234
235 /**
236 * @deprecated TODO: push to presentation
237 * @param {string} listKey full ref key
238 * @return {string} rendered number label
239 */
240 ve.dm.MWGroupReferences.prototype.getIndexLabel = function ( listKey ) {
241 return this.footnoteLabelLookup[ listKey ];
242 };