$package("com.qn.ui");

com.qn.ui.FileListView = function (){
	this.multiSelected = true;
	if(js.ui.list.Columns)
		this.columns =  new js.ui.list.Columns();
}
com.qn.ui.FileListView.prototype = new js.ui.list.AbstractListView();

com.qn.ui.FileListView.prototype.renderClass = com.qn.ui.FileIconRenderer;

com.qn.ui.FileListView.prototype.viewType = 0;

com.qn.ui.FileListView.prototype.list = function(path, exts, isListDirs, startRecord){
	var __this = this;
	this.getFile().onerror = function(msg){
		alert(msg);
	}
	this.getFile().onload = function(files){
		__this.clear();
		__this.fillFiles(files);
	}
	this.getFile().setPath(path);
	this.getFile().list(exts, isListDirs, startRecord);
}

com.qn.ui.FileListView.prototype.parseText = function(responseText){
	var __this = this;
	this.getFile().onerror = function(msg){
		alert(msg);
	}
	this.getFile().onload = function(files){
		__this.clear();
		__this.fillFiles(files);
	}
	this.getFile().doListed(responseText);
}

com.qn.ui.FileListView.prototype.refresh = function(){
	this.list(this.getFile().getPath());
}

com.qn.ui.FileListView.prototype.file = null;

com.qn.ui.FileListView.prototype.getFile = function(){
	if(!this.file){
		this.addListener(810);
		this.file = new com.qn.io.File();
	}
	return this.file;
}

com.qn.ui.FileListView.prototype.fillFiles = function(files){
	if(this.onlist)
		this.onlist(files);
	var f;
	for(var i=0; i<files.length; i++){
		f = files[i];
		this.addItem(f);
	}
	if(this.onlisted)
		this.onlisted(files);
}

com.qn.ui.FileListView.prototype.processMessage = function (msg, action, src){
	if(msg==810){
		if(this.getFile().getPath()!=src.getParent() && !this.isRecycle()) return;
		if(action==0){//new
			this.refresh();
		}
		else if(action==1){//rename
			this.refresh();
		}
		else if(action==2){//delete
			this.refresh();
		}
		else if(action==6){//recovery
			this.refresh();
		}
	}
}

com.qn.ui.FileListView.prototype.getSelFileIds = function (){
	var sels = this.getSelectedValues();
	var fns = "";
	for(var i=0; i<sels.length; i++){
		fns += sels[i].fileId + ":";
	}
	return fns;
}
com.qn.ui.FileListView.prototype.buildDefColumns = function (){
	var __this = this;
	var column = new js.ui.list.Column();
	column.caption = "";
	column.width = "16";
	column.colSpan = 2;
	column.renderClass = js.ui.list.CheckBoxColumnCellRenderer;
	this.addColumn(column);
	this.selectAllCheckBox = column.renderer.checkbox;
	column.renderer.checkbox.onclick = function(){
		if(this.checked)
			__this.selectAll();
		else
			__this.clearSelection();
	}


	var column = new js.ui.list.Column();
	column.caption = props("Name");
	column.width = "270";
	column.colSpan = 2;
	this.addColumn(column);

	var column = new js.ui.list.Column();
	column.caption = props("Size");
	column.width = "100";
	this.addColumn(column);

	var column = new js.ui.list.Column();
	column.caption = props("Date");
	column.width = "100";
	this.addColumn(column);
}

com.qn.ui.FileListView.prototype.buildDefExColumns = function (){
	var column = new js.ui.list.Column();
	column.caption = props("Downloads");
	column.width = "100";
	this.addColumn(column);

	var column = new js.ui.list.Column();
	column.caption = props("Edit");
	column.width = "50";
	this.addColumn(column);

	var column = new js.ui.list.Column();
	column.caption = props("Link");
	column.width = "50";
	this.addColumn(column);
}

com.qn.ui.FileListView.prototype.bindPopupMenu = function (){
	var __this = this;
	this.ui.super_oncontextmenu = js.ui.Component.default_oncontextmenu;
    this.ui.oncontextmenu = function(event){
		this.owner.doPopupMenu();
		return this.super_oncontextmenu(event);
	}
	this.ui.ondblclick = function(event){
		if(__this.popupMenu)
			__this.popupMenu.hide();
	}
/*	this.ui.onclick = function(event){
		if(__this.getSelFileIds().length>0){
			__this.doPopupMenu();
			return this.super_oncontextmenu(event);
		}
	}*/

}

com.qn.ui.FileListView.prototype.doPopupMenu = function (){
	if(this.isRecycle()){
		if(this.getSelFileIds().length>0){
			this.popupMenu = this.getRecyclePopMenu();
		}
	}
	else {
		if(this.getSelFileIds().length>0){
			this.popupMenu = this.getSelPopMenu();
		}
		else{
			this.popupMenu = this.getWhileSpacePopMenu();
		}
	}
}

com.qn.ui.FileListView.prototype.getSelPopMenu = function (){
	if(this.selPopMenu) return this.selPopMenu;
	var __this = this;

	var popmenu = this.selPopMenu = new js.ui.PopupMenu();
	popmenu.hide();
	rootPanel.appendChild(popmenu.getUI());

	var item = new js.ui.MenuItem();
	item.setText(props("Open"));
	popmenu.add(item);
	item.onclick = function(){
		var selFile = __this.getSelectedValues()[0];
		if(selFile!=null){
			__this.openWithExplorer(selFile.getPath());
		}
	}

	popmenu.addSeparator();
	var item = new js.ui.MenuItem();
	item.setText(props("Link"));
	popmenu.add(item);
	item.onclick = function() {
		__this.link();
	}

	popmenu.addSeparator();
	var item = new js.ui.MenuItem();
	item.setText(props("Move"));
	popmenu.add(item);
	item.onclick = function(){
		__this.moveSels();
	}

	popmenu.addSeparator();
	var item = new js.ui.MenuItem();
	item.setText(props("Delete"));
	popmenu.add(item);
	item.onclick = function(){
		__this.deleteSels();
	}

	var item = new js.ui.MenuItem();
	item.setText(props("Rename"));
	popmenu.add(item);
	item.onclick = function(){
		var selFile = __this.getSelectedValues()[0];
		if(selFile!=null){
			__this.onedit(selFile);
		}
	}

	popmenu.addSeparator();
	var item = new js.ui.MenuItem();
	item.setText(props("Properties"));
	popmenu.add(item);
	item.onclick = function(){
		__this.property();
	}
	return this.selPopMenu;
}

com.qn.ui.FileListView.prototype.getWhileSpacePopMenu = function (){
	if(this.whileSpacePopMenu) return this.whileSpacePopMenu;
	var __this = this;

	var popmenu = this.whileSpacePopMenu = new js.ui.PopupMenu();
	popmenu.hide();
	rootPanel.appendChild(popmenu.getUI());

	var item = new js.ui.MenuItem();
	item.setText(props("Upload"));
	popmenu.add(item);
	item.onclick = function(){
		__this.upload();
	}
	var item = new js.ui.MenuItem();
	item.setText(props("Batch Upload"));
	popmenu.add(item);
	item.onclick = function(){
		__this.batchUpload();
	}
	var item = new js.ui.MenuItem();
	item.setText(props("Remote Upload"));
	popmenu.add(item);
	item.onclick = function(){
		__this.remoteUpload();
	}

	popmenu.addSeparator();
	var item = new js.ui.MenuItem();
	item.setText(props("Refresh"));
	popmenu.add(item);
	item.onclick = function(){
		__this.refresh();
	}

	popmenu.addSeparator();
	var item = new js.ui.MenuItem();
	item.setText(props("New Folder"));
	popmenu.add(item);
	item.onclick = function(){
		__this.newFolder();
	}

/*	var item = new js.ui.MenuItem();
	item.setText(props("New File"));
	popmenu.add(item);*/

	popmenu.addSeparator();
	var item = new js.ui.MenuItem();
	item.setText(props("Properties"));
	popmenu.add(item);
	item.onclick = function(){
		__this.property();
	}
	return this.whileSpacePopMenu;
}

com.qn.ui.FileListView.prototype.getRecyclePopMenu = function (){
	if(this.recyclePopMenu) return this.recyclePopMenu;
	var __this = this;

	var popmenu = this.recyclePopMenu = new js.ui.PopupMenu();
	popmenu.hide();
	rootPanel.appendChild(popmenu.getUI());

	var item = new js.ui.MenuItem();
	item.setText(props("Recovery"));
	popmenu.add(item);
	item.onclick = function(){
		__this.recoverySels();
	}
	popmenu.addSeparator();
	var item = new js.ui.MenuItem();
	item.setText(props("Properties"));
	popmenu.add(item);
	item.onclick = function(){
		__this.property();
	}
}


com.qn.ui.FileListView.prototype.onclickItem = function(item){
	var f = item.userObject;
	if(f.isDirectory() && !this.isRecycle()){
		this.list(f.getTarget());
	}
}

com.qn.ui.FileListView.prototype.openWithExplorer = function (path){

}

com.qn.ui.FileListView.prototype.newFolder = function (path){
	var __this = this;
	var dlg = new com.qn.dlg.UrlDlg();
	dlg.setActive(true);
	dlg.ui.className += " NewFolderDlg";
	dlg.setTitle(props("New Folder"));
	rootPanel.appendChild(dlg.ui);
	dlg.center();
	dlg.onload = function(contentWindow){
		contentWindow.parentPath = __this.getFile().getPath();
	}
	dlg.openUrl("/explorer/newFolder.html");
}

com.qn.ui.FileListView.prototype.deleteSels = function (){
	var selCount = this.getSelectedIndices().length;
	if(selCount<=0) return;
	if(window.confirm(props("Are you sure delete?"))){
		var selFile = this.getSelectedValues()[0];
		selFile.deleteFile(this.getSelFileIds());
	}
}

com.qn.ui.FileListView.prototype.onedit = function (f){
	var __this = this;
	var dlg = new com.qn.dlg.UrlDlg();
	dlg.setActive(true);
	dlg.ui.className += " EditFileDlg";
	rootPanel.appendChild(dlg.ui);
	dlg.center();
	dlg.onload = function(contentWindow){
		contentWindow.getData(f.fileId);
	}
	dlg.setTitle(props("File Edit"));
	dlg.openUrl("/explorer/editFile.html");
}

com.qn.ui.FileListView.prototype.moveSels = function (){
	var __this = this;
	var selCount = this.getSelectedIndices().length;
	if(selCount<=0) return;
	var dlg = new com.qn.dlg.DirDlg();
	document.body.appendChild(dlg.getUI());
	dlg.showModal();
	dlg.center();
	dlg.list();
	dlg.onclose = function(){
		if(this.result==1) {//ok
			var currentPath = this.currentPath;
			var selFile = __this.getSelectedValues()[0];
			return selFile.move(__this.getSelFileIds(), currentPath);
		}
		return true;
	}
}

com.qn.ui.FileListView.prototype.upload = function (){
	var __this = this;
	var dlg = new com.qn.dlg.UrlDlg();
	dlg.setActive(true);
	dlg.ui.className += " UploadDlg";
	rootPanel.appendChild(dlg.ui);
	dlg.center();
	dlg.onload = function(contentWindow){
		if(contentWindow.setParentPath)
			contentWindow.setParentPath(__this.getFile().getPath());
	}
	dlg.setTitle(props("File Upload"));
	dlg.openUrl("/view?module=upload&action=uploadDlg");
}

com.qn.ui.FileListView.prototype.batchUpload = function (){
	var __this = this;
	var dlg = new com.qn.dlg.UrlDlg();
	dlg.setActive(true);
	dlg.ui.className += " BatchUploadDlg";
	rootPanel.appendChild(dlg.ui);
	dlg.center();
	dlg.onload = function(contentWindow){
		if(contentWindow.setParentPath)
			contentWindow.setParentPath(__this.getFile().getPath());
	}
	dlg.setTitle(props("Batch Upload"));
	dlg.openUrl("/view?module=flashUpload&action=uploadDlg");
}

com.qn.ui.FileListView.prototype.remoteUpload = function (){
	var __this = this;
	var dlg = new com.qn.dlg.UrlDlg();
	dlg.setActive(true);
	dlg.ui.className += " RemoteUploadDlg";
	rootPanel.appendChild(dlg.ui);
	dlg.center();
	dlg.onload = function(contentWindow){
		if(contentWindow.setParentPath)
			contentWindow.setParentPath(__this.getFile().getPath());
	}
	dlg.setTitle(props("Remote Upload"));
	dlg.openUrl("/view?module=remoteUpload&action=uploadDlg");
}

com.qn.ui.FileListView.prototype.property = function (){
	var __this = this;
	var dlg = new com.qn.dlg.UrlDlg();
	dlg.getUI().className += " PropertyDlg";
	rootPanel.appendChild(dlg.ui);
	dlg.center();
	dlg.onload = function(contentWindow){
		var selFile = __this.getSelectedValues()[0];
		if(selFile)
			contentWindow.getData(selFile.fileId);
		else
			contentWindow.getData(__this.getFile().fileId, __this.getFile().path);
	}
	dlg.setTitle(props("Properties"));
	dlg.setActive(true);
	dlg.openUrl("/explorer/attribute.html");
}

com.qn.ui.FileListView.prototype.link = function (){
	var __this = this;
	var selFile = __this.getSelectedValues()[0];
	if(selFile) {
		var dlg = new com.qn.dlg.UrlDlg();
		dlg.getUI().className += " LinkDlg";
		rootPanel.appendChild(dlg.ui);
		dlg.center();
		dlg.setTitle(props("Link"));
		dlg.setActive(true);
		dlg.openUrl("/explorer/getLinkDlg/"+selFile.userId+"/"+selFile.fileId+".html");
	}
}
com.qn.ui.FileListView.prototype.isRecycle = function(){
	return this.getFile().path=="Recycle";
}

com.qn.ui.FileListView.prototype.recoverySels = function(){
	var selCount = this.getSelectedIndices().length;
	if(selCount<=0) return;
	var selFile = this.getSelectedValues()[0];
	selFile.recovery(this.getSelFileIds());
}
com.qn.ui.FileListView.prototype.clear = function(){
	this.__superClear = js.ui.list.AbstractListView.prototype.clear;
	this.__superClear();
	if(this.selectAllCheckBox)
		this.selectAllCheckBox.checked = false;
}

