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 1341 2011-03-17 15:02:02Z 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 .= '<input type="submit" id="'.$this->id.'" name="'.$this->id.'" class="norButton" value="'.$this->title.'"'.($width == 0'':' style="width:'.$width.';"').'/>';
  111.                 break;
  112.                 case 'file':
  113.                     $html .= '<input type="file" id="'.$this->id.'" name="'.$this->id.'" class="norButton" value="'.$this->title.'"'.($width == 0'':' style="width:'.$width.';"').'/>';
  114.                 break;
  115.                 default:
  116.                     $html .= '<input type="button" id="'.$this->id.'" name="'.$this->id.'" class="norButton" onClick="'.$this->action.';" value="'.$this->title.'"'.($width == 0'':' style="width:'.$width.';"').'/>';
  117.                 break;
  118.             }
  119.         }else{
  120.             // in the special case where a clickable image is being used            
  121.             $html .= '<img src="'.$this->imgURL.'" alt="'.$this->title.'" onClick="'.$this->action.'" style="cursor:pointer; vertical-align:bottom;"/>';
  122.         }
  123.         
  124.         return $html;
  125.     }
  126.     
  127.     /**
  128.      * Returns the Javascript for injecting into <head> to control the behaviour of the buttons.
  129.      * 
  130.      * @since 1.0
  131.      * @return string 
  132.      */
  133.     public static function renderJavascript({
  134.         $javascript "
  135.                         
  136.         function addButtonEvent(obj, type, fn) { 
  137.                   if (obj.attachEvent) { 
  138.                 obj['e'+type+fn] = fn; 
  139.                 obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
  140.                 obj.attachEvent('on'+type, obj[type+fn]); 
  141.               }else{
  142.                 obj.addEventListener( type, fn, false );
  143.             }
  144.         }
  145.          
  146.         function removeButtonEvent(obj, type, fn) { 
  147.               if (obj.detachEvent) { 
  148.                 obj.detachEvent('on'+type, obj[type+fn]); 
  149.                 obj[type+fn] = null;
  150.               }else{
  151.                 obj.removeEventListener( type, fn, false );
  152.             } 
  153.         }
  154.         
  155.         // onmouseover/onclick events for the side bar links
  156.         var selectedButton = null;
  157.         
  158.         function buttonOver(evt){
  159.             // first get the event
  160.             if (!evt) var evt = window.event;
  161.             // now get the target button
  162.             var button;
  163.             if (evt.target) button = evt.target;
  164.             else if (evt.srcElement) button = evt.srcElement;
  165.             if (button.nodeType == 3) // defeat Safari bug
  166.                 button = button.parentNode;
  167.             
  168.             // handles nested elements in button div tags
  169.             if (button.tagName != 'INPUT') {
  170.                 button = button.parentNode;
  171.             }
  172.                 
  173.             if (selectedButton != button){
  174.                 button.className = 'oveButton';
  175.                 button.style.cursor = 'hand';
  176.                 button.style.cursor = 'pointer';
  177.             }
  178.         }
  179.         
  180.         function buttonOut(evt){
  181.             // first get the event
  182.             if (!evt) var evt = window.event;
  183.             // now get the target button
  184.             var button;
  185.             if (evt.target) button = evt.target;
  186.             else if (evt.srcElement) button = evt.srcElement;
  187.             if (button.nodeType == 3) // defeat Safari bug
  188.                 button = button.parentNode;
  189.             
  190.             // handles nested elements in button div tags
  191.             if (button.tagName != 'INPUT') {
  192.                 button = button.parentNode;
  193.             }
  194.             
  195.             if (selectedButton != button) {
  196.                 button.className = 'norButton';
  197.             }
  198.         }
  199.         
  200.         function buttonSelect(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.             button.className = 'selButton';
  216.             button.style.cursor = 'hand';
  217.             button.style.cursor = 'pointer';
  218.             if (selectedButton != null && selectedButton != button) {
  219.                 selectedButton.className = 'norButton';
  220.             }
  221.             selectedButton = button;
  222.         }
  223.  
  224.  
  225.         function addButtonListeners() {
  226.             var no_page_inputs = document.getElementsByTagName('input').length;
  227.     
  228.             for(j=0; j<no_page_inputs; j++) {
  229.                 // add the listeners to button for highlights
  230.                 currentInput = document.getElementsByTagName('input')[j];
  231.                 if (currentInput.className == 'norButton') {
  232.                     addButtonEvent(currentInput, 'mouseover', buttonOver);
  233.                     addButtonEvent(currentInput, 'mouseout', buttonOut);
  234.                     addButtonEvent(currentInput, 'mousedown', buttonSelect);
  235.                 }
  236.             }
  237.         }
  238.         
  239.         addOnloadEvent(addButtonListeners);
  240.         ";
  241.         
  242.         return $javascript;
  243.     
  244.     
  245. }
  246.  
  247. ?>

Documentation generated on Thu, 17 Mar 2011 16:43:56 +0000 by phpDocumentor 1.4.3