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