User:WhoSaidThat/vector.js

From Wikispecies
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
(function( $ ){
  var methods = {
    init : function( options ){

      /* currently wiki-ing an element does nothing to it */

      /* currently a wiki-ed element has no special properties */
      
      /*  perhaps options should trigger avaible methods */
      
      return this;
    },
    w2h : function(){

      /* find wikitext within this elements html and replace with its html equivalent */ 
    
      var html=this.html();
      html=html.replace(/=====([^=]+)=====/g , '<h5>$1</h5>' );
      html=html.replace(/====([^=]+)====/g , '<h4>$1</h4>' );
      html=html.replace(/===([^=]+)===/g , '<h3>$1</h3>' );
      html=html.replace(/==([^=]+)==/g , '<h2>$1</h2>' );
      html=html.replace(/=([^=]+)=/g , '<h1>$1</h1>' );
      html=html.replace(/'''([^']+)'''/g , '<b>$1</b>' );
      html=html.replace(/''([^']+)''/g , '<i>$1</i>' );
      html=html.replace(/\[\[([^\|\]]+)||([^\]]+)\]\]/g , '<a href="/wiki/$1" target="_blank">$2</a>' );
      html=html.replace(/\[\[([^\]]+)\]\]/g,'<a href="/wiki/$1" target="_blank">$1</a>' );
      
      this.html(html);
      
      return this;
    },
    find: function ( type ){
      
      switch(type){
        case 'links':
          return this.find('a[href^="/wiki/]').filter(function(i){ return this.href.indexOf(':')<0; });
          break;
        case 'users':
          return this.find('a[href^="/wiki/User:"]');
          break;
        case 'special':
          return this.find('a[href^="/wiki/Special:"]');
          break;
        case 'categories':
          return this.find('a[href^="/wiki/Category:"]');
          break;
        case 'red-links':
          return this.find('a.new[href*="redlink"]');
          break;
        case 'infobox':
          return this.find('.infobox');
          break;
        case 'references':
          return this.find('.references').find('li');
          break;
      }
      
    },
    api : function ( params,	callback,	error,	context ) {
      
      /* call the wiki api */
      
      var url='';
      
      for(var i in params) url+='&'+i+'='+encodeURIComponent(params[i]);
      
      url='http:'+wgServer+'/w/api.php?format=json'+url;
      
      context=(typeof(context)=='object')?context:{};
           
      $.ajax({url:url, context:context, success:callback, error:error, dataType:'json'});
    
    },
    importScripts: function ( pages,	forceRefresh,	basePath ) {
      
      var basePath=basePath?basePath:"http:"+wgServer+wgScript;
      var order={
        pages:	pages,
        success:	[],
        index:	0,
        returnValues:	[],
        forceRefresh:	forceRefresh,
        basePath:	basePath,
        getNext:	function(){
                        
          if(typeof(this.pages[this.index])=='function'){
  
            this.returnValues[this.index]=this.pages[this.index](this);
            this.index++; this.getNext();
              
          }else{
            
            var sFn=function(data){
              this.returnValues[this.index]=data;
              this.success[this.index]=true;
              this.index++;
              this.getNext();
            };
            
            var fFn=function(a,b,c){
              this.returnValues[this.index]=[a,b,c];
              this.success[this.index]=false;
              this.index++;
              this.getNext();
            };
            
            $().wiki('userScript',this.pages[this.index],sFn,fFn,this,this.forceRefresh,this.basePath);
          
          }
          return;
          
        }
      };
      
      order.getNext();
    
    },
    
    'userScript' : function(page,callback,error,context,force,base){
      if(!page) return false;
      if(!base) base='http:'+wgServer+wgScript;
      if(!callback) callback=(function(){});
      if(!context) context=({});
      if(page.indexOf('http://')!=0  && page.indexOf('https://')!=0){
        page = base + "?title=" + page.replace(/ /g, "_").replace(/%2F/gi, "/").replace(/%3A/gi, ":");
        page +=  "&action=raw&ctype=text/javascript";
      }
      if(force){
        if(typeof force != 'boolean') force=force+''; else force='aZrandom';
        if(page.indexOf('?')<0) page+='?';
        page += '&' + force + '='+(new Date()).getTime();
      }
      
      $.ajax({'url':page,'success':callback,'success':callback,'context':context,'error':error,'dataType':'script'});
    }
  };
  
  $.fn.wiki = function( method ) {
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.wiki' );
    }    
  
  };


})( jQuery );




$().wiki('importScripts',['User:WhoSaidThat/CodeMirror.js',
                          'http://codemirror.net/lib/util/overlay.js',
                          'User:WhoSaidThat/CodeMirrorInterface.js',
                          'http://codemirror.net/mode/javascript/javascript.js',
                          'http://codemirror.net/mode/css/css.js',
                          'http://codemirror.net/mode/xml/xml.js',
                          'http://codemirror.net/mode/htmlmixed/htmlmixed.js',
                          'http://codemirror.net/lib/util/foldcode.js',
                          'http://codemirror.net/lib/util/simple-hint.js',
                          'http://codemirror.net/lib/util/javascript-hint.js',whenReady ], true); 

function whenReady(){

  var fileExt=wgPageName.split('.');
  fileExt=fileExt[fileExt.length-1];
  var ta=document.getElementById('wpTextbox1');
  
  if(ta){
    
    window.editor=new CodeMirrorInterface( ta , fileExt);
    
    var toolbar=$('<div id="CodeMirrorInterface-buttons"></div>');
    toolbar.append($('<input id="CodeMirrorInterface-find">'));
    toolbar.append($('<input id="CodeMirrorInterface-search" type="button" value="Find">'));
    toolbar.append($('<input id="CodeMirrorInterface-new">'));
    toolbar.append($('<input id="CodeMirrorInterface-replace1" type="button" value="Replace">'));
    
    toolbar.append($('<input id="CodeMirrorInterface-replace" type="button" value="Replace All">'));
    
    /*var t=$('#editpage-copywarn').offset().top;
    var l=$('#editpage-copywarn').offset().left;
    
    toolbar.css( { position: 'absolute', top: t, left:l , 'z-index':10000} );

    $('body').append(toolbar);*/
    
    toolbar.insertBefore('#editpage-copywarn');
    
    //$('#CodeMirrorInterface-find');
    $('#CodeMirrorInterface-search').click(function(){
      window.editor.search($('#CodeMirrorInterface-find').val());   
      return false;
    });
    $('#CodeMirrorInterface-replace').click(function(){
      window.editor.replaceAll( $('#CodeMirrorInterface-find').val(),	$('#CodeMirrorInterface-new').val() );   
      return false;
    });
    $('#CodeMirrorInterface-replace1').click(function(){
      window.editor.replaceSingle( $('#CodeMirrorInterface-find').val(),	$('#CodeMirrorInterface-new').val() );   
      return false;
    });

  }
  
  
}