| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.dm.MWGroupReferences = function VeDmMWGroupReferences() { |
| 17 |
|
| 18 |
OO.EventEmitter.call( this ); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
this.footnoteNumberLookup = {}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
this.footnoteLabelLookup = {}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
this.subRefsByParent = {}; |
| 44 |
|
| 45 |
|
| 46 |
this.topLevelCounter = 1; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
this.nodeGroup = null; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
OO.initClass( ve.dm.MWGroupReferences ); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 123 |
'.' + ve.dm.MWDocumentReferences.static.contentLangDigits( subrefIndex ); |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
ve.dm.MWGroupReferences.prototype.isEmpty = function () { |
| 132 |
|
| 133 |
|
| 134 |
return this.topLevelCounter === 1; |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 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 |
|
| 161 |
|
| 162 |
.filter( ( listKey ) => this.footnoteNumberLookup[ listKey ][ 1 ] === -1 ); |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
ve.dm.MWGroupReferences.prototype.getInternalModelNode = function ( key ) { |
| 187 |
const ref = this.getRefNode( key ); |
| 188 |
return ref && ref.getInternalItem(); |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 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 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 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 |
|
| 229 |
|
| 230 |
|
| 231 |
ve.dm.MWGroupReferences.prototype.getSubrefs = function ( parentKey ) { |
| 232 |
return this.subRefsByParent[ parentKey ] || []; |
| 233 |
}; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
ve.dm.MWGroupReferences.prototype.getIndexLabel = function ( listKey ) { |
| 241 |
return this.footnoteLabelLookup[ listKey ]; |
| 242 |
}; |