|
@@ -37,7 +37,12 @@ class Select extends Presenter
|
|
|
/**
|
|
|
* @var string
|
|
|
*/
|
|
|
- protected $script = '';
|
|
|
+ protected $script;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $placeholder;
|
|
|
|
|
|
/**
|
|
|
* @var bool
|
|
@@ -103,25 +108,19 @@ class Select extends Presenter
|
|
|
}
|
|
|
|
|
|
if (empty($this->script)) {
|
|
|
- $placeholder = json_encode([
|
|
|
- 'id' => '',
|
|
|
- 'text' => trans('admin.choose'),
|
|
|
- ]);
|
|
|
-
|
|
|
$configs = array_merge([
|
|
|
- 'allowClear' => true,
|
|
|
+ 'allowClear' => true,
|
|
|
+ 'placeholder' => [
|
|
|
+ 'id' => '',
|
|
|
+ 'text' => $this->placeholder(),
|
|
|
+ ],
|
|
|
], $this->config);
|
|
|
|
|
|
$configs = json_encode($configs);
|
|
|
- $configs = substr($configs, 1, strlen($configs) - 2);
|
|
|
|
|
|
- $this->script = <<<SCRIPT
|
|
|
-$(".{$this->getElementClass()}").select2({
|
|
|
- placeholder: $placeholder,
|
|
|
- $configs
|
|
|
-});
|
|
|
-
|
|
|
-SCRIPT;
|
|
|
+ $this->script = <<<JS
|
|
|
+$(".{$this->getElementClass()}").select2($configs);
|
|
|
+JS;
|
|
|
}
|
|
|
|
|
|
Admin::script($this->script);
|
|
@@ -138,7 +137,7 @@ SCRIPT;
|
|
|
*
|
|
|
* @return $this
|
|
|
*/
|
|
|
- public function model($model, $idField = 'id', $textField = 'name')
|
|
|
+ public function model($model, string $idField = 'id', string $textField = 'name')
|
|
|
{
|
|
|
if (! class_exists($model)
|
|
|
|| ! in_array(Model::class, class_parents($model))
|
|
@@ -178,7 +177,7 @@ SCRIPT;
|
|
|
*
|
|
|
* @return $this
|
|
|
*/
|
|
|
- protected function loadRemoteOptions($url, $parameters = [], $options = [])
|
|
|
+ protected function loadRemoteOptions(string $url, array $parameters = [], array $options = [])
|
|
|
{
|
|
|
$ajaxOptions = [
|
|
|
'url' => admin_url($url.'?'.http_build_query($parameters)),
|
|
@@ -187,7 +186,7 @@ SCRIPT;
|
|
|
'allowClear' => true,
|
|
|
'placeholder' => [
|
|
|
'id' => '',
|
|
|
- 'text' => trans('admin.choose'),
|
|
|
+ 'text' => $this->placeholder(),
|
|
|
],
|
|
|
], $this->config);
|
|
|
|
|
@@ -213,6 +212,24 @@ $.ajax($ajaxOptions).done(function(data) {
|
|
|
JS;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Set input placeholder.
|
|
|
+ *
|
|
|
+ * @param string $placeholder
|
|
|
+ *
|
|
|
+ * @return $this|string
|
|
|
+ */
|
|
|
+ public function placeholder(string $placeholder = null)
|
|
|
+ {
|
|
|
+ if ($placeholder === null) {
|
|
|
+ return $this->placeholder ?: __('admin.choose');
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->placeholder = $placeholder;
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Load options from ajax.
|
|
|
*
|
|
@@ -220,11 +237,11 @@ JS;
|
|
|
* @param $idField
|
|
|
* @param $textField
|
|
|
*/
|
|
|
- public function ajax($resourceUrl, $idField = 'id', $textField = 'text')
|
|
|
+ public function ajax(string $resourceUrl, string $idField = 'id', string $textField = 'text')
|
|
|
{
|
|
|
$configs = array_merge([
|
|
|
'allowClear' => true,
|
|
|
- 'placeholder' => trans('admin.choose'),
|
|
|
+ 'placeholder' => $this->placeholder(),
|
|
|
'minimumInputLength' => 1,
|
|
|
], $this->config);
|
|
|
|
|
@@ -302,7 +319,7 @@ JS;
|
|
|
*
|
|
|
* @return $this
|
|
|
*/
|
|
|
- public function load($target, $resourceUrl, $idField = 'id', $textField = 'text'): self
|
|
|
+ public function load($target, string $resourceUrl, string $idField = 'id', string $textField = 'text'): self
|
|
|
{
|
|
|
$class = $this->getElementClass();
|
|
|
|