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.

avatars.js 7.6 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Redmine - project management software
  2. Copyright (C) 2006-2013 Jean-Philippe Lang
  3. Modified by Jianfeng Tao, to upload one avatar for user*/
  4. (function(){
  5. function addFile(inputEl, file, eagerUpload) {
  6. var fileSpan = $('#avatar_file');
  7. //fileSpan.append(
  8. // $('<input>', { type: 'text', 'class': 'filename readonly', name: 'avatar[filename]', readonly: 'readonly'} ).val(file.name),
  9. // $('<a>&nbsp</a>').attr({ href: "#", 'class': 'remove-upload' }).click(removeFile).toggle(!eagerUpload)
  10. //).appendTo('#avatar_fields');
  11. if(eagerUpload) {
  12. ajaxUpload(file, fileSpan, inputEl);
  13. }
  14. return 1;
  15. }
  16. function ajaxUpload(file, fileSpan, inputEl) {
  17. function onLoadstart(e) {
  18. fileSpan.removeClass('ajax-waiting');
  19. fileSpan.addClass('ajax-loading');
  20. $('input:submit', $(this).parents('form')).attr('disabled', 'disabled');
  21. }
  22. function onProgress(e) {
  23. if(e.lengthComputable) {
  24. this.progressbar( 'value', e.loaded * 100 / e.total );
  25. }
  26. }
  27. function actualUpload(file, fileSpan, inputEl) {
  28. ajaxUpload.uploading++;
  29. uploadBlob(file, $(inputEl).data('upload-path'),$(inputEl).data('source-type'), $(inputEl).data('source-id'),$(inputEl).data('is-direct'),{
  30. loadstartEventHandler: onLoadstart.bind(progressSpan),
  31. progressEventHandler: onProgress.bind(progressSpan)
  32. })
  33. .done(function(result) {
  34. progressSpan.progressbar( 'value', 100 );
  35. progressSpan.progressbar( 'destroy');
  36. fileSpan.find('input.description, a').css('display', 'inline-block');
  37. })
  38. .fail(function(result) {
  39. progressSpan.text(result.statusText);
  40. }).always(function() {
  41. ajaxUpload.uploading--;
  42. fileSpan.removeClass('ajax-loading');
  43. var form = fileSpan.parents('form');
  44. // if (form.queue('upload').length == 0 && ajaxUpload.uploading == 0) {
  45. // $('input:submit', form).removeAttr('disabled');
  46. // }
  47. // form.dequeue('upload');
  48. });
  49. }
  50. var progressSpan = $('#upload_progressbar');
  51. progressSpan.progressbar();
  52. fileSpan.addClass('ajax-waiting');
  53. var maxSyncUpload = $(inputEl).data('max-concurrent-uploads');
  54. if(maxSyncUpload == null || maxSyncUpload <= 0 || ajaxUpload.uploading < maxSyncUpload)
  55. actualUpload(file, fileSpan, inputEl);
  56. else
  57. $(inputEl).parents('form').queue('upload', actualUpload.bind(this, file, fileSpan, inputEl));
  58. }
  59. ajaxUpload.uploading = 0;
  60. function removeFile() {
  61. $(this).parent('span').remove();
  62. return false;
  63. }
  64. function uploadBlob(blob, uploadUrl, source_type,source_id,is_direct, options) {
  65. var actualOptions = $.extend({
  66. loadstartEventHandler: $.noop,
  67. progressEventHandler: $.noop
  68. }, options);
  69. uploadUrl = uploadUrl + '?source_type=' + source_type + '&source_id=' + source_id + '&is_direct=' + is_direct;
  70. if (blob instanceof window.File) {
  71. uploadUrl += '&filename=' + encodeURIComponent(blob.name);
  72. }
  73. return $.ajax(uploadUrl, {
  74. type: 'POST',
  75. contentType: 'application/octet-stream',
  76. beforeSend: function(jqXhr) {
  77. jqXhr.setRequestHeader('Accept', 'application/js');
  78. },
  79. xhr: function() {
  80. var xhr = $.ajaxSettings.xhr();
  81. xhr.upload.onloadstart = actualOptions.loadstartEventHandler;
  82. xhr.upload.onprogress = actualOptions.progressEventHandler;
  83. return xhr;
  84. },
  85. data: blob,
  86. cache: false,
  87. processData: false
  88. });
  89. }
  90. window.addInputAvatar = function(inputEl) {
  91. // var clearedFileInput = $(inputEl).clone().val('');
  92. if (inputEl.files) {
  93. // upload files using ajax
  94. uploadAndAttachFiles(inputEl.files, inputEl);
  95. // $(inputEl).remove();
  96. } else {
  97. // browser not supporting the file API, upload on form submission
  98. var avatarId;
  99. var aFilename = inputEl.value.split(/\/|\\/);
  100. avatarId = addFile(inputEl, { name: aFilename[ aFilename.length - 1 ] }, false);
  101. if (avatarId) {
  102. $(inputEl).attr({ name: 'avatar[file]', style: 'display:none;' }).appendTo('#avatar_image');
  103. }
  104. }
  105. //clearedFileInput.insertAfter('#avatars_fields');
  106. }
  107. function uploadAndAttachFiles(files, inputEl) {
  108. var maxFileSize = $(inputEl).data('max-file-size');
  109. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  110. var sizeExceeded = false;
  111. $.each(files, function() {
  112. if (this.size && maxFileSize && this.size > parseInt(maxFileSize)) {sizeExceeded=true;}
  113. });
  114. if (sizeExceeded) {
  115. window.alert(maxFileSizeExceeded);
  116. } else {
  117. uploadAndTypeFiles(files,inputEl);
  118. //$.each(files, function() {addFile(inputEl, this, true);});
  119. }
  120. }
  121. function uploadAndTypeFiles(files, inputEl) {
  122. var enableType = $(inputEl).data('file-type');
  123. var typeSupportrdMessage = $(inputEl).data('type-support-message');
  124. if (enableType == null || enableType.trim() == "")
  125. {
  126. $.each(files, function() {addFile(inputEl, this, true);});
  127. return;
  128. }
  129. var typeSupported = false;
  130. $.each(files, function() {
  131. var a = this.name.split('.');
  132. var type = a[a.length-1];
  133. var rs = enableType.indexOf(type);
  134. if(rs >= 0) {typeSupported = true }
  135. });
  136. if (typeSupported) {
  137. $.each(files, function() {addFile(inputEl, this, true);});
  138. } else {
  139. window.alert(typeSupportrdMessage + enableType);
  140. }
  141. }
  142. function handleFileDropEvent(e) {
  143. $(this).removeClass('fileover');
  144. blockEventPropagation(e);
  145. if ($.inArray('Files', e.dataTransfer.types) > -1) {
  146. uploadAndAttachFiles(e.dataTransfer.files, $('input:file.file_selector'));
  147. }
  148. }
  149. function dragOverHandler(e) {
  150. $(this).addClass('fileover');
  151. blockEventPropagation(e);
  152. }
  153. function dragOutHandler(e) {
  154. $(this).removeClass('fileover');
  155. blockEventPropagation(e);
  156. }
  157. function setupFileDrop() {
  158. console.log("setupFileDrop");
  159. $('#avatar_image').on('click', function(){
  160. console.log("click");
  161. });
  162. if (window.File && window.FileList && window.ProgressEvent && window.FormData) {
  163. $.event.fixHooks.drop = { props: [ 'dataTransfer' ] };
  164. $('form div.box').has('input:file').each(function() {
  165. $(this).on({
  166. dragover: dragOverHandler,
  167. dragleave: dragOutHandler,
  168. drop: handleFileDropEvent
  169. });
  170. });
  171. }
  172. }
  173. $(document).ready(setupFileDrop);
  174. })();

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