1
1
<?php
2
2
namespace avikarsha \tagsinput ;
3
3
4
+ use yii \base \InvalidConfigException ;
5
+ use yii \helpers \ArrayHelper ;
4
6
use yii \helpers \Html ;
5
7
use yii \helpers \Json ;
6
- use yii \web \View ;
7
8
use yii \web \JsExpression ;
8
- use yii \helpers \ArrayHelper ;
9
- use yii \base \InvalidConfigException ;
9
+ use yii \web \View ;
10
10
11
11
/**
12
12
* The yii2-tagsinput is a Yii 2 wrapper for bootstrap-tagsinput with typeahead.
@@ -43,16 +43,16 @@ class TagsinputWidget extends \yii\widgets\InputWidget
43
43
44
44
/**
45
45
* @var array dataset an object that defines a set of data that hydrates suggestions.
46
- * For TypeaheadBasic, this is a single dimensional array consisting of following settings.
47
- * For Typeahead, this is a multi-dimensional array, with each array item being an array that
46
+ * For TypeaheadBasic, this is a single dimensional array consisting of following settings.
47
+ * For Typeahead, this is a multi-dimensional array, with each array item being an array that
48
48
* consists of the following settings.
49
- * - source: The backing data source for suggestions. Expected to be a function with the
49
+ * - source: The backing data source for suggestions. Expected to be a function with the
50
50
* signature `(query, syncResults, asyncResults)`. This can also be a Bloodhound instance.
51
51
* If not set, this will be automatically generated based on the bloodhound specific
52
52
* properties in the next section below.
53
53
* - display: string the key used to access the value of the datum in the datum
54
54
* object. Defaults to 'value'.
55
- * - async: boolean, lets the dataset know if async suggestions should be expected. Defaults to `true`.
55
+ * - async: boolean, lets the dataset know if async suggestions should be expected. Defaults to `true`.
56
56
* - limit: integer the max number of suggestions from the dataset to display for
57
57
* a given query. Defaults to 5.
58
58
* - templates: array the templates used to render suggestions.
@@ -76,7 +76,6 @@ class TagsinputWidget extends \yii\widgets\InputWidget
76
76
*/
77
77
protected $ _bloodhound ;
78
78
79
-
80
79
/**
81
80
* @var string the generated HashPluginOptions script
82
81
*/
@@ -91,7 +90,7 @@ class TagsinputWidget extends \yii\widgets\InputWidget
91
90
* @var bool whether default suggestions are enabled
92
91
*/
93
92
protected $ _defaultSuggest = false ;
94
-
93
+
95
94
/**
96
95
* @var array the bloodhound settings variables
97
96
*/
@@ -104,16 +103,15 @@ class TagsinputWidget extends \yii\widgets\InputWidget
104
103
'identify ' ,
105
104
'local ' ,
106
105
'prefetch ' ,
107
- 'remote '
106
+ 'remote ' ,
108
107
];
109
108
110
109
/**
111
110
* @inheritdoc
112
111
*/
113
112
public function run ()
114
113
{
115
-
116
- if (isset ($ this ->dataset )) {
114
+ if (isset ($ this ->dataset ) && !empty ($ this ->dataset )) {
117
115
if (empty ($ this ->dataset ) || !is_array ($ this ->dataset )) {
118
116
throw new InvalidConfigException ("You must define the 'dataset' property for Typeahead which must be an array. " );
119
117
}
@@ -156,12 +154,12 @@ protected function validateConfig()
156
154
*/
157
155
protected function hashPluginOptions ($ view )
158
156
{
159
- if (isset ($ this ->typeaheadOptions )) {
160
- $ this ->clientOptions ['typeaheadjs ' ][] = $ this ->typeaheadOptions ;
157
+ if (isset ($ this ->typeaheadOptions ) && ! empty ( $ this -> typeaheadOptions )) {
158
+ $ this ->clientOptions ['typeaheadjs ' ][] = $ this ->typeaheadOptions ;
161
159
}
162
160
163
- if (isset ($ this ->_dataset )) {
164
- $ this ->clientOptions ['typeaheadjs ' ][] = $ this ->_dataset ;
161
+ if (isset ($ this ->_dataset ) && ! empty ( $ this -> _dataset )) {
162
+ $ this ->clientOptions ['typeaheadjs ' ][] = $ this ->_dataset ;
165
163
}
166
164
167
165
$ encOptions = empty ($ this ->clientOptions ) ? '{} ' : Json::encode ($ this ->clientOptions );
@@ -211,7 +209,7 @@ protected static function parseJsExpr($expr)
211
209
{
212
210
return ($ expr instanceof JsExpression) ? $ expr : new JsExpression ($ expr );
213
211
}
214
-
212
+
215
213
/**
216
214
* Parses the data source array and prepares the bloodhound configuration
217
215
*
0 commit comments