Debian Codegraph @

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

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor DataModel MWReferenceModel class.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 /**
11 * Corresponds to one ref and its metadata, chosen for an action.
12 *
13 * TODO: Distinguish this module from ve.dm.MWReferenceNode
14 *
15 * @constructor
16 * @mixes OO.EventEmitter
17 * @param {ve.dm.Document} [parentDoc] The parent Document we can use to auto-generate a blank
18 * Document for the reference in case {@see setDocument} was never called
19 * @property {ve.dm.Document|Function|undefined} doc Might be deferred via a function, to be
20 * lazy-evaluated when {@see getDocument} is called
21 */
22 ve.dm.MWReferenceModel = function VeDmMWReferenceModel( parentDoc ) {
23 // Mixin constructors
24 OO.EventEmitter.call( this );
25
26 // Properties
27 this.extendsRef = null;
28 this.listKey = '';
29 this.listGroup = '';
30 this.listIndex = null;
31 this.group = '';
32 if ( parentDoc ) {
33 this.doc = () => parentDoc.cloneWithData( [
34 { type: 'paragraph', internal: { generated: 'wrapper' } },
35 { type: '/paragraph' },
36 { type: 'internalList' },
37 { type: '/internalList' }
38 ] );
39 }
40 };
41
42 /* Inheritance */
43
44 OO.mixinClass( ve.dm.MWReferenceModel, OO.EventEmitter );
45
46 /* Static Methods */
47
48 /**
49 * Create a reference model from a reference internal item.
50 *
51 * @param {ve.dm.MWReferenceNode} node Reference node
52 * @return {ve.dm.MWReferenceModel} Reference model
53 */
54 ve.dm.MWReferenceModel.static.newFromReferenceNode = function ( node ) {
55 const doc = node.getDocument();
56 const internalList = doc.getInternalList();
57 const attributes = node.getAttributes();
58 const ref = new ve.dm.MWReferenceModel();
59
60 ref.extendsRef = attributes.extendsRef;
61 ref.listKey = attributes.listKey;
62 ref.listGroup = attributes.listGroup;
63 ref.listIndex = attributes.listIndex;
64 ref.group = attributes.refGroup;
65 ref.doc = function () {
66 // cloneFromRange is very expensive, so lazy evaluate it
67 return doc.cloneFromRange( internalList.getItemNode( attributes.listIndex ).getRange() );
68 };
69
70 return ref;
71 };
72
73 /* Methods */
74
75 /**
76 * Find matching item in a surface.
77 *
78 * @param {ve.dm.Surface} surfaceModel Surface reference is in
79 * @return {ve.dm.InternalItemNode|null} Internal reference item, null if none exists
80 */
81 ve.dm.MWReferenceModel.prototype.findInternalItem = function ( surfaceModel ) {
82 if ( this.listIndex !== null ) {
83 return surfaceModel.getDocument().getInternalList().getItemNode( this.listIndex );
84 }
85 return null;
86 };
87
88 /**
89 * Insert reference internal item into a surface.
90 *
91 * If the internal item for this reference doesn't exist, use this method to create one.
92 * The inserted reference is empty and auto-numbered.
93 *
94 * @param {ve.dm.Surface} surfaceModel Surface model of main document
95 */
96 ve.dm.MWReferenceModel.prototype.insertInternalItem = function ( surfaceModel ) {
97 // Create new internal item
98 const doc = surfaceModel.getDocument();
99 const internalList = doc.getInternalList();
100
101 // Fill in data
102 this.listKey = 'auto/' + internalList.getNextUniqueNumber();
103 this.listGroup = 'mwReference/' + this.group;
104
105 // Insert internal reference item into document
106 const item = internalList.getItemInsertion( this.listGroup, this.listKey, [] );
107 surfaceModel.change( item.transaction );
108 this.listIndex = item.index;
109
110 // Inject reference document into internal reference item
111 surfaceModel.change(
112 ve.dm.TransactionBuilder.static.newFromDocumentInsertion(
113 doc,
114 internalList.getItemNode( item.index ).getRange().start,
115 this.getDocument()
116 )
117 );
118 };
119
120 /**
121 * Update an internal reference item.
122 *
123 * An internal item for the reference will be created if no `ref` argument is given.
124 *
125 * @param {ve.dm.Surface} surfaceModel Surface model of main document
126 */
127 ve.dm.MWReferenceModel.prototype.updateInternalItem = function ( surfaceModel ) {
128 const doc = surfaceModel.getDocument();
129 const internalList = doc.getInternalList();
130 const listGroup = 'mwReference/' + this.group;
131
132 // Group/key has changed
133 if ( this.listGroup !== listGroup ) {
134 // Get all reference nodes with the same group and key
135 const group = internalList.getNodeGroup( this.listGroup );
136 const refNodes = group.keyedNodes[ this.listKey ] ?
137 group.keyedNodes[ this.listKey ].slice() :
138 [ group.firstNodes[ this.listIndex ] ];
139 // Check for name collision when moving items between groups
140 const keyIndex = internalList.getKeyIndex( this.listGroup, this.listKey );
141 if ( keyIndex !== undefined ) {
142 // Resolve name collision by generating a new list key
143 this.listKey = 'auto/' + internalList.getNextUniqueNumber();
144 }
145 // Update the group name of all references nodes with the same group and key
146 const txs = [];
147 for ( let i = 0, len = refNodes.length; i < len; i++ ) {
148 txs.push( ve.dm.TransactionBuilder.static.newFromAttributeChanges(
149 doc,
150 refNodes[ i ].getOuterRange().start,
151 { refGroup: this.group, listGroup: listGroup }
152 ) );
153 }
154 surfaceModel.change( txs );
155 this.listGroup = listGroup;
156 }
157 // Update internal node content
158 const itemNodeRange = internalList.getItemNode( this.listIndex ).getRange();
159 surfaceModel.change(
160 ve.dm.TransactionBuilder.static
161 .newFromRemoval( doc, itemNodeRange, true ) );
162 surfaceModel.change(
163 ve.dm.TransactionBuilder.static
164 .newFromDocumentInsertion( doc, itemNodeRange.start, this.getDocument() ) );
165 };
166
167 /**
168 * Insert reference at the end of a surface fragment.
169 *
170 * @param {ve.dm.SurfaceFragment} surfaceFragment Surface fragment to insert at
171 * @param {boolean} [placeholder] Reference is a placeholder for staging purposes
172 */
173 ve.dm.MWReferenceModel.prototype.insertReferenceNode = function ( surfaceFragment, placeholder ) {
174 const attributes = {
175 extendsRef: this.extendsRef,
176 listKey: this.listKey,
177 listGroup: this.listGroup,
178 listIndex: this.listIndex,
179 refGroup: this.group
180 };
181 if ( placeholder ) {
182 attributes.placeholder = true;
183 }
184 surfaceFragment
185 .insertContent( [
186 {
187 type: 'mwReference',
188 attributes: attributes,
189 // See ve.dm.MWReferenceNode.static.cloneElement
190 originalDomElementsHash: Math.random()
191 },
192 { type: '/mwReference' }
193 ] );
194 };
195
196 /**
197 * Get the key of a reference in the references list.
198 *
199 * @return {string} Reference's list key
200 */
201 ve.dm.MWReferenceModel.prototype.getListKey = function () {
202 return this.listKey;
203 };
204
205 /**
206 * Get the name of the group a references list is in.
207 *
208 * @return {string} References list's group
209 */
210 ve.dm.MWReferenceModel.prototype.getListGroup = function () {
211 return this.listGroup;
212 };
213
214 /**
215 * Get the index of reference in the references list.
216 *
217 * @return {string} Reference's index
218 */
219 ve.dm.MWReferenceModel.prototype.getListIndex = function () {
220 return this.listIndex;
221 };
222
223 /**
224 * Get the name of the group a reference is in.
225 *
226 * @return {string} Reference's group
227 */
228 ve.dm.MWReferenceModel.prototype.getGroup = function () {
229 return this.group;
230 };
231
232 /**
233 * Get reference document.
234 *
235 * Auto-generates a blank document if no document exists.
236 *
237 * @return {ve.dm.Document} The (small) document with the content of the reference
238 */
239 ve.dm.MWReferenceModel.prototype.getDocument = function () {
240 if ( typeof this.doc === 'function' ) {
241 this.doc = this.doc();
242 }
243 return this.doc;
244 };
245
246 /**
247 * Set the name of the group a reference is in.
248 *
249 * @param {string} group Reference's group
250 */
251 ve.dm.MWReferenceModel.prototype.setGroup = function ( group ) {
252 this.group = group;
253 };
254
255 /**
256 * Set the reference document.
257 *
258 * @param {ve.dm.Document} doc The (small) document with the content of the reference
259 */
260 ve.dm.MWReferenceModel.prototype.setDocument = function ( doc ) {
261 this.doc = doc;
262 };