ferrugemjs-node
Advanced tools
Comparing version 0.31.4 to 0.32.0
{ | ||
"name": "ferrugemjs-node", | ||
"version": "0.31.4", | ||
"version": "0.32.0", | ||
"description": "a node package to convert html template to Incremental DOM", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,72 +8,72 @@ var htmlparser = require("htmlparser2"); | ||
var requireNamespaces = []; | ||
var parser_configs = {templateExtension:".html",viewModel:"testeViewModel"}; | ||
var parser_configs = { templateExtension: ".html", viewModel: "testeViewModel" }; | ||
function nextUID(){ | ||
function nextUID() { | ||
let alphabet = 'abcdefghijklmnopkrstuvwxzABCDEFGHIJKLMNOPKRSTUVWXZ'; | ||
var incrementalUID = generate(alphabet,3)+generate(`0123456789_${alphabet}`,19); | ||
var incrementalUID = generate(alphabet, 3) + generate(`0123456789_${alphabet}`, 19); | ||
return incrementalUID; | ||
} | ||
function appendBuffer(txt){ | ||
function appendBuffer(txt) { | ||
buffer.push(txt); | ||
} | ||
function flush () { | ||
buffer.length = 0; | ||
buffer = []; | ||
function flush() { | ||
buffer.length = 0; | ||
buffer = []; | ||
requireScriptList.length = 0; | ||
requireScriptList = []; | ||
requireScriptList.length = 0; | ||
requireScriptList = []; | ||
requireNamespaces.length = 0; | ||
requireNamespaces = []; | ||
requireNamespaces.length = 0; | ||
requireNamespaces = []; | ||
} | ||
function slashToCamelCase(str){ | ||
function slashToCamelCase(str) { | ||
return str | ||
.toLowerCase() | ||
.replace( | ||
.toLowerCase() | ||
.replace( | ||
/-(.)/g, | ||
function(match, group1) { | ||
function (match, group1) { | ||
return group1.toUpperCase(); | ||
} | ||
); | ||
); | ||
} | ||
function pathToAlias(p_resource_url){ | ||
function pathToAlias(p_resource_url) { | ||
var _aliasname; | ||
var _trueurl; | ||
if(p_resource_url.indexOf(' as ') > -1){ | ||
if (p_resource_url.indexOf(' as ') > -1) { | ||
var _urlsplit = p_resource_url.split(' as '); | ||
_trueurl = _urlsplit[0]; | ||
_aliasname = _urlsplit[1]; | ||
}else{ | ||
} else { | ||
_trueurl = p_resource_url; | ||
_aliasname = p_resource_url.substring(p_resource_url.lastIndexOf("/")+1,p_resource_url.length); | ||
_aliasname = p_resource_url.substring(p_resource_url.lastIndexOf("/") + 1, p_resource_url.length); | ||
}; | ||
return {alias:_aliasname,url:_trueurl}; | ||
return { alias: _aliasname, url: _trueurl }; | ||
} | ||
function contextToAlias(str){ | ||
function contextToAlias(str) { | ||
var list_ignore = list_ignore || []; | ||
if(typeof str === "string"){ | ||
var nstr = str.replace(/this\.([_$a-zA-Z]+[a-zA-Z0-9_$]*)/g,function($0,$1){ | ||
if(list_ignore.indexOf($1) > -1 ){ | ||
if (typeof str === "string") { | ||
var nstr = str.replace(/this\.([_$a-zA-Z]+[a-zA-Z0-9_$]*)/g, function ($0, $1) { | ||
if (list_ignore.indexOf($1) > -1) { | ||
return $1; | ||
} | ||
return context_alias+"."+$1; | ||
return context_alias + "." + $1; | ||
}); | ||
return nstr; | ||
} | ||
return str; | ||
return str; | ||
} | ||
function encodeValue(value){ | ||
function encodeValue(value) { | ||
return value | ||
.replace(/"\$\{/g,'(') | ||
.replace(/\}"/g,')'); | ||
.replace(/"\$\{/g, '(') | ||
.replace(/\}"/g, ')'); | ||
} | ||
function attrToContext(attribs){ | ||
var mod_tmp_attr_str = encodeValue(JSON.stringify(attribs)); | ||
function attrToContext(attribs) { | ||
var mod_tmp_attr_str = encodeValue(JSON.stringify(attribs)); | ||
return mod_tmp_attr_str; | ||
@@ -83,126 +83,126 @@ } | ||
function encodeAndSetContext(str){ | ||
return str.replace(/\$\{([^}]*)\}/g,function($1,$2){ | ||
return '"+('+contextToAlias($2)+')+"'; | ||
}); | ||
function encodeAndSetContext(str) { | ||
return str.replace(/\$\{([^}]*)\}/g, function ($1, $2) { | ||
return '"+(' + contextToAlias($2) + ')+"'; | ||
}); | ||
} | ||
function adjustEvents(key,value){ | ||
var argslist = '('+context_alias+')'; | ||
value = contextToAlias(value); | ||
function adjustEvents(key, value) { | ||
var argslist = '(' + context_alias + ')'; | ||
value = contextToAlias(value); | ||
var argsInitIndex = value.indexOf("("); | ||
if(argsInitIndex > 0){ | ||
argslist = value.substring(argsInitIndex+1,value.length); | ||
argslist = '('+context_alias+','+argslist; | ||
value = value.substring(0,argsInitIndex); | ||
} | ||
value = '${'+value+'.bind'+argslist+'}'; | ||
if (argsInitIndex > 0) { | ||
argslist = value.substring(argsInitIndex + 1, value.length); | ||
argslist = '(' + context_alias + ',' + argslist; | ||
value = value.substring(0, argsInitIndex); | ||
} | ||
value = '${' + value + '.bind' + argslist + '}'; | ||
return { | ||
key:key | ||
,value:value | ||
key: key | ||
, value: value | ||
} | ||
} | ||
function separateAttribs(attribs){ | ||
function separateAttribs(attribs) { | ||
var static_attr = {}; | ||
var dinamic_attr = {}; | ||
for (var key in attribs) { | ||
if(key.lastIndexOf(".if") > -1){ | ||
if (key.lastIndexOf(".if") > -1) { | ||
//obj_array.push(''+key+''); | ||
//obj_array.push(''+attribs[key]+''); | ||
static_attr[key] = attribs[key]; | ||
}else if(key.indexOf(".") > 0){ | ||
} else if (key.indexOf(".") > 0) { | ||
//is a custom event | ||
dinamic_attr[key] = contextToAlias(attribs[key]); | ||
//dinamic_attr[key] = "${"+contextToAlias(attribs[key])+"}"; | ||
dinamic_attr[key] = contextToAlias(attribs[key]); | ||
//dinamic_attr[key] = "${"+contextToAlias(attribs[key])+"}"; | ||
//var eventStripped = adjustEvents(key,attribs[key]); | ||
//dinamic_attr[key] = eventStripped.value; | ||
}else{ | ||
if(attribs[key].indexOf("${") === 0){ | ||
} else { | ||
if (attribs[key].indexOf("${") === 0) { | ||
dinamic_attr[key] = contextToAlias(attribs[key]); | ||
}else{ | ||
} else { | ||
static_attr[key] = attribs[key]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return { | ||
static:static_attr | ||
,dinamic:dinamic_attr | ||
static: static_attr | ||
, dinamic: dinamic_attr | ||
} | ||
} | ||
function objStaticAttrToStr(attribs){ | ||
var bindField = ""; | ||
var obj_array_static = []; | ||
for(var key in attribs){ | ||
obj_array_static.push(''+key+''); | ||
obj_array_static.push(attribs[key]); | ||
} | ||
var mod_tmp_static_attr_str = '["'+obj_array_static.join('","')+'"]'; | ||
return mod_tmp_static_attr_str; | ||
function objStaticAttrToStr(attribs) { | ||
var bindField = ""; | ||
var obj_array_static = []; | ||
for (var key in attribs) { | ||
obj_array_static.push('' + key + ''); | ||
obj_array_static.push(attribs[key]); | ||
} | ||
var mod_tmp_static_attr_str = '["' + obj_array_static.join('","') + '"]'; | ||
return mod_tmp_static_attr_str; | ||
} | ||
function objDinamicAttrToStr(attribs,tagName,type){ | ||
var obj_array = []; | ||
var bindField = ""; | ||
for(var key in attribs){ | ||
var indxBind = key.indexOf(".bind"); | ||
function objDinamicAttrToStr(attribs, tagName, type) { | ||
var obj_array = []; | ||
var bindField = ""; | ||
for (var key in attribs) { | ||
var indxBind = key.indexOf(".bind"); | ||
//console.log(key,key.lastIndexOf(".if") < 0); | ||
if(indxBind > -1 && (tagName === "input" || tagName === "textarea" || tagName === "select")){ | ||
var evtstr = "on"+key.substring(0,indxBind); | ||
if (indxBind > -1 && (tagName === "input" || tagName === "textarea" || tagName === "select")) { | ||
var evtstr = "on" + key.substring(0, indxBind); | ||
obj_array.push(evtstr); | ||
//console.log(attribs.type); | ||
var attr_pure = attribs[key].replace(context_alias+".",""); | ||
if(tagName=="select"){ | ||
obj_array.push('#{#function($evt){\nvar tmp_$target$_evt=$evt.target;\n'+context_alias+'.refresh({"'+( attr_pure )+'":tmp_$target$_evt.options[tmp_$target$_evt.selectedIndex].value});\n}#}#'); | ||
}else if(type=="checkbox"||type=="radio"){ | ||
var attr_pure = attribs[key].replace(context_alias + ".", ""); | ||
if (tagName == "select") { | ||
obj_array.push('#{#function($evt){\nvar tmp_$target$_evt=$evt.target;\n' + context_alias + '.refresh({"' + (attr_pure) + '":tmp_$target$_evt.options[tmp_$target$_evt.selectedIndex].value});\n}#}#'); | ||
} else if (type == "checkbox" || type == "radio") { | ||
//console.log( attribs[key]); | ||
obj_array.push('#{#function($evt){\n'+context_alias+'.refresh({"'+( attr_pure )+'":$evt.target.checked?$evt.target.value:null})\n}\n#}#'); | ||
}else{ | ||
obj_array.push('#{#function($evt){\n' + context_alias + '.refresh({"' + (attr_pure) + '":$evt.target.checked?$evt.target.value:null})\n}\n#}#'); | ||
} else { | ||
//console.log( attribs[key]); | ||
obj_array.push('#{#function($evt){\n'+context_alias+'.refresh({"'+( attr_pure )+'":$evt.target.value})\n}\n#}#'); | ||
obj_array.push('#{#function($evt){\n' + context_alias + '.refresh({"' + (attr_pure) + '":$evt.target.value})\n}\n#}#'); | ||
} | ||
//console.log(attribs[key]) | ||
}else if(key.indexOf(".") > 0){ | ||
var eventStripped = adjustEvents('on'+key.substring(0,key.indexOf("."))+'',attribs[key]); | ||
} else if (key.indexOf(".") > 0) { | ||
var eventStripped = adjustEvents('on' + key.substring(0, key.indexOf(".")) + '', attribs[key]); | ||
obj_array.push(eventStripped.key); | ||
var vlFn = eventStripped.value; | ||
obj_array.push("#{#"+vlFn.substring(2,vlFn.length-1)+"#}#"); | ||
}else{ | ||
if(typeof attribs[key] === "string" && attribs[key].indexOf("${") === 0){ | ||
obj_array.push(''+key+''); | ||
obj_array.push("#{#" + vlFn.substring(2, vlFn.length - 1) + "#}#"); | ||
} else { | ||
if (typeof attribs[key] === "string" && attribs[key].indexOf("${") === 0) { | ||
obj_array.push('' + key + ''); | ||
var vlFn = attribs[key]; | ||
if(key === "style" && vlFn.lastIndexOf(";") === (vlFn.length - 1) ){ | ||
if (key === "style" && vlFn.lastIndexOf(";") === (vlFn.length - 1)) { | ||
var lastComman = vlFn.lastIndexOf(";"); | ||
vlFn = vlFn.substring(0,lastComman); | ||
vlFn = vlFn.substring(0, lastComman); | ||
} | ||
//obj_array.push(contextToAlias(attribs[key])); | ||
obj_array.push(contextToAlias("#{#"+vlFn.substring(2,vlFn.length-1)+"#}#")); | ||
obj_array.push(contextToAlias("#{#" + vlFn.substring(2, vlFn.length - 1) + "#}#")); | ||
} | ||
} | ||
} | ||
} | ||
var mod_tmp_attr_str_ = '"'+obj_array.join('","')+'"'; | ||
/* | ||
var mod_tmp_attr_str = mod_tmp_attr_str_.replace(/\"\$\{([^}]*)\}\"/g,function($0,$1){ | ||
return "("+$1+")"; | ||
}); | ||
*/ | ||
var mod_tmp_attr_str = mod_tmp_attr_str_; | ||
var mod_tmp_attr_str_ = '"' + obj_array.join('","') + '"'; | ||
/* | ||
var mod_tmp_attr_str = mod_tmp_attr_str_.replace(/\"\$\{([^}]*)\}\"/g,function($0,$1){ | ||
return "("+$1+")"; | ||
}); | ||
*/ | ||
var mod_tmp_attr_str = mod_tmp_attr_str_; | ||
//console.log(mod_tmp_attr_str_,mod_tmp_attr_str); | ||
mod_tmp_attr_str = mod_tmp_attr_str.replace(/\"#{#/g,"("); | ||
mod_tmp_attr_str = mod_tmp_attr_str.replace(/#}#\"/g,")"); | ||
mod_tmp_attr_str = mod_tmp_attr_str.replace(/\"#{#/g, "("); | ||
mod_tmp_attr_str = mod_tmp_attr_str.replace(/#}#\"/g, ")"); | ||
return mod_tmp_attr_str; | ||
return mod_tmp_attr_str; | ||
} | ||
function tagSkipToStr(comp, indexLoopName){ | ||
var txtIf = '\tif('+contextToAlias(comp.attribs.condition)+'){\n'; | ||
function tagSkipToStr(comp, indexLoopName) { | ||
var txtIf = '\tif(' + contextToAlias(comp.attribs.condition) + '){\n'; | ||
txtIf += '\t_idom.skip()\n'; | ||
txtIf += '\t}else{\n'; | ||
comp.children.forEach(sub_comp => txtIf += '\t'+componentToStr(sub_comp, indexLoopName)); | ||
comp.children.forEach(sub_comp => txtIf += '\t' + componentToStr(sub_comp, indexLoopName)); | ||
txtIf += '\t};\n'; | ||
@@ -212,5 +212,5 @@ return txtIf; | ||
function tagIfToStr(comp, indexLoopName){ | ||
var txtIf = '\t\nif('+contextToAlias(comp.attribs.condition)+'){\n'; | ||
comp.children.forEach(sub_comp => txtIf += '\t'+componentToStr(sub_comp, indexLoopName)); | ||
function tagIfToStr(comp, indexLoopName) { | ||
var txtIf = '\t\nif(' + contextToAlias(comp.attribs.condition) + '){\n'; | ||
comp.children.forEach(sub_comp => txtIf += '\t' + componentToStr(sub_comp, indexLoopName)); | ||
txtIf += '\t};\n'; | ||
@@ -220,5 +220,5 @@ return txtIf; | ||
function tagElseToStr(comp, indexLoopName){ | ||
function tagElseToStr(comp, indexLoopName) { | ||
var txtElse = '\t\n}else{\n'; | ||
comp.children.forEach(sub_comp => txtElse += '\t'+componentToStr(sub_comp, indexLoopName)); | ||
comp.children.forEach(sub_comp => txtElse += '\t' + componentToStr(sub_comp, indexLoopName)); | ||
txtElse += '\t'; | ||
@@ -228,5 +228,5 @@ return txtElse; | ||
function tagElseIfToStr(comp, indexLoopName){ | ||
var txtElseIf = '\t\n}else if('+contextToAlias(comp.attribs.condition)+'){\n'; | ||
comp.children.forEach(sub_comp => txtElseIf += '\t'+componentToStr(sub_comp, indexLoopName)); | ||
function tagElseIfToStr(comp, indexLoopName) { | ||
var txtElseIf = '\t\n}else if(' + contextToAlias(comp.attribs.condition) + '){\n'; | ||
comp.children.forEach(sub_comp => txtElseIf += '\t' + componentToStr(sub_comp, indexLoopName)); | ||
txtElseIf += '\t'; | ||
@@ -237,7 +237,7 @@ return txtElseIf; | ||
function tagForToStr(comp, indexLoopName){ | ||
function tagForToStr(comp, indexLoopName) { | ||
var array_each = comp.attribs.each.split(" in "); | ||
var sub_array_each = array_each[0].split(","); | ||
var index_array = "$tmp_index_name_"+nextUID(); | ||
if(sub_array_each.length > 1){ | ||
var index_array = "$tmp_index_name_" + nextUID(); | ||
if (sub_array_each.length > 1) { | ||
index_array = sub_array_each[1]; | ||
@@ -248,21 +248,21 @@ //lasts_index_alias.push(sub_array_each[1]); | ||
//renderIDOMHTML += '\t'+appendContext(array_each[1])+'.forEach(function('+sub_array_each[0]+','+index_array+'){\n'; | ||
var txtFor = '\n\t'+contextToAlias(array_each[1])+'.forEach(function('+sub_array_each[0]+','+index_array+'){'; | ||
comp.children.forEach(sub_comp => txtFor += '\t'+componentToStr(sub_comp,index_array, indexLoopName)); | ||
var txtFor = '\n\t' + contextToAlias(array_each[1]) + '.forEach(function(' + sub_array_each[0] + ',' + index_array + '){'; | ||
comp.children.forEach(sub_comp => txtFor += '\t' + componentToStr(sub_comp, index_array, indexLoopName)); | ||
txtFor += '\t});\n'; | ||
return txtFor; | ||
} | ||
function formatTextToStr(text){ | ||
if(text && text.trim()){ | ||
function formatTextToStr(text) { | ||
if (text && text.trim()) { | ||
var strTmp = text; | ||
var strBlankLineReplace = "-x-abc"+new Date().getTime()+"zxv-x-" ; | ||
var strBlankLineReplace = "-x-abc" + new Date().getTime() + "zxv-x-"; | ||
strTmp = strTmp | ||
.replace(/\s/g,strBlankLineReplace) | ||
.trim() | ||
.replace(/\n/g,' ') | ||
.replace(/\t/g,'') | ||
.replace(new RegExp(strBlankLineReplace,'g')," "); | ||
.replace(/\s/g, strBlankLineReplace) | ||
.trim() | ||
.replace(/\n/g, ' ') | ||
.replace(/\t/g, '') | ||
.replace(new RegExp(strBlankLineReplace, 'g'), " "); | ||
if(strTmp.indexOf('${') === -1){ | ||
if (strTmp.indexOf('${') === -1) { | ||
// have'nt interpolation | ||
@@ -272,7 +272,7 @@ return strTmp; | ||
strTmp = strTmp | ||
.replace(/([^$])((\{)(.+?)(\}))/g, '$1#beg-brackets#$4#end-brackets#') | ||
.replace(/\$\{([^}]*)\}/g,function($1,$2){ | ||
return '"+('+contextToAlias($2)+')+"'; | ||
}) | ||
.replace(/#beg-brackets#/g,'{').replace(/#end-brackets#/g,'}'); | ||
.replace(/([^$])((\{)(.+?)(\}))/g, '$1#beg-brackets#$4#end-brackets#') | ||
.replace(/\$\{([^}]*)\}/g, function ($1, $2) { | ||
return '"+(' + contextToAlias($2) + ')+"'; | ||
}) | ||
.replace(/#beg-brackets#/g, '{').replace(/#end-brackets#/g, '}'); | ||
return strTmp; | ||
@@ -282,14 +282,14 @@ } | ||
} | ||
function tagTextToStr(comp, indexLoopName){ | ||
function tagTextToStr(comp, indexLoopName) { | ||
let attrDirectives = []; | ||
if(comp.parent && comp.parent.attribs){ | ||
if (comp.parent && comp.parent.attribs) { | ||
let attrKeys = Object.keys(comp.parent.attribs); | ||
attrDirectives = attrKeys.filter(tmpattr => tmpattr !== "key:id" && tmpattr.indexOf(":") > -1 && requireNamespaces.indexOf(tmpattr.split(":")[0]) > -1); | ||
} | ||
attrDirectives = attrKeys.filter(tmpattr => tmpattr !== "key:id" && tmpattr.indexOf(":") > -1 && requireNamespaces.indexOf(tmpattr.split(":")[0]) > -1); | ||
} | ||
let text = comp.data; | ||
if(text && text.trim()){ | ||
if (text && text.trim()) { | ||
let strTmp = formatTextToStr(text); | ||
if(attrDirectives.length){ | ||
let tmpNodeAlias = 'executedNode_'+nextUID(); | ||
concatenedStr = '\t\nvar '+tmpNodeAlias+' = _idom.text("'+strTmp+'");\t\n'; | ||
if (attrDirectives.length) { | ||
let tmpNodeAlias = 'executedNode_' + nextUID(); | ||
concatenedStr = '\t\nvar ' + tmpNodeAlias + ' = _idom.text("' + strTmp + '");\t\n'; | ||
attrDirectives.forEach(attr => { | ||
@@ -299,8 +299,8 @@ var splited = attr.split(":"); | ||
var directiveCamelCase = slashToCamelCase(splited[1]); | ||
let attrVlw = '"'+encodeAndSetContext(comp.parent.attribs[attr])+'"'; | ||
concatenedStr += '\t\n' +namespace+'.'+directiveCamelCase+'('+tmpNodeAlias+( comp.parent.attribs[attr] ? ',' + attrVlw : '')+');\t\n'; | ||
}); | ||
let attrVlw = '"' + encodeAndSetContext(comp.parent.attribs[attr]) + '"'; | ||
concatenedStr += '\t\n' + namespace + '.' + directiveCamelCase + '(' + tmpNodeAlias + (comp.parent.attribs[attr] ? ',' + attrVlw : '') + ');\t\n'; | ||
}); | ||
return concatenedStr; | ||
} | ||
return '\t\n_idom.text("'+strTmp+'");\t\n'; | ||
return '\t\n_idom.text("' + strTmp + '");\t\n'; | ||
} | ||
@@ -310,13 +310,13 @@ return ""; | ||
function tagContentToStr(comp){ | ||
return '\t\n_libfjs_factory.default.content.call('+context_alias+');\n'; | ||
function tagContentToStr(comp) { | ||
return '\t\n_libfjs_factory.default.content.call(' + context_alias + ');\n'; | ||
} | ||
function tagCommandToStr(comp){ | ||
if(comp.children && comp.children.length){ | ||
function tagCommandToStr(comp) { | ||
if (comp.children && comp.children.length) { | ||
var text = comp.children[0].data; | ||
if(text && text.trim()){ | ||
if (text && text.trim()) { | ||
//return text.replace(/@this\./gm,context_alias+'.'); | ||
return '\n\t(function(){\n\t'+text.trim()+'\n\t}.bind('+context_alias+'))();\n'; | ||
return '\n\t(function(){\n\t' + text.trim() + '\n\t}.bind(' + context_alias + '))();\n'; | ||
}; | ||
@@ -327,40 +327,40 @@ } | ||
function tagRefreshToStr(comp){ | ||
function tagRefreshToStr(comp) { | ||
var attribs_srt = ''; | ||
if(comp.attribs){ | ||
for(var key in comp.attribs){ | ||
if (comp.attribs) { | ||
for (var key in comp.attribs) { | ||
var pattExpression = /^\$\{(.*)+\}$/g; | ||
if(pattExpression.test(comp.attribs[key])){ | ||
attribs_srt += ',"'+slashToCamelCase(key)+'":'+comp.attribs[key].replace(pattExpression,(p1,p2) => { | ||
return '('+contextToAlias(p2)+')'; | ||
}); | ||
}else{ | ||
attribs_srt += ',"'+slashToCamelCase(key)+'":"'+comp.attribs[key]+'"'; | ||
} | ||
if (pattExpression.test(comp.attribs[key])) { | ||
attribs_srt += ',"' + slashToCamelCase(key) + '":' + comp.attribs[key].replace(pattExpression, (p1, p2) => { | ||
return '(' + contextToAlias(p2) + ')'; | ||
}); | ||
} else { | ||
attribs_srt += ',"' + slashToCamelCase(key) + '":"' + comp.attribs[key] + '"'; | ||
} | ||
} | ||
attribs_srt = attribs_srt.substring(1,attribs_srt.length) | ||
attribs_srt = attribs_srt.substring(1, attribs_srt.length) | ||
} | ||
attribs_srt = '{'+attribs_srt+'}'; | ||
attribs_srt = '{' + attribs_srt + '}'; | ||
return `\n\t${context_alias}.refresh(${attribs_srt});\n`; | ||
} | ||
function tagRouteToStr(comp, indexLoopName){ | ||
function tagRouteToStr(comp, indexLoopName) { | ||
var separateAttrsElement = separateAttribs(comp.attribs) | ||
var mod_tmp_static_attr_str=objStaticAttrToStr(separateAttrsElement.static); | ||
var mod_tmp_static_attr_str = objStaticAttrToStr(separateAttrsElement.static); | ||
//console.log(separateAttrsElement.static); | ||
var attrsCamel = {}; | ||
for(var key in separateAttrsElement.static){ | ||
for (var key in separateAttrsElement.static) { | ||
attrsCamel[slashToCamelCase(key)] = separateAttrsElement.static[key]; | ||
} | ||
var routeStr = '\n_$_inst_$_.routes.push('+JSON.stringify(attrsCamel)+');\n'; | ||
var routeStr = '\n_$_inst_$_.routes.push(' + JSON.stringify(attrsCamel) + ');\n'; | ||
return routeStr; | ||
} | ||
function tagCustomToStr(comp, indexLoopName){ | ||
function tagCustomToStr(comp, indexLoopName) { | ||
//provendo um key caso nao exista, mas nao eh funcional em caso de foreach | ||
var static_key = 'custom_comp_keyid_'+nextUID(); | ||
var static_key = 'custom_comp_keyid_' + nextUID(); | ||
/* | ||
@@ -373,3 +373,3 @@ if(comp.attribs && comp.attribs["key:id"]){ | ||
var alreadyHasKeyId = true; | ||
if(!comp.attribs["key:id"]){ | ||
if (!comp.attribs["key:id"]) { | ||
comp.attribs["key:id"] = static_key; | ||
@@ -390,3 +390,3 @@ alreadyHasKeyId = false; | ||
if(name.indexOf(":") > -1){ | ||
if (name.indexOf(":") > -1) { | ||
let tagname_splited = name.split(":"); | ||
@@ -396,3 +396,3 @@ namespace = tagname_splited[0]; | ||
if(requireNamespaces.indexOf(namespace) < 0){ | ||
if (requireNamespaces.indexOf(namespace) < 0) { | ||
namespaceNotFound = true; | ||
@@ -402,30 +402,30 @@ return tagBasicToStr(comp, indexLoopName); | ||
tagname_underscore = slashToCamelCase(tagname);// tagname.replace(/-/g,"_"); | ||
tagname_with_namespace = namespace+'.'+tagname_underscore; | ||
tagname_underscore = slashToCamelCase(tagname);// tagname.replace(/-/g,"_"); | ||
tagname_with_namespace = namespace + '.' + tagname_underscore; | ||
tagname_constructor = tagname_with_namespace; | ||
}else{ | ||
} else { | ||
namespace = ""; | ||
tagname = name; | ||
tagname_underscore = tagname.replace(/-/g,"_"); | ||
tagname_underscore = tagname.replace(/-/g, "_"); | ||
tagname_with_namespace = tagname_underscore; | ||
tagname_constructor = tagname_with_namespace+'.default'; | ||
} | ||
tagname_constructor = tagname_with_namespace + '.default'; | ||
} | ||
var separate_attrs = separateAttribs(comp.attribs); | ||
separate_attrs.static.is = comp.name; | ||
separate_attrs.static.is = comp.name; | ||
var regx = /(\w*)+\.if$/g; | ||
for(key in separate_attrs.dinamic){ | ||
if(key.indexOf(".") > 0){ | ||
separate_attrs.dinamic[key] = adjustEvents(key,separate_attrs.dinamic[key]).value; | ||
for (key in separate_attrs.dinamic) { | ||
if (key.indexOf(".") > 0) { | ||
separate_attrs.dinamic[key] = adjustEvents(key, separate_attrs.dinamic[key]).value; | ||
} | ||
} | ||
for(key in separate_attrs.static){ | ||
if(regx.test(key)){ | ||
for (key in separate_attrs.static) { | ||
if (regx.test(key)) { | ||
//console.log(key,separate_attrs.static[key]); | ||
var attrcondi = key.replace(".if",""); | ||
separate_attrs.dinamic[key] = "${"+separate_attrs.static[key]+" ? new String('"+attrcondi+"') : null }"; | ||
var attrcondi = key.replace(".if", ""); | ||
separate_attrs.dinamic[key] = "${" + separate_attrs.static[key] + " ? new String('" + attrcondi + "') : null }"; | ||
delete separate_attrs.static[key]; | ||
@@ -435,24 +435,24 @@ } | ||
var _tmp_host_vars_ = attrToContext(separate_attrs.dinamic); | ||
var _tmp_host_vars_ = attrToContext(separate_attrs.dinamic); | ||
var _tmp_static_vars = JSON.stringify(separate_attrs.static); | ||
if(!alreadyHasKeyId && indexLoopName){ | ||
if (!alreadyHasKeyId && indexLoopName) { | ||
//static_key | ||
//indexLoopName | ||
_tmp_static_vars = _tmp_static_vars.replace(static_key,static_key+'_"+'+indexLoopName+'+"'); | ||
_tmp_static_vars = _tmp_static_vars.replace(static_key, static_key + '_"+' + indexLoopName + '+"'); | ||
} | ||
//console.log('aqui----->',separate_attrs.dinamic) | ||
basicTag = '\t\n(function(){\n var _$_inst_$_ = _libfjs_factory.default.build({"classFactory":'+tagname_constructor+',"tag":"div","alias":"'+name+'","target":"","hostVars":'+_tmp_host_vars_+',"staticVars":'+_tmp_static_vars+'});\n'; | ||
if(comp.children && comp.children.length){ | ||
var hasRoute = comp.children.some(sub_comp=>sub_comp.name === "route"); | ||
basicTag = '\t\n(function(){\n var _$_inst_$_ = _libfjs_factory.default.build({"classFactory":' + tagname_constructor + ',"tag":"div","alias":"' + name + '","target":"","hostVars":' + _tmp_host_vars_ + ',"staticVars":' + _tmp_static_vars + '});\n'; | ||
if (comp.children && comp.children.length) { | ||
var hasRoute = comp.children.some(sub_comp => sub_comp.name === "route"); | ||
//console.log('has route',hasRoute) | ||
if(hasRoute){ | ||
if (hasRoute) { | ||
//console.log(comp.children[1].type); | ||
comp.children.forEach(sub_comp => basicTag += '\t'+componentToStr(sub_comp, indexLoopName)); | ||
}else{ | ||
comp.children.forEach(sub_comp => basicTag += '\t' + componentToStr(sub_comp, indexLoopName)); | ||
} else { | ||
basicTag += '\t\n_libfjs_factory.default.content.call(_$_inst_$_,function(){\n'; | ||
comp.children.forEach(sub_comp => basicTag += '\t'+componentToStr(sub_comp, indexLoopName)); | ||
comp.children.forEach(sub_comp => basicTag += '\t' + componentToStr(sub_comp, indexLoopName)); | ||
basicTag += '\t\n});\n'; | ||
@@ -468,3 +468,3 @@ } | ||
function tagRpFunctionToStr(comp){ | ||
function tagRpFunctionToStr(comp) { | ||
var rpfnStr = ''; | ||
@@ -474,37 +474,37 @@ var nameCamel = slashToCamelCase(comp.name); | ||
var separate_attrs = separateAttribs(comp.attribs); | ||
for(var key in separate_attrs.dinamic){ | ||
var keyCamel = slashToCamelCase(key); | ||
attrsCamel[keyCamel] = separate_attrs.dinamic[key]; | ||
for (var key in separate_attrs.dinamic) { | ||
var keyCamel = slashToCamelCase(key); | ||
attrsCamel[keyCamel] = separate_attrs.dinamic[key]; | ||
} | ||
for(var key in separate_attrs.static){ | ||
for (var key in separate_attrs.static) { | ||
var keyCamel = slashToCamelCase(key); | ||
//verificar se eh uma funcao | ||
if(key.indexOf("on-") === 0){ | ||
attrsCamel[keyCamel] = adjustEvents(key,separate_attrs.static[key]).value; | ||
}else{ | ||
if (key.indexOf("on-") === 0) { | ||
attrsCamel[keyCamel] = adjustEvents(key, separate_attrs.static[key]).value; | ||
} else { | ||
attrsCamel[keyCamel] = separate_attrs.static[key]; | ||
} | ||
} | ||
} | ||
rpfnStr += '\t'+comp.name.replace(/-/g,"_")+'.default('+attrToContext(attrsCamel)+');\n' | ||
rpfnStr += '\t' + comp.name.replace(/-/g, "_") + '.default(' + attrToContext(attrsCamel) + ');\n' | ||
return rpfnStr; | ||
} | ||
function tagComposeToStr(comp, indexLoopName){ | ||
function tagComposeToStr(comp, indexLoopName) { | ||
var attrview = "view:from"; | ||
if(!comp.attribs[attrview]){ | ||
if (!comp.attribs[attrview]) { | ||
return ""; | ||
} | ||
//provendo um key caso nao exista, mas nao eh funcional em caso de foreach | ||
var static_key = '"compose_keyid_'+nextUID()+'"'; | ||
var static_key = '"compose_keyid_' + nextUID() + '"'; | ||
var alreadyHasKeyId = true; | ||
if(comp.attribs && comp.attribs["key:id"]){ | ||
static_key = '"'+encodeAndSetContext(comp.attribs["key:id"])+'"'; | ||
if (comp.attribs && comp.attribs["key:id"]) { | ||
static_key = '"' + encodeAndSetContext(comp.attribs["key:id"]) + '"'; | ||
//delete comp.attribs["key:id"]; | ||
comp.attribs["key-id"] = comp.attribs["key:id"]; | ||
comp.attribs["id"] = comp.attribs["key:id"]; | ||
}else{ | ||
comp.attribs["key:id"] = static_key.replace(/"/g,""); | ||
comp.attribs["key-id"] = static_key.replace(/"/g,""); | ||
comp.attribs["id"] = static_key.replace(/"/g,""); | ||
} else { | ||
comp.attribs["key:id"] = static_key.replace(/"/g, ""); | ||
comp.attribs["key-id"] = static_key.replace(/"/g, ""); | ||
comp.attribs["id"] = static_key.replace(/"/g, ""); | ||
alreadyHasKeyId = false; | ||
@@ -518,4 +518,4 @@ } | ||
var tmp_view = (separateAttrsElement.static[attrview]?separateAttrsElement.static[attrview]:encodeAndSetContext(separateAttrsElement.dinamic[attrview])); | ||
var tmp_view = (separateAttrsElement.static[attrview] ? separateAttrsElement.static[attrview] : encodeAndSetContext(separateAttrsElement.dinamic[attrview])); | ||
delete separateAttrsElement.static[attrview]; | ||
@@ -526,23 +526,23 @@ delete separateAttrsElement.dinamic[attrview]; | ||
var mod_tmp_static_attr_str_array_flat = objStaticAttrToStr(separateAttrsElement.static); | ||
var mod_tmp_attr_str = objDinamicAttrToStr(separateAttrsElement.dinamic); | ||
//console.log(mod_tmp_static_attr_str_array_flat); | ||
if(!alreadyHasKeyId && indexLoopName){ | ||
if (!alreadyHasKeyId && indexLoopName) { | ||
let tmpReplace = static_key + '+"_"+'+indexLoopName; | ||
mod_tmp_static_attr_str_array_flat = mod_tmp_static_attr_str_array_flat.replace(new RegExp(static_key, 'g'),tmpReplace); | ||
mod_tmp_static_attr_str = mod_tmp_static_attr_str.replace(new RegExp(static_key, 'g'),tmpReplace); | ||
let tmpReplace = static_key + '+"_"+' + indexLoopName; | ||
mod_tmp_static_attr_str_array_flat = mod_tmp_static_attr_str_array_flat.replace(new RegExp(static_key, 'g'), tmpReplace); | ||
mod_tmp_static_attr_str = mod_tmp_static_attr_str.replace(new RegExp(static_key, 'g'), tmpReplace); | ||
static_key = tmpReplace; | ||
} | ||
var basicTag = '\n\t_idom.elementOpen("div",'+static_key+','+mod_tmp_static_attr_str_array_flat+','+mod_tmp_attr_str+');\n'; | ||
var basicTag = '\n\t_idom.elementOpen("div",' + static_key + ',' + mod_tmp_static_attr_str_array_flat + ',' + mod_tmp_attr_str + ');\n'; | ||
basicTag += '\n\t_idom.elementClose("div");\n'; | ||
basicTag += '\n\t_libfjs_factory.default.compose("'+tmp_view+'",'+static_key+','+attrToContext(separateAttrsElement.dinamic)+','+mod_tmp_static_attr_str+',function(){\n'; | ||
basicTag += '\n\t_libfjs_factory.default.compose("' + tmp_view + '",' + static_key + ',' + attrToContext(separateAttrsElement.dinamic) + ',' + mod_tmp_static_attr_str + ',function(){\n'; | ||
if(comp.children){ | ||
comp.children.forEach(sub_comp => basicTag += '\t'+componentToStr(sub_comp, indexLoopName)); | ||
if (comp.children) { | ||
comp.children.forEach(sub_comp => basicTag += '\t' + componentToStr(sub_comp, indexLoopName)); | ||
} | ||
@@ -557,6 +557,6 @@ | ||
function tagBasicToStr(comp, indexLoopName){ | ||
function tagBasicToStr(comp, indexLoopName) { | ||
var static_key = 'null'; | ||
if(comp.attribs && comp.attribs["key:id"]){ | ||
static_key = encodeAndSetContext(comp.attribs["key:id"]); | ||
if (comp.attribs && comp.attribs["key:id"]) { | ||
static_key = encodeAndSetContext(comp.attribs["key:id"]); | ||
delete comp.attribs["key:id"]; | ||
@@ -567,7 +567,7 @@ } | ||
var regx = /(\w*)+\.if$/g; | ||
for(key in separateAttrsElement.static){ | ||
if(regx.test(key)){ | ||
var attrcondi = key.replace(".if",""); | ||
separateAttrsElement.dinamic[attrcondi] = "${"+separateAttrsElement.static[key]+" ? new String('"+attrcondi+"') : null }"; | ||
delete separateAttrsElement.static[key]; | ||
for (key in separateAttrsElement.static) { | ||
if (regx.test(key)) { | ||
var attrcondi = key.replace(".if", ""); | ||
separateAttrsElement.dinamic[attrcondi] = "${" + separateAttrsElement.static[key] + " ? new String('" + attrcondi + "') : null }"; | ||
delete separateAttrsElement.static[key]; | ||
} | ||
@@ -578,13 +578,13 @@ }; | ||
var mod_tmp_attr_str = objDinamicAttrToStr(separateAttrsElement.dinamic,comp.name,type); | ||
var mod_tmp_attr_str = objDinamicAttrToStr(separateAttrsElement.dinamic, comp.name, type); | ||
var basicTag = ''; | ||
var haveStaticKeyGen = false; | ||
if(static_key === 'null' && (mod_tmp_static_attr_str !== '[""]' || mod_tmp_attr_str !== '""')){ | ||
if (static_key === 'null' && (mod_tmp_static_attr_str !== '[""]' || mod_tmp_attr_str !== '""')) { | ||
static_key = nextUID(); | ||
haveStaticKeyGen = true; | ||
} | ||
if(static_key === 'null'){ | ||
basicTag = '\n\t_idom.elementOpen("'+comp.name+'");\n'; | ||
}else{ | ||
if (static_key === 'null') { | ||
basicTag = '\n\t_idom.elementOpen("' + comp.name + '");\n'; | ||
} else { | ||
/* | ||
@@ -597,189 +597,216 @@ if(comp.parent && comp.parent.attribs && comp.parent.attribs['key:id']){ | ||
//comp.attribs['key:id'] = static_key; | ||
basicTag = '\n\t_idom.elementOpen("'+comp.name+'","'+static_key + (indexLoopName && haveStaticKeyGen ? '_"+'+indexLoopName : '"') +','+mod_tmp_static_attr_str+','+mod_tmp_attr_str+');\n'; | ||
basicTag = ' _idom.elementOpen("' + comp.name + '","' + static_key + (indexLoopName && haveStaticKeyGen ? '_"+' + indexLoopName : '"') + ',' + mod_tmp_static_attr_str + ',' + mod_tmp_attr_str + ');\n'; | ||
} | ||
if(comp.children){ | ||
comp.children.forEach(sub_comp => basicTag += '\t'+componentToStr(sub_comp,indexLoopName)); | ||
if (comp.children) { | ||
comp.children.forEach(sub_comp => basicTag += '\t' + componentToStr(sub_comp, indexLoopName)); | ||
} | ||
basicTag += '\n\t_idom.elementClose("'+comp.name+'");\n'; | ||
let attrDirectives = []; | ||
if (comp.attribs) { | ||
let attrKeys = Object.keys(comp.attribs); | ||
attrDirectives = attrKeys.filter(tmpattr => tmpattr !== "key:id" && tmpattr.indexOf(":") > -1 && requireNamespaces.indexOf(tmpattr.split(":")[0]) > -1); | ||
} | ||
let tmpNodeAlias = 'executedNode_' + nextUID(); | ||
if (attrDirectives.length) { | ||
basicTag = '\t\nvar ' + tmpNodeAlias + ' ='+basicTag+'\t\n'; | ||
} | ||
basicTag += '\n\t_idom.elementClose("' + comp.name + '");\n'; | ||
if (attrDirectives.length) { | ||
let concatenedStr = ''; | ||
attrDirectives.forEach(attr => { | ||
var splited = attr.split(":"); | ||
var namespace = splited[0]; | ||
var directiveCamelCase = slashToCamelCase(splited[1]); | ||
let attrVlw = '"' + encodeAndSetContext(comp.attribs[attr]) + '"'; | ||
concatenedStr += '\t\n' + namespace + '.' + directiveCamelCase + '(' + tmpNodeAlias + (comp.attribs[attr] ? ',' + attrVlw : '') + ');\t\n'; | ||
}); | ||
basicTag += concatenedStr; | ||
} | ||
return basicTag; | ||
} | ||
function tagTemplateToStr(comp,viewModel){ | ||
function tagTemplateToStr(comp, viewModel) { | ||
//console.log(comp.type,comp.name,viewModel); | ||
var stylesStr = ""; | ||
var templatePre = ""; | ||
var templatePre = ""; | ||
var requiresComp = []; | ||
var viewModelAlias = ""; | ||
if(comp.attribs && typeof comp.attribs["no-model"] === 'string'){ | ||
if (comp.attribs && typeof comp.attribs["no-model"] === 'string') { | ||
viewModel = ""; | ||
}else if(comp.attribs && comp.attribs["model"]){ | ||
} else if (comp.attribs && comp.attribs["model"]) { | ||
viewModel = comp.attribs["model"]; | ||
viewModelAlias = '_'+pathToAlias(viewModel).alias.replace(/-/g,"_"); | ||
requiresComp.push({type: 'controller', path: viewModel, alias:viewModelAlias }); | ||
}else{ | ||
viewModelAlias = '_' + pathToAlias(viewModel).alias.replace(/-/g, "_"); | ||
requiresComp.push({ type: 'controller', path: viewModel, alias: viewModelAlias }); | ||
} else { | ||
//console.log(pathToAlias('./'+viewModel)); | ||
viewModel = './'+viewModel; | ||
viewModelAlias = '_'+pathToAlias(viewModel).alias.replace(/-/g,"_"); | ||
requiresComp.push({type: 'controller', path:viewModel, alias: viewModelAlias}); | ||
viewModel = './' + viewModel; | ||
viewModelAlias = '_' + pathToAlias(viewModel).alias.replace(/-/g, "_"); | ||
requiresComp.push({ type: 'controller', path: viewModel, alias: viewModelAlias }); | ||
} | ||
var tmp_mod_name = "_mod_"+viewModelAlias+"_"+nextUID(); | ||
var _tmp_constructor_no_view_ = '"_tmp_constructor_no_view_'+tmp_mod_name+'"'; | ||
var tmp_mod_name = "_mod_" + viewModelAlias + "_" + nextUID(); | ||
if(comp.children && comp.children.length){ | ||
var _tmp_constructor_no_view_ = '"_tmp_constructor_no_view_' + tmp_mod_name + '"'; | ||
if (comp.children && comp.children.length) { | ||
var firstElementArray = comp | ||
.children | ||
.filter(sub_comp => sub_comp.type === 'tag' && ['require','style','script','command'].indexOf(sub_comp.name) < 0); | ||
.children | ||
.filter(sub_comp => sub_comp.type === 'tag' && ['require', 'style', 'script', 'command'].indexOf(sub_comp.name) < 0); | ||
var firstElementAttrs = {name:'div'}; | ||
var firstElementAttrs = { name: 'div' }; | ||
if(firstElementArray.length){ | ||
if (firstElementArray.length) { | ||
var separateAttrsFirstElement = separateAttribs(firstElementArray[0].attribs) | ||
var flat_static_array = []; | ||
for(key in separateAttrsFirstElement.static){ | ||
flat_static_array.push(key,separateAttrsFirstElement.static[key]) | ||
} | ||
var separateAttrsFirstElement = separateAttribs(firstElementArray[0].attribs) | ||
var flat_static_array = []; | ||
for (key in separateAttrsFirstElement.static) { | ||
flat_static_array.push(key, separateAttrsFirstElement.static[key]) | ||
} | ||
firstElementAttrs = { | ||
name:firstElementArray[0].name | ||
,static:flat_static_array | ||
,dinamic:objDinamicAttrToStr(separateAttrsFirstElement.dinamic,firstElementArray[0].name) | ||
firstElementAttrs = { | ||
name: firstElementArray[0].name | ||
, static: flat_static_array | ||
, dinamic: objDinamicAttrToStr(separateAttrsFirstElement.dinamic, firstElementArray[0].name) | ||
}; | ||
comp | ||
.children | ||
.filter(sub_comp => sub_comp.type === 'tag' && sub_comp.name === 'require' && sub_comp.attribs['from']) | ||
.forEach(sub_comp => { | ||
//console.log(`${resolveTagRequire(sub_comp).type} : ${sub_comp.attribs['type']} : ${sub_comp.attribs['from']} : ${resolveTagRequire(sub_comp).alias}`); | ||
requiresComp.push(resolveTagRequire(sub_comp)) | ||
}); | ||
//console.log(modAlias,requiresPath) | ||
/* | ||
template // "_"+tagobject.alias.replace(/-/g,"_") | ||
script (acho que nao precisa) // .replace(/-/g,"_") | ||
namespace // "_"+tagobject.alias | ||
*/ | ||
var modAlias = requiresComp | ||
.filter(item => item.type !== 'style') | ||
.sort(p => p.path) | ||
.map(req_comp => { | ||
if(['template', 'script', 'namespace'].indexOf(req_comp.type) > -1){ | ||
return req_comp.alias.replace(/-/g,'_'); | ||
} | ||
return req_comp.alias; | ||
}); | ||
comp | ||
.children | ||
.filter(sub_comp => sub_comp.type === 'tag' && sub_comp.name === 'require' && sub_comp.attribs['from']) | ||
.forEach(sub_comp => { | ||
//console.log(`${resolveTagRequire(sub_comp).type} : ${sub_comp.attribs['type']} : ${sub_comp.attribs['from']} : ${resolveTagRequire(sub_comp).alias}`); | ||
requiresComp.push(resolveTagRequire(sub_comp)) | ||
}); | ||
//console.log(modAlias,requiresPath) | ||
/* | ||
template // "_"+tagobject.alias.replace(/-/g,"_") | ||
script (acho que nao precisa) // .replace(/-/g,"_") | ||
namespace // "_"+tagobject.alias | ||
*/ | ||
var modAlias = requiresComp | ||
.filter(item => item.type !== 'style') | ||
.sort(p => p.path) | ||
.map(req_comp => { | ||
if (['template', 'script', 'namespace'].indexOf(req_comp.type) > -1) { | ||
return req_comp.alias.replace(/-/g, '_'); | ||
} | ||
return req_comp.alias; | ||
}); | ||
//console.log(requiresComp); | ||
var requiresPath = requiresComp | ||
.filter(item => item.type !== "style") | ||
.sort(p => p.path) | ||
.map(req_comp => '"'+req_comp.path+'"'); | ||
var requiresPath = requiresComp | ||
.filter(item => item.type !== "style") | ||
.sort(p => p.path) | ||
.map(req_comp => '"' + req_comp.path + '"'); | ||
var onlyRequiresStyles = requiresComp | ||
.filter(item => item.type === "style") | ||
.map(req_comp => '"'+req_comp.path+'"'); | ||
var onlyRequiresStyles = requiresComp | ||
.filter(item => item.type === "style") | ||
.map(req_comp => '"' + req_comp.path + '"'); | ||
requireScriptList = requiresComp | ||
.filter(reqcomp => reqcomp.type === "script") | ||
.map(reqcomp => reqcomp.alias); | ||
requireScriptList = requiresComp | ||
.filter(reqcomp => reqcomp.type === "script") | ||
.map(reqcomp => reqcomp.alias); | ||
requireNamespaces = requiresComp | ||
.filter(reqcomp => reqcomp.type === "namespace") | ||
.map(reqcomp => reqcomp.alias); | ||
requireNamespaces = requiresComp | ||
.filter(reqcomp => reqcomp.type === "namespace") | ||
.map(reqcomp => reqcomp.alias); | ||
templatePre += 'define(["exports","incremental-dom","ferrugemjs/dist/core/component-factory"'; | ||
templatePre += 'define(["exports","incremental-dom","ferrugemjs/dist/core/component-factory"'; | ||
if(requiresPath.length){ | ||
templatePre += ','; | ||
} | ||
if (requiresPath.length) { | ||
templatePre += ','; | ||
} | ||
templatePre += requiresPath.join(); | ||
templatePre += requiresPath.join(); | ||
if(onlyRequiresStyles.length){ | ||
templatePre += ','; | ||
} | ||
if (onlyRequiresStyles.length) { | ||
templatePre += ','; | ||
} | ||
templatePre += onlyRequiresStyles.join(); | ||
templatePre += onlyRequiresStyles.join(); | ||
templatePre += '], function (exports,_idom,_libfjs_factory'; | ||
templatePre += '], function (exports,_idom,_libfjs_factory'; | ||
if(modAlias.length){ | ||
templatePre += ','; | ||
} | ||
if (modAlias.length) { | ||
templatePre += ','; | ||
} | ||
templatePre += modAlias.join(); | ||
templatePre += modAlias.join(); | ||
templatePre += '){'; | ||
templatePre += '){'; | ||
if(viewModel){ | ||
templatePre += '\n\tvar _'+viewModelAlias+'_tmp = Object.keys('+viewModelAlias+')[0];\n'; | ||
}else{ | ||
templatePre +='\n\tvar _'+tmp_mod_name+'_tmp = '+_tmp_constructor_no_view_+';\n'; | ||
} | ||
if (viewModel) { | ||
templatePre += '\n\tvar _' + viewModelAlias + '_tmp = Object.keys(' + viewModelAlias + ')[0];\n'; | ||
} else { | ||
templatePre += '\n\tvar _' + tmp_mod_name + '_tmp = ' + _tmp_constructor_no_view_ + ';\n'; | ||
} | ||
comp | ||
.children | ||
.filter(sub_comp => sub_comp.type=='style' && sub_comp.name == 'style') | ||
.forEach(sub_comp => stylesStr += '\t'+tagStyleToStr(sub_comp)); | ||
comp | ||
.children | ||
.filter(sub_comp => sub_comp.type == 'style' && sub_comp.name == 'style') | ||
.forEach(sub_comp => stylesStr += '\t' + tagStyleToStr(sub_comp)); | ||
templatePre += stylesStr+'\t'; | ||
var subClazzName = '_clazz_sub_'+nextUID()+'_tmp'; | ||
templatePre += 'exports.default = (function(super_clazz){\n'; | ||
templatePre += '\t\tfunction '+subClazzName+'(){\n'; | ||
templatePre += '\t\t\tif(super_clazz.call){\n'; | ||
templatePre += '\t\t\t\tsuper_clazz.call(this);\n'; | ||
templatePre += '\t\t\t}\n'; | ||
templatePre += '\t\t};\n'; | ||
templatePre += '\t\t'+subClazzName+'.prototype = Object.create(super_clazz.prototype || super_clazz);\n'; | ||
templatePre += '\t\t'+subClazzName+'.prototype.constructor = '+subClazzName+';\n'; | ||
templatePre += stylesStr + '\t'; | ||
templatePre += '\t\t'+subClazzName+'.prototype._$attrs$_ = '+JSON.stringify(firstElementAttrs)+';\n'; | ||
var subClazzName = '_clazz_sub_' + nextUID() + '_tmp'; | ||
templatePre += 'exports.default = (function(super_clazz){\n'; | ||
templatePre += '\t\tfunction ' + subClazzName + '(){\n'; | ||
templatePre += '\t\t\tif(super_clazz.call){\n'; | ||
templatePre += '\t\t\t\tsuper_clazz.call(this);\n'; | ||
templatePre += '\t\t\t}\n'; | ||
templatePre += '\t\t};\n'; | ||
templatePre += '\t\t' + subClazzName + '.prototype = Object.create(super_clazz.prototype || super_clazz);\n'; | ||
templatePre += '\t\t' + subClazzName + '.prototype.constructor = ' + subClazzName + ';\n'; | ||
templatePre += '\t\t'+subClazzName+'.prototype.render = '; | ||
var childrenstr = ''; | ||
childrenstr += 'function('+context_alias+'){'; | ||
templatePre += '\t\t' + subClazzName + '.prototype._$attrs$_ = ' + JSON.stringify(firstElementAttrs) + ';\n'; | ||
comp.children.filter( sub_comp => sub_comp.type=='tag' && ['require','style','script'].indexOf(sub_comp.name) == -1 )[0].children.forEach(sub_comp => childrenstr += '\t'+componentToStr(sub_comp)); | ||
templatePre += '\t\t' + subClazzName + '.prototype.render = '; | ||
childrenstr += '\t}'; | ||
templatePre += childrenstr; | ||
templatePre += ';\n\t\treturn '+subClazzName+';\n'; | ||
if(viewModel){ | ||
//tmp_mod_name | ||
//templatePre += ' })('+tmp_mod_name+'[_'+tmp_mod_name+'_tmp]);'; | ||
templatePre += '\t})('+viewModelAlias+'[_'+viewModelAlias+'_tmp] || '+viewModelAlias+');'; | ||
}else{ | ||
templatePre += '\t})(function(){})'; | ||
} | ||
templatePre += '\n});'; | ||
return templatePre; | ||
}else{ | ||
console.warn(`warn: you need a root element into a template element to '${viewModel}' !`) | ||
return ""; | ||
} | ||
}else{ | ||
var childrenstr = ''; | ||
childrenstr += 'function(' + context_alias + '){'; | ||
comp.children.filter(sub_comp => sub_comp.type == 'tag' && ['require', 'style', 'script'].indexOf(sub_comp.name) == -1)[0].children.forEach(sub_comp => childrenstr += '\t' + componentToStr(sub_comp)); | ||
childrenstr += '\t}'; | ||
templatePre += childrenstr; | ||
templatePre += ';\n\t\treturn ' + subClazzName + ';\n'; | ||
if (viewModel) { | ||
//tmp_mod_name | ||
//templatePre += ' })('+tmp_mod_name+'[_'+tmp_mod_name+'_tmp]);'; | ||
templatePre += '\t})(' + viewModelAlias + '[_' + viewModelAlias + '_tmp] || ' + viewModelAlias + ');'; | ||
} else { | ||
templatePre += '\t})(function(){})'; | ||
} | ||
templatePre += '\n});'; | ||
return templatePre; | ||
} else { | ||
console.warn(`warn: you need a root element into a template element to '${viewModel}' !`) | ||
return ""; | ||
} | ||
} else { | ||
console.warn(`warn: you need a root element into a template element to '${viewModel}' !`); | ||
return ""; | ||
} | ||
return ""; | ||
return ""; | ||
} | ||
return ""; | ||
} | ||
function tagStyleToStr(comp){ | ||
function tagStyleToStr(comp) { | ||
//console.log(comp); | ||
var text = comp.children && comp.children[0] && comp.children[0].data; | ||
if(text && text.trim()){ | ||
if (text && text.trim()) { | ||
var styletxt = ""; | ||
@@ -790,3 +817,3 @@ styletxt += "\n\tvar tmp_style = document.createElement('style');"; | ||
//.replace(/\n/g," '\n\t\t+' ") | ||
styletxt += "\n\ttmp_style.innerHTML = '"+text.replace(/'/g,'"').replace(/[\n\t\r]/g," ")+"';"; | ||
styletxt += "\n\ttmp_style.innerHTML = '" + text.replace(/'/g, '"').replace(/[\n\t\r]/g, " ") + "';"; | ||
styletxt += "\n\tdocument.getElementsByTagName('head')[0].appendChild(tmp_style);"; | ||
@@ -797,26 +824,26 @@ return styletxt; | ||
} | ||
function resolveTagRequire(comp){ | ||
var fromstr = comp.attribs["from"]; | ||
function resolveTagRequire(comp) { | ||
var fromstr = comp.attribs["from"]; | ||
var tagobject = pathToAlias(fromstr); | ||
if(comp.attribs.type && comp.attribs.type === "script"){ | ||
if (comp.attribs.type && comp.attribs.type === "script") { | ||
return { | ||
type:comp.attribs.type | ||
,path:tagobject.url | ||
,alias:tagobject.alias | ||
type: comp.attribs.type | ||
, path: tagobject.url | ||
, alias: tagobject.alias | ||
} | ||
} | ||
if(comp.attribs.type && comp.attribs.type === "namespace"){ | ||
if (comp.attribs.type && comp.attribs.type === "namespace") { | ||
return { | ||
type: comp.attribs.type | ||
,path:tagobject.url | ||
,alias:tagobject.alias | ||
} | ||
, path: tagobject.url | ||
, alias: tagobject.alias | ||
} | ||
} | ||
//suporte aos plugins mais conhecidos | ||
if( /^(css|style)!/gm.test(fromstr) || /\.(sass|scss|styl|css|less)$/gm.test(fromstr) || /scss!?$/gm.test(fromstr) || /css!$/gm.test(fromstr) || fromstr.indexOf("style!") === 0){ | ||
if (/^(css|style)!/gm.test(fromstr) || /\.(sass|scss|styl|css|less)$/gm.test(fromstr) || /scss!?$/gm.test(fromstr) || /css!$/gm.test(fromstr) || fromstr.indexOf("style!") === 0) { | ||
return { | ||
type:"style" | ||
,path:fromstr | ||
,alias:"" | ||
type: "style" | ||
, path: fromstr | ||
, alias: "" | ||
}; | ||
@@ -826,38 +853,38 @@ } | ||
type: 'template' | ||
,path: tagobject.url + parser_configs.templateExtension | ||
,alias: tagobject.alias | ||
, path: tagobject.url + parser_configs.templateExtension | ||
, alias: tagobject.alias | ||
} | ||
} | ||
function skipConditionExtractor(comp, indexLoopName){ | ||
function skipConditionExtractor(comp, indexLoopName) { | ||
var skipcomp = { | ||
type:"tag" | ||
,name:"skip" | ||
,attribs:{condition:comp.attribs["skip"]} | ||
type: "tag" | ||
, name: "skip" | ||
, attribs: { condition: comp.attribs["skip"] } | ||
}; | ||
delete comp.attribs["skip"]; | ||
//skipcomp.children=[comp]; | ||
skipcomp.children=comp.children; | ||
skipcomp.children = comp.children; | ||
delete comp.attribs["children"]; | ||
comp.children=[skipcomp]; | ||
comp.children = [skipcomp]; | ||
return componentToStr(comp, indexLoopName); | ||
} | ||
function ifConditionExtractor(comp, indexLoopName){ | ||
function ifConditionExtractor(comp, indexLoopName) { | ||
var ifcomp = { | ||
type:"tag" | ||
,name:"if" | ||
,attribs:{condition:comp.attribs["if"]} | ||
type: "tag" | ||
, name: "if" | ||
, attribs: { condition: comp.attribs["if"] } | ||
}; | ||
delete comp.attribs["if"]; | ||
ifcomp.children=[comp]; | ||
ifcomp.children = [comp]; | ||
return componentToStr(ifcomp, indexLoopName); | ||
} | ||
function forConditionExtractor(comp){ | ||
function forConditionExtractor(comp) { | ||
//console.log(comp); | ||
var forcomp = { | ||
type:"tag" | ||
,name:"for" | ||
,attribs:{"each":comp.attribs["each"],"dinamic":true} | ||
type: "tag" | ||
, name: "for" | ||
, attribs: { "each": comp.attribs["each"], "dinamic": true } | ||
}; | ||
@@ -870,10 +897,10 @@ delete comp.attribs["each"]; | ||
*/ | ||
forcomp.children=[comp]; | ||
forcomp.children = [comp]; | ||
return componentToStr(forcomp); | ||
} | ||
function componentToStr(comp, indexLoopName){ | ||
function componentToStr(comp, indexLoopName) { | ||
//ignorando os comentarios | ||
if(comp.type === 'comment'){ | ||
if (comp.type === 'comment') { | ||
return ""; | ||
@@ -883,47 +910,47 @@ } | ||
//eliminando os textos vazios | ||
if(comp.type === 'text'){ | ||
if (comp.type === 'text') { | ||
return tagTextToStr(comp, indexLoopName); | ||
} | ||
//tratando os skips embutidos | ||
if(comp.attribs && comp.attribs["skip"]){ | ||
if (comp.attribs && comp.attribs["skip"]) { | ||
return skipConditionExtractor(comp, indexLoopName); | ||
} | ||
//tratando os ifs embutidos | ||
if(comp.attribs && comp.attribs["if"]){ | ||
if (comp.attribs && comp.attribs["if"]) { | ||
return ifConditionExtractor(comp, indexLoopName); | ||
} | ||
//precisa esta aqui para evitar deadlock | ||
if(comp.name === 'for'){ | ||
if (comp.name === 'for') { | ||
return tagForToStr(comp, indexLoopName); | ||
} | ||
if(comp.attribs && comp.attribs["each"]){ | ||
if (comp.attribs && comp.attribs["each"]) { | ||
return forConditionExtractor(comp, indexLoopName); | ||
} | ||
if(comp.name === 'if'){ | ||
if (comp.name === 'if') { | ||
return tagIfToStr(comp, indexLoopName); | ||
} | ||
if(comp.name === 'skip'){ | ||
if (comp.name === 'skip') { | ||
return tagSkipToStr(comp, indexLoopName); | ||
} | ||
if(comp.name === 'else'){ | ||
if (comp.name === 'else') { | ||
return tagElseToStr(comp, indexLoopName); | ||
} | ||
if(comp.name === 'elseif'){ | ||
if (comp.name === 'elseif') { | ||
return tagElseIfToStr(comp, indexLoopName); | ||
} | ||
if(comp.name === 'route'){ | ||
if (comp.name === 'route') { | ||
return tagRouteToStr(comp, indexLoopName); | ||
} | ||
if(comp.name === 'compose'){ | ||
if (comp.name === 'compose') { | ||
return tagComposeToStr(comp, indexLoopName); | ||
} | ||
if(comp.name === 'content'){ | ||
if (comp.name === 'content') { | ||
return tagContentToStr(comp, indexLoopName); | ||
} | ||
if(comp.name === 'script'){ | ||
if (comp.name === 'script') { | ||
return tagCommandToStr(comp, indexLoopName); | ||
@@ -933,13 +960,13 @@ } | ||
if(comp.name === 'refresh'){ | ||
if (comp.name === 'refresh') { | ||
return tagRefreshToStr(comp, indexLoopName); | ||
} | ||
if(comp.name.indexOf("-") > 0 && requireScriptList.indexOf(comp.name) > -1){ | ||
if (comp.name.indexOf("-") > 0 && requireScriptList.indexOf(comp.name) > -1) { | ||
return tagRpFunctionToStr(comp, indexLoopName); | ||
} | ||
if(comp.name.indexOf('-') > 0){ | ||
if (comp.name.indexOf('-') > 0) { | ||
return tagCustomToStr(comp, indexLoopName); | ||
} | ||
} | ||
@@ -949,15 +976,15 @@ return tagBasicToStr(comp, indexLoopName); | ||
module.exports = function(rawHtml,config){ | ||
module.exports = function (rawHtml, config) { | ||
flush(); | ||
var finalBuffer = ""; | ||
parser_configs = Object.assign({},parser_configs,config); | ||
parser_configs = Object.assign({}, parser_configs, config); | ||
var handler = new htmlparser.DomHandler(function (error, dom) { | ||
if (error){ | ||
console.log(error) | ||
}else{ | ||
dom.filter(elementDom => elementDom.name === 'template').forEach(root_comp => appendBuffer(tagTemplateToStr(root_comp,config.viewModel))); | ||
if (error) { | ||
console.log(error) | ||
} else { | ||
dom.filter(elementDom => elementDom.name === 'template').forEach(root_comp => appendBuffer(tagTemplateToStr(root_comp, config.viewModel))); | ||
finalBuffer = buffer.join(''); | ||
} | ||
}); | ||
var parser = new htmlparser.Parser(handler,{decodeEntities: true,recognizeSelfClosing:true}); | ||
var parser = new htmlparser.Parser(handler, { decodeEntities: true, recognizeSelfClosing: true }); | ||
parser.write(rawHtml); | ||
@@ -964,0 +991,0 @@ //parser.write(rawHtml.replace(/[\n\t\r]/g," ")); |
34015
829