script.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * FancyUpload Showcase
  3. *
  4. * @license MIT License
  5. * @author Harald Kirschner <mail [at] digitarald [dot] de>
  6. * @copyright Authors
  7. */
  8. window.addEvent('domready', function() {
  9. // One Roar instance for our notofications, positioned in the top-right corner of our demo.
  10. var log = new Roar({
  11. container: $('demo'),
  12. position: 'topRight',
  13. duration: 5000
  14. });
  15. var link = $('select-0');
  16. var linkIdle = link.get('html');
  17. function linkUpdate() {
  18. if (!swf.uploading) return;
  19. var size = Swiff.Uploader.formatUnit(swf.size, 'b');
  20. link.set('html', '<span class="small">' + swf.percentLoaded + '% of ' + size + '</span>');
  21. }
  22. // Uploader instance
  23. var swf = new Swiff.Uploader({
  24. path: '../../source/Swiff.Uploader.swf',
  25. url: '../script.php',
  26. verbose: true,
  27. queued: false,
  28. multiple: false,
  29. target: link,
  30. instantStart: true,
  31. typeFilter: {
  32. 'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
  33. },
  34. fileSizeMax: 2 * 1024 * 1024,
  35. onSelectSuccess: function(files) {
  36. if (Browser.Platform.linux) window.alert('Warning: Due to a misbehaviour of Adobe Flash Player on Linux,\nthe browser will probably freeze during the upload process.\nSince you are prepared now, the upload will start right away ...');
  37. log.alert('Starting Upload', 'Uploading <em>' + files[0].name + '</em> (' + Swiff.Uploader.formatUnit(files[0].size, 'b') + ')');
  38. this.setEnabled(false);
  39. },
  40. onSelectFail: function(files) {
  41. log.alert('<em>' + files[0].name + '</em> was not added!', 'Please select an image smaller than 2 Mb. (Error: #' + files[0].validationError + ')');
  42. },
  43. appendCookieData: true,
  44. onQueue: linkUpdate,
  45. onFileComplete: function(file) {
  46. // We *don't* save the uploaded images, we only take the md5 value and create a monsterid ;)
  47. if (file.response.error) {
  48. log.alert('Failed Upload', 'Uploading <em>' + this.fileList[0].name + '</em> failed, please try again. (Error: #' + this.fileList[0].response.code + ' ' + this.fileList[0].response.error + ')');
  49. } else {
  50. var md5 = JSON.decode(file.response.text, true).hash; // secure decode
  51. log.alert('Successful Upload', 'an MD5 hash was created from <em>' + this.fileList[0].name + '</em>: <code>' + md5 + '</code>.<br />gravatar.com generated a fancy and unique monsterid for it, since we did not save the image.');
  52. var img = $('demo-portrait');
  53. img.setStyle('background-image', img.getStyle('background-image').replace(/\w{32}/, md5));
  54. img.highlight();
  55. }
  56. file.remove();
  57. this.setEnabled(true);
  58. },
  59. onComplete: function() {
  60. link.set('html', linkIdle);
  61. }
  62. });
  63. // Button state
  64. link.addEvents({
  65. click: function() {
  66. return false;
  67. },
  68. mouseenter: function() {
  69. this.addClass('hover');
  70. swf.reposition();
  71. },
  72. mouseleave: function() {
  73. this.removeClass('hover');
  74. this.blur();
  75. },
  76. mousedown: function() {
  77. this.focus();
  78. }
  79. });
  80. });