@@ -32,17 +32,61 @@ class FileManagerInputWidget extends InputWidget
32
32
*/
33
33
public $ handlerUrl ;
34
34
35
+ /**
36
+ * Limit the number of results
37
+ * if not set, the limit is the one from api::search which is normally way to high
38
+ * to be bc, we do NOT set a default here.
39
+ *
40
+ * @var
41
+ */
42
+ public $ limit ;
43
+
44
+ /**
45
+ * min input that must be typed before we search result from api
46
+ * if set to 0, the search will be triggered without typing.
47
+ * so this should not be done without limit and/or basePath defined!
48
+ *
49
+ * handle with care!
50
+ *
51
+ * @var int
52
+ */
53
+ public $ minInputLength = 3 ;
54
+
55
+ /**
56
+ * optional basePath that can be used to "limit" the search within a
57
+ * subDir of the file-System
58
+ *
59
+ * @var string
60
+ */
61
+ public $ basePath ;
62
+
63
+ /**
64
+ * if true thumbnail img tag will be added IF item has thumbnail property
65
+ * see: JS formatFiles()
66
+ *
67
+ * @var bool
68
+ */
69
+ public $ enableThumbnails = false ;
70
+
35
71
/**
36
72
* @var array
37
73
*/
38
74
public $ select2Options = [];
39
75
76
+ /**
77
+ * internal var for the random func name required to init searchData
78
+ * for every input instance
79
+ *
80
+ * @var
81
+ */
82
+ protected $ jsGetSearchDataFuncName ;
83
+
40
84
public function init ()
41
85
{
42
86
parent ::init ();
43
87
44
88
if (empty ($ this ->handlerUrl )) {
45
- throw new Exception ('Missing handlerUrl confiugration ' );
89
+ throw new Exception ('Missing handlerUrl configuration ' );
46
90
}
47
91
48
92
if ($ this ->hasModel ()) {
@@ -60,9 +104,15 @@ public function init()
60
104
61
105
$ this ->select2Options ['addon ' ] = $ this ->generateAddonButtons ();
62
106
107
+ // do NOT use more_entropy here, as this will result in a string that can NOT be used as JS func name!
108
+ $ this ->jsGetSearchDataFuncName = uniqid ('searchData ' , false );
109
+
110
+ // should format func with enabled thumbnail be used?
111
+ $ formatFilesCallback = $ this ->enableThumbnails ? 'formatFilesWithThumb ' : 'formatFiles ' ;
112
+
63
113
$ this ->select2Options ['pluginOptions ' ] = [
64
114
'allowClear ' => true ,
65
- 'minimumInputLength ' => 3 ,
115
+ 'minimumInputLength ' => $ this -> minInputLength ,
66
116
'language ' => [
67
117
'errorLoading ' => \Yii::t ('afm ' , 'Waiting for results ... ' ),
68
118
],
@@ -71,11 +121,11 @@ public function init()
71
121
'url ' => $ this ->to ('search ' , null ),
72
122
'dataType ' => 'json ' ,
73
123
'delay ' => 220 ,
74
- 'data ' => new JsExpression (' searchData ' ),
124
+ 'data ' => new JsExpression ($ this -> jsGetSearchDataFuncName ),
75
125
'processResults ' => new JsExpression ('resultJs ' ),
76
126
],
77
127
'escapeMarkup ' => new JsExpression ('escapeMarkup ' ),
78
- 'templateResult ' => new JsExpression (' formatFiles ' ),
128
+ 'templateResult ' => new JsExpression ($ formatFilesCallback ),
79
129
'templateSelection ' => new JsExpression ('formatFileSelection ' ),
80
130
'width ' => '100% '
81
131
];
@@ -127,7 +177,7 @@ protected function registerClientScript()
127
177
url: " {$ searchUrl }",
128
178
dataType:"json",
129
179
delay:220,
130
- data: {q : selectedPath},
180
+ data: {$ this -> jsGetSearchDataFuncName } ({term : selectedPath}) ,
131
181
success: function(result){
132
182
if (result.length == 1) {
133
183
onSelect(result[0], ' {$ inputId }');
@@ -138,14 +188,56 @@ protected function registerClientScript()
138
188
});
139
189
JS ;
140
190
141
- $ this ->view ->registerJs ($ initJs , View::POS_READY );
191
+ $ this ->view ->registerJs ($ initJs , View::POS_HEAD );
142
192
143
193
// format result markup and register addon button scripts and events
194
+ // we need a separate searchData func for EVERY instance to be able to inject search params
195
+ // (basePath, limit) on instance basis
144
196
$ inputJs = <<<JS
145
- var searchData = function(params) {
146
- return {q:params.term};
197
+
198
+ var {$ this ->jsGetSearchDataFuncName } = function (params) {
199
+ var qData = {
200
+ q:''
201
+ };
202
+ var basePath = ' {$ this ->basePath }';
203
+ var limit = ' {$ this ->limit }';
204
+ if (basePath != '') {
205
+ qData.basePath = basePath;
206
+ }
207
+ if (limit != '') {
208
+ qData.limit = limit;
209
+ }
210
+ if (params.term) {
211
+ qData.q = params.term;
212
+ }
213
+ return qData;
214
+ };
215
+
216
+
217
+ var hasImageExtension = function(path) {
218
+ if (typeof path !== 'string') {
219
+ return false
220
+ }
221
+
222
+ var imageExtensions = ['jpg', 'jpeg', 'gif', 'svg', 'png', 'bmp', 'tif']
223
+
224
+ if (window.FILEFLYCONFIG && window.FILEFLYCONFIG['imageExtensions']) {
225
+ imageExtensions = window.FILEFLYCONFIG['imageExtensions']
226
+ }
227
+
228
+ imageExtensions = imageExtensions.map(extension => {
229
+ return extension.toLowerCase()
230
+ })
231
+
232
+ var extension = path.split('.').pop().toLowerCase()
233
+
234
+ return (imageExtensions.indexOf(extension) !== -1)
235
+ };
236
+ // just a wrapper to call formatFiles() with useThumb = true property
237
+ var formatFilesWithThumb = function (file) {
238
+ return formatFiles(file, true);
147
239
};
148
- var formatFiles = function (file) {
240
+ var formatFiles = function (file, useThumb = false ) {
149
241
150
242
// show loading / placeholder
151
243
if (file.loading) {
@@ -155,24 +247,33 @@ protected function registerClientScript()
155
247
// mime types
156
248
var preview = '';
157
249
var text = file.path;
158
- if (file.mime.indexOf("image") > -1) {
159
- preview = '<img src=" {$ previewUrl }' + file.id + '" style="width:38px" />';
160
- } else if (file.mime.indexOf("directory") > -1) {
161
- preview = '<span style="width:40px"><i class="fa fa-folder-open fa-3x"></i></span>';
162
- } else if (file.mime.indexOf("pdf") > -1) {
163
- preview = '<span style="width:40px"><i class="fa fa-file-pdf-o fa-3x"></i></span>';
164
- } else if (file.mime.indexOf("zip") > -1) {
165
- preview = '<span style="width:40px"><i class="fa fa-file-zip-o fa-3x"></i></span>';
166
- } else if (file.mime.indexOf("doc") > -1) {
167
- preview = '<span style="width:40px"><i class="fa fa-file-word-o fa-3x"></i></span>';
168
- } else if (file.mime.indexOf("xls") > -1) {
169
- preview = '<span style="width:40px"><i class="fa fa-file-excel-o fa-3x"></i></span>';
170
- } else if (file.mime.indexOf("ppt") > -1) {
171
- preview = '<span style="width:40px"><i class="fa fa-file-powerpoint-o fa-3x"></i></span>';
172
- } else {
173
- preview = '<span style="width:40px"><i class="fa fa-file-o fa-3x"></i></span>';
250
+ preview = '<span style="width:40px"><i class="fa fa-file-o fa-3x"></i></span>';
251
+
252
+ if (file.mime != '') {
253
+ if (file.mime.indexOf("image") > -1) {
254
+ preview = '<span style="width:40px"><i class="fa fa-picture-o fa-3x"></i></span>';
255
+ } else if (file.mime.indexOf("directory") > -1) {
256
+ preview = '<span style="width:40px"><i class="fa fa-folder-open fa-3x"></i></span>';
257
+ } else if (file.mime.indexOf("pdf") > -1) {
258
+ preview = '<span style="width:40px"><i class="fa fa-file-pdf-o fa-3x"></i></span>';
259
+ } else if (file.mime.indexOf("zip") > -1) {
260
+ preview = '<span style="width:40px"><i class="fa fa-file-zip-o fa-3x"></i></span>';
261
+ } else if (file.mime.indexOf("doc") > -1) {
262
+ preview = '<span style="width:40px"><i class="fa fa-file-word-o fa-3x"></i></span>';
263
+ } else if (file.mime.indexOf("xls") > -1) {
264
+ preview = '<span style="width:40px"><i class="fa fa-file-excel-o fa-3x"></i></span>';
265
+ } else if (file.mime.indexOf("ppt") > -1) {
266
+ preview = '<span style="width:40px"><i class="fa fa-file-powerpoint-o fa-3x"></i></span>';
267
+ }
268
+ } else if (hasImageExtension(file.path)) {
269
+ preview = '<span style="width:40px"><i class="fa fa-picture-o fa-3x"></i></span>';
270
+ // don't do it, this will load way too much data....
271
+ // preview = '<img src=" {$ previewUrl }' + file.id + '" style="width:38px" />';
174
272
}
175
-
273
+ if (useThumb == true && file.thumbnail && file.thumbnail != '') {
274
+ preview = '<img src="' + file.thumbnail + '" style="width:38px" />';
275
+ }
276
+
176
277
// one result line markup
177
278
var markup =
178
279
'<div class="row" style="min-height:38px">' +
0 commit comments