Skip to content

Commit

Permalink
Fix number with units for repeated step
Browse files Browse the repository at this point in the history
  • Loading branch information
Minstel committed Nov 15, 2018
1 parent 56eed81 commit 8234859
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions js/ractive-legalform.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@

if (isComputed) return;

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 @@ -200,6 +202,28 @@
}, 10);
},

/**
* 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 @@ -230,6 +254,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 @@ -238,7 +263,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 @@ -334,36 +359,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 8234859

Please sign in to comment.