From 25da96d1becdc806f5d1d3f27c976692d846609f Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 14 Jun 2019 12:10:59 +0200 Subject: [PATCH] wip #2834 @3 --- .../Magic/Models/CubistMagicAbstractModel.php | 2 + src/resources/lang/fr/fields.php | 14 ++++ src/resources/lang/fr/templates.php | 23 +++++++ src/resources/views/when.blade.php | 67 +++++++++++++++---- 4 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 src/resources/lang/fr/fields.php create mode 100644 src/resources/lang/fr/templates.php diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index b0cd1ab..21714b0 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -89,6 +89,8 @@ class CubistMagicAbstractModel extends Model implements HasMedia return $res; } + + public function onRetreived(){ } diff --git a/src/resources/lang/fr/fields.php b/src/resources/lang/fr/fields.php new file mode 100644 index 0000000..d563f3b --- /dev/null +++ b/src/resources/lang/fr/fields.php @@ -0,0 +1,14 @@ + 'Valeur minimale', + 'second_value' => 'Valeur maximale' +]; diff --git a/src/resources/lang/fr/templates.php b/src/resources/lang/fr/templates.php new file mode 100644 index 0000000..6ad6f5d --- /dev/null +++ b/src/resources/lang/fr/templates.php @@ -0,0 +1,23 @@ + 'Name', + 'value' => 'Value', + 'description' => 'Description', + 'template_singular' => 'template', + 'template_plural' => 'templates', + 'parent' => 'Parent', + 'field_singular' => 'Field', + 'field_plural' => 'Fields', + 'first_value' => 'First value', + 'second_value' => 'Second value' + +]; diff --git a/src/resources/views/when.blade.php b/src/resources/views/when.blade.php index ed224a2..dcc34fa 100644 --- a/src/resources/views/when.blade.php +++ b/src/resources/views/when.blade.php @@ -10,28 +10,33 @@ } $(form).find('[data-when]').removeClass('when-visible').each(function () { - var conditions = $(this).data('when'); + if ($(this).data('when-normalized') === undefined) { + $(this).data('when-normalized', normalizeWhen($(this).data('when'))); + } + var when = $(this).data('when-normalized'); var match = true; - $.each(conditions, function (k, v) { - if (!Array.isArray(v)) { - v = [v]; - } - var val = $(form).find('[name="' + k + '"]').val(); - var valnum = parseFloat(val); - if (!isNumber(valnum)) { - valnum = val; - } - if (v.indexOf(val) === -1 && v.indexOf(valnum) === -1) { - console.log('no match', v, val, valnum); - match = false; + var order = 1000; + $.each(when, function (property, conditions) { + var val = $(form).find('[name="' + property + '"]').val().toString(); + $.each(conditions, function (k, condition) { + if (condition.id !== val) { + match = false; + return false; + } + order = Math.min(order, condition.order); + }); + if (match === false) { return false; } }); if (match) { $(this).addClass('when-visible'); + $(this).attr('data-when-order', order); } }); + + orderWhen(); }; function isNumber(n) { @@ -42,6 +47,42 @@ triggersWhenChange($(this)); }); triggersWhenChange(); + + function orderWhen() { + $("[data-when]").parent().each(function () { + // Inspired from https://stackoverflow.com/a/14160529/1082031 + $(this).find('[data-when-order]').sort(function (a, b) { + return $(a).data('when-order') - $(b).data('when-order'); + }).appendTo(this); + }); + } + + function normalizeWhen(w) { + var res = {}; + $.each(w, function (property, conditions) { + if (!Array.isArray(conditions)) { + conditions = [conditions]; + } + var normalizedConditions = []; + $.each(conditions, function (k, condition) { + if (!isObject(condition)) { + condition = {id: condition}; + } + if (!condition.hasOwnProperty('order')) { + condition.order = k; + } + condition.id = condition.id.toString(); + normalizedConditions[k] = condition; + }); + res[property] = normalizedConditions; + }); + return res; + } + + function isObject(value) { + return value && typeof value === 'object' && value.constructor === Object; + } + }); -- 2.39.5