一段自以为写得不错的JS代码

// expand event:
//
        mouseClick            plus/minus
//
        rowSelected            row
ctit.treeNode = Class.create({
    initialize:
function(element, options) {
        
this.element = $(element);
        
this.options = options;
        
this.eventMouseClick = this.options.mouseClick.bindAsEventListener(this);
        
this.eventRowSelected = this.rowSelected.bindAsEventListener(this);
        
this._onRequest = false;
        Event.observe(
this.element, "click"this.eventMouseClick);
        Event.observe(
this.options.row, "dblclick"this.eventMouseClick);
        Event.observe(
this.options.row, "click"this.eventRowSelected);
    }
,
    destroy:
function(){
        Event.stopObserving(
this.element, "click"this.eventMouseClick);
        Event.stopObserving(
this.options.row, "dblclick"this.eventMouseClick);
        Event.stopObserving(
this.options.row, "click"this.eventRowSelected);
    }
,
    rowSelected:
function(event){
        
var cs = this.options.table.getElementsByClassName(this.options.currentSelectedClass);
        
for(var i=0; i<cs.length; i++){
            Element.removeClassName(cs[i], 
this.options.currentSelectedClass);
            cs[i].style.backgroundColor 
= '#fff';
        }

        
var tds = this.options.row.cells;
        
for(var i=0; i<tds.length; i++){
            Element.addClassName(tds[i], 
this.options.currentSelectedClass);
            tds[i].style.backgroundColor 
= '#d7e2e8';
        }

    }
,
    ajaxRequest:
function(url, param, onSuccessCallback){
        
if(!this._beginRequest()) {
            
return;
        }

        
if(this._changePlusMinusIcon()){
            param.ajax 
= true;
            pThis 
= this;
            
new Ajax.Request(url, {
                method: 
'post',
                postBody: $H(param).toQueryString(),
                onSuccess: 
function(o){
                    
if(typeof(onSuccessCallback) === 'function'){
                        
var ls = o.responseText.evalJSON();
                        onSuccessCallback(ls, pThis.options.tBody, pThis.options.row);
                    }

                }

                onFailure: 
function(o){
                    alert(
"Connect to server failure.");
                }
,
                onComplete: 
function(o){
                    pThis._endRequest();
                }
,
                evalScripts:
false
            }
);
        }
else{
            
this._endRequest();
        }

    }
,
    _changePlusMinusIcon:
function(){
        
var imgs = this.element.getElementsByTagName('img');
        
if(!imgs) return false;

        
if(imgs[0].src.indexOf('inactive_img.gif')>0){
            
var src = imgs[0].src.replace(/\binactive_img.gif\b/'active_img.gif');
            imgs[
0].src = src;
            imgs[
0].alt = 'Open';
            
this._removeItWithChildren();
            
return false;
        }
else{
            
var src = imgs[0].src.replace(/\bactive_img.gif\b/'inactive_img.gif');
            imgs[
0].src = src;
            imgs[
0].alt = 'Close';
            
return true;
        }

    }
,
    _beginRequest:
function(){
        
if(this._onRequest) return false;
        
this._onRequest = true;
        
return true;
    }
,
    _endRequest:
function(){
        
this._onRequest = false;
    }
,
    _removeItWithChildren:
function(){
        
var row = this.options.row;
        
var level = this._getLevel(row);
        
var tBody = this.options.tBody;
        
var i=0;
        
while(true){
            
var nextRow = row.nextSibling;
            
if(isNull(nextRow)) break;
            
var pLevel = this._getLevel(nextRow);
            
if(pLevel>level){
                tBody.removeChild(nextRow);
            }
else{
                
break;
            }

        }

    }
,
    _getLevel:
function(row){
        
if(!isNull(row)) {
            
var o = row.cells[this.options.treeNodeIndex];
            
if(o){
                
var mtch = o.className.match(/\bcontent_indent([0-9]+)\b/);
                
if(mtch && mtch[1]){
                    
return parseInt(mtch[1]);
                }

            }

        }

        
return 0;
    }

}
);
原文地址:https://www.cnblogs.com/afxcn/p/1089572.html