Alpha Framework alpha--view--widgets
[ class tree: alpha--view--widgets ] [ index: alpha--view--widgets ] [ all elements ]

Source for file Button.inc

Documentation is available at Button.inc

  1. <?php
  2.  
  3. /**
  4.  * Button HTML custom widget
  5.  * 
  6.  * @package alpha::view::widgets
  7.  * @since 1.0
  8.  * @author John Collins <dev@alphaframework.org>
  9.  * @version $Id: Button.inc 1453 2011-12-04 15:12:54Z johnc $
  10.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  11.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  12.  *  All rights reserved.
  13.  * 
  14.  *  <pre>
  15.  *  Redistribution and use in source and binary forms, with or
  16.  *  without modification, are permitted provided that the
  17.  *  following conditions are met:
  18.  * 
  19.  *  * Redistributions of source code must retain the above
  20.  *    copyright notice, this list of conditions and the
  21.  *    following disclaimer.
  22.  *  * Redistributions in binary form must reproduce the above
  23.  *    copyright notice, this list of conditions and the
  24.  *    following disclaimer in the documentation and/or other
  25.  *    materials provided with the distribution.
  26.  *  * Neither the name of the Alpha Framework nor the names
  27.  *    of its contributors may be used to endorse or promote
  28.  *    products derived from this software without specific
  29.  *    prior written permission.
  30.  *   
  31.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  34.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  36.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  42.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.  *  </pre>
  45.  *  
  46.  */
  47. class Button {
  48.     /**
  49.      * The Javascript action to carry out when the button is pressed.
  50.      * 
  51.      * @var string 
  52.      * @since 1.0
  53.      */
  54.     private $action;
  55.     
  56.     /**
  57.      * The title to display on the button.
  58.      * 
  59.      * @var string 
  60.      * @since 1.0
  61.      */
  62.     private $title;
  63.     
  64.     /**
  65.      * The HTML id attribute for the button.
  66.      * 
  67.      * @var string 
  68.      * @since 1.0
  69.      */
  70.     private $id;
  71.     
  72.     /**
  73.      * If provided, the button will be a clickable image using this image.
  74.      * 
  75.      * @var string 
  76.      * @since 1.0
  77.      */
  78.     private $imgURL;
  79.     
  80.     /**
  81.      * The constructor
  82.      * 
  83.      * @param string $action The javascript action to be carried out (or set to "submit" to make a submit button, "file" for file uploads).
  84.      * @param string $title The title to appear on the button.
  85.      * @param string $id The HTML id attribute for the button.
  86.      * @param string $imgURL If provided, the button will be a clickable image using this image.
  87.      * @since 1.0
  88.      */
  89.     public function __construct($action$title$id$imgURL=''{
  90.         $this->action $action;
  91.         $this->title $title;
  92.         $this->id $id;
  93.         $this->imgURL $imgURL;            
  94.         $this->title $title;
  95.     }
  96.     
  97.     /**
  98.      * Renders the HTML and javascript for the button.
  99.      * 
  100.      * @param integer $width The width in pixels of the button (will also accept percentage values), defaults to 0 meaning auto-width to fit text.
  101.      * @since 1.0
  102.      * @return string 
  103.      */
  104.     public function render($width=0{
  105.         $html '';
  106.         
  107.         if(empty($this->imgURL)) {
  108.             switch ($this->action{
  109.                 case 'submit':
  110.                     $html .= '<script>
  111.                         $(function() {
  112.                             $("#'.$this->id.'").button();
  113.                         });
  114.                         </script>
  115.                     ';
  116.                     $html .= '<input type="submit" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'"'.($width == 0'':' style="width:'.$width.';"').'/>';
  117.                 break;
  118.                 case 'file':
  119.                     $html .= '<script>
  120.                         $(function() {
  121.                             $("#'.$this->id.'").button();
  122.                         });
  123.                         </script>
  124.                     ';
  125.                     $html .= '<input type="file" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'"'.($width == 0'':' style="width:'.$width.';"').'/>';
  126.                 break;
  127.                 default:
  128.                     $html .= '<script>
  129.                         $(function() {
  130.                             $("#'.$this->id.'").button();
  131.                             $("#'.$this->id.'").click(function() { '.$this->action.'; });
  132.                         });
  133.                         </script>
  134.                     ';
  135.                     $html .= '<input type="button" id="'.$this->id.'" name="'.$this->id.'" value="'.$this->title.'"'.($width == 0'':' style="width:'.$width.';"').'/>';
  136.                 break;
  137.             }
  138.         }else{
  139.             // in the special case where a clickable image is being used            
  140.             $html .= '<img src="'.$this->imgURL.'" alt="'.$this->title.'" onClick="'.$this->action.'" style="cursor:pointer; vertical-align:bottom;"/>';
  141.         }
  142.         
  143.         return $html;
  144.     }
  145.     
  146.     /**
  147.      * Returns the Javascript for injecting into <head> to control the behaviour of the buttons.
  148.      * 
  149.      * @since 1.0
  150.      * @return string 
  151.      * @deprecated Now using JQuery UI Button widget, so this custom JavaScript is no longer required
  152.      */
  153.     public static function renderJavascript({
  154.         $javascript "
  155.                         
  156.         function addButtonEvent(obj, type, fn) { 
  157.                   if (obj.attachEvent) { 
  158.                 obj['e'+type+fn] = fn; 
  159.                 obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
  160.                 obj.attachEvent('on'+type, obj[type+fn]); 
  161.               }else{
  162.                 obj.addEventListener( type, fn, false );
  163.             }
  164.         }
  165.          
  166.         function removeButtonEvent(obj, type, fn) { 
  167.               if (obj.detachEvent) { 
  168.                 obj.detachEvent('on'+type, obj[type+fn]); 
  169.                 obj[type+fn] = null;
  170.               }else{
  171.                 obj.removeEventListener( type, fn, false );
  172.             } 
  173.         }
  174.         
  175.         // onmouseover/onclick events for the side bar links
  176.         var selectedButton = null;
  177.         
  178.         function buttonOver(evt){
  179.             // first get the event
  180.             if (!evt) var evt = window.event;
  181.             // now get the target button
  182.             var button;
  183.             if (evt.target) button = evt.target;
  184.             else if (evt.srcElement) button = evt.srcElement;
  185.             if (button.nodeType == 3) // defeat Safari bug
  186.                 button = button.parentNode;
  187.             
  188.             // handles nested elements in button div tags
  189.             if (button.tagName != 'INPUT') {
  190.                 button = button.parentNode;
  191.             }
  192.                 
  193.             if (selectedButton != button){
  194.                 button.className = 'oveButton';
  195.                 button.style.cursor = 'hand';
  196.                 button.style.cursor = 'pointer';
  197.             }
  198.         }
  199.         
  200.         function buttonOut(evt){
  201.             // first get the event
  202.             if (!evt) var evt = window.event;
  203.             // now get the target button
  204.             var button;
  205.             if (evt.target) button = evt.target;
  206.             else if (evt.srcElement) button = evt.srcElement;
  207.             if (button.nodeType == 3) // defeat Safari bug
  208.                 button = button.parentNode;
  209.             
  210.             // handles nested elements in button div tags
  211.             if (button.tagName != 'INPUT') {
  212.                 button = button.parentNode;
  213.             }
  214.             
  215.             if (selectedButton != button) {
  216.                 button.className = 'norButton';
  217.             }
  218.         }
  219.         
  220.         function buttonSelect(evt){
  221.             // first get the event
  222.             if (!evt) var evt = window.event;
  223.             // now get the target button
  224.             var button;
  225.             if (evt.target) button = evt.target;
  226.             else if (evt.srcElement) button = evt.srcElement;
  227.             if (button.nodeType == 3) // defeat Safari bug
  228.                 button = button.parentNode;
  229.             
  230.             // handles nested elements in button div tags
  231.             if (button.tagName != 'INPUT') {
  232.                 button = button.parentNode;
  233.             }
  234.             
  235.             button.className = 'selButton';
  236.             button.style.cursor = 'hand';
  237.             button.style.cursor = 'pointer';
  238.             if (selectedButton != null && selectedButton != button) {
  239.                 selectedButton.className = 'norButton';
  240.             }
  241.             selectedButton = button;
  242.         }
  243.  
  244.  
  245.         function addButtonListeners() {
  246.             var no_page_inputs = document.getElementsByTagName('input').length;
  247.     
  248.             for(j=0; j<no_page_inputs; j++) {
  249.                 // add the listeners to button for highlights
  250.                 currentInput = document.getElementsByTagName('input')[j];
  251.                 if (currentInput.className == 'norButton') {
  252.                     addButtonEvent(currentInput, 'mouseover', buttonOver);
  253.                     addButtonEvent(currentInput, 'mouseout', buttonOut);
  254.                     addButtonEvent(currentInput, 'mousedown', buttonSelect);
  255.                 }
  256.             }
  257.         }
  258.         
  259.         addOnloadEvent(addButtonListeners);
  260.         ";
  261.         
  262.         return $javascript;
  263.     
  264.     
  265. }
  266.  
  267. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:33 +0000 by phpDocumentor 1.4.3