From 145c4aef09e278d29bfd8e9858e114ca097e5901 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 18 Feb 2006 07:24:51 +0000 Subject: client script reorganization. --- framework/Web/UI/TClientScriptManager.php | 164 +++++++++------------ framework/Web/UI/WebControls/TBaseValidator.php | 5 +- framework/Web/UI/WebControls/TColorPicker.php | 8 +- framework/Web/UI/WebControls/TDatePicker.php | 14 +- framework/Web/UI/WebControls/THtmlArea.php | 3 +- framework/Web/UI/WebControls/TJavascriptLogger.php | 2 +- framework/Web/UI/WebControls/TRatingList.php | 5 +- framework/Web/UI/WebControls/TTextHighlighter.php | 2 +- .../Web/UI/WebControls/TValidationSummary.php | 3 +- 9 files changed, 88 insertions(+), 118 deletions(-) (limited to 'framework/Web/UI') diff --git a/framework/Web/UI/TClientScriptManager.php b/framework/Web/UI/TClientScriptManager.php index 39c23b99..c4ac316a 100644 --- a/framework/Web/UI/TClientScriptManager.php +++ b/framework/Web/UI/TClientScriptManager.php @@ -22,40 +22,91 @@ Prado::using('System.Web.Javascripts.*'); */ class TClientScriptManager extends TApplicationComponent { - const SCRIPT_DIR='Web/Javascripts/js'; - //const POSTBACK_FUNC='Prado.doPostBack'; + /** + * directory containing Prado javascript files + */ + const SCRIPT_PATH='Web/Javascripts/js'; + /** + * the PHP script for loading Prado javascript files + */ + const SCRIPT_LOADER='clientscripts.php'; + /** + * @var TPage page who owns this manager + */ private $_page; + /** + * @var array registered hidden fields, indexed by hidden field names + */ private $_hiddenFields=array(); private $_beginScripts=array(); private $_endScripts=array(); private $_scriptFiles=array(); + private $_onSubmitStatements=array(); + private $_arrayDeclares=array(); + private $_expandoAttributes=array(); - //private $_headScriptFiles=array(); - //private $_headScripts=array(); + private $_headScriptFiles=array(); + private $_headScripts=array(); private $_styleSheetFiles=array(); private $_styleSheets=array(); - private $_client; - - /*private $_onSubmitStatements=array(); - private $_arrayDeclares=array(); - private $_expandoAttributes=array(); - private $_postBackScriptRegistered=false; - private $_focusScriptRegistered=false; - private $_scrollScriptRegistered=false; - */ - - private $_publishedScriptFiles=array(); + private $_registeredPradoScripts=array(); + private $_registeredPradoFiles=array(); + /** + * Client-side javascript library dependencies + * @var array + */ + private static $_pradoScripts=array( + 'prado' => array('prado'), + 'effects' => array('prado', 'effects'), + 'ajax' => array('prado', 'effects', 'ajax'), + 'validator' => array('prado', 'validator'), + 'logger' => array('prado', 'logger'), + 'datepicker' => array('prado', 'datepicker'), + 'rico' => array('prado', 'effects', 'ajax', 'rico'), + 'colorpicker' => array('prado', 'colorpicker') + ); + /** + * Constructor. + * @param TPage page that owns this client script manager + */ public function __construct(TPage $owner) { $this->_page=$owner; - $this->_client = new TClientScript($this); } + /** + * Registers Prado scripts by library name. + * The script files will be published. + * @param string script library name. Valid names include + * 'prado', 'effects', 'ajax', 'validator', 'logger', + * 'datepicker', 'rico', 'colorpicker'. + */ + public function registerPradoScript($name) + { + if(!isset($this->_registeredPradoScripts[$name])) + { + $this->_registeredPradoScripts[$name]=true; + if(!isset(self::$_pradoScripts[$name])) + throw new TInvalidOperationException('csmanager_pradoscript_invalid',$name); + $basePath=Prado::getFrameworkPath().'/'.self::SCRIPT_PATH; + foreach(self::$_pradoScripts[$name] as $script) + { + $this->publishFilePath($basePath.'/'.$script.'.js'); + $this->_registeredPradoFiles[$script]=true; + } + $scriptLoader=$basePath.'/'.self::SCRIPT_LOADER; + $url=$this->publishFilePath($scriptLoader); + $url.='?js='.implode(',',array_keys($this->_registeredPradoFiles)); + if($this->getApplication()->getMode()===TApplication::STATE_DEBUG) + $url.='&__nocache'; + $this->registerScriptFile('prado:pradoscripts',$url); + } + } public function registerPostBackControl($control,$namespace='Prado.WebUI') { @@ -67,7 +118,7 @@ class TClientScriptManager extends TApplicationComponent $this->registerHiddenField(TPage::FIELD_POSTBACK_TARGET,''); $this->registerHiddenField(TPage::FIELD_POSTBACK_PARAMETER,''); - $this->registerClientScript('prado'); + $this->registerPradoScript('prado'); } protected function getPostBackOptions($control) @@ -77,8 +128,7 @@ class TClientScriptManager extends TApplicationComponent $postback['ID'] = $control->getClientID(); if(!isset($postback['FormID'])) $postback['FormID'] = $this->_page->getForm()->getClientID(); - $options = new TJavascriptSerializer($postback); - return $options->toJavascript(); + return TJavaScript::encode($postback); } /** @@ -89,12 +139,9 @@ class TClientScriptManager extends TApplicationComponent */ public function registerDefaultButton($panel, $button) { - $serializer = new TJavascriptSerializer( - $this->getDefaultButtonOptions($panel, $button)); - $options = $serializer->toJavascript(); + $options = TJavaScript::encode($this->getDefaultButtonOptions($panel, $button)); $code = "new Prado.WebUI.DefaultButton($options);"; - $scripts = $this->_page->getClientScript(); - $scripts->registerEndScript("prado:".$panel->getClientID(), $code); + $this->registerEndScript("prado:".$panel->getClientID(), $code); } /** @@ -108,63 +155,6 @@ class TClientScriptManager extends TApplicationComponent return $options; } - - /** - * Register client scripts. - */ - public function registerClientScript($script) - { - static $scripts = array(); - $scripts = array_unique(array_merge($scripts, - TClientScript::getScripts($script))); - - $this->publishClientScriptAssets($scripts); - - //create the client script url - $url = $this->publishClientScriptCompressorAsset(); - $url .= '?js='.implode(',', $scripts); - if($this->getApplication()->getMode() == TApplication::STATE_DEBUG) - $url .= '&__nocache'; - $this->registerScriptFile('prado:gzipscripts', $url); - } - - /** - * Publish each individual javascript file. - */ - protected function publishClientScriptAssets($scripts) - { - foreach($scripts as $lib) - { - if(!isset($this->_publishedScriptFiles[$lib])) - { - $base = Prado::getFrameworkPath(); - $clientScripts = self::SCRIPT_DIR; - $file = "{$base}/{$clientScripts}/{$lib}.js"; - $this->publishFilePath($file); - $this->_publishedScriptFiles[$lib] = true; - } - } - } - - /** - * @return string URL of the compressor asset script. - */ - protected function publishClientScriptCompressorAsset() - { - $scriptFile = 'clientscripts.php'; - if(isset($this->_publishedScriptFiles[$scriptFile])) - return $this->_publishedScriptFiles[$scriptFile]; - else - { - $base = Prado::getFrameworkPath(); - $clientScripts = self::SCRIPT_DIR; - $file = "{$base}/{$clientScripts}/{$scriptFile}"; - $url= $this->publishFilePath($file); - $this->_publishedScriptFiles[$scriptFile] = $url; - return $url; - } - } - /* protected function registerPostBackScript() { if(!$this->_postBackScriptRegistered) @@ -449,18 +439,6 @@ class TClientScriptManager extends TApplicationComponent $this->registerEndScript($key, $script); } */ - - - /* - private void EnsureEventValidationFieldLoaded(); - internal string GetEventValidationFieldValue(); - public string GetWebResourceUrl(Type type, string resourceName); - public void RegisterClientScriptResource(Type type, string resourceName); - internal void RegisterDefaultButtonScript(Control button, $writer, bool useAddAttribute); - public function SaveEventValidationField(); - public void ValidateEvent(string uniqueId, string argument); - public function getCallbackEventReference() - */ } /** diff --git a/framework/Web/UI/WebControls/TBaseValidator.php b/framework/Web/UI/WebControls/TBaseValidator.php index 97ba1a8b..46f031ca 100644 --- a/framework/Web/UI/WebControls/TBaseValidator.php +++ b/framework/Web/UI/WebControls/TBaseValidator.php @@ -156,7 +156,7 @@ abstract class TBaseValidator extends TLabel implements IValidator $scriptKey = "TBaseValidator:$formID"; if($this->getEnableClientScript() && !$scripts->isEndScriptRegistered($scriptKey)) { - $scripts->registerClientScript('validator'); + $scripts->registerPradoScript('validator'); $scripts->registerEndScript($scriptKey, "Prado.Validation.AddForm('$formID');"); } if($this->getEnableClientScript()) @@ -194,8 +194,7 @@ abstract class TBaseValidator extends TLabel implements IValidator $class = get_class($this); $scriptKey = "prado:".$this->getClientID(); $scripts = $this->getPage()->getClientScript(); - $serializer = new TJavascriptSerializer($this->getClientScriptOptions()); - $options = $serializer->toJavascript(); + $options = TJavaScript::encode($this->getClientScriptOptions()); $js = "new Prado.Validation(Prado.Validation.{$class}, {$options});"; $scripts->registerEndScript($scriptKey, $js); } diff --git a/framework/Web/UI/WebControls/TColorPicker.php b/framework/Web/UI/WebControls/TColorPicker.php index c0cc3ae7..fda6bced 100644 --- a/framework/Web/UI/WebControls/TColorPicker.php +++ b/framework/Web/UI/WebControls/TColorPicker.php @@ -168,8 +168,7 @@ class TColorPicker extends TTextBox } $imgs['button.gif'] = $list['button.gif']; $imgs['background.png'] = $list['background.png']; - $serializer = new TJavascriptSerializer($imgs); - $options = $serializer->toJavascript(); + $options = TJavaScript::encode($imgs); $code = "Prado.WebUI.TColorPicker.UIImages = {$options};"; $cs->registerEndScript($key, $code); return $list; @@ -186,9 +185,8 @@ class TColorPicker extends TTextBox parent::addAttributesToRender($writer); $writer->addAttribute('id',$this->getClientID()); $scripts = $this->getPage()->getClientScript(); - $scripts->registerClientScript("colorpicker"); - $serializer = new TJavascriptSerializer($this->getColorPickerOptions()); - $options = $serializer->toJavascript(); + $scripts->registerPradoScript("colorpicker"); + $options = TJavaScript::encode($this->getColorPickerOptions()); $id = $this->getClientID(); $code = "Event.OnLoad(function(){ new Prado.WebUI.TColorPicker($options); });"; $scripts->registerEndScript("prado:$id", $code); diff --git a/framework/Web/UI/WebControls/TDatePicker.php b/framework/Web/UI/WebControls/TDatePicker.php index fd280938..a70e25b3 100644 --- a/framework/Web/UI/WebControls/TDatePicker.php +++ b/framework/Web/UI/WebControls/TDatePicker.php @@ -228,7 +228,8 @@ class TDatePicker extends TTextBox { $options['Format'] = $this->getDateFormat(); $options['FirstDayOfWeek'] = $this->getFirstDayOfWeek(); - $options['ClassName'] = $this->getCssClass(); + if(($cssClass=$this->getCssClass())!=='') + $options['ClassName'] = $cssClass; $options['FromYear'] = $this->getFromYear(); $options['UpToYear'] = $this->getUpToYear(); if($this->getMode()!=='Basic') @@ -252,10 +253,8 @@ class TDatePicker extends TTextBox Prado::using('System.I18N.core.DateTimeFormatInfo'); $info = Prado::createComponent('System.I18N.core.CultureInfo', $culture); $date = $info->getDateTimeFormat(); - $serializer = new TJavascriptSerializer($date->getMonthNames()); - $options['MonthNames'] = $serializer->toList(); - $serializer = new TJavascriptSerializer($date->getAbbreviatedDayNames()); - $options['ShortWeekDayNames'] = $serializer->toList(); + $options['MonthNames'] = TJavaScript::encode($date->getMonthNames(),false); + $options['ShortWeekDayNames'] = TJavaScript::encode($date->getAbbreviatedDayNames(),false); return $options; } @@ -367,9 +366,8 @@ class TDatePicker extends TTextBox if($this->getShowCalendar()) { $scripts = $this->getPage()->getClientScript(); - $scripts->registerClientScript("datepicker"); - $serializer = new TJavascriptSerializer($this->getDatePickerOptions()); - $options = $serializer->toJavascript(); + $scripts->registerPradoScript("datepicker"); + $options = TJavaScript::encode($this->getDatePickerOptions()); $id = $this->getClientID(); $code = "Event.OnLoad(function(){ new Prado.WebUI.TDatePicker('$id', $options); });"; $scripts->registerEndScript("prado:$id", $code); diff --git a/framework/Web/UI/WebControls/THtmlArea.php b/framework/Web/UI/WebControls/THtmlArea.php index 1801e739..70788e2d 100644 --- a/framework/Web/UI/WebControls/THtmlArea.php +++ b/framework/Web/UI/WebControls/THtmlArea.php @@ -262,8 +262,7 @@ class THtmlArea extends TTextBox $scripts = $this->getPage()->getClientScript(); if(!$scripts->isScriptFileRegistered('prado:THtmlArea')) $scripts->registerScriptFile('prado:THtmlArea', $this->getScriptUrl()); - $serializer = new TJavascriptSerializer($this->getEditorOptions()); - $options = $serializer->toJavascript(true); + $options = TJavaScript::encode($this->getEditorOptions()); $script = "if(tinyMCE){ tinyMCE.init($options); }"; $scripts->registerEndScript('THtmlArea'.$this->ClientID,$script); } diff --git a/framework/Web/UI/WebControls/TJavascriptLogger.php b/framework/Web/UI/WebControls/TJavascriptLogger.php index fa28d2b0..c38f7bc4 100644 --- a/framework/Web/UI/WebControls/TJavascriptLogger.php +++ b/framework/Web/UI/WebControls/TJavascriptLogger.php @@ -54,7 +54,7 @@ class TJavascriptLogger extends TWebControl */ public function renderContents($writer) { - $this->Page->ClientScript->registerClientScript('logger'); + $this->Page->ClientScript->registerPradoScript('logger'); $info = '(more info).'; $usage = 'Press ALT-D (Or CTRL-D on OS X) to toggle the javascript log console'; $writer->write("{$usage} {$info}"); diff --git a/framework/Web/UI/WebControls/TRatingList.php b/framework/Web/UI/WebControls/TRatingList.php index 4302c3b3..006391f7 100644 --- a/framework/Web/UI/WebControls/TRatingList.php +++ b/framework/Web/UI/WebControls/TRatingList.php @@ -106,9 +106,8 @@ class TRatingList extends TRadioButtonList $this->publishRatingListAssets(); $id = $this->getClientID(); $scripts = $this->getPage()->getClientScript(); - $scripts->registerClientScript('prado'); - $serializer = new TJavascriptSerializer($this->getRatingClientOptions()); - $options = $serializer->toJavascript(); + $scripts->registerPradoScript('prado'); + $options = TJavaScript::encode($this->getRatingClientOptions()); $code = "new Prado.WebUI.TRatingList($options);"; $scripts->registerEndScript("prado:$id", $code); } diff --git a/framework/Web/UI/WebControls/TTextHighlighter.php b/framework/Web/UI/WebControls/TTextHighlighter.php index 4eec7de1..cedc807f 100644 --- a/framework/Web/UI/WebControls/TTextHighlighter.php +++ b/framework/Web/UI/WebControls/TTextHighlighter.php @@ -85,7 +85,7 @@ class TTextHighlighter extends TWebControl { parent::onPreRender($writer); $this->registerHighlightStyleSheet(); - $this->getPage()->getClientScript()->registerClientScript('prado'); + $this->getPage()->getClientScript()->registerPradoScript('prado'); } /** diff --git a/framework/Web/UI/WebControls/TValidationSummary.php b/framework/Web/UI/WebControls/TValidationSummary.php index 027a2cee..e7115c99 100644 --- a/framework/Web/UI/WebControls/TValidationSummary.php +++ b/framework/Web/UI/WebControls/TValidationSummary.php @@ -182,8 +182,7 @@ class TValidationSummary extends TWebControl { if(!$this->getEnabled(true) || !$this->getEnableClientScript()) return; - $serializer = new TJavascriptSerializer($this->getClientScriptOptions()); - $options = $serializer->toJavascript(); + $options=TJavaScript::encode($this->getClientScriptOptions()); $script = "new Prado.Validation.Summary({$options});"; $this->getPage()->getClientScript()->registerEndScript($this->getClientID(), $script); } -- cgit v1.2.3