[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/extensions/WikiEditor/modules/ -> jquery.wikiEditor.preview.js (source)

   1  /* Preview module for wikiEditor */
   2  ( function ( $, mw ) {
   3  /*jshint onevar:false */
   4  $.wikiEditor.modules.preview = {
   5  
   6  /**
   7   * Compatability map
   8   */
   9  browsers: {
  10      // Left-to-right languages
  11      ltr: {
  12          msie: [['>=', 7]],
  13          firefox: [['>=', 3]],
  14          opera: [['>=', 9.6]],
  15          safari: [['>=', 4]]
  16      },
  17      // Right-to-left languages
  18      rtl: {
  19          msie: [['>=', 8]],
  20          firefox: [['>=', 3]],
  21          opera: [['>=', 9.6]],
  22          safari: [['>=', 4]]
  23      }
  24  },
  25  
  26  /**
  27   * Internally used functions
  28   */
  29  fn: {
  30      /**
  31       * Creates a preview module within a wikiEditor
  32       * @param context Context object of editor to create module in
  33       * @param config Configuration object to create module from
  34       */
  35      create: function ( context ) {
  36          if ( 'initialized' in context.modules.preview ) {
  37              return;
  38          }
  39          context.modules.preview = {
  40              'initialized': true,
  41              'previewText': null,
  42              'changesText': null
  43          };
  44          context.modules.preview.$preview = context.fn.addView( {
  45              'name': 'preview',
  46              'titleMsg': 'wikieditor-preview-tab',
  47              'init': function ( context ) {
  48                  // Gets the latest copy of the wikitext
  49                  var wikitext = context.$textarea.textSelection( 'getContents' );
  50                  // Aborts when nothing has changed since the last preview
  51                  if ( context.modules.preview.previewText === wikitext ) {
  52                      return;
  53                  }
  54                  context.modules.preview.$preview.find( '.wikiEditor-preview-contents' ).empty();
  55                  context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).show();
  56                  $.ajax( {
  57                      url: mw.util.wikiScript( 'api' ),
  58                      type: 'POST',
  59                      dataType: 'json',
  60                      data: {
  61                          format: 'json',
  62                          action: 'parse',
  63                          title: mw.config.get( 'wgPageName' ),
  64                          text: wikitext,
  65                          prop: 'text|modules',
  66                          pst: ''
  67                      }
  68                  } ).done( function ( data ) {
  69                      if ( !data.parse || !data.parse.text || data.parse.text['*'] === undefined ) {
  70                          return;
  71                      }
  72  
  73                      context.modules.preview.previewText = wikitext;
  74                      context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).hide();
  75                      context.modules.preview.$preview.find( '.wikiEditor-preview-contents' )
  76                          .html( data.parse.text['*'] )
  77                          .find( 'a:not([href^=#])' )
  78                              .click( false );
  79  
  80                      var loadmodules = data.parse.modules.concat(
  81                          data.parse.modulescripts,
  82                          data.parse.modulestyles,
  83                          data.parse.modulemessages
  84                      );
  85                      mw.loader.load( loadmodules );
  86                  } );
  87              }
  88          } );
  89  
  90          context.$changesTab = context.fn.addView( {
  91              'name': 'changes',
  92              'titleMsg': 'wikieditor-preview-changes-tab',
  93              'init': function ( context ) {
  94                  // Gets the latest copy of the wikitext
  95                  var wikitext = context.$textarea.textSelection( 'getContents' );
  96                  // Aborts when nothing has changed since the last time
  97                  if ( context.modules.preview.changesText === wikitext ) {
  98                      return;
  99                  }
 100                  context.$changesTab.find( 'table.diff tbody' ).empty();
 101                  context.$changesTab.find( '.wikiEditor-preview-loading' ).show();
 102  
 103                  // Call the API. First PST the input, then diff it
 104                  $.ajax( {
 105                      url: mw.util.wikiScript( 'api' ),
 106                      type: 'POST',
 107                      dataType: 'json',
 108                      data: {
 109                          format: 'json',
 110                          action: 'parse',
 111                          title: mw.config.get( 'wgPageName' ),
 112                          onlypst: '',
 113                          text: wikitext
 114                      }
 115                  } ).done( function ( data ) {
 116                      try {
 117                          var postdata2 = {
 118                              format: 'json',
 119                              action: 'query',
 120                              indexpageids: '',
 121                              prop: 'revisions',
 122                              titles: mw.config.get( 'wgPageName' ),
 123                              rvdifftotext: data.parse.text['*'],
 124                              rvprop: ''
 125                          };
 126                          var section = $( '[name="wpSection"]' ).val();
 127                          if ( section !== '' ) {
 128                              postdata2.rvsection = section;
 129                          }
 130  
 131                          $.ajax( {
 132                              url: mw.util.wikiScript( 'api' ),
 133                              type: 'POST',
 134                              dataType: 'json',
 135                              data: postdata2
 136                          } ).done( function ( data ) {
 137                              // Add diff CSS
 138                              mw.loader.load( 'mediawiki.action.history.diff' );
 139                              try {
 140                                  var diff = data.query.pages[data.query.pageids[0]]
 141                                      .revisions[0].diff['*'];
 142  
 143                                  context.$changesTab.find( 'table.diff tbody' ).html( diff );
 144                                  context.modules.preview.changesText = wikitext;
 145                              } catch ( e ) {
 146                                  // "data.blah is undefined" error, ignore
 147                              }
 148                              context.$changesTab.find( '.wikiEditor-preview-loading' ).hide();
 149                          } );
 150                      } catch ( e ) {
 151                          // "data.blah is undefined" error, ignore
 152                      }
 153                  } );
 154              }
 155          } );
 156  
 157          var loadingMsg = mw.msg( 'wikieditor-preview-loading' );
 158          context.modules.preview.$preview
 159              .add( context.$changesTab )
 160              .append( $( '<div>' )
 161                  .addClass( 'wikiEditor-preview-loading' )
 162                  .append( $( '<img>' )
 163                      .addClass( 'wikiEditor-preview-spinner' )
 164                      .attr( {
 165                          'src': $.wikiEditor.imgPath + 'dialogs/loading.gif',
 166                          'valign': 'absmiddle',
 167                          'alt': loadingMsg,
 168                          'title': loadingMsg
 169                      } )
 170                  )
 171                  .append(
 172                      $( '<span>' ).text( loadingMsg )
 173                  )
 174              )
 175              .append( $( '<div>' )
 176                  .addClass( 'wikiEditor-preview-contents' )
 177              );
 178          context.$changesTab.find( '.wikiEditor-preview-contents' )
 179              .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
 180                  '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' );
 181      }
 182  }
 183  
 184  };
 185  
 186  }( jQuery, mediaWiki ) );


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1