From 48e79cb18ef0dac29a8cc81679cea07676c9debf Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 1 Oct 2019 18:54:54 +0200 Subject: [PATCH] wip #3075 @6 --- app/Models/Product.php | 51 +++++++++++++++++++-- config/app.php | 2 +- resources/js/components/ProductsFilters.vue | 46 +++++++++++++------ 3 files changed, 82 insertions(+), 17 deletions(-) diff --git a/app/Models/Product.php b/app/Models/Product.php index 2bf747b..f49fd66 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -491,6 +491,7 @@ class Product extends CubistMagicPageModel $f['max'] = -INF; $f['unit'] = $data->unit; $f['type'] = 'range'; + $f['scale'] = $data->logarithmic_scale ? 'log' : 'linear'; if ($data->type == 'numeric' || $data->type == 'numeric_list') { $f['prefix'] = $data['prefix']; @@ -506,6 +507,15 @@ class Product extends CubistMagicPageModel continue; } + + $fvmax = $filter_value[1]; + $fvmin = $filter_value[0]; + + if ($f['scale'] == 'log') { + $fvmax = safeExp($fvmax); + $fvmin = safeExp($fvmin); + } + if ($data->type == 'range') { $f['min'] = min($f['min'], $v['first'] ?? INF, $v['second'] ?? INF); $f['max'] = max($f['max'], $v['first'] ?? -INF, $v['second'] ?? -INF); @@ -514,15 +524,17 @@ class Product extends CubistMagicPageModel $min = min($v['first'], $v['second']); $max = max($v['first'], $v['second']); - if (null === $v || ($min <= $filter_value[1] && $max >= $filter_value[0])) { + + if (null === $v || ($min <= $fvmax && $max >= $fvmin)) { $matching[] = $product->id; } + } } else if ($data->type == 'numeric') { $f['min'] = min($f['min'], $v); $f['max'] = max($f['max'], $v); if (null !== $filter_value) { - if (null === $v || ($v >= $filter_value[0] && $v <= $filter_value[1])) { + if (null === $v || ($v >= $fvmin && $v <= $fvmax)) { $matching[] = $product->id; } } @@ -535,12 +547,21 @@ class Product extends CubistMagicPageModel $f['max'] = max($f['max'], $vv); if (null !== $filter_value) { - if (null === $vv || ($vv >= $filter_value[0] && $vv <= $filter_value[1])) { + if (null === $vv || ($vv >= $fvmin && $vv <= $fvmax)) { $matching[] = $product->id; } } } } + + if ($f['scale'] === 'log') { + $f['minscale'] = safeLog($f['min']); + $f['maxscale'] = safeLog($f['max']); + } else { + $f['minscale'] = $f['min']; + $f['maxscale'] = $f['max']; + } + if (null === $filter_value && $v != '') { $matching[] = $product->id; } @@ -567,3 +588,27 @@ class Product extends CubistMagicPageModel } } + +function safeLog($v) +{ + if ($v == 0) { + $res = 0; + } elseif ($v < 0) { + $res = -log10(abs($v)); + } else { + $res = log10($v); + } + + return round($res,8); +} + +function safeExp($log) +{ + if ($log === 0) { + $res = 0; + } else { + $m = $log > 0 ? 1 : -1; + $res = $m * (10 ** ($log * $m)); + } + return $res; +} diff --git a/config/app.php b/config/app.php index 63f08b3..fc0bb7a 100644 --- a/config/app.php +++ b/config/app.php @@ -117,7 +117,7 @@ return [ | */ - 'fallback_locale' => 'en', + 'fallback_locale' => 'fr', /* |-------------------------------------------------------------------------- diff --git a/resources/js/components/ProductsFilters.vue b/resources/js/components/ProductsFilters.vue index e6f146a..e4148c5 100644 --- a/resources/js/components/ProductsFilters.vue +++ b/resources/js/components/ProductsFilters.vue @@ -3,21 +3,24 @@
-
+
-
+

{{ filter.label }}

    -
  • +
  • @@ -29,12 +32,13 @@
    @@ -55,7 +59,8 @@
    -
    +
    @@ -68,9 +73,11 @@ {{ getFilter(filterID).options[filter_option].label }} - - +
  • @@ -144,6 +151,19 @@ filters: {}, matches: {}, products: [], + log: function (v) { + var res = Math.pow(10, v); + var nbZeros = Math.max(-1, Math.round(v - 2)); + var roundFactor = Math.pow(10, nbZeros); + var introunded = Math.round(res / roundFactor); + var rounded = introunded * roundFactor; + rounded = rounded.toFixed(Math.max(0, -nbZeros)); + + return rounded; + }, + linear: function (v) { + return v; + } }), computed: { @@ -176,8 +196,8 @@ // Don't include range values if they're unchanged (at min / max values) if (filter.type === 'range' - && filter.min === this.filters[filterID][0] - && filter.max === this.filters[filterID][1]) { + && filter.minscale === this.filters[filterID][0] + && filter.maxscale === this.filters[filterID][1]) { return; } @@ -197,7 +217,7 @@ let filters = {}; Object.values(this.filterData).forEach(filter => { if (filter.type === 'range') { - filters[filter.id] = [filter.min, filter.max]; + filters[filter.id] = [filter.minscale, filter.maxscale]; } else { filters[filter.id] = []; } -- 2.39.5