לדלג לתוכן

מדיה ויקי:Gadget-HotCat.js

מתוך צפונות ויקי
גרסה מ־21:20, 20 בינואר 2008 מאת wikipedia:he>Lupo~hewiki (Bug fix for upload form if category input form is still open.)

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
//<pre><nowiki>
addOnloadHook ( hotcat ) ;

var hotcat_running = 0 ;
var hotcat_last_v = "" ;
var hotcat_exists_yes = "http://upload.wikimedia.org/wikipedia/commons/thumb/b/be/P_yes.svg/20px-P_yes.svg.png" ;
var hotcat_exists_no = "http://upload.wikimedia.org/wikipedia/commons/thumb/4/42/P_no.svg/20px-P_no.svg.png" ;
var hotcat_upload = 0 ;
var hotcat_no_autocommit = 1;
var hotcat_old_onsubmit = null;
var hotcat_nosuggestions = false;
// hotcat_nosuggestions is set to true if we don't have XMLHttp! (On IE6, XMLHttp uses
// ActiveX, and the user may deny execution.) If true, no suggestions will ever be
// displayed, and there won't be any checking whether the category  exists.
// Lupo, 2008-01-20
var hotcat_xmlhttp = null;

var hotcat_modify_blacklist = new Array (
"CC-" ,
"GFDL" ,
"PD"
) ;

function hotcat_remove_upload ( text ) {
  var cats = document.getElementById ( "catlinks" ) ;
  cats = cats.getElementsByTagName ( "span" ) ;
  for ( var i = 0 ; i < cats.length ; i++ ) {
    var a = cats[i].getElementsByTagName("a")[0] ;
    if ( a.getAttribute ( "title" ) != text ) continue ;
    cats[i].parentNode.removeChild ( cats[i].nextSibling ) ;
    cats[i].parentNode.removeChild ( cats[i] ) ;
    break ;
  }
}

function hotcat_check_upload () {
  // Don't do anything if not "Special:Upload", or user not logged in.
  if ( wgNamespaceNumber != -1 || wgTitle != "Upload" || wgUserName == null) return ;
  hotcat_upload = 1 ;
  var ip = document.getElementById ( "wpWatchthis" ) ;
  var tr = ip.parentNode.parentNode ;
  var ntr = document.createElement ( "tr" ) ;
  var ntd = document.createElement ( "td" ) ;
  var ntde = document.createElement ( "td" ) ;
  var catline = document.createElement ( "div" ) ;
  var np = document.createElement ( "p" ) ;

  ntde.appendChild ( document.createTextNode ( "Categories:" ) ) ;
  ntde.style.textAlign = "right" ;
  ntde.style.verticalAlign = "middle" ;
  catline.id = "catlinks" ;
  // On the upload form, the suggestion box appears at the very top of the page. That is because
  // the innermost enclosing div of the upload form (and its table) that has position "relative"
  // is the bodyContent div. Try to fix that by giving catline relative positioning, so absolute
  // positioning within should be relative to catline. Lupo, 2008-01-18
  catline.style.position ="relative";
  np.className = "catlinks" ;
  catline.appendChild ( np ) ;
  ntd.appendChild ( catline ) ;
  ntr.appendChild ( ntde ) ;
  ntr.appendChild ( ntd ) ;
  tr.parentNode.insertBefore ( ntr , tr.nextSibling ) ;

  // Add handler for submit (changed by Lupo, 2008-01-18)
  var form = document.getElementById ('upload');
  hotcat_old_onsubmit = form.onsubmit;
  form.onsubmit =hotcat_on_upload;
  //var sub = document.getElementsByName("wpUpload")[0] ;
  //sub.onclick = hotcat_on_upload ;
  //sub.type = "button" ;
}

function hotcat_on_upload () {
  // First, make sure that if we have an open category input form, we close it.
  var input = document.getElementById ('hotcat_text');
  if (input != null) hotcat_ok ();

  var do_submit = true;
  // Call previous onsubmit handler, if any
  if (hotcat_old_onsubmit) {
    if (typeof hotcat_old_onsubmit == 'string')
      do_submit = eval (hotcat_old_onsubmit);
    else if (typeof hotcat_old_onsubmit == 'function')
      do_submit = hotcat_old_onsubmit ();
  }
  if (!do_submit) return false;
  // Only copy the categories if we do submit
  var cats = document.getElementById ( "catlinks" ) ;
  cats = cats.getElementsByTagName ( "span" ) ;
  var eb = document.getElementById ( "wpUploadDescription" )
           || document.getElementById ( "wpDesc" ); // New upload form
  for ( var i = 0 ; i < cats.length ; i++ ) {
    var a = cats[i].getElementsByTagName("a")[0] ;
    var t = a.innerHTML ;
    if ( t == "" || t == "(+)" ) continue ;
    eb.value += "\n[[Category:" + t + "]]" ;
  }
  return true;


  //var sub = document.getElementsByName("wpUpload")[0] ;
  //sub.onclick = "" ;
  //sub.type = "submit" ;
  //sub.click();
}

function hotcat () {
  JSconfig.registerKey('HotCatDelay', 100, 'HotCat autocompletion delay (ms):', 5);

  if ( hotcat_check_action() ) return ; // Edited page, reloading anyway
  hotcat_check_upload () ;
  var catline = getElementsByClassName ( document , "p" , "catlinks" ) [0] ;
  if ( catline == null || typeof catline == 'undefined' ) return ;

  // do not add interface to protected pages, if user has no edit permission
  if( document.getElementById('ca-viewsource' ) != null ) return; 

  hotcat_modify_existing ( catline ) ;
  hotcat_append_add_span ( catline ) ;
}

function hotcat_append_add_span ( catline ) {
  var span_add = document.createElement ( "span" ) ;
  var span_sep = document.createTextNode ( " | " ) ;
  if ( catline.firstChild ) catline.appendChild ( span_sep ) ;
  catline.appendChild ( span_add ) ;
  hotcat_create_span ( span_add ) ;
}

String.prototype.ucFirst = function () {
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}

function hotcat_is_on_blacklist ( cat_title ) {
  if ( !cat_title ) return 0 ;
  cat_title = cat_title.split(":",2).pop() ;
  for ( var i = 0 ; i < hotcat_modify_blacklist.length ; i++ ) {
    if ( cat_title.substr ( 0 , hotcat_modify_blacklist[i].length ) == hotcat_modify_blacklist[i] ) return 1 ;
  }
  return 0 ;
}

function hotcat_modify_span ( span , i ) {
  var cat_title = span.firstChild.getAttribute ( "title" ) ;
  var sep1 = document.createTextNode ( " " ) ;
  var a1 = document.createTextNode ( "(-)" ) ;
  var remove_link = document.createElement ( "a" ) ;
  remove_link.href = "javascript:hotcat_remove(\"" + encodeURIComponent( cat_title ) + "\");" ;
  remove_link.appendChild ( a1 ) ;
  span.appendChild ( sep1 ) ;
  span.appendChild ( remove_link ) ;

  if ( hotcat_is_on_blacklist ( cat_title ) ) return ;
  var mod_id = "hotcat_modify_" + i ;
  var sep2 = document.createTextNode ( " " ) ;
  var a2 = document.createTextNode ( "(±)" ) ;
  var modify_link = document.createElement ( "a" ) ;
  modify_link.id = mod_id ;
  modify_link.href = "javascript:hotcat_modify(\"" + mod_id + "\");" ;
  modify_link.appendChild ( a2 ) ;
  span.appendChild ( sep2 ) ;
  span.appendChild ( modify_link ) ;
}

function hotcat_modify_existing ( catline ) {
  var spans = catline.getElementsByTagName ( "span" ) ;
  for ( var i = 0 ; i < spans.length ; i++ ) {
    hotcat_modify_span ( spans[i] , i ) ;
  }
}

function hotcat_remove ( cat_title ) {
  if ( hotcat_upload ) {
    hotcat_remove_upload ( cat_title ) ;
    return ;
  }
  var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
  document.location = editlk + '&hotcat_removecat=' + cat_title ;
}

function hotcatGetParamValue(paramName, h) {
        if (typeof h == 'undefined' ) { h = document.location.href; }
        var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)');
        var m=cmdRe.exec(h);
        if (m) {
                try {
                        return decodeURI(m[1]);
                } catch (someError) {}
        }
        return null;
}

function hotcat_check_action () {
  var ret = 0 ;
  if ( wgAction != "edit" ) return ret ; // Not an edit page, so no business...
  var summary = new Array () ;
  var t = document.editform.wpTextbox1.value ;
  var prevent_autocommit = 0 ;
  if ( typeof hotcat_no_autocommit != "undefined" ) prevent_autocommit = 1 ;
//  if ( hotcat_no_autocommit ) prevent_autocommit = 1 ;

  // Remove existing category?
  var hrc = hotcatGetParamValue('hotcat_removecat') ;
  if ( typeof hrc != "undefined" && hrc != null && hrc != "" ) {
    var tl = t.toLowerCase() ;
    var hrcl = "[[" + hrc.toLowerCase() ;
    var tn = tl.split ( hrcl ) ;
    if ( tn.length == 2 ) { // Found one occurrence of the category - good!
      var l1 = tn.shift().length ;
      var t1 = t.substr ( 0 , l1 ) ;
      var t2 = t.substr ( l1 + hrcl.length ) ;
      t2 = t2.split("]]") ;
      t2.shift() ;
      t2 = t2.join ( "]]" ) ;
      t = t1.replace(/\s+$/,"") + "\n" + t2.replace(/^\s+/,"") ; // Trying to remove whitespace
      summary.push ( "Removed \"" + hrc + "\"" ) ;
      ret = 1 ;
    } else {
      alert ( "Did not find a unique occurrence of \"" + hrc + "\" - maybe it is in a template?" ) ;
      prevent_autocommit = 1 ;
    }
  }  

  // Add new category?
  var hnc = hotcatGetParamValue('hotcat_newcat') ;
  if ( typeof hnc != "undefined" && hnc != null && hnc != "" ) {
    var txt = "[[Category:" + hnc + "]]" ;
    t = t + '\n' + txt ;
    summary.push ( "Quick-adding category \"" + hnc + "\"" ) ;
    var t2 = t.split("{{uncat}}").join("") ;
    t2 = t2.split("{{Uncat}}").join("") ;
    if ( t2 != t ) {
      t = t2 ;
      summary.push ( "removed {{uncat}}" ) ;
    }
    ret = 1 ;
  }

  if ( ret ) {
    document.editform.wpTextbox1.value = t ;

    document.editform.wpSummary.value = summary.join( "; " ) + " (using [[MediaWiki:HotCat.js|HotCat.js]])" ;
    document.editform.wpMinoredit.checked = true ;
    if ( !prevent_autocommit ) {
      document.getElementById("bodyContent").style.display = "none" ; // Hiding the entire edit section so as not to tempt the user into editing...
      document.editform.wpSave.click();
    }
  }

  // This is the end, my friend, the end...
  return ret ;
}

function hotcat_clear_span ( span_add ) {
  while ( span_add.firstChild ) span_add.removeChild ( span_add.firstChild ) ;
}

function hotcat_create_span ( span_add ) {
  hotcat_clear_span ( span_add ) ;
  var a_add = document.createElement ( "a" ) ;
  var a_text = document.createTextNode ( "(+)" ) ;
  span_add.id = "hotcat_add" ;
  a_add.href = "javascript:hotcat_add_new()" ;
  a_add.appendChild ( a_text ) ;
  span_add.appendChild ( a_add ) ;
}

function hotcat_modify ( link_id ) {
  var link = document.getElementById ( link_id ) ;
  var span = link.parentNode ;
  var catname = span.firstChild.firstChild.data ;
  
  while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ;
  span.firstChild.style.display = "none" ;
  hotcat_create_new_span ( span , catname ) ;
  hotcat_last_v = "" ;
  hotcat_text_changed () ; // Update icon
}

function hotcat_add_new () {
  var span_add = document.getElementById ( "hotcat_add" ) ;
  hotcat_clear_span ( span_add ) ;
  hotcat_last_v = "" ;
  hotcat_create_new_span ( span_add , "" ) ;
}

function hotcat_create_new_span ( thespan , init_text ) {
  var form = document.createElement ( "form" ) ;
  form.method = "post" ;
  form.onsubmit = function () { hotcat_ok(); return false; } ;
  form.id = "hotcat_form" ;
  form.style.display = "inline" ;

  var list = null;
  
  if (!hotcat_nosuggestions) {
  	// Only do this if we may actually use XMLHttp...
    list = document.createElement ( "select" ) ;
    list.id = "hotcat_list" ;
    list.onclick = function ()
      {
        var l = document.getElementById("hotcat_list");
        if (l != null)
          document.getElementById("hotcat_text").value = l.options[l.selectedIndex].text;
        hotcat_text_changed();
      };
    list.ondblclick = function ()
      {
        var l = document.getElementById("hotcat_list");
        if (l != null)
          document.getElementById("hotcat_text").value = l.options[l.selectedIndex].text;
        // Don't call text_changed here if on upload form: hotcat_ok will remove the list
        // anyway, so we must not ask for new suggestions since show_suggestions might
        // raise an exception if it tried to show a no longer existing list.
        // Lupo, 2008-01-20
        if (!hotcat_upload) hotcat_text_changed();
        hotcat_ok();
      };
    list.style.display = "none" ;
  }
  
  var text = document.createElement ( "input" ) ;
  text.size = 40 ;
  text.id = "hotcat_text" ;
  text.type = "text" ;
  text.value = init_text ;
  text.onkeyup = function () { window.setTimeout("hotcat_text_changed();", JSconfig.keys['HotCatDelay'] ); } ;

  var exists = null;
  if (!hotcat_nosuggestions) {
    exists = document.createElement ( "img" ) ;
    exists.id = "hotcat_exists" ;
    exists.src = hotcat_exists_no ;
  }

  var OK = document.createElement ( "input" ) ;
  OK.type = "button" ;
  OK.value = "OK" ;
  OK.onclick = hotcat_ok ;

  var cancel = document.createElement ( "input" ) ;
  cancel.type = "button" ;
  cancel.value = "Cancel" ;
  cancel.onclick = hotcat_cancel ;

  if (list != null) form.appendChild ( list ) ;
  form.appendChild ( text ) ;
  if (exists != null) form.appendChild ( exists ) ;
  form.appendChild ( OK ) ;
  form.appendChild ( cancel ) ;
  thespan.appendChild ( form ) ;
  text.focus () ;
}

function hotcat_ok () {
  var text = document.getElementById ( "hotcat_text" ) ;
  var v = text.value ;

  // Empty category ?
  if ( v == "" ) {
    hotcat_cancel() ;
    return ;
  }

  // Special:Upload ?
  if ( hotcat_upload ) {
    hotcat_just_add ( text.value ) ;
    return ;
  }

  var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
  var url = editlk + '&hotcat_newcat=' + encodeURIComponent( v ) ;

  // Editing existing?
  var span = text.parentNode.parentNode ; // span.form.text
  if ( span.id != "hotcat_add" ) { // Not plain "addition"
    var cat_title = span.firstChild.getAttribute ( "title" ) ;
    url += '&hotcat_removecat=' + cat_title ;
  }

  document.location = url ;
}

function hotcat_just_add ( text ) {
  var span = document.getElementById("hotcat_form") ;
  while ( span.tagName != "SPAN" ) span = span.parentNode ;
  var add = 0 ;
  if ( span.id == "hotcat_add" ) add = 1 ;
  span.id = "" ;
  while ( span.firstChild ) span.removeChild ( span.firstChild ) ;
  var na = document.createElement ( "a" ) ;
  na.href = wgArticlePath.split("$1").join("Category:" + encodeURIComponent( text )) ;
  na.appendChild ( document.createTextNode ( text ) ) ;
  na.setAttribute ( "title" , text ) ;
  span.appendChild ( na ) ;
  var catline = getElementsByClassName ( document , "p" , "catlinks" ) [0] ;
  if ( add ) hotcat_append_add_span ( catline ) ;

  for ( var i = 0 ; i < span.parentNode.childNodes.length ; i++ ) {
    if ( span.parentNode.childNodes[i] != span ) continue ;
    hotcat_modify_span ( span , i ) ;
    break ;
  }
}

function hotcat_cancel () {
  var span = document.getElementById("hotcat_form").parentNode ;
  if ( span.id == "hotcat_add" ) {
    hotcat_create_span ( span ) ;
  } else {
    while ( span.firstChild.nextSibling ) span.removeChild ( span.firstChild.nextSibling ) ;
    span.firstChild.style.display = "" ;
    for ( var i = 0 ; i < span.parentNode.childNodes.length ; i++ ) {
      if ( span.parentNode.childNodes[i] != span ) continue ;
      hotcat_modify_span ( span , i ) ;
      break ;
    }
  }
}

function hotcat_text_changed () {
  if ( hotcat_running ) return ;
  var text = document.getElementById ( "hotcat_text" ) ;
  var v = text.value.ucFirst() ;
  if ( hotcat_last_v == v ) return ; // Nothing's changed...

  if (hotcat_nosuggestions) {
    // On IE, XMLHttp uses ActiveX, and the user may deny execution... just make sure
    // the list is not displayed.
    var list = document.getElementById ('hotcat_list');
    if (list != null) list.style.display = "none" ;
    var exists = document.getElementById ('hotcat_exists');
    if (exists != null) exists.style.display = "none" ;
    return;
  }
  
  hotcat_running = 1 ;
  hotcat_last_v = v ;

  if ( v != "" ) {
    var url = wgServer + "/" + wgScriptPath + "/api.php?format=xml&action=query&list=allpages&apnamespace=14&apfrom=" + encodeURIComponent( v ) ;
    if (hotcat_xmlhttp != null) {
      hotcat_xmlhttp.abort() ; // Just to make sure...
      delete hotcat_xmlhttp;
    }
    hotcat_xmlhttp = sajax_init_object() ;
    if (hotcat_xmlhttp == null) {
      //Oops! We don't have XMLHttp...
      hotcat_nosuggestions = true;
      var list = document.getElementById ('hotcat_list');
      if (list != null) list.style.display = "none" ;
      var exists = document.getElementById ('hotcat_exists');
      if (exists != null) exists.style.display = "none" ;
      hotcat_running = 0;
      return;
    }	
    hotcat_xmlhttp.open('GET', url, true);
    hotcat_xmlhttp.onreadystatechange = function () {
          if ( typeof hotcat_xmlhttp == "undefined" ) return ;
          if (hotcat_xmlhttp.readyState == 4) {
              var xml = hotcat_xmlhttp.responseXML ;
              if ( xml == null ) return ;
              var pages = xml.getElementsByTagName( "p" ) ;
              var titles = new Array () ;
              for ( var i = 0 ; i < pages.length ; i++ ) {
                var s = pages[i].getAttribute("title").split(":",2).pop() ;
                if ( s.substr ( 0 , hotcat_last_v.length ) != hotcat_last_v ) break ;
                titles.push ( s ) ;
              }
              hotcat_show_suggestions ( titles ) ;
              hotcat_running = 0 ;
          }
      };
    hotcat_xmlhttp.send(null);
  } else {
    var titles = new Array () ;
    hotcat_show_suggestions ( titles ) ;
    hotcat_running = 0 ;
  }
}

function hotcat_show_suggestions ( titles ) {
  var text = document.getElementById ( "hotcat_text" ) ;
  var list = document.getElementById ( "hotcat_list" ) ;
  var icon = document.getElementById ( "hotcat_exists" ) ;
  // Somehow, after a double click on the selection list, we still get here in IE, but
  // the list may no longer exist... Lupo, 2008-01-20
  if (list == null) return;
  if (hotcat_nosuggestions) {
    list.style.display = "none" ;
    if (icon != null) icon.style.display = "none";
    return;
  }
  if ( titles.length == 0 ) {
    list.style.display = "none" ;
    icon.src = hotcat_exists_no ;
    return ;
  }
  
  // Set list size to minimum of 5 and actual number of titles. Formerly was just 5.
  // Lupo, 2008-01-20
  list.size = (titles.length > 5 ? 5 : titles.length) ;
  list.style.align = "left" ;
  list.style.zIndex = 5 ;
  list.style.position = "absolute" ;

  // Was listh = titles.length * 20: that makes no sense if titles.length > list.size
  // Lupo, 2008-01-20
  var listh = list.size * 20;
  var nl = parseInt (text.parentNode.offsetLeft) - 1 ;
  var nt = parseInt (text.offsetTop) - listh ;
  list.style.top = nt + "px" ;
  list.style.width = text.offsetWidth + "px" ;
  list.style.height = listh + "px" ;
  list.style.left = nl + "px" ;
  while ( list.firstChild ) list.removeChild ( list.firstChild ) ;
  for ( var i = 0 ; i < titles.length ; i++ ) {
    var opt = document.createElement ( "option" ) ;
    var ot = document.createTextNode ( titles[i] ) ;
    opt.appendChild ( ot ) ;
    //opt.value = titles[i] ;
    list.appendChild ( opt ) ;
  }
  
  list.style.display = "block" ;

  icon.src = hotcat_exists_yes ;

  var first_title = titles.shift () ;
  if ( first_title == hotcat_last_v ) return ;

  // Put the first entry of the title list into the text field, and select the
  // new suffix such that it'll be overwritten if the user keeps typing.
  // ONLY do this if we have a way to select parts of the content of a text
  // field, otherwise, this is very annoying for the user. Note: IE does it
  // again differently from the two versions previously implemented.
  // Lupo, 2008-01-20
  if (text.setSelectionRange ||
      text.createTextRange ||
      typeof (text.selectionStart) != 'undefined' &&
      typeof (text.selectionEnd) != 'undefined')
  {
    var nosel = hotcat_last_v.length ;
  
    text.value = first_title ;
    
    if (text.setSelectionRange)      // e.g. khtml
      text.setSelectionRange (nosel, first_title.length);
    else if (text.createTextRange) { // IE
      var new_selection = text.createTextRange();
      new_selection.move ("character", nosel);
      new_selection.moveEnd ("character", first_title.length - nosel);
      new_selection.select();
    } else {
      text.selectionStart = nosel;
      text.selectionEnd   = first_title.length;
    }
  }
}
//</pre></nowiki>