| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
ve.ui.MWAddParameterPage = function VeUiMWAddParameterPage( parameter, name, config ) { |
| 16 |
|
| 17 |
ve.ui.MWAddParameterPage.super.call( this, name, ve.extendObject( { |
| 18 |
scrollable: false |
| 19 |
}, config ) ); |
| 20 |
|
| 21 |
this.template = parameter.getTemplate(); |
| 22 |
this.isExpanded = false; |
| 23 |
|
| 24 |
|
| 25 |
this.addParameterInputHeader = new OO.ui.ButtonWidget( { |
| 26 |
label: ve.msg( 'visualeditor-dialog-transclusion-add-undocumented-param' ), |
| 27 |
icon: 'add', |
| 28 |
framed: false, |
| 29 |
inline: true, |
| 30 |
classes: [ 've-ui-mwTransclusionDialog-addParameterFieldset-header' ] |
| 31 |
} ) |
| 32 |
.connect( this, { click: 'togglePlaceholder' } ); |
| 33 |
|
| 34 |
this.addParameterFieldset = new OO.ui.FieldsetLayout( { |
| 35 |
label: this.addParameterInputHeader.$element, |
| 36 |
classes: [ 've-ui-mwTransclusionDialog-addParameterFieldset' ] |
| 37 |
} ); |
| 38 |
|
| 39 |
|
| 40 |
this.togglePlaceholder( false ); |
| 41 |
|
| 42 |
|
| 43 |
this.$element |
| 44 |
.addClass( 've-ui-mwAddParameterPage' ) |
| 45 |
.append( this.addParameterFieldset.$element ); |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
OO.inheritClass( ve.ui.MWAddParameterPage, OO.ui.PageLayout ); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
ve.ui.MWAddParameterPage.prototype.initialize = function () { |
| 65 |
this.template |
| 66 |
.connect( this, { |
| 67 |
|
| 68 |
add: 'onTemplateParametersChanged', |
| 69 |
remove: 'onTemplateParametersChanged' |
| 70 |
} ); |
| 71 |
|
| 72 |
this.paramInputField = new OO.ui.TextInputWidget( { |
| 73 |
placeholder: ve.msg( 'visualeditor-dialog-transclusion-add-param-placeholder' ) |
| 74 |
} ) |
| 75 |
.connect( this, { |
| 76 |
change: 'updateParameterNameValidation', |
| 77 |
enter: 'onParameterNameSubmitted' |
| 78 |
} ); |
| 79 |
this.saveButton = new OO.ui.ButtonWidget( { |
| 80 |
label: ve.msg( 'visualeditor-dialog-transclusion-add-param-save' ), |
| 81 |
flags: [ 'progressive' ], |
| 82 |
disabled: true |
| 83 |
} ) |
| 84 |
.connect( this, { click: 'onParameterNameSubmitted' } ); |
| 85 |
|
| 86 |
this.actionFieldLayout = new OO.ui.ActionFieldLayout( |
| 87 |
this.paramInputField, |
| 88 |
this.saveButton, |
| 89 |
{ |
| 90 |
classes: [ 've-ui-mwTransclusionDialog-addParameterFieldset-input' ], |
| 91 |
align: 'top' |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
const link = this.template.getTitle() || this.template.getTarget().wt; |
| 96 |
const $helpText = mw.message( |
| 97 |
'visualeditor-dialog-transclusion-add-param-help', |
| 98 |
link |
| 99 |
).parseDom(); |
| 100 |
ve.init.platform.linkCache.styleElement( link, $helpText.filter( 'a:not(.external)' ) ); |
| 101 |
|
| 102 |
|
| 103 |
const helpWidget = new OO.ui.LabelWidget( { |
| 104 |
label: $helpText, |
| 105 |
classes: [ 'oo-ui-inline-help' ] |
| 106 |
} ); |
| 107 |
|
| 108 |
ve.targetLinksToNewWindow( helpWidget.$element[ 0 ] ); |
| 109 |
|
| 110 |
this.addParameterFieldset.$element.append( |
| 111 |
helpWidget.$element, |
| 112 |
this.addParameterFieldset.$group, |
| 113 |
this.actionFieldLayout.$element |
| 114 |
); |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
ve.ui.MWAddParameterPage.prototype.focus = function () { |
| 121 |
this.togglePlaceholder( true ); |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
ve.ui.MWAddParameterPage.prototype.onTemplateParametersChanged = function () { |
| 128 |
this.updateParameterNameValidation( this.paramInputField.getValue() ); |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
ve.ui.MWAddParameterPage.prototype.updateParameterNameValidation = function ( value ) { |
| 136 |
const paramName = value.trim(), |
| 137 |
errors = this.getValidationErrors( paramName ); |
| 138 |
|
| 139 |
this.actionFieldLayout.setErrors( errors ); |
| 140 |
this.saveButton.setDisabled( !paramName || errors.length ); |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
ve.ui.MWAddParameterPage.prototype.onParameterNameSubmitted = function () { |
| 148 |
const name = this.paramInputField.getValue().trim(); |
| 149 |
if ( !name || this.saveButton.isDisabled() ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
this.paramInputField.setValue( '' ); |
| 154 |
|
| 155 |
if ( this.template.hasParameter( name ) ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
const param = new ve.dm.MWParameterModel( this.template, name ); |
| 160 |
this.template.addParameter( param ); |
| 161 |
this.emit( 'templateParameterAdded', param.getId() ); |
| 162 |
|
| 163 |
ve.track( 'activity.transclusion', { |
| 164 |
action: 'add-unknown-parameter' |
| 165 |
} ); |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
ve.ui.MWAddParameterPage.prototype.getValidationErrors = function ( name ) { |
| 174 |
if ( !name ) { |
| 175 |
return []; |
| 176 |
} |
| 177 |
|
| 178 |
const forbiddenCharacter = name.match( /[={|}]/ ); |
| 179 |
if ( forbiddenCharacter ) { |
| 180 |
return [ mw.message( 'visualeditor-dialog-transclusion-add-param-error-forbidden-char', |
| 181 |
forbiddenCharacter[ 0 ] ).parseDom() ]; |
| 182 |
} |
| 183 |
|
| 184 |
const spec = this.template.getSpec(); |
| 185 |
|
| 186 |
let key; |
| 187 |
if ( spec.getParameterAliases( name ).indexOf( name ) !== -1 ) { |
| 188 |
key = 'visualeditor-dialog-transclusion-add-param-error-alias'; |
| 189 |
} else if ( this.template.hasParameter( name ) ) { |
| 190 |
key = 'visualeditor-dialog-transclusion-add-param-error-exists-selected'; |
| 191 |
} else if ( spec.isParameterDeprecated( name ) ) { |
| 192 |
key = 'visualeditor-dialog-transclusion-add-param-error-deprecated'; |
| 193 |
} else if ( spec.isKnownParameterOrAlias( name ) ) { |
| 194 |
key = 'visualeditor-dialog-transclusion-add-param-error-exists-unselected'; |
| 195 |
} |
| 196 |
|
| 197 |
if ( !key ) { |
| 198 |
return []; |
| 199 |
} |
| 200 |
|
| 201 |
const label = spec.getParameterLabel( this.template.getOriginalParameterName( name ) ), |
| 202 |
|
| 203 |
$msg = mw.message( key, name, label ).parseDom(); |
| 204 |
ve.targetLinksToNewWindow( $( '<div>' ).append( $msg )[ 0 ] ); |
| 205 |
return [ $msg ]; |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
ve.ui.MWAddParameterPage.prototype.togglePlaceholder = function ( expand ) { |
| 213 |
this.isExpanded = expand === undefined ? !this.isExpanded : !!expand; |
| 214 |
|
| 215 |
this.addParameterInputHeader.setIcon( this.isExpanded ? 'subtract' : 'add' ); |
| 216 |
this.addParameterFieldset.$element.toggleClass( |
| 217 |
've-ui-mwTransclusionDialog-addParameterFieldset-collapsed', |
| 218 |
!this.isExpanded |
| 219 |
); |
| 220 |
if ( this.isExpanded ) { |
| 221 |
if ( !this.paramInputField ) { |
| 222 |
this.initialize(); |
| 223 |
} |
| 224 |
this.paramInputField.focus(); |
| 225 |
} |
| 226 |
}; |