script.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. $(function(){
  2. var ul = null;
  3. $('.drop a').click(function(){
  4. // Simulate a click on the file input button
  5. // to show the file browser dialog
  6. $(this).parent().find('input').click();
  7. ul = $(this).parent().find('ul');
  8. });
  9. // Initialize the jQuery File Upload plugin
  10. $('.upload').fileupload({
  11. // This element will accept file drag/drop uploading
  12. dropZone: $('.drop'),
  13. // This function is called when a file is added to the queue;
  14. // either via the browse button, or via drag/drop:
  15. add: function (e, data) {
  16. var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
  17. ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
  18. // Append the file name and file size
  19. tpl.find('p').text('')
  20. .append('<i></i>');
  21. // Add the HTML to the UL element
  22. data.context = tpl.appendTo(ul);
  23. // Initialize the knob plugin
  24. tpl.find('input').knob();
  25. // Listen for clicks on the cancel icon
  26. tpl.find('span').click(function(){
  27. if(tpl.hasClass('working')){
  28. jqXHR.abort();
  29. }
  30. tpl.fadeOut(function(){
  31. tpl.remove();
  32. });
  33. });
  34. // Automatically upload the file once it is added to the queue
  35. var jqXHR = data.submit();
  36. },
  37. progress: function(e, data){
  38. // Calculate the completion percentage of the upload
  39. var progress = parseInt(data.loaded / data.total * 100, 10);
  40. // Update the hidden input field and trigger a change
  41. // so that the jQuery knob plugin knows to update the dial
  42. data.context.find('input').val(progress).change();
  43. if(progress == 100){
  44. data.context.removeClass('working');
  45. }
  46. },
  47. fail:function(e, data){
  48. // Something has gone wrong!
  49. data.context.addClass('error');
  50. },
  51. done: function(e,data) {
  52. $('.uploadedFile').val(data.files[0].name);
  53. }
  54. });
  55. // Prevent the default action when a file is dropped on the window
  56. $(document).on('drop dragover', function (e) {
  57. e.preventDefault();
  58. });
  59. // Helper function that formats the file sizes
  60. function formatFileSize(bytes) {
  61. if (typeof bytes !== 'number') {
  62. return '';
  63. }
  64. if (bytes >= 1000000000) {
  65. return (bytes / 1000000000).toFixed(2) + ' GB';
  66. }
  67. if (bytes >= 1000000) {
  68. return (bytes / 1000000).toFixed(2) + ' MB';
  69. }
  70. return (bytes / 1000).toFixed(2) + ' KB';
  71. }
  72. });