Skip to content

Commit

Permalink
Merge pull request #55 from legalthings/Numbers_with_unit_are_display…
Browse files Browse the repository at this point in the history
…ed_as_object_object

Fix for setting toString method for number with unit
  • Loading branch information
jasny authored May 12, 2017
2 parents 79b08aa + e3960b1 commit c418e7e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
17 changes: 4 additions & 13 deletions js/legalform.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,20 +536,11 @@ function LegalForm($) {
* @param {object} field Field data
*/
function addAmountDefaults(data, group, field) {
var fielddata = {};

Object.defineProperty(fielddata, 'toString', {
enumerable: false,
configurable: false,
get: function() {
return function() {
return this.amount !== '' ? this.amount + ' ' + this.unit : ''
}
}
});
var fielddata = {
amount: field.value !== '' ? field.value : "",
unit: field.value == 1 ? field.optionValue[0] : field.optionText[0]
};

fielddata.amount = field.value !== '' ? field.value : "";
fielddata.unit = fielddata.amount == 1 ? field.optionValue[0] : field.optionText[0];
addGroupedData(data, group, field.name, fielddata);
}

Expand Down
33 changes: 24 additions & 9 deletions js/ractive-legalform.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,16 @@

if (!newValue && oldValue !== undefined) {
var set = getByKeyPath(this.defaults, name, undefined);
if (typeof set === 'undefined') set = '';

if (typeof set === 'undefined') {
set = '';
} else if ($.type(set) === 'object') {
var toString = set.toString;
set = $.extend({}, set);

//$.extend does not copy hidden toString property, in case if we redefined it
defineProperty(set, 'toString', toString);
}

// Set field value to empty/default if condition is not true
this.set(name, set);
Expand Down Expand Up @@ -238,14 +247,20 @@
*/
initAmountField: function (key, meta) {
var amount = this.get(key);
if (!amount) return;

if (amount) {
defineProperty(amount, 'toString', function() {
return this.amount !== '' ? this.amount + ' ' + this.unit : ''
});
var toString = function() {
return (this.amount !== '' && this.amount !== null) ? this.amount + ' ' + this.unit : '';
};

this.update(key);
}
defineProperty(amount, 'toString', toString);
this.update(key);

var defaultValue = getByKeyPath(this.defaults, key, undefined);
if (!defaultValue) return;

defineProperty(defaultValue, 'toString', toString);
setByKeyPath(this.defaults, key, defaultValue);
},

/**
Expand Down Expand Up @@ -418,11 +433,11 @@
* Preview switch for mobile
*/
initPreviewSwitch: function () {

$('#nav-show-info, #nav-show-preview, #nav-show-form').on('click', function() {
$('#nav-show-info, #nav-show-preview, #nav-show-form').removeClass('active');
$(this).addClass('active');
});
});

$('#nav-show-info').on('click', function() {
$('#doc').removeClass('show-preview');
Expand Down

0 comments on commit c418e7e

Please sign in to comment.