You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachments.js 30 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /* Redmine - project management software
  2. Copyright (C) 2006-2013 Jean-Philippe Lang */
  3. function postUpMsg(attachmentId) {
  4. $.ajax({
  5. url: '/attachments/renderTag',
  6. type: "GET",
  7. data: {
  8. attachmentId: attachmentId
  9. }
  10. })
  11. }
  12. function reload(fileSpan) {
  13. fileSpan.remove();
  14. $('#upload_file_count').html("<%= l(:label_no_file_uploaded)%>");
  15. $old_file = $("#_file");
  16. $new_file = $old_file.clone(true);
  17. $old_file.replaceWith($new_file);
  18. $new_file.show();
  19. }
  20. function addFile_board(inputEl, file, eagerUpload, id,btnId) {
  21. var attachments_frame = '#attachments_fields' + id;
  22. if ($(attachments_frame).children().length < 30) {
  23. deleteallfiles = $(inputEl).data('deleteAllFiles');
  24. var attachmentId = addFile.nextAttachmentId++;
  25. var fileSpan = $('<span>', {
  26. 'id': 'attachments_' + attachmentId,
  27. 'class': 'attachment'
  28. });
  29. fileSpan.append(
  30. $('<input>', {
  31. 'type': 'text',
  32. 'class': 'upload_filename readonly',
  33. 'name': 'attachments[' + attachmentId + '][filename]',
  34. 'readonly': 'readonly'
  35. }).val(file.name),
  36. // $('<input>', {
  37. // 'type': 'text',
  38. // 'class': 'description',
  39. // 'name': 'attachments[' + attachmentId + '][description]',
  40. // 'maxlength': 254,
  41. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  42. // }).toggle(!eagerUpload),
  43. // $('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
  44. // 'class': 'ispublic-label'
  45. // }),
  46. // $('<input>', {
  47. // 'type': 'checkbox',
  48. // 'class': 'is_public_checkbox',
  49. // 'value': 1,
  50. // 'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
  51. // checked: 'checked'
  52. // }).toggle(!eagerUpload),
  53. $('<a>&nbsp</a>').attr({
  54. 'href': "#",
  55. 'class': 'remove-upload',
  56. 'data-containerid': id
  57. }).click(function() {
  58. if (confirm($(inputEl).data('areYouSure'))) {
  59. removeFile();
  60. if (!eagerUpload) {
  61. (function(e) {
  62. reload(e);
  63. })(fileSpan);
  64. }
  65. }
  66. }).toggle(!eagerUpload),
  67. $('<div>', {
  68. 'class': 'div_attachments',
  69. 'name': 'div_' + 'attachments_' + attachmentId
  70. })
  71. ).appendTo(attachments_frame);
  72. if (eagerUpload) {
  73. ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId);
  74. }
  75. return attachmentId;
  76. }
  77. return null;
  78. }
  79. function addFile(inputEl, file, eagerUpload,btnId) {
  80. var attachments_frame = '#attachments_fields';
  81. // $("#issue_confirm").attr('href', '#');
  82. $('#issue_confirm').attr("disabled",true);
  83. $('#issue_confirm').css('background-color','#c1c1c1');
  84. $('#issue_confirm').css('cursor','default');
  85. if (true) {
  86. deleteallfiles = $(inputEl).data('deleteAllFiles');
  87. var attachmentId = addFile.nextAttachmentId++;
  88. var fileSpan = $('<span>', {
  89. 'id': 'attachments_' + attachmentId,
  90. 'class': 'attachment'
  91. });
  92. fileSpan.append(
  93. $('<input>', {
  94. 'type': 'text',
  95. 'class': 'upload_filename readonly hidden',
  96. 'name': 'attachments[' + attachmentId + '][filename]',
  97. 'readonly': 'readonly',
  98. 'style': 'border:none; max-width:270px;'
  99. }).val(file.name),
  100. // $('<input>', {
  101. // 'type': 'text',
  102. // 'class': 'description',
  103. // 'name': 'attachments[' + attachmentId + '][description]',
  104. // 'maxlength': 254,
  105. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  106. // }).toggle(!eagerUpload),
  107. // $('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
  108. // 'class': 'ispublic-label'
  109. // }),
  110. // $('<input>', {
  111. // 'type': 'checkbox',
  112. // 'class': 'is_public_checkbox',
  113. // 'value': 1,
  114. // 'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
  115. // checked: 'checked'
  116. // }).toggle(!eagerUpload),
  117. $('<a>&nbsp</a>').attr({
  118. 'href': "#",
  119. 'class': 'remove-upload',
  120. 'style': 'vertical-align:top;'
  121. }).click(function() {
  122. if (confirm($(inputEl).data('areYouSure'))) {
  123. removeFile();
  124. if (!eagerUpload) {
  125. (function(e) {
  126. reload(e);
  127. })(fileSpan);
  128. }
  129. }
  130. }).toggle(!eagerUpload),
  131. $('<div>', {
  132. 'class': 'div_attachments',
  133. 'name': 'div_' + 'attachments_' + attachmentId
  134. })
  135. ).appendTo('#attachments_fields');
  136. if (eagerUpload) {
  137. ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId);
  138. }
  139. return attachmentId;
  140. }
  141. return null;
  142. }
  143. addFile.nextAttachmentId = 1;
  144. function addReviseFile(inputEl, file, eagerUpload,btnId) {
  145. var attachments_frame = '#attachments_fields';
  146. if ($(attachments_frame).children().length < 30) {
  147. deleteallfiles = $(inputEl).data('deleteAllFiles');
  148. var attachmentId = addFile.nextAttachmentId++;
  149. var fileSpan = $('<span>', {
  150. 'id': 'attachments_' + attachmentId,
  151. 'class': 'attachment'
  152. });
  153. fileSpan.append(
  154. $('<input>', {
  155. 'type': 'text',
  156. 'class': 'upload_filename readonly',
  157. 'name': 'attachments[' + attachmentId + '][filename]',
  158. 'readonly': 'readonly'
  159. }).val(file.name),
  160. // $('<input>', {
  161. // 'type': 'text',
  162. // 'class': 'description',
  163. // 'name': 'attachments[' + attachmentId + '][description]',
  164. // 'maxlength': 254,
  165. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  166. // }).toggle(!eagerUpload),
  167. // $('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
  168. // 'class': 'ispublic-label'
  169. // }),
  170. // $('<input>', {
  171. // 'type': 'checkbox',
  172. // 'class': 'is_public_checkbox',
  173. // 'value': 1,
  174. // 'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
  175. // checked: 'checked'
  176. // }).toggle(!eagerUpload),
  177. $('<a>&nbsp</a>').attr({
  178. 'href': "#",
  179. 'class': 'remove-upload'
  180. }).click(function() {
  181. if (confirm($(inputEl).data('areYouSure'))) {
  182. removeReviseFile();
  183. if (!eagerUpload) {
  184. (function(e) {
  185. reload(e);
  186. })(fileSpan);
  187. }
  188. }
  189. }).toggle(!eagerUpload),
  190. $('<div>', {
  191. 'class': 'div_attachments',
  192. 'name': 'div_' + 'attachments_' + attachmentId
  193. })
  194. ).appendTo('#attachments_fields');
  195. $("#uploadReviseBox").addClass('disable_link');
  196. $("#choose_revise_attach").attr("onclick","return false;");
  197. if (eagerUpload) {
  198. ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId);
  199. }
  200. return attachmentId;
  201. }
  202. return null;
  203. }
  204. function ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId) {
  205. //上传开始调用函数
  206. function onLoadstart(e) {
  207. fileSpan.removeClass('ajax-waiting');
  208. fileSpan.addClass('ajax-loading');
  209. $('input:submit', $(this).parents('form')).attr('disabled', 'disabled');
  210. }
  211. //更改progressbar的值
  212. function onProgress(e) {
  213. if (e.lengthComputable) {
  214. this.progressbar('value', e.loaded * 100 / e.total);
  215. }
  216. }
  217. function actualUpload(file, attachmentId, fileSpan, inputEl,btnId) {
  218. ajaxUpload.uploading++;
  219. uploadBlob(file, $(inputEl).data('upload-path'), attachmentId, {
  220. loadstartEventHandler: onLoadstart.bind(progressSpan),
  221. progressEventHandler: onProgress.bind(progressSpan)
  222. })
  223. .done(function(result) {
  224. progressSpan.progressbar('value', 100).remove(); //上传完成就去掉该文件名后的进度条
  225. fileSpan.find('input.description, a').css('display', 'inline-block');
  226. fileSpan.find('input.is_public_checkbox, a').css('display', 'inline-block');
  227. })
  228. .fail(function(result) {
  229. progressSpan.text("网络错误");
  230. if ($("#network_issue")) { //在上传处都要给出一个network_issue的div用来显示错误
  231. $("#network_issue").show();
  232. }
  233. }).always(function() {
  234. ajaxUpload.uploading--;
  235. fileSpan.removeClass('ajax-loading');
  236. var containerid = $(inputEl).data('containerid');//多个上传控件需要的容器id
  237. if(containerid == undefined ){
  238. var count = 1;//同步上传参数为1,所以不需要去获取正在上传的文件就知道是1
  239. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('lebelFileUploding'));
  240. }
  241. else{
  242. var count = 1;
  243. $('#upload_file_count' + containerid).html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('lebelFileUploding'));
  244. }
  245. var form = fileSpan.parents('form');
  246. if (form.queue('upload').length == 0 && ajaxUpload.uploading == 0) { //所有文件上传完毕,更改状态
  247. $('input:submit', form).removeAttr('disabled');
  248. $('#' + btnId).removeAttr("disabled");
  249. $('#' + btnId).val('确定')
  250. $('#' + btnId).css('background-color', '#269ac9')
  251. if(containerid == undefined ){
  252. var count = $('#attachments_fields>span').length;
  253. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  254. }else{
  255. var count = $('#attachments_fields' + containerid + '>span').length;
  256. $('#upload_file_count'+containerid).html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  257. }
  258. }
  259. form.dequeue('upload');//上传完成一个就退出一个
  260. });
  261. //gcm files count and add delete_all link
  262. //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ start
  263. var containerid = $(inputEl).data('containerid');
  264. if (containerid == undefined) {
  265. var count = $('#attachments_fields>span').length;
  266. $('#'+btnId).attr("disabled",true);
  267. $('#'+btnId).val('提交中');
  268. $('#'+btnId).css('background-color','#c1c1c1')
  269. $('#upload_file_count').html("<span id=\"count\">1</span>" + $(inputEl).data('lebelFileUploding'));//目前肯定是穿一个文件
  270. if (count >= 1) {
  271. var add_attachs = $('.add_attachment');
  272. var delete_all = $('.remove_all');
  273. if (delete_all.length < 1) {
  274. add_attachs.append($("<a>&nbsp</a>").attr({
  275. "href": "javascript:void(0)",
  276. 'class': 'remove_all',
  277. "onclick": "removeAll()"
  278. }));
  279. }
  280. }
  281. } else {
  282. var count = $('#attachments_fields' + containerid + '>span').length;
  283. $('#'+btnId).attr("disabled",true);
  284. $('#'+btnId).val('提交中');
  285. $('#'+btnId).css('background-color','#c1c1c1')
  286. $('#upload_file_count' + containerid).html("<span id=\"count\">1</span>" + $(inputEl).data('lebelFileUploding'));//目前肯定是穿一个文件
  287. if (count >= 1) {
  288. var add_attachs = $('.add_attachment').filter(function(index) {
  289. return $(this).data('containerid') == containerid;
  290. });
  291. var delete_all = $('.remove_all').filter(function(index) {
  292. return $(this).data('containerid') == containerid;
  293. });
  294. if (delete_all.length < 1) {
  295. add_attachs.append($("<a>&nbsp</a>").attr({
  296. "href": "javascript:void(0)",
  297. 'class': 'remove_all',
  298. 'data-containerid': containerid,
  299. "onclick": "removeAll('" + containerid + "')"
  300. }));
  301. }
  302. }
  303. //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ end
  304. }
  305. //gcm
  306. }
  307. //在插入文件名的后边插入div
  308. var progressSpan = $('<div>').insertAfter(fileSpan.find('input.upload_filename'));
  309. progressSpan.progressbar(); //将div处理成progress bar
  310. fileSpan.addClass('ajax-waiting'); //添加文件正在上传的状态
  311. var containerid = $(inputEl).data('containerid');//多个上传控件需要的容器id
  312. if(containerid == undefined ){ //选择了多少个文件也要显示,因为有时候文件过大,一时半会传不完,但还是显示文件没有选择
  313. var count = $('#attachments_fields>span').length;
  314. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + '个文件被选择');
  315. }else{
  316. var count = $('#attachments_fields' + containerid + '>span').length;
  317. $('#upload_file_count'+containerid).html("<span id=\"count\">" + count + "</span>" + '个文件被选择');
  318. }
  319. //最大的同步上传参数
  320. var maxSyncUpload = 1 //$(inputEl).data('max-concurrent-uploads');
  321. //如果没有指定同步上传参数 或者参数不对,或者需要上传的文件已经小于同步上传的个数,那么直接上传
  322. if (maxSyncUpload == null || maxSyncUpload <= 0 || ajaxUpload.uploading < maxSyncUpload)
  323. actualUpload(file, attachmentId, fileSpan, inputEl,btnId);
  324. else //否则就进行队列上传
  325. $(inputEl).parents('form').queue('upload', actualUpload.bind(this, file, attachmentId, fileSpan, inputEl,btnId));
  326. }
  327. ajaxUpload.uploading = 0;
  328. function removeFile() {
  329. $(this).parent('span').remove();
  330. return false;
  331. }
  332. function removeReviseFile() {
  333. $(this).parent('span').remove();
  334. $("#uploadReviseBox").removeClass('disable_link');
  335. $("#choose_revise_attach").attr("onclick","_file.click();");
  336. return false;
  337. }
  338. //gcm delete all file
  339. //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ start
  340. function removeAll(containerid) {
  341. if (confirm(deleteallfiles)) {
  342. if (containerid == undefined) {
  343. $(".remove-upload").removeAttr("data-confirm");
  344. $(".remove-upload").click();
  345. } else {
  346. var arr = $(".remove-upload").filter(function() {
  347. return $(this).data('containerid') == containerid;
  348. });
  349. arr.removeAttr("data-confirm");
  350. arr.click();
  351. }
  352. }
  353. // return false;
  354. }
  355. //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ end
  356. //gcm
  357. function uploadBlob(blob, uploadUrl, attachmentId, options) {
  358. var actualOptions = $.extend({
  359. loadstartEventHandler: $.noop,
  360. progressEventHandler: $.noop
  361. }, options);
  362. if (uploadUrl.indexOf('?') > 0) {
  363. uploadUrl = uploadUrl + '&attachment_id=' + attachmentId;
  364. } else {
  365. uploadUrl = uploadUrl + '?attachment_id=' + attachmentId;
  366. }
  367. if (blob instanceof window.File) {
  368. uploadUrl += '&filename=' + encodeURIComponent(blob.name);
  369. }
  370. return $.ajax(uploadUrl, {
  371. type: 'POST',
  372. contentType: 'application/octet-stream',
  373. beforeSend: function(jqXhr) {
  374. jqXhr.setRequestHeader('Accept', 'application/js');
  375. },
  376. xhr: function() {
  377. var xhr = $.ajaxSettings.xhr();
  378. xhr.upload.onloadstart = actualOptions.loadstartEventHandler;
  379. xhr.upload.onprogress = actualOptions.progressEventHandler;
  380. return xhr;
  381. },
  382. data: blob,
  383. cache: false,
  384. processData: false
  385. });
  386. }
  387. function addReviseInputFiles(inputEl,btnId) {
  388. // var clearedFileInput = $(inputEl).clone().val('');
  389. if (inputEl.files) {
  390. uploadAndAttachReviseFiles(inputEl.files, inputEl,btnId);
  391. // $(inputEl).remove();
  392. } else {
  393. // browser not supporting the file API, upload on form submission
  394. var attachmentId;
  395. var aFilename = inputEl.value.split(/\/|\\/);
  396. var count = $('#attachments_fields>span').length;
  397. attachmentId = addReviseFile(inputEl, {
  398. name: aFilename[aFilename.length - 1]
  399. }, false);
  400. if (attachmentId) {
  401. $(inputEl).attr({
  402. name: 'attachments[' + attachmentId + '][file]'
  403. }).hide();
  404. if (count <= 0) count = 1;
  405. }
  406. }
  407. //clearedFileInput.insertAfter('#attachments_fields');
  408. }
  409. function addInputFiles(inputEl,btnId) {
  410. // var clearedFileInput = $(inputEl).clone().val('');
  411. if (inputEl.files) {
  412. // if(inputEl.files.length >= 5){
  413. // alert('一次选择的文件不能超过5个')
  414. // return;
  415. // }
  416. // upload files using ajax
  417. uploadAndAttachFiles(inputEl.files, inputEl,btnId);
  418. // $(inputEl).remove();
  419. } else {
  420. // browser not supporting the file API, upload on form submission
  421. var attachmentId;
  422. var aFilename = inputEl.value.split(/\/|\\/);
  423. var count = $('#attachments_fields>span').length;
  424. attachmentId = addFile(inputEl, {
  425. name: aFilename[aFilename.length - 1]
  426. }, false);
  427. if (attachmentId) {
  428. $(inputEl).attr({
  429. name: 'attachments[' + attachmentId + '][file]'
  430. }).hide();
  431. if (count <= 0) count = 1;
  432. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  433. }
  434. }
  435. //clearedFileInput.insertAfter('#attachments_fields');
  436. }
  437. function addInputFiles_board(inputEl, id,btnId) {
  438. // var clearedFileInput = $(inputEl).clone().val('');
  439. if (inputEl.files) {
  440. // if(inputEl.files.length >= 5){
  441. // alert('一次选择的文件不能超过5个')
  442. // return;
  443. // }
  444. // upload files using ajax
  445. uploadAndAttachFiles_board(inputEl.files, inputEl, id,btnId);
  446. // $(inputEl).remove();
  447. } else {
  448. // browser not supporting the file API, upload on form submission
  449. var attachmentId;
  450. var aFilename = inputEl.value.split(/\/|\\/);
  451. attachmentId = addFile_board(inputEl, {
  452. name: aFilename[aFilename.length - 1]
  453. }, false, id);
  454. if (attachmentId) {
  455. $(inputEl).attr({
  456. name: 'attachments[' + attachmentId + '][file]'
  457. }).hide();
  458. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  459. }
  460. }
  461. //clearedFileInput.insertAfter('#attachments_fields');
  462. }
  463. function uploadAndAttachReviseFiles(files, inputEl,btnId) {
  464. var maxFileSize = $(inputEl).data('max-file-size');
  465. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  466. var sizeExceeded = false;
  467. $.each(files, function() {
  468. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
  469. sizeExceeded = true;
  470. }
  471. });
  472. if (sizeExceeded) {
  473. window.alert(maxFileSizeExceeded);
  474. } else {
  475. $.each(files, function() {
  476. addReviseFile(inputEl, this, true,btnId);
  477. });
  478. }
  479. }
  480. function uploadAndAttachFiles(files, inputEl,btnId) {
  481. var maxFileSize = $(inputEl).data('max-file-size');
  482. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  483. var sizeExceeded = false;
  484. $.each(files, function() {
  485. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
  486. sizeExceeded = true;
  487. }
  488. });
  489. if (sizeExceeded) {
  490. window.alert(maxFileSizeExceeded);
  491. } else {
  492. $.each(files, function() {
  493. addFile(inputEl, this, true,btnId);
  494. });
  495. }
  496. }
  497. function uploadAndAttachFiles_board(files, inputEl, id,btnId) {
  498. var maxFileSize = $(inputEl).data('max-file-size');
  499. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  500. var sizeExceeded = false;
  501. $.each(files, function() {
  502. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
  503. sizeExceeded = true;
  504. }
  505. });
  506. if (sizeExceeded) {
  507. window.alert(maxFileSizeExceeded);
  508. } else {
  509. $.each(files, function() {
  510. addFile_board(inputEl, this, true, id,btnId);
  511. });
  512. }
  513. }
  514. function handleFileDropEvent(e) {
  515. $(this).removeClass('fileover');
  516. blockEventPropagation(e);
  517. if ($.inArray('Files', e.dataTransfer.types) > -1) {
  518. uploadAndAttachFiles(e.dataTransfer.files, $('input:file.file_selector'));
  519. }
  520. }
  521. function dragOverHandler(e) {
  522. $(this).addClass('fileover');
  523. blockEventPropagation(e);
  524. }
  525. function dragOutHandler(e) {
  526. $(this).removeClass('fileover');
  527. blockEventPropagation(e);
  528. }
  529. function setupFileDrop() {
  530. if (window.File && window.FileList && window.ProgressEvent && window.FormData) {
  531. $.event.fixHooks.drop = {
  532. props: ['dataTransfer']
  533. };
  534. $('form div.box').has('input:file').each(function() {
  535. $(this).on({
  536. dragover: dragOverHandler,
  537. dragleave: dragOutHandler,
  538. drop: handleFileDropEvent
  539. });
  540. });
  541. }
  542. }
  543. $(document).ready(setupFileDrop);
  544. $(function() {
  545. $(".file_selector").each(function() {
  546. deleteallfiles = $(this).data('deleteAllFiles');
  547. var containerid = $(this).data('containerid');
  548. if (containerid == undefined) containerid = '';
  549. var count = $('#attachments_fields' + containerid + '>span').length;
  550. if (count >= 1) {
  551. $('#upload_file_count' + containerid).html("<span id=\"count\">" + count + "</span>" + $(this).data('fileCount'));
  552. var add_attachs = $('.add_attachment').filter(function(index) {
  553. return $(this).data('containerid') == containerid;
  554. });
  555. var delete_all = $('.remove_all').filter(function(index) {
  556. return $(this).data('containerid') == containerid;
  557. });
  558. if (delete_all.length < 1) {
  559. add_attachs.append($("<a>&nbsp</a>").attr({
  560. "href": "javascript:void(0)",
  561. 'class': 'remove_all',
  562. 'data-containerid': containerid,
  563. "onclick": "removeAll('" + containerid + "')"
  564. }));
  565. }
  566. }
  567. });
  568. });
  569. //课程课件
  570. function addInputFilesCourseSource(inputEl) {
  571. checkBox = arguments[1] == 'public' ? false : true;
  572. btnId = arguments[2];
  573. // var clearedFileInput = $(inputEl).clone().val('');
  574. if (inputEl.files) {
  575. // if(inputEl.files.length >= 5){
  576. // alert('一次选择的文件不能超过5个')
  577. // return;
  578. // }
  579. // upload files using ajax
  580. uploadAndAttachFilesCourseSource(inputEl.files, inputEl,checkBox,btnId);
  581. // $(inputEl).remove();
  582. } else {
  583. // browser not supporting the file API, upload on form submission
  584. var attachmentId;
  585. var aFilename = inputEl.value.split(/\/|\\/);
  586. var count = $('#attachments_fields>span').length;
  587. attachmentId = addFile(inputEl, {
  588. name: aFilename[aFilename.length - 1]
  589. }, false);
  590. if (attachmentId) {
  591. $(inputEl).attr({
  592. name: 'attachments[' + attachmentId + '][file]'
  593. }).hide();
  594. if (count <= 0) count = 1;
  595. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  596. }
  597. }
  598. //clearedFileInput.insertAfter('#attachments_fields');
  599. }
  600. function uploadAndAttachFilesCourseSource(files, inputEl,checkBox,btnId) {
  601. var maxFileSize = $(inputEl).data('max-file-size');
  602. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  603. var sizeExceeded = false;
  604. $.each(files, function() {
  605. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
  606. sizeExceeded = true;
  607. }
  608. });
  609. if (sizeExceeded) {
  610. window.alert(maxFileSizeExceeded);
  611. } else {
  612. $.each(files, function() {
  613. addFileCourseSource(inputEl, this, true,checkBox,btnId);
  614. });
  615. }
  616. }
  617. function addFileCourseSource(inputEl, file, eagerUpload,checkBox,btnId) {
  618. var attachments_frame = '#attachments_fields';
  619. if ($(attachments_frame).children().length < 30) {
  620. deleteallfiles = $(inputEl).data('deleteAllFiles');
  621. var attachmentId = addFile.nextAttachmentId++;
  622. var fileSpan = $('<span>', {
  623. 'id': 'attachments_' + attachmentId,
  624. 'class': 'attachment'
  625. });
  626. //alert(checkBox);
  627. if(checkBox){
  628. fileSpan.append(
  629. $('<input>', {
  630. 'type': 'text',
  631. 'class': 'upload_filename readonly',
  632. 'name': 'attachments[' + attachmentId + '][filename]',
  633. 'readonly': 'readonly'
  634. }).val(file.name),
  635. // $('<input>', {
  636. // 'type': 'text',
  637. // 'class': 'description',
  638. // 'name': 'attachments[' + attachmentId + '][description]',
  639. // 'maxlength': 254,
  640. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  641. // }).toggle(!eagerUpload),
  642. $('<div>', {
  643. 'class': 'div_attachments',
  644. 'name': 'div_' + 'attachments_' + attachmentId
  645. })
  646. ).appendTo('#attachments_fields');
  647. }else {
  648. fileSpan.append(
  649. $('<input>', {
  650. 'type': 'text',
  651. 'class': 'upload_filename readonly',
  652. 'name': 'attachments[' + attachmentId + '][filename]',
  653. 'readonly': 'readonly'
  654. }).val(file.name),
  655. // $('<input>', {
  656. // 'type': 'text',
  657. // 'class': 'description',
  658. // 'name': 'attachments[' + attachmentId + '][description]',
  659. // 'maxlength': 254,
  660. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  661. // }).toggle(!eagerUpload),
  662. // $('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
  663. // 'class': 'ispublic-label'
  664. // }),
  665. // $('<input>', {
  666. // 'type': 'checkbox',
  667. // 'class': 'is_public_checkbox',
  668. // 'value': 1,
  669. // 'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
  670. // checked: 'checked'
  671. // }).toggle(!eagerUpload),
  672. $('<a>&nbsp</a>').attr({
  673. 'href': "#",
  674. 'class': 'remove-upload'
  675. }).click(function () {
  676. if (confirm($(inputEl).data('areYouSure'))) {
  677. removeFile();
  678. if (!eagerUpload) {
  679. (function (e) {
  680. reload(e);
  681. })(fileSpan);
  682. }
  683. }
  684. }).toggle(!eagerUpload),
  685. $('<div>', {
  686. 'class': 'div_attachments',
  687. 'name': 'div_' + 'attachments_' + attachmentId
  688. })
  689. ).appendTo('#attachments_fields');
  690. }
  691. if (eagerUpload) {
  692. ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId);
  693. }
  694. return attachmentId;
  695. }
  696. return null;
  697. }
  698. addFileCourseSource.nextAttachmentId = 1;

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...