MediaWiki:Common.js/Wikinews:Article principal

/*

This javascript deals with stuff specific to [[User:Bawolff/sandbox/makeLead]]. Functions
specific to lead generation are at User:Bawolff/sanbox/leadGenerator.js
functions relating to api access are at User:Bawolff/mwapilib2.js
*/
importScript('Mediawiki:Common.js/Wikinews:Article_principal/leadGenerator');

var showPreview = function (html) {
 //We assume that the mediawiki api is not malicious...
 document.getElementById('LeadPreview').innerHTML = html;
}
var showLead = function (leadText, leadTitle) {
 var lsp = document.getElementById('leadSourcePrev');
 if (!lsp) {
  var src = document.createElement('textarea');
  src.id = 'leadSourcePrev';
  src.style.width = '48%';
  src.rows = 40;
  src.appendChild(document.createTextNode(leadText));
  document.getElementById('LeadBox').appendChild(src);
  document.getElementById('LeadPreview').style.visibility = 'visible';
 }
 else {
  lsp.value = leadText;
 }
 leadTitle = leadTitle.replace(/(\\|')/g, "\\$1").replace(/"/g, """).replace(/>/g, '>'); //prevent xss
 var leadNumb = document.getElementById('leadNum').selectedIndex + 1;
 document.getElementById('LeadControls').innerHTML = '<button onclick="syncToServer(' + leadNumb + ', \'' + leadTitle + '\')">Enregistrer l\'article principal ' + leadNumb + '</button> <button onclick="regeneratePreview()">Rafraîchir la prévisualisation</button>';

 api(leadText).replace(/<noinclude>[\d\D]*?<\/noinclude>/, '').parse('Main Page').lift(showPreview).exec();
 //this will strip out anything after a noinculde (this is kind of dirty)

}
var regeneratePreview = function() {
var wikiText = document.getElementById('leadSourcePrev').value;
api(wikiText).replace(/<noinclude>[\d\D]*?<\/noinclude>/, '').parse('Main Page').lift(showPreview).exec();
}
var syncToServer = function(leadNum, page) {
 if (leadNum === undefined || !confirm('Êtes-vous sûr de vouloir sauvegarder ceci sur Modèle:Une ' + leadNum + '?')) {
  return false;
 }
 api(document.getElementById('leadSourcePrev').value).setDefaultSummary('[[Wikinews:Article principal|AP]] : Actualisation de l\'article principal ' + leadNum + ' : [[' + page + ']]').savePage('Modèle:Une ' + leadNum).inject('Article principal correctement mis à jour').lift(alert).lift(function() {location = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', mw.config.get('wgPageName')) + '?js-good-update=' + encodeURIComponent(page);}).exec();
}
var startLeadMaker = function () {
 if (mw.config.get('wgAction') !== 'view' && mw.config.get('wgAction') !== 'purge') {
  return false;
 }
 if (!window.api || !window.Bawolff || !window.Bawolff.leadGen) {
  alert('Javascript ne s\'est pas chargé correctement, comme résultat que cette page peut ou ne peut fonctionner. Si ça ne fonctionne pas, essayer d\'actualiser la page.');
 }
 if (!mw.config.get('wgUserGroups') || mw.config.get('wgUserGroups').join(' ').indexOf("autoconfirmed") === -1) {
  jsMsg("<div style='border:solid red medium;font-weight: bold'>Attention : Vous ne pouvez pas modifier les articles principaux, seulement les personnes qui ont un compte enregistré depuis plus de quatre jours peuvent les modifier.</div>");
 }
 var lastUpdate = location.search.match(/[?&]js-good-update=([^&]*).*/);
 if (lastUpdate) {
  //this is all properly escaped since its in a text node.
  window.setTimeout(function () {jsMsg(document.createTextNode('Article principal a été actualisé avec «' + decodeURIComponent(lastUpdate[1]) + ' ».'));}, 400); //to get arround other things using jsMSg
 }
 var setupContainer = document.getElementById('lSetup');
 setupContainer.innerHTML += "<form id='leadForm' action='javascript:doMakeLead();void 0;'><label for='pageNameField'>Page&nbsp;:</label> <input type='text' id='pageNameField' size='50'><br/><label for='leadNum'>Article&nbsp;principal&nbsp;:</label> <select id='leadNum' onchange='doMakeLead(true);'><option selected value='1'>1 (en haut)</option><option>2 (milieu)</option><option>3 (milieu 2)</option><option>4 (bas)</option> </select> <label for='sumMethod'>Méthode&nbsp;de&nbsp;résumé&nbsp;:</label> <select id='sumMethod'><option selected value='0'>Première phrase</option><option value='1'>Deux premières phrases</option><option value='2'>Premier paragraphe</option><option>250 premières lettres</option> <option>500 premières lettres</option> </select> &nbsp;<input type='submit' value='Aperçu' ></form>";

 var dpl = document.getElementById('lDPL').getElementsByTagName('a');
 for (var i = 0; i < dpl.length; i++) {
  linkVal = encodeURIComponent(dpl[i].firstChild.data.replace(/(\\|")/g, "\\$1"));
  // %22 = "
  dpl[i].href = 'javascript:document.getElementById(%22pageNameField%22).value=%22' + linkVal + '%22;doMakeLead();void%200';
 }
 os_initHandlers('pageNameField','leadForm', document.getElementById('pageNameField'));
 
 var handleAfterLeadInfoTableLoads = function (table) {
  document.getElementById('lMetaTable').innerHTML = table;
  var pageName = location.search.replace(/.*?[?&]use-page=([^&]*).*/, '$1');

  if (Bawolff.leadGen.oldestLead) {
   var oldLeadNum = parseInt(Bawolff.leadGen.oldestLead.charAt(Bawolff.leadGen.oldestLead.length - 1));
   document.getElementById('leadNum').selectedIndex = oldLeadNum - 1;
   if (oldLeadNum === 1) {
    document.getElementById('sumMethod').selectedIndex = 1; //use 2 sentences for longer lead.
   }
  }
  if (pageName !== location.search) { //if it matched
   document.getElementById('pageNameField').value = decodeURIComponent(pageName.replace(/\+/g, ' '));
   location.href = '#leadForm'; //since already filled out.
   doMakeLead();
  }
  
 }
 Bawolff.leadGen.makeLeadTable(handleAfterLeadInfoTableLoads);

}
function doMakeLead (fromLeadNumChange) {
var pageName = document.getElementById('pageNameField').value;
var leadNum = document.getElementById('leadNum').selectedIndex + 1;
 if (fromLeadNumChange && leadNum === 1) {
    document.getElementById('sumMethod').selectedIndex = 1; //use 2 sentences for longer lead.
 }
var sumMethod = document.getElementById('sumMethod').selectedIndex;
if (!pageName) return; //stop error if not initialized.
Bawolff.leadGen(pageName, leadNum, showLead, sumMethod);
}

waitAWhileThanLoadLead.times = 0;
function waitAWhileThanLoadLead () {
//since we're already from an onload event, and importScript is async. wait a while
 if ((!window.api || !window.Bawolff || !window.Bawolff.leadGen) && waitAWhileThanLoadLead.times < 10) {
  waitAWhileThanLoadLead.times++;
  setTimeout(waitAWhileThanLoadLead, 200);
 } else {
 startLeadMaker();
 }
}
$(waitAWhileThanLoadLead);
/*

*/