@push('crud_fields_scripts')
- <script type="text/javascript">
- jQuery('document').ready(function ($) {
- var triggersWhenChange = function (f) {
- var form;
- var prefix = '';
- if (f === undefined) {
- form = $('section.content form[method="post"]');
+<script type="text/javascript">
+ jQuery('document').ready(function ($) {
+ var triggersWhenChange = function (f) {
+ var form;
+ var prefix = '';
+ if (f === undefined) {
+ form = $('section.content form[method="post"]');
+ } else {
+ if ($(f).closest('.subform').length > 0) {
+ form = $(f).closest('.subform');
+ prefix = $(form).data('bmid') + '___';
} else {
- if ($(f).closest('.subform').length > 0) {
- form = $(f).closest('.subform');
- prefix = $(form).data('bmid') + '___';
- } else {
- form = $(f).closest('form');
- }
+ form = $(f).closest('form');
}
+ }
- $(form).find('[data-when]').each(function () {
- if ($(this).parent().hasClass('bunchmultiple__wrapper')) {
- $(this).parent().parent().attr('data-when', $(this).attr('data-when'));
- $(this).attr('data-when', null);
- }
- });
+ $(form).find('[data-when]').each(function () {
+ if ($(this).parent().hasClass('bunchmultiple__wrapper')) {
+ $(this).parent().parent().attr('data-when', $(this).attr('data-when'));
+ $(this).attr('data-when', null);
+ }
+ });
- $(form).find('[data-when]').removeClass('when-visible').each(function () {
- if ($(this).data('when-normalized') === undefined) {
- $(this).data('when-normalized', normalizeWhen($(this).data('when')));
- }
- var when = $(this).data('when-normalized');
+ $(form).find('[data-when]').removeClass('when-visible').each(function () {
+ if ($(this).data('when-normalized') === undefined) {
+ $(this).data('when-normalized', normalizeWhen($(this).data('when')));
+ }
+ var when = $(this).data('when-normalized');
- var match = true;
- var order = 1000;
- $.each(when, function (property, conditions) {
- var fname = prefix + property;
- var e = $(form).find('[name="' + fname + '"]');
- var val;
- if ($(e).length === 1) {
- val = $(e).val().toString();
+ var match = true;
+ var order = 1000;
+ $.each(when, function (property, conditions) {
+ var fname = prefix + property;
+ var e = $(form).find('[name="' + fname + '"]');
+ var val;
+ if ($(e).length === 1) {
+ val = $(e).val().toString();
+ } else {
+ var f = $(e).filter(':checked,:not(:checkbox)').last();
+ if ($(f).length === 0) {
+ val = '';
} else {
- var f = $(e).filter(':checked,:not(:checkbox)').last();
- if ($(f).length === 0) {
- val = '';
- } else {
- val = $(f).val().toString();
- }
+ val = $(f).val().toString();
}
- var matchproperty = false;
- $.each(conditions, function (k, condition) {
- if (condition.id === val) {
- matchproperty = true;
- order = Math.min(order, condition.order);
- return false;
- }
- });
- if (matchproperty === false) {
- match = false
+ }
+ var matchproperty = false;
+ $.each(conditions, function (k, condition) {
+ if (condition.id === val) {
+ matchproperty = true;
+ order = Math.min(order, condition.order);
return false;
}
});
-
- if (match) {
- $(this).addClass('when-visible');
- $(this).attr('data-when-order', order);
+ if (matchproperty === false) {
+ match = false
+ return false;
}
});
- orderWhen();
- };
+ if (match) {
+ $(this).addClass('when-visible');
+ $(this).attr('data-when-order', order);
+ }
+ });
- function isNumber(n) {
- return typeof n === 'number' && !isNaN(n);
- }
+ //orderWhen();
+ };
- $(document).on('change', 'input,select,textarea', function () {
+ function isNumber(n) {
+ return typeof n === 'number' && !isNaN(n);
+ }
+
+ $(document).on('change', 'input,select,textarea', function () {
+ triggersWhenChange($(this));
+ });
+ $(document).on('cubist.bunchmultiple.added', function () {
+ $('.subform[data-bmid]').each(function () {
triggersWhenChange($(this));
});
- $(document).on('cubist.bunchmultiple.added', function () {
- $('.subform[data-bmid]').each(function () {
- 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);
- });
- }
+ $('main form').each(function () {
+ triggersWhenChange($(this));
+ });
- function normalizeWhen(w) {
- var res = {};
- $.each(w, function (property, conditions) {
- if (!Array.isArray(conditions)) {
- conditions = [conditions];
+ 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};
}
- 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;
+ if (!condition.hasOwnProperty('order')) {
+ condition.order = k;
+ }
+ condition.id = condition.id.toString();
+ normalizedConditions[k] = condition;
});
- return res;
- }
+ res[property] = normalizedConditions;
+ });
+ return res;
+ }
- function isObject(value) {
- return value && typeof value === 'object' && value.constructor === Object;
- }
+ function isObject(value) {
+ return value && typeof value === 'object' && value.constructor === Object;
+ }
- });
+ });
- </script>
+</script>
@endpush
@push('crud_fields_styles')
- <style type="text/css">
- [data-when] {
- display: none;
- }
+<style type="text/css">
+ [data-when] {
+ display: none;
+ }
- .when-visible[data-when] {
- display: block;
- }
- </style>
+ .when-visible[data-when] {
+ display: block;
+ }
+</style>
@endpush