script.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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() { // wait for the content
  9. // our uploader instance
  10. var up = new FancyUpload2($('demo-status'), $('demo-list'), { // options object
  11. // we console.log infos, remove that in production!!
  12. verbose: true,
  13. // url is read from the form, so you just have to change one place
  14. url: $('form-demo').action,
  15. // path to the SWF file
  16. path: '../../source/Swiff.Uploader.swf',
  17. // remove that line to select all files, or edit it, add more items
  18. typeFilter: {
  19. 'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
  20. },
  21. // this is our browse button, *target* is overlayed with the Flash movie
  22. target: 'demo-browse',
  23. // graceful degradation, onLoad is only called if all went well with Flash
  24. onLoad: function() {
  25. $('demo-status').removeClass('hide'); // we show the actual UI
  26. $('demo-fallback').destroy(); // ... and hide the plain form
  27. // We relay the interactions with the overlayed flash to the link
  28. this.target.addEvents({
  29. click: function() {
  30. return false;
  31. },
  32. mouseenter: function() {
  33. this.addClass('hover');
  34. },
  35. mouseleave: function() {
  36. this.removeClass('hover');
  37. this.blur();
  38. },
  39. mousedown: function() {
  40. this.focus();
  41. }
  42. });
  43. // Interactions for the 2 other buttons
  44. $('demo-clear').addEvent('click', function() {
  45. up.remove(); // remove all files
  46. return false;
  47. });
  48. $('demo-upload').addEvent('click', function() {
  49. up.start(); // start upload
  50. return false;
  51. });
  52. },
  53. // Edit the following lines, it is your custom event handling
  54. /**
  55. * Is called when files were not added, "files" is an array of invalid File classes.
  56. *
  57. * This example creates a list of error elements directly in the file list, which
  58. * hide on click.
  59. */
  60. onSelectFail: function(files) {
  61. files.each(function(file) {
  62. new Element('li', {
  63. 'class': 'validation-error',
  64. html: file.validationErrorMessage || file.validationError,
  65. title: MooTools.lang.get('FancyUpload', 'removeTitle'),
  66. events: {
  67. click: function() {
  68. this.destroy();
  69. }
  70. }
  71. }).inject(this.list, 'top');
  72. }, this);
  73. },
  74. /**
  75. * This one was directly in FancyUpload2 before, the event makes it
  76. * easier for you, to add your own response handling (you probably want
  77. * to send something else than JSON or different items).
  78. */
  79. onFileSuccess: function(file, response) {
  80. var json = new Hash(JSON.decode(response, true) || {});
  81. if (json.get('status') == '1') {
  82. file.element.addClass('file-success');
  83. file.info.set('html', '<strong>Image was uploaded:</strong> ' + json.get('width') + ' x ' + json.get('height') + 'px, <em>' + json.get('mime') + '</em>)');
  84. } else {
  85. file.element.addClass('file-failed');
  86. file.info.set('html', '<strong>An error occured:</strong> ' + (json.get('error') ? (json.get('error') + ' #' + json.get('code')) : response));
  87. }
  88. },
  89. /**
  90. * onFail is called when the Flash movie got bashed by some browser plugin
  91. * like Adblock or Flashblock.
  92. */
  93. onFail: function(error) {
  94. switch (error) {
  95. case 'hidden': // works after enabling the movie and clicking refresh
  96. alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
  97. break;
  98. case 'blocked': // This no *full* fail, it works after the user clicks the button
  99. alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
  100. break;
  101. case 'empty': // Oh oh, wrong path
  102. alert('A required file was not found, please be patient and we fix this.');
  103. break;
  104. case 'flash': // no flash 9+ :(
  105. alert('To enable the embedded uploader, install the latest Adobe Flash plugin.')
  106. }
  107. }
  108. });
  109. });