Jump to content

MediaWiki:Gadget-commentr.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*  _____________________________________________________________________________
 * |                                                                             |
 * |                    === WARNING: GLOBAL GADGET FILE ===                      |
 * |                  Changes to this page affect many users.                    |
 * | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
 * |_____________________________________________________________________________|
 *
 * Quick comments form, intended for use on help pages
 * Based on Teahouse "Ask a question" feature ([[MediaWiki:Gadget-teahouse.js]]) by Andrew Garrett
 */
//<nowiki>
(function($,mw) {
  mw.commentr = {
    addComment : function( sourcePage, text, targetPage ) {
      var editSummary = "New comment from [[" + sourcePage + "]]";
      var wikitext = "\n\n== Comment from [[" + sourcePage + "]] ==\n" + text;
      
      if ( (/~~~~\s*$/).test(wikitext) ) {
        wikitext += " \n"; // if they signed already
      } else {
        wikitext += " -- ~~~~ \n"; // else add a sig for them
      }

      $('#wp-commentr-form-save').button({disabled : true });
      $('#wp-commentr-form-save .ui-button-text').html('Saving...');

      $('#wp-commentr-link')
        .find('.selflink')
        .empty()
        .addClass('mw-ajax-loader');

      var api = new mw.Api();

      api.get( {
        'action' : 'query',
        'titles' : targetPage,
        'prop'   : 'revisions',
        'meta': 'tokens',
        'type'   : 'csrf',
        'rvprop' : 'content',
        'indexpageids' : 1
      }, {
        'ok' : function(result) {
          result = result.query;
          var page = result.pages[result.pageids[0]];
          var oldText = page.revisions[0]['*'];
          var newText = oldText + wikitext;
          
          api.post(
            {
              'action'  : 'edit',
              'title'   : targetPage,
              'text'    : newText,
              'summary' : editSummary,
              'token'   : result.tokens.csrftoken
            },
            {
              'ok' : function() {window.location.reload();}
            }
          );
        }
      });
    }
  };

  $(function() {
    mw.loader.using( ['jquery.ui', 'mediawiki.api'], function() {
      if ( !$('#wp-commentr-link').length ) { // if there's no link on the page, we're done here.
        return;
      }

      var $link = $('#wp-commentr-link');      
      var $form = $('#wp-commentr-form');
      var $saveButton = $form.find('#wp-commentr-form-save');
      
      var targetPage = $form.find('#wp-commentr-form-targetpage').html();
      
      // Add a real textarea, and remove the placeholder
      var $textArea = $('<textarea rows="10" cols="20" id="wp-commentr-form-textarea"></textarea>');
      $('#wp-commentr-form-textplaceholder').after($textArea).remove();

      // Prevent flash
      $form.css( 'left', '-10000px' );

      // Set up position
      setTimeout( function() {
        var pos = $link.position();
        var hCenter = ( $link.parent().width() / 2 );
        $form.css( 'top', pos.top + $link.height() + 'px' );
        $form.css( 'left', (hCenter - ($form.width()) / 2) + 'px' );
        $form.hide();
      }, 0);

      $saveButton
        .button({
          disabled : true // disable until they type something
        })
        .click( function(e) {
          e.preventDefault();

          var sourcePage = mw.config.get('wgPageName');
          var text = $textArea.val();

          if ( text ) {  // only submit if something there
            mw.commentr.addComment( sourcePage, text, targetPage );
          }
        })
    
      $textArea.keypress( function(e) {
        var $textbox = $(this);
        setTimeout( function() {
          if ( $textbox.val() !== '' ) {
            $saveButton.button( 'option','disabled', false );
          } else {
            $saveButton.button( 'option','disabled', true );
          }
        }, 0 );
      } );

      $link.click(function(e) {
        $form.toggle('fast');
        e.cancelBubble = true; // for IE
        if (e.stopPropagation) {
          e.stopPropagation();
          e.preventDefault();
        }
      });

      $(document).click( function(e) {
        var $target = $(e.target);
        if ( ! $target.is('#wp-commentr-form *') && ! $target.is('#wp-commentr-link *') ) {
          $form.fadeOut();
        }
      } );

      $(document).keydown( function(e) {
        if ( e.keyCode === 27 ) { // ESC
          $form.fadeOut();
        }
      });
    } );
  } );
} )(jQuery,mediaWiki);
//</nowiki>