home_uploadpic.js
5.19 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
/*
[Discuz!] (C)2001-2099 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: home_uploadpic.js 32590 2013-02-22 09:42:48Z monkey $
*/
var attachexts = new Array();
var attachwh = new Array();
var insertType = 1;
var thumbwidth = parseInt(60);
var thumbheight = parseInt(60);
var extensions = 'jpg,jpeg,gif,png';
var forms;
var nowUid = 0;
var albumid = 0;
var uploadStat = 0;
var picid = 0;
var nowid = 0;
var mainForm;
var successState = false;
function delAttach(id) {
$('attachbody').removeChild($('attach_' + id).parentNode.parentNode.parentNode);
if($('attachbody').innerHTML == '') {
addAttach();
}
$('localimgpreview_' + id + '_menu') ? document.body.removeChild($('localimgpreview_' + id + '_menu')) : null;
}
function addAttach() {
newnode = $('attachbodyhidden').rows[0].cloneNode(true);
var id = nowid;
var tags;
tags = newnode.getElementsByTagName('form');
for(i = 0;i < tags.length;i++) {
if(tags[i].id == 'upload') {
tags[i].id = 'upload_' + id;
}
}
tags = newnode.getElementsByTagName('input');
for(i = 0;i < tags.length;i++) {
if(tags[i].name == 'attach') {
tags[i].id = 'attach_' + id;
tags[i].name = 'attach';
tags[i].onchange = function() {insertAttach(id)};
tags[i].unselectable = 'on';
}
if(tags[i].id == 'albumid') {
tags[i].id = 'albumid_' + id;
}
}
tags = newnode.getElementsByTagName('span');
for(i = 0;i < tags.length;i++) {
if(tags[i].id == 'localfile') {
tags[i].id = 'localfile_' + id;
}
}
nowid++;
$('attachbody').appendChild(newnode);
}
addAttach();
function insertAttach(id) {
var localimgpreview = '';
var path = $('attach_' + id).value;
var ext = getExt(path);
var re = new RegExp("(^|\\s|,)" + ext + "($|\\s|,)", "ig");
var localfile = $('attach_' + id).value.substr($('attach_' + id).value.replace(/\\/g, '/').lastIndexOf('/') + 1);
if(path == '') {
return;
}
if(extensions != '' && (re.exec(extensions) == null || ext == '')) {
alert('对不起,不支持上传此类扩展名的图片');
return;
}
attachexts[id] = inArray(ext, ['gif', 'jpg', 'jpeg', 'png']) ? 2 : 1;
var inhtml = '<table cellspacing="0" cellpadding="0" class="up_row"><tr>';
if(typeof no_insert=='undefined') {
localfile += ' <a href="javascript:;" class="xi2" title="点击这里插入内容中当前光标的位置" onclick="insertAttachimgTag(' + id + ');return false;">[插入]</a>';
}
inhtml += '<td><strong>' + localfile +'</strong>';
inhtml += '</td><td class="d">图片描述<br/><textarea name="pic_title" cols="40" rows="2" class="pt"></textarea>';
inhtml += '</td><td class="o"><span id="showmsg' + id + '"><a href="javascript:;" onclick="delAttach(' + id + ');return false;" class="xi2">[删除]</a></span>';
inhtml += '</td></tr></table>';
$('localfile_' + id).innerHTML = inhtml;
$('attach_' + id).style.display = 'none';
addAttach();
}
function getPath(obj){
if (obj) {
if (BROWSER.ie && BROWSER.ie < 7) {
obj.select();
return document.selection.createRange().text;
} else if(BROWSER.firefox) {
if (obj.files) {
return obj.files.item(0).getAsDataURL();
}
return obj.value;
} else {
return '';
}
return obj.value;
}
}
function inArray(needle, haystack) {
if(typeof needle == 'string') {
for(var i in haystack) {
if(haystack[i] == needle) {
return true;
}
}
}
return false;
}
function insertAttachimgTag(id) {
edit_insert('[imgid=' + id + ']');
}
function uploadSubmit(obj) {
obj.disabled = true;
mainForm = obj.form;
forms = $('attachbody').getElementsByTagName("FORM");
albumid = $('uploadalbum').value;
upload();
}
function start_upload() {
$('btnupload').disabled = true;
mainForm = $('albumresultform');
forms = $('attachbody').getElementsByTagName("FORM");
upload();
}
function upload() {
if(typeof(forms[nowUid]) == 'undefined') return false;
var nid = forms[nowUid].id.split('_');
nid = nid[1];
if(nowUid>0) {
var upobj = $('showmsg'+nowid);
if(uploadStat==1) {
upobj.innerHTML = '上传成功';
successState = true;
var InputNode;
try {
var InputNode = document.createElement("<input type=\"hidden\" id=\"picid_" + picid + "\" value=\""+ nowid +"\" name=\"picids["+picid+"]\">");
} catch(e) {
var InputNode = document.createElement("input");
InputNode.setAttribute("name", "picids["+picid+"]");
InputNode.setAttribute("type", "hidden");
InputNode.setAttribute("id", "picid_" + picid);
InputNode.setAttribute("value", nowid);
}
mainForm.appendChild(InputNode);
} else {
upobj.style.color = "#f00";
upobj.innerHTML = '上传失败 '+uploadStat;
}
}
if($('showmsg'+nid) != null) {
$('showmsg'+nid).innerHTML = '上传中,请等待(<a href="javascript:;" onclick="forms[nowUid].submit();">重试</a>)';
$('albumid_'+nid).value = albumid;
forms[nowUid].submit();
} else if(nowUid+1 == forms.length) {
if(typeof (no_insert) != 'undefined') {
var albumidcheck = parseInt(parent.albumid);
$('opalbumid').value = isNaN(albumidcheck)? 0 : albumid;
if(!successState) return false;
}
window.onbeforeunload = null;
mainForm.submit();
}
nowid = nid;
nowUid++;
uploadStat = 0;
}