rs.utils.js
5.1 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
/**
* Runsa Function
*
* author:yijun
*
* Dependencies:
*
*/
(function($) {
/*
$.fn.yourPluginName = function(options) {
// 各种属性和参数
var options = $.extend(defaults, options);
this.each(function() {
// 插件的实现代码
});
};
*/
$.appendZero = function(num, n) {
return (Array(n).join(0) + num).slice(-n);
}
/**
* px to int
*/
$.parseCssInt = function(a) {
return parseInt(a.replace("px", ""));
}
$.JSON = function(a, b, c) {
var url = a || "./json";
var data;
var callback;
for (var i = 0; i < arguments.length; i++) {
if (typeof (arguments[i]) == "string") {
url = arguments[i];
} else if (typeof (arguments[i]) == "object") {
data = arguments[i];
} else if (typeof (arguments[i]) == "function") {
callback = arguments[i];
}
}
$.ajax({
url : url,
data : JSON.stringify(data),
type : "POST",
contentType : "application/json; charset=utf-8",
success : callback
});
}
$.getJSONP = function(a, b, c) {
var url = a || ".jsonp";
var data;
var callback;
for (var i = 0; i < arguments.length; i++) {
if (typeof (arguments[i]) == "string") {
url = arguments[i];
} else if (typeof (arguments[i]) == "object") {
data = arguments[i];
} else if (typeof (arguments[i]) == "function") {
callback = arguments[i];
}
}
$.ajax({
url : url,
data : data,
dataType : 'jsonp',
success : callback
});
};
/*检验返回格式*/
$.response=function(result) {
var success=false;
var data=(typeof result == 'string')?eval('(' + result + ')'):result;
if(data.status){
success=data && data.status == "success";
if (!success) {
$.messager.alert(resource.msg_title,resourceUtils(data.message));
}
}
return success;
}
/*加载过滤 */
$.loadFilterUtils=function(data,format){
var val;
var success=responseUtils(data);
var control=$(this);
var cls=control.attr("class");
if(format && typeof (format[i]) == "function"){
format.call(this, data.list);
}
if(cls.indexOf("datagrid")>-1){
return success?{rows:data.list,total:data.total}:{rows:[],total:0};
}else if(cls.indexOf("tree")>-1){//tree
return data.list;
}else{
return data.list;
}
}
/**
* enable request thread
*/
var threadList = {};
var threadSeed = 1;
var threadName = "taskThread";
$.taskThread = function(a, b) { // 任务线程
var options = a;
var id = b;
if (typeof options == 'string') {
if (id) {
$.taskThread.methods[options](id);
}
} else {
var obj = {};
var opts = $.extend({}, $.taskThread.defaults, options);
if (!opts.id) {
opts.id = threadName + (threadSeed++);
}
$.data(document, opts.id, opts);
threadList[opts.id] = null;
$.taskThread.methods["start"](opts.id);
}
};
$.taskThread.methods = {
start : function(id) {
var opts = $.data(document, id);
if (opts) {
if (threadList[opts.id]) {
clearInterval(threadList[opts.id]);
}
threadList[opts.id] = setInterval(function() {
var params = opts.onBegin.call(document, opts.params);
if (opts.dataType == "jsonp") {
$.JSONP(opts.url, params, opts.onSuccess);
} else {
$.ajax({
url : opts.url,
data : params,
type : opts.type,
dataType : opts.dataType,
success : opts.onSuccess
});
}
opts.onEnd.call(document, opts.id);
}, opts.delay);
}
},
stop : function(id) {
var opts = $.data(document, id);
if (opts && threadList[opts.id]) {
clearInterval(threadList[opts.id]);
}
}
};
$.taskThread.defaults = {
id : null,
delay : 60000,
start : true,
url : null,
type : "post",
dataType : "jsonp",
params : {},
onBegin : function(p) {
return p;
},
onSuccess : function(o) {
},
onEnd : function() {
}
}
$.encodeParamURI = function(a, b) {
var url;
var data;
for (var i = 0; i < arguments.length; i++) {
if (typeof (arguments[i]) == "string") {
url = arguments[i];
} else if (typeof (arguments[i]) == "object") {
data = arguments[i];
}
}
var existsParam = url && url.indexOf("?") > -1;
for (var key in data) {
if(data[key]!=null){
var link = existsParam ? "&" : "?";
url += (link + encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
if (!existsParam) {
existsParam = true;
}
}
}
return url;
}
/* 禁用后退 */
$.disableBack=function(){
$(document).bind("keydown",function(e){
if(e.keyCode==8){
var t=e.target.type || e.target.getAttribute('type');
var readonly = e.target.getAttribute('readonly');
if(t&&(t=="password" || t=="text" || t=="textarea")&&!(readonly=="readonly")){
return true;
}
// if (e&&e.stopPropagation){
// e.stopPropagation();
// }else{
// window.event.cancelBubble = true;
// }
return false;
}
return true;
});
}
$.beforeUnload = function(a) {
// 各种属性和参数
var message = a || "";
window.onbeforeunload = function(e) { // 绑定刷新等事件
if (e && e.preventDefault) {
e.preventDefault();
} else if (window.event) {
window.event.returnValue = message;
}
return message;
}
};
})(jQuery);