[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/rsc/js/ -> bozo_validator.js (source)

   1  /**

   2   * "BOZO VALIDATOR 2" : Check if a form has been changed but not submitted when a bozo clicks

   3   * on a link which will result in potential data input loss

   4   *

   5   * Used for bozos, ask for confirmation to change the current page when he clicks on a link after having done changes on inputs forms

   6   *    without saving them

   7   *

   8   * Tested on Firefox (XP & Mac OSX) , IE6 (XP), Safari (Mac OSX)

   9   *

  10   * This file is part of the evoCore framework - {@link http://evocore.net/}

  11   * See also {@link http://sourceforge.net/projects/evocms/}.

  12   *

  13   * @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}

  14   * Parts of this file are copyright (c)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}.

  15   *

  16   * {@internal License choice

  17   * - If you have received this file as part of a package, please find the license.txt file in

  18   *   the same folder or the closest folder above for complete license terms.

  19   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)

  20   *   then you must choose one of the following licenses before using the file:

  21   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php

  22   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php

  23   * }}

  24   *

  25   * {@internal Open Source relicensing agreement:

  26   * PROGIDISTRI S.A.S. grants Francois PLANQUE the right to license

  27   * PROGIDISTRI S.A.S.'s contributions to this file and the b2evolution project

  28   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  29   * }}

  30   *

  31   * @package admin

  32   *

  33   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  34   * @author fplanque: Francois PLANQUE.

  35   * @author mbruneau: Marc BRUNEAU / PROGIDISTRI

  36   */
  37  var bozo_confirm_mess;
  38  
  39  var bozo = {
  40  
  41      // array of changes for each form we need to verify (needed to detect if another form has changed when we submit)

  42      'tab_changes' : Object(),
  43      // Total number of changes

  44      'nb_changes' : 0,
  45  
  46      // If no translated message has been provided, use this default:

  47      'confirm_mess' : bozo_confirm_mess ? bozo_confirm_mess : 'You have modified this form but you haven\'t submitted it yet.\nYou are about to lose your edits.\nAre you sure?',
  48  
  49      /**

  50       *    BOZO VALIDATOR INITIALIZATION

  51       * This is designed to track changes on forms whith an ID including '_checkchanges'

  52       */
  53      init: function ( )
  54      {    // Loop through all forms
  55          var date_deb = new Date();
  56  
  57          // Loop through all forms:

  58          for( var i = 0; i < document.forms.length ; i++ )
  59          { // Get the next form element:
  60              var el_form = document.forms[i];
  61  
  62              // add submit event on the form to control if there are changes on others forms:

  63              addEvent( el_form, 'submit', bozo.validate_submit, false );
  64  
  65              // Get all inputs for this form:

  66              all_inputs = el_form.getElementsByTagName( 'input' );
  67  
  68              if( el_form.id.indexOf( '_checkchanges' ) == -1 )
  69              {    // The form has no '_checkchanges' ID, we won't react on changes BUT we still need to react on SUBMIT...
  70                  // Loop through all form inputs:

  71                  for( var j = 0; j < all_inputs.length; j++ )
  72                  {    // Get the next input element:
  73                      var field = all_inputs[j];
  74                      if( field.type == 'submit' )
  75                      {    // The input is a submit, so we add a click event to validate_submit function
  76                          addEvent( field , 'click', bozo.validate_submit, false );
  77                      }
  78                      // TODO: handle IMAGE type

  79                  }
  80                  continue;
  81              }
  82  
  83              // Initialize this form as having no changes yet:

  84              bozo.tab_changes[el_form.id] = 0;
  85  
  86              // Loop through all form inputs:

  87              for( var j = 0; j < all_inputs.length; j++ )
  88              {    // Get the next input element:
  89                  var field = all_inputs[j];
  90  
  91                  if( field.className.indexOf( '_nocheckchanges' ) == -1  )
  92                  {    // We want to track changes on this field:
  93  
  94                      if( field.type == 'submit' )
  95                      {    // The input is a submit, so we add no event
  96                      }
  97                      // TODO: handle IMAGE type

  98                      else if( field.type == 'reset' )
  99                      {    // The input is a reset, so we add a click event to reset_changes function
 100                          addEvent( field , 'click', bozo.reset_changes, false );
 101                      }
 102                      else
 103                      {    // The input is not a submit/image/reset, so we add a change event:
 104                          addEvent( field , 'change', bozo.change, false );
 105                          addEvent( field , 'keypress', bozo.change, false );
 106                      }
 107                  }
 108              }
 109  
 110              all_textareas = el_form.getElementsByTagName( 'textarea' );
 111              // Loop on all form textareas

 112              for( var j = 0; j < all_textareas.length; j++ )
 113              {
 114                      var field = all_textareas[j];
 115                      addEvent( field , 'change', bozo.change, false );
 116                      addEvent( field , 'keypress', bozo.change, false );
 117              }
 118  
 119              all_selects = el_form.getElementsByTagName( 'select' );
 120              // Loop on all form selects

 121              for( var j = 0; j < all_selects.length; j++ )
 122              {
 123                      var field = all_selects[j];
 124                      addEvent( field , 'change', bozo.change, false );
 125              }
 126          }
 127  
 128  
 129          var date_fin = new Date();
 130          var tps = date_fin.getTime() - date_deb.getTime();;
 131          //alert( tps );

 132      },
 133  
 134  
 135      /**

 136       *    caters for the differences between Internet Explorer and fully DOM-supporting browsers

 137       */
 138      findTarget: function ( e )
 139      {
 140          var target;
 141          if (window.event && window.event.srcElement)
 142              target = window.event.srcElement;
 143          else if (e && e.target)
 144              target = e.target;
 145  
 146          if (!target)
 147              return null;
 148  
 149          return target;
 150      },
 151  
 152  
 153      /*

 154       * called when there is a change event on an element

 155       */
 156      change: function( e )
 157      {    // Get the target element
 158          var target = bozo.findTarget( e );
 159          // Update changes number for his parent form

 160          bozo.tab_changes[ get_form( target ).id ]++;
 161          // Update Total changes number

 162          bozo.nb_changes++;
 163      },
 164  
 165  
 166      /*

 167       * Call when there is a click on a reset input

 168       * Reset changes

 169       */
 170      reset_changes: function ( e )
 171      {
 172          // Loop on the forms changes array

 173          for( i in bozo.tab_changes )
 174          {    // Reset changes number to 0
 175              bozo.tab_changes[i] = 0;
 176          }
 177          // Total changes number

 178          bozo.nb_changes = 0;
 179      },
 180  
 181  
 182      /*

 183       *    Called when there is a click event on a submit button

 184       *    If there are no changes on others forms, cancel onbeforeunload event

 185       */
 186      validate_submit: function( e )
 187      {
 188              var target = bozo.findTarget(e);
 189  
 190              var other_form_changes = 0;
 191  
 192              // Loop on the forms changes array

 193              for( i in bozo.tab_changes )
 194              {
 195                  if ( ( i != get_form( target ).id ) && bozo.tab_changes[i] )
 196                  {    // Another form contains input changes
 197                      other_form_changes++;
 198                  }
 199              }
 200  
 201              if( !other_form_changes )
 202              {    // There are no changes on others forms, so cancel onbeforeunload event
 203                  window.onbeforeunload = '';
 204                  return;
 205              }
 206      },
 207  
 208  
 209  
 210      /*

 211       *    Called when the user close the window

 212       *    Ask confirmation to close page without saving changes if there have been changes on all form inputs

 213       */
 214      validate_close: function( e )
 215      {
 216          if( bozo.nb_changes )
 217          {    // there are input changes
 218              if(window.event)
 219              { // For ie:
 220                  window.event.returnValue = bozo.confirm_mess;
 221              }
 222              else
 223              { // For firefox:
 224                  //e.preventDefault();

 225                  //alert('pjl');

 226                  return bozo.confirm_mess;
 227              }
 228          }
 229    }
 230  
 231  }
 232  
 233  // Init Bozo validator when the window is loaded:

 234  addEvent( window, 'load', bozo.init, false );
 235  // The following does not seem to work with addEvent... !?

 236  // addEvent( window, 'beforeunload', bozo.validate_close, false );

 237  // addEvent( window.document, 'beforeunload', bozo.validate_close, false );

 238  window.onbeforeunload = bozo.validate_close;


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics