| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.MWReferenceEditPanel = function VeUiMWReferenceEditPanel( config ) { |
| 19 |
|
| 20 |
config = Object.assign( { |
| 21 |
scrollable: true, |
| 22 |
padded: true |
| 23 |
}, config ); |
| 24 |
|
| 25 |
|
| 26 |
ve.ui.MWReferenceEditPanel.super.call( this, { scrollable: true, padded: true } ); |
| 27 |
|
| 28 |
|
| 29 |
this.$element.addClass( 've-ui-mwReferenceEditPanel' ); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
this.docRefs = null; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
this.referenceModel = null; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
this.originalGroup = null; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
this.isInsertingSubRef = false; |
| 51 |
|
| 52 |
|
| 53 |
this.referenceTarget = ve.init.target.createTargetWidget( |
| 54 |
{ |
| 55 |
includeCommands: null, |
| 56 |
excludeCommands: this.constructor.static.getExcludeCommands(), |
| 57 |
importRules: this.constructor.static.getImportRules(), |
| 58 |
inDialog: 'reference', |
| 59 |
placeholder: ve.msg( 'cite-ve-dialog-reference-placeholder' ) |
| 60 |
} |
| 61 |
); |
| 62 |
this.contentFieldset = new OO.ui.FieldsetLayout(); |
| 63 |
this.contentFieldset.$element.append( |
| 64 |
this.referenceTarget.$element |
| 65 |
); |
| 66 |
|
| 67 |
|
| 68 |
this.optionsFieldset = new OO.ui.FieldsetLayout( { |
| 69 |
label: ve.msg( 'cite-ve-dialog-reference-options-section' ), |
| 70 |
icon: 'settings' |
| 71 |
} ); |
| 72 |
this.referenceGroupInput = new ve.ui.MWReferenceGroupInputWidget( { |
| 73 |
$overlay: config.$overlay, |
| 74 |
emptyGroupName: ve.msg( 'cite-ve-dialog-reference-options-group-placeholder' ) |
| 75 |
} ); |
| 76 |
this.referenceGroupField = new OO.ui.FieldLayout( this.referenceGroupInput, { |
| 77 |
align: 'top', |
| 78 |
label: ve.msg( 'cite-ve-dialog-reference-options-group-label' ) |
| 79 |
} ); |
| 80 |
this.optionsFieldset.addItems( [ this.referenceGroupField ] ); |
| 81 |
|
| 82 |
|
| 83 |
this.reuseWarning = new OO.ui.MessageWidget( { |
| 84 |
icon: 'alert', |
| 85 |
inline: true, |
| 86 |
classes: [ 've-ui-mwReferenceDialog-reuseWarning' ] |
| 87 |
} ); |
| 88 |
|
| 89 |
this.extendsWarning = new OO.ui.MessageWidget( { |
| 90 |
icon: 'alert', |
| 91 |
inline: true, |
| 92 |
classes: [ 've-ui-mwReferenceDialog-extendsWarning' ] |
| 93 |
} ); |
| 94 |
|
| 95 |
this.referenceTarget.connect( this, { change: 'onInputChange' } ); |
| 96 |
this.referenceGroupInput.connect( this, { change: 'onInputChange' } ); |
| 97 |
|
| 98 |
|
| 99 |
this.$element.append( |
| 100 |
this.reuseWarning.$element, |
| 101 |
this.extendsWarning.$element, |
| 102 |
this.contentFieldset.$element, |
| 103 |
this.optionsFieldset.$element |
| 104 |
); |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
OO.inheritClass( ve.ui.MWReferenceEditPanel, OO.ui.PanelLayout ); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
ve.ui.MWReferenceEditPanel.static.excludeCommands = [ |
| 123 |
|
| 124 |
'paragraph', |
| 125 |
'heading1', |
| 126 |
'heading2', |
| 127 |
'heading3', |
| 128 |
'heading4', |
| 129 |
'heading5', |
| 130 |
'heading6', |
| 131 |
'preformatted', |
| 132 |
'blockquote', |
| 133 |
|
| 134 |
'insertTable', |
| 135 |
'deleteTable', |
| 136 |
'mergeCells', |
| 137 |
'tableCaption', |
| 138 |
'tableCellHeader', |
| 139 |
'tableCellData', |
| 140 |
|
| 141 |
'bullet', |
| 142 |
'bulletWrapOnce', |
| 143 |
'number', |
| 144 |
'numberWrapOnce', |
| 145 |
'indent', |
| 146 |
'outdent', |
| 147 |
|
| 148 |
'reference', |
| 149 |
'reference/existing', |
| 150 |
'citoid', |
| 151 |
'referencesList' |
| 152 |
]; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
ve.ui.MWReferenceEditPanel.static.getExcludeCommands = function () { |
| 162 |
const citeCommands = Object.keys( ve.init.target.getSurface().commandRegistry.registry ) |
| 163 |
.filter( ( command ) => command.indexOf( 'cite-' ) !== -1 ); |
| 164 |
|
| 165 |
return ve.ui.MWReferenceEditPanel.static.excludeCommands.concat( citeCommands ); |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
ve.ui.MWReferenceEditPanel.static.getImportRules = function () { |
| 175 |
const rules = ve.copy( ve.init.target.constructor.static.importRules ); |
| 176 |
return ve.extendObject( |
| 177 |
rules, |
| 178 |
{ |
| 179 |
all: { |
| 180 |
blacklist: ve.extendObject( |
| 181 |
{ |
| 182 |
|
| 183 |
mwReference: true, |
| 184 |
mwReferencesList: true, |
| 185 |
|
| 186 |
|
| 187 |
list: true, |
| 188 |
listItem: true, |
| 189 |
definitionList: true, |
| 190 |
definitionListItem: true, |
| 191 |
table: true, |
| 192 |
tableCaption: true, |
| 193 |
tableSection: true, |
| 194 |
tableRow: true, |
| 195 |
tableCell: true, |
| 196 |
mwTable: true, |
| 197 |
mwTransclusionTableCell: true |
| 198 |
}, |
| 199 |
ve.getProp( rules, 'all', 'blacklist' ) |
| 200 |
), |
| 201 |
|
| 202 |
conversions: ve.extendObject( |
| 203 |
{ |
| 204 |
mwHeading: 'paragraph' |
| 205 |
}, |
| 206 |
ve.getProp( rules, 'all', 'conversions' ) |
| 207 |
) |
| 208 |
} |
| 209 |
} |
| 210 |
); |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
ve.ui.MWReferenceEditPanel.prototype.setDocumentReferences = function ( docRefs ) { |
| 217 |
this.docRefs = docRefs; |
| 218 |
this.referenceGroupInput.populateMenu( docRefs.getAllGroupNames() ); |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
ve.ui.MWReferenceEditPanel.prototype.setReferenceForEditing = function ( ref ) { |
| 225 |
this.referenceModel = ref; |
| 226 |
this.isInsertingSubRef = ref.extendsRef && !ref.getDocument().data.hasContent(); |
| 227 |
|
| 228 |
this.setFormFieldsFromRef( ref ); |
| 229 |
this.updateReuseWarningFromRef( ref ); |
| 230 |
this.updateExtendsWarningFromRef( ref ); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
ve.ui.MWReferenceEditPanel.prototype.getReferenceFromEditing = function () { |
| 237 |
if ( this.referenceModel ) { |
| 238 |
this.referenceModel.setGroup( this.referenceGroupInput.getValue() ); |
| 239 |
} |
| 240 |
|
| 241 |
return this.referenceModel; |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
ve.ui.MWReferenceEditPanel.prototype.setFormFieldsFromRef = function ( ref ) { |
| 249 |
this.referenceTarget.setDocument( ref.getDocument() ); |
| 250 |
|
| 251 |
if ( this.isInsertingSubRef ) { |
| 252 |
this.referenceTarget.getSurface().setPlaceholder( |
| 253 |
ve.msg( 'cite-ve-dialog-reference-editing-add-details-placeholder' ) |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
this.optionsFieldset.toggle( !ref.extendsRef ); |
| 258 |
|
| 259 |
this.originalGroup = ref.getGroup(); |
| 260 |
|
| 261 |
|
| 262 |
this.referenceGroupInput.setDisabled( true ); |
| 263 |
this.referenceGroupInput.setValue( this.originalGroup ); |
| 264 |
this.referenceGroupInput.setDisabled( false ); |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
ve.ui.MWReferenceEditPanel.prototype.updateReuseWarningFromRef = function ( ref ) { |
| 272 |
const totalUsageCount = this.docRefs |
| 273 |
.getGroupRefs( ref.getListGroup() ) |
| 274 |
.getTotalUsageCount( ref.getListKey() ); |
| 275 |
this.reuseWarning |
| 276 |
.toggle( totalUsageCount > 1 ) |
| 277 |
.setLabel( ve.msg( 'cite-ve-dialog-reference-editing-reused-long', totalUsageCount ) ); |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
ve.ui.MWReferenceEditPanel.prototype.updateExtendsWarningFromRef = function ( ref ) { |
| 285 |
if ( ref.extendsRef ) { |
| 286 |
const parentNode = this.docRefs |
| 287 |
.getGroupRefs( ref.getListGroup() ) |
| 288 |
.getInternalModelNode( ref.extendsRef ); |
| 289 |
this.extendsWarning.setLabel( |
| 290 |
$( '<p>' ) |
| 291 |
.text( ve.msg( this.isInsertingSubRef ? |
| 292 |
'cite-ve-dialog-reference-editing-add-details' : |
| 293 |
'cite-ve-dialog-reference-editing-extends' |
| 294 |
) ) |
| 295 |
.append( parentNode ? |
| 296 |
new ve.ui.MWPreviewElement( parentNode, { useView: true } ).$element : |
| 297 |
$( '<div>' ) |
| 298 |
.addClass( 've-ui-mwReferenceContextItem-muted' ) |
| 299 |
.text( ve.msg( 'cite-ve-dialog-reference-missing-parent-ref' ) ) |
| 300 |
) |
| 301 |
); |
| 302 |
this.extendsWarning.setIcon( this.isInsertingSubRef ? null : 'alert' ); |
| 303 |
this.extendsWarning.toggle( true ); |
| 304 |
} else { |
| 305 |
this.extendsWarning.toggle( false ); |
| 306 |
} |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
ve.ui.MWReferenceEditPanel.prototype.onInputChange = function () { |
| 316 |
this.emit( 'change', { |
| 317 |
isModified: this.isModified(), |
| 318 |
hasContent: this.documentHasContent() |
| 319 |
} ); |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
ve.ui.MWReferenceEditPanel.prototype.documentHasContent = function () { |
| 329 |
|
| 330 |
return this.referenceModel && this.referenceModel.getDocument().data.hasContent(); |
| 331 |
}; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
ve.ui.MWReferenceEditPanel.prototype.isModified = function () { |
| 340 |
return this.documentHasContent() && ( this.referenceTarget.hasBeenModified() || |
| 341 |
this.referenceGroupInput.getValue() !== this.originalGroup ); |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
ve.ui.MWReferenceEditPanel.prototype.focus = function () { |
| 348 |
this.referenceTarget.focus(); |
| 349 |
}; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
ve.ui.MWReferenceEditPanel.prototype.setReadOnly = function ( readOnly ) { |
| 355 |
this.referenceTarget.setReadOnly( !!readOnly ); |
| 356 |
this.referenceGroupInput.setReadOnly( !!readOnly ); |
| 357 |
}; |
| 358 |
|
| 359 |
ve.ui.MWReferenceEditPanel.prototype.clear = function () { |
| 360 |
this.referenceTarget.clear(); |
| 361 |
this.referenceModel = null; |
| 362 |
}; |