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 6.8 kB

13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
13 years ago
​
​
​
13 years ago
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Redmine - project management software
  2. Copyright (C) 2006-2013 Jean-Philippe Lang */
  3. function postUpMsg(attachmentId)
  4. {
  5. $.ajax({
  6. url: '/attachments/renderTag',
  7. type: "GET",
  8. data: {
  9. attachmentId: attachmentId
  10. }
  11. })
  12. }
  13. function addFile(inputEl, file, eagerUpload) {
  14. if ($('#attachments_fields').children().length < 10) {
  15. var attachmentId = addFile.nextAttachmentId++;
  16. var fileSpan = $('<span>', { 'id': 'attachments_' + attachmentId, 'class':'attachment' });
  17. fileSpan.append(
  18. $('<input>', { 'type': 'text', 'class': 'filename readonly', 'name': 'attachments[' + attachmentId + '][filename]', 'readonly': 'readonly'} ).val(file.name),
  19. $('<input>', { 'type': 'text', 'class': 'description', 'name': 'attachments[' + attachmentId + '][description]', 'maxlength': 255, 'placeholder': $(inputEl).data('description-placeholder') } ).toggle(!eagerUpload),
  20. $('<a>&nbsp</a>').attr({ 'href': "#", 'class': 'remove-upload', 'data-confirm' : "您确定要删除吗?" }).click(removeFile).toggle(!eagerUpload),
  21. $('<div>', { 'class': 'div_attachments', 'name': 'div_'+'attachments_' + attachmentId} )
  22. ).appendTo('#attachments_fields');
  23. if(eagerUpload) {
  24. ajaxUpload(file, attachmentId, fileSpan, inputEl);
  25. }
  26. return attachmentId;
  27. }
  28. return null;
  29. }
  30. addFile.nextAttachmentId = 1;
  31. function ajaxUpload(file, attachmentId, fileSpan, inputEl) {
  32. function onLoadstart(e) {
  33. fileSpan.removeClass('ajax-waiting');
  34. fileSpan.addClass('ajax-loading');
  35. $('input:submit', $(this).parents('form')).attr('disabled', 'disabled');
  36. }
  37. function onProgress(e) {
  38. if(e.lengthComputable) {
  39. this.progressbar( 'value', e.loaded * 100 / e.total );
  40. }
  41. }
  42. function actualUpload(file, attachmentId, fileSpan, inputEl) {
  43. ajaxUpload.uploading++;
  44. uploadBlob(file, $(inputEl).data('upload-path'), attachmentId, {
  45. loadstartEventHandler: onLoadstart.bind(progressSpan),
  46. progressEventHandler: onProgress.bind(progressSpan)
  47. })
  48. .done(function(result) {
  49. progressSpan.progressbar( 'value', 100 ).remove();
  50. fileSpan.find('input.description, a').css('display', 'inline-block');
  51. })
  52. .fail(function(result) {
  53. progressSpan.text(result.statusText);
  54. }).always(function() {
  55. ajaxUpload.uploading--;
  56. fileSpan.removeClass('ajax-loading');
  57. var form = fileSpan.parents('form');
  58. if (form.queue('upload').length == 0 && ajaxUpload.uploading == 0) {
  59. $('input:submit', form).removeAttr('disabled');
  60. }
  61. form.dequeue('upload');
  62. });
  63. //gcm files count and add delete_all link
  64. var count=$('#attachments_fields>span').length;
  65. $('#upload_file_count').html("已上传"+"<span id=\"count\">"+count+"</span>"+"个文件");
  66. if(count>=1){
  67. var add_attachs=$('.add_attachment');
  68. var delete_all=$('.remove_all');
  69. if(delete_all.length<1){
  70. add_attachs.append($("<a>&nbsp</a>").attr({"href":"javascript:void(0)", 'class': 'remove_all',"onclick": "removeAll()"}));
  71. }
  72. }
  73. //gcm
  74. }
  75. var progressSpan = $('<div>').insertAfter(fileSpan.find('input.filename'));
  76. progressSpan.progressbar();
  77. fileSpan.addClass('ajax-waiting');
  78. var maxSyncUpload = $(inputEl).data('max-concurrent-uploads');
  79. if(maxSyncUpload == null || maxSyncUpload <= 0 || ajaxUpload.uploading < maxSyncUpload)
  80. actualUpload(file, attachmentId, fileSpan, inputEl);
  81. else
  82. $(inputEl).parents('form').queue('upload', actualUpload.bind(this, file, attachmentId, fileSpan, inputEl));
  83. }
  84. ajaxUpload.uploading = 0;
  85. function removeFile() {
  86. $(this).parent('span').remove();
  87. return false;
  88. }
  89. //gcm delete all file
  90. function removeAll(){
  91. if(confirm("您确定要删除所有文件吗?")){
  92. $(".remove-upload").removeAttr("data-confirm");
  93. $(".remove-upload").click();
  94. }
  95. // return false;
  96. }
  97. //gcm
  98. function uploadBlob(blob, uploadUrl, attachmentId, options) {
  99. var actualOptions = $.extend({
  100. loadstartEventHandler: $.noop,
  101. progressEventHandler: $.noop
  102. }, options);
  103. uploadUrl = uploadUrl + '?attachment_id=' + attachmentId;
  104. if (blob instanceof window.File) {
  105. uploadUrl += '&filename=' + encodeURIComponent(blob.name);
  106. }
  107. return $.ajax(uploadUrl, {
  108. type: 'POST',
  109. contentType: 'application/octet-stream',
  110. beforeSend: function(jqXhr) {
  111. jqXhr.setRequestHeader('Accept', 'application/js');
  112. },
  113. xhr: function() {
  114. var xhr = $.ajaxSettings.xhr();
  115. xhr.upload.onloadstart = actualOptions.loadstartEventHandler;
  116. xhr.upload.onprogress = actualOptions.progressEventHandler;
  117. return xhr;
  118. },
  119. data: blob,
  120. cache: false,
  121. processData: false
  122. });
  123. }
  124. function addInputFiles(inputEl) {
  125. var clearedFileInput = $(inputEl).clone().val('');
  126. if (inputEl.files) {
  127. // upload files using ajax
  128. uploadAndAttachFiles(inputEl.files, inputEl);
  129. $(inputEl).remove();
  130. } else {
  131. // browser not supporting the file API, upload on form submission
  132. var attachmentId;
  133. var aFilename = inputEl.value.split(/\/|\\/);
  134. attachmentId = addFile(inputEl, { name: aFilename[ aFilename.length - 1 ] }, false);
  135. if (attachmentId) {
  136. $(inputEl).attr({ name: 'attachments[' + attachmentId + '][file]', style: 'display:none;' }).appendTo('#attachments_' + attachmentId);
  137. }
  138. }
  139. clearedFileInput.insertAfter('#attachments_fields');
  140. }
  141. function uploadAndAttachFiles(files, inputEl) {
  142. var maxFileSize = $(inputEl).data('max-file-size');
  143. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  144. var sizeExceeded = false;
  145. $.each(files, function() {
  146. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {sizeExceeded=true;}
  147. });
  148. if (sizeExceeded) {
  149. window.alert(maxFileSizeExceeded);
  150. } else {
  151. $.each(files, function() {addFile(inputEl, this, true);});
  152. }
  153. }
  154. function handleFileDropEvent(e) {
  155. $(this).removeClass('fileover');
  156. blockEventPropagation(e);
  157. if ($.inArray('Files', e.dataTransfer.types) > -1) {
  158. uploadAndAttachFiles(e.dataTransfer.files, $('input:file.file_selector'));
  159. }
  160. }
  161. function dragOverHandler(e) {
  162. $(this).addClass('fileover');
  163. blockEventPropagation(e);
  164. }
  165. function dragOutHandler(e) {
  166. $(this).removeClass('fileover');
  167. blockEventPropagation(e);
  168. }
  169. function setupFileDrop() {
  170. if (window.File && window.FileList && window.ProgressEvent && window.FormData) {
  171. $.event.fixHooks.drop = { props: [ 'dataTransfer' ] };
  172. $('form div.box').has('input:file').each(function() {
  173. $(this).on({
  174. dragover: dragOverHandler,
  175. dragleave: dragOutHandler,
  176. drop: handleFileDropEvent
  177. });
  178. });
  179. }
  180. }
  181. $(document).ready(setupFileDrop);

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