Korisnik:Mhare/SearchBox.js - Wikipedia, slobodna enciklopedija

# Korisnik:Mhare/SearchBox.js

### Sa Wikipedije, slobodne enciklopedije

< Korisnik:Mhare

Pažnja: Nakon što sačuvate izmjene, morate "osvježiti" keš memoriju vašeg pretraživača da bi ste vidjeli nova podešenja: Mozilla / Safari / Konqueror: držite Shift tipku i kliknite na Reload dugme (ili Ctrl-Shift-R); IE / Opera: pritisnite Ctrl-F5 na tastaturi.

    // Search box for Mediawiki
    // (c) 2006 [[User:Zocky]], released under GPL
    //<pre><nowiki>

    var sr$t;
    var sr$f;
    var sr$s;
    var sr$r;

    function srBack()
    {
      if (sr$s.value=='') {sr$t.focus(); return }

      if (sr$f.srRegexp.checked) {
        var searchString = sr$s.value;
      } else {
        searchString=sr$s.value.replace(/([\[\]\{\}\|\.\*\?\(\)\$\^\\])/g,'\\$1');
      }

      searchString="("+searchString+")(?![\\s\\S]*"+searchString+")";
      if (sr$f.srCase.checked)
        var re=new RegExp(searchString);
      else
        var re=new RegExp(searchString,"i");

      var res = re.exec (sr$t.value.substring(0,sr$t.selectionStart));
      if (!res) {
        var res = re.exec (sr$t.value)
      }

      if (res)
      {
        sr$t.selectionStart=res.index;
        sr$t.selectionEnd=res.index+res[1].length;
      }
      else sr$t.selectionStart=sr$t.selectionEnd;

      srSync();
    }

    function srNext()
    {
      if (sr$s.value=='') {sr$t.focus(); return }

      if (sr$f.srRegexp.checked) {
        var searchString = sr$s.value;
      } else {
        searchString=sr$s.value.replace(/([\[\]\{\}\|\.\*\?\(\)\$\^\\])/g,'\\$1');
      }

      if (sr$f.srCase.checked)
        var re=new RegExp(searchString,"g");
      else
        var re=new RegExp(searchString,"gi");

      re.lastIndex=sr$t.selectionEnd;
      var res = re.exec (sr$t.value)
      if (!res) {
        re.lastIndex=0;
        var res = re.exec (sr$t.value)
      }

      if (res)
      {
        sr$t.selectionStart=res.index;
        sr$t.selectionEnd=res.index+res[0].length;
      }
      else sr$t.selectionStart=sr$t.selectionEnd;
      srSync();
    }

    function srReplace()
    {

      var sels=sr$t.selectionStart;
      var sele=sr$t.selectionEnd;
      var selr=sr$t.value.length-sele;

      if (sr$s.value=='' || sels==sele) {sr$t.focus(); return }

      if (sr$f.srRegexp.checked) {
        var searchString = sr$s.value;
        var replaceString = sr$r.value;

      } else {
        searchString=sr$s.value.replace(/([\[\]\{\}\|\.\*\?\(\)\$\^\\])/g,'\\$1');
        replaceString=sr$r.value.replace(/([\$\\])/g,'\\$1');
      }

      if (sr$f.srCase.checked)
        var re=new RegExp(searchString,"g");
      else
        var re=new RegExp(searchString,"gi");

      re.lastIndex=sels;
      var res = re.exec (sr$t.value);
      var $$=0;
      if (res && res.index==sels && res[0].length==sele-sels)
      {
        if (sr$f.srRegexp.checked) {
          replaceString=replaceString.replace(/\\\\/g,'&backslash;').replace(/\\\$/g,'&dollar;')
          var replaceBits=(" "+replaceString).split(/(?=\$\d)/);
          replaceString=replaceBits[0].substring(1);
          for (var i=1; i<replaceBits.length; i++)
          {
            $$=replaceBits[i][1]-'0';
            if ($$<res.length)
               replaceString += res[$$] + replaceBits[i].substring(2)
            else
               replaceString += replaceBits[i];
         }
         replaceString=replaceString.replace (/\\n/,"\n").replace (/&backslash;/g,"\\").replace
                                             (/&dollar;/g,"\$")

        }
         sr$t.value= sr$t.value.substring(0,sels) + replaceString + sr$t.value.substring(sele);
      }

      sr$t.selectionStart=sels;
      sr$t.selectionEnd=sr$t.value.length-selr;
      sr$t.focus();
    }

    function srReplaceall()
    {
      if (!sr$s.value) {sr$t.focus(); return }

      var sels=sr$t.selectionStart;
      var sele=sr$t.selectionEnd;
      var selr=sr$t.value.length-sele;

      var reps;

      if (sr$f.srRegexp.checked) {
        var searchString = sr$s.value;
        var replaceString = sr$r.value.replace(/\\\\/,'&backslash;').replace(/\\n/,'\n').replace(/&backslash;/,"\\");
      } else {
        searchString=sr$s.value.replace(/([\[\]\{\}\|\.\*\?\(\)\$\^\\])/g,'\\$1');
        replaceString=sr$r.value.replace(/([\$\\])/g,'\\$1');
      }

      if (sele>sels)
        reps=sr$t.value.substring(sels,sele);
      else
        reps=sr$t.value;

      if (sr$f.srCase.checked)
        var re=new RegExp(searchString,"g");
      else
        var re=new RegExp(searchString,"gi");

      var replaceCounter=0;

      var replaceFunc=function(){replaceCounter++;return replaceString};

      reps=reps.replace(re,replaceFunc);

      if (sele>sels)
        sr$t.value = sr$t.value.substring(0,sels) + reps + sr$t.value.substring(sele);
      else
        sr$t.value = reps;

      sr$t.selectionStart=sels;
      sr$t.selectionEnd=sele>sels ? sr$t.value.length-selr : sels;
      sr$t.focus();
      window.status = replaceCounter+" ocurrences of " + searchString + " replaced.";
    }

    function srToggleCase()
    {
      var sels=sr$t.selectionStart;
      var sele=sr$t.selectionEnd;
      var selr=sr$t.value.length-sele;
      var selt=sr$t.value.substring(sels,sele);

      if (sele>sels)
      {
        if (selt==selt.toUpperCase())
          selt=selt.toLowerCase()
        else if (selt==selt.toLowerCase() && sele-sels>1)
          selt=selt.substring(0,1).toUpperCase()+selt.substring(1).toLowerCase()
        else
          selt=selt.toUpperCase();

        sr$t.value = sr$t.value.substring(0,sels) + selt + sr$t.value.substring(sele);
        sr$t.selectionStart=sels;
        sr$t.selectionEnd=sele>sels ? sr$t.value.length-selr : sels;
      }
      srSync();
    }

    function srSync()
    {
        var i;
        var allLines=0;
        var lineNo=0;
        var w=sr$t.cols-5;

        var dummy=sr$t.value.split("\n");
        for (i=0;i<dummy.length;i++){allLines+=Math.ceil(dummy[i].length/w)}

        var dummy=sr$t.value.substring(0,sr$t.selectionStart).split("\n");
        for (i=0;i<dummy.length;i++){lineNo+=Math.ceil(dummy[i].length/w)}

    //    alert (w+" "+lineNo+"/"+allLines);

        sr$t.scrollTop=sr$t.scrollHeight*(lineNo-10)/allLines;
        sr$t.focus();
    }

    function srInit()
    {

      if(document.getElementById('wpTextbox1')) {

    var srBoxCode ='<form name="srForm"><table id="srBox" cellpadding="0" cellspacing="2">'
               +'<tr><td valign="bottom"><small>search for:</small><br/>'
               +'<input type="text" name="srSearch" accesskey="F" tabindex="8"'
               +'onkeypress="event.which == 13 && srNext()"; value=""/></td>'
               +'<td valign="bottom"><small>replace with:</small><br/><input type="text" name="srReplace" accesskey="G" tabindex="9"'
               +'onkeypress="event.which == 13 && srNext()"; value=""/></td>'
               +'<td valign="top">'
               +'<input type="checkbox" name="srCase" onclick="sr$t.focus()" tabindex="10"/><small>match case</small>'
               +'<input type="checkbox" name="srRegexp" onclick="sr$t.focus()" tabindex="11"/><small>use regexp</small><br/> '
               +'<a href="javascript:srBack()" onmouseover="sr$t.focus()" title="find previous match [alt-2]" accesskey="2">&lt;</a>&nbsp;'
               +'<a href="javascript:srNext()" onmouseover="sr$t.focus()" title="find next match [alt-3]" accesskey="3">find&nbsp;&nbsp;&gt;</a>&emsp;'
               +'<a href="javascript:srReplace();srBack()" onmouseover="sr$t.focus()" title="replace and find previous match [alt-4]" accesskey="4">&lt;</a>&nbsp;'
               +'<a href="javascript:srReplace()" onmouseover="sr$t.focus()" title="replace this match">replace</a>&nbsp;'
               +'<a href="javascript:srReplace();srNext()" onmouseover="sr$t.focus()" title="replace and find next match [alt-5]" accesskey="5">&gt;</a>&emsp;'
               +'<a href="javascript:srReplaceall()" onmouseover="sr$t.focus()" title="replace all matches [alt-7]" accesskey="7">replace&nbsp;all</a>&emsp;'
               +'<a href="javascript:srToggleCase()" onmouseover="sr$t.focus()" title="toggle selection case [alt-9]" accesskey="9">toggle&nbsp;case</a>'
               +'</td></tr></table></form>'

        var sr=document.createElement('div');

        sr.innerHTML=srBoxCode;
        var ep=document.getElementById('toolbar');
        if (!ep) ep= document.getElementById('editform');
        ep.parentNode.insertBefore(sr,ep);

        sr$t=document.editform.wpTextbox1;
        sr$f=document.srForm;
        sr$s=document.srForm.srSearch;
        sr$r=document.srForm.srReplace;
      }
    }

    document.write('<link rel="stylesheet" type="text/css" href="'
                 + 'http://en.wikipedia.org/w/index.php?title=User:Zocky/SearchBox.css'
                 + '&action=raw&ctype=text/css&dontcountme=s">');

    addOnloadHook(srInit);
    //</nowiki></pre>

Preuzeto sa: "http://bs.wikipedia.org../../../m/h/a/Korisnik%7EMhare_SearchBox.js_4a1f.html"

##### Views

- Korisnikova stranica
- Razgovor
- Trenutna verzija

##### interakcija

- Pomoć
- Vrata zajednice
- Čaršija
- Novosti
- Dobrovoljni prilozi

##### Pretraga