hadron-document
Version:
Hadron Document
65 lines • 2.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const hadron_type_checker_1 = __importDefault(require("hadron-type-checker"));
const element_events_1 = require("../element-events");
const utils_1 = require("../utils");
const standard_1 = __importDefault(require("./standard"));
/**
* CRUD editor for double values.
*/
class DoubleEditor extends standard_1.default {
/**
* Complete the double edit by converting the valid string to a double
* value or leaving as invalid.
*/
complete() {
super.complete();
if (this.element.isCurrentTypeValid()) {
this.element.edit(hadron_type_checker_1.default.cast(this.element.currentValue, 'Double'));
}
}
/**
* Edit Double element. Check if the value is a Double before setting tped
* up value.
*
* @param {Object} value - The new value.
*/
edit(value) {
try {
const double = hadron_type_checker_1.default.cast(value, 'Double');
if (isNaN(double.value)) {
this.element.setInvalid(value, 'Double', `${String(value)} is not a valid double format`);
}
else {
this.element.currentValue = value;
this.element.setValid();
this.element._bubbleUp(element_events_1.ElementEvents.Edited, this.element);
}
}
catch (error) {
this.element.setInvalid(value, this.element.currentType, error.message);
}
}
/**
* Get the number of characters the value should display.
*
* @param {Boolean} editMode - If the element is being edited.
*
* @returns {Number} The number of characters.
*/
size() {
const currentValue = this.element.currentValue;
return (0, utils_1.fieldStringLen)(
// Not all values that will be checked here are bson types with a `value`
// property, using valueOf is a more resilient way of getting the "native"
// value from `currentValue`
typeof currentValue.valueOf === 'function'
? currentValue.valueOf()
: currentValue);
}
}
exports.default = DoubleEditor;
//# sourceMappingURL=double.js.map