[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/image_fupload/swfupload/ -> handlers.js (source)

   1  /* $Id: handlers.js,v 1.18 2009/04/05 20:31:56 grandcat Exp $ */
   2  
   3  /* 
   4      File: handlers.js
   5      Note: This code is mainly based on the demo "SWFUpload Demos - Simple Demo" 
   6  */
   7  
   8  
   9  /* **********************
  10     Event Handlers
  11     These are my custom event handlers to make my
  12     web application behave the way I went when SWFUpload
  13     completes different tasks.  These aren't part of the SWFUpload
  14     package.  They are part of my application.  Without these none
  15     of the actions SWFUpload makes will show up in my application.
  16     ********************** */
  17     
  18  function fileQueued(file) {
  19      try {
  20          var progress = new FileProgress(file, this.customSettings.progressTarget);
  21          progress.setStatus(Drupal.t('Image queued.'));
  22          progress.toggleCancel(true, this);
  23  
  24      } catch (ex) {
  25          this.debug(ex);
  26      }
  27  
  28  }
  29  
  30  function fileQueueError(file, errorCode, message) {
  31      try {
  32          if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
  33              alert(Drupal.t("You have attempted to queue too many files.") + "\n" + (Drupal.t("You may select @files?", { '@files': Drupal.formatPlural(message, 'one file.', 'up to @count files.')})));
  34              return;
  35          }
  36  
  37          var progress = new FileProgress(file, this.customSettings.progressTarget);
  38          progress.setError();
  39          progress.toggleCancel(false);
  40  
  41          switch (errorCode) {
  42          case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
  43              progress.setStatus(Drupal.t("File is too big."));
  44              this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  45              break;
  46          case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
  47              progress.setStatus(Drupal.t("Cannot upload Zero Byte files."));
  48              this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  49              break;
  50          case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
  51              progress.setStatus(Drupal.t("Invalid File Type."));
  52              this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  53              break;
  54          default:
  55              if (file !== null) {
  56                  progress.setStatus(Drupal.t("Unhandled Error"));
  57              }
  58              this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  59              break;
  60          }
  61      } catch (ex) {
  62          this.debug(ex);
  63      }
  64  }
  65  
  66  function fileDialogComplete(numFilesSelected, numFilesQueued) {
  67      try {
  68          if (numFilesSelected > 0) {
  69              document.getElementById(this.customSettings.cancelButtonId).disabled = false;
  70        // hide info for user how to select images
  71        document.getElementById('fsUploadProgress').getElementsByTagName('span')[0].style.display = "none";
  72          }
  73          
  74          /* I want auto start the upload and I can do that here */
  75          // this.startUpload();
  76      } catch (ex)  {
  77          this.debug(ex);
  78      }
  79  }
  80  
  81  function uploadStart(file) {
  82      try {
  83          /* I don't want to do any file validation or anything,  I'll just update the UI and
  84          return true to indicate that the upload should start.
  85          It's important to update the UI here because in Linux no uploadProgress events are called. The best
  86          we can do is say we are uploading.
  87           */
  88          var progress = new FileProgress(file, this.customSettings.progressTarget);
  89          progress.setStatus(Drupal.t("Uploading..."));
  90          progress.toggleCancel(true, this);      
  91      }
  92      catch (ex) {}
  93      
  94      return true;
  95  }
  96  
  97  function uploadProgress(file, bytesLoaded, bytesTotal) {
  98      try {
  99          var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
 100  
 101          var progress = new FileProgress(file, this.customSettings.progressTarget);
 102          progress.setProgress(percent);
 103          progress.setStatus(Drupal.t("Uploading..."));
 104          
 105          // Check if there are any form errors; if true, stop everything
 106          stopOnFormerrors();
 107          
 108      } catch (ex) {
 109          this.debug(ex);
 110      }
 111  }
 112  
 113  function uploadSuccess(file, serverData) {
 114      try {
 115          var progress = new FileProgress(file, this.customSettings.progressTarget);
 116      var response = Drupal.parseJson(serverData); // parse server response
 117      if (response['status']) { // Upload successful?
 118        // good =)
 119        progress.setComplete();
 120        progress.setStatus(response['data']);
 121        progress.toggleCancel(false);
 122        // There's at least one image in image queue because one was just uploaded
 123        if (isNaN(document.getElementById('num_queued_images')))
 124            document.getElementById('num_queued_images').value = '1';        
 125        // Let's try to create a node of eventuelly uploaded images
 126        if (!jsTimer)
 127          jsTimer = window.setInterval("processQueuedImages()", 500);
 128  
 129      } else {
 130        // bad =( something went wrong
 131        count_failed_uploads += 1; // add 1 failed upload
 132        progress.setError();//progress.setComplete();
 133        progress.toggleCancel(false);
 134        progress.setStatus(response['data']);
 135      }
 136  
 137      } catch (ex) {
 138          this.debug(ex);
 139      }
 140  }
 141  
 142  function uploadError(file, errorCode, message) {
 143      try {
 144          var progress = new FileProgress(file, this.customSettings.progressTarget);
 145          progress.setError();
 146          progress.toggleCancel(false);
 147  
 148          switch (errorCode) {
 149          case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
 150              progress.setStatus(Drupal.t('Upload Error: %message', { '%message': message }));
 151              this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
 152              break;
 153          case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
 154              progress.setStatus(Drupal.t("Upload Failed."));
 155              this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
 156              break;
 157          case SWFUpload.UPLOAD_ERROR.IO_ERROR:
 158              progress.setStatus(Drupal.t("Server (IO) Error"));
 159              this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
 160              break;
 161          case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
 162              progress.setStatus(Drupal.t("Security Error"));
 163              this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
 164              break;
 165          case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
 166              progress.setStatus(Drupal.t("Upload limit exceeded."));
 167              this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
 168              break;
 169          case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
 170              progress.setStatus(Drupal.t("Failed Validation. Upload skipped."));
 171              this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
 172              break;
 173          case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
 174              // If there aren't any files left (they were all cancelled) disable the cancel button
 175              if (this.getStats().files_queued === 0) {
 176                  document.getElementById(this.customSettings.cancelButtonId).disabled = true;
 177              }
 178              progress.setStatus(Drupal.t("Cancelled"));
 179              progress.setCancelled();
 180              window.clearInterval(jsTimer);
 181              jsTimer = false;
 182              break;
 183          case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
 184              progress.setStatus(Drupal.t("Stopped"));
 185              window.clearInterval(jsTimer);
 186              jsTimer = false;
 187              break;
 188          default:
 189              progress.setStatus(Drupal.t("Unhandled Error: %code", {'%code' : errorCode}));
 190              this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
 191              break;
 192          }
 193      } catch (ex) {
 194          this.debug(ex);
 195      }
 196  }
 197  
 198  function uploadComplete(file) {
 199      if (this.getStats().files_queued === 0) {
 200          document.getElementById(this.customSettings.cancelButtonId).disabled = true;
 201      document.getElementById('startuploadbutton').disabled = false;
 202      }
 203  }
 204  
 205  // This event comes from the Queue Plugin
 206  function queueComplete(numFilesUploaded) {
 207      var status = document.getElementById("divStatus");
 208    numFilesUploaded = numFilesUploaded - count_failed_uploads; // get real number of successful uploads
 209      status.innerHTML = Drupal.formatPlural(numFilesUploaded, '1 file uploaded.', '@count files uploaded.') + ' ' + Drupal.t('Please wait until all images have been processed...');
 210      
 211    // fire up our function --> upload complete
 212    queue_complete = numFilesUploaded;   
 213  }
 214  
 215  function processQueuedImages() {
 216      try {
 217          stopOnFormerrors(); // Stop everything when a formerror response (by the server) is detected  
 218      
 219          if (isNaN(document.getElementById('num_queued_images'))) {
 220              var num_queued_images = parseInt(document.getElementById('num_queued_images').value);
 221              if (num_queued_images > 0 && (!document.getElementById('edit-node-create').disabled))
 222                  document.getElementById('edit-node-create').click();
 223              if (num_queued_images == 0) {
 224                  // no images left in queue
 225                  window.clearInterval(jsTimer);
 226                  jsTimer = false;
 227                  
 228                  if (queue_complete > 0) {
 229                    // if this was the last queued image, fire up our function to show preview button if necessary
 230                    UploadComplete(queue_complete);                  
 231                  }
 232              }          
 233          } else {
 234              // Execute at least once the image queue function to receive the hidden form element 'num_queued_images'
 235              if ((!document.getElementById('edit-node-create').disabled)) {
 236                  document.getElementById('edit-node-create').click();                
 237              }
 238          }             
 239           
 240      } catch (ex) {
 241          // Doing nothing
 242      }    
 243  }
 244  
 245  function stopOnFormerrors() {
 246      if (isNaN(document.getElementById('form_errors'))) {
 247          var formerrors = parseInt(document.getElementById('form_errors').value);
 248          if (formerrors == 1 && error_send == false) {
 249              // form error --> kill everything
 250              error_send = true;
 251              window.clearInterval(jsTimer);
 252              jsTimer = false;
 253              swfu.cancelQueue();
 254              document.getElementById('edit-delete-queue').click();
 255              
 256              // restore old form elements            
 257              setTimeout("upload_complete = false;swfu.setButtonDisabled(false);document.getElementById('startuploadbutton').value = Drupal.t('Save');document.getElementById('startuploadbutton').onclick = function() {startUploadProcess();window.location.href='#uploadform';};", 1000);
 258              setTimeout("document.getElementById('imagepreviewlistbutton').style.visibility = 'hidden';document.getElementById('divStatus').innerHTML = Drupal.t('Upload failed.'); ", 1000);                   
 259          }
 260          if (formerrors == 2 && error_send == false) {
 261            // error during upload (e.x. node file restriction)
 262            error_send = true;
 263            window.clearInterval(jsTimer);
 264            jsTimer = false;
 265            swfu.cancelQueue();
 266            document.getElementById('edit-delete-queue').click();
 267          }
 268      }
 269  }
 270  
 271  function fupload_redirect(url) {
 272    // check if updated redirect url exists
 273    if (isNaN(document.getElementById('redirect_url')))
 274      url = document.getElementById('redirect_url').value;
 275    if (url)
 276      window.location = url;
 277  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7