UNPKG

hadron-document

Version:
107 lines 3.4 kB
"use strict"; 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 date values. */ class DateEditor extends standard_1.default { /** * Create the editor with the element. * * @param {Element} element - The hadron document element. */ constructor(element) { super(element); } /** * Complete the date edit by converting the valid string to a date * object or leaving as invalid. */ complete() { super.complete(); if (this.element.isCurrentTypeValid()) { this.element.edit(hadron_type_checker_1.default.cast(this._formattedValue(), 'Date')); } } /** * Edit the element with the provided value. * * @param {Object} value - The new value. */ edit(value) { try { const date = hadron_type_checker_1.default.cast(value, 'Date'); if (date.toString() === 'Invalid Date') { this.element.setInvalid(value, 'Date', `${String(value)} is not in a valid date format`); } else { this.element.currentValue = value; this.element.setValid(); this.element._bubbleUp(element_events_1.ElementEvents.Edited, this.element); } } catch (e) { this.element.setInvalid(value, this.element.currentType, e.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(editMode) { const value = this.element.currentValue; if (editMode) { return (0, utils_1.fieldStringLen)(value); } return this.element.isCurrentTypeValid() ? (0, utils_1.fieldStringLen)(this._formattedValue()) : (0, utils_1.fieldStringLen)(value); } /** * Start the date edit. * * @param {Object} value - The value in the field. */ start() { super.start(); if (this.element.isCurrentTypeValid()) { this.edit(this._formattedValue()); } } /** * Get the value being edited. * * @param {Boolean} editMode - If the UI is in edit mode. * * @returns {String} The value. */ value() { const value = this.element.currentValue; if (!this.editing && this.element.isCurrentTypeValid()) { return this._formattedValue(); } return String(value); } _formattedValue() { // BSON Date are uint64_t ms, JS Date only supports float64 ms, // so some valid BSON Dates are not representable in JS. const date = new Date(this.element.currentValue); try { return date.toISOString().replace('Z', '+00:00'); } catch { return String(date); } } } exports.default = DateEditor; //# sourceMappingURL=date.js.map