Skip to content

Commit

Permalink
Merge pull request #119 from legalthings/repeater_issue_with_number_w…
Browse files Browse the repository at this point in the history
…ith_unit

Fix number with units for repeated step
  • Loading branch information
svenstm authored Dec 6, 2018
2 parents 4a213bf + a0541d1 commit cb2b590
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 @@ -207,6 +209,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 @@ -237,6 +261,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 +270,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 +366,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 cb2b590

Please sign in to comment.