| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
ve.ui.MWTemplateDialog = function VeUiMWTemplateDialog( config ) { |
| 29 |
|
| 30 |
ve.ui.MWTemplateDialog.super.call( this, config ); |
| 31 |
|
| 32 |
|
| 33 |
this.transclusionModel = null; |
| 34 |
this.loaded = false; |
| 35 |
this.altered = false; |
| 36 |
this.canGoBack = false; |
| 37 |
this.preventReselection = false; |
| 38 |
|
| 39 |
this.confirmDialogs = new ve.ui.WindowManager( { factory: ve.ui.windowFactory, isolate: true } ); |
| 40 |
$( OO.ui.getTeleportTarget() ).append( this.confirmDialogs.$element ); |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
OO.inheritClass( ve.ui.MWTemplateDialog, ve.ui.NodeDialog ); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
ve.ui.MWTemplateDialog.static.modelClasses = [ ve.dm.MWTransclusionNode ]; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
ve.ui.MWTemplateDialog.static.bookletLayoutConfig = {}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
ve.ui.MWTemplateDialog.prototype.getReadyProcess = function ( data ) { |
| 66 |
return ve.ui.MWTemplateDialog.super.prototype.getReadyProcess.call( this, data ) |
| 67 |
.next( () => { |
| 68 |
if ( this.transclusionModel.isEmpty() ) { |
| 69 |
|
| 70 |
this.bookletLayout.focus(); |
| 71 |
} |
| 72 |
|
| 73 |
this.bookletLayout.getPagesOrdered().forEach( ( page ) => { |
| 74 |
if ( page instanceof ve.ui.MWParameterPage ) { |
| 75 |
page.updateSize(); |
| 76 |
} |
| 77 |
} ); |
| 78 |
} ); |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
ve.ui.MWTemplateDialog.prototype.touch = function () { |
| 87 |
if ( this.loaded ) { |
| 88 |
this.altered = true; |
| 89 |
this.setApplicableStatus(); |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
ve.ui.MWTemplateDialog.prototype.onReplacePart = function ( removed, added ) { |
| 101 |
const removePages = []; |
| 102 |
|
| 103 |
if ( removed ) { |
| 104 |
|
| 105 |
if ( removed instanceof ve.dm.MWTemplateModel ) { |
| 106 |
const params = removed.getParameters(); |
| 107 |
for ( const name in params ) { |
| 108 |
removePages.push( params[ name ].getId() ); |
| 109 |
} |
| 110 |
removed.disconnect( this ); |
| 111 |
} |
| 112 |
removePages.push( removed.getId() ); |
| 113 |
this.bookletLayout.removePages( removePages ); |
| 114 |
} |
| 115 |
|
| 116 |
if ( added ) { |
| 117 |
const page = this.getPageFromPart( added ); |
| 118 |
if ( page ) { |
| 119 |
let reselect; |
| 120 |
|
| 121 |
this.bookletLayout.addPages( [ page ], this.transclusionModel.getIndex( added ) ); |
| 122 |
if ( removed ) { |
| 123 |
|
| 124 |
|
| 125 |
reselect = added.getId(); |
| 126 |
} |
| 127 |
|
| 128 |
if ( added instanceof ve.dm.MWTemplateModel ) { |
| 129 |
|
| 130 |
this.preventReselection = true; |
| 131 |
|
| 132 |
|
| 133 |
const names = added.getOrderedParameterNames(); |
| 134 |
for ( let i = 0; i < names.length; i++ ) { |
| 135 |
this.onAddParameter( added.getParameter( names[ i ] ) ); |
| 136 |
} |
| 137 |
added.connect( this, { add: 'onAddParameter', remove: 'onRemoveParameter' } ); |
| 138 |
|
| 139 |
this.preventReselection = false; |
| 140 |
|
| 141 |
if ( this.loaded ) { |
| 142 |
if ( reselect ) { |
| 143 |
this.bookletLayout.focusPart( reselect ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
const documentedParameters = added.getSpec().getDocumentedParameterOrder(), |
| 148 |
undocumentedParameters = added.getSpec().getUndocumentedParameterNames(); |
| 149 |
|
| 150 |
if ( !documentedParameters.length || undocumentedParameters.length ) { |
| 151 |
page.addPlaceholderParameter(); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if ( added || removed ) { |
| 158 |
this.touch(); |
| 159 |
} |
| 160 |
this.updateTitle(); |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
ve.ui.MWTemplateDialog.prototype.onAddParameter = function ( param ) { |
| 170 |
let page; |
| 171 |
|
| 172 |
if ( param.getName() ) { |
| 173 |
page = new ve.ui.MWParameterPage( param, { |
| 174 |
$overlay: this.$overlay, readOnly: this.isReadOnly() |
| 175 |
} ) |
| 176 |
.connect( this, { |
| 177 |
hasValueChange: 'onHasValueChange' |
| 178 |
} ); |
| 179 |
} else { |
| 180 |
|
| 181 |
page = new ve.ui.MWAddParameterPage( param, param.getId(), { |
| 182 |
$overlay: this.$overlay |
| 183 |
} ) |
| 184 |
.connect( this, { |
| 185 |
templateParameterAdded: this.bookletLayout.focusPart.bind( this.bookletLayout ) |
| 186 |
} ); |
| 187 |
} |
| 188 |
this.bookletLayout.addPages( [ page ], this.transclusionModel.getIndex( param ) ); |
| 189 |
if ( this.loaded ) { |
| 190 |
this.touch(); |
| 191 |
|
| 192 |
if ( page instanceof ve.ui.MWParameterPage ) { |
| 193 |
page.updateSize(); |
| 194 |
} |
| 195 |
} |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
ve.ui.MWTemplateDialog.prototype.onRemoveParameter = function ( param ) { |
| 205 |
this.bookletLayout.removePages( [ param.getId() ] ); |
| 206 |
|
| 207 |
this.touch(); |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
ve.ui.MWTemplateDialog.prototype.setApplicableStatus = function () { |
| 219 |
const canSave = !this.transclusionModel.isEmpty(); |
| 220 |
this.actions.setAbilities( { done: canSave && this.altered } ); |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
ve.ui.MWTemplateDialog.prototype.getBodyHeight = function () { |
| 227 |
return 400; |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
ve.ui.MWTemplateDialog.prototype.getPageFromPart = function ( part ) { |
| 238 |
if ( part instanceof ve.dm.MWTemplateModel ) { |
| 239 |
return new ve.ui.MWTemplatePage( part, part.getId(), { $overlay: this.$overlay, isReadOnly: this.isReadOnly() } ); |
| 240 |
} else if ( part instanceof ve.dm.MWTemplatePlaceholderModel ) { |
| 241 |
return new ve.ui.MWTemplatePlaceholderPage( |
| 242 |
part, |
| 243 |
part.getId(), |
| 244 |
{ $overlay: this.$overlay } |
| 245 |
); |
| 246 |
} |
| 247 |
return null; |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
ve.ui.MWTemplateDialog.prototype.getSelectedNode = function ( data ) { |
| 254 |
const selectedNode = ve.ui.MWTemplateDialog.super.prototype.getSelectedNode.call( this ); |
| 255 |
|
| 256 |
|
| 257 |
data = data || {}; |
| 258 |
|
| 259 |
|
| 260 |
if ( selectedNode && data.template && !selectedNode.isSingleTemplate( data.template ) ) { |
| 261 |
return null; |
| 262 |
} |
| 263 |
|
| 264 |
return selectedNode; |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
ve.ui.MWTemplateDialog.prototype.updateTitle = function () { |
| 273 |
let title = ve.msg( 'visualeditor-dialog-transclusion-loading' ); |
| 274 |
|
| 275 |
if ( this.transclusionModel.isSingleTemplate() ) { |
| 276 |
const part = this.transclusionModel.getParts()[ 0 ]; |
| 277 |
if ( part instanceof ve.dm.MWTemplateModel ) { |
| 278 |
title = ve.msg( |
| 279 |
this.getMode() === 'insert' ? |
| 280 |
'visualeditor-dialog-transclusion-title-insert-known-template' : |
| 281 |
'visualeditor-dialog-transclusion-title-edit-known-template', |
| 282 |
part.getSpec().getLabel() |
| 283 |
); |
| 284 |
} else { |
| 285 |
title = ve.msg( 'visualeditor-dialog-transclusion-title-insert-template' ); |
| 286 |
} |
| 287 |
} |
| 288 |
this.title.setLabel( title ); |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
ve.ui.MWTemplateDialog.prototype.initialize = function () { |
| 295 |
|
| 296 |
ve.ui.MWTemplateDialog.super.prototype.initialize.call( this ); |
| 297 |
|
| 298 |
|
| 299 |
this.bookletLayout = new ve.ui.MWTwoPaneTransclusionDialogLayout( this.constructor.static.bookletLayoutConfig ); |
| 300 |
|
| 301 |
this.sidebar = this.bookletLayout.sidebar; |
| 302 |
|
| 303 |
|
| 304 |
this.$content.addClass( 've-ui-mwTemplateDialog' ); |
| 305 |
|
| 306 |
}; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
ve.ui.MWTemplateDialog.prototype.checkRequiredParameters = function () { |
| 318 |
const blankRequired = [], |
| 319 |
deferred = ve.createDeferred(); |
| 320 |
|
| 321 |
this.bookletLayout.stackLayout.getItems().forEach( ( page ) => { |
| 322 |
if ( !( page instanceof ve.ui.MWParameterPage ) ) { |
| 323 |
return; |
| 324 |
} |
| 325 |
if ( page.parameter.isRequired() && !page.valueInput.getValue() ) { |
| 326 |
blankRequired.push( mw.msg( |
| 327 |
'quotation-marks', |
| 328 |
page.parameter.template.getSpec().getParameterLabel( page.parameter.getName() ) |
| 329 |
) ); |
| 330 |
} |
| 331 |
} ); |
| 332 |
if ( blankRequired.length ) { |
| 333 |
this.confirmDialogs.openWindow( 'requiredparamblankconfirm', { |
| 334 |
message: mw.msg( |
| 335 |
'visualeditor-dialog-transclusion-required-parameter-is-blank', |
| 336 |
mw.language.listToText( blankRequired ), |
| 337 |
blankRequired.length |
| 338 |
), |
| 339 |
title: mw.msg( |
| 340 |
'visualeditor-dialog-transclusion-required-parameter-dialog-title', |
| 341 |
blankRequired.length |
| 342 |
) |
| 343 |
} ).closed.then( ( data ) => { |
| 344 |
if ( data && data.action === 'ok' ) { |
| 345 |
deferred.resolve(); |
| 346 |
} else { |
| 347 |
deferred.reject(); |
| 348 |
} |
| 349 |
} ); |
| 350 |
} else { |
| 351 |
deferred.resolve(); |
| 352 |
} |
| 353 |
return deferred.promise(); |
| 354 |
}; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
ve.ui.MWTemplateDialog.prototype.getActionProcess = function ( action ) { |
| 360 |
if ( action === 'done' ) { |
| 361 |
return new OO.ui.Process( () => { |
| 362 |
const deferred = ve.createDeferred(); |
| 363 |
this.checkRequiredParameters().done( () => { |
| 364 |
const surfaceModel = this.getFragment().getSurface(), |
| 365 |
obj = this.transclusionModel.getPlainObject(); |
| 366 |
|
| 367 |
this.pushPending(); |
| 368 |
|
| 369 |
let modelPromise = ve.createDeferred().resolve().promise(); |
| 370 |
if ( this.selectedNode instanceof ve.dm.MWTransclusionNode ) { |
| 371 |
this.transclusionModel.updateTransclusionNode( surfaceModel, this.selectedNode ); |
| 372 |
|
| 373 |
} else if ( obj !== null ) { |
| 374 |
|
| 375 |
this.fragment = this.getFragment().collapseToEnd(); |
| 376 |
modelPromise = this.transclusionModel.insertTransclusionNode( this.getFragment() ); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
const templateEvent = { |
| 382 |
action: 'save', |
| 383 |
|
| 384 |
template_names: [] |
| 385 |
}; |
| 386 |
const editCountBucket = mw.config.get( 'wgUserEditCountBucket' ); |
| 387 |
if ( editCountBucket !== null ) { |
| 388 |
|
| 389 |
templateEvent.user_edit_count_bucket = editCountBucket; |
| 390 |
} |
| 391 |
const parts = this.transclusionModel.getParts(); |
| 392 |
for ( let i = 0; i < parts.length; i++ ) { |
| 393 |
|
| 394 |
const title = parts[ i ].getTitle && parts[ i ].getTitle(); |
| 395 |
if ( title ) { |
| 396 |
templateEvent.template_names.push( title ); |
| 397 |
} |
| 398 |
} |
| 399 |
mw.track( 'event.VisualEditorTemplateDialogUse', templateEvent ); |
| 400 |
|
| 401 |
return modelPromise.then( () => { |
| 402 |
this.close( { action: action } ).closed.always( this.popPending.bind( this ) ); |
| 403 |
} ); |
| 404 |
} ).always( deferred.resolve ); |
| 405 |
|
| 406 |
return deferred; |
| 407 |
} ); |
| 408 |
} |
| 409 |
|
| 410 |
return ve.ui.MWTemplateDialog.super.prototype.getActionProcess.call( this, action ); |
| 411 |
}; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
ve.ui.MWTemplateDialog.prototype.getSetupProcess = function ( data ) { |
| 417 |
data = data || {}; |
| 418 |
return ve.ui.MWTemplateDialog.super.prototype.getSetupProcess.call( this, data ) |
| 419 |
.next( () => { |
| 420 |
let promise; |
| 421 |
|
| 422 |
|
| 423 |
this.loaded = false; |
| 424 |
this.altered = false; |
| 425 |
this.transclusionModel = new ve.dm.MWTransclusionModel( this.getFragment().getDocument() ); |
| 426 |
|
| 427 |
|
| 428 |
this.transclusionModel.connect( this, { |
| 429 |
replace: 'onReplacePart', |
| 430 |
change: 'touch' |
| 431 |
} ); |
| 432 |
|
| 433 |
|
| 434 |
this.bookletLayout.$element.detach(); |
| 435 |
|
| 436 |
this.transclusionModel.connect( this.bookletLayout, { replace: 'onReplacePart' } ); |
| 437 |
|
| 438 |
|
| 439 |
if ( !this.selectedNode ) { |
| 440 |
if ( data.template ) { |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
const template = ve.dm.MWTemplateModel.newFromName( |
| 445 |
this.transclusionModel, data.template |
| 446 |
); |
| 447 |
promise = this.transclusionModel.addPart( template ); |
| 448 |
} else { |
| 449 |
|
| 450 |
const placeholderPage = new ve.dm.MWTemplatePlaceholderModel( this.transclusionModel ); |
| 451 |
promise = this.transclusionModel.addPart( placeholderPage ); |
| 452 |
promise.then( () => { |
| 453 |
this.bookletLayout.setPage( placeholderPage.getId() ); |
| 454 |
} ); |
| 455 |
this.canGoBack = true; |
| 456 |
} |
| 457 |
} else { |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
const templateEvent = { |
| 463 |
action: 'edit', |
| 464 |
|
| 465 |
template_names: [] |
| 466 |
}; |
| 467 |
const editCountBucket = mw.config.get( 'wgUserEditCountBucket' ); |
| 468 |
if ( editCountBucket !== null ) { |
| 469 |
|
| 470 |
templateEvent.user_edit_count_bucket = editCountBucket; |
| 471 |
} |
| 472 |
for ( let i = 0; i < this.selectedNode.partsList.length; i++ ) { |
| 473 |
if ( this.selectedNode.partsList[ i ].templatePage ) { |
| 474 |
templateEvent.template_names.push( this.selectedNode.partsList[ i ].templatePage ); |
| 475 |
} |
| 476 |
} |
| 477 |
mw.track( 'event.VisualEditorTemplateDialogUse', templateEvent ); |
| 478 |
|
| 479 |
promise = this.transclusionModel |
| 480 |
.load( ve.copy( this.selectedNode.getAttribute( 'mw' ) ) ) |
| 481 |
.then( this.initializeTemplateParameters.bind( this ) ); |
| 482 |
} |
| 483 |
this.actions.setAbilities( { done: false } ); |
| 484 |
|
| 485 |
return promise.then( () => { |
| 486 |
|
| 487 |
this.transclusionModel.addPromptedParameters(); |
| 488 |
|
| 489 |
this.$body.append( this.bookletLayout.$element ); |
| 490 |
this.$element.addClass( 've-ui-mwTemplateDialog-ready' ); |
| 491 |
this.loaded = true; |
| 492 |
} ); |
| 493 |
} ); |
| 494 |
}; |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
ve.ui.MWTemplateDialog.prototype.initializeTemplateParameters = function () {}; |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
ve.ui.MWTemplateDialog.prototype.onHasValueChange = function ( pageName, hasValue ) { |
| 507 |
this.sidebar.toggleHasValueByPageName( pageName, hasValue ); |
| 508 |
}; |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
ve.ui.MWTemplateDialog.prototype.getTeardownProcess = function ( data ) { |
| 514 |
return ve.ui.MWTemplateDialog.super.prototype.getTeardownProcess.call( this, data ) |
| 515 |
.first( () => { |
| 516 |
|
| 517 |
this.$element.removeClass( 've-ui-mwTemplateDialog-ready' ); |
| 518 |
this.transclusionModel.disconnect( this ); |
| 519 |
this.transclusionModel.abortAllApiRequests(); |
| 520 |
this.transclusionModel = null; |
| 521 |
this.bookletLayout.clearPages(); |
| 522 |
this.content = null; |
| 523 |
} ); |
| 524 |
}; |