MediaWiki:Common.js

From cm2.liecourt.com
Revision as of 14:45, 15 May 2020 by Maintenance script (talk | contribs) (Fill database)

Note: After saving, 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: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Any JavaScript here will be loaded for all users on every page load. */

$( document ).ready(function() {


$('body.action-edit #wpTextbox1, body.action-submit #wpTextbox1').keydown(function(event)  {
    var x = event.keyCode;
    if (x == 13 ) {
        if(!event.shiftKey){
           var insert =  document.execCommand('insertText', false, '<!-- \n -->');
           if(insert){
               event.preventDefault();
                $('#wpTextbox1').focus()
                insert
            }
        }
    }
})




// Start of esc save
	
	
	
  $('body.action-edit, body.action-submit').keydown(function()  {
    var x = event.keyCode;
    if (x == 27) {
      $.ajax({
        url :'/api.php?action=query&meta=tokens&format=json',
        type: 'GET',
        dataType: 'json',
        success : function(result){
          var token = result.query.tokens.csrftoken;

          $("input[name='wpEditToken']").attr("value", token );

          saveEdit();

        }
      });
    }else{
      mw.confirmCloseWindow();
      $('#wpSave, #wpPreview, #wpDiff').on('click', function(){
        $( window ).off( 'beforeunload' );
      })
    }

  })

if($('body').hasClass('action-submit')){
  $('#top').prepend('<button class="btn btn-success enable-live-mode">Enable live mode</button>');
  $(document).on('click','.enable-live-mode', function(){
    if($(this).hasClass('btn-success')){
     liveMode();
     $(this).removeClass('btn-success');
     $(this).text('Live mode is on');
   }else{
      $(this).addClass('btn-success');
      $('body.action-submit textarea').off("change keyup paste");
      $(this).text('Enable live mode');
   }

  });
}
});

window.saveEdit = function() {
  $.ajax({
    url : $('form.mw-editform').attr('action'),
    type : 'POST',
    data :$('form.mw-editform').serialize(),
    success : function(html) {
     $( window ).off( 'beforeunload' );
      mw.notify( 'Saved' );
      if($('body').hasClass('action-submit')){
       var parser = new DOMParser();
       var doc = parser.parseFromString(html, "text/html");
       var elem = doc.querySelectorAll('.mw-content-ltr')[0];
      $('.mw-content-ltr').html(elem);
      }
    }
  })
}; 

window.liveMode = function() {
  var oldVal = "";
  $('body.action-submit textarea').on("change keyup paste", function() {
    var currentVal = $(this).val();
    if(currentVal == oldVal) {
        return; //check to prevent multiple simultaneous triggers
    }

    oldVal = currentVal;
    var text = encodeURIComponent($('textarea').val());
    
    $.ajax({
      url: '/api.php?action=parse&format=json&formatversion=2&title=New&text='+text+'&pst=&prop=text%7Cmodules%7Cjsconfigvars&preview=true&disableeditsection=true&uselang=en',
      dataType: 'json',
      success: function (x) {
        $('#wikiPreview').html(x.parse.text);
      } 
    });
  });
};
/* End of Esc Save */


/***** WSForm: loading Select2 ****/
$( document ).ready(function() {
  if ($('select[data-inputtype="ws-select2"]')[0]) {
    mw.loader.load('/extensions/WSForm/select2.min.css', 'text/css');
    $.getScript('/extensions/WSForm/select2.min.js').done(function() {
      $('select[data-inputtype="ws-select2"]').each(function() {
        var selectid = $(this).attr('id');
        var selectoptionsid = 'select2options-' + selectid;
        var select2config = $("input#" + selectoptionsid).val();
        var F = new Function(select2config);
        return (F());
      });
    });
  }
});
/* End of WSForm: Select2 */