MediaWiki:Common.js: Difference between revisions

From BoyWiki
 
(test)
Line 5: Line 5:
function addlinkportletmenu() {
function addlinkportletmenu() {
   if (wgUserName != null)
   if (wgUserName != null)
   addPortletLink("p-navigation", "https://en.boywiki.org/wiki/BoyWiki:Agora", "Agora", "n-agora", "A place to discuss the administration and editing of BoyWiki");
   addPortletLink("p-navigation", "https://www.boywiki.org/en/BoyWiki:Agora", "Agora", "n-agora", "A place to discuss the administration and editing of BoyWiki");
}
}


addOnloadHook(addlinkportletmenu);
addOnloadHook(addlinkportletmenu);


/* For the 'dialogue' pages attribute the dialog class to body */
/* For the 'dialogue' pages attribute the dialog class to body */
Line 24: Line 25:
addOnloadHook(attrib_class_dialog);
addOnloadHook(attrib_class_dialog);


/* Agora : if new sub-page created append a title section link (called by
 
submit) */
/* Agora : if new sub-page created append a title section link (called by submit) */


function pgagora_insert_topsection() {
function pgagora_insert_topsection() {
   /* only if agora new page and write permission */
   /* only if agora new page and write permission */
   if (wgAction != 'edit' && wgAction != 'submit') return true;
   if (wgAction != 'edit' && wgAction != 'submit') return true;
   if (wgUserGroups == null || wgUserGroups.join("").match(/scribe|sysop|fsc|bureaucrat/) == null) return true;
   if (wgUserGroups == null || wgUserGroups.join("").match(/user|sysop|bureaucrat/) == null) return true;
   if (wgPageName == null || wgPageName.match(/^BoyWiki:Agora\//) == null) return true;
   if (wgPageName == null || wgPageName.match(/^BoyWiki:Agora\//) == null) return true;
   if (wgTitle == null) return true;
   if (wgTitle == null) return true;
Line 42: Line 43:
   if (content.match(/^\n?=\[\[[^\]\/]*Agora\/[^\]]*\]\]=/)) return true;
   if (content.match(/^\n?=\[\[[^\]\/]*Agora\/[^\]]*\]\]=/)) return true;
   /* otherwise copy subject sub-title */
   /* otherwise copy subject sub-title */
   var subject = document.forms['editform'].elements['wpSummary'].value; document.forms['editform'].elements['wpSummary'].value = ""; if (subject.length) subject = "=="+subject+"==\n";
   var subject = document.forms['editform'].elements['wpSummary'].value;
  document.forms['editform'].elements['wpSummary'].value = "";
  if (subject.length) subject = "=="+subject+"==\n";
   /* and insert title + subject at beginning of page */
   /* and insert title + subject at beginning of page */
   var title = "=[[BoyWiki:"+wgTitle+"|"+wgTitle+"]]=\n"; document.forms['editform'].elements['wpTextbox1'].value =  title + subject + content; return true;
   var title = "=[[BoyWiki:"+wgTitle+"|"+wgTitle+"]]=\n";
  document.forms['editform'].elements['wpTextbox1'].value =  title + subject + content;
  return true;
}
}


/* Manage call to function() on submit action before sendind the page  */
/* Manage call to function() on submit action before sendind the page  */


function initeventpostform() {
function initeventpostform() {
   if (wgAction == 'edit' || wgAction == 'submit') {if (document.forms['editform'] == undefined || document.forms['editform'].elements['wpTextbox1'] == undefined) return; document.forms['editform'].onsubmit = function()  
   if (wgAction == 'edit' || wgAction == 'submit') {
{
    if (document.forms['editform'] == undefined || document.forms['editform'].elements['wpTextbox1'] == undefined) return;
    document.forms['editform'].onsubmit = function() {
         /* for agora pages: append section title link */
         /* for agora pages: append section title link */
         pgagora_insert_topsection();
         pgagora_insert_topsection();
         /* other function */
         /* other function */      
     
    }
    }
   }
   }
}
}


addOnloadHook(initeventpostform);
addOnloadHook(initeventpostform);


/* La première lecture du jour de la page Accueil de la veille  
/* La première lecture du jour de la page Accueil de la veille  
Line 92: Line 99:


       // init requete serveur - purge cache pour page Main_Page
       // init requete serveur - purge cache pour page Main_Page
       xhr_object.open('POST', '/wiki/Main_Page', true);
       xhr_object.open('POST', '/en/Main_Page', true);
       xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
       xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
       xhr_object.setRequestHeader("Charset", "utf-8");
       xhr_object.setRequestHeader("Charset", "utf-8");
Line 118: Line 125:
   return yer.toString()+mon.toString()+day.toString();
   return yer.toString()+mon.toString()+day.toString();
}
}
/* Création objet http pour requête serveur */
/* Création objet http pour requête serveur */
function createxhrobject() {
function createxhrobject() {
if (window.XMLHttpRequest) { // Firefox
  if (window.XMLHttpRequest) { // Firefox
return new XMLHttpRequest();
    return new XMLHttpRequest();
}
  }
else if (window.ActiveXObject) { // IE
  else if (window.ActiveXObject) { // IE
return new ActiveXObject("Msxml2.XMLHTTP");
    return new ActiveXObject("Msxml2.XMLHTTP");
}
  }
else return null;
  else return null;
}
}

Revision as of 07:31, 5 April 2014

/* Any JavaScript here will be loaded for all users on every page load. */

/* Append an Agora link to Navigation menu (if connected) */

function addlinkportletmenu() {
  if (wgUserName != null)
  addPortletLink("p-navigation", "https://www.boywiki.org/en/BoyWiki:Agora", "Agora", "n-agora", "A place to discuss the administration and editing of BoyWiki");
}

addOnloadHook(addlinkportletmenu);


/* For the 'dialogue' pages attribute the dialog class to body */
/* Used by css to colorize indentation in Agora */

function attrib_class_dialog() {
  /* only for Agora or discussion pages */
  var bodyclass = document.body.className;
  if (bodyclass.match(/page-BoyWiki_Agora/)
        || bodyclass.match(/page-Discussion_/)
        || bodyclass.match(/page-Discuss_/))
     document.body.className = bodyclass + ' dialog';
}

addOnloadHook(attrib_class_dialog);


/* Agora : if new sub-page created append a title section link (called by submit) */

function pgagora_insert_topsection() {
  /* only if agora new page and write permission */
  if (wgAction != 'edit' && wgAction != 'submit') return true;
  if (wgUserGroups == null || wgUserGroups.join("").match(/user|sysop|bureaucrat/) == null) return true;
  if (wgPageName == null || wgPageName.match(/^BoyWiki:Agora\//) == null) return true;
  if (wgTitle == null) return true;
  if (wgArticleId != 0) return true;
  if (document.forms['editform'].elements['wpSummary'] == undefined) return true;
  if (document.getElementById('wpSummaryLabel').firstChild.firstChild.data.match(/Subject\/title/) == null) return true;
 
  /* retrieve text */
  var content = document.forms['editform'].elements['wpTextbox1'].value;
  /* if title already present */
  if (content.match(/^\n?=\[\[[^\]\/]*Agora\/[^\]]*\]\]=/)) return true;
  /* otherwise copy subject sub-title */
  var subject = document.forms['editform'].elements['wpSummary'].value;
  document.forms['editform'].elements['wpSummary'].value = "";
  if (subject.length) subject = "=="+subject+"==\n";
  /* and insert title + subject at beginning of page */
  var title = "=[[BoyWiki:"+wgTitle+"|"+wgTitle+"]]=\n";
  document.forms['editform'].elements['wpTextbox1'].value =  title + subject + content;
  return true;
}


/* Manage call to function() on submit action before sendind the page  */

function initeventpostform() {
  if (wgAction == 'edit' || wgAction == 'submit') {
    if (document.forms['editform'] == undefined || document.forms['editform'].elements['wpTextbox1'] == undefined) return;
    document.forms['editform'].onsubmit = function() {
        /* for agora pages: append section title link */
        pgagora_insert_topsection();
        /* other function */       
    }
  }
}

addOnloadHook(initeventpostform);


/* La première lecture du jour de la page Accueil de la veille 
/* provoque son rafraichissement par purge du cache */

function refresh_Image_of_the_day() {
   // seulement pour page Main_Page(tout utilisateur a visiblement le droit de puger le cache)
   if (wgPageName != 'Main_Page') return;
   if (wgAction != 'view') return;
   
   // récup date dernière mise en cache de la page
   var datecache = null;
   var nodes = document.getElementById('bodyContent').childNodes;
   for (var i=0; i < nodes.length ; i++) {
      if (nodes[i].nodeName == '#comment' && nodes[i].data.match(/timestamp [0-9]{14}/)) {
         datecache = nodes[i].data.match(/timestamp ([0-9]{8})/);
         break;
      }
   }
   if (datecache == null) return;

   // date actuelle locale
   var now = new Date();
   var datenow = formatdateyyyymmdd(now);

   if (datecache[1] < datenow) {

      // init objet requete serveur
      var xhr_object = createxhrobject();
      if (xhr_object == null) return;

      // init requete serveur - purge cache pour page Main_Page
      xhr_object.open('POST', '/en/Main_Page', true);
      xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xhr_object.setRequestHeader("Charset", "utf-8");
      // action sur fin requete async
      //xhr_object.onreadystatechange = function() {
      //   if (xhr_object.readyState == 4)  alert(xhr_object.status);
      //}
      xhr_object.send('action=purge');
   }
}


addOnloadHook(refresh_Image_of_the_day);



/* Retourne la date formatée yyyymmdd */

function formatdateyyyymmdd(obj) {
   var yer = obj.getFullYear();
   var mon = (obj.getMonth() + 1);
   var day = obj.getDate();
   if (mon < 10) mon = '0'+mon;
   if (day < 10) day = '0'+day;
   return yer.toString()+mon.toString()+day.toString();
}


/* Création objet http pour requête serveur */
function createxhrobject() {
  if (window.XMLHttpRequest) { // Firefox
    return new XMLHttpRequest();
  }
  else if (window.ActiveXObject) { // IE
    return new ActiveXObject("Msxml2.XMLHTTP");
  }
  else return null;
}