Jump to content

User:Zenexer/test-mode.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
/*globals api, escapeQuotesHTML, addPortletLink, importScriptURI, wgPageName, addOnloadHook, wgNamespaceNumber, sortables_init,   createCollapseButtons, createNavigationBarToggleButton, window, wgAction, ajaxPreviewExec  */

// Script to re-render page with context title 'test<some number>-'. See {{tl|test-mode}} for why this is useful.
// Script by Bawolff, feel free to ask if you have any questions.
// Some minor changes by Zenexer.  Original is at [[User:Bawolff/test-mode.js]].
//
// To install add importScript('User:Zenexer/test-mode.js') to [[Special:MyPage/monobook.js]]
// (replace monobook with skin if you use different skin).
//
// Known issue: <source> tags don't work (since they add css to head, which doesn't get picked up). Perhaps fixed on next wmf update.
// Only works on vector and monobook. YMMV on other skins. [Zenexer: This might not be true anymore.  Untested.]
// Could have better support for edit mode.

//-------------------

mw.loader.load('//en.wikinews.org/w/index.php?title=User:Bawolff/mwapilib2.js&action=raw&ctype=text/javascript&smaxage=259200');

var testMode = {
 init: function () {
  if (wgNamespaceNumber === -1) return; //no special

  var editing = false;
  if ((wgAction === 'submit' || wgAction === 'edit')) {
   editing = true;

   //add show preview with test mode.
   var button = document.createElement('button');
   button.type = 'button';
   button.id = 'testModePreviewButton';
   button.onclick = function () { testMode.exec(true); };
   button.appendChild(document.createTextNode('Preview in testing mode'));
   var prev = document.getElementById('wpDiff');
   prev.parentNode.insertBefore(button, prev);
  }

  mw.util.addPortletLink('p-tb', 'javascript:testMode.exec(' + editing + ')%3Bvoid%200', 'Display in test mode', 'testModePreviewSidebarLink', 'View page using alternative templates that are being tested. See [[template:Test-mode]] for details.');


 },
 exec: function (editing) {
  var testNum = prompt('Which test mode do you wish to use? (1, 2 or 3)', '1');
  switch (testNum) {
    case 1:
    case 2:
    case 3:
      break;
    
    default:
      testNum = 1;
      break;
  }

  var page;
  if (editing) {
    page = document.getElementById('wpTextbox1').value; //use edit box
  } else {
    page = '{{:' + wgPageName + '}}';
  }
  
  Bawolff.APIC.api(page).parse('Test' + testNum + '-').lift(this.display, testNum, editing).exec();
 },
 display: function (text, mode, editing) {
  var body = document.getElementById('wikiPreview');
  body = body ? body : document.getElementById('bodyContent');
  body.style.display = 'block';
  var titleMsg = '<br/><small>(Testing mode ' + escapeQuotesHTML(mode) + '. &lt;source&gt; tags and anything else that adds dynamic css/js may not display properly.';
  if (!editing) titleMsg += ' <a onclick="location.reload(); return false;" href="#">Reload</a> for normal view.';
  titleMsg += ')</small>';
  var warn = document.getElementById('testModeWarning');
  if (warn) {
   warn.innerHTML = titleMsg;
  } else {
   var title = document.getElementById('firstHeading');
   title.innerHTML += '<span id="testModeWarning">' + titleMsg + '</span>';
  }
  body.innerHTML = text;

  //call various js hooks (stolen from [[user:js/ajaxPreview]])
  sortables_init();
  if (window.createCollapseButtons){//en.wp
   createCollapseButtons();
   createNavigationBarToggleButton();
  } 
  if (window.ajaxPreviewExec) ajaxPreviewExec(body);

 }
}

$(testMode.init);