root > WikiSense-trunk > web > CategorySelector.js

CategorySelector.js

text/javascript, 6266 bytes (load raw)
/*
CategorySelector provides a convenient way to select a category.
Copyright (C) 2005, Daniel Kinzler, brightbyte.de

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


function getElementX( e ) {
    var x= 0;
   
    while ( e ) {
        x+= e.offsetLeft;
        e= e.offsetParent;
    }
   
    return x;
}

function getElementY( e ) {
    var x= 0;
   
    while ( e ) {
        x+= e.offsetTop;
        e= e.offsetParent;
    }
   
    return x;
}

function CategorySelector( target, field, wiki, root ) {
    this.rootCategory= root;
    this.wiki= wiki;
    this.selectorField= typeof field == 'string' ? document.getElementById(field) : field;
    this.category= this.selectorField.value;
    this.target= target;

    this.open= function ( field, selector, content ) {
        var cat= this.selectorField.value;
        cat= cat.replace( /^\s+|\s+$/, "" );
       
        if (cat == "") cat = this.rootCategory;
       
        if (cat == "") {
            alert("please choose a root category");
            return false;
        }
       
        this.load( cat );
        this.selectorDiv.style.top=  getElementY(this.selectorField) +'px';
        this.selectorDiv.style.left= getElementX(this.selectorField) + 'px';
        this.selectorDiv.style.display= 'block';
    }
   
    this.load= function ( cat ) {
        this.category= cat;
       
        //alert("loading "+cat);
       
        var u= 'CategorySelector.php?wiki=' + escape(this.wiki) + '&cat=' + escape( this.category ) + '&target=' + escape('javascript:' + this.target + '.load("%t")');
       
        loadSnippet(u, this.contentDiv);
    }

    this.close= function ( ) {
        this.selectorDiv.style.display= 'none';
    }
   
    this.apply= function ( ) {
        //alert("applying: "+this.category);
        this.selectorField.value= this.category;
    }
   
    this.attach= function ( label, tooltip ) {
        if ( this.wiki == "" || this.wiki == false || this.wiki == null ) return false;
   
        //insert selector code
        var okLnk= document.createElement("a");
        okLnk.onclick= function() { sel.apply(); sel.close(); return false; };
        okLnk.href= 'javascript:void(0)';
        okLnk.innerHTML= "OK";
       
        var cancelLnk= document.createElement("a");
        cancelLnk.onclick= function() { sel.close(); return false; };
        cancelLnk.href= 'javascript:void(0)';
        cancelLnk.innerHTML= "Cancel";
       
        var bottom= document.createElement("div");
        bottom.className= 'catselButtons';
        bottom.appendChild( okLnk );
        bottom.appendChild( document.createTextNode(' ') );
        bottom.appendChild( cancelLnk );
       
        this.contentDiv= document.createElement("div");
        this.contentDiv.className= 'catselContent';
        this.contentDiv.style.overflow= 'auto';
        this.contentDiv.style.width= '70ex';
        this.contentDiv.style.height= '16ex';
        this.contentDiv.style.padding= '1ex';
       
        this.selectorDiv= document.createElement("div");
        this.selectorDiv.className= 'catselBox';
       
        this.selectorDiv.appendChild(bottom);
        this.selectorDiv.appendChild(this.contentDiv);
       
        this.selectorDiv.style.display= 'none';
        this.selectorDiv.style.position= 'absolute';
        this.selectorDiv.style.borderWidth= '1px';
        this.selectorDiv.style.borderStyle= 'solid';
        this.selectorDiv.style.borderColor= '#888888;';
        //this.selectorDiv.style.padding= '1ex';
        //this.selectorDiv.style.width= '70ex';
        //this.selectorDiv.style.height= '22ex';
        this.selectorDiv.style.backgroundColor= '#F6F6F6';
       
        var p;
        if (this.selectorField.parentElement) p= this.selectorField.parentElement;
        else if (this.selectorField.parentNode) p= this.selectorField.parentNode;
        else alert('failed to attach CategorySelector (unable to detect parent element)');
       
        //alert(p);
       
        p.insertBefore( this.selectorDiv, this.selectorField.nextSibling );
       
        //insert link
        var sel= this;
       
        var lnk= document.createElement("a");
        lnk.onclick= function() { sel.open(); };
        lnk.title= tooltip;
        lnk.href= 'javascript:void(0)';
        lnk.innerHTML= label;
       
        var sp= document.createElement("span");
        sp.appendChild( document.createTextNode(" ") );
        sp.appendChild( lnk );
        sp.appendChild( document.createTextNode(" ") );
       
        p.insertBefore( sp, this.selectorField.nextSibling );
    }
}

function loadSnippet(url, div) {
  var page_request = false;
 
  div.innerHTML= "<i class='loading'>loading...</i>";
 
  if (window.XMLHttpRequest) // if Mozilla, Safari etc
    page_request = new XMLHttpRequest();
  else if (window.ActiveXObject){ // if IE
    try {
      page_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
      try{
        page_request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e){}
    }
  }
  else {
    div.innerHTML= "<div class='error'>Sorry, your browser does not support loading page snippets dynamically.</div>";
    return false;
  }
   
  page_request.onreadystatechange= function() {
    if (page_request.readyState != 4) return;
   
    var html= false;
   
    if ( page_request.status==200 ) html= "<!-- "+url+"-->" + page_request.responseText;
    else html= "failed to load "+url+" (code "+page_request.status+")";
   
    div.innerHTML= html;
  };
 
  page_request.open('GET', url, true);
  page_request.send(null);
}