Jump to content

User:Zocky/jsSandBox.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Zocky (talk | contribs) at 06:19, 11 September 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre><nowiki>
document.write('<script type="text/javascript" src="'
                + 'http://en.wikipedia.org/w/index.php?title=User:Zocky/Prototype.js'
                + '&action=raw&ctype=text/javascript&dontcountme=s\"><'+'/script>');

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


var jsSandBoxAcActive=false;
var jsSandBoxAcMatches=[];
var jsSandBoxAcNext=0;
var jsSandBoxAcStart=0;
var jsSandBoxCode;
var jsSandBoxScroll;

addLoadEvent (jsSandBoxInit);

function jsSandBoxInit()
{
  new Insertion.Top($('content'),'<div id="jsSandBox" style="display:none"><b>results</b> <a href="javascript:jsSandBoxClear()>(clear)</a></div>');
  new Insertion.Bottom($('jsSandBox'),'<div id="jsSandBoxScroll"></div>');
  new Insertion.Bottom($('jsSandBox'),'code <a href="javascript:jsSandBoxGo(jsSandBoxCode)">(eval)</a>');
  new Insertion.Bottom($('jsSandBox'),'<textarea id="jsSandBoxCode" rows="6"></textarea>');
  new Insertion.Bottom($('jsSandBox'),'<small>ctrl-enter to eval, ctr-space to autocomplete</small>');
  jsSandBoxCode=$('jsSandBoxCode');
  jsSandBoxScroll=$('jsSandBoxScroll');
  Event.observe(jsSandBoxCode,'keyup',jsSandBoxKey,true);
  new Insertion.Bottom($('t-specialpages').parentNode,'<li><a href="javascript:jsSandBoxToggle()">jsSandBox</a></li>');

  if (wgNamespaceNumber==2 && wgPageName.match(/\.js$/) && $('wpTextbox1'))
  {
    Event.observe($('wpTextbox1'),'keyup',jsSandBoxKey,false);
  }
}

function jsSandBoxToggle()
{
  $('jsSandBox').style.display = $('jsSandBox').style.display=='none' ? 'block' : 'none';
}

function jsSandBoxClear()
{
  jsSandBoxScroll.innerHTML='';
}

function jsSandBoxGo(target)
{
  $('jsSandBox').style.display='block';
  if (target.selectionStart!=target.selectionEnd)
  {
    code=target.value.substring(target.selectionStart,target.selectionEnd);
  }
  else
  {
    code=target.value;
  }

  var res = '<div class="jsSandBoxScrollCode" onclick="jsSandBoxRecall(this)">' 
  + code
  + '</div>';

  try
  {
    res += '<div class="jsSandBoxScrollResult">' 
    +   eval(code)
    + '</div>';
  }
  catch (err)
  {
    res += '<div class="jsSandBoxScrollError">' 
    +   '<b>' + err.name + '</b>: ' + err.message
    + '</div>';
  }
  
  new Insertion.Bottom(jsSandBoxScroll, 
      '<div class="jsSandBoxScrollItem">' 
    +   res 
    + '</div>'
  );

  jsSandBoxScroll.scrollTop=1e12;
  target.focus();
}

function jsSandBoxRecall(old)
{
  jsSandBoxCode.value=old.innerHTML;
  jsSandBoxCode.focus();
}

function jsSandBoxKey(e)
{
  keynum = window.event ? e.keyCode : e.which ;

  target=Event.element(e); 

  if (e.ctrlKey && keynum==13) { jsSandBoxGo(target);}
  if (e.ctrlKey && keynum==32)
  {
    if (!jsSandBoxAcActive)
    {
      var find = target.value.substr(0,target.selectionStart).match(/\w[a-zA-Z0-9\.]*$/);
      var where=find[0].split(/\./);
      var thing = window;
      try
      {
        for (var i=0;i<where.length-1;i++)
        {
          if (where[i]){thing=thing[where[i]]}
        }
        what=where[i];
        var j=0;
        jsSandBoxAcMatches=[];
        for (var bit in thing)
        {
          if ( bit.substr(0,what.length) == what)
          {
            jsSandBoxAcMatches[j]=bit;
            j++;
          }
        }
        if (j>0)
        {
          jsSandBoxAcNext=0;
          jsSandBoxAcStart=target.selectionStart-what.length;
          jsSandBoxAcActive=true;
        }
      }
      catch(e){return};
    }

    if(jsSandBoxAcActive)
    {
      var top=target.scrollTop;

      target.value = target.value.substr(0,jsSandBoxAcStart) 
                        + jsSandBoxAcMatches[jsSandBoxAcNext]
                        + target.value.substr(target.selectionStart);
      target.selectionStart=jsSandBoxAcStart+jsSandBoxAcMatches[jsSandBoxAcNext].length;
      target.selectionEnd=target.selectionStart;
      jsSandBoxAcNext=(jsSandBoxAcNext+1) % jsSandBoxAcMatches.length;

      target.scrollTop=top;
    }
  }
  else jsSandBoxAcActive=false;
}