"use strict";
const ADD_ROWS_ABOVE_AND_BELOW = 5;
const DBL_ADD_ROWS = ADD_ROWS_ABOVE_AND_BELOW * 2;
const MIN_ROW_FOR_RECREATE = 3;
const PICT_COL_SIZE_ADD = 6;
const LEVEL_OFFSET = 18;
const ICON_ROWS_BEFORE_AFTER = 3;
let sSclollSrc = "";
class CDinTableRow extends HTMLTableRowElement {
    constructor() {
        super(...arguments);
        this.selfNumber = -1;
    }
}
const FOLDER_CLOSE = -1;
const FOLDER_OPEN = 1;
const FOLDER_AUTO_OPEN = 2;
const FOLDER_EMPTY = 3;
const FOLDER_NO_FLDR_IN = 4;
const FOLDER_IS_HIDE_IN = 5;
class CBrControl {
    constructor(i = -1, p) {
        this.dep_ind = -1;
        this.jsBody = [];
        this.indJs = i;
        this.pParent = p;
    }
    fCloseAllFoldersDown() {
        if (!this.cont)
            return;
        if (this.folder != FOLDER_EMPTY) {
            this.folder = FOLDER_CLOSE;
            this.fCurtail();
        }
        for (const it of this.cont) {
            it.fCloseAllFoldersDown();
        }
    }
    AbortWaitBr() {
        if (this.fWaitingExit) {
            this.fWaitingExit();
        }
    }
    fFindPathEl(sElName, eDrill) {
        if (!this.cont || this.cont.length == 0 || !sElName)
            return;
        let ret = this.cont.find(it => it.pathEl == sElName);
        if (!ret && eDrill == 'down') {
            for (let i = 0; i < this.cont.length; i++) {
                ret = this.cont[i].fFindPathEl(sElName, eDrill);
                if (ret)
                    break;
            }
        }
        if (!ret && eDrill == 'up' && this.pParent) {
            ret = this.pParent.fFindPathEl(sElName, eDrill);
        }
        return ret;
    }
    fFindEl(sElName, sFieldName, eDrill, bCaseSens) {
        if (sElName && !this.cont)
            return;
        let ret;
        if (sFieldName) {
            if (!sElName) {
                if (sFieldName in this.fGetData()) {
                    ret = this;
                }
            }
            else if (bCaseSens) {
                ret = this.cont.find(it => it.fGetData()[sFieldName] == sElName);
            }
            else {
                ret = this.cont.find(it => it.fGetData()[sFieldName].toLowerCase() == sElName.toLowerCase());
            }
        }
        else if (sElName) {
            if (bCaseSens) {
                ret = this.cont.find(it => it.pathEl && it.pathEl == sElName);
            }
            else {
                ret = this.cont.find(it => it.pathEl && it.pathEl.toLowerCase() == sElName.toLowerCase());
            }
        }
        if (!ret) {
            if (eDrill == "down" && this.cont) {
                for (const oBr of this.cont) {
                    ret = oBr.fFindEl(sElName, sFieldName, "down", bCaseSens);
                    if (ret)
                        break;
                }
            }
            else if (eDrill == "up" && this.pParent) {
                ret = this.pParent.fFindEl(sElName, sFieldName, "up", bCaseSens);
            }
        }
        return ret;
    }
    fGetOpenFoldersTree() {
        const ret = {};
        if (!this.cont) {
            return undefined;
        }
        for (let it of this.cont) {
            if (it.folder == FOLDER_OPEN && it.pathEl) {
                ret[it.pathEl] = it.fGetOpenFoldersTree();
            }
        }
        if (this.cont.length > 0 && this.cont[this.cont.length - 1].bNextDataButton) {
            ret["////"] = this.jsBody.length - 1;
        }
        else {
            ret["////"] = this.jsBody.length;
        }
        if (Object.keys(ret).length > 0)
            return ret;
    }
    fCurtail() {
        if (this.cont) {
            for (const it of this.cont) {
                if (it.oIsWaiting) {
                    const oData = it.fGetData();
                    if (oData.__oAbort__) {
                        oData.__oAbort__.abort();
                    }
                    delete it.oIsWaiting;
                    delete it.fWaitingExit;
                    it.folder = FOLDER_CLOSE;
                }
                it.fCurtail();
                it.dep_ind = -1;
                delete it.bSelested;
            }
        }
    }
    fGetOpenBranchFolderArr() {
        if (!this.cont || !this.cont.length) {
            return [];
        }
        const ret = [];
        this.cont.map(it => { if (it.folder == FOLDER_OPEN) {
            ret.push(it.pathEl);
        } });
        return ret;
    }
    fSort(oDTP, sSortColName) {
        if (!this.cont || this.cont.length < 2) {
            return;
        }
        if (!sSortColName) {
            sSortColName = oDTP.sSortCol;
        }
        oDTP.SortBranch(this.cont, sSortColName);
    }
    fGetPathArr() {
        if (!this.pathEl)
            return [];
        if (this.pParent) {
            const ret = this.pParent.fGetPathArr();
            ret.push(this.pathEl);
            return ret;
        }
        else {
            return [this.pathEl];
        }
    }
    fGetPathStr(t) {
        return this.fGetPathArr().join(t);
    }
    fChangeAllData(xData, fDataProcessor) {
        const oJsBranch = this.fGetData();
        let oData = xData;
        if (fDataProcessor) {
            oData = fDataProcessor(xData);
        }
        SetSameObjFields(oData, oJsBranch);
    }
    fChangeOneField(xOld, xNew, sField, bPathEl) {
        const oBranch = this.fFindEl(xOld, sField);
        if (oBranch) {
            oBranch.fGetData()[sField] = xNew;
            if (bPathEl) {
                oBranch.pathEl = xNew;
            }
        }
        return oBranch;
    }
    fRecursiveOpen() {
        let ret = true;
        if (this.pParent) {
            const bParentFolderWasOpen = this.pParent.fRecursiveOpen();
            if (this.pParent.folder !== undefined) {
                ret = this.pParent.folder === FOLDER_OPEN && bParentFolderWasOpen;
                this.pParent.folder = FOLDER_OPEN;
            }
        }
        return ret;
    }
    fSetWaitingTimer(sText, bNotExit, bForce) {
        this.oIsWaiting = { text: "<div class='waiting_text'> " + sText + "</div>" + GetWaitingSvg(2), bNotExit, bForce };
    }
    fAddDataArr(arr, oParams) {
        if (!arr || !Array.isArray(arr) || arr.length == 0) {
            if (oParams && oParams.bClean) {
                this.jsBody.splice(0);
                if (this.cont) {
                    this.cont.splice(0);
                }
            }
            return;
        }
        if (!oParams) {
            oParams = {};
        }
        if (!this.cont) {
            this.cont = [];
        }
        else if (this.cont && this.cont.length > 0 && this.cont[this.cont.length - 1].bNextDataButton) {
            this.cont.splice(this.cont.length - 1, 1);
            this.jsBody.splice(this.jsBody.length - 1, 1);
        }
        else if (oParams.bClean) {
            this.cont.splice(0);
            this.jsBody.splice(0);
        }
        let ind = this.jsBody.length;
        let ret = undefined;
        if (oParams.sSpecValue) {
            if (!oParams.sFieldNameOfSpecValue) {
                oParams.sFieldNameOfSpecValue = oParams.sPathFieldName;
            }
        }
        if (oParams.bOpenBrAuto) {
            this.pParent.folder = FOLDER_OPEN;
        }
        if (!oParams.fDataProcessor && oParams.oDTP && oParams.oDTP.fDataProcessor) {
            oParams.fDataProcessor = oParams.oDTP.fDataProcessor;
        }
        if (!oParams.sPathFieldName && oParams.oDTP && oParams.oDTP.sPathFieldName) {
            oParams.sPathFieldName = oParams.oDTP.sPathFieldName;
        }
        const bObjRows = IsObject(arr[0]);
        if (!oParams.fDataProcessor && bObjRows) {
            this.jsBody = this.jsBody.concat(arr);
        }
        for (let j = 0; j < arr.length; j++, ind++) {
            const oTBC = new CBrControl(ind, this);
            if (oParams.fDataProcessor) {
                const oData = oParams.fDataProcessor(oTBC, arr[j]);
                if (!oData) {
                    ind--;
                    continue;
                }
                this.jsBody.push(oData);
            }
            else if (!bObjRows) {
                this.jsBody.push({ "-": arr[j] });
            }
            if (oParams.sPathFieldName && oParams.sPathFieldName in arr[j]) {
                oTBC.pathEl = arr[j][oParams.sPathFieldName];
            }
            if (oParams.oTdProperties) {
                oTBC.oTdProperties = oParams.oTdProperties;
            }
            if (oParams.oFunc) {
                if (Array.isArray(oParams.oFunc)) {
                    SetObjFieldsFromAnotherObj(oParams.oFunc[j], oTBC);
                }
                else {
                    SetObjFieldsFromAnotherObj(oParams.oFunc, oTBC);
                }
            }
            if (oParams.nInsertAboveRow !== undefined && oParams.nInsertAboveRow >= 0 && oParams.nInsertAboveRow < this.cont.length) {
                this.cont.splice(oParams.nInsertAboveRow, 0, oTBC);
                oParams.nInsertAboveRow++;
            }
            else {
                this.cont.push(oTBC);
            }
            if (oParams.bNoSelectWithShiftOrCtrl) {
                oTBC.bNoSelectWithShiftOrCtrl = true;
            }
            if (oParams.sSpecValue && oParams.sFieldNameOfSpecValue && arr[j][oParams.sFieldNameOfSpecValue].toLowerCase() == oParams.sSpecValue.toLowerCase()) {
                ret = oTBC;
            }
        }
        return ret;
    }
    fAddDatToParent(obj, oProp) {
        if (this.pParent)
            return this.pParent.fAddData(obj, oProp);
        return null;
    }
    fAddData(obj, oProp) {
        let i = this.jsBody.length;
        if (!this.cont) {
            this.cont = [];
        }
        const tmp = new CBrControl(i, this);
        if (oProp.fDataProcessor) {
            this.jsBody.push(oProp.fDataProcessor(tmp, obj));
        }
        else {
            this.jsBody.push(obj);
        }
        if (oProp.sPathFieldName && obj[oProp.sPathFieldName]) {
            tmp.pathEl = obj[oProp.sPathFieldName];
        }
        if (oProp.bOpenBrAuto) {
            tmp.cont = [];
            tmp.folder = FOLDER_OPEN;
        }
        if (oProp.bNoSelectWithShiftOrCtrl) {
            tmp.bNoSelectWithShiftOrCtrl = true;
        }
        if (oProp.oFunc) {
            SetObjFieldsFromAnotherObj(oProp.oFunc, tmp);
        }
        this.cont.push(tmp);
        return tmp;
    }
    fGetData() {
        return (this.pParent) ? this.pParent.jsBody[this.indJs] : {};
    }
    fSetData(sFieldName, xNewData) {
        if (!this.pParent)
            return;
        const oData = this.pParent.jsBody[this.indJs];
        oData[sFieldName] = xNewData;
    }
    fGetFullSize() {
        if (!this.cont)
            return 0;
        let summ = this.cont.length;
        for (const child of this.cont) {
            if (child.folder) {
                summ += child.fGetFullSize();
            }
        }
        return summ;
    }
    fClear() {
        this.jsBody = [];
        if (this.cont) {
            this.cont = [];
        }
        delete this.oIsWaiting;
        delete this.fWaitingExit;
        delete this.bError;
    }
    fDeleteSelf() {
        if (!this.pParent || !this.pParent.cont)
            return;
        const indCont = this.pParent.cont.findIndex(it => it == this);
        this.pParent.jsBody.splice(this.indJs, 1);
        this.pParent.cont.map(it => { if (it.indJs > this.indJs)
            it.indJs--; });
        this.pParent.cont.splice(indCont, 1);
    }
    fGetDataAsStr(aHead, oDTP, bDrillDown) {
        if (oDTP) {
            oDTP._aSelectRow.sort(compareNum);
            return oDTP._aSelectRow.map(it => aHead.map(sFN => (oDTP.aDeploedTable[it]._branch_).fGetData()[sFN.field_name]).join('\t')).join('\n');
        }
        if (!this.cont) {
            return "";
        }
        return this.cont.map(it => aHead.map(sFN => it.fGetData()[sFN.field_name]).join('\t')).join('\n');
    }
    fIsEmpty() {
        return !this.cont || this.cont.length === 0;
    }
    fMarkSelf(oDTP, bAdd) {
        if (!bAdd)
            oDTP.fSelTabClean();
        if (this.dep_ind < 0) {
            oDTP.ShowTable();
        }
        oDTP.fSelTabPush(this.dep_ind);
    }
    fMarkAndShow(oDTP) {
        this.fMarkSelf(oDTP);
        if (this.folder == FOLDER_OPEN) {
            oDTP.ShowTableWithOpenFolder(this);
        }
        else {
            oDTP.ShowMarkRowInCenter();
        }
    }
}
class CDynTabProp {
    constructor() {
        this.root = new CBrControl(-1);
        this.aDeploedTable = [];
        this.aHead = [];
        this._aSelectRow = [];
        this.aTableColsWidth = [];
        this.domContainer = undefined;
        this.domLimWindow = undefined;
        this.domTable = undefined;
        this.domBody = undefined;
        this.oFolderContTable = null;
        this.nBottomFalseRowHeight = 0;
        this.nFirstShowRow = 0;
        this.nLastShowRow = -1;
        this.nLastColWidth = 0;
        this.nRowHeight = 0;
        this.nScrollTop = 0;
        this.nHeadHeight = 0;
        this.nSortDirect = 1;
        this.nTopFalseRowHeight = 0;
        this.ind = -1;
        this.nHorizontalScroll = -1;
        this.nMinRowHeight = 1.25;
        this.nLines = -1;
        this.nWindowHeight = -1;
        this.sSortCol = "";
        this.unicId = "u" + GetCurrTime() + Math.random();
        this.bWithoutHead = false;
        this.bEditable = false;
        this.bNotSelRow = false;
        this.bOnlyOneSelRow = false;
        this.bWasStretch = false;
        this.bIsTree = false;
        this.bNeedSort = false;
        this.bNotObjRows = false;
        this.bStopScroll = false;
        this.bResetSelectIfClickOut = false;
        this.fRowClick = null;
        this.fUserResize = null;
        this.fRightBtn = null;
        this.fOpenFolder = null;
        this.fDblclick = null;
        this.fDataProcessor = null;
        this.fOnChange = null;
        this.fAutoDeploy = null;
        this.fFiltr = null;
        this.fKeyDown = null;
        this.fSaveColsSize = null;
        this.fGetColsSizeFromLocStor = null;
        this.fWaitingForce = null;
        this.fBackspace = null;
        this.oMeta = {};
    }
    _GetFirstAndLastDisplayedEls() {
        let nFirstDisplayEl = -1, nLastDisplayEl = -1;
        if (this.sIconField) {
            if (this.domContainer.children.length > 0) {
                const rect = this.domLimWindow.getBoundingClientRect();
                for (let i = 0; i < this.domContainer.children.length; i++) {
                    const it = this.domContainer.children[i];
                    if (it.getBoundingClientRect().top >= rect.top && it.children.length > 0) {
                        const icon = it.children[0];
                        nFirstDisplayEl = icon.ind;
                        break;
                    }
                }
                for (let i = this.domContainer.children.length - 1; i >= 0; i--) {
                    const it = this.domContainer.children[i];
                    if (it.getBoundingClientRect().bottom <= rect.bottom && it.children.length > 0) {
                        const icon = it.children[it.children.length - 1];
                        nLastDisplayEl = icon.ind;
                        break;
                    }
                }
            }
        }
        else {
            if (this.domBody.children.length > 0) {
                const rect = this.domLimWindow.getBoundingClientRect();
                for (let i = 0; i < this.domBody.children.length; i++) {
                    const row = this.domBody.children[i];
                    if (row.getBoundingClientRect().top >= rect.top) {
                        nFirstDisplayEl = row.selfNumber;
                        break;
                    }
                }
                for (let i = this.domBody.children.length - 1; i >= 0; i--) {
                    const row = this.domBody.children[i];
                    if (row.getBoundingClientRect().bottom <= rect.bottom) {
                        nLastDisplayEl = row.selfNumber;
                        break;
                    }
                }
            }
        }
        return { nFirstDisplayEl, nLastDisplayEl };
    }
    ShowTableWithOpenFolder(oBr) {
        this.ShowTable({ start: oBr.dep_ind, finish: oBr.dep_ind + oBr.fGetFullSize(), bStartPriority: true });
    }
    ShowMarkRowInCenter() {
        if (this.fGetSelTabLen() < 1) {
            if (this.sIconField) {
                this.ShowIcons();
            }
            else {
                this.ShowTable();
            }
            return;
        }
        const nFirstMarkRow = this.fGetSelRowNum();
        const nLastMarkRow = this.fGetSelRowNum(this.fGetSelTabLen() - 1);
        const nMarkRowsRange = nLastMarkRow - nFirstMarkRow + 1;
        if (!this.sIconField) {
            if (!this.nLines || this.nLines < 0) {
                this._CalcTableSizes();
            }
            if (nMarkRowsRange > this.nLines) {
                this.ShowTable({ start: nFirstMarkRow, bStartPriority: true });
            }
            else {
                let nNewFirstRow = nFirstMarkRow - (this.nLines - nMarkRowsRange) / 2;
                if (nNewFirstRow < 0)
                    nNewFirstRow = 0;
                this.ShowTable({ start: nNewFirstRow, bStartPriority: true });
            }
            return;
        }
        if (!this.rectIconLine) {
            this.ShowIcons(true);
            return;
        }
        const { nFirstDisplayEl, nLastDisplayEl } = this._GetFirstAndLastDisplayedEls();
        const nIconRows = Math.floor(this.domLimWindow.clientHeight / Math.max(this.rectIconLine.h, 1));
        const nIconNum = nIconRows * this.rectIconLine.n;
        if (nFirstDisplayEl > 0 && nLastDisplayEl > 0 && nFirstMarkRow >= nFirstDisplayEl && nLastMarkRow <= nLastDisplayEl)
            return;
        if (nMarkRowsRange <= nIconNum) {
            this.nFirstShowRow = nFirstMarkRow - (nIconNum - nMarkRowsRange) / 2;
        }
        else {
            this.nFirstShowRow = nFirstMarkRow - ICON_ROWS_BEFORE_AFTER * this.rectIconLine.n;
        }
        this.nFirstShowRow = Math.round(this.nFirstShowRow);
        const nAddBefore = this.nFirstShowRow % this.rectIconLine.n;
        this.nFirstShowRow -= nAddBefore;
        const nLastLineNum = this.root.cont.length % this.rectIconLine.n;
        let nRealLinesCnt = this.root.cont.length + (nLastLineNum ? this.rectIconLine.n - nLastLineNum : 0);
        if (this.nFirstShowRow + nIconNum + ICON_ROWS_BEFORE_AFTER * 2 > nRealLinesCnt) {
            this.nFirstShowRow = nFirstMarkRow - ICON_ROWS_BEFORE_AFTER * this.rectIconLine.n;
        }
        this.ShowIcons(true);
    }
    MarkTabRow(sFieldName, sMarkName, oBranch, bNotDrillDown, bCaseSens) {
        if (!sMarkName)
            return null;
        let oLocalBranch;
        if (!oBranch) {
            oLocalBranch = this.root;
        }
        else if ("_branch_" in oBranch) {
            oLocalBranch = oBranch._branch_;
        }
        else {
            oLocalBranch = oBranch;
        }
        if (!oLocalBranch.cont || !oLocalBranch.cont.length)
            return null;
        let oMarkBranch = oLocalBranch.fFindEl(sMarkName, sFieldName, bNotDrillDown ? "none" : "down", bCaseSens);
        if (oMarkBranch) {
            oMarkBranch.fMarkAndShow(this);
            TableRowClick(this, oMarkBranch);
        }
        return oMarkBranch;
    }
    fDoMarkRowsShow() {
        let firstRow = Math.floor(this.domLimWindow.scrollTop / this.nRowHeight) + 2;
        let lastRow = this.nLines + firstRow - 2;
        return (this._aSelectRow[0] >= firstRow && this._aSelectRow[0] <= lastRow &&
            this._aSelectRow[this._aSelectRow.length - 1] >= firstRow &&
            this._aSelectRow[this._aSelectRow.length - 1] <= lastRow);
    }
    fGetSelRowNum(i) {
        if (!i) {
            return this._aSelectRow[0];
        }
        return this._aSelectRow[i];
    }
    fGetSelRow(i) {
        return this.aDeploedTable[this._aSelectRow[i]];
    }
    fGetSelBranch(i) {
        if (this.aDeploedTable.length > this._aSelectRow[i])
            return this.aDeploedTable[this._aSelectRow[i]]._branch_;
        if (!this.bIsTree && this.root.cont && this.root.cont.length > this._aSelectRow[i])
            return this.root.cont[this._aSelectRow[i]];
        return undefined;
    }
    fGetSelTabLen() {
        return this._aSelectRow.length;
    }
    fSelTabClean(i) {
        if (i) {
            for (let it = i; it < this._aSelectRow.length; it++) {
                const j = this._aSelectRow[it];
                if (this.aDeploedTable.length > j && j >= 0) {
                    delete this.aDeploedTable[j]._branch_.bSelested;
                }
            }
            this._aSelectRow.splice(i);
        }
        else {
            this._aSelectRow.forEach(it => {
                if (it >= 0 && it < this.aDeploedTable.length) {
                    delete this.aDeploedTable[it]._branch_.bSelested;
                }
            });
            this._aSelectRow.splice(0);
        }
    }
    fDeleteRowBySelfNumber(nSelfNumber) {
        const i = this._aSelectRow.findIndex(it => it === nSelfNumber);
        if (i >= 0) {
            delete this.aDeploedTable[nSelfNumber]._branch_.bSelested;
            this._aSelectRow.splice(i, 1);
        }
    }
    fSelTabPush(item, i) {
        if (typeof item != 'number' || this._aSelectRow.includes(item))
            return;
        if (i === undefined || i >= this._aSelectRow.length) {
            this._aSelectRow.push(item);
        }
        else {
            this._aSelectRow.splice(i, 0, item);
        }
        if (this.aDeploedTable.length >= item + 1 && item >= 0) {
            this.aDeploedTable[item]._branch_.bSelested = true;
        }
    }
    fSelTabReplace(item, i) {
        if (typeof item != 'number')
            return;
        if (this.aDeploedTable.length >= this._aSelectRow[i] + 1 && this._aSelectRow[i] >= 0) {
            delete this.aDeploedTable[this._aSelectRow[i]]._branch_.bSelested;
        }
        if (this.aDeploedTable.length >= item + 1 && item >= 0) {
            this.aDeploedTable[item]._branch_.bSelested = true;
        }
        this._aSelectRow[i] = item;
    }
    fSelRowEnter(i) {
        const oRow = this.fGetSelRow(i);
        if (this.bEditable && !oRow._branch_.cont) {
            if (oRow._row_ && oRow._row_.firstChild) {
                oRow._row_.firstChild.dispatchEvent(new Event('dblclick'));
            }
        }
        else if (this.fDblclick) {
            DeleteAllDropMenu();
            this.fDblclick(oRow._branch_);
        }
        else if (oRow._branch_.cont) {
            if (oRow._branch_.folder === FOLDER_CLOSE) {
                DeleteAllDropMenu();
                this._OpenFolder(oRow._branch_, false);
                TableOpenFolder(this, oRow._branch_);
                this.ShowTable();
            }
            else {
                DeleteAllDropMenu();
                this._CloseFolder(oRow._branch_);
            }
            this.ShowTableWithOpenFolder(oRow._branch_);
        }
    }
    fGetLineByInd(i) {
        if (this.aDeploedTable.length > i)
            return this.aDeploedTable[i]._branch_;
        if (!this.bIsTree && this.root.cont && this.root.cont.length > i)
            return this.root.cont[i];
        return undefined;
    }
    fGetMarkArr(sFieldName) {
        const arr = [];
        for (let i = 0; i < this.fGetSelTabLen(); i++) {
            const oData = this.fGetSelBranch(i).fGetData();
            if (oData && sFieldName in oData) {
                arr.push(oData[sFieldName]);
            }
            else {
                let x = 0;
            }
        }
        return arr;
    }
    fMarkArr(arr, sFieldName, oBranch = this.root) {
        for (const key of arr) {
            const item = oBranch.fFindEl(key, sFieldName);
            if (item) {
                item.fMarkSelf(this, true);
            }
        }
    }
    CreateDomIcons() {
        if (!this.domLimWindow && !this.domContainer)
            return;
        if (!this.domLimWindow) {
            this.domLimWindow = this.domContainer;
        }
        else if (!this.domContainer) {
            this.domLimWindow.innerHTML = "";
            this.domContainer = document.createElement('div');
            this.domLimWindow.append(this.domContainer);
        }
        else if (!this.domLimWindow.contains(this.domContainer)) {
            this.domLimWindow.innerHTML = "";
            this.domLimWindow.append(this.domContainer);
        }
        this.domLimWindow.style.overflow = "auto";
        this.domLimWindow.tabIndex = 1;
        this.domLimWindow.classList.add("dt_lim_window");
        this.domContainer.classList.add("dt_icon_window");
        this.domContainer.innerHTML = "";
        this.domLimWindow.fKeyDown = (e) => {
            if (e.code == "ArrowUp" || e.code == "ArrowDown" || e.code == "ArrowRight" || e.code == "ArrowLeft") {
                e.preventDefault();
                if (this.fGetSelTabLen() === 0) {
                    const { nFirstDisplayEl } = this._GetFirstAndLastDisplayedEls();
                    this.fSelTabPush(nFirstDisplayEl);
                    this.ShowIcons();
                }
                else if (this.fGetSelTabLen() === 1) {
                    this.TableMoveSelIcon(e.code);
                }
            }
            else if (e.code == "Enter") {
                if (this.fGetSelTabLen() === 1) {
                    e.preventDefault();
                    if (e.ctrlKey) {
                        const oDepBranch = this.fGetSelRow(0);
                        if (oDepBranch && oDepBranch._row_) {
                            DeleteAllDropMenu();
                            const rect = oDepBranch._row_.getBoundingClientRect();
                            const oE = { clientX: rect.right, clientY: rect.bottom, target: oDepBranch._row_ };
                            if (oDepBranch._branch_.fRightBtn) {
                                oDepBranch._branch_.fRightBtn(oE, this.fGetSelBranch(0), this);
                            }
                            else if (this.fRightBtn) {
                                this.fRightBtn(oE, this.fGetSelBranch(0), this);
                            }
                        }
                    }
                    else {
                        this.fSelRowEnter(0);
                    }
                }
            }
            else if (e.code == "Backspace" && this.fBackspace) {
                e.preventDefault();
                this.fBackspace();
            }
            else if (this.fKeyDown) {
                this.fKeyDown(e);
            }
        };
        this.domLimWindow.onclick = () => {
            if (this.fGetSelTabLen() > 0) {
                this.fSelTabClean();
                this.ShowIcons();
            }
        };
        this.domLimWindow.addEventListener('contextmenu', () => {
            if (this.fGetSelTabLen() > 0) {
                this.fSelTabClean();
                this.ShowIcons();
            }
        });
        const oDTP1 = this;
        this.domLimWindow.onscroll = function () {
            oDTP1.ShowTableAfterScroll();
        };
        const oDomLimWin = this.domLimWindow;
        if (oDomLimWin.fUserResize) {
            if (oDomLimWin.fUserResize.name != "LimWindowUserResize") {
                const fPrev = oDomLimWin.fUserResize;
                oDomLimWin.fUserResize = function LimWindowUserResize() {
                    if (WasResize(oDomLimWin)) {
                        oDTP1.ShowTableAfterResize();
                        if (fPrev) {
                            fPrev();
                        }
                    }
                };
            }
        }
        else {
            oDomLimWin.fUserResize = function LimWindowUserResize() {
                if (WasResize(oDomLimWin)) {
                    oDTP1.ShowTableAfterResize();
                }
            };
        }
        this.domLimWindow.classList.add("I_have_user_resise");
        this.domTable = undefined;
        this.domBody = undefined;
        delete this.domHead;
    }
    CreateDomList() {
        if (!this.domLimWindow && !this.domContainer)
            return;
        if (!this.domLimWindow) {
            this.domLimWindow = this.domContainer;
        }
        else if (!this.domContainer) {
            this.domLimWindow.innerHTML = "";
            this.domContainer = document.createElement('div');
            this.domLimWindow.append(this.domContainer);
        }
        else if (!this.domLimWindow.contains(this.domContainer)) {
            this.domLimWindow.innerHTML = "";
            this.domLimWindow.append(this.domContainer);
        }
        if (this.bNoneScrollX) {
            this.domLimWindow.style.overflowY = "auto";
            this.domLimWindow.style.overflowX = "hidden";
        }
        else {
            this.domLimWindow.style.overflow = "auto";
        }
        this.domLimWindow.tabIndex = 1;
        this.domContainer.classList.remove("dt_icon_window");
        this.domLimWindow.classList.add("dt_lim_window");
        this.domContainer.innerHTML = "";
        this.domLimWindow.fKeyDown = (e) => {
            if (e.code == "ArrowUp" || e.code == "ArrowDown") {
                e.preventDefault();
                if (this.fGetSelTabLen() === 0) {
                    this.fSelTabPush(0);
                    this.ShowFirstSelectRow();
                    TableRowClick(this, this.aDeploedTable[0]._branch_, true);
                }
                else if (this.fGetSelTabLen() === 1) {
                    this.TableMoveSelRow(e.code);
                }
            }
            else if (e.code == "ArrowRight" && this.oFolderContTable) {
                e.preventDefault();
                this.oFolderContTable.domLimWindow.focus();
                if (this.oFolderContTable.fGetSelTabLen() === 0) {
                    const { nFirstDisplayEl, nLastDisplayEl } = this.oFolderContTable._GetFirstAndLastDisplayedEls();
                    this.oFolderContTable.fSelTabPush(nFirstDisplayEl);
                    this.oFolderContTable.ShowRowsOrIcons();
                }
                else {
                    this.oFolderContTable.ShowFirstSelectRow();
                }
            }
            else if (e.code == "Enter") {
                if (this.fGetSelTabLen() === 1) {
                    if (e.ctrlKey) {
                        const oDepBranch = this.fGetSelRow(0);
                        if (oDepBranch && oDepBranch._row_) {
                            DeleteAllDropMenu();
                            const rect = oDepBranch._row_.getBoundingClientRect();
                            const oE = { clientX: rect.right, clientY: rect.bottom, target: oDepBranch._row_ };
                            if (oDepBranch._branch_.fRightBtn) {
                                oDepBranch._branch_.fRightBtn(oE, this.fGetSelBranch(0), this);
                            }
                            else if (this.fRightBtn) {
                                this.fRightBtn(oE, this.fGetSelBranch(0), this);
                            }
                        }
                    }
                    else {
                        this.fSelRowEnter(0);
                    }
                    e.preventDefault();
                }
            }
            else if (e.code == "Backspace" && this.fBackspace) {
                e.preventDefault();
                this.fBackspace();
            }
            else if (this.fKeyDown) {
                this.fKeyDown(e);
            }
        };
        this.domLimWindow.onclick = () => {
            if (this.fGetSelTabLen() > 0) {
                this.fSelTabClean();
                this.ShowRows();
            }
        };
        this.domLimWindow.addEventListener('contextmenu', () => {
            if (this.fGetSelTabLen() > 0) {
                this.fSelTabClean();
                this.ShowRows();
            }
        });
        const oDTP1 = this;
        this.domLimWindow.onscroll = function () {
            oDTP1.ShowTableAfterScroll();
        };
        const oDomLimWin = this.domLimWindow;
        if (oDomLimWin.fUserResize) {
            if (oDomLimWin.fUserResize.name != "LimWindowUserResize") {
                const fPrev = oDomLimWin.fUserResize;
                oDomLimWin.fUserResize = function LimWindowUserResize() {
                    if (WasResize(oDomLimWin)) {
                        oDTP1.ShowTableAfterResize();
                        if (fPrev) {
                            fPrev();
                        }
                    }
                };
            }
        }
        else {
            this.domLimWindow.fUserResize = function LimWindowUserResize() {
                if (WasResize(oDomLimWin)) {
                    oDTP1.ShowTableAfterResize();
                }
            };
        }
        this.domLimWindow.classList.add("I_have_user_resise");
        let table = document.createElement('table');
        table.style.width = "100%";
        if (this.bResetSelectIfClickOut) {
            table.classList.add("ResetSelectIfClickOut");
        }
        table.pTable = this;
        table.id = this.unicId + "_whole_table";
        this.domContainer.append(table);
        let head;
        if (!this.bWithoutHead && this.aHead.length > 0) {
            head = table.createTHead();
            let row = head.insertRow(0);
            if (this.bLineNumbers)
                row.insertCell(-1);
            head.className = "dyn_tab_head";
            head.ondblclick = function () {
                oDTP1.bWasStretch = false;
                oDTP1.CreateListElements();
            };
            for (let i = 0; i < this.aHead.length; i++) {
                const td = row.insertCell(-1);
                let cell = document.createElement('div');
                td.append(cell);
                if (this.bNeedSort && this.aHead[i].field_name == this.sSortCol)
                    cell.innerHTML += (this.nSortDirect > 0) ? "&#9650" : "&#9660;";
                cell.className = "dyn_tab_col_head dyn_cell";
                if (this.aHead[i].align) {
                    cell.style.textAlign = this.aHead[i].align;
                }
                else if (this.aHead[i].bool) {
                    cell.style.textAlign = "center";
                }
                if ((i + 1) < this.aHead.length) {
                    if (!this.aHead[i].img) {
                        cell.className += " stretch_x";
                        cell.style.width = "100%";
                        cell["sDriven"] = this.unicId + this.aHead[i].field_name;
                        cell["domLimEl"] = this.domLimWindow;
                        if (!this.aTableColsWidth[i])
                            this.aTableColsWidth[i] = -1;
                        cell["oFeedback"] = { arr: this.aTableColsWidth, i: i, oControl: this };
                    }
                }
                else {
                    cell.style.display = "inline-block";
                    this.nLastColWidth = cell.getBoundingClientRect().width;
                    cell.style.display = "";
                    cell.style.width = "100%";
                }
                if ("img" in this.aHead[i]) {
                    if (this.aHead[i].w) {
                        cell.style.width = this.aHead[i].w;
                    }
                    else {
                        cell.style.width = "calc(var(--horizontal-img-btn-size)*1.4)";
                    }
                    cell.innerHTML = "&nbsp;";
                    cell.classList.add("dt_img_head_cell");
                    td.style.width = "0";
                }
                else {
                    cell.innerHTML = "<span class='dyn_tab_head_cell_inner'>" + GetLocaleText(this.aHead[i].col_name) + "</span>";
                }
                cell["id"] = this.unicId + this.aHead[i].field_name + "_head";
                if (oDTP1.bNeedSort) {
                    let sFieldName = this.aHead[i].field_name;
                    cell.onclick = function () {
                        if (oDTP1.sSortCol == sFieldName) {
                            oDTP1.nSortDirect = -oDTP1.nSortDirect;
                        }
                        else {
                            oDTP1.sSortCol = sFieldName;
                        }
                        oDTP1.SortFunc(sFieldName);
                    };
                }
            }
            this.nHeadHeight = head.getBoundingClientRect().height;
        }
        let body = table.createTBody();
        body.className = "dyn_tab_body";
        if (this.bStriped) {
            body.className += " striped_children";
        }
        this["domTable"] = table;
        this["domHead"] = head;
        this["domBody"] = body;
    }
    _CreateIconLines(nIconLines, i, fragment) {
        const height = this.rectIconLine.h * Math.ceil(i / this.rectIconLine.n);
        let j = 0;
        for (; j < nIconLines && i < this.root.cont.length; j++) {
            const line = document.createElement("div");
            line.className = "dt_icons_line";
            line.style.top = (height + j * this.rectIconLine.h) + "px";
            fragment.append(line);
            for (let k = 0; k < this.rectIconLine.n && i < this.root.cont.length; k++) {
                let el = this._CreateIcon(i);
                this._SetElListeners(el, this.root.cont[i], this, i);
                line.append(el);
                i++;
            }
        }
        return j;
    }
    _CreateIcon(i) {
        let el = document.createElement("div");
        el.ind = i;
        this.domContainer.append(el);
        let oTableRow = this.root.cont[i].fGetData();
        if (oTableRow.icon) {
            const img = SetImg(oTableRow.icon, "var(--med-table-of-icon-size)", "med_icon_img");
            if (img) {
                el.append(img);
            }
        }
        let elInner = document.createElement("div");
        elInner.className = "short_icon_text";
        elInner.append(oTableRow[this.sIconField]);
        el.append(elInner);
        if (this.root.cont[i].bSelested) {
            el.classList.add("sel_row");
        }
        if (this.aHead) {
            let sTitle = "";
            for (const col of this.aHead) {
                if (!col.img && col.field_name in oTableRow) {
                    let sCellInner = oTableRow[col.field_name] + "";
                    if ("time" in col) {
                        if (sCellInner == "-1" || sCellInner == " ")
                            sCellInner = "";
                        else if (sCellInner == "") { }
                        else
                            sCellInner = GetDTString(sCellInner);
                    }
                    else if ("locale_number" in col) {
                        sCellInner = ToLocaleNumber(sCellInner, col.locale_number);
                    }
                    if (sCellInner) {
                        sTitle = (sTitle ? sTitle + "\n" : "") + (col.col_name ? col.col_name + ": " : "") + sCellInner;
                    }
                }
            }
            el.title = sTitle;
        }
        return el;
    }
    ShowRowsOrIcons() {
        if (this.domBody) {
            this.ShowRows();
        }
        else {
            this.ShowIcons();
        }
    }
    ShowIcons(bCalcScroll, bDeploy) {
        if (!this.sIconField || !this.domContainer || !this.root.cont)
            return;
        if (this.root.cont.length == 0) {
            this.domContainer.innerHTML = "";
            return;
        }
        this.nHorizontalScroll = this.domLimWindow.scrollLeft;
        if (!this.rectIconLine) {
            this.domContainer.innerHTML = "";
            let line = document.createElement("div");
            line.className = "dt_icons_line";
            let el = document.createElement("div");
            line.append(el);
            this.domContainer.append(line);
            let oTableRow = this.root.cont[0].fGetData();
            if (oTableRow.icon) {
                const img = SetImg("folder", "var(--med-table-of-icon-size)", "med_icon_img");
                if (img) {
                    let div = document.createElement("div");
                    div.append(img);
                    el.append(div);
                }
            }
            let elInner = document.createElement("div");
            elInner.className = "short_icon_text";
            elInner.append(oTableRow[this.sIconField]);
            el.append(elInner);
            let h = Math.ceil(line.getBoundingClientRect().height);
            this.domContainer.style.minHeight = (this.domLimWindow.clientHeight * 2) + "px";
            const n = GetIconsPerRow(this.domContainer, el);
            this.rectIconLine = { h, n };
            this.domContainer.innerHTML = "";
            this.domContainer.style.minHeight = (this.rectIconLine.h * Math.ceil(this.root.jsBody.length / this.rectIconLine.n)) + "px";
        }
        const nIconLines = Math.floor(this.domLimWindow.clientHeight / this.rectIconLine.h) + ICON_ROWS_BEFORE_AFTER * 2;
        const nIconNum = this.rectIconLine.n * nIconLines;
        this.nFirstShowRow = Math.round(this.nFirstShowRow);
        const nAddBefore = this.nFirstShowRow % this.rectIconLine.n;
        this.nFirstShowRow -= nAddBefore;
        const fragment = document.createDocumentFragment();
        if (this.nFirstShowRow + nIconNum > this.root.cont.length) {
            const nFictLen = Math.ceil(this.root.cont.length / this.rectIconLine.n) * this.rectIconLine.n;
            this.nFirstShowRow = nFictLen - nIconNum;
        }
        if (this.nFirstShowRow < 0)
            this.nFirstShowRow = 0;
        let height = this.rectIconLine.h * Math.ceil(this.nFirstShowRow / this.rectIconLine.n);
        if (bDeploy) {
            this.aDeploedTable = [];
            for (let i = 0; i < this.root.cont.length; i++) {
                this.aDeploedTable.push({ _branch_: this.root.cont[i] });
                this.root.cont[i].dep_ind = i;
            }
        }
        this._CreateIconLines(nIconLines, this.nFirstShowRow, fragment);
        if (bCalcScroll && this.nFirstShowRow > 0) {
            this.nScrollTop = this.rectIconLine.h * (height + ICON_ROWS_BEFORE_AFTER);
            this.domLimWindow.scrollTop = this.nScrollTop;
        }
        this.domContainer.replaceChildren(fragment);
    }
    ShowRows(oRange = {}) {
        if (oRange.start || oRange.finish) {
            this._CalcScroll(oRange);
        }
        else {
            this._CreateRows(oRange.bCalcScroll);
        }
    }
    async ShowTable(oRange) {
        this.aDeploedTable.splice(0);
        this._aSelectRow.splice(0);
        this._DeployTable(0);
        if (this.aDeploedTable.length <= this.nLines) {
            this.nFirstShowRow = 0;
            this.nLastShowRow = this.aDeploedTable.length - 1;
        }
        else if (this.nFirstShowRow > this.aDeploedTable.length - 1) {
            this.nLastShowRow = this.aDeploedTable.length - 1;
            this.nFirstShowRow = (this.nLastShowRow - this.nLines - DBL_ADD_ROWS >= 0) ? this.nLastShowRow - this.nLines - DBL_ADD_ROWS : 0;
        }
        else if (this.nLines > 0) {
            this.nLastShowRow = this.nFirstShowRow + this.nLines + DBL_ADD_ROWS;
        }
        await this._CalcMinContainerHeight();
        this.ShowRows(oRange);
        DrawDrag();
    }
    _SetElListeners(row, oBr, oDTP1, nThisRowNumber, row1) {
        row.onclick = (e) => {
            e.preventDefault();
            e.stopPropagation();
        };
        row.addEventListener('mouseup', function (e) {
            if (e.button === 0) {
                DeleteAllDropMenu();
                if (e.shiftKey) {
                    if (document) {
                        const tmp = document.getSelection();
                        if (tmp) {
                            tmp.empty();
                        }
                    }
                }
                e.preventDefault();
                e.stopPropagation();
                if ((e.ctrlKey || e.shiftKey) && oDTP1.fGetLineByInd(nThisRowNumber).bNoSelectWithShiftOrCtrl)
                    return;
                if ((e.ctrlKey || e.shiftKey) && oDTP1._aSelectRow.length == 1 && oDTP1.fGetSelBranch(0).bNoSelectWithShiftOrCtrl) {
                    oDTP1.fSelTabClean();
                }
                if (e.shiftKey && oDTP1._aSelectRow.length == 1 && !oDTP1.bOnlyOneSelRow) {
                    let nInc = (nThisRowNumber > oDTP1._aSelectRow[0]) ? 1 : -1;
                    for (let i = oDTP1._aSelectRow[0] + nInc; i != (nThisRowNumber + nInc); i += nInc) {
                        if (oDTP1.fGetLineByInd(i).bNoSelectWithShiftOrCtrl || oDTP1.aDeploedTable.length > i && oDTP1.aDeploedTable[i]._level_ != oDTP1.aDeploedTable[oDTP1._aSelectRow[0]]._level_)
                            break;
                        oDTP1.fSelTabPush(i);
                    }
                    oDTP1._aSelectRow.sort(compareNum);
                }
                else if (e.ctrlKey && nThisRowNumber !== undefined && oDTP1._aSelectRow.includes(nThisRowNumber)) {
                    oDTP1.fDeleteRowBySelfNumber(nThisRowNumber);
                }
                else if ((e.ctrlKey || e.shiftKey) && !oDTP1.bOnlyOneSelRow) {
                    if (nThisRowNumber !== undefined) {
                        if (oDTP1._aSelectRow.length > 0) {
                            let nInc = (nThisRowNumber > oDTP1._aSelectRow[0]) ? 1 : -1;
                            for (let i = oDTP1._aSelectRow[0] + nInc; i != (nThisRowNumber + nInc); i += nInc) {
                                if (oDTP1.fGetLineByInd(i).bNoSelectWithShiftOrCtrl || oDTP1.aDeploedTable.length > i && oDTP1.aDeploedTable[i]._level_ != oDTP1.aDeploedTable[oDTP1._aSelectRow[0]]._level_) {
                                    oDTP1.fSelTabClean();
                                    break;
                                }
                            }
                        }
                        oDTP1.fSelTabPush(nThisRowNumber);
                        oDTP1._aSelectRow.sort(compareNum);
                    }
                }
                else {
                    oDTP1.fSelTabClean();
                    oDTP1.fSelTabPush(nThisRowNumber);
                    TableRowClick(oDTP1, oBr, true);
                }
                if (!oDTP1.bNotSelRow)
                    oDTP1._SetRowAsSelect();
            }
        });
        row.addEventListener('contextmenu', function (e) {
            e.preventDefault();
            e.stopPropagation();
            DeleteAllDropMenu();
            if (!oDTP1._aSelectRow.includes(nThisRowNumber) || oDTP1.bOnlyOneSelRow) {
                oDTP1.fSelTabClean();
                oDTP1.fSelTabPush(nThisRowNumber);
                if (!oDTP1.bNotSelRow)
                    oDTP1._SetRowAsSelect();
            }
            if (oBr && oBr["fRightBtn"])
                oBr.fRightBtn(e, oBr, oDTP1);
            else if (oDTP1["fRightBtn"])
                oDTP1.fRightBtn(e, oBr, oDTP1);
        });
        if (oBr.fDblclick) {
            row.addEventListener('dblclick', (e) => {
                e.stopPropagation();
                if (e.ctrlKey || e.shiftKey)
                    return;
                oBr.fDblclick(oBr, oDTP1);
            });
        }
        else if (oDTP1.fDblclick) {
            row.addEventListener('dblclick', (e) => {
                e.stopPropagation();
                if (e.ctrlKey || e.shiftKey)
                    return;
                oDTP1.fDblclick(oBr, oDTP1);
            });
        }
        else if (!oDTP1.bEditable && oDTP1.bIsTree && "folder" in oBr && row1) {
            row.addEventListener('dblclick', (e) => {
                e.stopPropagation();
                if (e.ctrlKey || e.shiftKey)
                    return;
                if (oBr.folder === FOLDER_CLOSE) {
                    row1.fOpenFolder();
                }
                else if (oBr.folder !== FOLDER_EMPTY) {
                    DeleteAllDropMenu();
                    oDTP1._CloseFolder(oBr);
                }
                oDTP1.ShowTable();
            });
        }
    }
    __CreateRow(row, oTableRow, i, sRowH) {
        row.className = "dyn_tab_row";
        if (oTableRow)
            oTableRow._row_ = row;
        else {
            let x = 0;
        }
        if (oTableRow._branch_.bDeadRow) {
            row.className += " dead_img";
        }
        row.selfNumber = i;
        row.style.height = sRowH;
        const oDTP1 = this;
        const oBr = oTableRow._branch_;
        const row1 = row;
        const nThisRowNumber = row.selfNumber;
        row.fOpenFolder = function (bDontOpen) {
            DeleteAllDropMenu();
            oDTP1._OpenFolder(oBr, bDontOpen);
            TableOpenFolder(oDTP1, oBr, bDontOpen);
            oDTP1.ShowTableWithOpenFolder(oBr);
            if (oDTP1.oFolderContTable && oDTP1.oFolderContTable.oParentBr && oDTP1.oFolderContTable.oParentBr.pathEl) {
                const oDisplayedBr = oBr.fFindPathEl(oDTP1.oFolderContTable.oParentBr.pathEl, 'down');
                if (oDisplayedBr) {
                    oDisplayedBr.fMarkAndShow(oDTP1);
                    TableRowClick(oDTP1, oDisplayedBr);
                }
            }
        };
        this._SetElListeners(row, oBr, oDTP1, nThisRowNumber, row1);
        let span = "";
        if (oDTP1.bIsTree) {
            span = document.createElement("span");
            span.className = "tree_control nonselect";
            span.style.marginLeft = (oTableRow._level_ * LEVEL_OFFSET) + "px";
            if ("folder" in oTableRow._branch_) {
                if (oTableRow._branch_.folder == FOLDER_AUTO_OPEN) {
                    span.onmouseup = function (e) {
                        if (e.button === 0) {
                            e.stopPropagation();
                            row1.fOpenFolder();
                        }
                    };
                    const img = SetImg("chevron-right", "var(--horizontal-img-btn-size)");
                    if (img) {
                        span.append(img);
                    }
                    else {
                        span.innerHTML = "&#128898;";
                    }
                }
                else if (oTableRow._branch_.folder == FOLDER_OPEN) {
                    span.onmouseup = function (e) {
                        if (e.button === 0) {
                            e.stopPropagation();
                            DeleteAllDropMenu();
                            if (oTableRow._branch_.fCloseFolder) {
                                oTableRow._branch_.fCloseFolder();
                            }
                            oDTP1._CloseFolder(oBr);
                        }
                    };
                    const img = SetImg("chevron-down", "var(--horizontal-img-btn-size)");
                    if (img) {
                        span.append(img);
                    }
                    else {
                        span.innerHTML = "&#128899;";
                    }
                }
                else if (oTableRow._branch_.folder == FOLDER_CLOSE) {
                    span.onmouseup = function (e) {
                        if (e.button === 0) {
                            e.stopPropagation();
                            row1.fOpenFolder();
                        }
                    };
                    const img = SetImg("chevron-right", "var(--horizontal-img-btn-size)");
                    if (img) {
                        span.append(img);
                    }
                    else {
                        span.innerHTML = "&#128898;";
                    }
                }
            }
        }
        let td;
        if (this.bLineNumbers) {
            td = row.insertCell(-1);
            td.style.background = "#ddd";
            td.innerHTML = i + "";
        }
        let aTmpHead = this.aHead;
        if (aTmpHead.length == 0)
            aTmpHead = [{ "field_name": "-", "col_name": "-" }];
        for (let j = 0; j < aTmpHead.length; j++) {
            let oColHead = aTmpHead[j];
            td = row.insertCell(-1);
            let cell = document.createElement("div");
            cell.className = "dyn_tab_cell " + this.unicId + oColHead.field_name;
            if (this.sCellClass) {
                cell.className += " " + this.sCellClass;
            }
            if (oColHead.class) {
                cell.classList.add(oColHead.class);
            }
            td.append(cell);
            const data = oTableRow._branch_.fGetData() || {};
            let sCellInner = "" + data[oColHead.field_name];
            if (sCellInner == "undefined")
                sCellInner = " ";
            if (j == 0) {
                cell.append(span);
                if (oTableRow._wait_text_) {
                    let span2 = document.createElement("span");
                    span2.className = "nonselect flex_gor";
                    span2.innerHTML += oTableRow._wait_text_;
                    span2.onclick = function (e) {
                        e.stopPropagation();
                        e.preventDefault();
                        if (oTableRow._branch_.fGetData()["fWaitingExit"]) {
                            oTableRow._branch_.fGetData().fWaitingExit(oTableRow._branch_);
                        }
                        else {
                            oTableRow._branch_.folder = FOLDER_CLOSE;
                            oDTP1.ShowTable();
                        }
                    };
                    cell.append(span2);
                    return false;
                }
            }
            if ("img" in oColHead) {
                if (sCellInner != "-" && sCellInner.trim()) {
                    let img = SetImg(sCellInner, "var(--horizontal-img-btn-size)");
                    if (img) {
                        cell.style.width = "var(--horizontal-img-btn-size)";
                        cell.style.marginLeft = "2px";
                        if (oTableRow._branch_.oTdProperties) {
                            img.classList.add(oTableRow._branch_.oTdProperties[aTmpHead[j].field_name]);
                        }
                        cell.append(img);
                    }
                }
            }
            else if (oColHead.bool) {
                let img;
                if (!sCellInner || sCellInner === 'false') {
                    img = SetImg("checkbox-false", "var(--horizontal-img-btn-size)");
                }
                else {
                    img = SetImg("checkbox-true", "var(--horizontal-img-btn-size)");
                }
                cell.style.width = "var(--horizontal-img-btn-size)";
                cell.style.marginLeft = "45%";
                if (img) {
                    if (oTableRow._branch_.oTdProperties) {
                        img.classList.add(oTableRow._branch_.oTdProperties[aTmpHead[j].field_name]);
                    }
                    cell.append(img);
                }
                if (this.bEditable) {
                    cell["i"] = i;
                    cell["j"] = j;
                    td.addEventListener('dblclick', function () {
                        oDTP1.SetEditableBool(cell);
                    });
                }
            }
            else {
                const nPercent = (!oColHead.progressbar) ? null : GetPercent(sCellInner);
                let div2;
                if (nPercent !== null) {
                    cell.classList.add("cell_for_pb");
                    div2 = document.createElement("div");
                    let def_pb_class;
                    if (oTableRow._branch_.oTdProperties) {
                        def_pb_class = oTableRow._branch_.oTdProperties[aTmpHead[j].field_name + "_pb_class"];
                    }
                    if (!def_pb_class) {
                        def_pb_class = "def_pb_color";
                    }
                    div2.classList.add(def_pb_class);
                    div2.style.width = nPercent + "%";
                    div2.style.height = "1rem";
                    cell.append(div2);
                    let div3 = document.createElement("span");
                    div3.className = "table_percent_num";
                    div3.innerHTML = sCellInner;
                    cell.append(div3);
                }
                else {
                    if (oColHead.align) {
                        switch (oColHead.align) {
                            case "right":
                                cell.style.justifyContent = "flex-end";
                                break;
                            case "center":
                                cell.style.justifyContent = "center";
                                break;
                        }
                    }
                    if (("icon" in oColHead) && (oColHead.icon in data)) {
                        let img = SetImg(data[oColHead.icon], "var(--horizontal-img-btn-size)");
                        if (img) {
                            img.classList.add("dyn_tab_cell_icon");
                            if (oTableRow._branch_.oTdProperties && oTableRow._branch_.oTdProperties[oColHead.icon]) {
                                img.classList.add(oTableRow._branch_.oTdProperties[oColHead.icon]);
                            }
                            cell.append(img);
                        }
                    }
                    if ("time" in oColHead) {
                        if (sCellInner == "-1" || sCellInner == " ")
                            sCellInner = "";
                        else if (sCellInner == "") { }
                        else
                            sCellInner = GetDTString(sCellInner);
                    }
                    else if ("locale_number" in oColHead) {
                        sCellInner = ToLocaleNumber(sCellInner, oColHead.locale_number);
                    }
                    div2 = document.createElement("span");
                    cell.append(div2);
                    div2.className = "dyn_tab_cell_inner dyn_cell";
                    div2.innerHTML = sCellInner;
                    if (oTableRow._branch_.oTdProperties && oTableRow._branch_.oTdProperties[aTmpHead[j].field_name]) {
                        const aClasses = oTableRow._branch_.oTdProperties[aTmpHead[j].field_name].trim().split(' ');
                        for (const sClass of aClasses) {
                            div2.classList.add(sClass);
                        }
                    }
                    div2["i"] = i;
                    div2["j"] = j;
                    if (this.bEditable) {
                        td.addEventListener('dblclick', () => {
                            oDTP1.EditDinTableCell(div2, div2.innerText, cell.getBoundingClientRect().width);
                        });
                    }
                }
                if ((j + 1) < aTmpHead.length) {
                    if (this.bWithoutHead) {
                        cell.classList.add("stretch_x");
                        cell["sDriven"] = this.unicId + oColHead.field_name;
                        cell["domLimEl"] = this.domLimWindow;
                    }
                    else {
                        let dom = document.getElementById(this.unicId + aTmpHead[j].field_name + "_head");
                        if (dom) {
                            let width = dom.style.width;
                            if (width)
                                cell.style.width = width;
                        }
                    }
                }
                else {
                    let nLastSpanWidth = div2.getBoundingClientRect().width;
                    if (this.nLastColWidth < nLastSpanWidth)
                        this.nLastColWidth = nLastSpanWidth;
                }
                cell.title = ExterminateHtmlTags(sCellInner);
            }
            if (oColHead.button && cell.innerHTML.trim() && sCellInner != "-") {
                cell.classList.add("dyn_tab_cell_button");
                try {
                    cell.title = GetLocaleText(eval("GetHint" + oColHead.button + "('" + oColHead.field_name + "', oDTP1.aDeploedTable[" + i + "]._branch_.fGetData())"));
                }
                catch { }
                let nI = i;
                let nJ = j;
                cell.onmouseup = function (e) {
                    if (e.button === 0) {
                        DeleteAllDropMenu();
                        e.stopPropagation();
                        oBr.fMarkSelf(oDTP1);
                        oDTP1.ShowRows();
                        const cell1 = oDTP1.aDeploedTable[nI]._row_.childNodes[nJ];
                        eval(oColHead.button + "('" + oColHead.field_name + "', oDTP1.aDeploedTable[" + nI + "], oDTP1, cell1)");
                    }
                };
            }
        }
        td.style.width = "90%";
        if (oBr.bSelested) {
            row.classList.add("sel_row");
        }
    }
    _CalcTableSizes() {
        if (this.nWindowHeight > 0 && this.nLines > 0 && this.nRowHeight > 0)
            return;
        let row = this.domBody.children[1];
        if (this.domBody.children.length < 2) {
            let oTableRow = this.aDeploedTable[0];
            row = this.domBody.insertRow(-1);
            this.__CreateRow(row, oTableRow, 0, "");
        }
        this.nRowHeight = Math.ceil(row.getBoundingClientRect().height + 0.1);
        if (this.nWindowHeight > 0) {
            this.nLines = Math.ceil((this.nWindowHeight - this.nHeadHeight) / this.nRowHeight);
            this.domLimWindow.style.height = this.nWindowHeight + "px";
            this.domLimWindow.style.maxHeight = this.nWindowHeight + "px";
        }
        else if (this.nLines > 0) {
            this.nWindowHeight = this.nHeadHeight + this.nLines * this.nRowHeight;
            this.domLimWindow.style.height = this.nWindowHeight + "px";
            this.domLimWindow.style.maxHeight = this.nWindowHeight + "px";
        }
        else {
            this.nWindowHeight = this.domLimWindow.getBoundingClientRect().height;
            this.nLines = Math.ceil((this.nWindowHeight - this.nHeadHeight) / this.nRowHeight);
        }
    }
    async _CalcMinContainerHeight() {
        this._CalcTableSizes();
        if (this.sAutoDeploy && oAutoDeploy[this.sAutoDeploy] && this.fAutoDeploy) {
            for (let i = this.nFirstShowRow; i < this.nFirstShowRow + this.nLines + DBL_ADD_ROWS * 2 && i < this.aDeploedTable.length; i++) {
                if (this.aDeploedTable[i]._branch_.folder == FOLDER_AUTO_OPEN) {
                    const row = this.aDeploedTable[i];
                    await this.fAutoDeploy(row._branch_);
                    if (!row._branch_.cont) {
                        continue;
                    }
                    if (row._branch_.cont.length === 0) {
                        row._branch_.folder = FOLDER_EMPTY;
                        continue;
                    }
                    row._branch_.folder = FOLDER_OPEN;
                    for (let j = 0; j < row._branch_.cont.length; j++) {
                        const it = row._branch_.cont[j];
                        if (it.cont) {
                            it.folder = FOLDER_AUTO_OPEN;
                        }
                        this.aDeploedTable.splice(i + j + 1, 0, { _level_: row._level_ + 1, _branch_: it, });
                    }
                }
            }
        }
        this.domContainer.style.minHeight = (this.aDeploedTable.length * this.nRowHeight + this.nHeadHeight) + "px";
        if (this.nRowHeight > 0 && this.domLimWindow.clientHeight > this.nRowHeight) {
            this.nLastShowRow = Math.min(this.nFirstShowRow + Math.ceil(this.domLimWindow.clientHeight / this.nRowHeight) + DBL_ADD_ROWS, this.aDeploedTable.length - 1);
        }
        else {
            this.nLastShowRow = Math.min(this.nFirstShowRow + 200, this.aDeploedTable.length - 1);
        }
    }
    _CreateRows(bCalcScroll) {
        if (!this.domBody) {
            return;
        }
        if (this.aDeploedTable.length === 0) {
            this.domBody.innerHTML = "";
            return;
        }
        const t1 = GetCurrTime();
        this.nHorizontalScroll = this.domLimWindow.scrollLeft;
        let sRowH = (this.nMinRowHeight * rem2px) + "px";
        if (this.nRowHeight > 0) {
            sRowH = this.nRowHeight + "px";
        }
        const newTbody = document.createElement('tbody');
        let row = document.createElement('tr');
        newTbody.appendChild(row);
        let tdf = row.insertCell(-1);
        tdf.style.height = (this.nFirstShowRow * this.nRowHeight) + "px";
        if (bCalcScroll && this.nFirstShowRow > ADD_ROWS_ABOVE_AND_BELOW) {
            this.nScrollTop = (this.nFirstShowRow + ADD_ROWS_ABOVE_AND_BELOW) * this.nRowHeight;
            this.domLimWindow.scrollTop = this.nScrollTop;
        }
        tdf.style.verticalAlign = "bottom";
        tdf.style.maxWidth = "1px";
        tdf.style.whiteSpace = "nowrap";
        tdf.innerHTML = "Please wait...";
        if (this.nFirstShowRow === 0) {
            row.style.display = "none";
        }
        for (let i = this.nFirstShowRow; i <= this.nLastShowRow; i++) {
            let oTableRow = this.aDeploedTable[i];
            row = document.createElement('tr');
            newTbody.appendChild(row);
            this.__CreateRow(row, oTableRow, i, sRowH);
            if (i >= this.nLastShowRow && i < this.aDeploedTable.length - 1) {
                row = document.createElement('tr');
                newTbody.appendChild(row);
                let tdf = row.insertCell(-1);
                tdf.innerHTML = "Please wait...";
                tdf.style.maxWidth = "1px";
                tdf.style.whiteSpace = "nowrap";
                break;
            }
        }
        const dT = GetCurrTime() - t1;
        if (bDebugMode) {
            performance.mark('replace-start');
        }
        this.domTable.replaceChild(newTbody, this.domBody);
        this.domBody = newTbody;
        this.domLimWindow.scrollLeft = this.nHorizontalScroll;
        if (bDebugMode) {
            table_create.innerHTML = "Table " + this.domLimWindow.id + " creating time = " + dT + "ms";
            requestAnimationFrame(() => {
                requestAnimationFrame(() => {
                    performance.mark('paint-complete');
                    const measure = performance.measure('full-render', 'replace-start', 'paint-complete');
                    table_drawing.innerHTML = "Table " + this.domLimWindow.id + " drawing = " + measure.duration.toFixed(2) + "ms";
                    performance.clearMarks('replace-start');
                    performance.clearMarks('paint-complete');
                    performance.clearMeasures('full-render');
                });
            });
        }
    }
    _DeployTable(level, pParent) {
        let brCont = this.root.cont;
        let brBody = this.root.jsBody;
        if (pParent) {
            brCont = pParent.cont;
            if (pParent.jsBody) {
                brBody = pParent.jsBody;
            }
            else {
                return false;
            }
        }
        if (!brBody || !brCont) {
            return false;
        }
        if (IsObject(brBody[0])) {
            if (this.bNeedSort && (!this.nLevelNotSort || level >= this.nLevelNotSort)) {
                this.SortBranch(brCont);
            }
            for (let i = 0; i < brCont.length; i++) {
                let oBranch = brCont[i];
                if (this.bHide && oBranch.bHide)
                    continue;
                const oDB = { "_level_": level, "_branch_": oBranch, };
                oBranch["dep_ind"] = this.aDeploedTable.length;
                if (oBranch.bSelested) {
                    this._aSelectRow.push(oBranch.dep_ind);
                }
                if (oBranch.oIsWaiting) {
                    this.aDeploedTable.push(oDB);
                    if (oBranch.oIsWaiting.bNotExit) {
                        this.aDeploedTable.push({ "_level_": level + 1, "_branch_": oBranch, _wait_text_: oBranch.oIsWaiting.text });
                    }
                    else {
                        let sAdd = "<div class='waiting_exit' onclick='AbortWait(\"" + this.unicId + "_whole_table\", " + this.aDeploedTable.length + ")'>X</div>";
                        if (oBranch.oIsWaiting.bForce && this.fWaitingForce) {
                            sAdd += "<div class='waiting_force' onclick='ForceWait(\"" + this.unicId + "_whole_table\", " + this.aDeploedTable.length + ")'>+</div>";
                        }
                        this.aDeploedTable.push({ "_level_": level + 1, "_branch_": oBranch, _wait_text_: oBranch.oIsWaiting.text + sAdd });
                    }
                }
                else if ("cont" in oBranch && Array.isArray(oBranch.cont) && (this.bIsTree === true || this.bIsTree === undefined)) {
                    this.bIsTree = true;
                    if (!("folder" in oBranch)) {
                        if (this.sAutoDeploy && oAutoDeploy[this.sAutoDeploy] && this.fAutoDeploy) {
                            oBranch.folder = FOLDER_AUTO_OPEN;
                        }
                        else {
                            oBranch.folder = FOLDER_CLOSE;
                        }
                        this.aDeploedTable.push(oDB);
                    }
                    else {
                        this.aDeploedTable.push(oDB);
                        if (oBranch.folder == FOLDER_OPEN) {
                            this._DeployTable(level + 1, oBranch);
                        }
                    }
                }
                else {
                    this.aDeploedTable.push(oDB);
                }
            }
        }
        else {
            for (let i = 0; i < brCont.length; i++) {
                this.aDeploedTable.push({ _level_: 0, _branch_: brCont[i] });
                brCont[i].dep_ind = i;
                if (brCont[i].bSelested) {
                    this._aSelectRow.push(i);
                }
            }
        }
        return true;
    }
    _SetColsWidth(nContainerWidth) {
        if (!this.root.cont || this.root.cont.length === 0 || this.aHead.length === 0 || this.aDeploedTable.length == 0)
            return;
        let nWidthSumm = 0;
        let nNotImgColsNum = 0;
        let nPercentSumm = 0;
        if (this.fGetColsSizeFromLocStor && this.fGetColsSizeFromLocStor()) {
            this.bWasStretch = true;
        }
        if (!this.bWasStretch && this.domBody.children.length > 1) {
            this.bWasStretch = true;
            const aTableColls = this.domBody.children[1].cells;
            let i = 0;
            if (this.bLineNumbers) {
                i = 1;
                nWidthSumm = 50;
            }
            for (; (i + 1) < aTableColls.length; i++) {
                const oHead = this.aHead[i];
                const nColWidth = aTableColls[i].getBoundingClientRect().width;
                nWidthSumm += nColWidth;
                nNotImgColsNum += (oHead.img || oHead.bool) ? 0 : 1;
                nPercentSumm += oHead.auto_ext || 0;
                this.aTableColsWidth[i] = (nColWidth);
            }
            const oHead = this.aHead[i];
            nPercentSumm += oHead.auto_ext || 0;
            nWidthSumm += this.nLastColWidth + 20;
            if (nWidthSumm > nContainerWidth) {
                let delta = nWidthSumm - nContainerWidth;
                const nAlignStep = Math.min(delta / 10, 20);
                TableAlign(this.aTableColsWidth, delta, nAlignStep, 60);
            }
            else if (nNotImgColsNum > 0) {
                let delta = nContainerWidth - nWidthSumm;
                if (nPercentSumm) {
                    for (let i = 0; i < this.aTableColsWidth.length; i++) {
                        const oHead = this.aHead[i];
                        if (oHead.auto_ext) {
                            this.aTableColsWidth[i] += delta * oHead.auto_ext / nPercentSumm - 30;
                        }
                    }
                }
                else {
                    for (let i = 0; i < this.aTableColsWidth.length; i++) {
                        const oHead = this.aHead[i];
                        if (!oHead.img && !oHead.bool) {
                            this.aTableColsWidth[i] += delta;
                        }
                    }
                }
            }
        }
        for (let i = 0; i < this.aTableColsWidth.length && !this.bWithoutHead; i++) {
            const oHead = this.aHead[i];
            if (!oHead) {
                return;
            }
            if (!oHead.img && !oHead.bool) {
                const tmp = document.getElementById(this.unicId + oHead.field_name + "_head");
                if (tmp) {
                    tmp.style.width = this.aTableColsWidth[i] + "px";
                }
                else {
                    console.error("No document.getElementById(" + this.unicId + oHead.field_name + "'_head')!");
                }
                SetClassWidth(this.unicId + oHead.field_name, this.aTableColsWidth[i] + "px");
            }
        }
    }
    SetEditableBool(domCell) {
        const tmp = this.aDeploedTable[domCell.i]._branch_.fGetData()[this.aHead[domCell.j].field_name];
        this.aDeploedTable[domCell.i]._branch_.fGetData()[this.aHead[domCell.j].field_name] = !tmp;
        this._CreateRows(false);
        if (this.fOnChange) {
            this.fOnChange();
        }
    }
    SetEditableCell(domCell, el) {
        let newVal = el.value;
        domCell.innerHTML = newVal;
        const tmp = el;
        this.aDeploedTable[tmp.i]._branch_.fGetData()[this.aHead[tmp.j].field_name] = newVal;
        this._CreateRows(false);
        if (this.fOnChange) {
            this.fOnChange();
        }
    }
    EditDinTableCell(domCell, value, nWidth) {
        let edit = document.createElement("input");
        edit.type = 'text';
        edit.style.width = (nWidth - 10) + "px";
        edit.style.border = "none";
        domCell.innerHTML = "";
        domCell.append(edit);
        edit["i"] = domCell.i;
        edit["j"] = domCell.j;
        edit.value = value + "";
        const oDTP = this;
        edit.onblur = function () {
            oDTP.SetEditableCell(domCell, edit);
        };
        edit.addEventListener('keydown', function (key) {
            if (key.code == "Enter") {
                oDTP.bStopScroll = false;
                this.onblur = null;
                oDTP.SetEditableCell(domCell, this);
                oDTP.domLimWindow.focus();
                key.preventDefault();
                key.stopImmediatePropagation();
            }
            else if (key.code == "Escape") {
                oDTP.bStopScroll = false;
                this.onblur = null;
                domCell.innerHTML = value + "";
                key.preventDefault();
                key.stopImmediatePropagation();
                oDTP.domLimWindow.focus();
            }
        });
        edit.focus();
        oDTP.bStopScroll = true;
    }
    CreateIconElements(bDisplaySelect) {
        if (!this.domLimWindow && !this.domContainer || !IsDisplay(this.domLimWindow || this.domContainer))
            return;
        this.CreateDomIcons();
        if (bDisplaySelect) {
            this.ShowMarkRowInCenter();
        }
        else {
            this.ShowIcons();
        }
        this.domLimWindow.scrollTop = this.nScrollTop;
    }
    async CreateListElements(bDisplaySelect) {
        if (!this.domLimWindow && !this.domContainer || !IsDisplay(this.domLimWindow || this.domContainer))
            return;
        this.CreateDomList();
        if (!this.root.cont || this.root.cont.length == 0)
            return;
        if (bDisplaySelect) {
            this.ShowMarkRowInCenter();
        }
        else {
            await this.ShowTable();
        }
        this.domLimWindow.scrollTop = this.nScrollTop;
        this._SetColsWidth(this.domLimWindow.getBoundingClientRect().width);
        SetStretchEl();
    }
    SetColsWidth() {
        if (this.domLimWindow) {
            this._SetColsWidth(this.domLimWindow.getBoundingClientRect().width);
        }
    }
    fGetCont(oBranch) {
        if (!oBranch) {
            oBranch = this.root;
        }
        if (!oBranch.cont) {
            return [];
        }
        const ret = [];
        for (const it of oBranch.cont) {
            ret.push(it.fGetData());
        }
        return ret;
    }
    fFindBranchStr(sPath, t, oBranch) {
        let aPath;
        if (t) {
            aPath = SplitWithTrim(sPath, t);
        }
        else
            return;
        return this.fFindBranchArr(aPath, oBranch);
    }
    async fAutoDeployBranch(aPath, oBr) {
        if (aPath.length === 0 || !this.fAutoDeploy)
            return;
        let oBranch = this.root;
        if (oBr) {
            oBranch = oBr;
        }
        if (aPath[0] == "") {
            aPath.splice(0, 1);
        }
        for (let i = 0; i < aPath.length; i++) {
            if (!oBranch.cont)
                return;
            if (oBranch.folder === FOLDER_AUTO_OPEN) {
                try {
                    await this.fAutoDeploy(oBranch);
                    oBranch.folder = FOLDER_OPEN;
                }
                catch {
                    return;
                }
            }
            let tmp = oBranch.cont.findIndex(it1 => it1.pathEl && it1.pathEl.toLowerCase() == aPath[i].toLowerCase());
            if (tmp < 0)
                return;
            oBranch = oBranch.cont[tmp];
        }
    }
    fFindBranchArr(aPath, oBr) {
        if (aPath.length === 0) {
            return this.root;
        }
        let oBranch = this.root;
        if (oBr) {
            oBranch = oBr;
        }
        if (aPath[0] == "") {
            aPath.splice(0, 1);
        }
        for (let i = 0; i < aPath.length; i++) {
            if (!oBranch.cont) {
                return;
            }
            let tmp = oBranch.cont.findIndex(it1 => it1.pathEl && it1.pathEl.toLowerCase() == aPath[i].toLowerCase());
            if (tmp < 0) {
                return;
            }
            if (i + 1 == aPath.length) {
                return oBranch.cont[tmp];
            }
            oBranch = oBranch.cont[tmp];
        }
    }
    fSetNewBranchData(oNewData, fDataProcessor, oBranch) {
        if (!oBranch.pParent)
            return;
        if (fDataProcessor) {
            oNewData = fDataProcessor(oBranch, oNewData);
        }
        else if (this.fDataProcessor) {
            oNewData = this.fDataProcessor(oBranch, oNewData);
        }
        SetSameObjFields(oNewData, oBranch.pParent.jsBody[oBranch.indJs]);
    }
    fIsNotEmpty() {
        return this.root.cont && this.root.cont.length > 0;
    }
    fClear(pParent) {
        if (pParent) {
            pParent.cont = [];
            pParent.jsBody = [];
        }
        else {
            this.root.cont = [];
            this.root.jsBody = [];
            this.aDeploedTable = [];
            this._aSelectRow = [];
        }
    }
    fGetBrLen(oBranch) {
        if (!oBranch) {
            oBranch = this.root;
        }
        return oBranch.jsBody.length;
    }
    ShowTableAfterScroll() {
        if (this.bStopScroll) {
            this.domLimWindow.scrollTop = this.nScrollTop;
            return false;
        }
        let nMinScroll = this.nRowHeight;
        if (!this.domTable && this.rectIconLine) {
            nMinScroll = this.rectIconLine.h;
        }
        if (Math.abs(this.nScrollTop - this.domLimWindow.scrollTop) < nMinScroll)
            return;
        DeleteAllDropMenu();
        DrawDragAfterScrolling();
        this.nScrollTop = this.domLimWindow.scrollTop;
        if (!this.domTable) {
            if (!this.rectIconLine)
                return;
            if (this.domContainer.children.length == 0) {
                this.ShowIcons();
                return;
            }
            const domFirstLine = this.domContainer.children[0];
            const domLastLine = this.domContainer.children[this.domContainer.children.length - 1];
            const fragment = document.createDocumentFragment();
            const nTopFirstLine = parseFloat(domFirstLine.style.top);
            const nBttmLastLine = parseFloat(domLastLine.style.top) + this.rectIconLine.h;
            if (nTopFirstLine > this.nScrollTop) {
                let delta = Math.ceil((nTopFirstLine - this.nScrollTop) / this.rectIconLine.h) + ICON_ROWS_BEFORE_AFTER;
                this.nFirstShowRow -= delta * this.rectIconLine.n;
                if (this.nFirstShowRow < 0) {
                    delta -= this.nFirstShowRow / this.rectIconLine.n;
                    if (delta == 0)
                        return;
                    this.nFirstShowRow = 0;
                }
                if (delta * this.rectIconLine.h > this.domLimWindow.clientHeight) {
                    this.ShowIcons();
                    return;
                }
                this._CreateIconLines(delta, this.nFirstShowRow, fragment);
                this.domContainer.prepend(fragment);
                for (let i = 0; i < delta && this.domContainer.lastChild; i++) {
                    this.domContainer.removeChild(this.domContainer.lastChild);
                }
            }
            else if (nBttmLastLine < this.nScrollTop + this.domLimWindow.clientHeight) {
                let delta = Math.ceil((this.nScrollTop + this.domLimWindow.clientHeight - nBttmLastLine) / this.rectIconLine.h) + ICON_ROWS_BEFORE_AFTER;
                this.nFirstShowRow += delta * this.rectIconLine.n;
                if (delta * this.rectIconLine.h > this.domLimWindow.clientHeight) {
                    this.ShowIcons();
                    return;
                }
                const nLastInd = domLastLine.children[domLastLine.children.length - 1].ind + 1;
                delta = this._CreateIconLines(delta, nLastInd, fragment);
                this.domContainer.appendChild(fragment);
                for (let i = 0; i < delta && this.domContainer.firstChild; i++) {
                    this.domContainer.removeChild(this.domContainer.firstChild);
                }
            }
            return;
        }
        this.ShowTableAfterResize(true);
    }
    ShowTableAfterResize(bAfterScroll) {
        if (!IsDisplay(this.domLimWindow))
            return;
        if (!this.domTable) {
            if (!this.rectIconLine || this.domContainer.children.length == 0)
                return;
            this.rectIconLine.n = GetIconsPerRow(this.domContainer);
            const nIconNum = this.rectIconLine.n * (Math.floor(this.domLimWindow.clientHeight / this.rectIconLine.h) + ICON_ROWS_BEFORE_AFTER * 2);
            this.domContainer.style.minHeight = (this.rectIconLine.h * Math.ceil(this.root.jsBody.length / this.rectIconLine.n)) + "px";
            if (this.root.cont.length <= nIconNum) {
                if (this.nFirstShowRow === 0)
                    return;
                this.nFirstShowRow = 0;
                this.ShowIcons();
                return;
            }
            if (!bAfterScroll) {
                this.ShowIcons(true);
                return;
            }
            const nHideRows = Math.floor(this.nScrollTop / this.rectIconLine.h) - ICON_ROWS_BEFORE_AFTER;
            if (nHideRows <= 0) {
                this.nFirstShowRow = 0;
                this.ShowIcons();
                return;
            }
            this.nFirstShowRow = nHideRows * this.rectIconLine.n;
            this.ShowIcons();
            return;
        }
        if (this.nRowHeight < 1) {
            this.ShowTable();
        }
        else {
            this.nLines = Math.floor(this.domLimWindow.clientHeight / this.nRowHeight);
            let firstRow = Math.floor(this.domLimWindow.scrollTop / this.nRowHeight);
            let lastRow = this.nLines + firstRow;
            if ((this.nLastShowRow - lastRow) < MIN_ROW_FOR_RECREATE || (firstRow - this.nFirstShowRow) < MIN_ROW_FOR_RECREATE) {
                this.nLastShowRow = (lastRow + ADD_ROWS_ABOVE_AND_BELOW < this.aDeploedTable.length) ? lastRow + ADD_ROWS_ABOVE_AND_BELOW : this.aDeploedTable.length - 1;
                this.nFirstShowRow = (firstRow - ADD_ROWS_ABOVE_AND_BELOW > 0) ? firstRow - ADD_ROWS_ABOVE_AND_BELOW : 0;
                this.ShowTable();
            }
        }
    }
    SortFunc(sFieldName) {
        for (let i = 0; i < this.aHead.length; i++) {
            let cell = document.getElementById(this.unicId + this.aHead[i].field_name + "_head");
            if (cell) {
                if (this.aHead[i].field_name == sFieldName) {
                    cell.innerHTML = "<span class='dyn_tab_head_cell_inner'>" + GetLocaleText(this.aHead[i].col_name) + " " + ((this.nSortDirect > 0) ? "&#9650" : "&#9660;") + "</span>";
                }
                else {
                    cell.innerHTML = "<span class='dyn_tab_head_cell_inner'>" + GetLocaleText(this.aHead[i].col_name) + "</span>";
                }
            }
        }
        this.ShowTable();
    }
    ShowFirstSelectRow() {
        if (this._aSelectRow.length == 0) {
            return;
        }
        this._aSelectRow.sort(compareNum);
        this._CalcScroll({ start: this._aSelectRow[0], finish: this._aSelectRow[this._aSelectRow.length - 1], bStartPriority: true });
    }
    _CalcScroll(oRange) {
        const nRealLimWimHeight = this.domLimWindow.getBoundingClientRect().height - this.nHeadHeight;
        if (this.nRowHeight <= 1) {
            this._CreateRows(oRange.bCalcScroll);
            return;
        }
        const nLines = Math.floor(nRealLimWimHeight / this.nRowHeight) - 2;
        if (this.aDeploedTable.length <= nLines ||
            ((oRange.start === undefined || oRange.start < 0) && (oRange.finish === undefined || oRange.finish < 0))) {
            this._CreateRows(oRange.bCalcScroll);
            return;
        }
        if (oRange.start === undefined) {
            oRange.start = -1;
        }
        else {
            oRange.start = Math.round(oRange.start);
        }
        if (oRange.finish === undefined) {
            oRange.finish = -1;
        }
        else {
            oRange.finish = Math.round(oRange.finish);
        }
        const firstRow = Math.floor((this.domLimWindow.scrollTop + this.nHeadHeight) / this.nRowHeight);
        const lastRow = firstRow + nLines;
        this.nScrollTop = this.domLimWindow.scrollTop;
        if (oRange.start >= 0 && oRange.finish >= 0) {
            if (oRange.start > oRange.finish) {
                oRange = { "start": oRange.finish, "finish": oRange.start, "bStartPriority": oRange.bStartPriority };
            }
            if (oRange.start >= firstRow && oRange.finish <= lastRow) {
                this._CreateRows(oRange.bCalcScroll);
                return;
            }
            if ((oRange.finish - oRange.start) <= nLines) {
                if (oRange.start < firstRow) {
                    oRange.bStartPriority = true;
                }
                else {
                    oRange.bStartPriority = false;
                }
            }
            if (oRange.bStartPriority) {
                oRange.finish = -1;
            }
            else {
                oRange.start = -1;
            }
        }
        if (this.aDeploedTable.length <= nLines + DBL_ADD_ROWS) {
            this._CreateRows(oRange.bCalcScroll);
            if (oRange.start >= 0) {
                this.nScrollTop += (oRange.start - firstRow) * this.nRowHeight;
            }
            else if (oRange.finish >= 0) {
                this.nScrollTop += (oRange.finish - lastRow) * this.nRowHeight;
            }
            this.domLimWindow.scrollTop = this.nScrollTop;
            return;
        }
        if (oRange.start >= 0) {
            let nNewFirstRow = (oRange.start - ADD_ROWS_ABOVE_AND_BELOW - 1 > 0) ? oRange.start - ADD_ROWS_ABOVE_AND_BELOW - 1 : 0;
            if (nNewFirstRow + nLines + DBL_ADD_ROWS <= this.aDeploedTable.length) {
                this.nLastShowRow = nNewFirstRow + nLines + DBL_ADD_ROWS;
            }
            else {
                this.nLastShowRow = this.aDeploedTable.length - 1;
                nNewFirstRow = this.nLastShowRow - (nLines + DBL_ADD_ROWS);
            }
            this.nFirstShowRow = nNewFirstRow;
            this.nScrollTop += (oRange.start - firstRow - 1) * this.nRowHeight;
        }
        else if (oRange.finish >= 0) {
            let nNewLastRow = (oRange.finish + ADD_ROWS_ABOVE_AND_BELOW < this.aDeploedTable.length) ? oRange.finish + ADD_ROWS_ABOVE_AND_BELOW : this.aDeploedTable.length - 1;
            if (nNewLastRow - (nLines + DBL_ADD_ROWS) >= 0) {
                this.nFirstShowRow = nNewLastRow - (nLines + DBL_ADD_ROWS);
            }
            else {
                this.nFirstShowRow = 0;
                nNewLastRow = this.nFirstShowRow + (nLines + DBL_ADD_ROWS);
            }
            this.nLastShowRow = nNewLastRow;
            this.nScrollTop += (oRange.finish - lastRow) * this.nRowHeight;
        }
        else {
            this._CreateRows(oRange.bCalcScroll);
            return;
        }
        this._CreateRows(oRange.bCalcScroll);
        this.domLimWindow.scrollTop = this.nScrollTop;
    }
    _SetRowAsSelect() {
        if (this.domBody) {
            for (let item of this.domBody.children) {
                if (this._aSelectRow.includes(item.selfNumber)) {
                    item.classList.add("sel_row");
                }
                else {
                    item.classList.remove("sel_row");
                }
            }
        }
        else {
            for (let i = 0; i < this.domContainer.children.length; i++) {
                for (let j = 0; j < this.domContainer.children[i].children.length; j++) {
                    const item = this.domContainer.children[i].children[j];
                    if (this._aSelectRow.includes(item.ind)) {
                        item.classList.add("sel_row");
                    }
                    else {
                        item.classList.remove("sel_row");
                    }
                }
            }
        }
    }
    _OpenFolder(oNode, bDontOpen) {
        if (bDontOpen)
            return;
        oNode.folder = FOLDER_OPEN;
    }
    _CloseFolder(oNode) {
        oNode.folder = FOLDER_CLOSE;
        oNode.fCurtail();
        this.ShowTable();
    }
    async AddDinTableRow() {
        let nInd = this.aDeploedTable.length - 1;
        if (nInd < 0 || IsObjNotEmpty(this.aDeploedTable[nInd]._branch_.fGetData())) {
            this.root.jsBody.push({});
            if (!this.root.cont)
                this.root.cont = [];
            this.root.cont.push(new CBrControl(this.root.jsBody.length - 1, this.root));
            nInd++;
            await this.ShowTable({ finish: nInd });
        }
        if (this.bEditable && this.aDeploedTable[nInd] && this.aDeploedTable[nInd]._row_.firstChild) {
            this.aDeploedTable[nInd]._row_.firstChild.dispatchEvent(new Event('dblclick'));
        }
    }
    DeleteSelectRows() {
        if (this._aSelectRow.length == 0)
            return;
        this._aSelectRow.sort(compareNum);
        for (let i = this._aSelectRow.length - 1; i >= 0; i--) {
            this.aDeploedTable[this._aSelectRow[i]]._branch_.fDeleteSelf();
        }
        this.fSelTabClean();
        this.ShowTable();
        if (this.fOnChange) {
            this.fOnChange();
        }
    }
    fGetBranchByName(sName, sFieldName, branch) {
        if (!sName || !sFieldName) {
            return null;
        }
        let arr;
        let cont;
        if (branch && branch.cont) {
            arr = branch.jsBody;
            cont = branch.cont;
        }
        else {
            arr = this.root.jsBody;
            cont = this.root.cont;
        }
        const i = arr.findIndex(item => item[sFieldName] == sName);
        if (i >= 0) {
            const control = cont.find(it => it.indJs == i);
            if (!control)
                return null;
            return { "data": arr[i], "control": control };
        }
        return null;
    }
    fGetOpendFolders() {
        return this.root.fGetOpenFoldersTree();
    }
    AddDinTableRowsFromText(sText, pBranch) {
        const aLines = sText.split(/\r?\n/);
        if (aLines.length == 0)
            return;
        if (aLines[aLines.length - 1].trim() == "") {
            aLines.splice(-1);
        }
        if (aLines.length == 0)
            return;
        let aRows = [];
        for (let i = 0; i < aLines.length; i++) {
            let aCells = aLines[i].split("\t");
            let oRow = {};
            for (let j = 0; j < aCells.length && j < this.aHead.length; j++) {
                oRow[this.aHead[j].field_name] = aCells[j];
            }
            aRows.push(oRow);
        }
        if (!pBranch) {
            pBranch = this.root;
        }
        if (pBranch.fIsEmpty()) {
            pBranch.fAddDataArr(aRows);
        }
        else if (this._aSelectRow.length > 0 && pBranch.cont) {
            const nSel = this._aSelectRow[0], nStart = pBranch.cont[0].dep_ind, nFinish = pBranch.cont[pBranch.cont.length - 1].dep_ind;
            if (nSel >= nStart && nSel <= nFinish) {
                pBranch.fAddDataArr(aRows, { nInsertAboveRow: nSel - nStart });
            }
            else {
                pBranch.fAddDataArr(aRows);
            }
        }
        else {
            const nLastRow = this.aDeploedTable[this.aDeploedTable.length - 1]._branch_;
            if (!IsObjNotEmpty(nLastRow.fGetData())) {
                nLastRow.fDeleteSelf();
            }
            pBranch.fAddDataArr(aRows);
        }
        this.ShowTable();
        if (this.fOnChange) {
            this.fOnChange();
        }
    }
    SetContTable(sSortCol, sFindField) {
        if (!this.oFolderContTable)
            return;
        this.oFolderContTable.bIsTree = false;
        this.oFolderContTable.oParentDTP = this;
        this.oFolderContTable.aHead = jsFloatMenuTableHeads.WinExploreTableHead;
        this.oFolderContTable.sSortCol = sSortCol;
        this.oFolderContTable.bNeedSort = true;
        this.oFolderContTable.fDataProcessor = (oBrCont, data) => {
            const oData = data.fGetData();
            if (!oData.bIsFile) {
                oBrCont.cont = [];
            }
            return oData;
        };
        this.oFolderContTable.fDblclick = (oBrunch, oDTPx) => {
            const oData = oBrunch.fGetData();
            if (oData.bIsFile) {
                OpenFileByLink({ DTP: oDTPx });
                return;
            }
            if (this.oFolderContTable.oParentBr) {
                const oBr = this.oFolderContTable.oParentBr.fFindEl(oData[sFindField], sFindField);
                if (oBr) {
                    this.oFolderContTable.oParentBr.folder = FOLDER_OPEN;
                    this.ShowTableWithOpenFolder(this.oFolderContTable.oParentBr);
                    oBr.fMarkSelf(this);
                    this.fRowClick(oBr, true);
                    this.ShowMarkRowInCenter();
                }
            }
        };
        this.oFolderContTable.fRightBtn = function (e, Branch, DTP) {
            TableRowClick(DTP, Branch);
            oUpOrDownData.oBranch = Branch;
            oUpOrDownData.oDTP = DTP;
            const oDate = Branch.fGetData();
            if (oDate.sAccKey) {
                DropMenuWithFavor(e, Branch, DTP, jsBrowseDialogAccMenu);
            }
            else if (oDate.bIsServer) {
                CreateDropMenuItems(jsBrowseDialogServerMenu, e.clientX, e.clientY, e.clientX, e.target, false, { Branch, DTP });
            }
            else if (oDate.bIsFile) {
                CreateDropMenuItems(jsBrowseDialogFileMenu, e.clientX, e.clientY, e.clientX, e.target, false, { Branch, DTP });
            }
            else {
                CreateDropMenuItems(jsBrowseDialogFolderMenu, e.clientX, e.clientY, e.clientX, e.target, false, { Branch, DTP });
            }
        };
        this.oFolderContTable.fRowClick = (oBranch) => {
            const oDataIn = oBranch.fGetData();
            const oDataOut = this.oFolderContTable.oParentBr.fGetData();
            const sUrl = ExploreTableRowClick(this.oFolderContTable.oParentBr, {}, ged.pUrl1, "/" + (oDataIn.sName || ""));
            const sKey = oDataOut.fGetAccKey ? oDataOut.fGetAccKey() : "";
            SaveExplorerData(sUrl, sKey);
        };
    }
    SortBranch(brCont, sFieldName) {
        if (!brCont)
            return;
        if (!sFieldName) {
            sFieldName = this.sSortCol;
        }
        if (sFieldName == "")
            return;
        let nDirect = this.nSortDirect;
        brCont.sort(function (a, b) {
            if (("cont" in a) && !("cont" in b))
                return -nDirect;
            if (("cont" in b) && !("cont" in a))
                return nDirect;
            const cA = a.fGetData()[sFieldName];
            const cB = b.fGetData()[sFieldName];
            const bA_undef = (cA === undefined);
            const bB_undef = (cB === undefined);
            if (bB_undef && !bA_undef)
                return -nDirect;
            if (bA_undef && !bB_undef)
                return nDirect;
            if (bA_undef && bB_undef)
                return 0;
            const bA_IsNumber = (typeof cA) === "number";
            const bB_IsNumber = (typeof cB) === "number";
            if (bA_IsNumber && bB_IsNumber)
                return (cA - cB) * nDirect;
            if (bA_IsNumber && !bB_IsNumber)
                return -nDirect;
            if (!bA_IsNumber && bB_IsNumber)
                return nDirect;
            return CompareTextWithNumbers(cA, cB, nDirect);
        });
    }
    SetRowsAsSelect(aRows, bResetOldSelects = false) {
        if (aRows.length <= 0)
            return;
        if (bResetOldSelects) {
            this.fSelTabClean();
        }
        for (const it of aRows) {
            this.fSelTabPush(it);
        }
        this._aSelectRow.sort(compareNum);
        this.ShowFirstSelectRow();
    }
    TableMoveSelIcon(eDirect) {
        if (this.fGetSelTabLen() !== 1 || !this.rectIconLine)
            return;
        let nCurrInd = this.fGetSelRowNum(0);
        switch (eDirect) {
            case "ArrowDown":
                {
                    if (nCurrInd + this.rectIconLine.n < this.aDeploedTable.length) {
                        nCurrInd += this.rectIconLine.n;
                    }
                }
                break;
            case "ArrowUp":
                {
                    if (nCurrInd - this.rectIconLine.n >= 0) {
                        nCurrInd -= this.rectIconLine.n;
                    }
                }
                break;
            case "ArrowRight":
                {
                    if (nCurrInd + 1 < this.aDeploedTable.length) {
                        nCurrInd++;
                    }
                }
                break;
            case "ArrowLeft":
                {
                    if (nCurrInd - 1 >= 0) {
                        nCurrInd--;
                    }
                }
                break;
        }
        this.fSelTabReplace(nCurrInd, 0);
        this.ShowIcons();
        const selEl = this.domContainer.querySelector('.sel_row');
        if (selEl) {
            let bNeedToRedraw = false;
            const rect = this.domLimWindow.getBoundingClientRect();
            if (selEl.getBoundingClientRect().top < rect.top) {
                this.domLimWindow.scrollTop -= this.rectIconLine.h;
                bNeedToRedraw = true;
            }
            else if (selEl.getBoundingClientRect().bottom > rect.bottom) {
                this.domLimWindow.scrollTop += this.rectIconLine.h;
                bNeedToRedraw = true;
            }
            if (bNeedToRedraw) {
                this.ShowTable();
            }
        }
        TableRowClick(this, this.fGetSelBranch(0), true);
    }
    TableMoveSelRow(eDirect) {
        if (this._aSelectRow.length != 1) {
            return;
        }
        if (eDirect == "ArrowUp") {
            if (this._aSelectRow[0] > 0) {
                this.fSelTabReplace(this._aSelectRow[0] - 1, 0);
            }
        }
        else {
            if ((this._aSelectRow[0] + 1) < this.aDeploedTable.length) {
                this.fSelTabReplace(this._aSelectRow[0] + 1, 0);
            }
        }
        this.ShowRows();
        TableRowClick(this, this.aDeploedTable[this._aSelectRow[0]]._branch_, true);
        const domSelRow = this.fGetSelRow(0)._row_;
        if (domSelRow) {
            const oRowRect = domSelRow.getBoundingClientRect();
            if (oRowRect.top < 1 && oRowRect.bottom < 1) {
                this.ShowFirstSelectRow();
            }
            else if (eDirect == "ArrowUp") {
                let nRowTop = oRowRect.top;
                let nLimWinTop = this.domLimWindow.getBoundingClientRect().top;
                if (this.domHead) {
                    nLimWinTop += this.domHead.getBoundingClientRect().height;
                }
                if (nLimWinTop > nRowTop) {
                    this.nScrollTop -= (nLimWinTop - nRowTop);
                    this.domLimWindow.scrollTop = this.nScrollTop;
                }
            }
            else {
                let nRowBottom = oRowRect.bottom;
                let nLimWinBottom = this.domLimWindow.getBoundingClientRect().top + this.domLimWindow.clientHeight;
                if (nRowBottom > nLimWinBottom) {
                    this.nScrollTop += (nRowBottom - nLimWinBottom);
                    this.domLimWindow.scrollTop = this.nScrollTop;
                }
            }
        }
        else {
            this.ShowTableAfterResize();
        }
    }
}
async function CreateTable(oDTP) {
    if (!oDTP.domContainer && !oDTP.domLimWindow) {
        return null;
    }
    if (!oDTP.root.cont) {
        oDTP.root.cont = [];
    }
    if (oDTP.aHead.length == 0) {
        if (IsObject(oDTP.root.jsBody[0])) {
            for (let key in oDTP.root.jsBody[0]) {
                if (_IsValidFildName(oDTP.root.jsBody[0][key])) {
                    oDTP.aHead.push({ "field_name": key, "col_name": key });
                }
            }
        }
        else {
            oDTP.bWithoutHead = true;
            oDTP.aHead.push({ "field_name": "-", "col_name": "-" });
            oDTP.bNotObjRows = true;
        }
    }
    if (oDTP.fDblclick) {
        oDTP.bEditable = false;
    }
    await oDTP.CreateListElements();
    return oDTP;
}
function _IsValidFildName(fildName) {
    return (typeof fildName === 'string' && fildName.slice(0, 1) != "__");
}
function _IsCellNotEmpty(xCellInner) {
    let sType = typeof xCellInner;
    return (sType === 'string' && xCellInner != "" && xCellInner != "undefined") ||
        sType === 'number' || sType === 'boolean' || sType === 'bigint' || sType === 'symbol';
}
function ExternCopyAll(id, sMenuId) {
    if (sMenuId) {
        id += "_" + sMenuId;
    }
    bStopDeselect = true;
    let table = document.getElementById(id);
    if (!table || !table.pTable || !table.pTable.root.cont)
        return;
    let sClipBoard;
    if (table.pTable.fGetSelTabLen() > 0) {
        sClipBoard = table.pTable.root.fGetDataAsStr(table.pTable.aHead, table.pTable);
    }
    else {
        sClipBoard = table.pTable.root.fGetDataAsStr(table.pTable.aHead);
    }
    if (!sClipBoard) {
        HtmlMsgbox('Nothing was copied', 'Warning!', { bNoCurrJobName: true });
    }
    navigator.clipboard.writeText(sClipBoard)
        .then(() => { }).catch(err => {
        alert('Something went wrong: ' + err);
    });
}
async function ExternPaste(id, sMenuId) {
    if (sMenuId) {
        id += "_" + sMenuId;
    }
    bStopDeselect = true;
    let table = document.getElementById(id);
    if (!table || !table.pTable || table.pTable.bIsTree)
        return;
    try {
        const text = await navigator.clipboard.readText();
        table.pTable.AddDinTableRowsFromText(text);
    }
    catch (err) {
        alert('Something went wrong: ' + err);
    }
}
function ExternDelDinTableRow(id, sMenuId) {
    if (sMenuId) {
        id += "_" + sMenuId;
    }
    bStopDeselect = true;
    let table = document.getElementById(id);
    if (!table || !table.pTable) {
        return;
    }
    table.pTable.DeleteSelectRows();
}
function ExternAddDinTableRow(id, sMenuId) {
    if (sMenuId) {
        id += "_" + sMenuId;
    }
    bStopDeselect = true;
    let table = document.getElementById(id);
    if (!table || !table.pTable)
        return;
    table.pTable.AddDinTableRow();
}
function DeleteSpaceFromTable(brCont) {
    if (IsObject(brCont[0])) {
        for (let i = brCont.length - 1; i >= 0; i--) {
            let bIsEmpty = true;
            for (let key in brCont[i]) {
                if (key != "__cont__" && _IsValidFildName(key) && _IsCellNotEmpty(brCont[i][key])) {
                    bIsEmpty = false;
                    break;
                }
            }
            if (bIsEmpty) {
                brCont.splice(i, 1);
            }
        }
    }
    else {
        for (let i = brCont.length - 1; i >= 0; i--) {
            if (!_IsCellNotEmpty(brCont[i])) {
                brCont.splice(i, 1);
            }
        }
    }
}
function TableAlign(arr, d, x, minLen) {
    if (arr.length < 2)
        return;
    let longest = 0, subLongest = 1;
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] >= arr[longest]) {
            subLongest = longest;
            longest = i;
        }
    }
    if (arr[longest] <= minLen)
        return;
    if (longest == 0 && arr.length > 2) {
        for (let i = 2; i < arr.length; i++) {
            if (arr[i] > arr[subLongest]) {
                subLongest = i;
            }
        }
    }
    let d2 = arr[longest] - arr[subLongest];
    if (d2 >= d) {
        arr[longest] -= d;
        return;
    }
    arr[longest] -= d2;
    d -= d2;
    if (d <= x) {
        arr[subLongest] -= d;
        return;
    }
    arr[subLongest] -= x;
    d -= x;
    TableAlign(arr, d, x, minLen);
}
function AbortWait(sTableId, nRowNum) {
    const domTable = document.getElementById(sTableId);
    if (domTable && domTable.pTable) {
        const oBr = domTable.pTable.aDeploedTable[nRowNum - 1]._branch_;
        if (oBr.fWaitingExit) {
            oBr.fWaitingExit();
        }
    }
}
function ForceWait(sTableId, nRowNum) {
    const domTable = document.getElementById(sTableId);
    if (domTable && domTable.pTable) {
        if (domTable.pTable.fWaitingForce) {
            domTable.pTable.fWaitingForce(domTable.pTable.aDeploedTable[nRowNum - 1]._branch_);
        }
    }
}
async function TableRowClick(table, branch, bMouseClick) {
    DeleteAllDropMenu();
    if (table && !branch && table.fGetSelTabLen() > 0) {
        branch = table.fGetSelBranch(0);
    }
    if (branch && branch["fRowClick"]) {
        await branch.fRowClick(branch, bMouseClick);
    }
    else if (table && table["fRowClick"]) {
        await table.fRowClick(branch, bMouseClick);
    }
}
async function TableOpenFolder(table, branch, bMouseClick) {
    if (branch && branch["fOpenFolder"]) {
        await branch.fOpenFolder(branch, table, bMouseClick);
        return true;
    }
    else if (table["fOpenFolder"]) {
        await table.fOpenFolder(branch, table, bMouseClick);
        return true;
    }
    return false;
}
function TableSelRoweNum(xAddArgs) {
    if (!xAddArgs || !xAddArgs.DTP)
        return -1;
    return xAddArgs.DTP.fGetSelTabLen();
}
function IsFromEmtySpace(xAddArgs) {
    return xAddArgs.bFromEmptySpace;
}
function initDropZone(oDTP) {
    if (!oDTP.oFolderContTable || !oDTP.oFolderContTable.domLimWindow)
        return;
    const dropZone = oDTP.oFolderContTable.domLimWindow;
    const dragOverHandler = (e) => {
        e.preventDefault();
        e.stopPropagation();
        if (e.dataTransfer) {
            e.dataTransfer.dropEffect = 'copy';
        }
        dropZone.classList.add('drag-over');
    };
    const dragLeaveHandler = (e) => {
        e.preventDefault();
        e.stopPropagation();
        if (!dropZone.contains(e.relatedTarget)) {
            dropZone.classList.remove('drag-over');
        }
    };
    const dropHandler = async (e) => {
        if (oDTP.fGetSelTabLen() !== 1)
            return;
        e.preventDefault();
        e.stopPropagation();
        dropZone.classList.remove('drag-over');
        const dataTransfer = e.dataTransfer;
        if (!dataTransfer || !dataTransfer.items || dataTransfer.items.length === 0)
            return;
        const oBranch = oDTP.fGetSelBranch(0);
        const oShowProcess = new CShowFileOperation("Title_UploadFiles", "", "<span class='waiting_box'></span>");
        oShowProcess.SpeedAndRestDisplay("none");
        try {
            const upload = new CUpload(oDTP, oBranch, oShowProcess);
            await upload.processDrop(dataTransfer);
        }
        catch (oErr) {
            if (oErr !== 'canceled') {
                CatchErrProcess(oErr);
            }
        }
        finally {
            CloseFloatMenu(oShowProcess.fm);
        }
    };
    dropZone.addEventListener('dragover', dragOverHandler);
    dropZone.addEventListener('dragleave', dragLeaveHandler);
    dropZone.addEventListener('drop', dropHandler);
    return () => {
        dropZone.removeEventListener('dragover', dragOverHandler);
        dropZone.removeEventListener('dragleave', dragLeaveHandler);
        dropZone.removeEventListener('drop', dropHandler);
    };
}
