[ Index ]
 

Code source de PRADO 3.0.6

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

title

Body

[fermer]

/framework/Web/UI/ -> TClientScriptManager.php (source)

   1  <?php
   2  /**
   3   * TClientScriptManager class file
   4   *
   5   * @author Qiang Xue <qiang.xue@gmail.com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2005 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TClientScriptManager.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.Web.UI
  11   */
  12  
  13  /**
  14   * TClientScriptManager class.
  15   *
  16   * TClientScriptManager manages javascript and CSS stylesheets for a page.
  17   *
  18   * @author Qiang Xue <qiang.xue@gmail.com>
  19   * @version $Id: TClientScriptManager.php 1397 2006-09-07 07:55:53Z wei $
  20   * @package System.Web.UI
  21   * @since 3.0
  22   */
  23  class TClientScriptManager extends TApplicationComponent
  24  {
  25      /**
  26       * directory containing Prado javascript files
  27       */
  28      const SCRIPT_PATH='Web/Javascripts/js';
  29      /**
  30       * the PHP script for loading Prado javascript files
  31       */
  32      const SCRIPT_LOADER='clientscripts.php';
  33  
  34      /**
  35       * @var TPage page who owns this manager
  36       */
  37      private $_page;
  38      /**
  39       * @var array registered hidden fields, indexed by hidden field names
  40       */
  41      private $_hiddenFields=array();
  42      /**
  43       * @var array javascript blocks to be rendered at the beginning of the form
  44       */
  45      private $_beginScripts=array();
  46      /**
  47       * @var array javascript blocks to be rendered at the end of the form
  48       */
  49      private $_endScripts=array();
  50      /**
  51       * @var array javascript files to be rendered in the form
  52       */
  53      private $_scriptFiles=array();
  54      /**
  55       * @var array javascript files to be renderd in page head section
  56       */
  57      private $_headScriptFiles=array();
  58      /**
  59       * @var array javascript blocks to be renderd in page head section
  60       */
  61      private $_headScripts=array();
  62      /**
  63       * @var array CSS files
  64       */
  65      private $_styleSheetFiles=array();
  66      /**
  67       * @var array CSS declarations
  68       */
  69      private $_styleSheets=array();
  70      /**
  71       * @var array registered PRADO script libraries
  72       */
  73      private $_registeredPradoScripts=array();
  74      /**
  75       * @var array registered PRADO script files
  76       */
  77      private $_registeredPradoFiles=array();
  78      /**
  79       * Client-side javascript library dependencies
  80       * @var array
  81       */
  82      private static $_pradoScripts=array(
  83          'prado'            => array('prado'),
  84          'effects'        => array('prado', 'effects'),
  85          'ajax'            => array('prado', 'effects', 'ajax'),
  86          'validator'        => array('prado', 'validator'),
  87          'logger'        => array('prado', 'logger'),
  88          'datepicker'    => array('prado', 'datepicker'),
  89          'rico'            => array('prado', 'effects', 'ajax', 'rico'),
  90          'colorpicker'    => array('prado', 'colorpicker')
  91          );
  92  
  93      /**
  94       * Constructor.
  95       * @param TPage page that owns this client script manager
  96       */
  97  	public function __construct(TPage $owner)
  98      {
  99          $this->_page=$owner;
 100      }
 101  
 102      /**
 103       * Registers Prado scripts by library name.
 104       * Each library may include one or several script files.
 105       * Currently, the following libraries are available:
 106       * - prado : basic prado js framework
 107       * - effects :
 108       * - ajax : ajax related js
 109       * - validator : validator js
 110       * - logger : js logger
 111       * - datepicker : datepicker js
 112       * - rico :
 113       * - colorpicker : colorpicker js
 114       * The script files registered will be published.
 115       * @param string script library name.
 116       */
 117  	public function registerPradoScript($name)
 118      {
 119          if(!isset($this->_registeredPradoScripts[$name]))
 120          {
 121              $this->_registeredPradoScripts[$name]=true;
 122              if(!isset(self::$_pradoScripts[$name]))
 123                  throw new TInvalidOperationException('csmanager_pradoscript_invalid',$name);
 124              $basePath=$this->getPradoScriptBasePath();
 125              foreach(self::$_pradoScripts[$name] as $script)
 126              {
 127                  if(!isset($this->_registeredPradoFiles[$script]))
 128                  {
 129                      $this->publishFilePath($basePath.'/'.$script.'.js');
 130                      $this->_registeredPradoFiles[$script]=false;
 131                  }
 132              }
 133          }
 134      }
 135  
 136      /**
 137       * @return string the directory containing the PRADO js script files
 138       */
 139  	protected function getPradoScriptBasePath()
 140      {
 141          $basePath = Prado::getFrameworkPath().'/'.self::SCRIPT_PATH;
 142          if($this->getApplication()->getMode()===TApplicationMode::Debug)
 143              return $basePath.'/debug';
 144          else
 145              return $basePath.'/compressed';
 146      }
 147  
 148      /**
 149       * Renders the HTML tags for PRADO js files
 150       * @param THtmlWriter writer
 151       */
 152  	protected function renderPradoScripts($writer)
 153      {
 154          $files='';
 155          foreach($this->_registeredPradoFiles as $file=>$rendered)
 156          {
 157              if(!$rendered)
 158              {
 159                  $files.=','.$file;
 160                  $this->_registeredPradoFiles[$file]=true;
 161              }
 162          }
 163          if($files!=='')
 164          {
 165              $basePath=$this->getPradoScriptBasePath();
 166              $scriptLoader=$basePath.'/'.self::SCRIPT_LOADER;
 167              $url=$this->publishFilePath($scriptLoader).'?js='.trim($files,',');
 168              if($this->getApplication()->getMode()===TApplicationMode::Debug)
 169                  $url.='&amp;mode=debug';
 170              $writer->write(TJavaScript::renderScriptFile($url));
 171          }
 172      }
 173  
 174      /**
 175       * Registers postback javascript for a control.
 176       * @param string javascript class responsible for the control being registered for postback
 177       * @param array postback options
 178       */
 179  	public function registerPostBackControl($jsClass,$options)
 180      {
 181          if(!isset($options['FormID']))
 182              $options['FormID']=$this->_page->getForm()->getClientID();
 183          $optionString=TJavaScript::encode($options);
 184          $code="new $jsClass($optionString);";
 185          $this->registerEndScript(sprintf('%08X', crc32($code)), $code);
 186  
 187          $this->registerHiddenField(TPage::FIELD_POSTBACK_TARGET,'');
 188          $this->registerHiddenField(TPage::FIELD_POSTBACK_PARAMETER,'');
 189  
 190          $this->registerPradoScript('prado');
 191      }
 192  
 193      /**
 194       * Register a default button to panel. When the $panel is in focus and
 195       * the 'enter' key is pressed, the $button will be clicked.
 196       * @param TControl panel to register the default button action
 197       * @param TControl button to trigger a postback
 198       */
 199  	public function registerDefaultButton($panel, $button)
 200      {
 201          $options = TJavaScript::encode($this->getDefaultButtonOptions($panel, $button));
 202          $code = "new Prado.WebUI.DefaultButton($options);";
 203          $this->registerEndScript("prado:".$panel->getClientID(), $code);
 204          $this->registerHiddenField(TPage::FIELD_POSTBACK_TARGET,'');
 205          $this->registerPradoScript('prado');
 206      }
 207  
 208      /**
 209       * Registers the control to receive default focus.
 210       * @param TControl|string the control or the client ID of the HTML element to receive default focus
 211       */
 212  	public function registerFocusControl($target)
 213      {
 214          $this->registerPradoScript('effects');
 215          if($target instanceof TControl)
 216              $target=$target->getClientID();
 217          $id = TJavaScript::quoteString($target);
 218          $script = 'new Effect.ScrollTo("'.$id.'"); Prado.Element.focus("'.$id.'");';
 219          $this->registerEndScript('prado:focus',$script);
 220      }
 221  
 222      /**
 223       * @return array default button options.
 224       */
 225  	protected function getDefaultButtonOptions($panel, $button)
 226      {
 227          $options['Panel'] = $panel->getClientID();
 228          $options['Target'] = $button->getClientID();
 229          $options['EventTarget'] = $button->getUniqueID();
 230          $options['Event'] = 'click';
 231          return $options;
 232      }
 233  
 234      /**
 235       * Registers a CSS file to be rendered in the page head
 236       * @param string a unique key identifying the file
 237       * @param string URL to the CSS file
 238       * @param string media type of the CSS (such as 'print', 'screen', etc.). Defaults to empty, meaning the CSS applies to all media types.
 239       */
 240  	public function registerStyleSheetFile($key,$url,$media='')
 241      {
 242          if($media==='')
 243              $this->_styleSheetFiles[$key]=$url;
 244          else
 245              $this->_styleSheetFiles[$key]=array($url,$media);
 246      }
 247  
 248      /**
 249       * Registers a CSS block to be rendered in the page head
 250       * @param string a unique key identifying the CSS block
 251       * @param string CSS block
 252       */
 253  	public function registerStyleSheet($key,$css,$media='')
 254      {
 255          $this->_styleSheets[$key]=$css;
 256      }
 257  
 258      /**
 259       * Registers a javascript file in the page head
 260       * @param string a unique key identifying the file
 261       * @param string URL to the javascript file
 262       */
 263  	public function registerHeadScriptFile($key,$url)
 264      {
 265          $this->_headScriptFiles[$key]=$url;
 266      }
 267  
 268      /**
 269       * Registers a javascript block in the page head.
 270       * @param string a unique key identifying the script block
 271       * @param string javascript block
 272       */
 273  	public function registerHeadScript($key,$script)
 274      {
 275          $this->_headScripts[$key]=$script;
 276      }
 277  
 278      /**
 279       * Registers a javascript file to be rendered within the form
 280       * @param string a unique key identifying the file
 281       * @param string URL to the javascript file to be rendered
 282       */
 283  	public function registerScriptFile($key,$url)
 284      {
 285          if(!isset($this->_scriptFiles[$key]))
 286              $this->_scriptFiles[$key]=$url;
 287      }
 288  
 289      /**
 290       * Registers a javascript script block at the beginning of the form
 291       * @param string a unique key identifying the script block
 292       * @param string javascript block
 293       */
 294  	public function registerBeginScript($key,$script)
 295      {
 296          $this->_beginScripts[$key]=$script;
 297      }
 298  
 299      /**
 300       * Registers a javascript script block at the end of the form
 301       * @param string a unique key identifying the script block
 302       * @param string javascript block
 303       */
 304  	public function registerEndScript($key,$script)
 305      {
 306          $this->_endScripts[$key]=$script;
 307      }
 308  
 309      /**
 310       * Registers a hidden field to be rendered in the form.
 311       * @param string a unique key identifying the hidden field
 312       * @param string|array hidden field value, if the value is an array, every element
 313       * in the array will be rendered as a hidden field value.
 314       */
 315  	public function registerHiddenField($name,$value)
 316      {
 317          if(!isset($this->_hiddenFields[$name]))
 318              $this->_hiddenFields[$name]=$value;
 319      }
 320  
 321      /**
 322       * @param string a unique key
 323       * @return boolean whether there is a CSS file registered with the specified key
 324       */
 325  	public function isStyleSheetFileRegistered($key)
 326      {
 327          return isset($this->_styleSheetFiles[$key]);
 328      }
 329  
 330      /**
 331       * @param string a unique key
 332       * @return boolean whether there is a CSS block registered with the specified key
 333       */
 334  	public function isStyleSheetRegistered($key)
 335      {
 336          return isset($this->_styleSheets[$key]);
 337      }
 338  
 339      /**
 340       * @param string a unique key
 341       * @return boolean whether there is a head javascript file registered with the specified key
 342       */
 343  	public function isHeadScriptFileRegistered($key)
 344      {
 345          return isset($this->_headScriptFiles[$key]);
 346      }
 347  
 348      /**
 349       * @param string a unique key
 350       * @return boolean whether there is a head javascript block registered with the specified key
 351       */
 352  	public function isHeadScriptRegistered($key)
 353      {
 354          return isset($this->_headScripts[$key]);
 355      }
 356  
 357      /**
 358       * @param string a unique key
 359       * @return boolean whether there is a javascript file registered with the specified key
 360       */
 361  	public function isScriptFileRegistered($key)
 362      {
 363          return isset($this->_scriptFiles[$key]);
 364      }
 365  
 366      /**
 367       * @param string a unique key
 368       * @return boolean whether there is a beginning javascript block registered with the specified key
 369       */
 370  	public function isBeginScriptRegistered($key)
 371      {
 372          return isset($this->_beginScripts[$key]);
 373      }
 374  
 375      /**
 376       * @param string a unique key
 377       * @return boolean whether there is an ending javascript block registered with the specified key
 378       */
 379  	public function isEndScriptRegistered($key)
 380      {
 381          return isset($this->_endScripts[$key]);
 382      }
 383  
 384      /**
 385       * @param string a unique key
 386       * @return boolean whether there is a hidden field registered with the specified key
 387       */
 388  	public function isHiddenFieldRegistered($key)
 389      {
 390          return isset($this->_hiddenFields[$key]);
 391      }
 392  
 393      /**
 394       * @param THtmlWriter writer for the rendering purpose
 395       */
 396  	public function renderStyleSheetFiles($writer)
 397      {
 398          $str='';
 399          foreach($this->_styleSheetFiles as $url)
 400          {
 401              if(is_array($url))
 402                  $str.="<link rel=\"stylesheet\" type=\"text/css\" media=\"{$url[1]}\" href=\"".THttpUtility::htmlEncode($url[0])."\" />\n";
 403              else
 404                  $str.="<link rel=\"stylesheet\" type=\"text/css\" href=\"".THttpUtility::htmlEncode($url)."\" />\n";
 405          }
 406          $writer->write($str);
 407      }
 408  
 409      /**
 410       * @param THtmlWriter writer for the rendering purpose
 411       */
 412  	public function renderStyleSheets($writer)
 413      {
 414          if(count($this->_styleSheets))
 415              $writer->write("<style type=\"text/css\">\n/*<![CDATA[*/\n".implode("\n",$this->_styleSheets)."\n/*]]>*/\n</style>\n");
 416      }
 417  
 418      /**
 419       * @param THtmlWriter writer for the rendering purpose
 420       */
 421  	public function renderHeadScriptFiles($writer)
 422      {
 423          $writer->write(TJavaScript::renderScriptFiles($this->_headScriptFiles));
 424      }
 425  
 426      /**
 427       * @param THtmlWriter writer for the rendering purpose
 428       */
 429  	public function renderHeadScripts($writer)
 430      {
 431          $writer->write(TJavaScript::renderScriptBlocks($this->_headScripts));
 432      }
 433  
 434      /**
 435       * @param THtmlWriter writer for the rendering purpose
 436       */
 437  	public function renderScriptFiles($writer)
 438      {
 439          $this->renderPradoScripts($writer);
 440          $files=array();
 441          foreach($this->_scriptFiles as $key=>$file)
 442          {
 443              if($file!==true)
 444              {
 445                  $files[]=$file;
 446                  $this->_scriptFiles[$key]=true;
 447              }
 448          }
 449          if(!empty($files))
 450              $writer->write(TJavaScript::renderScriptFiles($files));
 451      }
 452  
 453      /**
 454       * @param THtmlWriter writer for the rendering purpose
 455       */
 456  	public function renderBeginScripts($writer)
 457      {
 458          $writer->write(TJavaScript::renderScriptBlocks($this->_beginScripts));
 459      }
 460  
 461      /**
 462       * @param THtmlWriter writer for the rendering purpose
 463       */
 464  	public function renderEndScripts($writer)
 465      {
 466          $writer->write(TJavaScript::renderScriptBlocks($this->_endScripts));
 467      }
 468  
 469      /**
 470       * @param THtmlWriter writer for the rendering purpose
 471       */
 472  	public function renderHiddenFields($writer)
 473      {
 474          $str='';
 475          foreach($this->_hiddenFields as $name=>$value)
 476          {
 477              if($value!==true)
 478              {
 479                  if(is_array($value))
 480                  {
 481                      foreach($value as $v)
 482                          $str.='<input type="hidden" name="'.$name.'[]" id="'.$name.'" value="'.THttpUtility::htmlEncode($value)."\" />\n";
 483                  }
 484                  else
 485                  {
 486                      $str.='<input type="hidden" name="'.$name.'" id="'.$name.'" value="'.THttpUtility::htmlEncode($value)."\" />\n";
 487                  }
 488                  // set hidden field value to true to indicate this field is rendered
 489                  // Note, hidden field rendering is invoked twice (at the beginning and ending of TForm)
 490                  $this->_hiddenFields[$name]=true;
 491              }
 492          }
 493          if($str!=='')
 494              $writer->write("<div>\n".$str."</div>\n");
 495      }
 496  }
 497  
 498  /**
 499   * TClientSideOptions abstract class.
 500   *
 501   * TClientSideOptions manages client-side options for components that have
 502   * common client-side javascript behaviours and client-side events such as
 503   * between ActiveControls and validators.
 504   *
 505   * @author <weizhuo[at]gmail[dot]com>
 506   * @version $Id: TClientScriptManager.php 1397 2006-09-07 07:55:53Z wei $
 507   * @package System.Web.UI
 508   * @since 3.0
 509   */
 510  abstract class TClientSideOptions extends TComponent
 511  {
 512      /**
 513       * @var TMap list of client-side options.
 514       */
 515      private $_options;
 516  
 517      /**
 518       * Constructor, initialize the options list.
 519       */
 520  	public function __construct()
 521      {
 522          $this->_options = Prado::createComponent('System.Collections.TMap');
 523      }
 524  
 525      /**
 526       * Adds on client-side event handler by wrapping the code within a
 527       * javascript function block. If the code begins with "javascript:", the
 528       * code is assumed to be a javascript function block rather than arbiturary
 529       * javascript statements.
 530       * @param string option name
 531       * @param string javascript statements.
 532       */
 533  	protected function setFunction($name, $code)
 534      {
 535          if(!TJavascript::isFunction($code))
 536              $code = TJavascript::quoteFunction($this->ensureFunction($code));
 537          $this->setOption($name, $code);
 538      }
 539  
 540      /**
 541       * @return string gets a particular option, null if not set.
 542       */
 543  	protected function getOption($name)
 544      {
 545          return $this->_options->itemAt($name);
 546      }
 547  
 548      /**
 549       * @param string option name
 550       * @param mixed option value.
 551       */
 552  	protected function setOption($name, $value)
 553      {
 554          $this->_options->add($name, $value);
 555      }
 556  
 557      /**
 558       * @return TMap gets the list of options as TMap
 559       */
 560  	public function getOptions()
 561      {
 562          return $this->_options;
 563      }
 564  
 565      /**
 566       * Ensure that the javascript statements are wrapped in a javascript
 567       * function block as <code>function(sender, parameter){ //code }</code>.
 568       */
 569  	protected function ensureFunction($javascript)
 570      {
 571          return "function(sender, parameter){ {$javascript} }";
 572      }
 573  }
 574  
 575  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7