| 1 |
( function () { |
| 2 |
'use strict'; |
| 3 |
let ApiSandbox = null, Util = null, |
| 4 |
windowManager, |
| 5 |
formatDropdown, |
| 6 |
resultPage = null, |
| 7 |
suppressErrors = true, |
| 8 |
updatingBooklet = false, |
| 9 |
baseRequestParams = {}, |
| 10 |
pages = {}; |
| 11 |
const api = new mw.Api(), |
| 12 |
bookletPages = [], |
| 13 |
availableFormats = {}, |
| 14 |
moduleInfoCache = {}, |
| 15 |
OptionalParamWidget = require( './OptionalParamWidget.js' ), |
| 16 |
ParamLabelWidget = require( './ParamLabelWidget.js' ), |
| 17 |
BooleanToggleSwitchParamWidget = require( './BooleanToggleSwitchParamWidget.js' ), |
| 18 |
DateTimeParamWidget = require( './DateTimeParamWidget.js' ), |
| 19 |
LimitParamWidget = require( './LimitParamWidget.js' ), |
| 20 |
PasswordParamWidget = require( './PasswordParamWidget.js' ), |
| 21 |
UploadSelectFileParamWidget = require( './UploadSelectFileParamWidget.js' ); |
| 22 |
|
| 23 |
const WidgetMethods = { |
| 24 |
textInputWidget: { |
| 25 |
getApiValue: function () { |
| 26 |
return this.getValue(); |
| 27 |
}, |
| 28 |
setApiValue: function ( v ) { |
| 29 |
if ( v === undefined ) { |
| 30 |
v = this.paramInfo.default; |
| 31 |
} |
| 32 |
this.setValue( v ); |
| 33 |
}, |
| 34 |
apiCheckValid: function ( shouldSuppressErrors ) { |
| 35 |
const widget = this; |
| 36 |
return this.getValidity().then( () => $.Deferred().resolve( true ).promise(), () => $.Deferred().resolve( false ).promise() ).done( ( ok ) => { |
| 37 |
ok = ok || shouldSuppressErrors; |
| 38 |
widget.setIcon( ok ? null : 'alert' ); |
| 39 |
widget.setTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() ); |
| 40 |
} ); |
| 41 |
} |
| 42 |
}, |
| 43 |
|
| 44 |
tokenWidget: { |
| 45 |
alertTokenError: function ( code, error ) { |
| 46 |
windowManager.openWindow( 'errorAlert', { |
| 47 |
title: Util.parseMsg( 'apisandbox-results-fixtoken-fail', this.paramInfo.tokentype ), |
| 48 |
message: error, |
| 49 |
actions: [ |
| 50 |
{ |
| 51 |
action: 'accept', |
| 52 |
label: OO.ui.msg( 'ooui-dialog-process-dismiss' ), |
| 53 |
flags: 'primary' |
| 54 |
} |
| 55 |
] |
| 56 |
} ); |
| 57 |
}, |
| 58 |
fetchToken: function () { |
| 59 |
this.pushPending(); |
| 60 |
return api.getToken( this.paramInfo.tokentype ) |
| 61 |
.done( this.setApiValue.bind( this ) ) |
| 62 |
.fail( this.alertTokenError.bind( this ) ) |
| 63 |
.always( this.popPending.bind( this ) ); |
| 64 |
}, |
| 65 |
setApiValue: function ( v ) { |
| 66 |
if ( v === undefined ) { |
| 67 |
v = this.paramInfo.default; |
| 68 |
} |
| 69 |
this.setValue( v ); |
| 70 |
if ( v === '123ABC' ) { |
| 71 |
this.fetchToken(); |
| 72 |
} |
| 73 |
} |
| 74 |
}, |
| 75 |
|
| 76 |
dropdownWidget: { |
| 77 |
getApiValue: function () { |
| 78 |
const selected = this.getMenu().findFirstSelectedItem(); |
| 79 |
return selected ? selected.getData() : undefined; |
| 80 |
}, |
| 81 |
setApiValue: function ( v ) { |
| 82 |
if ( v === undefined ) { |
| 83 |
v = this.paramInfo.default; |
| 84 |
} |
| 85 |
const menu = this.getMenu(); |
| 86 |
if ( v === undefined ) { |
| 87 |
menu.selectItem(); |
| 88 |
} else { |
| 89 |
menu.selectItemByData( String( v ) ); |
| 90 |
} |
| 91 |
}, |
| 92 |
apiCheckValid: function ( shouldSuppressErrors ) { |
| 93 |
const ok = this.getApiValue() !== undefined || shouldSuppressErrors; |
| 94 |
this.setIcon( ok ? null : 'alert' ); |
| 95 |
this.setTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() ); |
| 96 |
return $.Deferred().resolve( ok ).promise(); |
| 97 |
} |
| 98 |
}, |
| 99 |
|
| 100 |
tagWidget: { |
| 101 |
parseApiValue: function ( v ) { |
| 102 |
if ( v === undefined || v === '' || v === '\x1f' ) { |
| 103 |
return []; |
| 104 |
} else { |
| 105 |
v = String( v ); |
| 106 |
if ( v[ 0 ] !== '\x1f' ) { |
| 107 |
return v.split( '|' ); |
| 108 |
} else { |
| 109 |
return v.slice( 1 ).split( '\x1f' ); |
| 110 |
} |
| 111 |
} |
| 112 |
}, |
| 113 |
getApiValueForTemplates: function () { |
| 114 |
return this.isDisabled() ? this.parseApiValue( this.paramInfo.default ) : this.getValue(); |
| 115 |
}, |
| 116 |
getApiValue: function () { |
| 117 |
const items = this.getValue(); |
| 118 |
if ( items.join( '' ).indexOf( '|' ) === -1 ) { |
| 119 |
return items.join( '|' ); |
| 120 |
} else { |
| 121 |
return '\x1f' + items.join( '\x1f' ); |
| 122 |
} |
| 123 |
}, |
| 124 |
setApiValue: function ( v ) { |
| 125 |
if ( v === undefined ) { |
| 126 |
v = this.paramInfo.default; |
| 127 |
} |
| 128 |
this.setValue( this.parseApiValue( v ) ); |
| 129 |
}, |
| 130 |
apiCheckValid: function ( shouldSuppressErrors ) { |
| 131 |
let ok = true; |
| 132 |
if ( !shouldSuppressErrors ) { |
| 133 |
const pi = this.paramInfo; |
| 134 |
ok = this.getApiValue() !== undefined && !( |
| 135 |
pi.allspecifier !== undefined && |
| 136 |
this.getValue().length > 1 && |
| 137 |
this.getValue().indexOf( pi.allspecifier ) !== -1 |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
this.setIcon( ok ? null : 'alert' ); |
| 142 |
this.setTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() ); |
| 143 |
return $.Deferred().resolve( ok ).promise(); |
| 144 |
}, |
| 145 |
createTagItemWidget: function ( data, label ) { |
| 146 |
const item = OO.ui.TagMultiselectWidget.prototype.createTagItemWidget.call( this, data, label ); |
| 147 |
if ( this.paramInfo.deprecatedvalues && |
| 148 |
this.paramInfo.deprecatedvalues.indexOf( data ) >= 0 |
| 149 |
) { |
| 150 |
item.$element.addClass( 'mw-apisandbox-deprecated-value' ); |
| 151 |
} |
| 152 |
if ( this.paramInfo.internalvalues && |
| 153 |
this.paramInfo.internalvalues.indexOf( data ) >= 0 |
| 154 |
) { |
| 155 |
item.$element.addClass( 'mw-apisandbox-internal-value' ); |
| 156 |
} |
| 157 |
return item; |
| 158 |
} |
| 159 |
}, |
| 160 |
|
| 161 |
submoduleWidget: { |
| 162 |
single: function () { |
| 163 |
const v = this.isDisabled() ? this.paramInfo.default : this.getApiValue(); |
| 164 |
return v === undefined ? [] : [ { value: v, path: this.paramInfo.submodules[ v ] } ]; |
| 165 |
}, |
| 166 |
multi: function () { |
| 167 |
const map = this.paramInfo.submodules, |
| 168 |
v = this.isDisabled() ? this.paramInfo.default : this.getApiValue(); |
| 169 |
return v === undefined || v === '' ? [] : String( v ).split( '|' ).map( ( val ) => ( { value: val, path: map[ val ] } ) ); |
| 170 |
} |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
const Validators = { |
| 175 |
generic: function () { |
| 176 |
return !Util.apiBool( this.paramInfo.required ) || this.getApiValue() !== ''; |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
Util = { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
fetchModuleInfo: function ( module ) { |
| 192 |
const deferred = $.Deferred(); |
| 193 |
|
| 194 |
if ( Object.prototype.hasOwnProperty.call( moduleInfoCache, module ) ) { |
| 195 |
return deferred |
| 196 |
.resolve( moduleInfoCache[ module ] ) |
| 197 |
.promise( { abort: function () {} } ); |
| 198 |
} else { |
| 199 |
const apiPromise = api.post( { |
| 200 |
action: 'paraminfo', |
| 201 |
modules: module, |
| 202 |
helpformat: 'html', |
| 203 |
uselang: mw.config.get( 'wgUserLanguage' ) |
| 204 |
} ).done( ( data ) => { |
| 205 |
if ( data.warnings && data.warnings.paraminfo ) { |
| 206 |
deferred.reject( '???', data.warnings.paraminfo[ '*' ] ); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
const info = data.paraminfo.modules; |
| 211 |
if ( !info || info.length !== 1 || info[ 0 ].path !== module ) { |
| 212 |
deferred.reject( '???', 'No module data returned' ); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
moduleInfoCache[ module ] = info[ 0 ]; |
| 217 |
deferred.resolve( info[ 0 ] ); |
| 218 |
} ).fail( ( code, details ) => { |
| 219 |
if ( code === 'http' ) { |
| 220 |
details = 'HTTP error: ' + details.exception; |
| 221 |
} else if ( details.error ) { |
| 222 |
details = details.error.info; |
| 223 |
} |
| 224 |
deferred.reject( code, details ); |
| 225 |
} ); |
| 226 |
return deferred |
| 227 |
.promise( { abort: apiPromise.abort } ); |
| 228 |
} |
| 229 |
}, |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
markTokensBad: function () { |
| 235 |
const checkPages = [ pages.main ]; |
| 236 |
|
| 237 |
while ( checkPages.length ) { |
| 238 |
const page = checkPages.shift(); |
| 239 |
|
| 240 |
if ( page.tokenWidget ) { |
| 241 |
api.badToken( page.tokenWidget.paramInfo.tokentype ); |
| 242 |
} |
| 243 |
|
| 244 |
const subpages = page.getSubpages(); |
| 245 |
|
| 246 |
subpages.forEach( ( subpage ) => { |
| 247 |
if ( Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { |
| 248 |
checkPages.push( pages[ subpage.key ] ); |
| 249 |
} |
| 250 |
} ); |
| 251 |
} |
| 252 |
}, |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
apiBool: function ( value ) { |
| 261 |
return value !== undefined && value !== false; |
| 262 |
}, |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
createWidgetForParameter: function ( pi, opts ) { |
| 272 |
let multiModeButton = null, |
| 273 |
multiModeInput = null, |
| 274 |
multiModeAllowed = false; |
| 275 |
|
| 276 |
opts = opts || {}; |
| 277 |
|
| 278 |
let widget, items; |
| 279 |
switch ( pi.type ) { |
| 280 |
case 'boolean': |
| 281 |
widget = new BooleanToggleSwitchParamWidget(); |
| 282 |
widget.paramInfo = pi; |
| 283 |
pi.required = true; |
| 284 |
break; |
| 285 |
|
| 286 |
case 'string': |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
if ( pi.tokentype ) { |
| 296 |
|
| 297 |
|
| 298 |
widget = new OO.ui.TextInputWidget( { |
| 299 |
required: Util.apiBool( pi.required ) |
| 300 |
} ); |
| 301 |
widget.paramInfo = pi; |
| 302 |
Object.assign( widget, WidgetMethods.textInputWidget ); |
| 303 |
widget.setValidation( Validators.generic ); |
| 304 |
Object.assign( widget, WidgetMethods.tokenWidget ); |
| 305 |
break; |
| 306 |
} |
| 307 |
|
| 308 |
case 'user': |
| 309 |
case 'expiry': |
| 310 |
if ( Util.apiBool( pi.multi ) ) { |
| 311 |
widget = new OO.ui.TagMultiselectWidget( { |
| 312 |
allowArbitrary: true, |
| 313 |
allowDuplicates: Util.apiBool( pi.allowsduplicates ), |
| 314 |
$overlay: true |
| 315 |
} ); |
| 316 |
widget.paramInfo = pi; |
| 317 |
Object.assign( widget, WidgetMethods.tagWidget ); |
| 318 |
} else { |
| 319 |
widget = new OO.ui.TextInputWidget( { |
| 320 |
required: Util.apiBool( pi.required ) |
| 321 |
} ); |
| 322 |
widget.paramInfo = pi; |
| 323 |
Object.assign( widget, WidgetMethods.textInputWidget ); |
| 324 |
widget.setValidation( Validators.generic ); |
| 325 |
} |
| 326 |
break; |
| 327 |
|
| 328 |
case 'raw': |
| 329 |
case 'text': |
| 330 |
widget = new OO.ui.MultilineTextInputWidget( { |
| 331 |
required: Util.apiBool( pi.required ) |
| 332 |
} ); |
| 333 |
widget.paramInfo = pi; |
| 334 |
Object.assign( widget, WidgetMethods.textInputWidget ); |
| 335 |
widget.setValidation( Validators.generic ); |
| 336 |
break; |
| 337 |
|
| 338 |
case 'password': |
| 339 |
widget = new PasswordParamWidget( { |
| 340 |
required: Util.apiBool( pi.required ) |
| 341 |
} ); |
| 342 |
widget.paramInfo = pi; |
| 343 |
widget.setValidation( Validators.generic ); |
| 344 |
multiModeAllowed = true; |
| 345 |
multiModeInput = widget; |
| 346 |
break; |
| 347 |
|
| 348 |
case 'integer': |
| 349 |
widget = new OO.ui.NumberInputWidget( { |
| 350 |
step: 1, |
| 351 |
min: pi.min || -Infinity, |
| 352 |
max: pi.max || Infinity, |
| 353 |
required: Util.apiBool( pi.required ) |
| 354 |
} ); |
| 355 |
widget.paramInfo = pi; |
| 356 |
Object.assign( widget, WidgetMethods.textInputWidget ); |
| 357 |
multiModeAllowed = true; |
| 358 |
multiModeInput = widget; |
| 359 |
break; |
| 360 |
|
| 361 |
case 'limit': |
| 362 |
widget = new LimitParamWidget( { |
| 363 |
required: Util.apiBool( pi.required ) |
| 364 |
} ); |
| 365 |
pi.min = pi.min || 0; |
| 366 |
pi.apiSandboxMax = ( mw.config.get( 'apihighlimits' ) ? pi.highmax : pi.max ) || pi.max; |
| 367 |
widget.paramInfo = pi; |
| 368 |
multiModeAllowed = true; |
| 369 |
multiModeInput = widget; |
| 370 |
break; |
| 371 |
|
| 372 |
case 'timestamp': |
| 373 |
widget = new DateTimeParamWidget( { |
| 374 |
required: Util.apiBool( pi.required ) |
| 375 |
} ); |
| 376 |
widget.paramInfo = pi; |
| 377 |
multiModeAllowed = true; |
| 378 |
break; |
| 379 |
|
| 380 |
case 'upload': |
| 381 |
widget = new UploadSelectFileParamWidget(); |
| 382 |
widget.paramInfo = pi; |
| 383 |
break; |
| 384 |
|
| 385 |
case 'namespace': |
| 386 |
|
| 387 |
items = $.map( mw.config.get( 'wgFormattedNamespaces' ), ( name, ns ) => { |
| 388 |
if ( ns === '0' ) { |
| 389 |
name = mw.msg( 'blanknamespace' ); |
| 390 |
} |
| 391 |
return new OO.ui.MenuOptionWidget( { data: ns, label: name } ); |
| 392 |
} ).sort( ( a, b ) => a.data - b.data ); |
| 393 |
if ( Util.apiBool( pi.multi ) ) { |
| 394 |
if ( pi.allspecifier !== undefined ) { |
| 395 |
items.unshift( new OO.ui.MenuOptionWidget( { |
| 396 |
data: pi.allspecifier, |
| 397 |
label: mw.msg( 'apisandbox-multivalue-all-namespaces', pi.allspecifier ) |
| 398 |
} ) ); |
| 399 |
} |
| 400 |
|
| 401 |
widget = new OO.ui.MenuTagMultiselectWidget( { |
| 402 |
menu: { items: items }, |
| 403 |
$overlay: true |
| 404 |
} ); |
| 405 |
widget.paramInfo = pi; |
| 406 |
Object.assign( widget, WidgetMethods.tagWidget ); |
| 407 |
} else { |
| 408 |
widget = new OO.ui.DropdownWidget( { |
| 409 |
menu: { items: items }, |
| 410 |
$overlay: true |
| 411 |
} ); |
| 412 |
widget.paramInfo = pi; |
| 413 |
Object.assign( widget, WidgetMethods.dropdownWidget ); |
| 414 |
} |
| 415 |
break; |
| 416 |
|
| 417 |
case 'title': |
| 418 |
if ( Util.apiBool( pi.multi ) ) { |
| 419 |
widget = new mw.widgets.TitlesMultiselectWidget( { |
| 420 |
required: Util.apiBool( pi.required ), |
| 421 |
validateTitle: true, |
| 422 |
suggestions: true, |
| 423 |
showMissing: !Util.apiBool( pi.mustExist ), |
| 424 |
addQueryInput: !Util.apiBool( pi.mustExist ), |
| 425 |
tagLimit: pi.limit || undefined |
| 426 |
} ); |
| 427 |
widget.paramInfo = pi; |
| 428 |
Object.assign( widget, WidgetMethods.tagWidget ); |
| 429 |
} else { |
| 430 |
widget = new mw.widgets.TitleInputWidget( { |
| 431 |
required: Util.apiBool( pi.required ), |
| 432 |
validateTitle: true, |
| 433 |
suggestions: true, |
| 434 |
autocomplete: true, |
| 435 |
showMissing: !Util.apiBool( pi.mustExist ), |
| 436 |
addQueryInput: !Util.apiBool( pi.mustExist ) |
| 437 |
} ); |
| 438 |
widget.paramInfo = pi; |
| 439 |
Object.assign( widget, WidgetMethods.textInputWidget ); |
| 440 |
} |
| 441 |
break; |
| 442 |
|
| 443 |
default: |
| 444 |
if ( !Array.isArray( pi.type ) ) { |
| 445 |
throw new Error( 'Unknown parameter type ' + pi.type ); |
| 446 |
} |
| 447 |
|
| 448 |
items = pi.type.map( ( v ) => { |
| 449 |
const optionWidget = new OO.ui.MenuOptionWidget( { |
| 450 |
data: String( v ), |
| 451 |
label: String( v ) |
| 452 |
} ); |
| 453 |
if ( pi.deprecatedvalues && pi.deprecatedvalues.indexOf( v ) >= 0 ) { |
| 454 |
optionWidget.$element.addClass( 'mw-apisandbox-deprecated-value' ); |
| 455 |
optionWidget.$label.before( |
| 456 |
$( '<span>' ).addClass( 'mw-apisandbox-flag' ).text( mw.msg( 'api-help-param-deprecated-label' ) ) |
| 457 |
); |
| 458 |
} |
| 459 |
if ( pi.internalvalues && pi.internalvalues.indexOf( v ) >= 0 ) { |
| 460 |
optionWidget.$element.addClass( 'mw-apisandbox-internal-value' ); |
| 461 |
optionWidget.$label.before( |
| 462 |
$( '<span>' ).addClass( 'mw-apisandbox-flag' ).text( mw.msg( 'api-help-param-internal-label' ) ) |
| 463 |
); |
| 464 |
} |
| 465 |
return optionWidget; |
| 466 |
} ).sort( ( a, b ) => a.label < b.label ? -1 : ( a.label > b.label ? 1 : 0 ) ); |
| 467 |
if ( Util.apiBool( pi.multi ) ) { |
| 468 |
if ( pi.allspecifier !== undefined ) { |
| 469 |
items.unshift( new OO.ui.MenuOptionWidget( { |
| 470 |
data: pi.allspecifier, |
| 471 |
label: mw.msg( 'apisandbox-multivalue-all-values', pi.allspecifier ) |
| 472 |
} ) ); |
| 473 |
} |
| 474 |
|
| 475 |
widget = new OO.ui.MenuTagMultiselectWidget( { |
| 476 |
menu: { items: items }, |
| 477 |
$overlay: true |
| 478 |
} ); |
| 479 |
widget.paramInfo = pi; |
| 480 |
Object.assign( widget, WidgetMethods.tagWidget ); |
| 481 |
if ( Util.apiBool( pi.submodules ) ) { |
| 482 |
widget.getSubmodules = WidgetMethods.submoduleWidget.multi; |
| 483 |
widget.on( 'change', ApiSandbox.updateUI ); |
| 484 |
} |
| 485 |
} else { |
| 486 |
widget = new OO.ui.DropdownWidget( { |
| 487 |
menu: { items: items }, |
| 488 |
$overlay: true |
| 489 |
} ); |
| 490 |
widget.paramInfo = pi; |
| 491 |
Object.assign( widget, WidgetMethods.dropdownWidget ); |
| 492 |
if ( Util.apiBool( pi.submodules ) ) { |
| 493 |
widget.getSubmodules = WidgetMethods.submoduleWidget.single; |
| 494 |
widget.getMenu().on( 'select', ApiSandbox.updateUI ); |
| 495 |
} |
| 496 |
if ( pi.deprecatedvalues ) { |
| 497 |
widget.getMenu().on( 'select', function ( item ) { |
| 498 |
this.$element.toggleClass( |
| 499 |
'mw-apisandbox-deprecated-value', |
| 500 |
pi.deprecatedvalues.indexOf( item.data ) >= 0 |
| 501 |
); |
| 502 |
}, [], widget ); |
| 503 |
} |
| 504 |
if ( pi.internalvalues ) { |
| 505 |
widget.getMenu().on( 'select', function ( item ) { |
| 506 |
this.$element.toggleClass( |
| 507 |
'mw-apisandbox-internal-value', |
| 508 |
pi.internalvalues.indexOf( item.data ) >= 0 |
| 509 |
); |
| 510 |
}, [], widget ); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
break; |
| 515 |
} |
| 516 |
|
| 517 |
if ( Util.apiBool( pi.multi ) && multiModeAllowed ) { |
| 518 |
const innerWidget = widget; |
| 519 |
|
| 520 |
multiModeButton = new OO.ui.ButtonWidget( { |
| 521 |
label: mw.msg( 'apisandbox-add-multi' ) |
| 522 |
} ); |
| 523 |
const $content = innerWidget.$element.add( multiModeButton.$element ); |
| 524 |
|
| 525 |
widget = new OO.ui.PopupTagMultiselectWidget( { |
| 526 |
allowArbitrary: true, |
| 527 |
allowDuplicates: Util.apiBool( pi.allowsduplicates ), |
| 528 |
$overlay: true, |
| 529 |
popup: { |
| 530 |
classes: [ 'mw-apisandbox-popup' ], |
| 531 |
padded: true, |
| 532 |
$content: $content |
| 533 |
} |
| 534 |
} ); |
| 535 |
widget.paramInfo = pi; |
| 536 |
Object.assign( widget, WidgetMethods.tagWidget ); |
| 537 |
|
| 538 |
const func = function () { |
| 539 |
if ( !innerWidget.isDisabled() ) { |
| 540 |
innerWidget.apiCheckValid( suppressErrors ).done( ( ok ) => { |
| 541 |
if ( ok ) { |
| 542 |
widget.addTag( innerWidget.getApiValue() ); |
| 543 |
innerWidget.setApiValue( undefined ); |
| 544 |
} |
| 545 |
} ); |
| 546 |
return false; |
| 547 |
} |
| 548 |
}; |
| 549 |
|
| 550 |
if ( multiModeInput ) { |
| 551 |
multiModeInput.on( 'enter', func ); |
| 552 |
} |
| 553 |
multiModeButton.on( 'click', func ); |
| 554 |
} |
| 555 |
|
| 556 |
let finalWidget; |
| 557 |
if ( Util.apiBool( pi.required ) || opts.nooptional ) { |
| 558 |
finalWidget = widget; |
| 559 |
} else { |
| 560 |
finalWidget = new OptionalParamWidget( widget ); |
| 561 |
finalWidget.paramInfo = pi; |
| 562 |
if ( widget.getSubmodules ) { |
| 563 |
finalWidget.getSubmodules = widget.getSubmodules.bind( widget ); |
| 564 |
finalWidget.on( 'disable', () => { |
| 565 |
setTimeout( ApiSandbox.updateUI ); |
| 566 |
} ); |
| 567 |
} |
| 568 |
if ( widget.getApiValueForTemplates ) { |
| 569 |
finalWidget.getApiValueForTemplates = widget.getApiValueForTemplates.bind( widget ); |
| 570 |
} |
| 571 |
finalWidget.setDisabled( true ); |
| 572 |
} |
| 573 |
|
| 574 |
widget.setApiValue( pi.default ); |
| 575 |
|
| 576 |
return finalWidget; |
| 577 |
}, |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
parseHTML: function ( html ) { |
| 586 |
const $ret = $( $.parseHTML( html ) ); |
| 587 |
return Util.fixupHTML( $ret ); |
| 588 |
}, |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
parseMsg: function ( key, ...parameters ) { |
| 598 |
|
| 599 |
const $ret = mw.message( key, ...parameters ).parseDom(); |
| 600 |
return Util.fixupHTML( $ret ); |
| 601 |
}, |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
fixupHTML: function ( $html ) { |
| 613 |
$html.filter( 'a' ).add( $html.find( 'a' ) ) |
| 614 |
.filter( '[href]:not([target])' ) |
| 615 |
.attr( 'target', '_blank' ); |
| 616 |
return $html; |
| 617 |
}, |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
formatRequest: function ( displayParams, rawParams, method, ajaxOptions ) { |
| 630 |
let jsonLayout, phpLayout; |
| 631 |
const apiUrl = new URL( mw.util.wikiScript( 'api' ), location.origin ).toString(); |
| 632 |
const items = [ |
| 633 |
new OO.ui.MenuOptionWidget( { |
| 634 |
label: Util.parseMsg( 'apisandbox-request-format-url-label' ), |
| 635 |
data: new mw.widgets.CopyTextLayout( { |
| 636 |
label: Util.parseMsg( 'apisandbox-request-url-label' ), |
| 637 |
copyText: apiUrl + '?' + $.param( displayParams ) |
| 638 |
} ) |
| 639 |
} ), |
| 640 |
new OO.ui.MenuOptionWidget( { |
| 641 |
label: Util.parseMsg( 'apisandbox-request-format-json-label' ), |
| 642 |
data: jsonLayout = new mw.widgets.CopyTextLayout( { |
| 643 |
label: Util.parseMsg( 'apisandbox-request-json-label' ), |
| 644 |
copyText: JSON.stringify( displayParams, null, '\t' ), |
| 645 |
multiline: true, |
| 646 |
textInput: { |
| 647 |
classes: [ 'mw-apisandbox-textInputCode' ], |
| 648 |
autosize: true, |
| 649 |
maxRows: 6 |
| 650 |
} |
| 651 |
} ).on( 'toggle', ( visible ) => { |
| 652 |
if ( visible ) { |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
jsonLayout.textInput.updatePosition(); |
| 657 |
} |
| 658 |
} ) |
| 659 |
} ), |
| 660 |
new OO.ui.MenuOptionWidget( { |
| 661 |
label: Util.parseMsg( 'apisandbox-request-format-php-label' ), |
| 662 |
data: phpLayout = new mw.widgets.CopyTextLayout( { |
| 663 |
label: Util.parseMsg( 'apisandbox-request-php-label' ), |
| 664 |
copyText: '[\n' + |
| 665 |
Object.keys( displayParams ).map( |
| 666 |
|
| 667 |
( param ) => '\t' + |
| 668 |
JSON.stringify( param ) + |
| 669 |
' => ' + |
| 670 |
JSON.stringify( displayParams[ param ] ).replace( /\$/g, '\\$' ) |
| 671 |
).join( ',\n' ) + |
| 672 |
'\n]', |
| 673 |
multiline: true, |
| 674 |
textInput: { |
| 675 |
classes: [ 'mw-apisandbox-textInputCode' ], |
| 676 |
autosize: true, |
| 677 |
maxRows: 6 |
| 678 |
} |
| 679 |
} ).on( 'toggle', ( visible ) => { |
| 680 |
if ( visible ) { |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
phpLayout.textInput.updatePosition(); |
| 685 |
} |
| 686 |
} ) |
| 687 |
} ) |
| 688 |
]; |
| 689 |
|
| 690 |
mw.hook( 'apisandbox.formatRequest' ).fire( items, displayParams, rawParams, method, ajaxOptions ); |
| 691 |
|
| 692 |
return items; |
| 693 |
}, |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
onFormatDropdownChange: function () { |
| 699 |
const menu = formatDropdown.getMenu(), |
| 700 |
selected = menu.findFirstSelectedItem(), |
| 701 |
selectedField = selected ? selected.getData() : null; |
| 702 |
|
| 703 |
menu.getItems().forEach( ( item ) => { |
| 704 |
item.getData().toggle( item.getData() === selectedField ); |
| 705 |
} ); |
| 706 |
} |
| 707 |
}; |
| 708 |
|
| 709 |
let booklet, panel, oldhash; |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
ApiSandbox = { |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
init: function () { |
| 723 |
windowManager = new OO.ui.WindowManager(); |
| 724 |
$( OO.ui.getTeleportTarget() ).append( windowManager.$element ); |
| 725 |
windowManager.addWindows( { |
| 726 |
errorAlert: new OO.ui.MessageDialog() |
| 727 |
} ); |
| 728 |
|
| 729 |
const $toolbar = $( '<div>' ) |
| 730 |
.addClass( 'mw-apisandbox-toolbar' ) |
| 731 |
.append( |
| 732 |
new OO.ui.ButtonWidget( { |
| 733 |
label: mw.msg( 'apisandbox-submit' ), |
| 734 |
flags: [ 'primary', 'progressive' ] |
| 735 |
} ).on( 'click', ApiSandbox.sendRequest ).$element, |
| 736 |
new OO.ui.ButtonWidget( { |
| 737 |
label: mw.msg( 'apisandbox-reset' ), |
| 738 |
flags: 'destructive' |
| 739 |
} ).on( 'click', ApiSandbox.resetUI ).$element |
| 740 |
); |
| 741 |
|
| 742 |
booklet = new OO.ui.BookletLayout( { |
| 743 |
expanded: false, |
| 744 |
outlined: true, |
| 745 |
autoFocus: false |
| 746 |
} ); |
| 747 |
|
| 748 |
panel = new OO.ui.PanelLayout( { |
| 749 |
classes: [ 'mw-apisandbox-container' ], |
| 750 |
content: [ booklet ], |
| 751 |
expanded: false |
| 752 |
} ); |
| 753 |
|
| 754 |
pages.main = new ApiSandbox.PageLayout( { key: 'main', path: 'main' } ); |
| 755 |
|
| 756 |
|
| 757 |
if ( !ApiSandbox.loadFromHash() ) { |
| 758 |
ApiSandbox.updateUI(); |
| 759 |
} |
| 760 |
|
| 761 |
$( window ).on( 'hashchange', ApiSandbox.loadFromHash ); |
| 762 |
|
| 763 |
$( '#mw-apisandbox' ) |
| 764 |
.empty() |
| 765 |
.append( $( '<p>' ).append( Util.parseMsg( 'apisandbox-intro' ) ) ) |
| 766 |
.append( |
| 767 |
$( '<div>' ).attr( 'id', 'mw-apisandbox-ui' ) |
| 768 |
.append( $toolbar ) |
| 769 |
.append( panel.$element ) |
| 770 |
); |
| 771 |
}, |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
loadFromHash: function () { |
| 779 |
let fragment = location.hash; |
| 780 |
|
| 781 |
if ( oldhash === fragment ) { |
| 782 |
return false; |
| 783 |
} |
| 784 |
oldhash = fragment; |
| 785 |
if ( fragment === '' ) { |
| 786 |
return false; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
const params = {}; |
| 791 |
fragment = fragment.replace( /\+/g, '%20' ); |
| 792 |
const pattern = /([^&=#]+)=?([^&#]*)/g; |
| 793 |
let match; |
| 794 |
while ( ( match = pattern.exec( fragment ) ) ) { |
| 795 |
params[ decodeURIComponent( match[ 1 ] ) ] = decodeURIComponent( match[ 2 ] ); |
| 796 |
} |
| 797 |
|
| 798 |
ApiSandbox.updateUI( params ); |
| 799 |
return true; |
| 800 |
}, |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
updateUI: function ( params ) { |
| 808 |
const addPages = []; |
| 809 |
|
| 810 |
if ( !$.isPlainObject( params ) ) { |
| 811 |
params = undefined; |
| 812 |
} |
| 813 |
|
| 814 |
if ( updatingBooklet ) { |
| 815 |
return; |
| 816 |
} |
| 817 |
updatingBooklet = true; |
| 818 |
try { |
| 819 |
if ( params !== undefined ) { |
| 820 |
pages.main.loadQueryParams( params ); |
| 821 |
} |
| 822 |
addPages.push( pages.main ); |
| 823 |
if ( resultPage !== null ) { |
| 824 |
addPages.push( resultPage ); |
| 825 |
} |
| 826 |
pages.main.apiCheckValid(); |
| 827 |
|
| 828 |
let i = 0; |
| 829 |
while ( addPages.length ) { |
| 830 |
const page = addPages.shift(); |
| 831 |
if ( bookletPages[ i ] !== page ) { |
| 832 |
for ( let j = i; j < bookletPages.length; j++ ) { |
| 833 |
if ( bookletPages[ j ].getName() === page.getName() ) { |
| 834 |
bookletPages.splice( j, 1 ); |
| 835 |
} |
| 836 |
} |
| 837 |
bookletPages.splice( i, 0, page ); |
| 838 |
booklet.addPages( [ page ], i ); |
| 839 |
} |
| 840 |
i++; |
| 841 |
|
| 842 |
if ( page.getSubpages ) { |
| 843 |
const subpages = page.getSubpages(); |
| 844 |
|
| 845 |
subpages.forEach( ( subpage, k ) => { |
| 846 |
if ( !Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { |
| 847 |
subpage.indentLevel = page.indentLevel + 1; |
| 848 |
pages[ subpage.key ] = new ApiSandbox.PageLayout( subpage ); |
| 849 |
} |
| 850 |
if ( params !== undefined ) { |
| 851 |
pages[ subpage.key ].loadQueryParams( params ); |
| 852 |
} |
| 853 |
addPages.splice( k, 0, pages[ subpage.key ] ); |
| 854 |
pages[ subpage.key ].apiCheckValid(); |
| 855 |
} ); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
if ( bookletPages.length > i ) { |
| 860 |
const removePages = bookletPages.splice( i, bookletPages.length - i ); |
| 861 |
booklet.removePages( removePages ); |
| 862 |
} |
| 863 |
|
| 864 |
if ( !booklet.getCurrentPageName() ) { |
| 865 |
booklet.selectFirstSelectablePage(); |
| 866 |
} |
| 867 |
} finally { |
| 868 |
updatingBooklet = false; |
| 869 |
} |
| 870 |
}, |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
resetUI: function () { |
| 876 |
suppressErrors = true; |
| 877 |
pages = { |
| 878 |
main: new ApiSandbox.PageLayout( { key: 'main', path: 'main' } ) |
| 879 |
}; |
| 880 |
resultPage = null; |
| 881 |
ApiSandbox.updateUI(); |
| 882 |
}, |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
sendRequest: function ( params ) { |
| 891 |
let method = 'get', |
| 892 |
infoMessage; |
| 893 |
const paramsAreForced = !!params, |
| 894 |
deferreds = [], |
| 895 |
displayParams = {}, |
| 896 |
ajaxOptions = {}, |
| 897 |
tokenWidgets = [], |
| 898 |
checkPages = [ pages.main ]; |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
const $focus = $( '#mw-apisandbox-ui' ).find( document.activeElement ); |
| 903 |
if ( $focus.length ) { |
| 904 |
$focus[ 0 ].blur(); |
| 905 |
} |
| 906 |
|
| 907 |
suppressErrors = false; |
| 908 |
|
| 909 |
|
| 910 |
if ( paramsAreForced ) { |
| 911 |
ApiSandbox.updateUI( params ); |
| 912 |
} |
| 913 |
params = {}; |
| 914 |
while ( checkPages.length ) { |
| 915 |
const checkPage = checkPages.shift(); |
| 916 |
if ( checkPage.tokenWidget ) { |
| 917 |
tokenWidgets.push( checkPage.tokenWidget ); |
| 918 |
} |
| 919 |
deferreds.push( ...checkPage.apiCheckValid() ); |
| 920 |
checkPage.getQueryParams( params, displayParams, ajaxOptions ); |
| 921 |
if ( checkPage.paramInfo.mustbeposted !== undefined ) { |
| 922 |
method = 'post'; |
| 923 |
infoMessage = mw.message( 'apisandbox-request-post' ).parseDom(); |
| 924 |
} |
| 925 |
const subpages = checkPage.getSubpages(); |
| 926 |
|
| 927 |
subpages.forEach( ( subpage ) => { |
| 928 |
if ( Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { |
| 929 |
checkPages.push( pages[ subpage.key ] ); |
| 930 |
} |
| 931 |
} ); |
| 932 |
} |
| 933 |
|
| 934 |
if ( !paramsAreForced ) { |
| 935 |
|
| 936 |
baseRequestParams = Object.assign( {}, params ); |
| 937 |
} |
| 938 |
|
| 939 |
$.when( ...deferreds ).done( function () { |
| 940 |
|
| 941 |
function countValues( value, array ) { |
| 942 |
let count = 0; |
| 943 |
for ( let n = 0; n < array.length; n++ ) { |
| 944 |
if ( array[ n ] === value ) { |
| 945 |
count++; |
| 946 |
} |
| 947 |
} |
| 948 |
return count; |
| 949 |
} |
| 950 |
|
| 951 |
const errorCount = countValues( false, arguments ); |
| 952 |
if ( errorCount > 0 ) { |
| 953 |
const actions = [ |
| 954 |
{ |
| 955 |
action: 'accept', |
| 956 |
label: OO.ui.msg( 'ooui-dialog-process-dismiss' ), |
| 957 |
flags: 'primary' |
| 958 |
} |
| 959 |
]; |
| 960 |
let deferred; |
| 961 |
if ( tokenWidgets.length ) { |
| 962 |
|
| 963 |
deferred = $.when( ...tokenWidgets.map( ( w ) => w.apiCheckValid( suppressErrors ) ) ); |
| 964 |
|
| 965 |
deferred.done( function () { |
| 966 |
|
| 967 |
const tokenErrorCount = countValues( false, arguments ); |
| 968 |
if ( tokenErrorCount === errorCount ) { |
| 969 |
delete actions[ 0 ].flags; |
| 970 |
actions.push( { |
| 971 |
action: 'fix', |
| 972 |
label: mw.msg( 'apisandbox-results-fixtoken' ), |
| 973 |
flags: 'primary' |
| 974 |
} ); |
| 975 |
} |
| 976 |
} ); |
| 977 |
} else { |
| 978 |
deferred = $.Deferred().resolve(); |
| 979 |
} |
| 980 |
deferred.always( () => { |
| 981 |
windowManager.openWindow( 'errorAlert', { |
| 982 |
title: Util.parseMsg( 'apisandbox-submit-invalid-fields-title' ), |
| 983 |
message: Util.parseMsg( 'apisandbox-submit-invalid-fields-message' ), |
| 984 |
actions: actions |
| 985 |
} ).closed.then( ( data ) => { |
| 986 |
if ( data && data.action === 'fix' ) { |
| 987 |
ApiSandbox.fixTokenAndResend(); |
| 988 |
} |
| 989 |
} ); |
| 990 |
} ); |
| 991 |
return; |
| 992 |
} |
| 993 |
|
| 994 |
if ( params.format === undefined ) { |
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
throw new Error( "'format' parameter is required" ); |
| 999 |
} |
| 1000 |
if ( params.action === undefined ) { |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
throw new Error( "'action' parameter is required" ); |
| 1005 |
} |
| 1006 |
|
| 1007 |
const query = $.param( displayParams ); |
| 1008 |
|
| 1009 |
|
| 1010 |
if ( method !== 'post' && query.length > 7500 ) { |
| 1011 |
method = 'post'; |
| 1012 |
infoMessage = mw.message( 'apisandbox-request-post2' ).parseDom(); |
| 1013 |
} |
| 1014 |
|
| 1015 |
const formatItems = Util.formatRequest( displayParams, params, method, ajaxOptions ); |
| 1016 |
|
| 1017 |
|
| 1018 |
if ( params.format !== undefined ) { |
| 1019 |
if ( Object.prototype.hasOwnProperty.call( availableFormats, params.format + 'fm' ) ) { |
| 1020 |
params.format = params.format + 'fm'; |
| 1021 |
} |
| 1022 |
if ( params.format.slice( -2 ) === 'fm' ) { |
| 1023 |
params.wrappedhtml = 1; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
let progressLoading = false; |
| 1028 |
const $progressText = $( '<span>' ).text( mw.msg( 'apisandbox-sending-request' ) ); |
| 1029 |
const progress = new OO.ui.ProgressBarWidget( { |
| 1030 |
progress: false |
| 1031 |
} ); |
| 1032 |
|
| 1033 |
const $result = $( '<div>' ) |
| 1034 |
.append( $progressText, progress.$element ); |
| 1035 |
|
| 1036 |
const page = resultPage = new OO.ui.PageLayout( '|results|', { expanded: false } ); |
| 1037 |
page.setupOutlineItem = function () { |
| 1038 |
this.outlineItem.setLabel( mw.msg( 'apisandbox-results' ) ); |
| 1039 |
}; |
| 1040 |
|
| 1041 |
if ( !formatDropdown ) { |
| 1042 |
formatDropdown = new OO.ui.DropdownWidget( { |
| 1043 |
menu: { items: [] }, |
| 1044 |
$overlay: true |
| 1045 |
} ); |
| 1046 |
formatDropdown.getMenu().on( 'select', Util.onFormatDropdownChange ); |
| 1047 |
} |
| 1048 |
|
| 1049 |
const menu = formatDropdown.getMenu(); |
| 1050 |
let selectedLabel = menu.findSelectedItem() ? menu.findSelectedItem().getLabel() : ''; |
| 1051 |
if ( typeof selectedLabel !== 'string' ) { |
| 1052 |
selectedLabel = selectedLabel.text(); |
| 1053 |
} |
| 1054 |
menu.clearItems().addItems( formatItems ); |
| 1055 |
menu.chooseItem( menu.getItemFromLabel( selectedLabel ) || menu.findFirstSelectableItem() ); |
| 1056 |
|
| 1057 |
|
| 1058 |
Util.onFormatDropdownChange(); |
| 1059 |
|
| 1060 |
page.$element.empty() |
| 1061 |
.append( |
| 1062 |
new OO.ui.FieldLayout( |
| 1063 |
formatDropdown, { |
| 1064 |
label: Util.parseMsg( 'apisandbox-request-selectformat-label' ) |
| 1065 |
} |
| 1066 |
).$element, |
| 1067 |
formatItems.map( ( item ) => item.getData().$element ) |
| 1068 |
); |
| 1069 |
|
| 1070 |
if ( method === 'post' ) { |
| 1071 |
page.$element.append( new OO.ui.LabelWidget( { |
| 1072 |
label: infoMessage, |
| 1073 |
classes: [ 'oo-ui-inline-help' ] |
| 1074 |
} ).$element ); |
| 1075 |
} |
| 1076 |
if ( ajaxOptions.contentType === 'multipart/form-data' ) { |
| 1077 |
page.$element.append( new OO.ui.LabelWidget( { |
| 1078 |
label: mw.message( 'apisandbox-request-formdata' ).parseDom(), |
| 1079 |
classes: [ 'oo-ui-inline-help' ] |
| 1080 |
} ).$element ); |
| 1081 |
} |
| 1082 |
|
| 1083 |
page.$element.append( $result ); |
| 1084 |
|
| 1085 |
ApiSandbox.updateUI(); |
| 1086 |
booklet.setPage( '|results|' ); |
| 1087 |
|
| 1088 |
const selected = menu.findFirstSelectedItem(); |
| 1089 |
if ( selected ) { |
| 1090 |
const textInput = selected.getData().textInput; |
| 1091 |
if ( textInput instanceof OO.ui.MultilineTextInputWidget ) { |
| 1092 |
textInput.updatePosition(); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
location.href = oldhash = '#' + query; |
| 1097 |
|
| 1098 |
api[ method ]( params, Object.assign( ajaxOptions, { |
| 1099 |
dataType: 'text', |
| 1100 |
xhr: function () { |
| 1101 |
const xhr = new window.XMLHttpRequest(); |
| 1102 |
xhr.upload.addEventListener( 'progress', ( e ) => { |
| 1103 |
if ( !progressLoading ) { |
| 1104 |
if ( e.lengthComputable ) { |
| 1105 |
progress.setProgress( e.loaded * 100 / e.total ); |
| 1106 |
} else { |
| 1107 |
progress.setProgress( false ); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
} ); |
| 1111 |
xhr.addEventListener( 'progress', ( e ) => { |
| 1112 |
if ( !progressLoading ) { |
| 1113 |
progressLoading = true; |
| 1114 |
$progressText.text( mw.msg( 'apisandbox-loading-results' ) ); |
| 1115 |
} |
| 1116 |
if ( e.lengthComputable ) { |
| 1117 |
progress.setProgress( e.loaded * 100 / e.total ); |
| 1118 |
} else { |
| 1119 |
progress.setProgress( false ); |
| 1120 |
} |
| 1121 |
} ); |
| 1122 |
return xhr; |
| 1123 |
} |
| 1124 |
} ) ) |
| 1125 |
.catch( function ( code, data, result, jqXHR ) { |
| 1126 |
const d = $.Deferred(); |
| 1127 |
|
| 1128 |
if ( code !== 'http' ) { |
| 1129 |
|
| 1130 |
d.resolve( result, jqXHR ); |
| 1131 |
} else { |
| 1132 |
|
| 1133 |
d.reject.apply( d, arguments ); |
| 1134 |
} |
| 1135 |
return d.promise(); |
| 1136 |
} ) |
| 1137 |
.then( ( data, jqXHR ) => { |
| 1138 |
const ct = jqXHR.getResponseHeader( 'Content-Type' ), |
| 1139 |
loginSuppressed = jqXHR.getResponseHeader( 'MediaWiki-Login-Suppressed' ) || 'false'; |
| 1140 |
|
| 1141 |
$result.empty(); |
| 1142 |
if ( loginSuppressed !== 'false' ) { |
| 1143 |
$( '<div>' ) |
| 1144 |
.addClass( 'warning' ) |
| 1145 |
.append( Util.parseMsg( 'apisandbox-results-login-suppressed' ) ) |
| 1146 |
.appendTo( $result ); |
| 1147 |
} |
| 1148 |
let loadTime; |
| 1149 |
if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct ) ) { |
| 1150 |
try { |
| 1151 |
data = JSON.parse( data ); |
| 1152 |
} catch ( e ) { |
| 1153 |
|
| 1154 |
data = { modules: {}, html: data }; |
| 1155 |
} |
| 1156 |
if ( data.modules.length ) { |
| 1157 |
mw.loader.load( data.modules ); |
| 1158 |
} |
| 1159 |
if ( data.status && data.status !== 200 ) { |
| 1160 |
$( '<div>' ) |
| 1161 |
.addClass( 'api-pretty-header api-pretty-status' ) |
| 1162 |
.append( Util.parseMsg( 'api-format-prettyprint-status', data.status, data.statustext ) ) |
| 1163 |
.appendTo( $result ); |
| 1164 |
} |
| 1165 |
$result.append( Util.parseHTML( data.html ) ); |
| 1166 |
loadTime = data.time; |
| 1167 |
} else { |
| 1168 |
$( '<pre>' ) |
| 1169 |
.addClass( 'api-pretty-content' ) |
| 1170 |
.text( data ) |
| 1171 |
.appendTo( $result ); |
| 1172 |
} |
| 1173 |
if ( paramsAreForced || data.continue ) { |
| 1174 |
let clear; |
| 1175 |
$result.append( |
| 1176 |
$( '<div>' ).append( |
| 1177 |
new OO.ui.ButtonWidget( { |
| 1178 |
label: mw.msg( 'apisandbox-continue' ) |
| 1179 |
} ).on( 'click', () => { |
| 1180 |
ApiSandbox.sendRequest( Object.assign( {}, baseRequestParams, data.continue ) ); |
| 1181 |
} ).setDisabled( !data.continue ).$element, |
| 1182 |
( clear = new OO.ui.ButtonWidget( { |
| 1183 |
label: mw.msg( 'apisandbox-continue-clear' ) |
| 1184 |
} ).on( 'click', () => { |
| 1185 |
ApiSandbox.updateUI( baseRequestParams ); |
| 1186 |
clear.setDisabled( true ); |
| 1187 |
booklet.setPage( '|results|' ); |
| 1188 |
} ).setDisabled( !paramsAreForced ) ).$element, |
| 1189 |
new OO.ui.PopupButtonWidget( { |
| 1190 |
$overlay: true, |
| 1191 |
framed: false, |
| 1192 |
icon: 'info', |
| 1193 |
popup: { |
| 1194 |
$content: $( '<div>' ).append( Util.parseMsg( 'apisandbox-continue-help' ) ), |
| 1195 |
padded: true, |
| 1196 |
width: 'auto' |
| 1197 |
} |
| 1198 |
} ).$element |
| 1199 |
) |
| 1200 |
); |
| 1201 |
} |
| 1202 |
if ( typeof loadTime === 'number' ) { |
| 1203 |
$result.append( |
| 1204 |
$( '<div>' ).append( |
| 1205 |
new OO.ui.LabelWidget( { |
| 1206 |
label: mw.msg( 'apisandbox-request-time', loadTime ) |
| 1207 |
} ).$element |
| 1208 |
) |
| 1209 |
); |
| 1210 |
} |
| 1211 |
|
| 1212 |
if ( jqXHR.getResponseHeader( 'MediaWiki-API-Error' ) === 'badtoken' ) { |
| 1213 |
|
| 1214 |
Util.markTokensBad(); |
| 1215 |
const button = new OO.ui.ButtonWidget( { |
| 1216 |
label: mw.msg( 'apisandbox-results-fixtoken' ) |
| 1217 |
} ); |
| 1218 |
button.on( 'click', ApiSandbox.fixTokenAndResend ) |
| 1219 |
.on( 'click', button.setDisabled, [ true ], button ) |
| 1220 |
.$element.appendTo( $result ); |
| 1221 |
} |
| 1222 |
}, ( code, data ) => { |
| 1223 |
const details = 'HTTP error: ' + data.exception; |
| 1224 |
$result.empty() |
| 1225 |
.append( |
| 1226 |
new OO.ui.LabelWidget( { |
| 1227 |
label: mw.msg( 'apisandbox-results-error', details ), |
| 1228 |
classes: [ 'error' ] |
| 1229 |
} ).$element |
| 1230 |
); |
| 1231 |
} ); |
| 1232 |
} ); |
| 1233 |
}, |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
fixTokenAndResend: function () { |
| 1242 |
let ok = true; |
| 1243 |
const tokenWait = { dummy: true }, |
| 1244 |
checkPages = [ pages.main ], |
| 1245 |
success = function ( k ) { |
| 1246 |
delete tokenWait[ k ]; |
| 1247 |
if ( ok && $.isEmptyObject( tokenWait ) ) { |
| 1248 |
ApiSandbox.sendRequest(); |
| 1249 |
} |
| 1250 |
}, |
| 1251 |
failure = function ( k ) { |
| 1252 |
delete tokenWait[ k ]; |
| 1253 |
ok = false; |
| 1254 |
}; |
| 1255 |
|
| 1256 |
while ( checkPages.length ) { |
| 1257 |
const page = checkPages.shift(); |
| 1258 |
|
| 1259 |
if ( page.tokenWidget ) { |
| 1260 |
const key = page.apiModule + page.tokenWidget.paramInfo.name; |
| 1261 |
tokenWait[ key ] = page.tokenWidget.fetchToken(); |
| 1262 |
tokenWait[ key ] |
| 1263 |
.done( success.bind( page.tokenWidget, key ) ) |
| 1264 |
.fail( failure.bind( page.tokenWidget, key ) ); |
| 1265 |
} |
| 1266 |
|
| 1267 |
const subpages = page.getSubpages(); |
| 1268 |
|
| 1269 |
subpages.forEach( ( subpage ) => { |
| 1270 |
if ( Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { |
| 1271 |
checkPages.push( pages[ subpage.key ] ); |
| 1272 |
} |
| 1273 |
} ); |
| 1274 |
} |
| 1275 |
|
| 1276 |
success( 'dummy', '' ); |
| 1277 |
}, |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
updateValidityIndicators: function () { |
| 1283 |
const checkPages = [ pages.main ]; |
| 1284 |
|
| 1285 |
while ( checkPages.length ) { |
| 1286 |
const page = checkPages.shift(); |
| 1287 |
page.apiCheckValid(); |
| 1288 |
const subpages = page.getSubpages(); |
| 1289 |
|
| 1290 |
subpages.forEach( ( subpage ) => { |
| 1291 |
if ( Object.prototype.hasOwnProperty.call( pages, subpage.key ) ) { |
| 1292 |
checkPages.push( pages[ subpage.key ] ); |
| 1293 |
} |
| 1294 |
} ); |
| 1295 |
} |
| 1296 |
} |
| 1297 |
}; |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
ApiSandbox.PageLayout = function ( config ) { |
| 1309 |
config = Object.assign( { prefix: '', expanded: false }, config ); |
| 1310 |
this.displayText = config.key; |
| 1311 |
this.apiModule = config.path; |
| 1312 |
this.prefix = config.prefix; |
| 1313 |
this.paramInfo = null; |
| 1314 |
this.apiIsValid = true; |
| 1315 |
this.loadFromQueryParams = null; |
| 1316 |
this.widgets = {}; |
| 1317 |
this.itemsFieldset = null; |
| 1318 |
this.deprecatedItemsFieldset = null; |
| 1319 |
this.templatedItemsCache = {}; |
| 1320 |
this.tokenWidget = null; |
| 1321 |
this.indentLevel = config.indentLevel ? config.indentLevel : 0; |
| 1322 |
ApiSandbox.PageLayout.super.call( this, config.key, config ); |
| 1323 |
this.loadParamInfo(); |
| 1324 |
}; |
| 1325 |
OO.inheritClass( ApiSandbox.PageLayout, OO.ui.PageLayout ); |
| 1326 |
ApiSandbox.PageLayout.prototype.setupOutlineItem = function () { |
| 1327 |
this.outlineItem.setLevel( this.indentLevel ); |
| 1328 |
this.outlineItem.setLabel( this.displayText ); |
| 1329 |
this.outlineItem.setIcon( this.apiIsValid || suppressErrors ? null : 'alert' ); |
| 1330 |
this.outlineItem.setTitle( |
| 1331 |
this.apiIsValid || suppressErrors ? '' : mw.message( 'apisandbox-alert-page' ).plain() |
| 1332 |
); |
| 1333 |
}; |
| 1334 |
|
| 1335 |
function widgetLabelOnClick() { |
| 1336 |
const f = this.getField(); |
| 1337 |
if ( typeof f.setDisabled === 'function' ) { |
| 1338 |
f.setDisabled( false ); |
| 1339 |
} |
| 1340 |
if ( typeof f.focus === 'function' ) { |
| 1341 |
f.focus(); |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
ApiSandbox.PageLayout.prototype.makeWidgetFieldLayouts = function ( ppi, name ) { |
| 1357 |
const widget = Util.createWidgetForParameter( ppi ); |
| 1358 |
if ( ppi.tokentype ) { |
| 1359 |
this.tokenWidget = widget; |
| 1360 |
} |
| 1361 |
if ( this.paramInfo.templatedparameters.length ) { |
| 1362 |
widget.on( 'change', this.updateTemplatedParameters, [ null ], this ); |
| 1363 |
} |
| 1364 |
|
| 1365 |
const helpLabel = new ParamLabelWidget(); |
| 1366 |
|
| 1367 |
let $tmp = Util.parseHTML( ppi.description ); |
| 1368 |
$tmp.filter( 'dl' ).makeCollapsible( { |
| 1369 |
collapsed: true |
| 1370 |
} ).children( '.mw-collapsible-toggle' ).each( function () { |
| 1371 |
const $this = $( this ); |
| 1372 |
$this.parent().prev( 'p' ).append( $this ); |
| 1373 |
} ); |
| 1374 |
helpLabel.addDescription( $tmp ); |
| 1375 |
|
| 1376 |
if ( ppi.info && ppi.info.length ) { |
| 1377 |
for ( let i = 0; i < ppi.info.length; i++ ) { |
| 1378 |
helpLabel.addInfo( Util.parseHTML( ppi.info[ i ].text ) ); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
let flag = true; |
| 1382 |
let count = Infinity; |
| 1383 |
let tmp; |
| 1384 |
switch ( ppi.type ) { |
| 1385 |
case 'namespace': |
| 1386 |
flag = false; |
| 1387 |
count = mw.config.get( 'wgFormattedNamespaces' ).length; |
| 1388 |
break; |
| 1389 |
|
| 1390 |
case 'limit': |
| 1391 |
tmp = [ |
| 1392 |
mw.message( |
| 1393 |
'paramvalidator-help-type-number-minmax', 1, |
| 1394 |
widget.paramInfo.min, widget.paramInfo.apiSandboxMax |
| 1395 |
).parse(), |
| 1396 |
mw.message( 'apisandbox-param-limit' ).parse() |
| 1397 |
]; |
| 1398 |
helpLabel.addInfo( Util.parseHTML( tmp.join( mw.message( 'word-separator' ).escaped() ) ) ); |
| 1399 |
break; |
| 1400 |
|
| 1401 |
case 'integer': |
| 1402 |
tmp = ''; |
| 1403 |
if ( ppi.min !== undefined ) { |
| 1404 |
tmp += 'min'; |
| 1405 |
} |
| 1406 |
if ( ppi.max !== undefined ) { |
| 1407 |
tmp += 'max'; |
| 1408 |
} |
| 1409 |
if ( tmp !== '' ) { |
| 1410 |
helpLabel.addInfo( |
| 1411 |
Util.parseMsg( |
| 1412 |
'paramvalidator-help-type-number-' + tmp, |
| 1413 |
Util.apiBool( ppi.multi ) ? 2 : 1, |
| 1414 |
ppi.min, ppi.max |
| 1415 |
) |
| 1416 |
); |
| 1417 |
} |
| 1418 |
break; |
| 1419 |
|
| 1420 |
default: |
| 1421 |
if ( Array.isArray( ppi.type ) ) { |
| 1422 |
flag = false; |
| 1423 |
count = ppi.type.length; |
| 1424 |
} |
| 1425 |
break; |
| 1426 |
} |
| 1427 |
if ( Util.apiBool( ppi.multi ) ) { |
| 1428 |
tmp = []; |
| 1429 |
if ( flag && !( widget instanceof OO.ui.TagMultiselectWidget ) && |
| 1430 |
!( |
| 1431 |
widget instanceof OptionalParamWidget && |
| 1432 |
widget.widget instanceof OO.ui.TagMultiselectWidget |
| 1433 |
) |
| 1434 |
) { |
| 1435 |
tmp.push( mw.message( 'api-help-param-multi-separate' ).parse() ); |
| 1436 |
} |
| 1437 |
if ( count > ppi.lowlimit ) { |
| 1438 |
tmp.push( |
| 1439 |
mw.message( 'paramvalidator-help-multi-max', ppi.lowlimit, ppi.highlimit ).parse() |
| 1440 |
); |
| 1441 |
} |
| 1442 |
if ( tmp.length ) { |
| 1443 |
helpLabel.addInfo( Util.parseHTML( tmp.join( mw.message( 'word-separator' ).escaped() ) ) ); |
| 1444 |
} |
| 1445 |
} |
| 1446 |
if ( 'maxbytes' in ppi ) { |
| 1447 |
helpLabel.addInfo( Util.parseMsg( 'paramvalidator-help-type-string-maxbytes', ppi.maxbytes ) ); |
| 1448 |
} |
| 1449 |
if ( 'maxchars' in ppi ) { |
| 1450 |
helpLabel.addInfo( Util.parseMsg( 'paramvalidator-help-type-string-maxchars', ppi.maxchars ) ); |
| 1451 |
} |
| 1452 |
if ( ppi.usedTemplateVars && ppi.usedTemplateVars.length ) { |
| 1453 |
$tmp = $(); |
| 1454 |
for ( let j = 0, l = ppi.usedTemplateVars.length; j < l; j++ ) { |
| 1455 |
$tmp = $tmp.add( $( '<var>' ).text( ppi.usedTemplateVars[ j ] ) ); |
| 1456 |
if ( j === l - 2 ) { |
| 1457 |
$tmp = $tmp.add( mw.message( 'and' ).parseDom() ); |
| 1458 |
$tmp = $tmp.add( mw.message( 'word-separator' ).parseDom() ); |
| 1459 |
} else if ( j !== l - 1 ) { |
| 1460 |
$tmp = $tmp.add( mw.message( 'comma-separator' ).parseDom() ); |
| 1461 |
} |
| 1462 |
} |
| 1463 |
helpLabel.addInfo( |
| 1464 |
Util.parseMsg( |
| 1465 |
'apisandbox-templated-parameter-reason', |
| 1466 |
ppi.usedTemplateVars.length, |
| 1467 |
$tmp |
| 1468 |
) |
| 1469 |
); |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
const helpField = new OO.ui.FieldLayout( |
| 1476 |
helpLabel, |
| 1477 |
{ |
| 1478 |
align: 'top', |
| 1479 |
classes: [ 'mw-apisandbox-help-field' ] |
| 1480 |
} |
| 1481 |
); |
| 1482 |
|
| 1483 |
const layoutConfig = { |
| 1484 |
align: 'left', |
| 1485 |
classes: [ 'mw-apisandbox-widget-field' ], |
| 1486 |
label: name |
| 1487 |
}; |
| 1488 |
|
| 1489 |
let widgetField; |
| 1490 |
if ( ppi.tokentype ) { |
| 1491 |
const button = new OO.ui.ButtonWidget( { |
| 1492 |
label: mw.msg( 'apisandbox-fetch-token' ) |
| 1493 |
} ); |
| 1494 |
button.on( 'click', widget.fetchToken, [], widget ); |
| 1495 |
|
| 1496 |
widgetField = new OO.ui.ActionFieldLayout( widget, button, layoutConfig ); |
| 1497 |
} else { |
| 1498 |
widgetField = new OO.ui.FieldLayout( widget, layoutConfig ); |
| 1499 |
} |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
widgetField.$label.on( 'click', widgetLabelOnClick.bind( widgetField ) ); |
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
widgetField.onFieldDisable( false ); |
| 1509 |
widgetField.onFieldDisable = function () {}; |
| 1510 |
|
| 1511 |
widgetField.apiParamIndex = ppi.index; |
| 1512 |
|
| 1513 |
return { |
| 1514 |
widget: widget, |
| 1515 |
widgetField: widgetField, |
| 1516 |
helpField: helpField |
| 1517 |
}; |
| 1518 |
}; |
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
ApiSandbox.PageLayout.prototype.updateTemplatedParameters = function ( params ) { |
| 1527 |
const layout = this, |
| 1528 |
pi = this.paramInfo, |
| 1529 |
prefix = layout.prefix + pi.prefix; |
| 1530 |
|
| 1531 |
if ( !pi || !pi.templatedparameters.length ) { |
| 1532 |
return; |
| 1533 |
} |
| 1534 |
|
| 1535 |
if ( !$.isPlainObject( params ) ) { |
| 1536 |
params = null; |
| 1537 |
} |
| 1538 |
|
| 1539 |
let toRemove = {}; |
| 1540 |
|
| 1541 |
$.each( this.templatedItemsCache, ( k, el ) => { |
| 1542 |
if ( el.widget.isElementAttached() ) { |
| 1543 |
toRemove[ k ] = el; |
| 1544 |
} |
| 1545 |
} ); |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
const toProcess = pi.templatedparameters.map( ( info ) => ( { |
| 1550 |
name: prefix + info.name, |
| 1551 |
info: info, |
| 1552 |
vars: Object.assign( {}, info.templatevars ), |
| 1553 |
usedVars: [] |
| 1554 |
} ) ); |
| 1555 |
let p; |
| 1556 |
const doProcess = function ( placeholder, target ) { |
| 1557 |
target = prefix + target; |
| 1558 |
|
| 1559 |
if ( !layout.widgets[ target ] ) { |
| 1560 |
|
| 1561 |
|
| 1562 |
return true; |
| 1563 |
} |
| 1564 |
|
| 1565 |
if ( !layout.widgets[ target ].getApiValueForTemplates ) { |
| 1566 |
|
| 1567 |
return false; |
| 1568 |
} |
| 1569 |
|
| 1570 |
const values = layout.widgets[ target ].getApiValueForTemplates(); |
| 1571 |
if ( !Array.isArray( values ) || !values.length ) { |
| 1572 |
|
| 1573 |
|
| 1574 |
return false; |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
delete p.vars[ placeholder ]; |
| 1581 |
const usedVars = p.usedVars.concat( [ target ] ); |
| 1582 |
placeholder = '{' + placeholder + '}'; |
| 1583 |
const done = $.isEmptyObject( p.vars ); |
| 1584 |
let index, container; |
| 1585 |
if ( done ) { |
| 1586 |
container = Util.apiBool( p.info.deprecated ) ? layout.deprecatedItemsFieldset : layout.itemsFieldset; |
| 1587 |
const items = container.getItems(); |
| 1588 |
for ( let i = 0; i < items.length; i++ ) { |
| 1589 |
if ( items[ i ].apiParamIndex !== undefined && items[ i ].apiParamIndex > p.info.index ) { |
| 1590 |
index = i; |
| 1591 |
break; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
} |
| 1595 |
values.forEach( ( value ) => { |
| 1596 |
if ( !/^[^{}]*$/.exec( value ) ) { |
| 1597 |
|
| 1598 |
return; |
| 1599 |
} |
| 1600 |
|
| 1601 |
const name = p.name.replace( placeholder, value ); |
| 1602 |
if ( done ) { |
| 1603 |
let tmp; |
| 1604 |
if ( layout.templatedItemsCache[ name ] ) { |
| 1605 |
tmp = layout.templatedItemsCache[ name ]; |
| 1606 |
} else { |
| 1607 |
tmp = layout.makeWidgetFieldLayouts( |
| 1608 |
Object.assign( {}, p.info, { usedTemplateVars: usedVars } ), name |
| 1609 |
); |
| 1610 |
layout.templatedItemsCache[ name ] = tmp; |
| 1611 |
} |
| 1612 |
delete toRemove[ name ]; |
| 1613 |
if ( !tmp.widget.isElementAttached() ) { |
| 1614 |
layout.widgets[ name ] = tmp.widget; |
| 1615 |
container.addItems( [ tmp.widgetField, tmp.helpField ], index ); |
| 1616 |
if ( index !== undefined ) { |
| 1617 |
index += 2; |
| 1618 |
} |
| 1619 |
} |
| 1620 |
if ( params ) { |
| 1621 |
tmp.widget.setApiValue( Object.prototype.hasOwnProperty.call( params, name ) ? params[ name ] : undefined ); |
| 1622 |
} |
| 1623 |
} else { |
| 1624 |
const newVars = {}; |
| 1625 |
|
| 1626 |
$.each( p.vars, ( k, v ) => { |
| 1627 |
newVars[ k ] = v.replace( placeholder, value ); |
| 1628 |
} ); |
| 1629 |
toProcess.push( { |
| 1630 |
name: name, |
| 1631 |
info: p.info, |
| 1632 |
vars: newVars, |
| 1633 |
usedVars: usedVars |
| 1634 |
} ); |
| 1635 |
} |
| 1636 |
} ); |
| 1637 |
return false; |
| 1638 |
}; |
| 1639 |
while ( toProcess.length ) { |
| 1640 |
p = toProcess.shift(); |
| 1641 |
|
| 1642 |
$.each( p.vars, doProcess ); |
| 1643 |
} |
| 1644 |
|
| 1645 |
|
| 1646 |
toRemove = $.map( toRemove, ( el, name ) => { |
| 1647 |
delete layout.widgets[ name ]; |
| 1648 |
return [ el.widgetField, el.helpField ]; |
| 1649 |
} ); |
| 1650 |
if ( toRemove.length ) { |
| 1651 |
this.itemsFieldset.removeItems( toRemove ); |
| 1652 |
this.deprecatedItemsFieldset.removeItems( toRemove ); |
| 1653 |
} |
| 1654 |
}; |
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
ApiSandbox.PageLayout.prototype.loadParamInfo = function () { |
| 1660 |
let dynamicFieldset, dynamicParamNameWidget; |
| 1661 |
const layout = this, |
| 1662 |
removeDynamicParamWidget = function ( name, item ) { |
| 1663 |
dynamicFieldset.removeItems( [ item ] ); |
| 1664 |
delete layout.widgets[ name ]; |
| 1665 |
}, |
| 1666 |
addDynamicParamWidget = function () { |
| 1667 |
|
| 1668 |
const name = dynamicParamNameWidget.getValue().trim(); |
| 1669 |
if ( name === '' ) { |
| 1670 |
dynamicParamNameWidget.focus(); |
| 1671 |
return; |
| 1672 |
} |
| 1673 |
|
| 1674 |
if ( layout.widgets[ name ] !== undefined ) { |
| 1675 |
windowManager.openWindow( 'errorAlert', { |
| 1676 |
title: Util.parseMsg( 'apisandbox-dynamic-error-exists', name ), |
| 1677 |
actions: [ |
| 1678 |
{ |
| 1679 |
action: 'accept', |
| 1680 |
label: OO.ui.msg( 'ooui-dialog-process-dismiss' ), |
| 1681 |
flags: 'primary' |
| 1682 |
} |
| 1683 |
] |
| 1684 |
} ); |
| 1685 |
return; |
| 1686 |
} |
| 1687 |
|
| 1688 |
const widget = Util.createWidgetForParameter( { |
| 1689 |
name: name, |
| 1690 |
type: 'string', |
| 1691 |
default: '' |
| 1692 |
}, { |
| 1693 |
nooptional: true |
| 1694 |
} ); |
| 1695 |
const button = new OO.ui.ButtonWidget( { |
| 1696 |
icon: 'trash', |
| 1697 |
flags: 'destructive' |
| 1698 |
} ); |
| 1699 |
const actionFieldLayout = new OO.ui.ActionFieldLayout( |
| 1700 |
widget, |
| 1701 |
button, |
| 1702 |
{ |
| 1703 |
label: name, |
| 1704 |
align: 'left' |
| 1705 |
} |
| 1706 |
); |
| 1707 |
button.on( 'click', removeDynamicParamWidget, [ name, actionFieldLayout ] ); |
| 1708 |
layout.widgets[ name ] = widget; |
| 1709 |
dynamicFieldset.addItems( [ actionFieldLayout ], dynamicFieldset.getItemCount() - 1 ); |
| 1710 |
widget.focus(); |
| 1711 |
|
| 1712 |
dynamicParamNameWidget.setValue( '' ); |
| 1713 |
}; |
| 1714 |
|
| 1715 |
this.$element.empty() |
| 1716 |
.append( |
| 1717 |
document.createTextNode( |
| 1718 |
mw.msg( 'apisandbox-loading', this.displayText ) |
| 1719 |
), |
| 1720 |
new OO.ui.ProgressBarWidget( { progress: false } ).$element |
| 1721 |
); |
| 1722 |
|
| 1723 |
Util.fetchModuleInfo( this.apiModule ) |
| 1724 |
.done( ( pi ) => { |
| 1725 |
const items = [], |
| 1726 |
deprecatedItems = [], |
| 1727 |
buttons = [], |
| 1728 |
filterFmModules = function ( v ) { |
| 1729 |
return v.slice( -2 ) !== 'fm' || |
| 1730 |
!Object.prototype.hasOwnProperty.call( availableFormats, v.slice( 0, v.length - 2 ) ); |
| 1731 |
}; |
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
if ( layout.apiModule === 'main' ) { |
| 1738 |
pi.parameters.forEach( ( parameter ) => { |
| 1739 |
if ( parameter.name === 'action' ) { |
| 1740 |
parameter.required = true; |
| 1741 |
delete parameter.default; |
| 1742 |
} |
| 1743 |
if ( parameter.name === 'format' ) { |
| 1744 |
const types = parameter.type; |
| 1745 |
types.forEach( ( type ) => { |
| 1746 |
availableFormats[ type ] = true; |
| 1747 |
} ); |
| 1748 |
parameter.type = types.filter( filterFmModules ); |
| 1749 |
parameter.default = 'json'; |
| 1750 |
parameter.required = true; |
| 1751 |
} |
| 1752 |
} ); |
| 1753 |
} |
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
if ( pi.group === 'format' ) { |
| 1759 |
pi.parameters = pi.parameters.filter( ( p ) => p.name !== 'wrappedhtml' ).map( ( p ) => { |
| 1760 |
if ( p.name === 'formatversion' ) { |
| 1761 |
|
| 1762 |
p.default = p.type.reduce( ( prev, current ) => !isNaN( current ) ? Math.max( prev, current ) : prev ); |
| 1763 |
p.required = true; |
| 1764 |
} |
| 1765 |
return p; |
| 1766 |
} ); |
| 1767 |
} |
| 1768 |
|
| 1769 |
layout.paramInfo = pi; |
| 1770 |
|
| 1771 |
let $desc = Util.parseHTML( pi.description ); |
| 1772 |
if ( pi.deprecated !== undefined ) { |
| 1773 |
$desc = $( '<span>' ).addClass( 'apihelp-deprecated' ).text( mw.msg( 'api-help-param-deprecated' ) ) |
| 1774 |
.add( document.createTextNode( mw.msg( 'word-separator' ) ) ).add( $desc ); |
| 1775 |
} |
| 1776 |
if ( pi.internal !== undefined ) { |
| 1777 |
$desc = $( '<span>' ).addClass( 'apihelp-internal' ).text( mw.msg( 'api-help-param-internal' ) ) |
| 1778 |
.add( document.createTextNode( mw.msg( 'word-separator' ) ) ).add( $desc ); |
| 1779 |
} |
| 1780 |
items.push( new OO.ui.FieldLayout( |
| 1781 |
new OO.ui.Widget( {} ).toggle( false ), { |
| 1782 |
align: 'top', |
| 1783 |
label: $desc |
| 1784 |
} |
| 1785 |
) ); |
| 1786 |
|
| 1787 |
if ( pi.helpurls.length ) { |
| 1788 |
buttons.push( new OO.ui.PopupButtonWidget( { |
| 1789 |
$overlay: true, |
| 1790 |
label: mw.msg( 'apisandbox-helpurls' ), |
| 1791 |
icon: 'help', |
| 1792 |
popup: { |
| 1793 |
width: 'auto', |
| 1794 |
padded: true, |
| 1795 |
classes: [ 'mw-apisandbox-popup-help' ], |
| 1796 |
$content: $( '<ul>' ).append( pi.helpurls.map( ( link ) => $( '<li>' ).append( $( '<a>' ) |
| 1797 |
.attr( { href: link, target: '_blank' } ) |
| 1798 |
.text( link ) |
| 1799 |
) ) ) |
| 1800 |
} |
| 1801 |
} ) ); |
| 1802 |
} |
| 1803 |
|
| 1804 |
if ( pi.examples.length ) { |
| 1805 |
buttons.push( new OO.ui.PopupButtonWidget( { |
| 1806 |
$overlay: true, |
| 1807 |
label: mw.msg( 'apisandbox-examples' ), |
| 1808 |
icon: 'code', |
| 1809 |
popup: { |
| 1810 |
width: 'auto', |
| 1811 |
padded: true, |
| 1812 |
classes: [ 'mw-apisandbox-popup-help' ], |
| 1813 |
$content: $( '<ul>' ).append( pi.examples.map( ( example ) => { |
| 1814 |
const $a = $( '<a>' ) |
| 1815 |
.attr( 'href', '#' + example.query ) |
| 1816 |
.html( example.description ); |
| 1817 |
$a.find( 'a' ).contents().unwrap(); |
| 1818 |
return $( '<li>' ).append( $a ); |
| 1819 |
} ) ) |
| 1820 |
} |
| 1821 |
} ) ); |
| 1822 |
} |
| 1823 |
|
| 1824 |
if ( buttons.length ) { |
| 1825 |
items.push( new OO.ui.FieldLayout( |
| 1826 |
new OO.ui.ButtonGroupWidget( { |
| 1827 |
items: buttons |
| 1828 |
} ), { align: 'top' } |
| 1829 |
) ); |
| 1830 |
} |
| 1831 |
|
| 1832 |
if ( pi.parameters.length ) { |
| 1833 |
const prefix = layout.prefix + pi.prefix; |
| 1834 |
pi.parameters.forEach( ( parameter ) => { |
| 1835 |
const tmpLayout = layout.makeWidgetFieldLayouts( parameter, prefix + parameter.name ); |
| 1836 |
layout.widgets[ prefix + parameter.name ] = tmpLayout.widget; |
| 1837 |
if ( Util.apiBool( parameter.deprecated ) ) { |
| 1838 |
deprecatedItems.push( tmpLayout.widgetField, tmpLayout.helpField ); |
| 1839 |
} else { |
| 1840 |
items.push( tmpLayout.widgetField, tmpLayout.helpField ); |
| 1841 |
} |
| 1842 |
} ); |
| 1843 |
} |
| 1844 |
|
| 1845 |
if ( !pi.parameters.length && !Util.apiBool( pi.dynamicparameters ) ) { |
| 1846 |
items.push( new OO.ui.FieldLayout( |
| 1847 |
new OO.ui.Widget( {} ).toggle( false ), { |
| 1848 |
align: 'top', |
| 1849 |
label: Util.parseMsg( 'apisandbox-no-parameters' ) |
| 1850 |
} |
| 1851 |
) ); |
| 1852 |
} |
| 1853 |
|
| 1854 |
layout.$element.empty(); |
| 1855 |
|
| 1856 |
layout.itemsFieldset = new OO.ui.FieldsetLayout( { |
| 1857 |
label: layout.displayText |
| 1858 |
} ); |
| 1859 |
layout.itemsFieldset.addItems( items ); |
| 1860 |
layout.itemsFieldset.$element.appendTo( layout.$element ); |
| 1861 |
|
| 1862 |
if ( Util.apiBool( pi.dynamicparameters ) ) { |
| 1863 |
dynamicFieldset = new OO.ui.FieldsetLayout(); |
| 1864 |
dynamicParamNameWidget = new OO.ui.TextInputWidget( { |
| 1865 |
placeholder: mw.msg( 'apisandbox-dynamic-parameters-add-placeholder' ) |
| 1866 |
} ).on( 'enter', addDynamicParamWidget ); |
| 1867 |
dynamicFieldset.addItems( [ |
| 1868 |
new OO.ui.FieldLayout( |
| 1869 |
new OO.ui.Widget( {} ).toggle( false ), { |
| 1870 |
align: 'top', |
| 1871 |
label: Util.parseHTML( pi.dynamicparameters ) |
| 1872 |
} |
| 1873 |
), |
| 1874 |
new OO.ui.ActionFieldLayout( |
| 1875 |
dynamicParamNameWidget, |
| 1876 |
new OO.ui.ButtonWidget( { |
| 1877 |
icon: 'add', |
| 1878 |
flags: 'progressive' |
| 1879 |
} ).on( 'click', addDynamicParamWidget ), |
| 1880 |
{ |
| 1881 |
label: mw.msg( 'apisandbox-dynamic-parameters-add-label' ), |
| 1882 |
align: 'left' |
| 1883 |
} |
| 1884 |
) |
| 1885 |
] ); |
| 1886 |
$( '<fieldset>' ) |
| 1887 |
.append( |
| 1888 |
$( '<legend>' ).text( mw.msg( 'apisandbox-dynamic-parameters' ) ), |
| 1889 |
dynamicFieldset.$element |
| 1890 |
) |
| 1891 |
.appendTo( layout.$element ); |
| 1892 |
} |
| 1893 |
|
| 1894 |
layout.deprecatedItemsFieldset = new OO.ui.FieldsetLayout().addItems( deprecatedItems ).toggle( false ); |
| 1895 |
const $tmp = $( '<fieldset>' ) |
| 1896 |
.toggle( !layout.deprecatedItemsFieldset.isEmpty() ) |
| 1897 |
.append( |
| 1898 |
$( '<legend>' ).append( |
| 1899 |
new OO.ui.ToggleButtonWidget( { |
| 1900 |
label: mw.msg( 'apisandbox-deprecated-parameters' ) |
| 1901 |
} ).on( 'change', layout.deprecatedItemsFieldset.toggle, [], layout.deprecatedItemsFieldset ).$element |
| 1902 |
), |
| 1903 |
layout.deprecatedItemsFieldset.$element |
| 1904 |
) |
| 1905 |
.appendTo( layout.$element ); |
| 1906 |
layout.deprecatedItemsFieldset.on( 'add', function () { |
| 1907 |
this.toggle( !layout.deprecatedItemsFieldset.isEmpty() ); |
| 1908 |
}, [], $tmp ); |
| 1909 |
layout.deprecatedItemsFieldset.on( 'remove', function () { |
| 1910 |
this.toggle( !layout.deprecatedItemsFieldset.isEmpty() ); |
| 1911 |
}, [], $tmp ); |
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
const tmp = layout.loadFromQueryParams; |
| 1916 |
layout.loadFromQueryParams = null; |
| 1917 |
if ( $.isPlainObject( tmp ) ) { |
| 1918 |
layout.loadQueryParams( tmp ); |
| 1919 |
} else { |
| 1920 |
layout.updateTemplatedParameters(); |
| 1921 |
} |
| 1922 |
if ( layout.getSubpages().length > 0 ) { |
| 1923 |
ApiSandbox.updateUI( tmp ); |
| 1924 |
} else { |
| 1925 |
layout.apiCheckValid(); |
| 1926 |
} |
| 1927 |
} ).fail( ( code, detail ) => { |
| 1928 |
layout.$element.empty() |
| 1929 |
.append( |
| 1930 |
new OO.ui.LabelWidget( { |
| 1931 |
label: mw.msg( 'apisandbox-load-error', layout.apiModule, detail ), |
| 1932 |
classes: [ 'error' ] |
| 1933 |
} ).$element, |
| 1934 |
new OO.ui.ButtonWidget( { |
| 1935 |
label: mw.msg( 'apisandbox-retry' ) |
| 1936 |
} ).on( 'click', layout.loadParamInfo, [], layout ).$element |
| 1937 |
); |
| 1938 |
} ); |
| 1939 |
}; |
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
ApiSandbox.PageLayout.prototype.apiCheckValid = function () { |
| 1947 |
if ( this.paramInfo === null ) { |
| 1948 |
return []; |
| 1949 |
} else { |
| 1950 |
|
| 1951 |
const promises = $.map( this.widgets, ( widget ) => widget.apiCheckValid( suppressErrors ) ); |
| 1952 |
$.when( ...promises ).then( ( ...results ) => { |
| 1953 |
this.apiIsValid = results.indexOf( false ) === -1; |
| 1954 |
if ( this.getOutlineItem() ) { |
| 1955 |
this.getOutlineItem().setIcon( this.apiIsValid || suppressErrors ? null : 'alert' ); |
| 1956 |
this.getOutlineItem().setTitle( |
| 1957 |
this.apiIsValid || suppressErrors ? '' : mw.message( 'apisandbox-alert-page' ).plain() |
| 1958 |
); |
| 1959 |
} |
| 1960 |
} ); |
| 1961 |
return promises; |
| 1962 |
} |
| 1963 |
}; |
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
ApiSandbox.PageLayout.prototype.loadQueryParams = function ( params ) { |
| 1971 |
if ( this.paramInfo === null ) { |
| 1972 |
this.loadFromQueryParams = params; |
| 1973 |
} else { |
| 1974 |
|
| 1975 |
$.each( this.widgets, ( name, widget ) => { |
| 1976 |
const v = Object.prototype.hasOwnProperty.call( params, name ) ? params[ name ] : undefined; |
| 1977 |
widget.setApiValue( v ); |
| 1978 |
} ); |
| 1979 |
this.updateTemplatedParameters( params ); |
| 1980 |
} |
| 1981 |
}; |
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
ApiSandbox.PageLayout.prototype.getQueryParams = function ( params, displayParams, ajaxOptions ) { |
| 1992 |
|
| 1993 |
$.each( this.widgets, ( name, widget ) => { |
| 1994 |
let value = widget.getApiValue(); |
| 1995 |
if ( value !== undefined ) { |
| 1996 |
params[ name ] = value; |
| 1997 |
if ( typeof widget.getApiValueForDisplay === 'function' ) { |
| 1998 |
value = widget.getApiValueForDisplay(); |
| 1999 |
} |
| 2000 |
displayParams[ name ] = value; |
| 2001 |
if ( typeof widget.requiresFormData === 'function' && widget.requiresFormData() ) { |
| 2002 |
ajaxOptions.contentType = 'multipart/form-data'; |
| 2003 |
} |
| 2004 |
} |
| 2005 |
} ); |
| 2006 |
}; |
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
ApiSandbox.PageLayout.prototype.getSubpages = function () { |
| 2014 |
const ret = []; |
| 2015 |
|
| 2016 |
$.each( this.widgets, ( name, widget ) => { |
| 2017 |
if ( typeof widget.getSubmodules === 'function' ) { |
| 2018 |
widget.getSubmodules().forEach( ( submodule ) => { |
| 2019 |
ret.push( { |
| 2020 |
key: name + '=' + submodule.value, |
| 2021 |
path: submodule.path, |
| 2022 |
prefix: widget.paramInfo.submoduleparamprefix || '' |
| 2023 |
} ); |
| 2024 |
} ); |
| 2025 |
} |
| 2026 |
} ); |
| 2027 |
return ret; |
| 2028 |
}; |
| 2029 |
|
| 2030 |
$( ApiSandbox.init ); |
| 2031 |
|
| 2032 |
module.exports = ApiSandbox; |
| 2033 |
|
| 2034 |
}() ); |