Skip to content

Commit

Permalink
feat(google): use formatValueForDisplay in ng-model
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarulg committed Oct 18, 2024
1 parent dc46108 commit bda1b59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
<b>{{ $ctrl.valueLabel }}</b>
</td>
<td>
<textarea class="form-control input input-sm" ng-model="pair.value" rows="4">
{{ $ctrl.formatValueForDisplay(pair.value) }}
</textarea
>
<textarea
class="form-control input input-sm"
ng-model="$ctrl.formatValueForDisplay[$index]"
rows="4"
ng-change="$ctrl.onValueChange($index)"
></textarea>
</td>
<td>
<div class="form-control-static">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ angular
this.onChange();
};

this.formatValueForDisplay = (value) => {
if (typeof value === 'object') {
return JSON.stringify(value, null, 2);
this.onValueChange = (index) => {
const formattedValue = this.formattedValues[index];

// Parse the JSON if it looks like an object or array, otherwise leave it as a string
try {
this.backingModel[index].value = JSON.parse(formattedValue);
} catch (e) {
this.backingModel[index].value = formattedValue; // Not JSON, so treat it as a string
}
return value;

// Sync changes with the model
this.synchronize();
};

this.formatValueForDisplay = (value) => {
return typeof value === 'object' ? JSON.stringify(value, null, 2) : value;
};

// Clears existing values from model, then replaces them
Expand Down Expand Up @@ -95,6 +106,8 @@ angular
this.backingModel.push({ key: key, value: this.model[key] });
});
}

this.formattedValues = this.backingModel.map((pair) => this.formatValueForDisplay(pair.value));
};

$scope.$watch(() => JSON.stringify(this.backingModel), this.synchronize);
Expand Down

0 comments on commit bda1b59

Please sign in to comment.