1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/2amigos/yii2-type-ahead-widget |
4
|
|
|
* @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC |
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dosamigos\typeahead; |
9
|
|
|
|
10
|
|
|
use yii\base\BaseObject; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\helpers\Json; |
13
|
|
|
use yii\web\JsExpression; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Bloodhound is a helper class to configure Bloodhound suggestion engines. |
17
|
|
|
* |
18
|
|
|
* @author Antonio Ramirez <[email protected]> |
19
|
|
|
* @link http://www.ramirezcobos.com/ |
20
|
|
|
* @link http://www.2amigos.us/ |
21
|
|
|
* @package dosamigos\typeahead |
22
|
|
|
*/ |
23
|
|
|
class Bloodhound extends BaseObject |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string the engine js name |
27
|
|
|
*/ |
28
|
|
|
public $name; |
29
|
|
|
/** |
30
|
|
|
* @var array the configuration of Bloodhound suggestion engine. |
31
|
|
|
* @see https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#options |
32
|
|
|
*/ |
33
|
|
|
public $clientOptions = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
* @throws \yii\base\InvalidConfigException |
38
|
|
|
*/ |
39
|
12 |
|
public function init() |
40
|
|
|
{ |
41
|
12 |
|
if ($this->name === null) { |
42
|
3 |
|
throw new InvalidConfigException("'name' cannot be null."); |
43
|
|
|
} |
44
|
9 |
|
parent::init(); |
45
|
9 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the engine adapter. To be used to configure [[TypeAhead::dataSets]] `source` option. |
49
|
|
|
* @return JsExpression |
50
|
|
|
*/ |
51
|
3 |
|
public function getAdapterScript() |
52
|
|
|
{ |
53
|
3 |
|
return new JsExpression("{$this->name}.ttAdapter()"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the javascript initialization code |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
6 |
|
public function getClientScript() |
61
|
|
|
{ |
62
|
6 |
|
$options = $this->clientOptions !== false && !empty($this->clientOptions) |
63
|
2 |
|
? Json::encode($this->clientOptions) |
64
|
6 |
|
: '{}'; |
65
|
|
|
|
66
|
6 |
|
return "var {$this->name} = new Bloodhound($options);\n{$this->name}.initialize();"; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|