"use strict";
/**
 * SubField Context
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework
 *
 *  Liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubFieldContext = void 0;
var FieldContext_1 = require("./FieldContext");
/**
 * Context responsible for a specific sub field (option)
 */
var SubFieldContext = /** @class */ (function (_super) {
    __extends(SubFieldContext, _super);
    function SubFieldContext() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        /**
         * Parent of field element within the content
         */
        _this._field_parent_element = null;
        /**
         * Field element within the content
         */
        _this._field_element = null;
        return _this;
    }
    /**
     * Attach the subfield to its parent
     *
     * @param to - Parent to attach to
     * @param next_element - Next element to attach before
     */
    SubFieldContext.prototype.attach = function (_to, _next_element) {
        if (this._field_element === null) {
            this._field_element = this._setFieldElement();
        }
        if (this._field_parent_element !== null) {
            this._field_parent_element.appendChild(this._field_element);
        }
        this.is_attached = true;
        this.is_visible = true;
    };
    /**
     * Detach the subfield from its parent
     */
    SubFieldContext.prototype.detach = function () {
        if (this._field_element === null) {
            this._field_element = this._setFieldElement();
        }
        if (this._field_element.parentElement !== null) {
            // save its parent before detaching
            this._field_parent_element = this._field_element.parentElement;
            this._field_parent_element.removeChild(this._field_element);
        }
        this.is_attached = false;
        this.is_visible = false;
    };
    /**
     * Query to get the field element
     */
    SubFieldContext.prototype._setFieldElement = function () {
        return this.content.querySelector('#' + this.element_id);
    };
    return SubFieldContext;
}(FieldContext_1.FieldContext));
exports.SubFieldContext = SubFieldContext;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU3ViRmllbGRDb250ZXh0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiU3ViRmllbGRDb250ZXh0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTs7Ozs7Ozs7Ozs7Ozs7Ozs7OztHQW1CRzs7Ozs7Ozs7Ozs7Ozs7OztBQUVILCtDQUl3QjtBQUV4Qjs7R0FFRztBQUNIO0lBQXFDLG1DQUFZO0lBQWpEO1FBQUEscUVBdURDO1FBdERDOztXQUVHO1FBQ0ssMkJBQXFCLEdBQTJCLElBQUksQ0FBQztRQUU3RDs7V0FFRztRQUNLLG9CQUFjLEdBQTJCLElBQUksQ0FBQzs7SUE4Q3hELENBQUM7SUE1Q0M7Ozs7O09BS0c7SUFDSCxnQ0FBTSxHQUFOLFVBQU8sR0FBbUIsRUFBRSxhQUFxQztRQUMvRCxJQUFJLElBQUksQ0FBQyxjQUFjLEtBQUssSUFBSSxFQUFFO1lBQ2hDLElBQUksQ0FBQyxjQUFjLEdBQUcsSUFBSSxDQUFDLGdCQUFnQixFQUFFLENBQUM7U0FDL0M7UUFFRCxJQUFJLElBQUksQ0FBQyxxQkFBcUIsS0FBSyxJQUFJLEVBQUU7WUFDdkMsSUFBSSxDQUFDLHFCQUFxQixDQUFDLFdBQVcsQ0FBQyxJQUFJLENBQUMsY0FBYyxDQUFDLENBQUM7U0FDN0Q7UUFFR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