Skip to content

Commit

Permalink
Merge branch 'master' into required_checkbox_does_not_work_for_option…
Browse files Browse the repository at this point in the history
…_groups
  • Loading branch information
svenstm authored Dec 6, 2018
2 parents ac091cb + ee774b2 commit b7ec94a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
2 changes: 2 additions & 0 deletions js/legalform-calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function LegalFormCalc($) {

if (type === 'group' && field.multiple) {
value = typeof(value) !== 'undefined' ? [value] : [];
} else if (type === 'money' && value) {
value = parseFloat(value);
}

addGroupedData(data, step.group, field.name, value);
Expand Down
69 changes: 45 additions & 24 deletions js/ractive-legalform.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@

if (isComputed) return;


this.onChangeMoney(newValue, oldValue, keypath);
this.onChangeAmount(newValue, oldValue, keypath);

var isEmpty = newValue === null ||
newValue === undefined ||
(typeof(newValue) === 'string' && !newValue.trim().length); //consider evalueted expressions, that have only spaces, as empty
Expand Down Expand Up @@ -207,6 +211,45 @@
}, 10);
},

/**
* Cast money to float
* @param {string} newValue
* @param {string} oldValue
* @param {string} keypath
*/
onChangeMoney: function(newValue, oldValue, keypath) {
var meta = this.get('meta.' + keypath);
var isMoney = typeof meta !== 'undefined' &&
typeof meta.type !== 'undefined' &&
meta.type === 'money';

if (isMoney && newValue) {
this.set(keypath, parseFloat(newValue));
}
},

/**
* Handle change of amount options from singular to plural, and backwords.
* @param {mixed} newValue
* @param {mixed} oldValue
* @param {string} keypath
*/
onChangeAmount: function(newValue, oldValue, keypath) {
var key = keypath.replace(/\.amount$/, '');
var meta = this.get('meta.' + key);
var isAmount = typeof meta !== 'undefined' &&
typeof meta.plural !== 'undefined' &&
typeof meta.singular !== 'undefined';

if (!isAmount) return;

var oldOptions = meta[newValue == 1 ? 'plural' : 'singular'];
var newOptions = meta[newValue == 1 ? 'singular' : 'plural'];
var index = oldOptions ? oldOptions.indexOf(this.get(key + '.unit')) : -1;

if (newOptions && index !== -1) this.set(key + '.unit', newOptions[index]);
},

/**
* We do not use computed for expression field itself, to avoid escaping dots in template,
* because in computed properties dots are just parts of name, and do not represent nested objects.
Expand Down Expand Up @@ -237,6 +280,7 @@
var ractive = this;
var name = unescapeDots(keypath.replace(this.suffix.repeater, ''));
var value = ractive.get(name);
var tmpl = typeof this.defaults[name] !== 'undefined' ? this.defaults[name][0] : {};
var repeater = newValue;
var stepCount = value.length;

Expand All @@ -245,7 +289,7 @@
else if (repeater > stepCount) {
var addLength = repeater - stepCount;
for (var i = 0; i < addLength; i++) {
value.push({});
value.push($.extend(true, {}, tmpl));
}
}

Expand Down Expand Up @@ -341,36 +385,13 @@
* Initialize special field types
*/
initField: function (key, meta) {
var amountFields = [];

if (meta.type === 'amount') {
amountFields.push(key + this.suffix.amount);
this.initAmountField(key, meta);
} else if (meta.type === 'external_data') {
this.initExternalData($.extend({name: key}, meta));
} else if (meta.external_source) {
this.initExternalSource($.extend({name: key}, meta));
}

this.initAmountChange(amountFields);
},

/**
* Init change of amount options from singular to plural, and backwords.
* This can not be processed in base form change observer, as it needs fields names.
* @param {array} fields All amount fields' names
*/
initAmountChange: function(fields) {
if (!fields.length) return;

this.observe(fields.join(' '), function(newValue, oldValue, keypath) {
var key = keypath.replace(/\.amount$/, '');
var oldOptions = this.get('meta.' + key + '.' + (newValue == 1 ? 'plural' : 'singular'));
var newOptions = this.get('meta.' + key + '.' + (newValue == 1 ? 'singular' : 'plural'));
var index = oldOptions ? oldOptions.indexOf(this.get(key + '.unit')) : -1;

if (newOptions && index !== -1) this.set(key + '.unit', newOptions[index]);
}, {defer: true});
},

/**
Expand Down

0 comments on commit b7ec94a

Please sign in to comment.