Authored by liuyue

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

var $ = require('jquery');
var util = {
__ajax: function(options, callback, notip) {
if (typeof options.data == "string") {
util.__tip(options.data, "warning");
var $=require('jquery');
var util={
__ajax:function(options,callback,notip){
if(typeof options.data=="string"){
util.__tip(options.data,"warning");
return false;
} else {
}else{
$.ajax({
type: options.type || 'POST',
type: options.type||'POST',
url: options.url,
async: options.async,
dataType: 'json',
data: options.data || {},
data:options.data||{},
success: function(res) {
res = res.data;
res=res.data;
if (("" + res.code) === "200") {
notip ? (callback && callback.bind(this, res)()) : util.__tip(res.message, "success", callback && callback.bind(this, res)());
notip?(callback&&callback.bind(this,res)()):util.__tip(res.message,"success",callback&&callback.bind(this,res)());
return true;
} else {
(!notip) && util.__tip(res.message, "danger");
return false;
}
},
error: function(err) {
console.log("ajax.ERROR", err);
error:function(err){
console.log("ajax.ERROR",err);
return false;
}
});
}
},
__ajax2: function(options, callback) {
if (typeof options.data == "string") {
util.__tip(options.data, "warning");
} else {
__ajax2:function(options,callback){
if(typeof options.data=="string"){
util.__tip(options.data,"warning");
}else{
$.ajax({
type: 'POST',
url: options.url,
async: options.async,
dataType: 'json',
data: options.data || {},
data:options.data||{},
success: function(res) {
res = res.data;
res=res.data;
if (res.code != 200) {
util.__tip(res.message, "danger");
util.__tip(res.message,"danger");
} else {
return callback.bind(this, res)();
return callback.bind(this,res)();
}
}
});
}
},
__tip: function(message, callback) {
__tip:function(message,callback){
//danger
var options = {};
options.class_name = "growl-danger";
options.title = arguments[0];
var options={};
options.class_name="growl-danger";
options.title=arguments[0];
if (arguments.length > 1) {
if (typeof(arguments[1]) === "string") {
options.class_name = "growl-" + arguments[1];
} else {
options.class_name = "growl-success";
if (typeof (arguments[1]) === "string") {
options.class_name="growl-"+arguments[1];
}else{
options.class_name="growl-success";
}
var lastargs = Array.prototype.slice.call(arguments, arguments.length - 1);
lastargs = lastargs[0];
if (typeof lastargs === "function") {
options.after_open = setTimeout(function() {
lastargs && lastargs();
}, 600);
var lastargs = Array.prototype.slice.call(arguments, arguments.length-1);
lastargs=lastargs[0];
if(typeof lastargs==="function"){
options.after_open=setTimeout(function() {
lastargs&&lastargs();
},600);
}
}
options.sticky = false;
options.time = 1000;
options.sticky=false;
options.time=1000;
$.gritter.add(options);
},
__template: function() {
var args = arguments,
result;
__template: function () {
var args = arguments, result;
if (args.length > 0) {
if (typeof args[0] === "string") {
if (typeof args[0]==="string") {
result = args[0];
if (args.length == 2 && typeof args[1] == "object") {
if (args.length == 2 && typeof args[1]=="object") {
for (var key in args[1]) {
if (args[1][key] != undefined) {
var regkey = ((+key) == (+key)) ? "[" + key + "]" : key;
var regkey=((+key)==(+key))?"["+key+"]":key;
var reg = new RegExp("({" + regkey + "})", "g");
result = result.replace(reg, args[1][key]);
}
... ... @@ -94,9 +93,10 @@ var util = {
}
return result;
},
__template2: function(str, source) {
var result = "";
var script = [];
__template2:function(str,source){
// console.log(str);
var result="";
var script=[];
script.push('var each=function(obj,fn){');
script.push(' var that=this;');
script.push(' if({}.toString.call(obj)=="[object Array]"){');
... ... @@ -112,91 +112,103 @@ var util = {
script.push(' return;');
script.push(' }');
script.push('}');
result = script.join('') + ";\n";
result=script.join('')+";\n";
var formstr = function(code) {
var formstr=function (code) {
return "'" + code
.replace(/('|\\)/g, '\\$1')
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n') + "'";
}
//获取参数
var keys = [];
for (var key in source) {
if (typeof source[key] == "boolean") {
keys.push(key + "=" + source[key]);
} else {
keys.push(key + "=" + JSON.stringify(source[key]));
}
}
//result+="var "+keys.join(',')+";";
result += "var obj=" + JSON.stringify(source) + ";";
var html = "";
html = str.replace(/\[\[(.*?)\]\]/igm, function(name, $1) {
var trim = $1.replace(/^\s+|\s+$/, '');
if (/^each/.test(trim)) {
trim = trim.replace(/^each\s+([a-zA-Z0-1.]+)\s+as\s+(\w+)\s+(\w+)/, function(s, $1, $2, $3) {
if (!/\w+\.\w+/g.test($1)) {
return 'each\( obj.' + $1 + ',function(' + $3 + ',' + $2 + '){';
}
return 'each\( ' + $1 + ',function(' + $3 + ',' + $2 + '){';
var params=[];
var html="";
html=str.replace(/\[\[(.*?)\]\]/igm,function(name,$1){
var trim=$1.replace(/^\s+|\s+$/,'');
if(/^each/.test(trim))
{
trim=trim.replace(/^each\s+([a-zA-Z0-1.]+)\s+as\s+(\w+)\s+(\w+)/,function($0,$1,$2,$3){
params.push($1);
return 'each\( '+$1+',function('+$3+','+$2+'){';
});
return "[[" + trim + "]]";
} else
if (/^\/each/.test(trim)) {
trim = trim.replace(/^\/each/, '});')
return "[[" + trim + "]]";
} else if (/^if/.test(trim)) {
trim = trim.replace(/^if(.+)/, function(s, $1) {
var aa = $1.replace(/^\s+|\s+$/, '');
if (!/\w+\.\w+/g.test(aa)) {
return 'if(obj.' + aa + '){';
}
return "[["+trim+"]]";
}else
if(/^\/each/.test(trim)){
trim=trim.replace(/^\/each/,'});')
return "[["+trim+"]]";
}
else if(/^if/.test(trim)){
trim=trim.replace(/^if(.+)/,function($0,$1){
params.push($1);
return 'if('+$1+'){';
});
return "[[" + trim + "]]";
} else if (/^\/if/.test(trim)) {
trim = trim.replace(/^\/if/, '};')
return "[[" + trim + "]]";
} else if (/^else$/.test(trim)) {
trim = trim.replace(/else/, '}else{');
return "[[" + trim + "]]";
} else {
var aa = $1.replace(/^\s+|\s+$/, '');
if (!/\w+\.\w+/g.test(aa)) {
if (!source[aa]) {
return "";
return "[["+trim+"]]";
}
else if(/^\/if/.test(trim)){
trim=trim.replace(/^\/if/,'};')
return "[["+trim+"]]";
}
return "[[+obj." + aa + "+]]";
else if(/^else$/.test(trim)){
trim=trim.replace(/else/,'}else{');
return "[["+trim+"]]";
}
return "[[+" + trim + "+]]";
else{
params.push(trim);
return "[[+"+trim+"+]]";
}
});
html = html.replace(/(^|\]\])(.*?)(\[\[|$)/igm, function(name, $1, $2) {
var trim = $2.replace(/^\s+|\s+$/, '');
return trim ? formstr($2) : '';
console.log(params);
//.match(/(\w+)+/g)
for(var i in params){
var aps=params[i].split(/\+|\-|\*|\/|\=|\!|\~/g);
for(var j in aps){
var ap=aps[j].replace(/^\s+|\s+$/,'');
if(!/^[0-9]+$/.test(ap)&&!/^\s*$/.test(ap)&&!/\./g.test(ap)){
if(!source.hasOwnProperty(ap)){
source[ap]="";
}
}
}
}
var keys=[];
for(var key in source){
if(typeof source[key]=="boolean"){
keys.push(key+"="+source[key]);
}else{
keys.push(key+"="+JSON.stringify(source[key]));
}
}
var header="var "+keys.join(',')+";";
html=html.replace(/(^|\]\])(.*?)(\[\[|$)/igm,function(name,$1,$2){
var trim=$2.replace(/^\s+|\s+$/,'');
return trim?formstr($2):'';
});
// var result="var result='';result+=";
var arr = html.split(/\n+/igm);
result += "var p=[];\n";
for (var i = 0; i < arr.length; i++) {
if (/^'/.test(arr[i])) {
result += "p.push(" + (arr[i]) + ");\n";
} else {
result += (arr[i]);
var arr=html.split(/\n+/igm);
result+="var p=[];\n";
for(var i=0;i<arr.length;i++){
if(/^'/.test(arr[i])){
result+="p.push("+(arr[i])+");\n";
}else{
result+=(arr[i]);
}
}
result += "return p.join('');\n";
result+="return p.join('');\n";
var fn = new Function(result);
var b = fn.call(source);
var fn=new Function(header+result);
var b=fn.call(source);
return b;
},
__input: function(id) {
return !!~$.trim($('#' + id).val()) ? $('#' + id).val() : ""
__input:function(id){
return !!~$.trim($('#'+id).val())?$('#'+id).val():""
}
}
module.exports = util;
\ No newline at end of file
module.exports=util;
\ No newline at end of file
... ...