bin.shen
2016-12-05 a4c9331bbfe3e8765ccdc1c54cc6931bac49cc82
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
 * @author ZhangHuihua@msn.com
 * 
 */
 
/**
 * 普通ajax表单提交
 * @param {Object} form
 * @param {Object} callback
 * @param {String} confirmMsg 提示确认信息
 */
function validateCallback(form, callback, confirmMsg) {
    var $form = $(form);
 
    if (!$form.valid()) {
        return false;
    }
    
    var _submitFn = function(){
        $.ajax({
            type: form.method || 'POST',
            url:$form.attr("action"),
            data:$form.serializeArray(),
            dataType:"json",
            cache: false,
            success: callback || DWZ.ajaxDone,
            error: DWZ.ajaxError
        });
    }
    
    if (confirmMsg) {
        alertMsg.confirm(confirmMsg, {okCall: _submitFn});
    } else {
        _submitFn();
    }
    
    return false;
}
/**
 * 带文件上传的ajax表单提交
 * @param {Object} form
 * @param {Object} callback
 */
function iframeCallback(form, callback){
    var $form = $(form), $iframe = $("#callbackframe");
    if(!$form.valid()) {return false;}
 
    if ($iframe.size() == 0) {
        $iframe = $("<iframe id='callbackframe' name='callbackframe' src='about:blank' style='display:none'></iframe>").appendTo("body");
    }
    if(!form.ajax) {
        $form.append('<input type="hidden" name="ajax" value="1" />');
    }
    form.target = "callbackframe";
    
    _iframeResponse($iframe[0], callback || DWZ.ajaxDone);
}
function _iframeResponse(iframe, callback){
    var $iframe = $(iframe), $document = $(document);
    
    $document.trigger("ajaxStart");
    
    $iframe.bind("load", function(event){
        $iframe.unbind("load");
        $document.trigger("ajaxStop");
        
        if (iframe.src == "javascript:'%3Chtml%3E%3C/html%3E';" || // For Safari
            iframe.src == "javascript:'<html></html>';") { // For FF, IE
            return;
        }
 
        var doc = iframe.contentDocument || iframe.document;
 
        // fixing Opera 9.26,10.00
        if (doc.readyState && doc.readyState != 'complete') return; 
        // fixing Opera 9.64
        if (doc.body && doc.body.innerHTML == "false") return;
       
        var response;
        
        if (doc.XMLDocument) {
            // response is a xml document Internet Explorer property
            response = doc.XMLDocument;
        } else if (doc.body){
            try{
                response = $iframe.contents().find("body").text();
                response = jQuery.parseJSON(response);
            } catch (e){ // response is html document or plain text
                response = doc.body.innerHTML;
            }
        } else {
            // response is a xml document
            response = doc;
        }
        
        callback(response);
    });
}
 
/**
 * navTabAjaxDone是DWZ框架中预定义的表单提交回调函数.
 * 服务器转回navTabId可以把那个navTab标记为reloadFlag=1, 下次切换到那个navTab时会重新载入内容. 
 * callbackType如果是closeCurrent就会关闭当前tab
 * 只有callbackType="forward"时需要forwardUrl值
 * navTabAjaxDone这个回调函数基本可以通用了,如果还有特殊需要也可以自定义回调函数.
 * 如果表单提交只提示操作是否成功, 就可以不指定回调函数. 框架会默认调用DWZ.ajaxDone()
 * <form action="/user.do?method=save" onsubmit="return validateCallback(this, navTabAjaxDone)">
 * 
 * form提交后返回json数据结构statusCode=DWZ.statusCode.ok表示操作成功, 做页面跳转等操作. statusCode=DWZ.statusCode.error表示操作失败, 提示错误原因. 
 * statusCode=DWZ.statusCode.timeout表示session超时,下次点击时跳转到DWZ.loginUrl
 * {"statusCode":"200", "message":"操作成功", "navTabId":"navNewsLi", "forwardUrl":"", "callbackType":"closeCurrent", "rel"."xxxId"}
 * {"statusCode":"300", "message":"操作失败"}
 * {"statusCode":"301", "message":"会话超时"}
 * 
 */
function navTabAjaxDone(json){
    DWZ.ajaxDone(json);
    if (json.statusCode == DWZ.statusCode.ok){
        if (json.navTabId){ //把指定navTab页面标记为需要“重新载入”。注意navTabId不能是当前navTab页面的
            navTab.reloadFlag(json.navTabId);
        } else { //重新载入当前navTab页面
            var $pagerForm = $("#pagerForm", navTab.getCurrentPanel());
            var args = $pagerForm.size()>0 ? $pagerForm.serializeArray() : {}
            navTabPageBreak(args, json.rel);
        }
        
        if ("closeCurrent" == json.callbackType) {
            setTimeout(function(){navTab.closeCurrentTab(json.navTabId);}, 100);
        } else if ("forward" == json.callbackType) {
            navTab.reload(json.forwardUrl);
        } else if ("forwardConfirm" == json.callbackType) {
            alertMsg.confirm(json.confirmMsg || DWZ.msg("forwardConfirmMsg"), {
                okCall: function(){
                    navTab.reload(json.forwardUrl);
                },
                cancelCall: function(){
                    navTab.closeCurrentTab(json.navTabId);
                }
            });
        } else {
            navTab.getCurrentPanel().find(":input[initValue]").each(function(){
                var initVal = $(this).attr("initValue");
                $(this).val(initVal);
            });
        }
    }
}
 
/**
 * dialog上的表单提交回调函数
 * 当前navTab页面有pagerForm就重新加载
 * 服务器转回navTabId,可以重新载入指定的navTab. statusCode=DWZ.statusCode.ok表示操作成功, 自动关闭当前dialog
 * 
 * form提交后返回json数据结构,json格式和navTabAjaxDone一致
 */
function dialogAjaxDone(json){
    DWZ.ajaxDone(json);
    if (json.statusCode == DWZ.statusCode.ok){
        if (json.navTabId){
            navTab.reload(json.forwardUrl, {navTabId: json.navTabId});
        } else {
            var $pagerForm = $("#pagerForm", navTab.getCurrentPanel());
            var args = $pagerForm.size()>0 ? $pagerForm.serializeArray() : {}
            navTabPageBreak(args, json.rel);
        }
        if ("closeCurrent" == json.callbackType) {
            $.pdialog.closeCurrent();
        }
    }
}
 
/**
 * 处理navTab上的查询, 会重新载入当前navTab
 * @param {Object} form
 */
function navTabSearch(form, navTabId){
    var $form = $(form);
    if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;
    navTab.reload($form.attr('action'), {data: $form.serializeArray(), navTabId:navTabId});
    return false;
}
/**
 * 处理dialog弹出层上的查询, 会重新载入当前dialog
 * @param {Object} form
 */
function dialogSearch(form){
    var $form = $(form);
    if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;
    $.pdialog.reload($form.attr('action'), {data: $form.serializeArray()});
    return false;
}
function dwzSearch(form, targetType){
    if (targetType == "dialog") dialogSearch(form);
    else navTabSearch(form);
    return false;
}
/**
 * 处理div上的局部查询, 会重新载入指定div
 * @param {Object} form
 */
function divSearch(form, rel){
    var $form = $(form);
    if (form[DWZ.pageInfo.pageNum]) form[DWZ.pageInfo.pageNum].value = 1;
    if (rel) {
        var $box = $("#" + rel);
        $box.ajaxUrl({
            type:"POST", url:$form.attr("action"), data: $form.serializeArray(), callback:function(){
                $box.find("[layoutH]").layoutH();
            }
        });
    }
    return false;
}
/**
 * 
 * @param {Object} args {pageNum:"",numPerPage:"",orderField:"",orderDirection:""}
 * @param String formId 分页表单选择器,非必填项默认值是 "pagerForm"
 */
function _getPagerForm($parent, args) {
    var form = $("#pagerForm", $parent).get(0);
 
    if (form) {
        if (args["pageNum"]) form[DWZ.pageInfo.pageNum].value = args["pageNum"];
        if (args["numPerPage"]) form[DWZ.pageInfo.numPerPage].value = args["numPerPage"];
        if (args["orderField"]) form[DWZ.pageInfo.orderField].value = args["orderField"];
        if (args["orderDirection"] && form[DWZ.pageInfo.orderDirection]) form[DWZ.pageInfo.orderDirection].value = args["orderDirection"];
    }
    
    return form;
}
 
 
/**
 * 处理navTab中的分页和排序
 * targetType: navTab 或 dialog
 * rel: 可选 用于局部刷新div id号
 * data: pagerForm参数 {pageNum:"n", numPerPage:"n", orderField:"xxx", orderDirection:""}
 * callback: 加载完成回调函数
 */
function dwzPageBreak(options){
    var op = $.extend({ targetType:"navTab", rel:"", data:{pageNum:"", numPerPage:"", orderField:"", orderDirection:""}, callback:null}, options);
    var $parent = op.targetType == "dialog" ? $.pdialog.getCurrent() : navTab.getCurrentPanel();
 
    if (op.rel) {
        var $box = $parent.find("#" + op.rel);
        var form = _getPagerForm($box, op.data);
        if (form) {
            $box.ajaxUrl({
                type:"POST", url:$(form).attr("action"), data: $(form).serializeArray(), callback:function(){
                    $box.find("[layoutH]").layoutH();
                }
            });
        }
    } else {
        var form = _getPagerForm($parent, op.data);
        var params = $(form).serializeArray();
        
        if (op.targetType == "dialog") {
            if (form) $.pdialog.reload($(form).attr("action"), {data: params, callback: op.callback});
        } else {
            if (form) navTab.reload($(form).attr("action"), {data: params, callback: op.callback});
        }
    }
}
/**
 * 处理navTab中的分页和排序
 * @param args {pageNum:"n", numPerPage:"n", orderField:"xxx", orderDirection:""}
 * @param rel: 可选 用于局部刷新div id号
 */
function navTabPageBreak(args, rel){
    dwzPageBreak({targetType:"navTab", rel:rel, data:args});
}
/**
 * 处理dialog中的分页和排序
 * 参数同 navTabPageBreak 
 */
function dialogPageBreak(args, rel){
    dwzPageBreak({targetType:"dialog", rel:rel, data:args});
}
 
 
function ajaxTodo(url, callback){
    var $callback = callback || navTabAjaxDone;
    if (! $.isFunction($callback)) $callback = eval('(' + callback + ')');
    $.ajax({
        type:'POST',
        url:url,
        dataType:"json",
        cache: false,
        success: $callback,
        error: DWZ.ajaxError
    });
}
 
/**
 * http://www.uploadify.com/documentation/uploadify/onqueuecomplete/    
 */
function uploadifyQueueComplete(queueData){
alert(queueData.uploadsSuccessful);
    var msg = "The total number of files uploaded: "+queueData.uploadsSuccessful+"<br/>"
        + "The total number of errors while uploading: "+queueData.uploadsErrored+"<br/>"
        + "The total number of bytes uploaded: "+queueData.queueBytesUploaded+"<br/>"
        + "The average speed of all uploaded files: "+queueData.averageSpeed;
    
    if (queueData.uploadsErrored) {
        alertMsg.error(msg);
    } else {
        alertMsg.correct(msg);
    }
}
/**
 * http://www.uploadify.com/documentation/uploadify/onuploadsuccess/
 */
function uploadifySuccess(file, data, response){
    console.log(response);
}
 
/**
 * http://www.uploadify.com/documentation/uploadify/onuploaderror/
 */
function uploadifyError(file, errorCode, errorMsg) {
    alertMsg.error(errorCode+": "+errorMsg);
}
 
 
/**
 * http://www.uploadify.com/documentation/
 * @param {Object} event
 * @param {Object} queueID
 * @param {Object} fileObj
 * @param {Object} errorObj
 */
function uploadifyError(event, queueId, fileObj, errorObj){
    alert("event:" + event + "\nqueueId:" + queueId + "\nfileObj.name:" 
        + fileObj.name + "\nerrorObj.type:" + errorObj.type + "\nerrorObj.info:" + errorObj.info);
}
 
 
$.fn.extend({
    ajaxTodo:function(){
        return this.each(function(){
            var $this = $(this);
            $this.click(function(event){
                var url = unescape($this.attr("href")).replaceTmById($(event.target).parents(".unitBox:first"));
                DWZ.debug(url);
                if (!url.isFinishedTm()) {
                    alertMsg.error($this.attr("warn") || DWZ.msg("alertSelectMsg"));
                    return false;
                }
                var title = $this.attr("title");
                if (title) {
                    alertMsg.confirm(title, {
                        okCall: function(){
                            ajaxTodo(url, $this.attr("callback"));
                        }
                    });
                } else {
                    ajaxTodo(url, $this.attr("callback"));
                }
                event.preventDefault();
            });
        });
    },
    dwzExport: function(){
        function _doExport($this) {
            var $p = $this.attr("targetType") == "dialog" ? $.pdialog.getCurrent() : navTab.getCurrentPanel();
            var $form = $("#pagerForm", $p);
            var url = $this.attr("href");
            window.location = url+(url.indexOf('?') == -1 ? "?" : "&")+$form.serialize();
        }
        
        return this.each(function(){
            var $this = $(this);
            $this.click(function(event){
                var title = $this.attr("title");
                if (title) {
                    alertMsg.confirm(title, {
                        okCall: function(){_doExport($this);}
                    });
                } else {_doExport($this);}
            
                event.preventDefault();
            });
        });
    }
});