| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.MWCitationDialog = function VeUiMWCitationDialog( config ) { |
| 19 |
|
| 20 |
ve.ui.MWCitationDialog.super.call( this, config ); |
| 21 |
|
| 22 |
|
| 23 |
this.referenceModel = null; |
| 24 |
this.referenceNode = null; |
| 25 |
this.inDialog = ''; |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
OO.inheritClass( ve.ui.MWCitationDialog, ve.ui.MWTransclusionDialog ); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
ve.ui.MWCitationDialog.static.name = 'cite'; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
ve.ui.MWCitationDialog.prototype.getReferenceNode = function () { |
| 44 |
const selectedNode = this.getFragment().getSelectedNode(); |
| 45 |
|
| 46 |
if ( selectedNode instanceof ve.dm.MWReferenceNode ) { |
| 47 |
return selectedNode; |
| 48 |
} |
| 49 |
|
| 50 |
return null; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
ve.ui.MWCitationDialog.prototype.getSelectedNode = function () { |
| 57 |
const referenceNode = this.getReferenceNode(); |
| 58 |
|
| 59 |
if ( referenceNode ) { |
| 60 |
const branches = referenceNode.getInternalItem().getChildren(); |
| 61 |
const leaves = branches && |
| 62 |
branches.length === 1 && |
| 63 |
branches[ 0 ].canContainContent() && |
| 64 |
branches[ 0 ].getChildren(); |
| 65 |
const transclusionNode = leaves && |
| 66 |
leaves.length === 1 && |
| 67 |
leaves[ 0 ] instanceof ve.dm.MWTransclusionNode && |
| 68 |
leaves[ 0 ]; |
| 69 |
|
| 70 |
|
| 71 |
if ( transclusionNode && transclusionNode.isSingleTemplate( this.citationTemplate ) ) { |
| 72 |
return transclusionNode; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return null; |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
ve.ui.MWCitationDialog.prototype.initialize = function ( data ) { |
| 83 |
|
| 84 |
ve.ui.MWCitationDialog.super.prototype.initialize.call( this, data ); |
| 85 |
|
| 86 |
|
| 87 |
this.$content.addClass( 've-ui-mwTransclusionDialog-single' ); |
| 88 |
|
| 89 |
this.$content.on( 'change', this.onInputChange.bind( this ) ); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
ve.ui.MWCitationDialog.prototype.getSetupProcess = function ( data ) { |
| 96 |
return ve.ui.MWCitationDialog.super.prototype.getSetupProcess.call( this, data ) |
| 97 |
.first( () => { |
| 98 |
data = data || {}; |
| 99 |
this.inDialog = data.inDialog; |
| 100 |
this.citationTemplate = data.template; |
| 101 |
this.citationTitle = data.title; |
| 102 |
|
| 103 |
this.trackedCitationInputChange = false; |
| 104 |
} ) |
| 105 |
.next( () => { |
| 106 |
this.updateTitle(); |
| 107 |
|
| 108 |
|
| 109 |
this.referenceNode = this.getReferenceNode(); |
| 110 |
if ( this.referenceNode ) { |
| 111 |
this.referenceModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( |
| 112 |
this.referenceNode |
| 113 |
); |
| 114 |
} |
| 115 |
} ); |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
ve.ui.MWCitationDialog.prototype.updateTitle = function () { |
| 122 |
if ( this.citationTitle ) { |
| 123 |
this.title.setLabel( this.citationTitle ); |
| 124 |
} else { |
| 125 |
|
| 126 |
ve.ui.MWCitationDialog.super.prototype.updateTitle.call( this ); |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
ve.ui.MWCitationDialog.prototype.setApplicableStatus = function () { |
| 134 |
ve.ui.MWCitationDialog.super.prototype.setApplicableStatus.call( this ); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
this.actions.setAbilities( { insert: this.transclusionModel.containsValuableData() } ); |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
ve.ui.MWCitationDialog.prototype.getActionProcess = function ( action ) { |
| 145 |
if ( |
| 146 |
this.inDialog !== 'reference' && |
| 147 |
( action === 'done' || action === 'insert' ) |
| 148 |
) { |
| 149 |
return new OO.ui.Process( () => { |
| 150 |
const deferred = $.Deferred(); |
| 151 |
this.checkRequiredParameters().done( () => { |
| 152 |
const surfaceModel = this.getFragment().getSurface(); |
| 153 |
const doc = surfaceModel.getDocument(); |
| 154 |
|
| 155 |
|
| 156 |
if ( this.referenceModel && !this.selectedNode ) { |
| 157 |
const refDoc = this.referenceModel.getDocument(); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
refDoc.commit( |
| 162 |
ve.dm.TransactionBuilder.static |
| 163 |
.newFromRemoval( refDoc, refDoc.getDocumentRange(), true ) |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
if ( !this.referenceModel ) { |
| 168 |
|
| 169 |
this.fragment = this.getFragment().collapseToEnd(); |
| 170 |
this.referenceModel = new ve.dm.MWReferenceModel( doc ); |
| 171 |
this.referenceModel.insertInternalItem( surfaceModel ); |
| 172 |
this.referenceModel.insertReferenceNode( this.getFragment() ); |
| 173 |
} |
| 174 |
|
| 175 |
const item = this.referenceModel.findInternalItem( surfaceModel ); |
| 176 |
if ( item ) { |
| 177 |
if ( this.selectedNode ) { |
| 178 |
this.transclusionModel.updateTransclusionNode( |
| 179 |
surfaceModel, this.selectedNode |
| 180 |
); |
| 181 |
} else if ( this.transclusionModel.getPlainObject() !== null ) { |
| 182 |
this.transclusionModel.insertTransclusionNode( |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
this.getFragment().clone( |
| 192 |
new ve.dm.LinearSelection( item.getChildren()[ 0 ].getRange() ) |
| 193 |
), |
| 194 |
'inline' |
| 195 |
); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
this.referenceModel.setDocument( |
| 203 |
doc.cloneFromRange( |
| 204 |
doc.getInternalList().getItemNode( this.referenceModel.getListIndex() ).getRange() |
| 205 |
) |
| 206 |
); |
| 207 |
this.referenceModel.updateInternalItem( surfaceModel ); |
| 208 |
|
| 209 |
this.close( { action: action } ); |
| 210 |
} ).always( deferred.resolve ); |
| 211 |
|
| 212 |
return deferred; |
| 213 |
} ); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
return ve.ui.MWCitationDialog.super.prototype.getActionProcess.call( this, action ); |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
ve.ui.MWCitationDialog.prototype.getTeardownProcess = function ( data ) { |
| 224 |
return ve.ui.MWCitationDialog.super.prototype.getTeardownProcess.call( this, data ) |
| 225 |
.first( () => { |
| 226 |
|
| 227 |
this.referenceModel = null; |
| 228 |
this.referenceNode = null; |
| 229 |
} ); |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
ve.ui.MWCitationDialog.prototype.onInputChange = function () { |
| 238 |
if ( !this.trackedCitationInputChange ) { |
| 239 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'manual-template-input' } ); |
| 240 |
this.trackedCitationInputChange = true; |
| 241 |
} |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
ve.ui.windowFactory.register( ve.ui.MWCitationDialog ); |