Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Gadget-paragraphfinder.js: Difference between revisions

MediaWiki interface page
Create gadget
(No difference)

Revision as of 06:29, 22 July 2025

( function () {
  // integration with VE and the cite dialog
  mw.hook( 've.startup' ).add( function () {
    mw.hook( 've.ui.MWCitationDialog.dialogReady' ).add( function ( dialog ) {
      var surfaceModel = dialog.getSurface().getModel();
      var store = dialog.getModel();

      if ( store.get( 'schema' ) !== 'Cite book' ) return;

      // Prevent default content panel
      dialog.content.clear();

      // Build OOUI form
      var bookSelect = new OO.ui.DropdownInputWidget( {
        options: mw.config.get( 'wgWingsofFireBooks' ).map( function ( b ) {
          return { data: b, label: b.replace( /_/g, ' ' ) };
        } ),
        required: true
      } );
      var chapterSelect = new OO.ui.DropdownInputWidget( {
        required: true
      } );
      var quoteInput = new OO.ui.MultilineTextInputWidget( {
        placeholder: 'Paste the full paragraph here',
        required: true
      } );
      var findBtn = new OO.ui.ButtonWidget( {
        label: 'Find paragraph',
        flags: [ 'primary' ]
      } );

      // When book changes, load chapters
      bookSelect.on( 'change', function ( val ) {
        if ( val ) {
          mw.loader.using( 'mediawiki.api' ).done( function () {
            ( new mw.Api() ).get( {
              action: 'json',
              format: 'json',
              url: mw.config.get( 'wgScriptPath' ) + '/find_paragraph/books/' + encodeURIComponent( val ) + '/chapters'
            } ).done( function ( res ) {
              var chapters = res.available_chapters || [];
              chapterSelect.clearOptions();
              chapters.forEach( function ( c ) {
                chapterSelect.addOptions( [ { data: c, label: c } ] );
              } );
            } );
          } );
        }
      } );

      findBtn.on( 'click', function () {
        var book = bookSelect.getValue(),
            chapter = chapterSelect.getValue(),
            quote = quoteInput.getValue();
        if ( !book || !chapter || !quote ) {
          alert( 'Please select a book, chapter and paste the full paragraph.' );
          return;
        }
        findBtn.setDisabled( true );
        new mw.Api().post( {
          url: mw.config.get( 'wgScriptPath' ) + '/find_paragraph',
          method: 'POST',
          data: JSON.stringify( { book_name: book, chapter: chapter, paragraph: quote } ),
          contentType: 'application/json'
        } ).done( function ( data ) {
          dialog.close(); // close mini form
          // Open normal cite dialog
          var citationDialog = new ve.ui.MWCitationDialog();
          citationDialog.open();
          // Pre-fill on open
          citationDialog.getModel().once( 'change:paragraph', function () {
            var model = citationDialog.getModel();
            model.set( {
              book: book,
              chapter: chapter,
              paragraph: String( data.index ),
              quote: data.match
            } );
          } );
        } ).fail( function ( xhr ) {
          alert( xhr.responseJSON && xhr.responseJSON.reason || 'No match found.' );
        } ).always( function () {
          findBtn.setDisabled( false );
        } );
      } );

      // Assemble and display the form
      var panel = new OO.ui.PanelLayout( {
        expanded: false,
        padded: true,
        framed: true
      } );
      panel.$element.append(
        new OO.ui.FieldLayout( bookSelect, { label: 'Book', align: 'top' } ).$element,
        new OO.ui.FieldLayout( chapterSelect, { label: 'Chapter', align: 'top' } ).$element,
        new OO.ui.FieldLayout( quoteInput, { label: 'Paragraph text', align: 'top' } ).$element,
        findBtn.$element
      );
      dialog.content.add( panel );
      bookSelect.focus();
    } );
  } );

  // Preload book list on page load
  mw.loader.using( 'mediawiki.api' ).then( function () {
    new mw.Api().get( {
      url: mw.config.get( 'wgScriptPath' ) + '/find_paragraph/books'
    } ).done( function ( res ) {
      mw.config.set( 'wgWingsofFireBooks', res.available_books );
    } );
  } );
}() );
Cookies help us deliver our services. By using our services, you agree to our use of cookies.