From 90d2a99bf32b9e0f70f07bd2c001d5e03b1cea8c Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 01:43:04 +0400 Subject: [PATCH 01/11] \yii\jui\Widget::registerWidget() improvement --- framework/yii/jui/Widget.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/framework/yii/jui/Widget.php b/framework/yii/jui/Widget.php index eea76d3..8cbd16c 100644 --- a/framework/yii/jui/Widget.php +++ b/framework/yii/jui/Widget.php @@ -58,13 +58,16 @@ class Widget extends \yii\base\Widget /** * Registers a specific jQuery UI widget and the related events * @param string $name the name of the jQuery UI widget + * @param boolean $theme whether register theme bundle */ - protected function registerWidget($name) + protected function registerWidget($name, $theme = true) { $id = $this->options['id']; $view = $this->getView(); $view->registerAssetBundle("yii/jui/$name"); - $view->registerAssetBundle(static::$theme . "/$name"); + if ($theme) { + $view->registerAssetBundle(static::$theme . "/$name"); + } if ($this->clientOptions !== false) { $options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions); From 0fd30598b34c56ef139f8354fc96a0e89bdc99d9 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 01:44:34 +0400 Subject: [PATCH 02/11] jQuery UI sortable widget --- framework/yii/jui/Sortable.php | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 framework/yii/jui/Sortable.php diff --git a/framework/yii/jui/Sortable.php b/framework/yii/jui/Sortable.php new file mode 100644 index 0000000..1b5b26b --- /dev/null +++ b/framework/yii/jui/Sortable.php @@ -0,0 +1,63 @@ + array( + * 'cursor' => 'move', + * ), + * 'options' => array( + * 'tag' => 'ul', + * ), + * )); + * + * echo '
  • Item 1
  • '; + * echo '
  • Item 2
  • '; + * echo '
  • Item 3
  • '; + * + * Sortable::end(); + * ``` + * + * @see http://api.jqueryui.com/sortable/ + * @author Alexander Kochetov + * @since 2.0 + */ +class Sortable extends Widget +{ + /** + * Initializes the widget. + */ + public function init() + { + parent::init(); + $options = $this->options; + $tag = isset($options['tag']) ? $options['tag'] : 'div'; + unset($options['tag']); + echo Html::beginTag($tag, $options) . "\n"; + } + + /** + * Renders the widget. + */ + public function run() + { + $options = $this->options; + $tag = isset($options['tag']) ? $options['tag'] : 'div'; + unset($options['tag']); + echo Html::endTag($tag) . "\n"; + $this->registerWidget('sortable', false); + } +} From fad2d4794d02b62db362974f63d427ef67a31d62 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 01:51:57 +0400 Subject: [PATCH 03/11] Refactoring --- framework/yii/jui/Sortable.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/framework/yii/jui/Sortable.php b/framework/yii/jui/Sortable.php index 1b5b26b..f5854dc 100644 --- a/framework/yii/jui/Sortable.php +++ b/framework/yii/jui/Sortable.php @@ -54,10 +54,7 @@ class Sortable extends Widget */ public function run() { - $options = $this->options; - $tag = isset($options['tag']) ? $options['tag'] : 'div'; - unset($options['tag']); - echo Html::endTag($tag) . "\n"; + echo Html::endTag(isset($this->options['tag']) ? $this->options['tag'] : 'div') . "\n"; $this->registerWidget('sortable', false); } } From 408e0edaccbfd7788708337f7756e5299e079192 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 01:54:54 +0400 Subject: [PATCH 04/11] Comment fix --- framework/yii/jui/Sortable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/jui/Sortable.php b/framework/yii/jui/Sortable.php index f5854dc..1d75cc6 100644 --- a/framework/yii/jui/Sortable.php +++ b/framework/yii/jui/Sortable.php @@ -10,7 +10,7 @@ namespace yii\jui; use yii\helpers\Html; /** - * AutoComplete renders a sortable jQuery UI widget. + * Sortable renders a sortable jQuery UI widget. * * For example: * From 95003093c508afe1209adfb236738403ea9dad3d Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 02:11:05 +0400 Subject: [PATCH 05/11] theme => enableTheme --- framework/yii/jui/Widget.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/yii/jui/Widget.php b/framework/yii/jui/Widget.php index 8cbd16c..063b1b9 100644 --- a/framework/yii/jui/Widget.php +++ b/framework/yii/jui/Widget.php @@ -58,14 +58,14 @@ class Widget extends \yii\base\Widget /** * Registers a specific jQuery UI widget and the related events * @param string $name the name of the jQuery UI widget - * @param boolean $theme whether register theme bundle + * @param boolean $enableTheme whether register theme bundle */ - protected function registerWidget($name, $theme = true) + protected function registerWidget($name, $enableTheme = true) { $id = $this->options['id']; $view = $this->getView(); $view->registerAssetBundle("yii/jui/$name"); - if ($theme) { + if ($enableTheme) { $view->registerAssetBundle(static::$theme . "/$name"); } From d2403ebf6d6328b743601edc21af9a7add2cd2ce Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 02:40:01 +0400 Subject: [PATCH 06/11] enableTheme => registerTheme --- framework/yii/jui/Widget.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/yii/jui/Widget.php b/framework/yii/jui/Widget.php index 063b1b9..d34a8bd 100644 --- a/framework/yii/jui/Widget.php +++ b/framework/yii/jui/Widget.php @@ -58,14 +58,14 @@ class Widget extends \yii\base\Widget /** * Registers a specific jQuery UI widget and the related events * @param string $name the name of the jQuery UI widget - * @param boolean $enableTheme whether register theme bundle + * @param boolean $registerTheme whether register theme bundle */ - protected function registerWidget($name, $enableTheme = true) + protected function registerWidget($name, $registerTheme = true) { $id = $this->options['id']; $view = $this->getView(); $view->registerAssetBundle("yii/jui/$name"); - if ($enableTheme) { + if ($registerTheme) { $view->registerAssetBundle(static::$theme . "/$name"); } From 489633b38216ff71427d8613b0fe9ca446b24ddc Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 02:43:24 +0400 Subject: [PATCH 07/11] Introducing items --- framework/yii/jui/Sortable.php | 45 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/framework/yii/jui/Sortable.php b/framework/yii/jui/Sortable.php index 1d75cc6..2263461 100644 --- a/framework/yii/jui/Sortable.php +++ b/framework/yii/jui/Sortable.php @@ -15,18 +15,34 @@ use yii\helpers\Html; * For example: * * ```php + * echo Sortable::widget(array( + * 'items' => array( + * '
  • Item 1
  • ', + * '
  • Item 2
  • ', + * '
  • Item 3
  • ', + * ), + * 'clientOptions' => array( + * 'cursor' => 'move', + * ), + * )); + * ``` + * + * The following example will show the content enclosed between the [[begin()]] + * and [[end()]] calls within the sortable widget: + * + * ```php * Sortable::begin(array( * 'clientOptions' => array( * 'cursor' => 'move', * ), * 'options' => array( - * 'tag' => 'ul', + * 'tag' => 'div', * ), * )); * - * echo '
  • Item 1
  • '; - * echo '
  • Item 2
  • '; - * echo '
  • Item 3
  • '; + * echo '
    Item 1
    '; + * echo '
    Item 2
    '; + * echo '
    Item 3
    '; * * Sortable::end(); * ``` @@ -38,13 +54,20 @@ use yii\helpers\Html; class Sortable extends Widget { /** + * @var array list of sortable containers. Each array element represents a single + * sortable container. + */ + public $items = array(); + + + /** * Initializes the widget. */ public function init() { parent::init(); $options = $this->options; - $tag = isset($options['tag']) ? $options['tag'] : 'div'; + $tag = isset($options['tag']) ? $options['tag'] : 'ul'; unset($options['tag']); echo Html::beginTag($tag, $options) . "\n"; } @@ -54,7 +77,17 @@ class Sortable extends Widget */ public function run() { - echo Html::endTag(isset($this->options['tag']) ? $this->options['tag'] : 'div') . "\n"; + echo $this->renderItems() . "\n"; + echo Html::endTag(isset($this->options['tag']) ? $this->options['tag'] : 'ul') . "\n"; $this->registerWidget('sortable', false); } + + /** + * Renders sortable items as specified on [[items]]. + * @return string the rendering result + */ + public function renderItems() + { + return implode("\n", $this->items); + } } From a834e1049da7cf60dd22a494ab7b2bf5fe36af90 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 17:52:02 +0400 Subject: [PATCH 08/11] Sortable rework --- framework/yii/jui/Sortable.php | 60 +++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/framework/yii/jui/Sortable.php b/framework/yii/jui/Sortable.php index 2263461..67213ca 100644 --- a/framework/yii/jui/Sortable.php +++ b/framework/yii/jui/Sortable.php @@ -7,6 +7,8 @@ namespace yii\jui; +use yii\base\InvalidConfigException; +use yii\helpers\ArrayHelper; use yii\helpers\Html; /** @@ -17,9 +19,9 @@ use yii\helpers\Html; * ```php * echo Sortable::widget(array( * 'items' => array( - * '
  • Item 1
  • ', - * '
  • Item 2
  • ', - * '
  • Item 3
  • ', + * 'Item 1', + * 'Item 2', + * 'Item 3', * ), * 'clientOptions' => array( * 'cursor' => 'move', @@ -32,17 +34,20 @@ use yii\helpers\Html; * * ```php * Sortable::begin(array( - * 'clientOptions' => array( - * 'cursor' => 'move', - * ), * 'options' => array( * 'tag' => 'div', * ), + * 'itemOptions' => array( + * 'tag' => 'div', + * ), + * 'clientOptions' => array( + * 'cursor' => 'move', + * ), * )); * - * echo '
    Item 1
    '; - * echo '
    Item 2
    '; - * echo '
    Item 3
    '; + * echo 'Item 1'; + * echo 'Item 2'; + * echo 'Item 3'; * * Sortable::end(); * ``` @@ -54,10 +59,15 @@ use yii\helpers\Html; class Sortable extends Widget { /** - * @var array list of sortable containers. Each array element represents a single - * sortable container. + * @var array. + * @todo comments */ public $items = array(); + /** + * @var array. + * @todo comments + */ + public $itemOptions = array(); /** @@ -67,8 +77,7 @@ class Sortable extends Widget { parent::init(); $options = $this->options; - $tag = isset($options['tag']) ? $options['tag'] : 'ul'; - unset($options['tag']); + $tag = ArrayHelper::remove($options, 'tag', 'ul'); echo Html::beginTag($tag, $options) . "\n"; } @@ -78,16 +87,35 @@ class Sortable extends Widget public function run() { echo $this->renderItems() . "\n"; - echo Html::endTag(isset($this->options['tag']) ? $this->options['tag'] : 'ul') . "\n"; + $tag = ArrayHelper::getValue($this->options, 'tag', 'ul'); + echo Html::endTag($tag) . "\n"; $this->registerWidget('sortable', false); } /** * Renders sortable items as specified on [[items]]. - * @return string the rendering result + * @return string the rendering result. + * @throws InvalidConfigException. */ public function renderItems() { - return implode("\n", $this->items); + $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."); + } + if (isset($item['options'])) { + $options = array_merge($options, $item['options']); + $tag = ArrayHelper::remove($options, 'tag', $tag); + } + $items[] = Html::tag($tag, $item['content'], $options); + } else { + $items[] = Html::tag($tag, $item, $options); + } + } + return implode("\n", $items); } } From c6076cb3148be19a82671c8a1260b780d5e56ff8 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 18:30:36 +0400 Subject: [PATCH 09/11] More refactoring --- framework/yii/jui/Sortable.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/framework/yii/jui/Sortable.php b/framework/yii/jui/Sortable.php index 67213ca..2ff25f7 100644 --- a/framework/yii/jui/Sortable.php +++ b/framework/yii/jui/Sortable.php @@ -107,10 +107,8 @@ class Sortable extends Widget if (!isset($item['content'])) { throw new InvalidConfigException("The 'content' option is required."); } - if (isset($item['options'])) { - $options = array_merge($options, $item['options']); - $tag = ArrayHelper::remove($options, 'tag', $tag); - } + $options = array_merge($options, ArrayHelper::getValue($item, 'options', array())); + $tag = ArrayHelper::remove($options, 'tag', $tag); $items[] = Html::tag($tag, $item['content'], $options); } else { $items[] = Html::tag($tag, $item, $options); From 9a98aa955c4b381cd35648629c9a4791c3ce5a12 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 19:06:28 +0400 Subject: [PATCH 10/11] Removed Sortable::begin() ... Sortable::end() usage. --- framework/yii/jui/Sortable.php | 48 +++++++++++------------------------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/framework/yii/jui/Sortable.php b/framework/yii/jui/Sortable.php index 2ff25f7..bd57fd5 100644 --- a/framework/yii/jui/Sortable.php +++ b/framework/yii/jui/Sortable.php @@ -20,36 +20,23 @@ use yii\helpers\Html; * echo Sortable::widget(array( * 'items' => array( * 'Item 1', - * 'Item 2', - * 'Item 3', + * array( + * 'content' => 'Item2', + * 'options' => array( + * 'tag' => 'li', + * ), + * ), * ), - * 'clientOptions' => array( - * 'cursor' => 'move', - * ), - * )); - * ``` - * - * The following example will show the content enclosed between the [[begin()]] - * and [[end()]] calls within the sortable widget: - * - * ```php - * Sortable::begin(array( * 'options' => array( - * 'tag' => 'div', + * 'tag' => 'ul', * ), * 'itemOptions' => array( - * 'tag' => 'div', + * 'tag' => 'li', * ), * 'clientOptions' => array( * 'cursor' => 'move', * ), * )); - * - * echo 'Item 1'; - * echo 'Item 2'; - * echo 'Item 3'; - * - * Sortable::end(); * ``` * * @see http://api.jqueryui.com/sortable/ @@ -59,35 +46,24 @@ use yii\helpers\Html; class Sortable extends Widget { /** - * @var array. - * @todo comments + * @var array list of sortable containers. */ public $items = array(); /** - * @var array. - * @todo comments + * @var array list of individual sortable container default options. */ public $itemOptions = array(); /** - * Initializes the widget. + * Renders the widget. */ - public function init() + public function run() { - parent::init(); $options = $this->options; $tag = ArrayHelper::remove($options, 'tag', 'ul'); echo Html::beginTag($tag, $options) . "\n"; - } - - /** - * Renders the widget. - */ - public function run() - { echo $this->renderItems() . "\n"; - $tag = ArrayHelper::getValue($this->options, 'tag', 'ul'); echo Html::endTag($tag) . "\n"; $this->registerWidget('sortable', false); } From 2a7dae5bd92d673028be5e0644e61e374ad7a0ae Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 19:17:46 +0400 Subject: [PATCH 11/11] Example from comments changed --- framework/yii/jui/Sortable.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/framework/yii/jui/Sortable.php b/framework/yii/jui/Sortable.php index bd57fd5..173b742 100644 --- a/framework/yii/jui/Sortable.php +++ b/framework/yii/jui/Sortable.php @@ -22,6 +22,9 @@ use yii\helpers\Html; * 'Item 1', * array( * 'content' => 'Item2', + * ), + * array( + * 'content' => 'Item3', * 'options' => array( * 'tag' => 'li', * ),