main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. $(function(){
  2. $(document).bind('drop dragover', function (e) {
  3. e.preventDefault();
  4. });
  5. $(document).bind('dragover', function () {
  6. $('.main').addClass('drop');
  7. })
  8. $(document).bind('dragexit dragleave dragend drop', function () {
  9. $('.main').removeClass('drop');
  10. })
  11. var fileupload = $('#fileupload').fileupload({
  12. dataType: 'json',
  13. url: 'upload.json',
  14. dropZone: $(document),
  15. pasteZone: $(document),
  16. add: add
  17. });
  18. fileupload.bind('fileuploaddone', done);
  19. fileupload.bind('fileuploadfail', fail);
  20. fileupload.bind('fileuploadprogress', progress);
  21. $(window).bind('beforeunload', function (e) {
  22. var confirmationMessage = "";
  23. if ($('.progress').not('.done').not('.fail').length > 0) {
  24. confirmationMessage = 'There are transfers in progress, navigating away will abort them.';
  25. }
  26. (e || window.event).returnValue = confirmationMessage; // Gecko + IE
  27. return confirmationMessage; // Webkit, Safari, Chrome etc.
  28. }
  29. var xhrCache = [];
  30. function add (e, data) {
  31. $('.message').hide();
  32. var xhr = data.submit();
  33. $.each(data.files, function(index, file){
  34. file._ID = xhrCache.length;
  35. xhrCache[file._ID] = xhr;
  36. var hasProgressbar = ('FormData' in window);
  37. var html = '<li data-file-id="' + file._ID + '">';
  38. html += '<div class="filename">' + file.name + '</div>';
  39. html += '<div class="progress"><div class="bar" style="width: 0%"></div>';
  40. if (!hasProgressbar) { html += '<span class="dots"></span>'; }
  41. html += '</div>';
  42. if (hasProgressbar) { html += '<div class="stop"></div>'; }
  43. html += '</li>';
  44. var tmpl = $(html);
  45. if (!hasProgressbar) {
  46. var progress = tmpl.find('.progress');
  47. var bar = progress.find('.bar');
  48. var dots = progress.find('.dots');
  49. function doting() {
  50. if (bar.css('width') != '0px') { return; }
  51. if (dots.text().length == 3) {
  52. dots.text('');
  53. }
  54. else {
  55. dots.text(dots.text() + '.');
  56. }
  57. setTimeout(doting, 750);
  58. }
  59. setTimeout(doting, 10);
  60. }
  61. $('.uploads ul').append(tmpl);
  62. });
  63. }
  64. function done (e, data) {
  65. $.each(data.files, function (index, file) {
  66. $('li[data-file-id=' + file._ID + ']').addClass('done');
  67. });
  68. }
  69. function progress (e, data) {
  70. var prog = Math.ceil(data.loaded / data.total * 100) + '%';
  71. $.each(data.files, function (index, file) {
  72. $('li[data-file-id=' + file._ID + '] .progress .bar').css('width', prog);
  73. });
  74. }
  75. function fail (e, data) {
  76. console.log('File transfer failed', data.errorThrown, data.textStatus);
  77. $.each(data.files, function (index, file) {
  78. $('li[data-file-id=' + file._ID + ']').addClass('fail');
  79. });
  80. }
  81. $('.uploads').delegate('.stop', 'click', stopTransfer);
  82. function stopTransfer (e) {
  83. var li = $(e.currentTarget).parent('li');
  84. var id = li.data('file-id');
  85. xhrCache[id].abort();
  86. li.addClass('fail');
  87. }
  88. });