main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. $(function(){
  2. function escapeTags(string) {
  3. return string
  4. .replace(/&/g, "&")
  5. .replace(/</g, "&lt;")
  6. .replace(/>/g, "&gt;")
  7. .replace(/"/g, "&quot;")
  8. .replace(/'/g, "&#039;");
  9. }
  10. $(document).bind('drop dragover', function (e) {
  11. e.preventDefault();
  12. });
  13. $(document).bind('dragover', function () {
  14. $('.main').addClass('drop');
  15. });
  16. $(document).bind('dragexit dragleave dragend drop', function () {
  17. $('.main').removeClass('drop');
  18. });
  19. var fileupload = $('#fileupload').fileupload({
  20. dataType: 'json',
  21. url: 'upload.json',
  22. dropZone: $(document),
  23. pasteZone: $(document),
  24. add: add
  25. });
  26. fileupload.bind('fileuploaddone', done);
  27. fileupload.bind('fileuploadfail', fail);
  28. fileupload.bind('fileuploadprogress', progress);
  29. $(window).bind('beforeunload', function (e) {
  30. if (xhrCache.length) {
  31. var confirmationMessage = 'There are transfers in progress, navigating away will abort them.';
  32. (e || window.event).returnValue = confirmationMessage; // Gecko + IE
  33. return confirmationMessage; // Webkit, Safari, Chrome etc.
  34. }
  35. });
  36. var xhrCache = [];
  37. var xhrNb = 0;
  38. function add (e, data) {
  39. $('.message').hide();
  40. var xhr = data.submit();
  41. $.each(data.files, function(index, file){
  42. file._ID = xhrNb++;
  43. xhrCache[file._ID] = xhr;
  44. var hasProgressbar = ('FormData' in window);
  45. var html = '<li data-file-id="' + file._ID + '">';
  46. html += '<div class="filename">' + escapeTags(file.name) + '</div>';
  47. html += '<div class="progress"><div class="bar" style="width: 0%">0%</div>';
  48. if (!hasProgressbar) { html += '<span class="dots"></span>'; }
  49. html += '</div>';
  50. if (hasProgressbar) { html += '<div class="stop"></div>'; }
  51. html += '</li>';
  52. var tmpl = $(html);
  53. if (!hasProgressbar) {
  54. var progress = tmpl.find('.progress');
  55. var bar = progress.find('.bar');
  56. var dots = progress.find('.dots');
  57. function doting() {
  58. if (bar.css('width') != '0px') { return; }
  59. if (dots.text().length == 3) {
  60. dots.text('');
  61. }
  62. else {
  63. dots.text(dots.text() + '.');
  64. }
  65. setTimeout(doting, 750);
  66. }
  67. setTimeout(doting, 10);
  68. }
  69. $('.uploads ul').append(tmpl);
  70. });
  71. }
  72. function done (e, data) {
  73. $.each(data.files, function (index, file) {
  74. xhrCache.splice(file._ID, 1);
  75. $('li[data-file-id=' + file._ID + ']').addClass('done').text(file.name + ' - 100%');
  76. });
  77. }
  78. function progress (e, data) {
  79. var prog = Math.ceil(data.loaded / data.total * 100) + '%';
  80. $.each(data.files, function (index, file) {
  81. $('li[data-file-id=' + file._ID + '] .progress .bar').css('width', prog).text(prog);
  82. });
  83. }
  84. function fail (e, data) {
  85. console.log('File transfer failed', data.errorThrown, data.textStatus);
  86. $.each(data.files, function (index, file) {
  87. xhrCache.splice(file._ID, 1);
  88. $('li[data-file-id=' + file._ID + ']').addClass('fail').text(file.name + ' - fail');
  89. });
  90. }
  91. $('.uploads').delegate('.stop', 'click', stopTransfer);
  92. function stopTransfer (e) {
  93. var li = $(e.currentTarget).parent('li');
  94. var id = li.data('file-id');
  95. xhrCache[id].abort();
  96. xhrCache.splice(id, 1);
  97. li.addClass('fail');
  98. }
  99. function resizeThumbnails() {
  100. $('.downloads > div').each(function(index, el) {
  101. $(this).height(Math.ceil($(this).width()*0.55));
  102. });
  103. }
  104. $(document).ready(function() {
  105. resizeThumbnails()
  106. });
  107. $(window).resize(function(event) {
  108. resizeThumbnails();
  109. });
  110. $('a.folder').click(function(event) {
  111. $('#overlay').addClass('shown');
  112. $('#modal ul.downloads').empty();
  113. $(this).parent().find('.content > div').clone().appendTo('#modal ul.downloads');
  114. resizeThumbnails();
  115. });
  116. $('#overlay').click(function(event) {
  117. $(this).toggleClass('shown');
  118. });
  119. if('backgroundSize' in document.documentElement.style) {
  120. $('.icon').addClass('bgz');
  121. }
  122. });