/** * @inheritdoc */ public function run() { $inputId = $this->options['id']; $hasModel = $this->hasModel(); if (array_key_exists('value', $this->options)) { $value = $this->options['value']; } elseif ($hasModel) { $value = Html::getAttributeValue($this->model, $this->attribute); } else { $value = $this->value; } $options = array_merge($this->options, ['multiple' => true, 'value' => $value]); if ($hasModel) { $output = Html::activeListBox($this->model, $this->attribute, $this->items, $options); } else { $output = Html::listBox($this->name, $this->value, $this->items, $options); } $clientOptions = array_merge(['filter' => $this->filter, 'multiple' => $this->multiple, 'multipleWidth' => $this->multipleWidth], $this->clientOptions); if (!array_key_exists('placeholder', $clientOptions) && array_key_exists('placeholder', $options)) { $clientOptions['placeholder'] = $options['placeholder']; } $js = 'jQuery(\'#' . $inputId . '\').multipleSelect(' . Json::htmlEncode($clientOptions) . ');'; if (Yii::$app->getRequest()->getIsAjax()) { $output .= Html::script($js); } else { $view = $this->getView(); MultipleSelectAsset::register($view); $view->registerJs($js); } return $output; }
/** * @inheritdoc * @throw NotSupportedException */ public function run() { if ($this->hasModel()) { if (!is_null($this->value)) { if (!in_array($this->attribute, $this->model->attributes())) { throw new NotSupportedException('Unable to set value of the property \'' . $this->attribute . '\'.'); } $stash = $this->model->{$this->attribute}; $this->model->{$this->attribute} = $this->value; } $output = Html::activeListBox($this->model, $this->attribute, $this->items, $this->options); if (isset($stash)) { $this->model->{$this->attribute} = $stash; } } else { $output = Html::listBox($this->name, $this->value, $this->items, $this->options); } $js = 'jQuery(\'#' . $this->options['id'] . '\').multipleSelect(' . Json::htmlEncode($this->clientOptions) . ');'; if (Yii::$app->getRequest()->getIsAjax()) { $output .= Html::script($js); } else { $view = $this->getView(); MultipleSelectAsset::register($view); $view->registerJs($js); } return $output; }
public function testBundle() { $bundle = Yii::$app->getAssetManager()->getBundle(MultipleSelectAsset::className()); $this->assertInstanceOf('yii\\jquery\\multipleselect\\MultipleSelectAsset', $bundle); $this->assertArrayHasKey(0, $bundle->depends); $this->assertEquals('yii\\web\\JqueryAsset', $bundle->depends[0]); $this->assertArrayHasKey(0, $bundle->js); $this->assertFileExists($bundle->basePath . DIRECTORY_SEPARATOR . $bundle->js[0]); $this->assertArrayHasKey(0, $bundle->css); $this->assertFileExists($bundle->basePath . DIRECTORY_SEPARATOR . $bundle->css[0]); }
/** * @param int $mode * @param string $actual * @param string $expectedHtml * @param string $expectedJs */ protected function checkExpected($mode, $actual, $expectedHtml, $expectedJs) { list($id, $name) = $this->getIdName($mode); switch ($mode) { case static::MODE_MODEL_ATTRIBUTE: case static::MODE_MODEL_ATTRIBUTE_VALUE: $expectedHtml = '<input type="hidden" name="' . $name . '" value="">' . $expectedHtml; $expectedHtml = '<div class="form-group field-testform-number">' . "\n" . $expectedHtml . "\n" . '</div>'; case static::MODE_NAME_VALUE: $this->assertEquals($expectedHtml, $actual); $view = Yii::$app->getView(); $this->assertArrayHasKey(MultipleSelectAsset::className(), $view->assetBundles); $this->assertArrayHasKey(View::POS_READY, $view->js); $jsKey = md5($expectedJs); $this->assertArrayHasKey($jsKey, $view->js[View::POS_READY]); $this->assertEquals($expectedJs, $view->js[View::POS_READY][$jsKey]); return; case static::MODE_NAME_VALUE_AJAX: $expectedHtml .= '<script>' . $expectedJs . '</script>'; $this->assertEquals($expectedHtml, $actual); return; case static::MODE_MODEL_ATTRIBUTE_AJAX: case static::MODE_MODEL_ATTRIBUTE_VALUE_AJAX: $expectedHtml .= '<script>' . $expectedJs . '</script>'; $expectedHtml = '<input type="hidden" name="' . $name . '" value="">' . $expectedHtml; $expectedHtml = '<div class="form-group field-testform-number">' . "\n" . $expectedHtml . "\n" . '</div>'; $this->assertEquals($expectedHtml, $actual); return; } throw new Exception(); }