wf.DataView=wf.extend(wf.BoxComponent,{
last:false,
singleSelect:true,
init:function(){
wf.DataView.superclass.init.call(this);
if(typeof this.tpl=="string"){
this.tpl=new wf.XTemplate(this.tpl);}
this.selected=new wf.MixedCollection(function(node){
return wf.id(node);});
this.all=new wf.MixedCollection(function(node){
return wf.id(node);});
this.addEvents('click','selectionchange');
this.el.on("click",this.onClick,this);
if(this.overCls){
this.el.on('mouseover',this.onMouseOver,this);
this.el.on('mouseout',this.onMouseOut,this);}
if(this.store){
this.setStore(this.store,true);}},
prepareData:function(data){
return data;},
collectData:function(records,startIndex){
var r=[];
for(var i=0,len=records.length;i<len;i++){
r[r.length]=this.prepareData(records[i].data,startIndex+i,records[i]);}
return r;},
bufferRender:function(records,index){
var div=document.createElement('div');
this.tpl.overwrite(div,this.collectData(records,index));
return wf.query(this.itemSelector,div);},
setStore:function(store,initial){
if(!initial&&this.store){
this.store.un("beforeload",this.onBeforeLoad,this);
this.store.un("datachanged",this.refresh,this);
this.store.un("add",this.onAdd,this);
this.store.un("remove",this.onRemove,this);
this.store.un("update",this.onUpdate,this);
this.store.un("clear",this.refresh,this);
this.store.un("failure",this.refresh,this);}
if(store){
store=wf.StoreMgr.lookup(store);
store.on("beforeload",this.onBeforeLoad,this);
store.on("datachanged",this.refresh,this);
store.on("add",this.onAdd,this);
store.on("remove",this.onRemove,this);
store.on("update",this.onUpdate,this);
store.on("clear",this.refresh,this);
store.on("failure",this.refresh,this);}
this.store=store;
if(store){
this.refresh();}},
onBeforeLoad:function(){
if(this.loadingText){
this.clearSelections(false);
this.el.update('<div class="loading-indicator">'+this.loadingText+'</div>');
this.all.clear();}},
onAdd:function(ds,records,index){
if(this.all.getCount()==0){
this.refresh();
return;}
var nodes=this.bufferRender(records,index),n;
if(index<this.all.getCount()){
n=wf.fly(this.all.itemAt(index)).insertSibling(nodes,'before',true);
this.all.items.splice(index,0,n);}else{
var el=wf.fly(this.all.last());
n=el.insertSibling(nodes,'after',true);
this.all.items.push(n);}
this.updateIndexes(index);},
onRemove:function(ds,record,index){
this.deselect(index);
var node=this.all.itemAt(index);
this.all.remove(node);
wf.removeNode(node);
this.updateIndexes(index);},
onUpdate:function(ds,record){
var index=this.store.indexOf(record);
if(index!=-1){
var sel=this.isSelected(index);
var original=this.all.items[index];
var node=this.bufferRender([record],index)[0];
original.parentNode.insertBefore(node,original);
wf.removeNode(original);
this.all.items.splice(index,1,node);
if(sel){
this.selected.items.splice(index,1,node);
wf.fly(this.all.itemAt(index)).addClass(this.selectedCls);}
this.updateIndexes(index,index);}},
updateIndexes:function(startIndex,endIndex){
var ns=this.all.items;
startIndex=startIndex||0;
endIndex=endIndex||((endIndex===0)?0:(ns.length-1));
for(var i=startIndex;i<=endIndex;i++){
ns[i].viewIndex=i;}},
findItemFromChild:function(node){
return wf.fly(node).findParent(this.itemSelector,this.el.dom);},
onClick:function(e){
var item=e.getTarget(this.itemSelector,this.el.dom);
if(item){
var index=this.indexOf(item);
if(this.onItemClick(item,index,e)!==false){
this.fireEvent("click",this,index,item,e);}}else{
this.clearSelections();}},
onMouseOver:function(e){
var item=e.getTarget(this.itemSelector,this.el.dom);
if(item&&item!==this.lastItem){
this.lastItem=item;
wf.fly(item).addClass(this.overCls);}},
onMouseOut:function(e){
if(this.lastItem){
wf.fly(this.lastItem).removeClass(this.overCls);
delete this.lastItem;}},
onItemClick:function(item,index,e){
if(!this.singleSelect){
this.doMultiSelection(item,index,e);
e.preventDefault();}else{
this.doSingleSelection(item,index,e);
e.preventDefault();}
return true;},
doSingleSelection:function(item,index,e){
if(e.ctrlKey&&this.isSelected(index)){
this.deselect(index);}else{
this.select(index,false);}},
doMultiSelection:function(item,index,e){
if(e.shiftKey&&this.last!==false){
var last=this.last;
this.selectRange(last,index,e.ctrlKey);
this.last=last;}else{
if((e.ctrlKey||this.simpleSelect)&&this.isSelected(index)){
this.deselect(index);}else{
this.select(index,e.ctrlKey||e.shiftKey||this.simpleSelect);}}},
deselect:function(node){
if(this.isSelected(node)){
var node=this.getNode(node);
this.selected.remove(node);
if(this.last==node.viewIndex){
this.last=false;}
wf.fly(node).removeClass(this.selectedCls);
this.fireEvent("selectionchange",this,this.selected.items);}},
select:function(nodeInfo,keepExisting,suppressEvent){
if(nodeInfo instanceof Array){
if(!keepExisting){
this.clearSelections(true);}
for(var i=0,len=nodeInfo.length;i<len;i++){
this.select(nodeInfo[i],true,true);}}else{
var node=this.getNode(nodeInfo);
if(!keepExisting){
this.clearSelections(true);}
if(node&&!this.isSelected(node)){
wf.fly(node).addClass(this.selectedCls);
this.selected.add(node);
this.last=node.viewIndex;
if(!suppressEvent){
this.fireEvent("selectionchange",this,this.selected.items);}}}},
selectRange:function(start,end,keepExisting){
if(!keepExisting){
this.clearSelections(true);}
this.select(this.getNodes(start,end),true);},
clearSelections:function(suppressEvent){
this.selected.each(function(node){
wf.fly(node).removeClass(this.selectedCls);},this);
this.selected.clear();
this.last=false;
if(!suppressEvent){
this.fireEvent("selectionchange",this,this.selected.items);}},
isSelected:function(node){
return this.selected.contains(this.getNode(node));},
indexOf:function(node){
node=this.getNode(node);
if(typeof node.viewIndex=="number"){
return node.viewIndex;}
return this.all.indexOf(node);},
getNode:function(nodeInfo){
if(typeof nodeInfo=="string"){
return document.getElementById(nodeInfo);}else if(typeof nodeInfo=="number"){
return this.all.itemAt(nodeInfo);}
return nodeInfo;},
getNodes:function(start,end){
var ns=this.all.items;
start=start||0;
end=typeof end=="undefined"?ns.length-1:end;
var nodes=[],i;
if(start<=end){
for(i=start;i<=end;i++){
nodes.push(ns[i]);}}else{
for(i=start;i>=end;i--){
nodes.push(ns[i]);}}
return nodes;},
getSelectedRecords:function(){
var r=[],s=this.selected.items;
for(var i=0,len=s.length;i<len;i++){
r[r.length]=this.store.getAt(s[i].viewIndex);}
return r;},
getSelectedIndexes:function(){
var indexes=[],s=this.selected.items;
for(var i=0,len=s.length;i<len;i++){
indexes.push(s[i].viewIndex);}
return indexes;},
refresh:function(){
this.clearSelections(false);
this.el.update("");
var html=[];
var records=this.store.getRange();
if(records.length<1){
this.el.update(this.emptyText);
this.all.clear();
return;}
this.tpl.overwrite(this.el,this.collectData(records,0));
this.all.clear();
this.all.addAll(wf.query(this.itemSelector,this.el.dom));
this.updateIndexes(0);}});