var Class = {
create: function()
{
return function()
{
this.initialize.apply(this, arguments);
}
}
}
Array.prototype.del=function(n) {
if(n<0)
return this;
else
return this.slice(0,n).concat(this.slice(n+1,this.length));
}
var addTpl = Class.create();
addTpl.prototype =
{
formHtml:"",
innerFormHtml:"",
echoHtml:"",
isIE:true,
isCreate:true,
isEdit:false,
field:{},
values:{},
tmpvals:{},
index:0,
formId:"",
echoId:"",
submitId:"",
className:"tpl",//设置类名
indexName:"num",//设置索引名
tmpClassName:/\[tmpClassName\]/g,//把模板中的[tmpClassName]换成类名可以用到要触发事件.
tmpNumIdName:/\[tmpNumIdName\]/g,//把模板中的[tmpNumIdName]换成索引可以用到自定义ID.
tmpIndexNameId:/\[tmpIndexNameId\]/g,//把模板中的[tmpIndexNameId]换成索引名可以用到自定义ID.
error:"",
errid:0,
errcode:{
noNull:"不能为空!",
isNum:"必须是数值!",
noChk:"信息不合法.请检查表单!",
noReCreate:"您不能重复创建输入对话!",
noSubmit:"提交失败提示:\n\n",
noReEdit:"您不能重复创建编辑对话"
},
submitName:{
add:"添加",
edit:"编辑"
},
/**
* 构造函数
*
* @param string html 模板html代码(以js方式存储)
* @param string alertId 显示输入信息的ID
* @param string echoId 显示输出信息的ID
*
* @return void
*/
initialize : function (formHtml,echoHtml,formId,echoId)
{
this.isIE = !!document.all;
this.formHtml = formHtml||"";
this.echoHtml = echoHtml||"";
this.formId = formId||"";
this.echoId = echoId||"";
},
$ : function (obj){
return typeof(obj)=="object"?"obj":document.getElementById(obj);
},
HtmlEncode : function (text){
return text.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>');
},
set : function (idi){
var i=0;
for(field in this.field){
id=field+"_"+idi;
this.tmpvals[field]=this.$(id).value;
i++;
}
this.tmpvals['length']=i;
this.values[idi]=this.tmpvals;
//this.values.push(this.tmpvals);
},
get : function (){
for(field in this.values){
alert(field);
}
},
chkstr : function (id){
if(this.$(id).value==''){
this.errid++;
this.error+=this.errid+"."+this.$(id).getAttribute('msg')+this.errcode.noNull+"\n";
}else{
this.$(id).value=this.HtmlEncode(this.$(id).value);
}
},
chkint : function (id){
if(this.$(id).value==''){
this.errid++;
this.error+=this.errid+"."+this.$(id).getAttribute('msg')+this.errcode.noNull+"\n";
}else{
var reg=/^[\d|.]+$/;
if(!reg.test(this.$(id).value)){
this.errid++;
this.error+=this.errid+"."+this.$(id).getAttribute('msg')+this.errcode.noChk+"!\n";
}
}
},
chk : function (i){
this.errid=0;
this.error="";
for(field in this.field){
id=field+"_"+i;
val=this.field[field];
switch (val){
case "str":
this.chkstr(id);
break;
case "int":
this.chkint(id);
break;
case "arr":
alert(val);
break;
default:
this.error+=this.errcode.noChk;
break;
}
}
if(this.error==''){
return true;
}else{
return false;
}
},
edit : function (i){
if(!this.isCreate)alert(this.errcode.noReEdit);
if(this.formId!=''&&this.isCreate){
this.show();
this.isEdit=true;
tmpHTML=
this.formHtml
.replace(this.tmpNumIdName,i)
.replace(this.tmpClassName,this.className);
for(field in this.field){
valid="["+field+"_value]";
id=field+"_edit_"+i;
val=this.field[field];
if(val=='str'||val=='int'){
tmpHTML=tmpHTML.replace(valid,this.$(id).innerHTML);
}
}
this.$(this.formId).innerHTML=tmpHTML;
this.updateSubmitName();
}
},
del: function (i){
id=this.className+"_"+i;
this.values[i]={};
this.delHtml(id);
this.update();
},
delHtml : function(n){
h=this.$(n);
h.parentNode.removeChild(h);
return false;
},
create : function (){
if(!this.isCreate)alert(this.errcode.noReCreate);
if(this.formId!=''&&this.isCreate){
this.index++;
this.show();
tmpHTML=
this.formHtml
.replace(this.tmpNumIdName,this.index)
.replace(this.tmpClassName,this.className);
for(field in this.field){
valid="["+field+"_value]";
val=this.field[field];
if(val=='str'||val=='int'){
tmpHTML=tmpHTML.replace(valid,'');
}
}
this.$(this.formId).innerHTML=tmpHTML;
this.updateSubmitName();
}
},
update : function (){
tmpi=1;
for(var i=1,j=this.index;i<=j;i++){
if(typeof(this.values[i].length)!='undefined'){
id=this.className+"_"+this.indexName+"_"+i;
this.$(id).innerHTML=tmpi;
tmpi++;
}
}
},
updateSubmitName : function (){
if(this.submitId!=''){
if(this.$(this.submitId).tagName=='INPUT'&&this.$(this.submitId).type!="image"){
if(this.isEdit){
this.$(this.submitId).value=this.submitName.edit;
}else if(!this.isCreate){
this.$(this.submitId).value=this.submitName.add;
}
}else if(this.$(this.submitId).tagName=='INPUT'&&this.$(this.submitId).type=="image"){
if(this.isEdit){
this.$(this.submitId).src=this.submitName.edit;
}else if(!this.isCreate){
this.$(this.submitId).src=this.submitName.add;
}
}else{
if(this.isEdit){
this.$(this.submitId).innerHTML=this.submitName.edit;
}else if(!this.isCreate){
this.$(this.submitId).innerHTML=this.submitName.add;
}
}
}
},
print : function (i){
if(this.isEdit){
if(this.chk(i)){
this.set(i);
for(field in this.field){
valid="["+field+"_value]";
newId=field+"_"+i;
editId=field+"_edit_"+i;
val=this.field[field];
if(val=='str'||val=='int'){
this.$(editId).innerHTML=this.$(newId).value;
}
}
this.innerFORM(i);
this.hide();
}else{
alert(this.errcode.noSubmit+this.error);
}
}else if(!this.isCreate){
if(this.chk(this.index)){
this.set(this.index);
var tmpHTML=
this.echoHtml
.replace(this.tmpNumIdName,this.index)
.replace(this.tmpClassName,this.className)
.replace(this.tmpIndexNameId,this.indexName);
for(field in this.field){
valid="["+field+"_value]";
id=field+"_"+this.index;
val=this.field[field];
if(val=='str'||val=='int'){
tmpHTML=tmpHTML.replace(valid,this.$(id).value);
}
}
this.hide();
this.$(this.echoId).innerHTML += tmpHTML;
this.innerFORM(this.index);
}else{
alert(this.errcode.noSubmit+this.error);
}
}
this.update();
},
innerFORM : function (i){
if(this.innerFormHtml!=''){
tmpHTML=this.innerFormHtml;
for(field in this.field){
valid="["+field+"_value]";
val=this.field[field];
if(val=='str'||val=='int'){
tmpHTML=tmpHTML.replace(valid,this.values[i][field]);
}
}
id='form_'+this.className+'_'+i;
this.$(id).innerHTML=tmpHTML;
}
},
hide : function (){
this.$(this.formId).innerHTML = '';
this.$(this.formId).style.display = 'none';
this.isCreate=true;
this.isEdit=false;
},
show : function (){
this.$(this.formId).innerHTML = '';
this.$(this.formId).style.display = '';
this.isCreate=false;
}
}