From 806c64be3a2e2c0177f8889675df84e75e71c72e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 28 Feb 2022 10:10:06 +0100 Subject: [PATCH] wip #5127 @0.5 --- .../Controllers/CubistSelect2Controller.php | 30 +++++++++++++++++-- src/app/Magic/Fields/SelectFromArray.php | 13 ++++++-- .../views/fields/select2_from_array.blade.php | 13 ++++---- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/app/Http/Controllers/CubistSelect2Controller.php b/src/app/Http/Controllers/CubistSelect2Controller.php index 339c709..470fda1 100644 --- a/src/app/Http/Controllers/CubistSelect2Controller.php +++ b/src/app/Http/Controllers/CubistSelect2Controller.php @@ -3,6 +3,8 @@ namespace Cubist\Backpack\Http\Controllers; use Cubist\Backpack\Http\Controllers\Base\XSendFileController; +use Cubist\Backpack\Magic\Fields\SelectFromArray; +use Cubist\Util\Text; use Illuminate\Support\Facades\Auth; class CubistSelect2Controller @@ -15,8 +17,32 @@ class CubistSelect2Controller if (!can('edition')) { return response(null)->setStatusCode('403'); } - $data = json_decode(storage_path('select2/' . $hash . '.json')); - $res = []; + + $perPage = 10; + $data = json_decode(file_get_contents(storage_path('select2/' . $hash . '.json')), true); + $term = request()->get('q', null); + $page = request()->get('page', 1); + $offset = ($page - 1) * $perPage; + if (null === $term) { + $sub = $data; + } else { + $search = SelectFromArray::normalize($term); + $sub = []; + foreach ($data as $k => $v) { + if (strpos($v['n'], $search) !== false) { + $sub[$k] = $v; + } + } + } + $sub = array_slice($sub, $offset, $perPage, true); + $results = []; + foreach ($sub as $k => $v) { + $results[] = ['id' => $k, 'text' => $v['t']]; + } + + $res = ['results' => $results, 'pagination' => ['more' => count($sub) == $perPage]]; return response()->json($res); } + + } diff --git a/src/app/Magic/Fields/SelectFromArray.php b/src/app/Magic/Fields/SelectFromArray.php index a6a795e..1a1055c 100644 --- a/src/app/Magic/Fields/SelectFromArray.php +++ b/src/app/Magic/Fields/SelectFromArray.php @@ -5,6 +5,7 @@ namespace Cubist\Backpack\Magic\Fields; use Cubist\Backpack\CubistBackpackServiceProvider; +use Cubist\Util\Text; class SelectFromArray extends Field { @@ -33,11 +34,10 @@ class SelectFromArray extends Field $this->setAttribute('allows_null', false); } if ($this->getAttribute('ajax', false)) { - $results = []; + $data = []; foreach ($this->getOptions() as $k => $v) { - $results[] = ['id' => $k, 'text' => $v]; + $data[$k] = ['t' => $v, 'n' => self::normalize($v)]; } - $data = ['results' => $results, 'pagination' => ['more' => false]]; $hash = hash('sha256', print_r($data, true)); $name = $hash . '.json'; $dir = \Cubist\Util\Files\Files::mkdir(storage_path('select2')); @@ -98,4 +98,11 @@ class SelectFromArray extends Field { return $this->getColumnData()['options']; } + + public static function normalize($str) + { + $str = trim($str); + $str = mb_strtolower($str); + return Text::removeAccents($str); + } } diff --git a/src/resources/views/fields/select2_from_array.blade.php b/src/resources/views/fields/select2_from_array.blade.php index f6c3507..28e993c 100644 --- a/src/resources/views/fields/select2_from_array.blade.php +++ b/src/resources/views/fields/select2_from_array.blade.php @@ -89,24 +89,21 @@ function initUntriggeredSelectFromArray() { // trigger select2 for each untriggered select2 box - $('.select2_from_array').each(function (i, obj) { + $('.select2_from_array:not(.select2-hidden-accessible)').each(function (i, obj) { if ($(obj).closest('.item.sample').length > 0) { return; } var options = { theme: "bootstrap" }; - if ($(this).is('[data-ajax]')) { options.ajax = { - url: '{{backpack_url('select2')}}' +'/'+ $(this).data('ajax'), - dataType: 'json' + url: '{{backpack_url('select2')}}' + '/' + $(this).data('ajax'), + dataType: 'json', + delay: 500, }; } - - if (!$(obj).hasClass("select2-hidden-accessible")) { - $(obj).select2(options); - } + $(obj).addClass('init').select2(options); }); } }); -- 2.39.5