--- /dev/null
+<?php
+
+return [
+ /*
+ |--------------------------------------------------------------------------
+ | Templates Language Lines
+ |--------------------------------------------------------------------------
+ |
+ | The following language lines are used for Cubist backpack
+ |
+ */
+ 'name' => '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'
+
+];
}
$(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) {
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;
+ }
+
});
</script>