r109226 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109225‎ | r109226 | r109227 >
Date:22:00, 17 January 2012
Author:maxsem
Status:ok
Tags:
Comment:
Bug 32739 - use the examples in ApiSandbox. UI still sucks, generators aren't supported.
Modified paths:
  • /trunk/extensions/ApiSandbox/ApiSandbox.i18n.php (modified) (history)
  • /trunk/extensions/ApiSandbox/ApiSandbox.php (modified) (history)
  • /trunk/extensions/ApiSandbox/SpecialApiSandbox.php (modified) (history)
  • /trunk/extensions/ApiSandbox/ext.apiSandbox.css (modified) (history)
  • /trunk/extensions/ApiSandbox/ext.apiSandbox.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ApiSandbox/SpecialApiSandbox.php
@@ -83,7 +83,7 @@
8484 ';
8585 $s .= '<tr><td class="api-sandbox-label"><label for="api-sandbox-format">format=</label></td><td class="api-sandbox-value">'
8686 . self::getSelect( 'format', $formats, 'json' )
87 - . '</td><td></td></tr>
 87+ . '</td><td>' . $this->getButtonsBox() . '</td></tr>
8888 ';
8989 $s .= '<tr><td class="api-sandbox-label"><label for="api-sandbox-action">action=</label></td><td class="api-sandbox-value">'
9090 . self::getSelect( 'action', $modules )
@@ -113,6 +113,10 @@
114114 return $s;
115115 }
116116
 117+ public function getButtonsBox() {
 118+ return '<div id="api-sandbox-buttons"><div id="api-sandbox-examples" style="display: none"></div></div>';
 119+ }
 120+
117121 /**
118122 * @param $type string
119123 * @return array
Index: trunk/extensions/ApiSandbox/ext.apiSandbox.js
@@ -6,6 +6,7 @@
77 * @param $container {jQuery} Container to put UI into
88 * @param info {Object} Query information
99 * @param prefix {String} Additional prefix for parameter names
 10+ * @param params {Object} Optional override for info.parameters
1011 */
1112 function UiBuilder( $container, info, prefix, params ) {
1213 this.$container = $container;
@@ -13,6 +14,9 @@
1415 this.prefix = prefix + info.prefix;
1516 this.params = isset( params ) ? params : info.parameters;
1617
 18+ $container.addClass( 'api-sandbox-builder' );
 19+ $container.data( 'builder', this );
 20+
1721 this.createInputs();
1822 }
1923
@@ -167,7 +171,16 @@
168172 $requestUrl = $( '#api-sandbox-url' ),
169173 $requestPost = $( '#api-sandbox-post' ),
170174 $output = $( '#api-sandbox-output' ),
171 - $postRow = $( '#api-sandbox-post-row' );
 175+ $postRow = $( '#api-sandbox-post-row' ),
 176+ $buttonsContainer = $( '#api-sandbox-buttons' ),
 177+ $examplesButton = $( '<button></button>' )
 178+ .click( function( e ) {
 179+ e.preventDefault();
 180+ $examplesContent.slideToggle();
 181+ } )
 182+ .hide()
 183+ .appendTo( $buttonsContainer ),
 184+ $examplesContent = $( '#api-sandbox-examples' );
172185
173186 // UiBuilder objects
174187 var mainRequest,
@@ -179,6 +192,14 @@
180193 var paramInfo = { modules: {}, querymodules: {} },
181194 namespaces = [];
182195
 196+ $( '<button></button>' )
 197+ .text(mw.msg( 'apisb-clear' ) )
 198+ .click( function( e ) {
 199+ e.preventDefault();
 200+ resetUI();
 201+ } )
 202+ .insertAfter( $examplesButton );
 203+
183204 // load namespaces
184205 $.getJSON( mw.util.wikiScript( 'api' ),
185206 { format: 'json', action: 'query', meta: 'siteinfo', siprop: 'namespaces' },
@@ -284,7 +305,7 @@
285306 }
286307 $output.html( data );
287308 },
288 - error: function( jqXHR, textStatus, errorThrown ) {
 309+ error: function() {
289310 showLoadError( $output, 'apisb-request-error' );
290311 }
291312 };
@@ -373,6 +394,7 @@
374395 function() {
375396 showLoading( $mainContainer );
376397 $submit.attr( 'disabled', 'disabled' );
 398+ $examplesContent.hide();
377399 },
378400 function() {
379401 var info;
@@ -384,14 +406,118 @@
385407 mainRequest = new UiBuilder( $mainContainer, info, '' );
386408 mainRequest.setHelp( $help );
387409 $submit.removeAttr( 'disabled' );
 410+ updateExamples( info );
388411 },
389412 function() {
390413 $submit.removeAttr( 'disabled' );
391414 showLoadError( $mainContainer, 'apisb-load-error' );
 415+ $examplesContent.hide();
392416 }
393417 );
394418 }
395419
 420+ function updateExamples( info ) {
 421+ if ( !isset( info.allexamples ) ) {
 422+ $examplesContainer.hide(); // just in case
 423+ return;
 424+ }
 425+ $examplesContent.hide()
 426+ .html( '' );
 427+ var $list = $( '<ul></ul>' );
 428+ var urlRegex = /api.php\?\S+/m;
 429+ var count = 0;
 430+ for ( var i = 0; i < info.allexamples.length; i++ ) {
 431+ var href = '';
 432+ var text = '';
 433+ while ( i < info.allexamples.length && !isset( info.allexamples[i].description ) ) {
 434+ var match = urlRegex.exec( info.allexamples[i]['*'] );
 435+ if ( match ) {
 436+ href = match[0];
 437+ break;
 438+ } else {
 439+ text += '\n' + info.allexamples[i]['*'];
 440+ }
 441+ i++;
 442+ }
 443+ if ( !href ) {
 444+ href = info.allexamples[i]['*'];
 445+ }
 446+ if ( !text ) {
 447+ text = isset( info.allexamples[i].description ) ? info.allexamples[i].description : href;
 448+ }
 449+ var prefix = text.replace( /[^\n]*$/, '' );
 450+ var $prefix = prefix.length ? $( '<b>' ).text( prefix ) : [];
 451+ var linkText = text.replace( /^.*\n/, '' );
 452+ $( '<li>' ).append( $prefix )
 453+ .append( $( '<a/>' )
 454+ .attr( 'href', href )
 455+ .text( linkText )
 456+ .click( exampleClick )
 457+ ).appendTo( $list );
 458+ count++;
 459+ }
 460+ $examplesButton.text( mw.msg( count == 1 ? 'apisb-example' : 'apisb-examples' ) );
 461+ $list.appendTo( $examplesContent );
 462+ if ( count ) {
 463+ $examplesButton.show();
 464+ } else {
 465+ $examplesButton.hide();
 466+ }
 467+ }
 468+
 469+ function exampleClick( e ) {
 470+ e.preventDefault();
 471+
 472+ resetUI();
 473+ var link = $( this ).attr( 'href' ).replace( /^.*?\?/, '' );
 474+ var params = link.split( '&' );
 475+ for ( var i = 0; i < params.length; i++ ) {
 476+ var pieces = params[i].split( '=' );
 477+ if ( pieces.length == 1 ) { // checkbox
 478+ $( '#param-' + pieces[0] ).attr( 'checked', 'checked' );
 479+ } else {
 480+ var key = pieces[0],
 481+ value = decodeURIComponent( pieces.slice( 1 ).join( '=' ) );
 482+ if ( [ 'action', 'format', 'list', 'prop', 'meta' ].indexOf( key ) != -1 ) {
 483+ continue;
 484+ }
 485+ var $el = $( '#param-' + key );
 486+ if ( !$el.length ) {
 487+ continue;
 488+ }
 489+ switch ( $el[0].nodeName ) {
 490+ case 'SELECT':
 491+ if ( $el.attr( 'multiple' ) ) {
 492+ var splitted = value.split( '|' );
 493+ for ( var j = 0; j < splitted.length; j++ ) {
 494+ $el.children( 'option[value=' + mw.html.escape( splitted[j] ) + ']' )
 495+ .attr( 'selected', 'selected' );
 496+ }
 497+ } else {
 498+ $el.children( 'option[value=' + mw.html.escape( value ) + ']' )
 499+ .attr( 'selected', 'selected' );
 500+ }
 501+ break;
 502+ case 'INPUT':
 503+ if ( $el.attr( 'type' ) == 'checkbox' ) {
 504+ $( '#param-' + key ).attr( 'checked', 'checked' );
 505+ } else {
 506+ $el.val( value );
 507+ }
 508+ break;
 509+ default:
 510+ continue;
 511+ }
 512+ }
 513+ }
 514+ }
 515+
 516+ function resetUI() {
 517+ $( '.api-sandbox-builder' ).each( function() {
 518+ $( this ).data( 'builder' ).createInputs();
 519+ } );
 520+ }
 521+
396522 /**
397523 * HTML-escapes and pretty-formats an API description string
398524 *
Index: trunk/extensions/ApiSandbox/ApiSandbox.i18n.php
@@ -33,6 +33,9 @@
3434 'apisb-query-meta' => 'Meta information',
3535 'apisb-generic-parameters'=> 'Generic parameters',
3636 'apisb-generator-parameters'=> 'Generator',
 37+ 'apisb-example' => 'Example',
 38+ 'apisb-examples' => 'Examples',
 39+ 'apisb-clear' => 'Clear',
3740 );
3841
3942 /** Message documentation (Message documentation)
Index: trunk/extensions/ApiSandbox/ApiSandbox.php
@@ -33,6 +33,9 @@
3434 'apisb-select-value',
3535 'apisb-namespaces-error',
3636 'apisb-ns-main',
 37+ 'apisb-example',
 38+ 'apisb-examples',
 39+ 'apisb-clear',
3740 'parentheses',
3841 ),
3942 'dependencies' => 'mediawiki.util',
Index: trunk/extensions/ApiSandbox/ext.apiSandbox.css
@@ -43,3 +43,15 @@
4444 text-align: left !important;
4545 font-size: 100% !important;
4646 }
 47+
 48+.api-sandbox-examples-outer {
 49+
 50+}
 51+
 52+#api-sandbox-examples-button {
 53+ -webkit-border-radius: 4px;
 54+ -moz-border-radius: 4px;
 55+ border-radius: 4px;
 56+
 57+ border: 1px solid #2F6FAB;
 58+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r109232Follow-up r109226: undo styles from a previous designmaxsem22:16, 17 January 2012

Status & tagging log