Authored by liuyue

Merge branch 'release/1.6' of git.dev.yoho.cn:platform/yohobuy-portal-fe into release/1.6

@@ -96,7 +96,6 @@ var util = { @@ -96,7 +96,6 @@ var util = {
96 }, 96 },
97 __template2: function(str, source) { 97 __template2: function(str, source) {
98 // console.log(str); 98 // console.log(str);
99 - var result = "";  
100 var script = []; 99 var script = [];
101 script.push('var each=function(obj,fn){'); 100 script.push('var each=function(obj,fn){');
102 script.push(' var that=this;'); 101 script.push(' var that=this;');
@@ -112,66 +111,81 @@ var util = { @@ -112,66 +111,81 @@ var util = {
112 script.push(' }'); 111 script.push(' }');
113 script.push(' return;'); 112 script.push(' return;');
114 script.push(' }'); 113 script.push(' }');
115 - script.push('}');  
116 - result = script.join('') + ";\n"; 114 + script.push('};');
117 115
118 var formstr = function(code) { 116 var formstr = function(code) {
119 - return "'" + code  
120 - .replace(/('|\\)/g, '\\$1')  
121 - .replace(/\r/g, '\\r')  
122 - .replace(/\n/g, '\\n') + "'";  
123 - }  
124 - //获取参数  
125 -  
126 - var params = [];  
127 -  
128 -  
129 - var html = "";  
130 - html = str.replace(/\[\[(.*?)\]\]/igm, function(name, $1) {  
131 - var trim = $1.replace(/^\s+|\s+$/, ''); 117 + return "'" + code
  118 + .replace(/('|\\)/g, '\\$1')
  119 + .replace(/\r/g, '\\r')
  120 + .replace(/\n/g, '\\n') + "'";
  121 + }
  122 + var formJs=function(code){
  123 + var trim = code.replace(/^\s+|\s+$/, '');
  124 + //each goodsSizeList as item __index
132 if (/^each/.test(trim)) { 125 if (/^each/.test(trim)) {
133 - trim = trim.replace(/^each\s+([a-zA-Z0-1.]+)\s+as\s+(\w+)\s+(\w+)/, function($0, $1, $2, $3) {  
134 - params.push($1); 126 + return trim.replace(/^each\b([\w\W]+)\bas\b([\w\W]+)\b([\w\W]+)/, function($0, $1, $2, $3) {
135 return 'each\( ' + $1 + ',function(' + $3 + ',' + $2 + '){'; 127 return 'each\( ' + $1 + ',function(' + $3 + ',' + $2 + '){';
136 }); 128 });
137 - return "[[" + trim + "]]";  
138 - } else  
139 - if (/^\/each/.test(trim)) {  
140 - trim = trim.replace(/^\/each/, '});')  
141 - return "[[" + trim + "]]"; 129 + }else if (/^\/each/.test(trim)) {
  130 + return trim.replace(/^\/each/, '});')
142 } else if (/^if/.test(trim)) { 131 } else if (/^if/.test(trim)) {
143 - trim = trim.replace(/^if(.+)/, function($0, $1) {  
144 - params.push($1); 132 + return trim.replace(/^if(.+)/, function($0, $1) {
145 return 'if(' + $1 + '){'; 133 return 'if(' + $1 + '){';
146 }); 134 });
147 - return "[[" + trim + "]]";  
148 } else if (/^\/if/.test(trim)) { 135 } else if (/^\/if/.test(trim)) {
149 - trim = trim.replace(/^\/if/, '};')  
150 - return "[[" + trim + "]]"; 136 + return trim.replace(/^\/if/, '};')
151 } else if (/^else$/.test(trim)) { 137 } else if (/^else$/.test(trim)) {
152 - trim = trim.replace(/else/, '}else{');  
153 - return "[[" + trim + "]]";  
154 - } else {  
155 - params.push(trim);  
156 - return "[[+(" + trim + ")+]]"; 138 + return trim.replace(/else/, '}else{');
  139 + } else{
  140 + return "result.push("+code+")";
157 } 141 }
158 - });  
159 - // console.log(params);  
160 - //.match(/(\w+)+/g)  
161 - for (var i in params) {  
162 - var aps = params[i].split(/\+|\-|\*|\/|\=|\!|\~|'|"|\||&/g);  
163 - for (var j in aps) {  
164 - var ap = aps[j].replace(/^\s+|\s+$/, '');  
165 - // console.log(":" + ap + ":");  
166 - if (!/^[0-9]+$/.test(ap) && !/^\s*$/.test(ap) && !/\./g.test(ap)) {  
167 - if (!source.hasOwnProperty(ap)) {  
168 - source[ap] = "";  
169 - // console.log(":" + ap + ":");  
170 - } 142 + }
  143 + // var PARAMS={};
  144 + var OBJECT=/\.\w+|'.*'|".*"/g;
  145 + var KEYWORDS='for,if,else,each,as';
  146 + var SPECHAR=/\W/g;
  147 + var NUMBER=/^\d[^,]*|,\d[^,]*/g;
  148 + var SPLIT2_RE = /^$|,+/;
  149 + var REGEXKEYWORDS=new RegExp("\\b"+[KEYWORDS.replace(/,/g,'\\b|\\b')].join('|'),'g');
  150 + var getVar=function(code){
  151 + var strArr= code.replace(OBJECT,'')
  152 + .replace(SPECHAR,',')
  153 + .replace(REGEXKEYWORDS,'')
  154 + .replace(NUMBER,'')
  155 + .split(SPLIT2_RE);
  156 + for(var key in strArr){
  157 + if(strArr[key]&&!source.hasOwnProperty(strArr[key])){
  158 + source[strArr[key]]="";
171 } 159 }
172 } 160 }
173 } 161 }
174 162
  163 + var code=[],params={};
  164 + var arr=str.split(/\[\[|\]\]/).map(function(item,index){
  165 + if(index%2){
  166 + return{
  167 + value:item,
  168 + regex:true
  169 + };
  170 + }else{
  171 + return{
  172 + value:item,
  173 + regex:false
  174 + };
  175 + }
  176 + });
  177 +
  178 + arr.forEach(function(item,index){
  179 + if(item.regex){
  180 + getVar(item.value);
  181 + code.push(formJs(item.value));
  182 + }else{
  183 + code.push("result.push("+formstr(item.value)+");");
  184 + }
  185 + });
  186 + // var header="var result=[],";
  187 + // header+=Object.keys(source).join(',')+";"
  188 +
175 var keys = []; 189 var keys = [];
176 for (var key in source) { 190 for (var key in source) {
177 if (typeof source[key] == "boolean") { 191 if (typeof source[key] == "boolean") {
@@ -180,27 +194,16 @@ var util = { @@ -180,27 +194,16 @@ var util = {
180 keys.push(key + "=" + JSON.stringify(source[key])); 194 keys.push(key + "=" + JSON.stringify(source[key]));
181 } 195 }
182 } 196 }
183 - var header = "var " + keys.join(',') + ";"; 197 + var header = "var result=[]," + keys.join(',') + ";";
  198 +
184 199
185 - html = html.replace(/(^|\]\])(.*?)(\[\[|$)/igm, function(name, $1, $2) {  
186 - var trim = $2.replace(/^\s+|\s+$/, '');  
187 - return trim ? formstr($2) : '';  
188 - });  
189 - // var result="var result='';result+=";  
190 - var arr = html.split(/\n+/igm);  
191 - result += "var p=[];\n";  
192 - for (var i = 0; i < arr.length; i++) {  
193 - if (/^'/.test(arr[i])) {  
194 - result += "p.push(" + (arr[i]) + ");\n";  
195 - } else {  
196 - result += (arr[i]);  
197 - }  
198 - }  
199 - result += "return p.join('');\n";  
200 200
201 - var fn = new Function(header + result);  
202 - var b = fn.call(source);  
203 - return b; 201 + var result=header + code.join('\n')+"return result.join('');";
  202 +
  203 + console.log(result);
  204 +
  205 + var fn = new Function(script.join('')+result);
  206 + return fn.call(source);
204 }, 207 },
205 __input: function(id) { 208 __input: function(id) {
206 return !!~$.trim($('#' + id).val()) ? $('#' + id).val() : "" 209 return !!~$.trim($('#' + id).val()) ? $('#' + id).val() : ""