[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Products/ -> multifile.js (source)

   1  /**
   2   * Convert a single file-input element into a 'multiple' input list
   3   *
   4   * Usage:
   5   *
   6   *   1. Create a file input element (no name)
   7   *      eg. <input type="file" id="first_file_element">
   8   *
   9   *   2. Create a DIV for the output to be written to
  10   *      eg. <div id="files_list"></div>
  11   *
  12   *   3. Instantiate a MultiSelector object, passing in the DIV and an (optional) maximum number of files
  13   *      eg. var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
  14   *
  15   *   4. Add the first element
  16   *      eg. multi_selector.addElement( document.getElementById( 'first_file_element' ) );
  17   *
  18   *   5. That's it.
  19   *
  20   *   You might (will) want to play around with the addListRow() method to make the output prettier.
  21   *
  22   *   You might also want to change the line 
  23   *       element.name = 'file_' + this.count;
  24   *   ...to a naming convention that makes more sense to you.
  25   * 
  26   * Licence:
  27   *   Use this however/wherever you like, just don't blame me if it breaks anything.
  28   *
  29   * Credit:
  30   *   If you're nice, you'll leave this bit:
  31   *  
  32   *   Class by Stickman -- http://www.the-stickman.com
  33   *      with thanks to:
  34   *      [for Safari fixes]
  35   *         Luis Torrefranca -- http://www.law.pitt.edu
  36   *         and
  37   *         Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com
  38   *      [for duplicate name bug]
  39   *         'neal'
  40   */
  41  function MultiSelector( list_target, max ){
  42  
  43      // Where to write the list
  44      this.list_target = list_target;
  45      // How many elements?
  46      this.count = 0;
  47      // How many elements?
  48      this.id = 0;
  49      // Is there a maximum?
  50      if( max ){
  51          this.max = max;
  52      } else {
  53          this.max = -1;
  54      };
  55      
  56      /**
  57       * Add a new file input element
  58       */
  59      this.addElement = function( element ){
  60  
  61          // Make sure it's a file input element
  62          if( element.tagName == 'INPUT' && element.type == 'file' ){
  63  
  64              // Element name -- what number am I?
  65              element.name = 'file_' + this.id++;
  66  
  67              // Add reference to this object
  68              element.multi_selector = this;
  69  
  70              // What to do when a file is selected
  71              element.onchange = function(){
  72  
  73                  // New file input
  74                  var new_element = document.createElement( 'input' );
  75                  new_element.type = 'file';
  76  
  77                  // Add new element
  78                  this.parentNode.insertBefore( new_element, this );
  79  
  80                  // Apply 'update' to element
  81                  this.multi_selector.addElement( new_element );
  82  
  83                  // Update list
  84                  this.multi_selector.addListRow( this );
  85  
  86                  // Hide this: we can't use display:none because Safari doesn't like it
  87                  this.style.position = 'absolute';
  88                  this.style.left = '-1000px';
  89  
  90              };
  91              // If we've reached maximum number, disable input element
  92              if( this.max != -1 && this.count >= this.max ){
  93                  element.disabled = true;
  94              };
  95  
  96              // File element counter
  97              this.count++;
  98              // Most recent element
  99              this.current_element = element;
 100              
 101          } else {
 102              // This can only be applied to file input elements!
 103              alert( 'Error: not a file input element' );
 104          };
 105  
 106      };
 107  
 108      /**
 109       * Add a new row to the list of files
 110       */
 111      this.addListRow = function( element ){
 112  
 113          // Row div
 114          var new_row = document.createElement( 'div' );
 115  
 116          // Delete button
 117          var new_row_button = document.createElement( 'input' );
 118          new_row_button.type = 'button';
 119          new_row_button.value = 'Delete';
 120  
 121          // References
 122          new_row.element = element;
 123  
 124          // Delete function
 125          new_row_button.onclick= function(){
 126  
 127              // Remove element from form
 128              this.parentNode.element.parentNode.removeChild( this.parentNode.element );
 129  
 130              // Remove this row from the list
 131              this.parentNode.parentNode.removeChild( this.parentNode );
 132  
 133              // Decrement counter
 134              this.parentNode.element.multi_selector.count--;
 135  
 136              // Re-enable input element (if it's disabled)
 137              this.parentNode.element.multi_selector.current_element.disabled = false;
 138  
 139              // Appease Safari
 140              //    without it Safari wants to reload the browser window
 141              //    which nixes your already queued uploads
 142              return false;
 143          };
 144  
 145          // Set row value
 146          new_row.innerHTML = element.value;
 147  
 148          // Add button
 149          new_row.appendChild( new_row_button );
 150  
 151          // Add it to the list
 152          this.list_target.appendChild( new_row );
 153          
 154      };
 155  
 156  };


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7