CJuiSortable.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * CJuiSortable 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. * CJuiSortable makes selected elements sortable by dragging with the mouse.
  13. *
  14. * CJuiSortable encapsulates the {@link http://jqueryui.com/sortable/ JUI Sortable}
  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.CJuiSortable',array(
  20. * 'items'=>array(
  21. * 'id1'=>'Item 1',
  22. * 'id2'=>'Item 2',
  23. * 'id3'=>'Item 3',
  24. * ),
  25. * // additional javascript options for the JUI Sortable 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 Sortable plugin. Please refer to
  34. * the {@link http://api.jqueryui.com/sortable/ JUI Sortable API} documentation
  35. * for possible options (name-value pairs) and
  36. * {@link http://jqueryui.com/sortable/ JUI Sortable page} for general
  37. * description and demo.
  38. *
  39. * If you are using JavaScript expressions anywhere in the code, please wrap it
  40. * with {@link CJavaScriptExpression} and Yii will use it as code.
  41. *
  42. * @author Sebastian Thierer <sebathi@gmail.com>
  43. * @package zii.widgets.jui
  44. * @since 1.1
  45. */
  46. class CJuiSortable extends CJuiWidget
  47. {
  48. /**
  49. * @var array list of sortable items (id=>item content).
  50. * Note that the item contents will not be HTML-encoded.
  51. */
  52. public $items=array();
  53. /**
  54. * @var string the name of the container element that contains all items. Defaults to 'ul'.
  55. */
  56. public $tagName='ul';
  57. /**
  58. * @var string the template that is used to generated every sortable item.
  59. * The token "{content}" in the template will be replaced with the item content,
  60. * while "{id}" be replaced with the item ID.
  61. */
  62. public $itemTemplate='<li id="{id}">{content}</li>';
  63. /**
  64. * Run this widget.
  65. * This method registers necessary javascript and renders the needed HTML code.
  66. */
  67. public function run()
  68. {
  69. $id=$this->getId();
  70. if(isset($this->htmlOptions['id']))
  71. $id=$this->htmlOptions['id'];
  72. else
  73. $this->htmlOptions['id']=$id;
  74. $options=CJavaScript::encode($this->options);
  75. Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').sortable({$options});");
  76. echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
  77. foreach($this->items as $id=>$content)
  78. echo strtr($this->itemTemplate,array('{id}'=>$id,'{content}'=>$content))."\n";
  79. echo CHtml::closeTag($this->tagName);
  80. }
  81. }