[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libs/Html/QuickForm/ -> autocomplete.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  // +----------------------------------------------------------------------+
   4  // | PHP Version 4                                                        |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997-2003 The PHP Group                                |
   7  // +----------------------------------------------------------------------+
   8  // | This source file is subject to version 2.0 of the PHP license,       |
   9  // | that is bundled with this package in the file LICENSE, and is        |
  10  // | available at through the world-wide-web at                           |
  11  // | http://www.php.net/license/2_02.txt.                                 |
  12  // | If you did not receive a copy of the PHP license and are unable to   |
  13  // | obtain it through the world-wide-web, please send a note to          |
  14  // | license@php.net so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Author:  Matteo Di Giovinazzo <matteodg@infinito.it>                 |
  17  // |                                                                      |
  18  // | For the JavaScript code thanks to Martin Honnen and                  |
  19  // | Nicholas C. Zakas                                                    |
  20  // | See:                                                                 |
  21  // |      http://www.faqts.com/knowledge_base/view.phtml/aid/13562        |
  22  // | and                                                                  |
  23  // |      http://www.sitepoint.com/article/1220                           |
  24  // +----------------------------------------------------------------------+
  25  //
  26  // $Id: autocomplete.php,v 1.1 2005/12/06 01:50:39 matthieu_ Exp $
  27  
  28  
  29  require_once("HTML/QuickForm/text.php");
  30  
  31  
  32  /**
  33   * Class to dynamically create an HTML input text element that
  34   * at every keypressed javascript event, check in an array of options
  35   * if there's a match and autocomplete the text in case of match.
  36   *
  37   * Ex:
  38   * $autocomplete =& $form->addElement('autocomplete', 'fruit', 'Favourite fruit:');
  39   * $options = array("Apple", "Orange", "Pear", "Strawberry");
  40   * $autocomplete->setOptions($options);
  41   *
  42   * @author       Matteo Di Giovinazzo <matteodg@infinito.it>
  43   */
  44  class HTML_QuickForm_autocomplete extends HTML_QuickForm_text
  45  {
  46      // {{{ properties
  47  
  48      /**
  49       * Options for the autocomplete input text element
  50       *
  51       * @var       array
  52       * @access    private
  53       */
  54      var $_options = array();
  55  
  56      /**
  57       * "One-time" javascript (containing functions), see bug #4611
  58       *
  59       * @var     string
  60       * @access  private
  61       */
  62      var $_js = '';
  63  
  64      // }}}
  65      // {{{ constructor
  66  
  67      /**
  68       * Class constructor
  69       *
  70       * @param     string    $elementName    (optional)Input field name attribute
  71       * @param     string    $elementLabel   (optional)Input field label in form
  72       * @param     array     $options        (optional)Autocomplete options
  73       * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
  74       *                                      or an associative array. Date format is passed along the attributes.
  75       * @access    public
  76       * @return    void
  77       */
  78      function HTML_QuickForm_autocomplete($elementName = null, $elementLabel = null, $options = null, $attributes = null)
  79      {
  80          $this->HTML_QuickForm_text($elementName, $elementLabel, $attributes);
  81          $this->_persistantFreeze = true;
  82          $this->_type = 'autocomplete';
  83          if (isset($options)) {
  84              $this->setOptions($options);
  85          }
  86      } //end constructor
  87  
  88      // }}}
  89      // {{{ setOptions()
  90  
  91      /**
  92       * Sets the options for the autocomplete input text element
  93       *
  94       * @param     array    $options    Array of options for the autocomplete input text element
  95       * @access    public
  96       * @return    void
  97       */
  98      function setOptions($options)
  99      {
 100          $this->_options = array_values($options);
 101      } // end func setOptions
 102  
 103      // }}}
 104      // {{{ toHtml()
 105  
 106      /**
 107       * Returns Html for the autocomplete input text element
 108       *
 109       * @access      public
 110       * @return      string
 111       */
 112      function toHtml()
 113      {
 114          // prevent problems with grouped elements
 115          $arrayName = str_replace(array('[', ']'), array('__', ''), $this->getName()) . '_values';
 116  
 117          $this->updateAttributes(array(
 118              'onkeypress' => 'return autocomplete(this, event, ' . $arrayName . ');'
 119          ));
 120          if ($this->_flagFrozen) {
 121              $js = '';
 122          } else {
 123              $js = "<script type=\"text/javascript\">\n//<![CDATA[\n";
 124              if (!defined('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS')) {
 125                  $this->_js .= <<<EOS
 126  
 127  /* begin javascript for autocomplete */
 128  function setSelectionRange(input, selectionStart, selectionEnd) {
 129      if (input.setSelectionRange) {
 130          input.setSelectionRange(selectionStart, selectionEnd);
 131      }
 132      else if (input.createTextRange) {
 133          var range = input.createTextRange();
 134          range.collapse(true);
 135          range.moveEnd("character", selectionEnd);
 136          range.moveStart("character", selectionStart);
 137          range.select();
 138      }
 139      input.focus();
 140  }
 141  
 142  function setCaretToPosition(input, position) {
 143      setSelectionRange(input, position, position);
 144  }
 145  
 146  function replaceSelection (input, replaceString) {
 147      var len = replaceString.length;
 148      if (input.setSelectionRange) {
 149          var selectionStart = input.selectionStart;
 150          var selectionEnd = input.selectionEnd;
 151  
 152          input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
 153          input.selectionStart  = selectionStart + len;
 154          input.selectionEnd  = selectionStart + len;
 155      }
 156      else if (document.selection) {
 157          var range = document.selection.createRange();
 158          var saved_range = range.duplicate();
 159  
 160          if (range.parentElement() == input) {
 161              range.text = replaceString;
 162              range.moveEnd("character", saved_range.selectionStart + len);
 163              range.moveStart("character", saved_range.selectionStart + len);
 164              range.select();
 165          }
 166      }
 167      input.focus();
 168  }
 169  
 170  
 171  function autocompleteMatch (text, values) {
 172      for (var i = 0; i < values.length; i++) {
 173          if (values[i].toUpperCase().indexOf(text.toUpperCase()) == 0) {
 174              return values[i];
 175          }
 176      }
 177  
 178      return null;
 179  }
 180  
 181  function autocomplete(textbox, event, values) {
 182      if (textbox.setSelectionRange || textbox.createTextRange) {
 183          switch (event.keyCode) {
 184              case 38:    // up arrow
 185              case 40:    // down arrow
 186              case 37:    // left arrow
 187              case 39:    // right arrow
 188              case 33:    // page up
 189              case 34:    // page down
 190              case 36:    // home
 191              case 35:    // end
 192              case 13:    // enter
 193              case 9:     // tab
 194              case 27:    // esc
 195              case 16:    // shift
 196              case 17:    // ctrl
 197              case 18:    // alt
 198              case 20:    // caps lock
 199              case 8:     // backspace
 200              case 46:    // delete
 201                  return true;
 202                  break;
 203  
 204              default:
 205                  var c = String.fromCharCode(
 206                      (event.charCode == undefined) ? event.keyCode : event.charCode
 207                  );
 208                  replaceSelection(textbox, c);
 209                  sMatch = autocompleteMatch(textbox.value, values);
 210                  var len = textbox.value.length;
 211                  
 212                  if (sMatch != null) {
 213                      textbox.value = sMatch;
 214                      setSelectionRange(textbox, len, textbox.value.length);
 215                  }
 216                  return false;
 217          }
 218      }
 219      else {
 220          return true;
 221      }
 222  }
 223  /* end javascript for autocomplete */
 224  
 225  EOS;
 226                  define('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS', true);
 227              }
 228              $jsEscape = array(
 229                  "\r"    => '\r',
 230                  "\n"    => '\n',
 231                  "\t"    => '\t',
 232                  "'"     => "\\'",
 233                  '"'     => '\"',
 234                  '\\'    => '\\\\'
 235              );
 236  
 237              $js .= $this->_js;
 238              $js .= 'var ' . $arrayName . " = new Array();\n";
 239              for ($i = 0; $i < count($this->_options); $i++) {
 240                  $js .= $arrayName . '[' . $i . "] = '" . strtr($this->_options[$i], $jsEscape) . "';\n";
 241              }
 242              $js .= "//]]>\n</script>";
 243          }
 244          return $js . parent::toHtml();
 245      }// end func toHtml
 246  
 247      // }}}
 248  } // end class HTML_QuickForm_autocomplete
 249  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics