summaryrefslogtreecommitdiff
path: root/framework
diff options
context:
space:
mode:
authorChristophe.Boulain <>2008-11-08 17:41:48 +0000
committerChristophe.Boulain <>2008-11-08 17:41:48 +0000
commit15161da7755a8d74853ccae0966afa22d9830b07 (patch)
treecb2343aafc94f4dfc06db40a137c840d346c8a55 /framework
parent4ee9040fbfce871db1bf01e7f7a051184bbca37b (diff)
Issue#36: Refactored TRatingList/TActiveRatingList, and added some docs.
Diffstat (limited to 'framework')
-rw-r--r--framework/Web/Javascripts/source/prado/activeratings/blocks.css42
-rw-r--r--framework/Web/Javascripts/source/prado/activeratings/default.css43
-rw-r--r--framework/Web/Javascripts/source/prado/activeratings/ratings.js178
-rw-r--r--framework/Web/Javascripts/source/prado/ratings/ratings.js207
-rw-r--r--framework/Web/UI/ActiveControls/TActiveRatingList.php343
-rw-r--r--framework/Web/UI/WebControls/TRatingList.php347
6 files changed, 461 insertions, 699 deletions
diff --git a/framework/Web/Javascripts/source/prado/activeratings/blocks.css b/framework/Web/Javascripts/source/prado/activeratings/blocks.css
deleted file mode 100644
index bb846094..00000000
--- a/framework/Web/Javascripts/source/prado/activeratings/blocks.css
+++ /dev/null
@@ -1,42 +0,0 @@
-.TActiveRatingList_blocks
-{
- border-collapse: collapse;
-}
-.TActiveRatingList_blocks input, .TActiveRatingList_blocks label
-{
- display: none;
-}
-
-.TActiveRatingList_blocks td
-{
- width: 18px;
- height: 9px;
- padding: 1px;
-}
-
-.TActiveRatingList_blocks td.rating
-{
- background-image: url(blocks_combined.gif);
- background-repeat: no-repeat;
- cursor: pointer;
- background-position: 1px 0px;
-}
-.TActiveRatingList_blocks td.rating_selected
-{
- background-position: 1px -100px;
-}
-
-.TActiveRatingList_blocks td.rating_half
-{
- background-position: 1px -200px;
-}
-
-.TActiveRatingList_blocks td.rating_hover
-{
- background-position: 1px -300px;
-}
-
-.TActiveRatingList_blocks td.rating_disabled
-{
- cursor: default !important;
-}
diff --git a/framework/Web/Javascripts/source/prado/activeratings/default.css b/framework/Web/Javascripts/source/prado/activeratings/default.css
deleted file mode 100644
index ba90eb27..00000000
--- a/framework/Web/Javascripts/source/prado/activeratings/default.css
+++ /dev/null
@@ -1,43 +0,0 @@
-.TActiveRatingList_default
-{
- border-collapse: collapse;
-}
-.TActiveRatingList_default input, .TActiveRatingList_default label
-{
- display: none;
-}
-
-.TActiveRatingList_default td
-{
- width: 18px;
- height: 18px;
- padding: 0;
-}
-
-.TActiveRatingList_default td.rating
-{
- background-image: url(default_combined.gif);
- background-repeat: no-repeat;
- cursor: pointer;
- background-position: 0px 0px;
-}
-
-.TActiveRatingList_default td.rating_selected
-{
- background-position: 0px -100px;
-}
-
-.TActiveRatingList_default td.rating_half
-{
- background-position: 0px -200px;
-}
-
-.TActiveRatingList_default td.rating_hover
-{
- background-position: 0px -300px;
-}
-
-.TActiveRatingList_default td.rating_disabled
-{
- cursor: default !important;
-} \ No newline at end of file
diff --git a/framework/Web/Javascripts/source/prado/activeratings/ratings.js b/framework/Web/Javascripts/source/prado/activeratings/ratings.js
deleted file mode 100644
index 4eeddbd8..00000000
--- a/framework/Web/Javascripts/source/prado/activeratings/ratings.js
+++ /dev/null
@@ -1,178 +0,0 @@
-Prado.WebUI.TActiveRatingList = Base.extend(
-{
- selectedIndex : -1,
- rating: -1,
- enabled : true,
- readOnly : false,
-
- constructor : function(options)
- {
- var cap = $(options.CaptionID);
- this.options = Object.extend(
- {
- caption : cap ? cap.innerHTML : ''
- }, options || {});
-
- Prado.WebUI.TActiveRatingList.register(this);
- this._init();
- this.selectedIndex = options.SelectedIndex;
- this.rating = options.Rating;
- if(options.Rating <= 0 && options.SelectedIndex >= 0)
- this.rating = options.SelectedIndex+1;
- this.showRating(this.rating);
- },
-
- _init: function(options)
- {
- Element.addClassName($(this.options.ListID),this.options.Style);
- this.radios = new Array();
- var index=0;
- for(var i = 0; i<this.options.ItemCount; i++)
- {
- var radio = $(this.options.ListID+'_c'+i);
- var td = radio.parentNode.parentNode;
- if(radio && td.tagName.toLowerCase()=='td')
- {
- this.radios.push(radio);
- Event.observe(td, "mouseover", this.hover.bindEvent(this,index));
- Event.observe(td, "mouseout", this.recover.bindEvent(this,index));
- Event.observe(td, "click", this.click.bindEvent(this, index));
- index++;
- Element.addClassName(td,"rating");
- }
- }
- },
-
- hover : function(ev,index)
- {
- if(this.enabled==false) return;
- for(var i = 0; i<this.radios.length; i++)
- {
- var node = this.radios[i].parentNode.parentNode;
- var action = i <= index ? 'addClassName' : 'removeClassName'
- Element[action](node,"rating_hover");
- Element.removeClassName(node,"rating_selected");
- Element.removeClassName(node,"rating_half");
- }
- this.showCaption(this.getIndexCaption(index));
- },
-
- recover : function(ev,index)
- {
- if(this.enabled==false) return;
- this.showRating(this.rating);
- this.showCaption(this.options.caption);
- },
-
- click : function(ev, index)
- {
- if(this.enabled==false) return;
- for(var i = 0; i<this.radios.length; i++)
- this.radios[i].checked = (i == index);
-
- this.selectedIndex = index;
- this.setRating(index+1);
-
- this.dispatchRequest(ev);
- },
-
- dispatchRequest : function(ev)
- {
- var requestOptions = Object.extend(
- {
- ID : this.options.ListID+"_c"+this.selectedIndex,
- EventTarget : this.options.ListName+"$c"+this.selectedIndex
- },this.options);
- var request = new Prado.CallbackRequest(requestOptions.EventTarget, requestOptions);
- if(request.dispatch()==false)
- Event.stop(ev);
- },
-
- setRating : function(value)
- {
- this.rating = value;
- var base = Math.floor(value-1);
- var remainder = value - base-1;
- var halfMax = this.options.HalfRating["1"];
- var index = remainder > halfMax ? base+1 : base;
- for(var i = 0; i<this.radios.length; i++)
- this.radios[i].checked = (i == index);
-
- var caption = this.getIndexCaption(index);
- this.setCaption(caption);
- this.showCaption(caption);
-
- this.showRating(value);
- },
-
- showRating: function(value)
- {
- var base = Math.floor(value-1);
- var remainder = value - base-1;
- var halfMin = this.options.HalfRating["0"];
- var halfMax = this.options.HalfRating["1"];
- var index = remainder > halfMax ? base+1 : base;
- var hasHalf = remainder >= halfMin && remainder <= halfMax;
- for(var i = 0; i<this.radios.length; i++)
- {
- var node = this.radios[i].parentNode.parentNode;
- var action = i > index ? 'removeClassName' : 'addClassName';
- Element[action](node, "rating_selected");
- if(i==index+1 && hasHalf)
- Element.addClassName(node, "rating_half");
- else
- Element.removeClassName(node, "rating_half");
- Element.removeClassName(node,"rating_hover");
- }
- },
-
- getIndexCaption : function(index)
- {
- return index > -1 ? this.radios[index].value : this.options.caption;
- },
-
- showCaption : function(value)
- {
- var caption = $(this.options.CaptionID);
- if(caption) caption.innerHTML = value;
- $(this.options.ListID).title = value;
- },
-
- setCaption : function(value)
- {
- this.options.caption = value;
- this.showCaption(value);
- },
-
- setEnabled : function(value)
- {
- this.enabled = value;
- for(var i = 0; i<this.radios.length; i++)
- {
- var action = value ? 'removeClassName' : 'addClassName'
- Element[action](this.radios[i].parentNode.parentNode, "rating_disabled");
- }
- }
-},
-{
-ratings : {},
-register : function(rating)
-{
- Prado.WebUI.TActiveRatingList.ratings[rating.options.ListID] = rating;
-},
-
-setEnabled : function(id,value)
-{
- Prado.WebUI.TActiveRatingList.ratings[id].setEnabled(value);
-},
-
-setRating : function(id,value)
-{
- Prado.WebUI.TActiveRatingList.ratings[id].setRating(value);
-},
-
-setCaption : function(id,value)
-{
- Prado.WebUI.TActiveRatingList.ratings[id].setCaption(value);
-}
-}); \ No newline at end of file
diff --git a/framework/Web/Javascripts/source/prado/ratings/ratings.js b/framework/Web/Javascripts/source/prado/ratings/ratings.js
index e1770f1f..0c5412ea 100644
--- a/framework/Web/Javascripts/source/prado/ratings/ratings.js
+++ b/framework/Web/Javascripts/source/prado/ratings/ratings.js
@@ -1,60 +1,205 @@
-Prado.WebUI.TRatingList = Class.create();
-Prado.WebUI.TRatingList.prototype =
+Prado.WebUI.TRatingList = Base.extend(
{
selectedIndex : -1,
+ rating: -1,
+ readOnly : false,
- initialize : function(options)
+ constructor : function(options)
{
- this.options = options;
- this.element = $(options['ID']);
- Element.addClassName(this.element,options.cssClass);
- this.radios = document.getElementsByName(options.field);
- for(var i = 0; i<this.radios.length; i++)
+ var cap = $(options.CaptionID);
+ this.options = Object.extend(
+ {
+ caption : cap ? cap.innerHTML : ''
+ }, options || {});
+
+ Prado.WebUI.TRatingList.register(this);
+ this._init();
+ this.selectedIndex = options.SelectedIndex;
+ this.rating = options.Rating;
+ this.readOnly = options.ReadOnly
+ if(options.Rating <= 0 && options.SelectedIndex >= 0)
+ this.rating = options.SelectedIndex+1;
+ this.setReadOnly(this.readOnly);
+ },
+
+ _init: function(options)
+ {
+ Element.addClassName($(this.options.ListID),this.options.Style);
+ this.radios = new Array();
+ this._mouseOvers = new Array();
+ this._mouseOuts = new Array();
+ this._clicks = new Array();
+ var index=0;
+ for(var i = 0; i<this.options.ItemCount; i++)
{
- Event.observe(this.radios[i].parentNode.parentNode, "mouseover", this.hover.bindEvent(this,i));
- Event.observe(this.radios[i].parentNode.parentNode, "mouseout", this.recover.bindEvent(this,i));
- Event.observe(this.radios[i].parentNode.parentNode, "click", this.click.bindEvent(this, i));
+ var radio = $(this.options.ListID+'_c'+i);
+ var td = radio.parentNode.parentNode;
+ if(radio && td.tagName.toLowerCase()=='td')
+ {
+ this.radios.push(radio);
+ this._mouseOvers.push(this.hover.bindEvent(this,index));
+ this._mouseOuts.push(this.recover.bindEvent(this,index));
+ this._clicks.push(this.click.bindEvent(this,index));
+ index++;
+ Element.addClassName(td,"rating");
+ }
}
- this.caption = CAPTION();
- this.element.appendChild(this.caption);
- this.selectedIndex = options.selectedIndex;
- this.setRating(this.selectedIndex);
},
hover : function(ev,index)
{
+ if(this.readOnly==true) return;
for(var i = 0; i<this.radios.length; i++)
- this.radios[i].parentNode.parentNode.className = (i<=index) ? "rating_hover" : "";
- this.setCaption(index);
+ {
+ var node = this.radios[i].parentNode.parentNode;
+ var action = i <= index ? 'addClassName' : 'removeClassName'
+ Element[action](node,"rating_hover");
+ Element.removeClassName(node,"rating_selected");
+ Element.removeClassName(node,"rating_half");
+ }
+ this.showCaption(this.getIndexCaption(index));
},
recover : function(ev,index)
{
- for(var i = 0; i<=index; i++)
- Element.removeClassName(this.radios[i].parentNode.parentNode, "rating_hover");
- this.setRating(this.selectedIndex);
+ if(this.readOnly==true) return;
+ this.showRating(this.rating);
+ this.showCaption(this.options.caption);
},
click : function(ev, index)
{
+ if(this.readOnly==true) return;
for(var i = 0; i<this.radios.length; i++)
this.radios[i].checked = (i == index);
this.selectedIndex = index;
- this.setRating(index);
- if(typeof(this.options.onChange)=="function")
- this.options.onChange(this,index);
+ this.setRating(index+1);
+
+ if(this.options['AutoPostBack']==true){
+ this.dispatchRequest(ev);
+ }
+ },
+
+ dispatchRequest : function(ev)
+ {
+ var requestOptions = Object.extend(
+ {
+ ID : this.options.ListID+"_c"+this.selectedIndex,
+ EventTarget : this.options.ListName+"$c"+this.selectedIndex
+ },this.options);
+ Prado.PostBack(ev, requestOptions);
+ },
+
+ setRating : function(value)
+ {
+ this.rating = value;
+ var base = Math.floor(value-1);
+ var remainder = value - base-1;
+ var halfMax = this.options.HalfRating["1"];
+ var index = remainder > halfMax ? base+1 : base;
+ for(var i = 0; i<this.radios.length; i++)
+ this.radios[i].checked = (i == index);
+
+ var caption = this.getIndexCaption(index);
+ this.setCaption(caption);
+ this.showCaption(caption);
+
+ this.showRating(this.rating);
+ },
+
+ showRating: function(value)
+ {
+ var base = Math.floor(value-1);
+ var remainder = value - base-1;
+ var halfMin = this.options.HalfRating["0"];
+ var halfMax = this.options.HalfRating["1"];
+ var index = remainder > halfMax ? base+1 : base;
+ var hasHalf = remainder >= halfMin && remainder <= halfMax;
+ for(var i = 0; i<this.radios.length; i++)
+ {
+ var node = this.radios[i].parentNode.parentNode;
+ var action = i > index ? 'removeClassName' : 'addClassName';
+ Element[action](node, "rating_selected");
+ if(i==index+1 && hasHalf)
+ Element.addClassName(node, "rating_half");
+ else
+ Element.removeClassName(node, "rating_half");
+ Element.removeClassName(node,"rating_hover");
+ }
+ },
+
+ getIndexCaption : function(index)
+ {
+ return index > -1 ? this.radios[index].value : this.options.caption;
+ },
+
+ showCaption : function(value)
+ {
+ var caption = $(this.options.CaptionID);
+ if(caption) caption.innerHTML = value;
+ $(this.options.ListID).title = value;
},
- setRating: function(index)
+ setCaption : function(value)
{
- for(var i = 0; i<=index; i++)
- this.radios[i].parentNode.parentNode.className = "rating_selected";
- this.setCaption(index);
+ this.options.caption = value;
+ this.showCaption(value);
},
- setCaption : function(index)
+ setReadOnly : function(value)
{
- this.caption.innerHTML = index > -1 ?
- this.radios[index].value : this.options.caption;
+ this.readOnly = value;
+ for(var i = 0; i<this.radios.length; i++)
+ {
+
+ var action = value ? 'addClassName' : 'removeClassName';
+ Element[action](this.radios[i].parentNode.parentNode, "rating_disabled");
+
+ var action = value ? 'stopObserving' : 'observe';
+ var td = this.radios[i].parentNode.parentNode;
+ Event[action](td, "mouseover", this._mouseOvers[i]);
+ Event[action](td, "mouseout", this._mouseOuts[i]);
+ Event[action](td, "click", this._clicks[i]);
+ }
+
+ this.showRating(this.rating);
}
-};
+},
+{
+ratings : {},
+register : function(rating)
+{
+ Prado.WebUI.TRatingList.ratings[rating.options.ListID] = rating;
+},
+
+setReadOnly : function(id,value)
+{
+ Prado.WebUI.TRatingList.ratings[id].setReadOnly(value);
+},
+
+setRating : function(id,value)
+{
+ Prado.WebUI.TRatingList.ratings[id].setRating(value);
+},
+
+setCaption : function(id,value)
+{
+ Prado.WebUI.TRatingList.ratings[id].setCaption(value);
+}
+});
+
+Prado.WebUI.TActiveRatingList = Prado.WebUI.TRatingList.extend(
+{
+ dispatchRequest : function(ev)
+ {
+ var requestOptions = Object.extend(
+ {
+ ID : this.options.ListID+"_c"+this.selectedIndex,
+ EventTarget : this.options.ListName+"$c"+this.selectedIndex
+ },this.options);
+ var request = new Prado.CallbackRequest(requestOptions.EventTarget, requestOptions);
+ if(request.dispatch()==false)
+ Event.stop(ev);
+ },
+
+});
diff --git a/framework/Web/UI/ActiveControls/TActiveRatingList.php b/framework/Web/UI/ActiveControls/TActiveRatingList.php
index c8da9295..473ca06a 100644
--- a/framework/Web/UI/ActiveControls/TActiveRatingList.php
+++ b/framework/Web/UI/ActiveControls/TActiveRatingList.php
@@ -3,8 +3,9 @@
* TActiveRatingList class file.
*
* @author Wei Zhuo <weizhuo[at]gamil[dot]com>
+ * @author Bradley Booms <bradley[dot]booms[at]gmail[dot]com>
* @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2008 PradoSoft
+ * @copyright Copyright &copy; 2005-2008 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Id$
* @package System.Web.UI.ActiveControls
@@ -13,107 +14,85 @@
/**
* TActiveRatingList Class
*
- * Displays clickable images that represent a TActiveRadioButtonList
+ * Displays clickable images that represent a TRadioButtonList
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @author Bradley Booms <bradley[dot]booms[at]gmail[dot]com>
* @version $Id$
* @package System.Web.UI.ActiveControls
* @since 3.1
*/
-class TActiveRatingList extends TActiveRadioButtonList
+class TActiveRatingList extends TRatingList implements IActiveControl, ICallbackEventHandler
{
- const SCRIPT_PATH = 'prado/activeratings';
-
- /**
- * @var array list of published rating images.
- */
- private $_ratingImages = array();
-
/**
- * Sets the default repeat direction to horizontal.
+ * Creates a new callback control, sets the adapter to
+ * TActiveListControlAdapter. If you override this class, be sure to set the
+ * adapter appropriately by, for example, by calling this constructor.
*/
public function __construct()
{
+ $this->setAdapter(new TActiveListControlAdapter($this));
+ $this->setAutoPostBack(true);
parent::__construct();
- $this->setRepeatDirection(TRepeatDirection::Horizontal);
- }
-
- /**
- * @return boolean whether the items in the column can be edited. Defaults to false.
- */
- public function getReadOnly()
- {
- return $this->getViewState('ReadOnly',false);
}
/**
- * @param boolean whether the items in the column can be edited
+ * @return TBaseActiveCallbackControl standard callback control options.
*/
- public function setReadOnly($value)
+ public function getActiveControl()
{
- $this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
+ return $this->getAdapter()->getBaseActiveControl();
}
/**
- * The repeat layout must be Table.
- * @param string repeat layout type
- * @throws TInvaliddataValueException when repeat layout is not Table.
+ * @return TCallbackClientSide client side request options.
*/
- public function setRepeatLayout($value)
+ public function getClientSide()
{
- if($value!==TRepeatLayout::Table)
- throw new TInvalidDataValueException('ratinglist_table_layout_only');
- else
- parent::setRepeatLayout($value);
+ return $this->getAdapter()->getBaseActiveControl()->getClientSide();
}
/**
- * @return float rating value.
+ * Raises the callback event. This method is required by {@link
+ * ICallbackEventHandler} interface.
+ * This method is mainly used by framework and control developers.
+ * @param TCallbackEventParameter the event parameter
*/
- public function getRating()
+ public function raiseCallbackEvent($param)
{
- return $this->getViewState('Rating',0.0);
+ $this->onCallback($param);
}
/**
- * @param float rating value, also sets the selected Index
+ * This method is invoked when a callback is requested. The method raises
+ * 'OnCallback' event to fire up the event handlers. If you override this
+ * method, be sure to call the parent implementation so that the event
+ * handler can be invoked.
+ * @param TCallbackEventParameter event parameter to be passed to the event handlers
*/
- public function setRating($value)
+ public function onCallback($param)
{
- $rating = TPropertyValue::ensureFloat($value);
- $this->setViewState('Rating', $rating);
- $canUpdate = $this->getActiveControl()->getEnableUpdate();
- $this->getActiveControl()->setEnableUpdate(false);
- parent::setSelectedIndex($this->getRatingIndex($rating));
- $this->getActiveControl()->setEnableUpdate($canUpdate);
- if($this->getActiveControl()->canUpdateClientSide())
- $this->callClientFunction('setRating',$rating);
+ $this->raiseEvent('OnCallback', $this, $param);
}
/**
- * @param float rating value
- * @return int rating as integer
+ * @param boolean whether the items in the column can be edited
*/
- protected function getRatingIndex($rating)
+ public function setReadOnly($value)
{
- $interval = $this->getHalfRatingInterval();
- $base = intval($rating)-1;
- $remainder = $rating-$base-1;
- return $remainder > $interval[1] ? $base+1 : $base;
+ parent::setReadOnly($value);
+ $value = $this->getReadOnly();
+ $this->callClientFunction('setReadOnly',$value);
}
/**
- * @param int change the rating selection index
+ * @param float rating value, also sets the selected Index
*/
- public function setSelectedIndex($value)
+ public function setRating($value)
{
- $value = TPropertyValue::ensureInteger($value);
- $canUpdate = $this->getActiveControl()->getEnableUpdate();
- $this->getActiveControl()->setEnableUpdate(false);
- parent::setSelectedIndex($value);
- $this->getActiveControl()->setEnableUpdate($canUpdate);
- if($this->getActiveControl()->canUpdateClientSide())
- $this->callClientFunction('setRating',$value+1);
+ parent::setRating($value);
+ $value = $this->getRating();
+ $this->callClientFunction('setRating',$value);
}
/**
@@ -123,250 +102,22 @@ class TActiveRatingList extends TActiveRadioButtonList
*/
protected function callClientFunction($func,$value)
{
- $client = $this->getPage()->getCallbackClient();
- $code = $this->getClientClassName().'.'.$func;
- $client->callClientFunction($code,array($this,$value));
- }
-
- /**
- * @return string control or html element ID for displaying a caption.
- */
- public function getCaptionID()
- {
- return $this->getViewState('CaptionID', '');
- }
-
- /**
- * @param string control or html element ID for displaying a caption.
- */
- public function setCaptionID($value)
- {
- $this->setViewState('CaptionID', $value, '');
- }
-
- protected function getCaptionControl()
- {
- if(($id=$this->getCaptionID())!=='')
- {
- if($control=$this->getParent()->findControl($id))
- return $control;
- }
- throw new TInvalidDataValueException(
- 'ratinglist_invalid_caption_id',$id,$this->getID());
- }
-
- public function setCaption($value)
- {
- $this->getCaptionControl()->setText($value);
- if($this->getActiveControl()->canUpdateClientSide())
- $this->callClientFunction('setCaption',$value);
- }
-
- public function getCaption()
- {
- return $this->getCaptionControl()->getText();
- }
-
- /**
- * @param boolean true to enable the rating to be changed.
- */
- public function setEnabled($value)
- {
- $value = TPropertyValue::ensureBoolean($value);
- parent::setEnabled($value);
if($this->getActiveControl()->canUpdateClientSide())
- $this->callClientFunction('setEnabled',$value);
- }
-
- /**
- * @param string set the rating style, default is "default"
- */
- public function setRatingStyle($value)
- {
- $this->setViewState('RatingStyle', $value, 'default');
- }
-
- /**
- * @return TActiveRatingListStyle current rating style
- */
- public function getRatingStyle()
- {
- return $this->getViewState('RatingStyle', 'default');
- }
-
- /**
- * Sets the interval such that those rating values within the interval
- * will be considered as a half star rating.
- * @param array rating display half value interval, default is array(0.3, 0.7);
- */
- public function setHalfRatingInterval($value)
- {
- $this->setViewState('HalfRating',
- TPropertyValue::ensureArray($value), array(0.3, 0.7));
- }
-
- /**
- * @return array rating display half value interval, default is array(0.3, 0.7);
- */
- public function getHalfRatingInterval()
- {
- return $this->getViewState('HalfRating', array(0.3, 0.7));
- }
-
- /**
- * @return string rating style css class name.
- */
- protected function getRatingStyleCssClass()
- {
- return 'TActiveRatingList_'.$this->getRatingStyle();
- }
-
- /**
- * @return array list of post back options.
- */
- protected function getPostBackOptions()
- {
- $options = parent::getPostBackOptions();
- $options['Style'] = $this->getRatingStyleCssClass();
- $options['CaptionID'] = $this->getCaptionControlID();
- $options['SelectedIndex'] = $this->getSelectedIndex();
- $options['Rating'] = $this->getRating();
- $options['HalfRating'] = $this->getHalfRatingInterval();
- return $options;
- }
-
- /**
- * Registers the javascript code for initializing the active control
- * only if {@link setReadOnly ReadOnly} property is false.
- */
- protected function renderClientControlScript($writer)
- {
- if($this->getReadOnly()===false)
- parent::renderClientControlScript($writer);
- }
-
- /**
- * @return string find the client ID of the caption control.
- */
- protected function getCaptionControlID()
- {
- if(($id=$this->getCaptionID())!=='')
{
- if($control=$this->getParent()->findControl($id))
- {
- if($control->getVisible(true))
- return $control->getClientID();
- }
- else
- return $id;
+ $client = $this->getPage()->getCallbackClient();
+ $code = parent::getClientClassName().'.'.$func;
+ $client->callClientFunction($code,array($this,$value));
}
- return '';
}
/**
- * @param string asset file in the self::SCRIPT_PATH directory.
- * @return string asset file url.
+ * @param string caption text
*/
- protected function getAssetUrl($file='')
- {
- $base = $this->getPage()->getClientScript()->getPradoScriptAssetUrl();
- return $base.'/'.self::SCRIPT_PATH.'/'.$file;
- }
-
- /**
- * @param string rating style name
- * @return string URL of the css style file
- */
- protected function publishRatingListStyle($style)
- {
- $cs = $this->getPage()->getClientScript();
- $url = $this->getAssetUrl($style.'.css');
- if(!$cs->isStyleSheetFileRegistered($url))
- $cs->registerStyleSheetFile($url, $url);
- return $url;
- }
-
- /**
- * @param string rating style name
- * @param string rating image file extension, default is '.gif'
- * @return array URL of publish the rating images
- */
- protected function publishRatingListImages($style, $fileExt='.gif')
- {
- $types = array('blank', 'selected', 'half', 'combined');
- $files = array();
- foreach($types as $type)
- $files[$type] = $this->getAssetUrl("{$style}_{$type}{$fileExt}");
- return $files;
- }
-
- /**
- * Add rating style class name to the class attribute
- * when {@link setReadOnly ReadOnly} property is true and when the
- * {@link setCssClass CssClass} property is empty.
- * @param THtmlWriter renderer
- */
- public function render($writer)
- {
- if($this->getReadOnly())
- $writer->addAttribute('class', $this->getRatingStyleCssClass());
- else
- {
- $writer->addAttribute('id',$this->getClientID());
- $this->getActiveControl()->registerCallbackClientScript(
- $this->getClientClassName(), $this->getPostBackOptions());
- }
- parent::render($writer);
- }
-
- /**
- * Publish the the rating style css file and rating image files.
- */
- public function onPreRender($param)
- {
- parent::onPreRender($param);
-
- $this->publishRatingListStyle($this->getRatingStyle());
- $this->_ratingImages = $this->publishRatingListImages($this->getRatingStyle());
- }
-
- /**
- * Renders the rating images if {@link setReadOnly ReadOnly} is true
- * otherwise render the radio buttons.
- */
- public function renderItem($writer,$repeatInfo,$itemType,$index)
- {
- if($this->getReadOnly())
- $this->renderStaticRating($writer, $repeatInfo, $itemType, $index);
- else
- parent::renderItem($writer, $repeatInfo, $itemType, $index);
- }
-
- /**
- * Renders the static rating images.
- */
- protected function renderStaticRating($writer, $repeatInfo, $itemType, $index)
- {
- $image = new TImage;
- $image->setImageUrl($this->_ratingImages[$this->getRatingImageType($index)]);
- $image->setAlternateText($this->getRating());
- $image->render($writer);
- }
-
- /**
- * @param integer rating image index
- * @return string the rating image corresponding to current index to be rendered.
- */
- protected function getRatingImageType($index)
+ public function setCaption($value)
{
- $rating = floatval($this->getRating());
- $int = intval($rating);
- $limit = $this->getHalfRatingInterval();
- if($index < $int || ($rating < $index+1 && $rating > $index+$limit[1]))
- return 'selected';
- if($rating >= $index+$limit[0] && $rating <= $index+$limit[1])
- return 'half';
- return 'blank';
+ parent::setCaption($value);
+ // if it's an active control, this should not be needed.
+ $this->callClientFunction('setCaption',$value);
}
/**
diff --git a/framework/Web/UI/WebControls/TRatingList.php b/framework/Web/UI/WebControls/TRatingList.php
index 85e7f66c..aba44fb0 100644
--- a/framework/Web/UI/WebControls/TRatingList.php
+++ b/framework/Web/UI/WebControls/TRatingList.php
@@ -4,7 +4,7 @@
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
* @link http://www.pradosoft.com/
- * @copyright Copyright &copy; 2005-2008 PradoSoft
+ * @copyright Copyright &copy; 2005-2008 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Id$
* @package System.Web.UI.WebControls
@@ -21,191 +21,320 @@ Prado::using('System.Web.UI.WebControls.TRadioButtonList');
* This class is EXPERIMENTAL.
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @author Bradley Booms <bradley[dot]booms[at]gmail[dot]com>
* @version $Id$
* @package System.Web.UI.WebControls
* @since 3.0
*/
class TRatingList extends TRadioButtonList
{
+ /**
+ * Script path relative to the TClientScriptManager::SCRIPT_PATH
+ */
const SCRIPT_PATH='prado/ratings';
+ /**
+ * @var array list of published rating images.
+ */
private $_ratingImages = array();
+ /**
+ * Sets the default repeat direction to horizontal.
+ */
public function __construct()
{
parent::__construct();
- $this->getRepeatInfo()->setRepeatDirection('Horizontal');
+ $this->setRepeatDirection(TRepeatDirection::Horizontal);
}
+ /**
+ * @return boolean whether the items in the column can be edited. Defaults to false.
+ */
+ public function getReadOnly()
+ {
+ return $this->getViewState('ReadOnly',false);
+ }
+
+ /**
+ * @param boolean whether the items in the column can be edited
+ */
+ public function setReadOnly($value)
+ {
+ $this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
+ }
+
+ /**
+ * Wrapper for {@link setReadOnly ReadOnly} property.
+ * @return boolean whether the rating list can be edited. Defaults to true.
+ */
public function getAllowInput()
{
- return $this->getViewState('AllowInput', true);
+ return !$this->getReadOnly();
}
+ /**
+ * Wrapper for {@link setReadOnly ReadOnly} property.
+ * @param boolean whether the rating list can be edited
+ */
public function setAllowInput($value)
{
- $this->setViewState('AllowInput', TPropertyValue::ensureBoolean($value), true);
+ $this->setReadOnly(!TPropertyValue::ensureBoolean($value));
}
- public function getRating()
+ /**
+ * Wrapper for {@link setReadOnly ReadOnly} property.
+ * @param boolean whether the rating list can be edited
+ */
+ public function setEnabled($value)
{
- if($this->getAllowInput())
- return $this->getSelectedIndex();
- else
- return $this->getViewState('Rating',0);
+ $this->setReadOnly(!TPropertyValue::ensureBoolean($value));
}
- public function setRating($value)
+ /**
+ * The repeat layout must be Table.
+ * @param string repeat layout type
+ * @throws TInvaliddataValueException when repeat layout is not Table.
+ */
+ public function setRepeatLayout($value)
{
- if($this->getAllowInput())
- $this->setSelectedIndex($value);
+ if($value!==TRepeatLayout::Table)
+ throw new TInvalidDataValueException('ratinglist_table_layout_only');
else
- $this->setViewState('Rating', TPropertyValue::ensureFloat($value),0);
+ parent::setRepeatLayout($value);
}
/**
- * @param string set the rating style
+ * @return float rating value.
*/
- public function setRatingStyle($value)
+ public function getRating()
{
- $this->setViewState('RatingStyle', $value, 'default');
+ $rating = $this->getViewState('Rating', null);
+ if ($rating === null)
+ return $this->getSelectedIndex()+1;
+ else
+ return $rating;
}
/**
- * @return TRatingListStyle current rating style
+ * @param float rating value, also sets the selected Index
*/
- public function getRatingStyle()
+ public function setRating($value)
{
- return $this->getViewState('RatingStyle', 'default');
+ $value = TPropertyValue::ensureFloat($value);
+ $this->setViewState('Rating', $value, null);
+ $index = $this->getRatingIndex($value);
+ parent::setSelectedIndex($index);
+ }
+
+ public function setSelectedIndex($value)
+ {
+ $this->setRating($value+1);
+ parent::setSelectedIndex($value);
}
/**
- * @return string caption text. Default is "Rate It:".
+ * @param float rating value
+ * @return int rating as integer
*/
- public function getCaption()
+ protected function getRatingIndex($rating)
{
- return $this->getViewState('Caption', 'Rate It:');
+ $interval = $this->getHalfRatingInterval();
+ $base = intval($rating)-1;
+ $remainder = $rating-$base-1;
+ return $remainder > $interval[1] ? $base+1 : $base;
}
/**
- * @param string caption text
+ * @param int change the rating selection index
*/
- public function setCaption($value)
+ public function onSelectedIndexChanged($param)
{
- $this->setViewState('Caption', $value, 'Rate It:');
+ $value = $this->getRating();
+ $value = TPropertyValue::ensureInteger($value);
+ $this->setRating($value);
+ parent::onSelectedIndexChanged($param);
}
+ /**
+ * @return string control or html element ID for displaying a caption.
+ */
+ public function getCaptionID()
+ {
+ return $this->getViewState('CaptionID', '');
+ }
- public function setHalfRatingLimit($value)
+ /**
+ * @param string control or html element ID for displaying a caption.
+ */
+ public function setCaptionID($value)
{
- $this->setViewState('HalfRating',
- TPropertyValue::ensureArray($value), array(0.3, 0.7));
+ $this->setViewState('CaptionID', $value, '');
}
- public function getHalfRatingLimit()
+ protected function getCaptionControl()
{
- return $this->getViewState('HalfRating', array(0.3, 0.7));
+ if(($id=$this->getCaptionID())!=='')
+ {
+ if($control=$this->getParent()->findControl($id))
+ return $control;
+ }
+ throw new TInvalidDataValueException(
+ 'ratinglist_invalid_caption_id',$id,$this->getID());
}
/**
- * @param string asset file in the self::SCRIPT_PATH directory.
- * @return string asset file url.
+ * @return string caption text. Default is "Rate It:".
*/
- protected function getAssetUrl($file='')
+ public function getCaption()
{
- $base = $this->getPage()->getClientScript()->getPradoScriptAssetUrl();
- return $base.'/'.self::SCRIPT_PATH.'/'.$file;
+ return $this->getCaptionControl()->getText();
}
- public function getRatingClientOptions()
- {
- $options['cssClass'] = 'TRatingList_'.$this->getRatingStyle();
- $options['ID'] = $this->getClientID();
- $options['caption'] = $this->getCaption();
- $options['field'] = $this->getUniqueID();
- $options['selectedIndex'] = $this->getSelectedIndex();
+ /**
+ * @return TRatingListStyle current rating style
+ */
+ public function setCaption($value)
+ {
+ $this->getCaptionControl()->setText($value);
+ }
+
+ /**
+ * @param string set the rating style, default is "default"
+ */
+ public function setRatingStyle($value)
+ {
+ $this->setViewState('RatingStyle', $value, 'default');
+ }
+
+ /**
+ * @return TRatingListStyle current rating style
+ */
+ public function getRatingStyle()
+ {
+ return $this->getViewState('RatingStyle', 'default');
+ }
+
+ /**
+ * @return string rating style css class name.
+ */
+ protected function getRatingStyleCssClass()
+ {
+ return 'TRatingList_'.$this->getRatingStyle();
+ }
+
+ /**
+ * Sets the interval such that those rating values within the interval
+ * will be considered as a half star rating.
+ * @param array rating display half value interval, default is array(0.3, 0.7);
+ */
+ public function setHalfRatingInterval($value)
+ {
+ $this->setViewState('HalfRating',
+ TPropertyValue::ensureArray($value), array(0.3, 0.7));
+ }
+
+ /**
+ * @return array rating display half value interval, default is array(0.3, 0.7);
+ */
+ public function getHalfRatingInterval()
+ {
+ return $this->getViewState('HalfRating', array(0.3, 0.7));
+ }
+
+ /**
+ * @return array list of post back options.
+ */
+ protected function getPostBackOptions()
+ {
+ $options = parent::getPostBackOptions();
+ $options['AutoPostBack'] = $this->getAutoPostBack();
+ $options['ReadOnly'] = $this->getReadOnly();
+ $options['Style'] = $this->getRatingStyleCssClass();
+ $options['CaptionID'] = $this->getCaptionControlID();
+ $options['SelectedIndex'] = $this->getSelectedIndex();
+ $options['Rating'] = $this->getRating();
+ $options['HalfRating'] = $this->getHalfRatingInterval();
return $options;
- }
+ }
- protected function publishRatingListStyle($style)
- {
+ /**
+ * @return string find the client ID of the caption control.
+ */
+ protected function getCaptionControlID()
+ {
+ if(($id=$this->getCaptionID())!=='')
+ {
+ if($control=$this->getParent()->findControl($id))
+ {
+ if($control->getVisible(true))
+ return $control->getClientID();
+ }
+ else
+ return $id;
+ }
+ return '';
+ }
+
+ /**
+ * Publish the the rating style css file and rating image files.
+ */
+ public function onPreRender($param)
+ {
+ parent::onPreRender($param);
+ $this->publishStyle($this->getRatingStyle());
+ $this->_ratingImages = $this->publishImages($this->getRatingStyle());
+ }
+
+ /**
+ * @param string rating style name
+ * @return string URL of the css style file
+ */
+ protected function publishStyle($style)
+ {
$cs = $this->getPage()->getClientScript();
$url = $this->getAssetUrl($style.'.css');
if(!$cs->isStyleSheetFileRegistered($url))
$cs->registerStyleSheetFile($url, $url);
return $url;
- }
+ }
- protected function publishRatingListImages($style, $fileExt='.gif')
- {
- $images = array('blank', 'hover', 'selected', 'half');
+ /**
+ * @param string rating style name
+ * @param string rating image file extension, default is '.gif'
+ * @return array URL of publish the rating images
+ */
+ protected function publishImages($style, $fileExt='.gif')
+ {
+ $types = array('blank', 'selected', 'half', 'combined');
$files = array();
- foreach($images as $type)
+ foreach($types as $type)
$files[$type] = $this->getAssetUrl("{$style}_{$type}{$fileExt}");
return $files;
- }
+ }
/**
- * @param THtmlWriter writer
+ * @param string asset file in the self::SCRIPT_PATH directory.
+ * @return string asset file url.
*/
- public function onPreRender($param)
- {
- parent::onPreRender($param);
-
- $this->publishRatingListStyle($this->getRatingStyle());
- $this->_ratingImages = $this->publishRatingListImages($this->getRatingStyle());
-
- if($this->getAllowInput())
- $this->registerRatingListClientScript();
- else
- {
- $this->getRepeatInfo()->setCaption($this->getCaption());
- $this->setAttribute('title', $this->getRating());
- }
- }
-
- protected function registerRatingListClientScript()
- {
- $id = $this->getClientID();
- $scripts = $this->getPage()->getClientScript();
- $scripts->registerPradoScript('prado');
- $options = TJavaScript::encode($this->getRatingClientOptions());
- $code = "new Prado.WebUI.TRatingList($options);";
- $scripts->registerEndScript("prado:$id", $code);
- }
-
- public function renderItem($writer,$repeatInfo,$itemType,$index)
- {
- if($this->getAllowInput())
- parent::renderItem($writer, $repeatInfo, $itemType, $index);
- else
- $this->renderRatingListItem($writer, $repeatInfo, $itemType, $index);
- }
-
- protected function renderRatingListItem($writer, $repeatInfo, $itemType, $index)
- {
- $image = new TImage;
- $image->setImageUrl($this->_ratingImages[$this->getRatingImageType($index)]);
- $image->setAlternateText($this->getRating());
- $image->render($writer);
- }
-
- protected function getRatingImageType($index)
- {
- $rating = floatval($this->getRating());
- $int = intval($rating);
- $limit = $this->getHalfRatingLimit();
- if($index < $int || ($rating < $index + 1 && $rating > $index +$limit[1]))
- return 'selected';
- if($rating >= $index+$limit[0] && $rating <= $index+$limit[1])
- return 'half';
- return 'blank';
- }
+ protected function getAssetUrl($file='')
+ {
+ $base = $this->getPage()->getClientScript()->getPradoScriptAssetUrl();
+ return $base.'/'.self::SCRIPT_PATH.'/'.$file;
+ }
- public function generateItemStyle($itemType,$index)
- {
- return new TStyle;
- }
+ /**
+ * Add rating style class name to the class attribute
+ * when {@link setReadOnly ReadOnly} property is true and when the
+ * {@link setCssClass CssClass} property is empty.
+ * @param THtmlWriter renderer
+ */
+ public function render($writer)
+ {
+ $writer->addAttribute('id',$this->getClientID());
+ $this->getPage()->getClientScript()->registerPostBackControl(
+ $this->getClientClassName(), $this->getPostBackOptions());
+ parent::render($writer);
+ }
/**
* Gets the name of the javascript class responsible for performing postback for this control.