MediaWiki:Gadget-LiveRC.js/Extensions/SpecialLogListExtensionExample.js

Une page de Wikipédia, l'encyclopédie libre.
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
/* ************************************************************************************************************************************************

 --------------------------------------------------------------------------------------
 ---------LLLL---------III--------------------------RRRRRRRRRR--------CCCCC------------
 ---------LLLL---------III--------------------------RRRRRRRRRRRR----CCCCCCCCC----------
 ---------LLLL--------------------------------------RRR------RRR---CCC-----CCC---------
 ---------LLLL---------III--VV-----VV--EEEEEEEEE----RRR------RRR--CCC------------------
 ---------LLLL---------III---VV---VV---EEE----------RRRRRRRRRRR---CCC------------------
 ---------LLLL---------III---VV---VV---EEEEEE-------RRRRRRRRRR----CCC------------------
 ---------LLLL---------III----VV-VV----EEEEEE-------RRR-----RRR----CCC-----CCC---------
 ---------LLLLLLLLLLL--III----VVVVV----EEE----------RRR------RRR----CCCCCCCCC----------
 ---------LLLLLLLLLLL--III-----VVV-----EEEEEEEEE----RRR-------RRR-----CCCCC------------
 --------------------------------------------------------------------------------------

// LiveRC extension
 
// This is an example of extension
// using hooks related to special log lists
//
// change all "SpecialLogListExtensionExample"
// by what ever you want
//
// For this example, we will get all edits made 
// on pages that belongs to Category:ExampleCategory


{{Catégorisation JS|LiveRC}} 
 
************************************************************************************************************************************************ */
if (typeof(LiveRC_AddHook)==="function") { // START IF, to be sure LiveRC is loaded and prevent javascript errors
 
LiveRC_Config["InstalledLiveRCExtensions"].push("SpecialLogListExtensionExample");  // Used to warn LiveRC that this extension is enabled
 
/* ************************************************************************************************************************************************ */



// == Variables ==


  // === Param ===

lrcParams["SLLEE_Category"] = "ExampleCategory";


  // === Texts ===

    // Title of the special list
lrcTexts['liveSpecialLogListExtensionExampleTitle'] = 'SpecialLogListExtensionExample';

    // Description of the show/hide option
lrcParamDesc['DescDisplayliveSpecialLogListExtensionExample'] = 'Show SpecialLogListExtensionExample list';

   // Description of the special list title
lrcParamDesc['DescliveSpecialLogListExtensionExampleTitle'] = 'SpecialLogListExtensionExample special list title';

   // Description of the category param
lrcParamDesc['DescSLLEE_Category'] = 'Category to match';


  // === List set up ===

    // This is optional, but you have to do it if you want the default state to be false

LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"] = {
       "Values" : {},             // list of pages with rc values
       "DefaultState" : true      // default display state for the log list
}



// == Functions ==


// === Check the rc to see if the conditions match ===

window.SpecialLogListExtensionExample_AfterSpecialLogListUpdated = function(Args){

  // get all rc params we need from Args
  var rc = Args.rc;
  var title = rc.title;
  var timestamp = rc.timestamp;
  var categories = rc.categories;
  var state = rc.state;

  // no need the patrol log
  if(lrcHasState(state, "PATROL")) return;

  // no cat means the edit don't match what we want
  if(!categories || categories.length ===0) return       

  // set the target category
  var Target = lrcGetNamespaceName(14)+":"+lrcMakeParam("SLLEE_Category");

  // test each category
  for(var a=0,l=categories.length;a<l;a++){
    var cat = categories[a];
    if(cat !== Target) continue;
    // If we are here, we found an edit made on a page of the target category
    // So lets update the list

    // If there where no edit on that page yet, set the item
    if(!LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title]){
      LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title] = {};
      LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title].users = [];
    }

    // Set the last timestamp and add the edit to the page rc list
    LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title].ts = timestamp;
    lrcSubFollowAddItem("liveSpecialLogListExtensionExample", title, "users", rc);

    // Update the log
    SpecialLogListExtensionExample_AfterAllSpecialLogListUpdated();
  }
}


// === Update the log list ===

window.SpecialLogListExtensionExample_AfterAllSpecialLogListUpdated = function(){

  // add an entry in the debug log
  lrcDisplayDebug("Update list (liveSpecialLogListExtensionExample)");

  // Set the lines Array
  var tempAr = [];
  
  // List all the pages
  for(var title in LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"]){

    // Make sure that the item exists. Sometimes, for ... in ... loops are shitty
    if(!LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"].hasOwnProperty(title)) continue;

    // Don't show list if there is no rc. For some purpose, this can be avoided
    if(LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title].users.length == 0) continue;

    // Get the last timestamp
    var timestamp = LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title].ts;
    
    // set the line node
    var Line = document.createElement('li');
    Line.id = 'specialloglistextensionexample-' + timestamp; // timestamp needed here to sort the lines later

    // set the timestamp tag
    var TS = document.createElement('span');
    TS.innerHTML = getTimeTag(timestamp);

    // set the remove link
    var uremove = document.createElement('a');
    uremove.className = "hidelineLink";
    uremove.href = "javascript:;";
    uremove.title = lrcMakeText("HIDE_THIS");
    uremove.onclick = function(){ SpecialLogListExtensionExample_removeSpecialLogListExtensionExample(title); return false; }; // Here we have a special function set above
    uremove.innerHTML = 'x';

    // set the watch link
    var watchbutton = document.createTextNode(' · ');
    if(lrcGetFlowStatusForAction(title, "watch")) watchbutton = lrcCreateWatchPageLink(title, LiveRC_Config["SpecialLogListConfig"]["liveSuivi"]["Values"][title], 0);

    // set the delete link (if you can delete)
    var sdelete = lrcGetDeleteLink({title:title});

    // set the page link
    var sarti = document.createElement('a');
    sarti.className = "lrc_ArticleLink";
    sarti.href = lrcGetPageURL(title);
    sarti.onclick = function(){ liveArticle(title); return false; };
    sarti.title = title;
    sarti.innerHTML = title;

    // Set the log sublist for this title
    var MoreLink  = SpecialLogListExtensionExample_lrcGetAllSpecialLogListExtensionExample(title);

    // Add all links except main link in a small tag and put it in the line, along with some separator text nodes
    var Small = document.createElement('small');
    Small.appendChild(uremove);
    Small.appendChild(document.createTextNode(' · '));
    Small.appendChild(TS);
    Small.appendChild(document.createTextNode(' : '));
    Small.appendChild(watchbutton);
    if(sdelete){
      Small.appendChild(document.createTextNode(' • '));
      Small.appendChild(sdelete);
    }
    Small.appendChild(document.createTextNode(' • '));
    Line.appendChild(Small);
 
    // add the main link in the line
    Line.appendChild(sarti);

    // add the sub-list in the line
    if(MoreLink) Line.appendChild(MoreLink);
    
    // put the line in the Array
    tempAr.push(Line);
  }

  // sort the lines (by timestamp)
  tempAr = lrcSortFollow(tempAr);
  
  // get the special list node
  var liveSpecialLogListExtensionExample = document.getElementById('liveSpecialLogListExtensionExample');
  if(!liveSpecialLogListExtensionExample) return;

  // Reset its content and add all the lines
  liveSpecialLogListExtensionExample.innerHTML = "";
  var List = document.createElement('ul');
  List.className = "FollowList";
  var len = tempAr.length;
  for(var n=(tempAr.length)-1; n>=0; n--){
    if(tempAr[n]) List.appendChild(tempAr[n]);
  }
  liveSpecialLogListExtensionExample.appendChild(List);
}


// === Create a sublist for all edits on a given page ===

window.SpecialLogListExtensionExample_lrcGetAllSpecialLogListExtensionExample = function(title){
  var GlobalVar = LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"];

  // Check if we have the infos. if not, return false
  if(!GlobalVar[title]) return false;
  var users = GlobalVar[title].users;
  if(users.length<1) return false;

  // set the sublist display status
  var display = ( GlobalVar[title].display ? '' : 'none');

  // create the node and add the rc count
  var SubListSpan = document.createElement('span');
  SubListSpan.appendChild(document.createTextNode(' - '+users.length));

  // set the toggle link
  var ToggleLink = document.createElement('a');
  ToggleLink.className = "FollowSublistToggleLink";
  ToggleLink.href="javascript:;";
  ToggleLink.onclick = function(){ SpecialLogListExtensionExample_lrcShowHideAllSpecialLogListExtensionExample(this,title); }; // Here we have a special function set above
  ToggleLink.title = lrcMakeText("FollowSublistToggleTitle");
  ToggleLink.innerHTML = '<b>±</b>';
  SubListSpan.appendChild(ToggleLink);

  // Set the sublist
  var SubList = document.createElement('ul');
  SubList.className = "FollowSublist";
  SubList.style.display = display;
  SubListSpan.appendChild(SubList);
  var Lines = [];

  // List all the edits
  for(var a=0,l=users.length;a<l;a++){
    var thisedit = users[a];
   
    // set the line and add the timestamp
    var Line = document.createElement('span');
    Line.innerHTML = getTimeTag(thisedit.timestamp);

    // add the diff link
    Line.appendChild(document.createTextNode(' – '));
    Line.appendChild(lrcGetDiffLink(thisedit));

    // add the user link
    Line.appendChild(document.createTextNode(' : '));
    Line.appendChild(lrcGetUserLink(thisedit.user));

    // add the line to the sublist
    Lines.push(Line);
  }

  // add the edits to the sublist
  for(var a=0,l=Lines.length;a<l;a++){
    var Li = document.createElement('li');
    Li.appendChild(Lines[a]);
    SubList.appendChild(Li);
  }

  // return the sublist
  return SubListSpan;
}


// === Toggle a sublist ===

window.SpecialLogListExtensionExample_lrcShowHideAllSpecialLogListExtensionExample = function(Link, title){

   // get the sublist node
   if(!Link) return false;
   var Span = Link.nextSibling;
   if(!Span) return false;

   // The list is hidden
   if(Span.style.display == "none"){

      // unhide it
      Span.style.display = "";
 
      // set the display status, for the next updates
      if(LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title])
        LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title].display = true;

   // The list is not hidden
   }else{

      // hide it
      Span.style.display = "none";
 
      // set the display status, for the next updates
      if(LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title])
        LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title].display = false;
   }
}


// === Delete an element from the list ===

window.SpecialLogListExtensionExample_removeSpecialLogListExtensionExample = function(title){

  // Delete the element
  delete LiveRC_Config["SpecialLogListConfig"]["liveSpecialLogListExtensionExample"]["Values"][title];

  // update the log list
  SpecialLogListExtensionExample_AfterAllSpecialLogListUpdated();
}


// === Init the log list ===

window.SpecialLogListExtensionExample_AfterSpecialLogList = function(){

  // Here can be tested some conditions (mediawiki extension, user right, user group, etc...)
  // to prevent the log list to be set.
  // As here we will use a page category, LiveRC must have loaded the page infos, 
  // that means lrcMakeParam("GetPageInfos") must return true;

  if(!lrcMakeParam("GetPageInfos")) return;

  // If OK until there, set the list
  lrcAddSpecialLogList("liveSpecialLogListExtensionExample", true, false);

  // Then set the other hooks
  LiveRC_AddHook("AfterSpecialLogListUpdated",    SpecialLogListExtensionExample_AfterSpecialLogListUpdated   );
  LiveRC_AddHook("AfterAllSpecialLogListUpdated", SpecialLogListExtensionExample_AfterAllSpecialLogListUpdated);
}



// == Set the main hook ==

LiveRC_AddHook("AfterSpecialLogList", SpecialLogListExtensionExample_AfterSpecialLogList);




/* ************************************************************************************************************************************************ */
} // END IF