Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix number with units for repeated step #119

Merged
merged 3 commits into from
Dec 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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