upload.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * JavaScript library to deal with file uploads
  3. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  4. */
  5. /**
  6. * Upload class. Used to pack functions into one practical object.
  7. * Call like this: var myUpload = new upload(5);
  8. */
  9. function upload(latency){
  10. /**
  11. * Starts the timer
  12. * Call like this:
  13. * @param string Name of the DOM element we need to update
  14. * @param string Loading image to display
  15. * @return true
  16. */
  17. function start(domid,img,text,formid){
  18. __progress_bar_domid = domid;
  19. __progress_bar_img = img;
  20. __progress_bar_text = text;
  21. __progress_bar_interval = setTimeout(__display_progress_bar,latency,__progress_bar_text);
  22. __upload_form_domid = formid;
  23. }
  24. /**
  25. * Starts the timer of the real upload progress
  26. */
  27. function startRealUpload(domid, upload_id, formid, waitAfterUpload){
  28. __progress_bar_domid = domid;
  29. __progress_bar_uploadid = upload_id;
  30. __progress_bar_waitAfterUpload = waitAfterUpload;
  31. __upload_form_domid = formid;
  32. __progress_bar_interval = setInterval(__refreshUpload,latency);
  33. document.getElementById(domid+'_container').style.display = 'block';
  34. if(waitAfterUpload){
  35. document.getElementById(domid+'_waiter_container').style.display = 'block';
  36. }
  37. }
  38. /**
  39. * Function called by a timer to update every x seconds the progress bar
  40. */
  41. function __refreshUpload(){
  42. xajax_updateProgress(__progress_bar_domid, __progress_bar_uploadid, __progress_bar_waitAfterUpload);
  43. }
  44. /**
  45. * Displays the progress bar in the given DOM element
  46. */
  47. function __display_progress_bar(){
  48. var my_html ='<span style="font-style:italic;">'+ __progress_bar_text+'</span><br/><img src="'+__progress_bar_img+'" alt="'+__progress_bar_text+'" title="'+__progress_bar_text+'"/>';
  49. document.getElementById(__progress_bar_domid).innerHTML = my_html;
  50. if(__upload_form_domid != ''){
  51. document.getElementById(__upload_form_domid).style.display = 'none';
  52. }
  53. }
  54. this.start = start;
  55. this.startRealUpload = startRealUpload;
  56. var __progress_bar_domid = '';
  57. var __progress_bar_img = '../img/progress_bar.gif';
  58. var __progress_bar_text = 'Uploading... Please wait';
  59. var __progress_bar_interval = 1;
  60. var __upload_form_domid = '';
  61. }