util.js
8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var $ = require('jquery');
var util = {
__ajax: function(options, callback, notip) {
if (typeof options.data == "string") {
util.__tip(options.data, "warning");
return false;
} else {
$.ajax({
type: options.type || 'POST',
url: options.url,
async: options.async,
dataType: 'json',
data: options.data || {},
success: function(res) {
res = res.data;
if (("" + res.code) === "200") {
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);
return false;
}
});
}
},
__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 || {},
success: function(res) {
res = res.data;
if (res.code != 200) {
util.__tip(res.message, "danger");
} else {
return callback.bind(this, res)();
}
}
});
}
},
__tip: function(message, callback) {
//danger
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";
}
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;
$.gritter.add(options);
},
__template: function() {
var args = arguments,
result;
if (args.length > 0) {
if (typeof args[0] === "string") {
result = args[0];
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 reg = new RegExp("({" + regkey + "})", "g");
result = result.replace(reg, args[1][key]);
}
}
}
var reg = new RegExp("({.*})", "g");
result = result.replace(reg, '');
}
}
return result;
},
__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]"){');
script.push(' for(var i=0;i<obj.length;i++){');
script.push(' fn.call(i,i,obj[i]);');
script.push(' }');
script.push(' return;');
script.push(' }');
script.push(' if(typeof obj=="function"||typeof obj=="object"&&!!obj){');
script.push(' for(var i in obj){');
script.push(' fn.call(i,i,obj[i]);');
script.push(' }');
script.push(' return;');
script.push(' }');
script.push('}');
result = script.join('') + ";\n";
var formstr = function(code) {
return "'" + code
.replace(/('|\\)/g, '\\$1')
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n') + "'";
}
//获取参数
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($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 {
params.push(trim);
return "[[+(" + trim + ")+]]";
}
});
// 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+$/, '');
// console.log(":" + ap + ":");
if (!/^[0-9]+$/.test(ap) && !/^\s*$/.test(ap) && !/\./g.test(ap)) {
if (!source.hasOwnProperty(ap)) {
source[ap] = "";
// console.log(":" + 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]);
}
}
result += "return p.join('');\n";
var fn = new Function(header + result);
var b = fn.call(source);
return b;
},
__input: function(id) {
return !!~$.trim($('#' + id).val()) ? $('#' + id).val() : ""
},
__dateFormat:function(data,format) {
var date = {
"M+": data.getMonth() + 1,
"d+": data.getDate(),
"h+": data.getHours(),
"m+": data.getMinutes(),
"s+": data.getSeconds(),
"q+": Math.floor((data.getMonth() + 3) / 3),
"S+": data.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (data.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
}
module.exports = util;