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

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

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