const tagsAndAttrs = require('./lib/tagsAndAttrs');
class outwxml{
constructor(option){
const _ts = this;
_ts.config = {};
option = option || {};
for(let i in option){
_ts.config[i] = option[i];
};
_ts.m = {
fs:require('fs'),
path:require('path')
};
}
init(){
const _ts = this;
_ts.outtag();
let s = _ts.outwxml();
_ts.m.fs.writeFileSync('./renderTemplate.wxml',s);
}
//输出tag
outtag(id){
const _ts = this;
let s = '',
wxmlTag = tagsAndAttrs.wxml;
wxmlTag.forEach((item,index)=>{
let imgMode = '',
attr = _ts.outattr(item);
if(item === 'image'){
imgMode = `mode="{{item.type === 'audio' ? '' : 'widthFix'}}"`;
};
// todo添加绑定事件
if(item === 'checkbox-group'){
attr += `bindchange="{{item.attr['bindchange']}}"`;
};
if(item === 'checkbox'){
attr += `value="{{item.attr['value']}}"`;
};
s+= `<${item} wx:if="{{item.node === 'element' && item.tag === '${item}'}}" ${attr} ${imgMode}>${item}>`;
});
return s;
}
//生成模版对应属性
outattr(tagName){
tagName = tagName || '';
const _ts = this;
let s = '',
attr = [];
attr.push(...tagsAndAttrs.attrs);
switch (tagName) {
case 'navigator':
attr.push('href');
break;
case 'checkbox':
case 'radio':
case 'switch':
attr.push('checked');
break;
case 'audio':
attr.push('poster');
attr.push('src');
attr.push('name');
attr.push('author');
attr.push('loop');
// attr.push('controls');
s += 'controls="true" ';
break;
case 'video':
attr.push('poster');
attr.push('src');
break;
case 'image':
attr.push('src');
break;
};
s += `data-_el="{{item}}"`;
attr.forEach((item,index)=>{
switch (item) {
case 'class':
s += `${item}="{{item.attr.class}}"`;
break;
case 'href':
s += `url="{{item.attr.${item}}}"`;
break;
default:
let aItem = item.split(':');
if(aItem.length > 1){
s += `${item}='__${aItem[0]}_${aItem[1]}'`;
}else{
s += `${item}="{{item.attr['${item}']}}"`;
};
break;
};
});
return s;
}
//wxml模版生成
outwxml(){
const _ts = this;
let s = '';
for (let i = 0, len = _ts.config.depth; i{{item.text}}${_ts.outtag(c)}`;
// let temp = `
//
//
// {{item.text}}
//
// ${_ts.outtag(c)}
//
// `;
// let temp = `
//
//
//
// {{item.text}}
//
//
//
//
//
//
//
//
//
//
// `;
s+=temp;
};
return s;
}
};
new outwxml({depth:10}).init();