MediaWiki:Gadget-dataitemsummary.js

From OpenStreetMap Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*
 * Prompts user to enter an optional edit summary when changing claims on a data item
 *
 * @author [[User:Yurik]] based on the code by Wikipedia's [[User:Ricordisamoa]]
*/
( function( $, mw, window ) {
  $( function () {
    if( !mw.config.exists( 'wbEntityId' ) ) {
      return;
    }
    mw.hook('wikibase.entityPage.entityView.rendered').add(function () {
      var oldPost = wb.api.RepoApi.prototype._post;
      var summary = '';

      wb.api.RepoApi.prototype._post = function ( params ) {
        try {
          if ( params.summary === undefined
            // && params.action !== "wbsetaliases"
            // && params.action !== "wbsetlabel"
            // && params.action !== "wbsetdescription"
          ) {
            var type;
            switch (params.action) {
              case 'wbsetaliases': type = 'alias '; break;
              case 'wbsetlabel': type = 'label '; break;
              case 'wbsetdescription': type = 'description '; break;
              default: type = ''; break;
            }
            var newSummary = window.prompt('Enter ' + type + 'edit summary (optional):', summary);
            if (newSummary !== null) {
              summary = newSummary;
              if ( newSummary ) {
                params.summary = newSummary;
              }
            }
          }
        } catch (err) {
          console.error(err);
        }
        return oldPost.call( this, params );
      };
    } );
  } );
} )( jQuery, mediaWiki, window );