makehtml.js
4.59 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
/*
[Discuz!] (C)2001-2099 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: makehtml.js 33047 2013-04-12 08:46:56Z zhangguosheng $
*/
function make_html(url, obj) {
var x = Ajax();
if(url && url.indexOf('?') < 0) {
url = url + '?';
}
x.getJSON(url+'&_makehtml&r='+(+ new Date()), function(ret){
var title = obj ? obj.getAttribute('mktitle') || '' : '';
if(ret && (ret=ret['data']) && ret['status'] == 'html_ok') {
if(obj) {
obj.style.color = 'blue';
obj.innerHTML = '<a href="'+ret['path']+'" target="_blank">'+title+'生成成功</a>';
}
if(ret['nexturl']) {
if(obj) {
obj.style.color = 'green';
obj.innerHTML = '生成'+title+(Math.round((ret['current']/ret['count'])*100))+'%';
}
make_html(ret['nexturl'], obj);
}
} else {
if(obj) {
obj.style.color = 'red';
obj.innerHTML = title+'生成失败';
}
}
});
}
function make_html_batch(url, ids, callback, dom, single) {
this.url = url;
this.ids = ids;
this.count = this.ids.length;
this.callback = callback;
this.dom = dom;
this.single = single && 1;
this.makedcount = 0;
this.jumptime = 2000;
if(this.single) {
this.make(this.ids, this.dom);
} else if(this.ids) {
id = this.ids.pop();
var child = document.createElement('div');
child.style.color = 'green';
var cent = ((1/this.count)*100).toFixed(2);
progress_bar(cent);
child.innerHTML = '开始生成'+this.dom.getAttribute('mktitle');
this.dom.innerHTML = '';
this.dom.appendChild(child);
this.make(id, child);
this.child = child;
var child2 = document.createElement('div');
child2.innerHTML = '<a href="javascript:void(0);" id="mk_goon">如果您的浏览器没有反应,请点击继续...</a>';
this.dom.appendChild(child2);
var obj = this;
$('mk_goon').onclick = function (e) {make_html_batch.prototype.make_goon.call(obj, e)};
}
}
make_html_batch.prototype = {
make_goon : function (){
var id = this.ids.pop();
if(id) {
this.make(this.ids.pop(), this.child);
} else if(this.callback) {
var obj = this;
setTimeout(function(){obj.dom.style.display = 'none';(obj.callback)();}, 1000);
}
},
make : function (id, child) {
var obj = this;
var x = Ajax();
x.getJSON(this.url+id+'&_makehtml&r='+(+ new Date()), function(ret){
if(ret && (data=ret['data']) && data['status'] == 'html_ok') {
obj.makedcount++;
if(data['nexturl']) {
make_html(data['nexturl']);
}
} else if(ret && ret['message']) {
var makehtml_error = $('makehtml_error');
if(!makehtml_error) {
obj.jumptime = 500000;
makehtml_error = document.createElement('div');
makehtml_error.style.color = 'red';
makehtml_error.style.height = '200px';
makehtml_error.style.overflow = 'scroll';
makehtml_error.id = 'makehtml_error';
makehtml_error.innerHTML = '错误信息';
obj.dom.appendChild(makehtml_error);
}
makehtml_error.innerHTML += '<br>[id:' + id + ']' + ret['message'];
makehtml_error.scrollTop = makehtml_error.scrollHeight;
}
if(obj.single) {
child.style.color = 'blue';
child.innerHTML = '<div class="mk_msg">'+'<a href="'+data['path']+'" target="_blank">'+obj.dom.getAttribute('mktitle')+'</a>生成完成'+'</div>';
if(obj.callback) {
setTimeout(function(){(obj.callback)();}, 2000);
}
} else if((id = obj.ids.pop()) || obj.ids.length == 0){
var current = obj.count - obj.ids.length;
var cent = ((current/obj.count)*100).toFixed(2);
progress_bar(cent);
var str = '本次共需要生成'+obj.count+'个'+obj.dom.getAttribute('mktitle')+'文件,成功生成'+obj.makedcount+'个,';
if(cent != 100) {
child.innerHTML = str+'正在生成第'+current+'个,已经完成'+cent+'%';
} else {
child.style.color = 'blue';
child.innerHTML = str+obj.dom.getAttribute('mktitle')+'生成完成';
}
if(id) {
obj.make(id, child);
} else if(obj.callback) {
setTimeout(function(){progress_bar_reset(); obj.dom.innerHTML = ''; obj.dom.style.display = 'none'; (obj.callback)();}, obj.jumptime);
}
}
});
delete x;
}
};
function progress_bar(cent) {
var dom = $('progress_bar');
if(dom) {
if(dom.style.display != 'block') {
dom.style.display = 'block';
}
var allwidth = 400;
var setwidth = allwidth * (cent / 100);
dom.style.borderLeftWidth = setwidth + 'px';
dom.style.width = (allwidth - setwidth) + 'px';
}
}
function progress_bar_reset() {
var dom = $('progress_bar');
if(dom) {
dom.style.display = 'none';
dom.style.borderLeftWidth = '1px';
dom.style.width = '400px';
}
}