1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\FormElements; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\TextField; |
6
|
|
|
use SilverStripe\View\Requirements; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Autocomplete |
10
|
|
|
*/ |
11
|
|
|
class BsAutocompleteField extends TextField implements AjaxPoweredField |
12
|
|
|
{ |
13
|
|
|
use BaseElement; |
14
|
|
|
use Autocompleter; |
|
|
|
|
15
|
|
|
use AttributesHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @config |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private static $default_config = []; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @config |
25
|
|
|
* @var boolean |
26
|
|
|
*/ |
27
|
|
|
private static $enable_requirements = true; |
|
|
|
|
28
|
|
|
|
29
|
|
|
public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($name, $title, $value, $maxLength, $form); |
32
|
|
|
$this->mergeDefaultConfig(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function Type() |
36
|
|
|
{ |
37
|
|
|
return 'bsautocomplete'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function extraClass() |
41
|
|
|
{ |
42
|
|
|
return 'text ' . parent::extraClass(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getServerVars() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'queryParam' => 'query', |
49
|
|
|
'dataKey' => 'data', |
50
|
|
|
'valueField' => 'value', |
51
|
|
|
'labelField' => 'label', |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function Field($properties = array()) |
56
|
|
|
{ |
57
|
|
|
return $this->wrapInElement('bs-autocomplete', $properties); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function requirements() |
61
|
|
|
{ |
62
|
|
|
Requirements::javascript("lekoala/silverstripe-form-elements: client/custom-elements/bs-autocomplete.min.js"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getAjax() |
66
|
|
|
{ |
67
|
|
|
return $this->getConfig('server'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setAjax($url, $opts = []) |
71
|
|
|
{ |
72
|
|
|
$this->setConfig('server', $url); |
73
|
|
|
$this->setConfig('serverParams', [ |
74
|
|
|
'SecurityID' => $this->getForm()?->getSecurityToken()->getValue() |
75
|
|
|
]); |
76
|
|
|
$this->setConfig('liveServer', true); |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
|
|
public function isAjax() |
84
|
|
|
{ |
85
|
|
|
return $this->ajaxClass || $this->getConfig('server'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|