📄 treegrid.js
字号:
//rowMap => row_SerialIdMapItem
//rowMap2 => row_ItemIdMapSerialId
//colMap => header_SerialIdMapItem
//colMap2 => header_ItemIdMapSerialId
//cellMap => cells_SerialIdMapItem
Class("linb.UI.TreeGrid",["linb.UI","linb.absValue"],{
Instance:{
_setCtrlValue:function(value){
return this.each(function(profile){
if(!profile.domNode)return;
var box = profile.boxing(),
uiv = box.getUIValue(),
p = profile.properties,
k = p.activeMode=='row'?'CELLS':'CELL',
getN = function(k,i){return profile.getSubNode(k,i)},
getI = function(i){
var map1=profile.rowMap2;
if(p.activeMode=='row')
return map1[i];
else{
if(!i)return;
var r=i.split('|');
return _.get(profile.rowMap,[map1[r[0]],'_cells',r[1]]);
}
};
if(p.selMode=='single'){
var itemId = getI(uiv);
if(uiv && itemId)
getN(k,itemId).tagClass('-checked',false);
itemId = getI(value);
if(itemId)
getN(k,itemId).tagClass('-checked');
//scroll
if(itemId){
var o = getN(k,itemId);
if(o){
var top = o.offsetTop(),
items = getN('SCROLL'),
sh=items.scrollHeight(),
st=items.scrollTop(),
hh=items.height()
;
if(sh > hh)
if(top<st || top>st+hh)
items.scrollTop(top);
}
}
}else if(p.selMode=='multi'){
uiv = uiv?uiv.split(';'):[];
value = value?value.split(';'):[];
//check all
_.arr.each(uiv,function(o){
if(_.arr.indexOf(value,o)==-1)
getN(k, getI(o)).tagClass('-checked',false)
});
_.arr.each(value,function(o){
if(_.arr.indexOf(uiv,o)==-1)
getN(k, getI(o)).tagClass('-checked')
});
}
});
},
/*insert rows to dom
arr is formatted properties
pid,base are item id
before: insert before?
*/
_insertRowsToDom:function(profile, arr, pid, base, before){
//if parent not open, return
if(pid){
var parent = profile.rowMap[pid];
if(parent && !parent._created)return;
}
var obj,hw,
hw=profile.getSubNode('HCELL0').width();
//give width at here
_.arr.each(arr,function(o){
o._row0DfW = hw-o._layer*profile.properties.$subMargin;
_.arr.each(o.cells,function(v,i){
v.width=v._col.width;
})
});
//build dom
var nodes = _.str.toDom(profile.buildItems('rows', arr));
//get base dom
if(!base){
//no base add to parent
if(pid){
obj = profile.getSubNode('SUB', pid);
}else{
obj = profile.getSubNode('BODY');
}
if(before)
obj.prepend(nodes);
else
obj.append(nodes);
}else{
//
obj = profile.getSubNode('ROW', base);
if(before)
obj.addPrev(nodes);
else{
nodes.get().reverse();
obj.addNext(nodes);
}
}
//add sub
_.arr.each(arr,function(o){
o.open=false;
});
//clear rows cache
delete profile.$allrowscache;
},
_refreshHeader:function(header){
var profile=this.get(0),
pro=profile.properties,
rows = _.copy(pro.rows),
arr = profile.box._prepareHeader(profile, header),
nodes = _.str.toDom(profile.buildItems('header', arr));
pro.header = header;
this.removeAllRows();
profile.getSubNode('HCELL', true).remove(false);
profile.getSubNode('HCELLS').append(nodes);
this.insertRows(rows);
profile.box._ajdustBody(profile);
},
//pid,base are id
insertRows:function(arr, pid, base ,before){
var c=this.constructor, profile=this.get(0), pro=profile.properties, row_m=profile.rowMap2, t;
base = row_m[base];
if(base){
t=profile.rowMap[base];
if(t)pid=t._pid;
}
//prepareData(add links)
var rows = c._prepareItems(profile, arr, pid),
tar,
b=profile.rowMap;
pid = row_m[pid];
if(!pid)
tar = (pro.rows || (pro.rows=[]));
else
tar = (b[pid].sub || (b[pid].sub=[]));
if(!base)
_.arr.insertAny(tar,arr, before?0:-1);
else{
var index = _.arr.subIndexOf(tar,'_serialId', base);
_.arr.insertAny(tar,arr, before?index:(index+1));
}
//insert
this._insertRowsToDom(profile, rows, pid, base, before);
if(!pro.iniFold)
profile.boxing()._toggleRows(rows, true);
profile.box._asy(profile,false);
return this;
},
//delete row according to id
/*
linb.UI.TreeGrid.getAll().removeRows(['2','5'])
*/
removeRows:function(ids){
var self=this,
profile=self.get(0),
p=profile.properties,
cell=profile.cellMap,
nodes=[],v;
//get array
ids = _.isArr(ids)?ids:[ids];
_.arr.each(ids,function(id){
//get item id
if(!(id=profile.rowMap2[id]))return;
//get row
var row;
if(row = profile.rowMap[id]){
var tdids = row._cells,
rowid = row.id,
temp;
//for sub delete
if(row.sub){
var arr=[];
_.arr.each(row.sub,function(o){
arr.push(o.id)
});
self.removeRows(arr);
}
////delete and clear links
_.each(tdids,function(o,i){
//clear colMap/properties.header
delete cell[o]._col._cells[rowid];
//clear cellMap
delete cell[o];
profile.reclaimSubId(o.slice(2), 'cell');
});
//clear properties.row array
if(temp= row._pid?(temp=profile.rowMap[profile.rowMap2[row._pid]])?temp.sub:null:profile.properties.rows)
_.filter(temp,function(o){
return o._serialId != id;
});
//clear profile.rowMap2
delete profile.rowMap2[rowid];
//clear rowMap
delete profile.rowMap[id];
nodes.push(profile.getSubNode('ROW', id).get(0));
}
profile.reclaimSubId(id.slice(2), 'row');
});
// clear value
if(v=p.value){
if((v=v.split(';')).length>1){
_.filter(v,function(o){
return _.arr.indexOf(arr,o)==-1;
});
p.value=v.join(';');
}else{
if(_.arr.indexOf(arr,p.value)!=-1)
p.value=null;
}
}
linb(nodes).remove();
profile.box._asy(profile,false);
return self;
},
removeAllRows:function(){
var profile=this.get(0);
for(var i in profile.cellMap)
profile.reclaimSubId(i.slice(2), 'cell');
for(var i in profile.rowMap)
profile.reclaimSubId(i.slice(2), 'row');
_.each(profile.colMap,function(o){
o._cells={};
});
profile.rowMap={};
profile.cellMap={};
profile.rowMap2={};
profile.properties.rows.length=0;
profile.getSubNode('BODY').empty();
profile.getSubNode('SCROLL').scrollTop(0).scrollLeft(0);
return this;
},
updateCellByRowCol:function(rowId, colId, hash){
var t,self=this,con=self.constructor;
if(t=con._getCellId(self.get(0), rowId, colId))
con._updCell(self.get(0), t, hash);
return self;
},
updateCell:function(cellId, hash){
var self=this;
self.constructor._updCell(self.get(0),cellId,hash);
return self;
},
_toggleRows:function(rows, expend){
var self=this;
if(rows && rows.length)
_.arr.each(rows,function(o){
if(o.sub && o.sub.length && !o.iniFold && !o._checked)
self.toggleRow(o.id, expend);
});
},
toggleRow:function(id, expend){
var profile = this.get(0),
row = profile.rowMap[profile.rowMap2[id]];
if(row && !row._checked)
profile.box._setSub(profile, row, expend);
},
editCellbyRowCol:function(rowId, colId){
var profile=this.get(0),con=profile.box;
return con._editCell(profile, con._getCellId(profile, rowId, colId));
},
getCellbyRowCol:function(rowId, colId){
var profile=this.get(0),v;
v=_.get(profile.rowMap,[profile.rowMap2[rowId], '_cells',colId]);
return v?profile.cellMap[v]:null;
},
getRowbyRowId:function(rowId){
var profile=this.get(0),v=profile.rowMap2[rowId];
return v?profile.rowMap[v]:null;
},
resetGridValue:function(){
return this.each(function(profile){
_.each(profile.cellMap,function(v){
v.$value=v.value;
});
profile.getSubNode('CELLA',true).removeClass('ui-dirty');
})
},
resetRowValue:function(rowId){
var profile=this.get(0),row=this.getRowbyRowId(rowId),arr=[];
_.each(row.cells,function(o){
if(o.$value!==o.value){
o.$value=o.value;
arr.push(profile.getSubNode('CELLA',o._serialId).get(0));
}
});
linb(arr).removeClass('ui-dirty');
},
showColumn:function(colId, flag){
return this.each(function(profile){
var map=profile.colMap2,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -