Sortable.php 2.96 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\jui;

Alexander Kochetov committed
10 11
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
12 13 14
use yii\helpers\Html;

/**
Alexander Kochetov committed
15
 * Sortable renders a sortable jQuery UI widget.
16 17 18 19
 *
 * For example:
 *
 * ```php
Alexander Kochetov committed
20 21
 * echo Sortable::widget(array(
 *     'items' => array(
Alexander Kochetov committed
22
 *         'Item 1',
23 24
 *         array(
 *             'content' => 'Item2',
25 26 27
 *         ),
 *         array(
 *             'content' => 'Item3',
28 29 30 31
 *             'options' => array(
 *                 'tag' => 'li',
 *             ),
 *         ),
Alexander Kochetov committed
32
 *     ),
33
 *     'options' => array(
34
 *         'tag' => 'ul',
35
 *     ),
Alexander Kochetov committed
36
 *     'itemOptions' => array(
37
 *         'tag' => 'li',
Alexander Kochetov committed
38 39 40 41
 *     ),
 *     'clientOptions' => array(
 *         'cursor' => 'move',
 *     ),
42 43 44 45 46 47 48 49 50
 * ));
 * ```
 *
 * @see http://api.jqueryui.com/sortable/
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
class Sortable extends Widget
{
Alexander Kochetov committed
51
	/**
Qiang Xue committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	 * @var array the HTML attributes for the widget container tag. The following special options are recognized:
	 *
	 * - tag: string, defaults to "ul", the tag name of the container tag of this widget
	 */
	public $options = array();
	/**
	 * @var array list of sortable items. Each item can be a string representing the item content
	 * or an array of the following structure:
	 *
	 * ~~~
	 * array(
	 *     'content' => 'item content',
	 *     // the HTML attributes of the item container tag. This will overwrite "itemOptions".
	 *     'options' => array(),
	 * )
	 * ~~~
Alexander Kochetov committed
68 69
	 */
	public $items = array();
Alexander Kochetov committed
70
	/**
Qiang Xue committed
71 72 73 74
	 * @var array list of HTML attributes for the item container tags. This will be overwritten
	 * by the "options" set in individual [[items]]. The following special options are recognized:
	 *
	 * - tag: string, defaults to "li", the tag name of the item container tags.
Alexander Kochetov committed
75 76
	 */
	public $itemOptions = array();
Alexander Kochetov committed
77 78


79
	/**
80
	 * Renders the widget.
81
	 */
82
	public function run()
83 84
	{
		$options = $this->options;
Alexander Kochetov committed
85
		$tag = ArrayHelper::remove($options, 'tag', 'ul');
86
		echo Html::beginTag($tag, $options) . "\n";
Alexander Kochetov committed
87
		echo $this->renderItems() . "\n";
Alexander Kochetov committed
88
		echo Html::endTag($tag) . "\n";
89 90
		$this->registerWidget('sortable', false);
	}
Alexander Kochetov committed
91 92 93

	/**
	 * Renders sortable items as specified on [[items]].
Alexander Kochetov committed
94 95
	 * @return string the rendering result.
	 * @throws InvalidConfigException.
Alexander Kochetov committed
96 97 98
	 */
	public function renderItems()
	{
Alexander Kochetov committed
99 100 101 102 103 104 105 106
		$items = array();
		foreach ($this->items as $item) {
			$options = $this->itemOptions;
			$tag = ArrayHelper::remove($options, 'tag', 'li');
			if (is_array($item)) {
				if (!isset($item['content'])) {
					throw new InvalidConfigException("The 'content' option is required.");
				}
Alexander Kochetov committed
107 108
				$options = array_merge($options, ArrayHelper::getValue($item, 'options', array()));
				$tag = ArrayHelper::remove($options, 'tag', $tag);
Alexander Kochetov committed
109 110 111 112 113 114
				$items[] = Html::tag($tag, $item['content'], $options);
			} else {
				$items[] = Html::tag($tag, $item, $options);
			}
		}
		return implode("\n", $items);
Alexander Kochetov committed
115
	}
116
}