1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web\fields; |
3
|
|
|
|
4
|
|
|
use rtens\domin\delivery\web\Element; |
5
|
|
|
use rtens\domin\delivery\web\HeadElements; |
6
|
|
|
use rtens\domin\delivery\web\WebField; |
7
|
|
|
use rtens\domin\Parameter; |
8
|
|
|
|
9
|
|
|
abstract class AutoCompleteField implements WebField { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param Parameter $parameter |
13
|
|
|
* @return array With captions indexed by values |
14
|
|
|
*/ |
15
|
|
|
protected abstract function getOptions(Parameter $parameter); |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Parameter $parameter |
19
|
|
|
* @param array $serialized |
20
|
|
|
* @return mixed |
21
|
|
|
*/ |
22
|
|
|
public function inflate(Parameter $parameter, $serialized) { |
23
|
|
|
return $serialized; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Parameter $parameter |
28
|
|
|
* @param mixed $value |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function render(Parameter $parameter, $value) { |
32
|
|
|
return $this->renderComboBox($parameter, $value); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function renderComboBox(Parameter $parameter, $value, $fieldNameSuffix = '', $options = []) { |
36
|
|
|
$id = str_replace(['[', ']'], '-', $parameter->getName()); |
37
|
|
|
|
38
|
|
|
$optionsString = json_encode($options); |
39
|
|
|
$children = $this->renderOptions($parameter, (string)$value); |
40
|
|
|
$children[] = new Element('script', [], [" |
41
|
|
|
$(function(){ |
42
|
|
|
$('#$id').combobox($optionsString); |
43
|
|
|
}); |
44
|
|
|
"]); |
45
|
|
|
|
46
|
|
|
return (string)new Element('select', [ |
47
|
|
|
'name' => $parameter->getName() . $fieldNameSuffix, |
48
|
|
|
'id' => $id, |
49
|
|
|
'class' => 'form-control combobox' |
50
|
|
|
], $children); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function renderOptions(Parameter $parameter, $value) { |
54
|
|
|
$options = [new Element('option', [], [])]; |
55
|
|
|
foreach ($this->getOptions($parameter) as $key => $caption) { |
56
|
|
|
$options[] = new Element('option', array_merge([ |
57
|
|
|
'value' => $key |
58
|
|
|
], (string)$key === (string)$value ? [ |
59
|
|
|
'selected' => 'selected' |
60
|
|
|
] : []), [ |
61
|
|
|
$caption |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
return $options; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Parameter $parameter |
69
|
|
|
* @return array|\rtens\domin\delivery\web\Element[] |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function headElements(Parameter $parameter) { |
|
|
|
|
72
|
|
|
return [ |
73
|
|
|
HeadElements::jquery(), |
74
|
|
|
HeadElements::style('//cdn.rawgit.com/rtens/bootstrap-combobox/cffe84e7/css/bootstrap-combobox.css'), |
75
|
|
|
HeadElements::script('//cdn.rawgit.com/rtens/bootstrap-combobox/cffe84e7/js/bootstrap-combobox.js'), |
76
|
|
|
new Element('style', [], ['.typeahead-long { width:100% }']) |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
} |