1
|
|
|
<?php namespace Rocket\UI\Forms\Fields; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Adds the autocomplete logic |
5
|
|
|
* |
6
|
|
|
* @method $this autocomplete(array $config) |
7
|
|
|
*/ |
8
|
|
|
class Autocomplete extends Field |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* {@inheritdoc} |
12
|
|
|
*/ |
13
|
|
|
protected function getDefaults() |
14
|
|
|
{ |
15
|
|
|
return parent::getDefaults() + [ |
16
|
|
|
'autocomplete' => [ |
17
|
|
|
'multiple' => false, |
18
|
|
|
'length' => 2, |
19
|
|
|
], |
20
|
|
|
]; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Adds some attributes |
25
|
|
|
*/ |
26
|
|
|
protected function inputAttributes() |
27
|
|
|
{ |
28
|
|
|
if (array_key_exists('placeholder', $this->params)) { |
29
|
|
|
$this->input_attributes['placeholder'] = $this->params['placeholder']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->input_attributes['id'] = $this->id; |
33
|
|
|
$this->input_attributes['name'] = $this->name; |
34
|
|
|
$this->input_attributes['type'] = 'text'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Renders the scripts |
39
|
|
|
*/ |
40
|
|
|
protected function renderScript() |
41
|
|
|
{ |
42
|
|
|
if ($this->params['autocomplete']['multiple']) { |
43
|
|
|
$select_callback = ''; |
44
|
|
|
if (isset($this->params['autocomplete']['callback'])) { |
45
|
|
|
$select_callback = $this->params['autocomplete']['callback']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
//TODO :: convert to jquery plugin |
49
|
|
|
$this->getJS()->ready( |
50
|
|
|
'$("#' . $this->id . '").autocomplete( |
51
|
|
|
{ |
52
|
|
|
minLength:' . $this->params['autocomplete']['length'] . ', |
53
|
|
|
source: function( request, response ) { |
54
|
|
|
$.getJSON( "' . \URL::to($this->params['autocomplete']['url']) . '", { |
55
|
|
|
term: request.term.split( /,\s*/ ).pop() |
56
|
|
|
}, response ); |
57
|
|
|
}, |
58
|
|
|
search: function() { |
59
|
|
|
// custom minLength |
60
|
|
|
var term = this.value.split( /,\s*/ ).pop(); |
61
|
|
|
if (term.length < ' . |
62
|
|
|
$this->params['autocomplete']['length'] . ') { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
}, |
66
|
|
|
focus: function() { |
67
|
|
|
// prevent value inserted on focus |
68
|
|
|
return false; |
69
|
|
|
}, |
70
|
|
|
select: function( event, ui ) { |
71
|
|
|
var terms = this.value.split( /,\s*/ ); |
72
|
|
|
// remove the current input |
73
|
|
|
terms.pop(); |
74
|
|
|
terms.push( ui.item.value ); |
75
|
|
|
terms.push( "" ); |
76
|
|
|
this.value = terms.join( ", " );' . |
77
|
|
|
$select_callback . ' |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
});' |
82
|
|
|
); |
83
|
|
|
} else { |
84
|
|
|
$params = [ |
85
|
|
|
'minLength' => $this->params['autocomplete']['length'], |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if (!empty($this->params['autocomplete']['url'])) { |
89
|
|
|
$params['source'] = \URL::to($this->params['autocomplete']['url']); |
90
|
|
|
} elseif (!empty($this->params['autocomplete']['source'])) { |
91
|
|
|
$params['source'] = $this->params['autocomplete']['source']; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (isset($this->params['autocomplete']['callback'])) { |
95
|
|
|
$params['select'] = 'function(event, ui) { ' . $this->params['autocomplete']['callback'] . ' }'; |
96
|
|
|
} |
97
|
|
|
if (isset($this->params['autocomplete']['callback_search'])) { |
98
|
|
|
$params['search'] = 'function() { ' . $this->params['autocomplete']['callback_search'] . ' }'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$script = '$("#' . $this->id . '")' . |
102
|
|
|
'.autocomplete(' . json_encode_with_functions($params) . ');'; |
103
|
|
|
$this->getJS()->ready($script); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|