14
14
use yii \base \Exception ;
15
15
use yii \helpers \BaseUrl ;
16
16
use yii \helpers \Html ;
17
+ use yii \helpers \Url ;
17
18
use yii \web \JsExpression ;
18
19
use yii \web \View ;
19
20
use yii \widgets \InputWidget ;
@@ -29,14 +30,60 @@ class FileManagerInputWidget extends InputWidget
29
30
* File Handler Url
30
31
* @var null|string
31
32
*/
32
- public $ handlerUrl = null ;
33
+ public $ handlerUrl ;
34
+
35
+ /**
36
+ * @var array
37
+ */
38
+ public $ select2Options = [];
33
39
34
40
public function init ()
35
41
{
42
+ parent ::init ();
43
+
36
44
if (empty ($ this ->handlerUrl )) {
37
45
throw new Exception ('Missing handlerUrl confiugration ' );
38
46
}
39
- parent ::init ();
47
+
48
+ if ($ this ->hasModel ()) {
49
+ $ this ->select2Options ['model ' ] = $ this ->model ;
50
+ $ this ->select2Options ['attribute ' ] = $ this ->attribute ;
51
+ } else {
52
+ $ this ->select2Options ['name ' ] = $ this ->name ;
53
+ }
54
+
55
+ $ this ->select2Options ['options ' ] = [
56
+ 'id ' => $ this ->options ['id ' ],
57
+ 'placeholder ' => \Yii::t ('afm ' , 'Search for a file ... ' ),
58
+ 'style ' => ['width ' => '100% ' ]
59
+ ];
60
+
61
+ $ this ->select2Options ['addon ' ] = $ this ->generateAddonButtons ();
62
+
63
+ $ this ->select2Options ['pluginOptions ' ] = [
64
+ 'allowClear ' => true ,
65
+ 'minimumInputLength ' => 3 ,
66
+ 'language ' => [
67
+ 'errorLoading ' => \Yii::t ('afm ' , 'Waiting for results ... ' ),
68
+ ],
69
+ 'ajax ' => [
70
+ 'cache ' => true ,
71
+ 'url ' => $ this ->to ('search ' , null ),
72
+ 'dataType ' => 'json ' ,
73
+ 'delay ' => 220 ,
74
+ 'data ' => new JsExpression ('searchData ' ),
75
+ 'processResults ' => new JsExpression ('resultJs ' ),
76
+ ],
77
+ 'escapeMarkup ' => new JsExpression ('escapeMarkup ' ),
78
+ 'templateResult ' => new JsExpression ('formatFiles ' ),
79
+ 'templateSelection ' => new JsExpression ('formatFileSelection ' ),
80
+ 'width ' => '100% '
81
+ ];
82
+
83
+ $ this ->select2Options ['pluginEvents ' ] = [
84
+ "select2:select " => new JsExpression ('onSelect ' ),
85
+ "select2:unselect " => new JsExpression ('onUnSelect ' ),
86
+ ];
40
87
}
41
88
42
89
/**
@@ -48,44 +95,8 @@ public function init()
48
95
public function run ()
49
96
{
50
97
$ this ->registerClientScript ();
51
-
52
98
// render select2 input widget
53
- return Select2::widget (
54
- [
55
- 'model ' => ($ this ->model ) ? $ this ->model : null ,
56
- 'attribute ' => ($ this ->attribute ) ? $ this ->attribute : null ,
57
- 'name ' => ($ this ->name ) ? $ this ->name : null ,
58
- 'options ' => [
59
- 'id ' => $ this ->options ['id ' ],
60
- 'placeholder ' => \Yii::t ('afm ' , 'Search for a file ... ' ),
61
- ],
62
- 'addon ' => [
63
- 'append ' => $ this ->generateAddonButtons (),
64
- ],
65
- 'pluginOptions ' => [
66
- 'allowClear ' => true ,
67
- 'minimumInputLength ' => 3 ,
68
- 'language ' => [
69
- 'errorLoading ' => \Yii::t ('afm ' , 'Waiting for results ... ' ),
70
- ],
71
- 'ajax ' => [
72
- 'cache ' => true ,
73
- 'url ' => $ this ->to ('search ' , null ),
74
- 'dataType ' => 'json ' ,
75
- 'delay ' => 220 ,
76
- 'data ' => new JsExpression ('searchData ' ),
77
- 'processResults ' => new JsExpression ('resultJs ' ),
78
- ],
79
- 'escapeMarkup ' => new JsExpression ('escapeMarkup ' ),
80
- 'templateResult ' => new JsExpression ('formatFiles ' ),
81
- 'templateSelection ' => new JsExpression ('formatFileSelection ' ),
82
- ],
83
- 'pluginEvents ' => [
84
- "select2:select " => new JsExpression ('onSelect ' ),
85
- "select2:unselect " => new JsExpression ('onUnSelect ' ),
86
- ]
87
- ]
88
- );
99
+ return Select2::widget ($ this ->select2Options );
89
100
}
90
101
91
102
/**
@@ -257,34 +268,36 @@ protected function registerClientScript()
257
268
protected function generateAddonButtons ()
258
269
{
259
270
return [
260
- 'content ' => Html::button (
261
- FA ::i ('copy ' ),
262
- [
263
- 'class ' => 'btn btn-default ' ,
264
- 'id ' => $ this ->options ['id ' ] . '-afm-copy-btn ' ,
265
- 'disabled ' => 'disabled ' ,
266
- 'title ' => \Yii::t ('afm ' , 'Copy path to clipboard ' )
267
- ]
268
- )
269
- . Html::button (
270
- FA ::i ('link ' ),
271
- [
272
- 'class ' => 'btn btn-default ' ,
273
- 'id ' => $ this ->options ['id ' ] . '-afm-link-btn ' ,
274
- 'disabled ' => 'disabled ' ,
275
- 'title ' => \Yii::t ('afm ' , 'Copy link to clipboard ' )
276
- ]
277
- )
278
- . Html::button (
279
- FA ::i ('download ' ),
280
- [
281
- 'class ' => 'btn btn-default ' ,
282
- 'id ' => $ this ->options ['id ' ] . '-afm-download-btn ' ,
283
- 'disabled ' => 'disabled ' ,
284
- 'title ' => \Yii::t ('afm ' , 'Download file ' )
285
- ]
286
- ),
287
- 'asButton ' => true
271
+ 'append ' => [
272
+ 'content ' => Html::button (
273
+ FA ::i (FA ::_COPY ),
274
+ [
275
+ 'class ' => 'btn btn-default ' ,
276
+ 'id ' => $ this ->options ['id ' ] . '-afm-copy-btn ' ,
277
+ 'disabled ' => 'disabled ' ,
278
+ 'title ' => \Yii::t ('afm ' , 'Copy path to clipboard ' )
279
+ ]
280
+ )
281
+ . Html::button (
282
+ FA ::i (FA ::_LINK ),
283
+ [
284
+ 'class ' => 'btn btn-default ' ,
285
+ 'id ' => $ this ->options ['id ' ] . '-afm-link-btn ' ,
286
+ 'disabled ' => 'disabled ' ,
287
+ 'title ' => \Yii::t ('afm ' , 'Copy link to clipboard ' )
288
+ ]
289
+ )
290
+ . Html::button (
291
+ FA ::i (FA ::_DOWNLOAD ),
292
+ [
293
+ 'class ' => 'btn btn-default ' ,
294
+ 'id ' => $ this ->options ['id ' ] . '-afm-download-btn ' ,
295
+ 'disabled ' => 'disabled ' ,
296
+ 'title ' => \Yii::t ('afm ' , 'Download file ' )
297
+ ]
298
+ ),
299
+ 'asButton ' => true
300
+ ]
288
301
];
289
302
}
290
303
@@ -299,6 +312,6 @@ protected function generateAddonButtons()
299
312
*/
300
313
private function to ($ action , $ path = '' )
301
314
{
302
- return BaseUrl ::to ([$ this ->handlerUrl , 'action ' => $ action , 'path ' => $ path ]);
315
+ return Url ::to ([$ this ->handlerUrl , 'action ' => $ action , 'path ' => $ path ]);
303
316
}
304
317
}
0 commit comments