Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial implementation of skin feature.
- Loading branch information
qiang.xue
committed
Sep 13, 2009
1 parent
4b31cdd
commit 768d676
Showing
9 changed files
with
232 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
/** | ||
* CWidgetFactory class file. | ||
* | ||
* @author Qiang Xue <qiang.xue@gmail.com> | ||
* @link http://www.yiiframework.com/ | ||
* @copyright Copyright © 2008-2009 Yii Software LLC | ||
* @license http://www.yiiframework.com/license/ | ||
*/ | ||
|
||
|
||
/** | ||
* CWidgetFactory creates new widgets to be used in views. | ||
* | ||
* CWidgetFactory is used as the default "widgetFactory" application component. | ||
* | ||
* When calling {@link CBaseController::createWidget}, {@link CBaseController::widget} | ||
* or {@link CBaseController::beginWidget}, if the "widgetFactory" component is enabled | ||
* (disabled by default), it will be used to create the requested widget. | ||
* | ||
* CWidgetFactory implements the "skin" feature, which allows a new widget to be created | ||
* and initialized with a set of predefined property values (called skin). | ||
* | ||
* When CWidgetFactory is used to create a new widget, it will first instantiate the | ||
* widget instance. It then checks if there is a skin available for this widget | ||
* according to the widget class name and the widget {@link CWidget::skin} property. | ||
* If a skin is found, it will be merged with the initial properties passed via | ||
* {@link createWidget}. Then the merged initial properties will be used to initialize | ||
* the newly created widget instance. | ||
* | ||
* As aforementioned, a skin is a set of initial property values for a widget. | ||
* It is thus represented as an associative array of name-value pairs. | ||
* Skins are stored in PHP scripts like other configurations. Each script file stores the skins | ||
* for a particular widget type and is named as the wiget class name (e.g. CLinkPager.php). | ||
* Each widget type may have one or several skins, identified by the skin ID set via | ||
* {@link CWidget::skin} property. If the {@link CWidget::skin} property is not set for a given | ||
* widget, it means the default skin would be used. The following shows the possible skins for | ||
* the {@link CLinkPager} widget: | ||
* <pre> | ||
* return array( | ||
* 'default'=>array( | ||
* 'nextPageLabel'=>'>>', | ||
* 'prevPageLabel'=>'<<', | ||
* ), | ||
* 'short'=>array( | ||
* 'header'=>'', | ||
* 'maxButtonCount'=>5, | ||
* ), | ||
* ); | ||
* </pre> | ||
* In the above, there are two skins. The first one is the default skin which is indexed by the string "default". | ||
* Note that {@link CWidget::skin} defaults to "default". Therefore, this is the skin that will be applied | ||
* if we do not explicitly specify the {@link CWidget::skin} property. | ||
* The second one is named as the "short" skin which will be used only when we set {@link CWidget::skin} | ||
* to be "short". | ||
* | ||
* By default, CWidgetFactory looks for the skin of a widget under the "skins" directory | ||
* of the current application's {@link CWebApplication::viewPath} (e.g. protected/views/skins). | ||
* If a theme is being used, it will look for the skin under the "skins" directory of | ||
* the theme's {@link CTheme::viewPath}. In case the specified skin is not found, a widget will still be created | ||
* normally without causing any error. | ||
* | ||
* Note that by default, the "widgetFactory" component is not enabled. It may be enabled | ||
* by configuring the component in the application configuration like the following: | ||
* <pre> | ||
* return array( | ||
* 'components'=>array( | ||
* 'widgetFactory'=>array( | ||
* 'enabled'=>true, | ||
* ), | ||
* ), | ||
* ) | ||
* </pre> | ||
* | ||
* @author Qiang Xue <qiang.xue@gmail.com> | ||
* @version $Id$ | ||
* @package system.web | ||
* @since 1.1 | ||
*/ | ||
class CWidgetFactory extends CApplicationComponent implements IWidgetFactory | ||
{ | ||
/** | ||
* @var string the directory containing all the skin files. Defaults to null, | ||
* meaning using the "skin" directory under the current application's {@link CWebApplication::viewPath}. | ||
*/ | ||
public $skinPath; | ||
|
||
private $_skins=array(); // class name, skin name, property name => value | ||
|
||
/** | ||
* Initializes the application component. | ||
* This method overrides the parent implementation by resolving the skin path. | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
if(($theme=Yii::app()->getTheme())!==null) | ||
$this->skinPath=$theme->getSkinPath(); | ||
else if($this->skinPath===null) | ||
$this->skinPath=Yii::app()->getViewPath().DIRECTORY_SEPARATOR.'skins'; | ||
} | ||
|
||
/** | ||
* Creates a new widget based on the given class name and initial properties. | ||
* @param CBaseController the owner of the new widget | ||
* @param string the class name of the widget. This can also be a path alias (e.g. system.web.widgets.COutputCache) | ||
* @param array the initial property values (name=>value) of the widget. | ||
* @return CWidget the newly created widget whose properties have been initialized with the given values. | ||
*/ | ||
public function createWidget($owner,$className,$properties=array()) | ||
{ | ||
$className=Yii::import($className,true); | ||
$widget=new $className($this); | ||
$skinName=isset($properties['skin']) ? $properties['skin'] : 'default'; | ||
if(($skin=$this->getSkin($className,$skinName))!==array()) | ||
$properties=$properties===array() ? $skin : CMap::mergeArray($skin,$properties); | ||
foreach($properties as $name=>$value) | ||
$widget->$name=$value; | ||
} | ||
|
||
/** | ||
* Returns the skin for the specified widget class and skin name. | ||
* @param string the widget class name | ||
* @param string the widget skin name | ||
* @return array the skin (name=>value) for the widget | ||
*/ | ||
protected function getSkin($className,$skinName) | ||
{ | ||
if(!isset($this->_skins[$className][$skinName])) | ||
{ | ||
$skinFile=$this->skinPath.DIRECTORY_SEPARATOR.$className.'.php'; | ||
if(is_file($skinFile)) | ||
$this->_skins[$className]=require($skinFile); | ||
else | ||
$this->_skins[$className]=array(); | ||
if(!isset($this->_skins[$className][$skinName])) | ||
$this->_skins[$className][$skinName]=array(); | ||
} | ||
return $this->_skins[$className][$skinName]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters