CJuiSelectable.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * CJuiSelectable class file.
  4. *
  5. * @author Sebastian Thierer <sebathi@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('zii.widgets.jui.CJuiWidget');
  11. /**
  12. * CJuiSelectable displays an accordion widget.
  13. *
  14. * CJuiSelectable encapsulates the {@link http://jqueryui.com/selectable/ JUI Selectable}
  15. * plugin.
  16. *
  17. * To use this widget, you may insert the following code in a view:
  18. * <pre>
  19. * $this->widget('zii.widgets.jui.CJuiSelectable',array(
  20. * 'items'=>array(
  21. * 'id1'=>'Item 1',
  22. * 'id2'=>'Item 2',
  23. * 'id3'=>'Item 3',
  24. * ),
  25. * // additional javascript options for the selectable plugin
  26. * 'options'=>array(
  27. * 'delay'=>'300',
  28. * ),
  29. * ));
  30. * </pre>
  31. *
  32. * By configuring the {@link options} property, you may specify the options
  33. * that need to be passed to the JUI Selectable plugin. Please refer to
  34. * the {@link http://api.jqueryui.com/selectable/ JUI Selectable API}
  35. * documentation for possible options (name-value pairs) and
  36. * {@link http://jqueryui.com/selectable/ JUI Selectable page} for general
  37. * description and demo.
  38. *
  39. * @author Sebastian Thierer <sebathi@gmail.com>
  40. * @package zii.widgets.jui
  41. * @since 1.1
  42. */
  43. class CJuiSelectable extends CJuiWidget {
  44. /**
  45. * @var array list of selectable items (id=>item content).
  46. * Note that the item contents will not be HTML-encoded.
  47. */
  48. public $items=array();
  49. /**
  50. * @var string the name of the container element that contains all items. Defaults to 'ol'.
  51. */
  52. public $tagName='ol';
  53. /**
  54. * @var string the template that is used to generated every selectable item.
  55. * The token "{content}" in the template will be replaced with the item content,
  56. * while "{id}" will be replaced with the item ID.
  57. */
  58. public $itemTemplate='<li id="{id}">{content}</li>';
  59. /**
  60. * Run this widget.
  61. * This method registers necessary javascript and renders the needed HTML code.
  62. */
  63. public function run()
  64. {
  65. $id=$this->getId();
  66. if(isset($this->htmlOptions['id']))
  67. $id=$this->htmlOptions['id'];
  68. else
  69. $this->htmlOptions['id']=$id;
  70. $options=CJavaScript::encode($this->options);
  71. Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').selectable({$options});");
  72. echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
  73. foreach($this->items as $id=>$content)
  74. echo strtr($this->itemTemplate,array('{id}'=>$id,'{content}'=>$content))."\n";
  75. echo CHtml::closeTag($this->tagName);
  76. }
  77. }