handlers.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. [Discuz!] (C)2001-2099 Comsenz Inc.
  3. This is NOT a freeware, use is subject to license terms
  4. $Id: handlers.js 31952 2012-10-25 09:20:40Z zhengqingpeng $
  5. */
  6. var sdCloseTime = 2;
  7. function preLoad() {
  8. if(!this.support.loading) {
  9. disableMultiUpload(this.customSettings);
  10. return false;
  11. }
  12. }
  13. function loadFailed() {
  14. disableMultiUpload(this.customSettings);
  15. }
  16. function disableMultiUpload(obj) {
  17. if(obj.uploadSource == 'forum' && obj.uploadFrom != 'fastpost') {
  18. try{
  19. obj.singleUpload.style.display = '';
  20. var dIdStr = obj.singleUpload.getAttribute("did");
  21. if(dIdStr != null) {
  22. if(typeof forum_post_inited == 'undefined') {
  23. appendscript(JSPATH + 'forum_post.js?' + VERHASH);
  24. }
  25. var idArr = dIdStr.split("|");
  26. $(idArr[0]).style.display = 'none';
  27. if(idArr[1] == 'local') {
  28. switchImagebutton('local');
  29. } else if(idArr[1] == 'upload') {
  30. switchAttachbutton('upload');
  31. }
  32. }
  33. } catch (e) {
  34. }
  35. }
  36. }
  37. function fileDialogStart() {
  38. if(this.customSettings.uploadSource == 'forum') {
  39. this.customSettings.alertType = 0;
  40. if(this.customSettings.uploadFrom == 'fastpost') {
  41. if(typeof forum_post_inited == 'undefined') {
  42. appendscript(JSPATH + 'forum_post.js?' + VERHASH);
  43. }
  44. }
  45. }
  46. }
  47. function fileQueued(file) {
  48. try {
  49. var createQueue = true;
  50. if(this.customSettings.uploadSource == 'forum' && this.customSettings.uploadType == 'poll') {
  51. var inputObj = $(this.customSettings.progressTarget+'_aid');
  52. if(inputObj && parseInt(inputObj.value)) {
  53. this.addPostParam('aid', inputObj.value);
  54. }
  55. } else if(this.customSettings.uploadSource == 'portal') {
  56. var inputObj = $('catid');
  57. if(inputObj && parseInt(inputObj.value)) {
  58. this.addPostParam('catid', inputObj.value);
  59. }
  60. }
  61. var progress = new FileProgress(file, this.customSettings.progressTarget);
  62. if(this.customSettings.uploadSource == 'forum') {
  63. if(this.customSettings.maxAttachNum != undefined) {
  64. if(this.customSettings.maxAttachNum > 0) {
  65. this.customSettings.maxAttachNum--;
  66. } else {
  67. this.customSettings.alertType = 6;
  68. createQueue = false;
  69. }
  70. }
  71. if(createQueue && this.customSettings.maxSizePerDay != undefined) {
  72. if(this.customSettings.maxSizePerDay - file.size > 0) {
  73. this.customSettings.maxSizePerDay = this.customSettings.maxSizePerDay - file.size
  74. } else {
  75. this.customSettings.alertType = 11;
  76. createQueue = false;
  77. }
  78. }
  79. if(createQueue && this.customSettings.filterType != undefined) {
  80. var fileSize = this.customSettings.filterType[file.type.substr(1).toLowerCase()];
  81. if(fileSize != undefined && fileSize && file.size > fileSize) {
  82. this.customSettings.alertType = 5;
  83. createQueue = false;
  84. }
  85. }
  86. }
  87. if(createQueue) {
  88. progress.setStatus("等待上传...");
  89. } else {
  90. this.cancelUpload(file.id);
  91. progress.setCancelled();
  92. }
  93. progress.toggleCancel(true, this);
  94. } catch (ex) {
  95. this.debug(ex);
  96. }
  97. }
  98. function fileQueueError(file, errorCode, message) {
  99. try {
  100. if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
  101. message = parseInt(message);
  102. showDialog("您选择的文件个数超过限制。\n"+(message === 0 ? "您已达到上传文件的上限了。" : "您还可以选择 " + message + " 个文件"), 'notice', null, null, 0, null, null, null, null, sdCloseTime);
  103. return;
  104. }
  105. var progress = new FileProgress(file, this.customSettings.progressTarget);
  106. progress.setError();
  107. progress.toggleCancel(false);
  108. switch (errorCode) {
  109. case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
  110. progress.setStatus("文件太大.");
  111. this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  112. break;
  113. case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
  114. progress.setStatus("不能上传零字节文件.");
  115. this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  116. break;
  117. case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
  118. progress.setStatus("禁止上传该类型的文件.");
  119. this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  120. break;
  121. case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
  122. alert("You have selected too many files. " + (message > 1 ? "You may only add " + message + " more files" : "You cannot add any more files."));
  123. break;
  124. default:
  125. if (file !== null) {
  126. progress.setStatus("Unhandled Error");
  127. }
  128. this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  129. break;
  130. }
  131. } catch (ex) {
  132. this.debug(ex);
  133. }
  134. }
  135. function fileDialogComplete(numFilesSelected, numFilesQueued) {
  136. try {
  137. if(this.customSettings.uploadSource == 'forum') {
  138. if(this.customSettings.uploadType == 'attach') {
  139. if(typeof switchAttachbutton == "function") {
  140. switchAttachbutton('attachlist');
  141. }
  142. try {
  143. if(this.getStats().files_queued) {
  144. $('attach_tblheader').style.display = '';
  145. $('attach_notice').style.display = '';
  146. }
  147. } catch (ex) {}
  148. } else if(this.customSettings.uploadType == 'image') {
  149. if(typeof switchImagebutton == "function") {
  150. switchImagebutton('imgattachlist');
  151. }
  152. try {
  153. $('imgattach_notice').style.display = '';
  154. } catch (ex) {}
  155. }
  156. var objId = this.customSettings.uploadType == 'attach' ? 'attachlist' : 'imgattachlist';
  157. var listObj = $(objId);
  158. var tableObj = listObj.getElementsByTagName("table");
  159. if(!tableObj.length) {
  160. listObj.innerHTML = "";
  161. }
  162. } else if(this.customSettings.uploadType == 'blog') {
  163. if(typeof switchImagebutton == "function") {
  164. switchImagebutton('imgattachlist');
  165. }
  166. }
  167. this.startUpload();
  168. } catch (ex) {
  169. this.debug(ex);
  170. }
  171. }
  172. function uploadStart(file) {
  173. try {
  174. this.addPostParam('filetype', file.type);
  175. if(this.customSettings.uploadSource == 'forum' && this.customSettings.uploadType == 'poll') {
  176. var preObj = $(this.customSettings.progressTarget);
  177. preObj.style.display = 'none';
  178. preObj.innerHTML = '';
  179. }
  180. var progress = new FileProgress(file, this.customSettings.progressTarget);
  181. progress.setStatus("上传中...");
  182. progress.toggleCancel(true, this);
  183. if(this.customSettings.uploadSource == 'forum') {
  184. var objId = this.customSettings.uploadType == 'attach' ? 'attachlist' : 'imgattachlist';
  185. var attachlistObj = $(objId).parentNode;
  186. attachlistObj.scrollTop = $(file.id).offsetTop - attachlistObj.clientHeight;
  187. }
  188. } catch (ex) {
  189. }
  190. return true;
  191. }
  192. function uploadProgress(file, bytesLoaded, bytesTotal) {
  193. try {
  194. var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
  195. var progress = new FileProgress(file, this.customSettings.progressTarget);
  196. progress.setStatus("正在上传("+percent+"%)...");
  197. } catch (ex) {
  198. this.debug(ex);
  199. }
  200. }
  201. function uploadSuccess(file, serverData) {
  202. try {
  203. var progress = new FileProgress(file, this.customSettings.progressTarget);
  204. if(this.customSettings.uploadSource == 'forum') {
  205. if(this.customSettings.uploadType == 'poll') {
  206. var data = eval('('+serverData+')');
  207. if(parseInt(data.aid)) {
  208. var preObj = $(this.customSettings.progressTarget);
  209. preObj.innerHTML = "";
  210. preObj.style.display = '';
  211. var img = new Image();
  212. img.src = IMGDIR + '/attachimg_2.png';//data.smallimg;
  213. var imgObj = document.createElement("img");
  214. imgObj.src = img.src;
  215. imgObj.className = "cur1";
  216. imgObj.onmouseout = function(){hideMenu('poll_img_preview_'+data.aid+'_menu');};//"hideMenu('poll_img_preview_"+data.aid+"_menu');";
  217. imgObj.onmouseover = function(){showMenu({'menuid':'poll_img_preview_'+data.aid+'_menu','ctrlclass':'a','duration':2,'timeout':0,'pos':'34'});};//"showMenu({'menuid':'poll_img_preview_"+data.aid+"_menu','ctrlclass':'a','duration':2,'timeout':0,'pos':'34'});";
  218. preObj.appendChild(imgObj);
  219. var inputObj = document.createElement("input");
  220. inputObj.type = 'hidden';
  221. inputObj.name = 'pollimage[]';
  222. inputObj.id = this.customSettings.progressTarget+'_aid';
  223. inputObj.value= data.aid;
  224. preObj.appendChild(inputObj);
  225. var preImgObj = document.createElement("span");
  226. preImgObj.style.display = 'none';
  227. preImgObj.id = 'poll_img_preview_'+data.aid+'_menu';
  228. img = new Image();
  229. img.src = data.smallimg;
  230. imgObj = document.createElement("img");
  231. imgObj.src = img.src;
  232. preImgObj.appendChild(imgObj);
  233. preObj.appendChild(preImgObj);
  234. }
  235. } else {
  236. aid = parseInt(serverData);
  237. if(aid > 0) {
  238. if(this.customSettings.uploadType == 'attach') {
  239. ajaxget('forum.php?mod=ajax&action=attachlist&aids=' + aid + (!fid ? '' : '&fid=' + fid)+(typeof resulttype == 'undefined' ? '' : '&result=simple'), file.id);
  240. } else if(this.customSettings.uploadType == 'image') {
  241. var tdObj = getInsertTdId(this.customSettings.imgBoxObj, 'image_td_'+aid);
  242. ajaxget('forum.php?mod=ajax&action=imagelist&type=single&pid=' + pid + '&aids=' + aid + (!fid ? '' : '&fid=' + fid), tdObj.id);
  243. $(file.id).style.display = 'none';
  244. }
  245. } else {
  246. aid = aid < -1 ? Math.abs(aid) : aid;
  247. if(typeof STATUSMSG[aid] == "string") {
  248. progress.setStatus(STATUSMSG[aid]);
  249. showDialog(STATUSMSG[aid], 'notice', null, null, 0, null, null, null, null, sdCloseTime);
  250. } else {
  251. progress.setStatus("取消上传");
  252. }
  253. this.cancelUpload(file.id);
  254. progress.setCancelled();
  255. progress.toggleCancel(true, this);
  256. var stats = this.getStats();
  257. var obj = {'successful_uploads':--stats.successful_uploads, 'upload_cancelled':++stats.upload_cancelled};
  258. this.setStats(obj);
  259. }
  260. }
  261. } else if(this.customSettings.uploadType == 'album') {
  262. var data = eval('('+serverData+')');
  263. if(parseInt(data.picid)) {
  264. var newTr = document.createElement("TR");
  265. var newTd = document.createElement("TD");
  266. var img = new Image();
  267. img.src = data.url;
  268. var imgObj = document.createElement("img");
  269. imgObj.src = img.src;
  270. newTd.className = 'c';
  271. newTd.appendChild(imgObj);
  272. newTr.appendChild(newTd);
  273. newTd = document.createElement("TD");
  274. newTd.innerHTML = '<strong>'+file.name+'</strong>';
  275. newTr.appendChild(newTd);
  276. newTd = document.createElement("TD");
  277. newTd.className = 'd';
  278. newTd.innerHTML = '图片描述<br/><textarea name="title['+data.picid+']" cols="40" rows="2" class="pt"></textarea>';
  279. newTr.appendChild(newTd);
  280. this.customSettings.imgBoxObj.appendChild(newTr);
  281. } else {
  282. showDialog('图片上传失败', 'notice', null, null, 0, null, null, null, null, sdCloseTime);
  283. }
  284. $(file.id).style.display = 'none';
  285. } else if(this.customSettings.uploadType == 'blog') {
  286. var data = eval('('+serverData+')');
  287. if(parseInt(data.picid)) {
  288. var tdObj = getInsertTdId(this.customSettings.imgBoxObj, 'image_td_'+data.picid);
  289. var img = new Image();
  290. img.src = data.url;
  291. var imgObj = document.createElement("img");
  292. imgObj.src = img.src;
  293. imgObj.className = "cur1";
  294. imgObj.onclick = function() {insertImage(data.bigimg);};
  295. tdObj.appendChild(imgObj);
  296. var inputObj = document.createElement("input");
  297. inputObj.type = 'hidden';
  298. inputObj.name = 'picids['+data.picid+']';
  299. inputObj.value= data.picid;
  300. tdObj.appendChild(inputObj);
  301. } else {
  302. showDialog('图片上传失败', 'notice', null, null, 0, null, null, null, null, sdCloseTime);
  303. }
  304. $(file.id).style.display = 'none';
  305. } else if(this.customSettings.uploadSource == 'portal') {
  306. var data = eval('('+serverData+')');
  307. if(data.aid) {
  308. if(this.customSettings.uploadType == 'attach') {
  309. ajaxget('portal.php?mod=attachment&op=getattach&type=attach&id=' + data.aid, file.id);
  310. if($('attach_tblheader')) {
  311. $('attach_tblheader').style.display = '';
  312. }
  313. } else {
  314. var tdObj = getInsertTdId(this.customSettings.imgBoxObj, 'attach_list_'+data.aid);
  315. ajaxget('portal.php?mod=attachment&op=getattach&id=' + data.aid, tdObj.id);
  316. $(file.id).style.display = 'none';
  317. }
  318. } else {
  319. showDialog('上传失败', 'notice', null, null, 0, null, null, null, null, sdCloseTime);
  320. progress.setStatus("Cancelled");
  321. this.cancelUpload(file.id);
  322. progress.setCancelled();
  323. progress.toggleCancel(true, this);
  324. }
  325. } else {
  326. progress.setComplete();
  327. progress.setStatus("上传完成.");
  328. progress.toggleCancel(false);
  329. }
  330. } catch (ex) {
  331. this.debug(ex);
  332. }
  333. }
  334. function getInsertTdId(boxObj, tdId) {
  335. var tableObj = boxObj.getElementsByTagName("table");
  336. var tbodyObj, trObj, tdObj;
  337. if(!tableObj.length) {
  338. tableObj = document.createElement("table");
  339. tableObj.className = "imgl";
  340. tbodyObj = document.createElement("TBODY");
  341. tableObj.appendChild(tbodyObj);
  342. boxObj.appendChild(tableObj);
  343. } else if(!tableObj[0].getElementsByTagName("tbody").length) {
  344. tbodyObj = document.createElement("TBODY");
  345. tableObj.appendChild(tbodyObj);
  346. } else {
  347. tableObj = tableObj[0];
  348. tbodyObj = tableObj.getElementsByTagName("tbody")[0];
  349. }
  350. var createTr = true;
  351. var inserID = 0;
  352. if(tbodyObj.childNodes.length) {
  353. trObj = tbodyObj.childNodes[tbodyObj.childNodes.length -1];
  354. var findObj = trObj.getElementsByTagName("TD");
  355. for(var j=0; j < findObj.length; j++) {
  356. if(findObj[j].id == "") {
  357. inserID = j;
  358. tdObj = findObj[j];
  359. break;
  360. }
  361. }
  362. if(inserID) {
  363. createTr = false;
  364. }
  365. }
  366. if(createTr) {
  367. trObj = document.createElement("TR");
  368. for(var i=0; i < 4; i++) {
  369. var newTd = document.createElement("TD");
  370. newTd.width = "25%";
  371. newTd.vAlign = "bottom";
  372. newTd.appendChild(document.createTextNode(" "));
  373. trObj.appendChild(newTd);
  374. }
  375. tdObj = trObj.childNodes[0];
  376. tbodyObj.appendChild(trObj);
  377. }
  378. tdObj.id = tdId;
  379. return tdObj;
  380. }
  381. function uploadComplete(file) {
  382. try {
  383. if (this.getStats().files_queued === 0) {
  384. } else {
  385. this.startUpload();
  386. }
  387. } catch (ex) {
  388. this.debug(ex);
  389. }
  390. }
  391. function uploadError(file, errorCode, message) {
  392. try {
  393. var progress = new FileProgress(file, this.customSettings.progressTarget);
  394. progress.setError();
  395. progress.toggleCancel(false);
  396. switch (errorCode) {
  397. case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
  398. progress.setStatus("Upload Error: " + message);
  399. this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
  400. break;
  401. case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
  402. progress.setStatus("Configuration Error");
  403. this.debug("Error Code: No backend file, File name: " + file.name + ", Message: " + message);
  404. break;
  405. case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
  406. progress.setStatus("Upload Failed.");
  407. this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  408. break;
  409. case SWFUpload.UPLOAD_ERROR.IO_ERROR:
  410. progress.setStatus("Server (IO) Error");
  411. this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
  412. break;
  413. case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
  414. progress.setStatus("Security Error");
  415. this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
  416. break;
  417. case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
  418. progress.setStatus("Upload limit exceeded.");
  419. this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  420. break;
  421. case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
  422. progress.setStatus("File not found.");
  423. this.debug("Error Code: The file was not found, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  424. break;
  425. case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
  426. progress.setStatus("Failed Validation. Upload skipped.");
  427. this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  428. break;
  429. case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
  430. if (this.getStats().files_queued === 0) {
  431. }
  432. progress.setStatus(this.customSettings.alertType ? STATUSMSG[this.customSettings.alertType] : "Cancelled");
  433. progress.setCancelled();
  434. break;
  435. case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
  436. progress.setStatus("Stopped");
  437. break;
  438. default:
  439. progress.setStatus("Unhandled Error: " + error_code);
  440. this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  441. break;
  442. }
  443. } catch (ex) {
  444. this.debug(ex);
  445. }
  446. }