1414use yii \base \Exception ;
1515use yii \helpers \BaseUrl ;
1616use yii \helpers \Html ;
17+ use yii \helpers \Url ;
1718use yii \web \JsExpression ;
1819use yii \web \View ;
1920use yii \widgets \InputWidget ;
@@ -29,14 +30,60 @@ class FileManagerInputWidget extends InputWidget
2930 * File Handler Url
3031 * @var null|string
3132 */
32- public $ handlerUrl = null ;
33+ public $ handlerUrl ;
34+
35+ /**
36+ * @var array
37+ */
38+ public $ select2Options = [];
3339
3440 public function init ()
3541 {
42+ parent ::init ();
43+
3644 if (empty ($ this ->handlerUrl )) {
3745 throw new Exception ('Missing handlerUrl confiugration ' );
3846 }
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+ ];
4087 }
4188
4289 /**
@@ -48,44 +95,8 @@ public function init()
4895 public function run ()
4996 {
5097 $ this ->registerClientScript ();
51-
5298 // 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 );
89100 }
90101
91102 /**
@@ -257,34 +268,36 @@ protected function registerClientScript()
257268 protected function generateAddonButtons ()
258269 {
259270 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+ ]
288301 ];
289302 }
290303
@@ -299,6 +312,6 @@ protected function generateAddonButtons()
299312 */
300313 private function to ($ action , $ path = '' )
301314 {
302- return BaseUrl ::to ([$ this ->handlerUrl , 'action ' => $ action , 'path ' => $ path ]);
315+ return Url ::to ([$ this ->handlerUrl , 'action ' => $ action , 'path ' => $ path ]);
303316 }
304317}
0 commit comments