mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-23 22:45:20 +00:00
Remove separate private deferDoc property in ve.dm.MWReferenceModel
This is a direct follow-up for what I started in I3c9b9bb. Again all this does is moving existing code around. The separate this.deferDoc property is never used anywhere: https://codesearch.wmcloud.org/search/?q=deferDoc&files=%5C.js%24 Instead I store the deferred function directly in the target property this.doc and resolve it when needed. Note that the concept of a parentDoc still exists. I tried to make this as obvious as possible via comments and by arranging the code accordingly: 1. A lot of code calls setDocument which directly sets this.doc. The parentDoc is never used in this case. 2. The parentDoc is only needed to auto-generate the (empty) document for the reference when a new reference is created. That's also why the existing code always calls the constructor with the parentDoc, even if this ends being unused in many cases: It's a simple fallback. Bug: T363096 Change-Id: I7874f187b2b397ed511b695ca9ff95838fcff302
This commit is contained in:
parent
81f774c789
commit
6c5c2dc6cb
|
@ -12,7 +12,10 @@
|
|||
*
|
||||
* @constructor
|
||||
* @mixes OO.EventEmitter
|
||||
* @param {ve.dm.Document} [parentDoc] Document that contains or will contain the reference
|
||||
* @param {ve.dm.Document} [parentDoc] The parent Document we can use to auto-generate a blank
|
||||
* Document for the reference in case {@see setDocument} was never called
|
||||
* @property {ve.dm.Document|Function|undefined} doc Might be deferred via a function, to be
|
||||
* lazy-evaluated when {@see getDocument} is called
|
||||
*/
|
||||
ve.dm.MWReferenceModel = function VeDmMWReferenceModel( parentDoc ) {
|
||||
// Mixin constructors
|
||||
|
@ -24,13 +27,14 @@ ve.dm.MWReferenceModel = function VeDmMWReferenceModel( parentDoc ) {
|
|||
this.listGroup = '';
|
||||
this.listIndex = null;
|
||||
this.group = '';
|
||||
this.doc = null;
|
||||
this.deferDoc = () => parentDoc.cloneWithData( [
|
||||
{ type: 'paragraph', internal: { generated: 'wrapper' } },
|
||||
{ type: '/paragraph' },
|
||||
{ type: 'internalList' },
|
||||
{ type: '/internalList' }
|
||||
] );
|
||||
if ( parentDoc ) {
|
||||
this.doc = () => parentDoc.cloneWithData( [
|
||||
{ type: 'paragraph', internal: { generated: 'wrapper' } },
|
||||
{ type: '/paragraph' },
|
||||
{ type: 'internalList' },
|
||||
{ type: '/internalList' }
|
||||
] );
|
||||
}
|
||||
};
|
||||
|
||||
/* Inheritance */
|
||||
|
@ -56,7 +60,7 @@ ve.dm.MWReferenceModel.static.newFromReferenceNode = function ( node ) {
|
|||
ref.listGroup = attributes.listGroup;
|
||||
ref.listIndex = attributes.listIndex;
|
||||
ref.group = attributes.refGroup;
|
||||
ref.deferDoc = function () {
|
||||
ref.doc = function () {
|
||||
// cloneFromRange is very expensive, so lazy evaluate it
|
||||
return doc.cloneFromRange( internalList.getItemNode( attributes.listIndex ).getRange() );
|
||||
};
|
||||
|
@ -228,12 +232,11 @@ ve.dm.MWReferenceModel.prototype.getGroup = function () {
|
|||
*
|
||||
* Auto-generates a blank document if no document exists.
|
||||
*
|
||||
* @return {ve.dm.Document} Reference document
|
||||
* @return {ve.dm.Document} The (small) document with the content of the reference
|
||||
*/
|
||||
ve.dm.MWReferenceModel.prototype.getDocument = function () {
|
||||
if ( !this.doc ) {
|
||||
this.doc = this.deferDoc();
|
||||
delete this.deferDoc;
|
||||
if ( typeof this.doc === 'function' ) {
|
||||
this.doc = this.doc();
|
||||
}
|
||||
return this.doc;
|
||||
};
|
||||
|
@ -250,7 +253,7 @@ ve.dm.MWReferenceModel.prototype.setGroup = function ( group ) {
|
|||
/**
|
||||
* Set the reference document.
|
||||
*
|
||||
* @param {ve.dm.Document} doc Reference document
|
||||
* @param {ve.dm.Document} doc The (small) document with the content of the reference
|
||||
*/
|
||||
ve.dm.MWReferenceModel.prototype.setDocument = function ( doc ) {
|
||||
this.doc = doc;
|
||||
|
|
Loading…
Reference in a new issue