wf.Format=function(){
var trimRe=/^\s+|\s+$/g;
return{
ellipsis:function(value,len){
if(value&&value.length>len){
return value.substr(0,len-3)+"...";}
return value;},
defaultValue:function(value,defaultValue){
return value!==undefined&&value!==''?value:defaultValue;},
htmlEncode:function(value){
return !value?value:String(value).replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;");},
htmlDecode:function(value){
return !value?value:String(value).replace(/&amp;/g,"&").replace(/&gt;/g,">").replace(/&lt;/g,"<").replace(/&quot;/g,'"');},
trim:function(value){
return String(value).replace(trimRe,"");},
func:function(v,fn){if(fn){return fn(v);}},
substr:function(value,start,length){
return String(value).substr(start,length);},
lowercase:function(value){
return String(value).toLowerCase();},
uppercase:function(value){
return String(value).toUpperCase();},
capitalize:function(value){
return !value?value:value.charAt(0).toUpperCase()+value.substr(1).toLowerCase();},
usMoney:function(v){
v=(Math.round((v-0)*100))/100;
v=(v==Math.floor(v))?v+".00":((v*10==Math.floor(v*10))?v+"0":v);
v=String(v);
var ps=v.split('.');
var whole=ps[0];
var sub=ps[1]?'.'+ps[1]:'.00';
var r=/(\d+)(\d{3})/;
while(r.test(whole)){
whole=whole.replace(r,'$1'+','+'$2');}
v=whole+sub;
if(v.charAt(0)=='-'){
return '-$'+v.substr(1);}
return "$"+v;},
date:function(v,format){
if(!v){
return "";}
if(!(v instanceof Date)){
v=new Date(Date.parse(v));}
return v.dateFormat(format||"m/d/Y");},
dateRenderer:function(format){
return function(v){
return wf.Format.date(v,format);};},
number:function(v,scale){
if(!v||isNaN(v))return "";v=parseFloat(v);scale=(scale>0)?scale:0;
if(v){v=v.toFixed(scale);}return v;},
stripTagsRE:/<\/?[^>]+>/gi,
stripTags:function(v){
return !v?v:String(v).replace(this.stripTagsRE,"");},
stripScriptsRe:/(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,
stripScripts:function(v){
return !v?v:String(v).replace(this.stripScriptsRe,"");},
fileSize:function(size){
if(size<1024){
return size+" bytes";}else if(size<1048576){
return(Math.round(((size*10)/1024))/10)+" KB";}else{
return(Math.round(((size*10)/1048576))/10)+" MB";}},
math:function(){
var fns={};
return function(v,a){
if(!fns[a]){
fns[a]=new Function('v','return v '+a+';');}
return fns[a](v);}}()}}();
wf.Template=function(html){
var a=arguments;
if(html instanceof Array){
html=html.join("");}else if(a.length>1){
var buf=[];
for(var i=0,len=a.length;i<len;i++){
if(typeof a[i]=='object'){
wf.apply(this,a[i]);}else{
buf[buf.length]=a[i];}}
html=buf.join('');}
this.html=html;
if(this.compiled){
this.compile();}};
wf.Template.prototype={
disableFormats:false,
re:/\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g,
applyTemplate:function(values){
if(this.compiled){
return this.compiled(values);}
var useF=this.disableFormats!==true;
var fm=wf.Format,tpl=this;
var fn=function(m,name,format,args){
if(format&&useF){
if(format.substr(0,5)=="this."){
return tpl.call(format.substr(5),values[name],values);}else{
if(args){
var re=/^\s*['"](.*)["']\s*$/;
args=args.split(',');
for(var i=0,len=args.length;i<len;i++){
args[i]=args[i].replace(re,"$1");}
args=[values[name]].concat(args);}else{
args=[values[name]];}
return fm[format].apply(fm,args);}}else{
return values[name]!==undefined?values[name]:"";}};
return this.html.replace(this.re,fn);},
compile:function(){
var fm=wf.Format;
var useF=this.disableFormats!==true;
var sep=wf.isGecko?"+":",";
var fn=function(m,name,format,args){
if(format&&useF){
args=args?','+args:"";
if(format.substr(0,5)!="this."){
format="fm."+format+'(';}else{
format='this.call("'+format.substr(5)+'", ';
args=", values";}}else{
args='';format="(values['"+name+"'] == undefined ? '' : ";}
return "'"+sep+format+"values['"+name+"']"+args+")"+sep+"'";};
var body;
if(wf.isGecko){
body="this.compiled = function(values){ return '"+
this.html.replace(/\\/g,'\\\\').replace(/(\r\n|\n)/g,'\\n').replace(/'/g,"\\'").replace(this.re,fn)+
"';};";}else{
body=["this.compiled = function(values){ return ['"];
body.push(this.html.replace(/\\/g,'\\\\').replace(/(\r\n|\n)/g,'\\n').replace(/'/g,"\\'").replace(this.re,fn));
body.push("'].join('');};");
body=body.join('');}
eval(body);
return this;},
doInsert:function(where,el,values,returnEl){
el=wf.getDom(el);
var newNode=wf.DomHelper.insertHtml(where,el,this.applyTemplate(values));
return returnEl?wf.get(newNode,true):newNode;},
overwrite:function(el,values,returnElement){
el=wf.getDom(el);
el.innerHTML=this.applyTemplate(values);
return returnElement?wf.get(el.firstChild,true):el.firstChild;}};
wf.Template.prototype.apply=wf.Template.prototype.applyTemplate;
wf.Template.from=function(el,config){
el=wf.getDom(el);
return new wf.Template(el.value||el.innerHTML,config||'');};
wf.XTemplate=function(){
wf.XTemplate.superclass.constructor.apply(this,arguments);
var s=this.html;
s=['<tpl>',s,'</tpl>'].join('');
var re=/<tpl\b[^>]*>((?:(?=([^<]+))\2|<(?!tpl\b[^>]*>))*?)<\/tpl>/;
var nameRe=/^<tpl\b[^>]*?for="(.*?)"/;
var ifRe=/^<tpl\b[^>]*?if="(.*?)"/;
var execRe=/^<tpl\b[^>]*?exec="(.*?)"/;
var m,id=0;
var tpls=[];
while(m=s.match(re)){
var m2=m[0].match(nameRe);
var m3=m[0].match(ifRe);
var m4=m[0].match(execRe);
var exp=null,fn=null,exec=null;
var name=m2&&m2[1]?m2[1]:'';
if(m3){
exp=m3&&m3[1]?m3[1]:null;
if(exp){
fn=new Function('values','parent','xindex','xcount','with(values){ return '+(wf.Format.htmlDecode(exp))+'; }');}}
if(m4){
exp=m4&&m4[1]?m4[1]:null;
if(exp){
exec=new Function('values','parent','xindex','xcount','with(values){ '+(wf.Format.htmlDecode(exp))+'; }');}}
if(name){
switch(name){
case '.':name=new Function('values','parent','with(values){ return values; }');break;
case '..':name=new Function('values','parent','with(values){ return parent; }');break;
default:name=new Function('values','parent','with(values){ return '+name+'; }');}}
tpls.push({
id:id,
target:name,
exec:exec,
test:fn,
body:m[1]||''});
s=s.replace(m[0],'{xtpl'+id+'}');++id;}
for(var i=tpls.length-1;i>=0;--i){
this.compileTpl(tpls[i]);}
this.master=tpls[tpls.length-1];
this.tpls=tpls;}
wf.extend(wf.XTemplate,wf.Template,{
re:/\{([\w-\.\#]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?(\s?[\+\-\*\\]\s?[\d\.\+\-\*\\\(\)]+)?\}/g,
codeRe:/\{\[((?:\\\]|.|\n)*?)\]\}/g,
applySubTemplate:function(id,values,parent,xindex,xcount){
var t=this.tpls[id];
if(t.test&&!t.test.call(this,values,parent,xindex,xcount)){
return '';}
if(t.exec&&t.exec.call(this,values,parent,xindex,xcount)){
return '';}
var vs=t.target?t.target.call(this,values,parent):values;
parent=t.target?values:parent;
if(t.target&&vs instanceof Array){
var buf=[];
for(var i=0,len=vs.length;i<len;i++){
buf[buf.length]=t.compiled.call(this,vs[i],parent,i+1,len);}
return buf.join('');}
return t.compiled.call(this,vs,parent,xindex,xcount);},
compileTpl:function(tpl){
var fm=wf.Format;
var useF=this.disableFormats!==true;
var sep=wf.isGecko?"+":",";
var fn=function(m,name,format,args,math){
if(name.substr(0,4)=='xtpl'){
return "'"+sep+'this.applySubTemplate('+name.substr(4)+', values, parent, xindex, xcount)'+sep+"'";}
var v;
if(name==='.'){
v='values';}else if(name==='#'){
v='xindex';}else if(name.indexOf('.')!=-1){
v=name;}else{
v="values['"+name+"']";}
if(math){
v='('+v+math+')';}
if(format&&useF){
args=args?','+args:"";
if(format.substr(0,5)!="this."){
format="fm."+format+'(';}else{
format='this.call("'+format.substr(5)+'", ';
args=", values";}}else{
args='';format="("+v+" === undefined ? '' : ";}
return "'"+sep+format+v+args+")"+sep+"'";};
var codeFn=function(m,code){
return "'"+sep+'('+code+')'+sep+"'";};
var body;
if(wf.isGecko){
body="tpl.compiled = function(values, parent, xindex, xcount){ return '"+
tpl.body.replace(/(\r\n|\n)/g,'\\n').replace(/'/g,"\\'").replace(this.re,fn).replace(this.codeRe,codeFn)+
"';};";}else{
body=["tpl.compiled = function(values, parent, xindex, xcount){ return ['"];
body.push(tpl.body.replace(/(\r\n|\n)/g,'\\n').replace(/'/g,"\\'").replace(this.re,fn).replace(this.codeRe,codeFn));
body.push("'].join('');};");
body=body.join('');}
eval(body);
return this;},
apply:function(values){
return this.master.compiled.call(this,values,{},1,1);},
applyTemplate:function(values){
return this.master.compiled.call(this,values,{},1,1);},
compile:function(){return this;}});
wf.XTemplate.from=function(el){
el=wf.getDom(el);
return new wf.XTemplate(el.value||el.innerHTML);};