all files / grid/renderer/ dialog-edit-renderer.js

94.12% Statements 128/136
87.5% Branches 63/72
95% Functions 19/20
94.12% Lines 128/136
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219   24× 24× 24×     24× 24×   26×     20× 20×   26× 26× 26× 26×     24×     23× 18×         24×   26× 26× 26× 26×   26× 26× 26× 26×                                               26× 26× 26× 26× 26× 26× 26×     26×   26× 26×                     38× 38× 114×       38× 38× 38× 38× 26× 26×         27× 27× 27× 27×   27×                                   22× 22× 22× 22× 147×     139× 139×         139× 139× 139× 139×   22× 22× 22× 22×   50× 26×   24× 24×        
define(["require", "exports", "@syncfusion/ej2-popups", "@syncfusion/ej2-base", "../base/constant", "../base/util", "./responsive-dialog-renderer", "../base/enum", "../base/string-literals"], function (require, exports, ej2_popups_1, ej2_base_1, events, util_1, responsive_dialog_renderer_1, enum_1, literals) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var DialogEditRender = (function () {
        function DialogEditRender(parent, serviceLocator) {
            this.parent = parent;
            this.serviceLocator = serviceLocator;
            Iif (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.dialogDestroy, this.destroy, this);
            this.parent.on(events.destroy, this.destroy, this);
        }
        DialogEditRender.prototype.setLocaleObj = function () {
            this.l10n = this.serviceLocator.getService('localization');
        };
        DialogEditRender.prototype.addNew = function (elements, args) {
            this.isEdit = false;
            this.createDialog(elements, args);
        };
        DialogEditRender.prototype.update = function (elements, args) {
            this.isEdit = true;
            this.createDialog(elements, args);
        };
        DialogEditRender.prototype.createDialogHeader = function (args) {
            var _this = this;
            var gObj = this.parent;
            var header;
            if (this.parent.enableAdaptiveUI) {
                var responsiveDlgRenderer = new responsive_dialog_renderer_1.ResponsiveDialogRenderer(this.parent, this.serviceLocator);
                responsiveDlgRenderer.action = this.isEdit ? enum_1.ResponsiveDialogAction.isEdit : enum_1.ResponsiveDialogAction.isAdd;
                return responsiveDlgRenderer.renderResponsiveHeader(undefined, args);
            }
            else {
                if (gObj.editSettings.headerTemplate) {
                    header = ej2_base_1.initializeCSPTemplate(function () {
                        return _this.getDialogEditTemplateElement('HeaderTemplate', args).outerHTML;
                    });
                }
                else if (this.isEdit) {
                    header = this.l10n.getConstant('EditFormTitle') + args.primaryKeyValue[0];
                }
                else {
                    header = this.l10n.getConstant('AddFormTitle');
                }
            }
            return header;
        };
        DialogEditRender.prototype.createDialog = function (elements, args) {
            var _this = this;
            var gObj = this.parent;
            this.dialog = this.parent.createElement('div', { id: gObj.element.id + '_dialogEdit_wrapper', styles: 'width: auto' });
            if (gObj.enableAdaptiveUI) {
                this.dialog.classList.add('e-responsive-dialog');
            }
            gObj.element.appendChild(this.dialog);
            this.setLocaleObj();
            this.dialog.setAttribute('aria-label', this.l10n.getConstant('DialogEdit'));
            this.dialogObj = new ej2_popups_1.Dialog(ej2_base_1.extend({
                header: this.createDialogHeader(args), isModal: true, visible: true,
                cssClass: this.parent.cssClass ? 'e-edit-dialog' + ' ' + this.parent.cssClass : 'e-edit-dialog',
                content: this.getEditElement(elements, args),
                showCloseIcon: true,
                allowDragging: true,
                close: this.dialogClose.bind(this),
                created: this.dialogCreated.bind(this),
                closeOnEscape: true, width: gObj.editSettings.template ? 'auto' : '330px',
                target: args.target ? args.target : document.body, animationSettings: { effect: 'None' },
                footerTemplate: gObj.editSettings.footerTemplate ? ej2_base_1.initializeCSPTemplate(function () {
                    return _this.getDialogEditTemplateElement('FooterTemplate', args).outerHTML;
                }) : null,
                buttons: [{
                        click: this.btnClick.bind(this),
                        buttonModel: { content: this.l10n.getConstant('SaveButton'),
                            cssClass: this.parent.cssClass ? 'e-primary' + ' ' + this.parent.cssClass : 'e-primary',
                            isPrimary: true }
                    },
                    { click: this.btnClick.bind(this),
                        buttonModel: {
                            cssClass: this.parent.cssClass ? 'e-flat' + ' ' + this.parent.cssClass : 'e-flat',
                            content: this.l10n.getConstant('CancelButton')
                        } }]
            }, gObj.editSettings.dialog ? (gObj.editSettings.dialog.params || {}) : {}));
            args.dialog = this.dialogObj;
            var isStringTemplate = 'isStringTemplate';
            this.dialogObj["" + isStringTemplate] = true;
            this.renderResponsiveDialog();
            this.dialogObj.appendTo(this.dialog);
            util_1.applyBiggerTheme(this.parent.element, this.dialogObj.element.parentElement);
            if (gObj.enableAdaptiveUI) {
                this.dialogObj.show(true);
            }
        };
        DialogEditRender.prototype.dialogCreated = function () {
            util_1.addBiggerDialog(this.parent);
        };
        DialogEditRender.prototype.renderResponsiveDialog = function () {
            var _this = this;
            if (this.parent.enableAdaptiveUI) {
                Iif (this.parent.adaptiveDlgTarget) {
                    this.dialogObj.target = this.parent.adaptiveDlgTarget;
                }
                this.dialogObj.buttons = [{}];
                this.dialogObj.showCloseIcon = true;
                this.dialogObj.visible = false;
                this.dialogObj.width = '100%';
                this.dialogObj.open = function () {
                    _this.dialogObj.element.style.maxHeight = '100%';
                };
            }
        };
        DialogEditRender.prototype.btnClick = function (e) {
            if (this.l10n.getConstant('CancelButton').toLowerCase() === e.target.innerText.trim().toLowerCase()) {
                this.dialogClose();
            }
            else {
                this.parent.endEdit();
            }
        };
        DialogEditRender.prototype.dialogClose = function () {
            this.parent.closeEdit();
        };
        DialogEditRender.prototype.destroy = function (args) {
            var dialogEditTemplates = ['template', 'headerTemplate', 'footerTemplate'];
            for (var i = 0; i < dialogEditTemplates.length; i++) {
                if (this.parent.editSettings[dialogEditTemplates[parseInt(i.toString(), 10)]]) {
                    var templateName = dialogEditTemplates[parseInt(i.toString(), 10)].charAt(0).toUpperCase()
                        + dialogEditTemplates[parseInt(i.toString(), 10)].slice(1);
                    var editTemplateID = this.parent.element.id + 'editSettings' + templateName;
                    ej2_base_1.updateBlazorTemplate(editTemplateID, templateName, this.parent.editSettings);
                }
            }
            this.parent.notify(events.destroyForm, {});
            this.parent.isEdit = false;
            this.parent.notify(events.toolbarRefresh, {});
            if (this.dialog && !this.dialogObj.isDestroyed) {
                this.dialogObj.destroy();
                ej2_base_1.remove(this.dialog);
            }
        };
        DialogEditRender.prototype.getDialogEditTemplateElement = function (dialogTemp, args) {
            var tempDiv = this.parent.createElement('div', { className: 'e-dialog' + dialogTemp });
            var dummyData = ej2_base_1.extend({}, args.rowData, { isAdd: !this.isEdit }, true);
            var templateID = this.parent.element.id + 'editSettings' + dialogTemp;
            util_1.appendChildren(tempDiv, (dialogTemp === 'HeaderTemplate' ? this.parent.getEditHeaderTemplate() :
                this.parent.getEditFooterTemplate())(dummyData, this.parent, 'editSettings' + dialogTemp, templateID));
            ej2_base_1.updateBlazorTemplate(templateID, dialogTemp, this.parent.editSettings);
            return tempDiv;
        };
        DialogEditRender.prototype.getEditElement = function (elements, args) {
            var _this = this;
            var gObj = this.parent;
            var div = this.parent.createElement('div', { className: this.isEdit ? literals.editedRow : 'e-insertedrow' });
            var form = args.form =
                this.parent.createElement('form', { id: gObj.element.id + 'EditForm', className: 'e-gridform' });
            if (this.parent.editSettings.template) {
                var editTemplateID = this.parent.element.id + 'editSettingsTemplate';
                var dummyData = ej2_base_1.extend({}, args.rowData, { isAdd: !this.isEdit }, true);
                var isReactCompiler = this.parent.isReact && typeof (this.parent.editSettings.template) !== 'string' &&
                    !(this.parent.editSettings.template.prototype &&
                        this.parent.editSettings.template.prototype.CSPTemplate);
                var isReactChild = this.parent.parentDetails && this.parent.parentDetails.parentInstObj &&
                    this.parent.parentDetails.parentInstObj.isReact;
                Iif (isReactCompiler || isReactChild) {
                    this.parent.getEditTemplate()(dummyData, this.parent, 'editSettingsTemplate', editTemplateID, null, null, form);
                    this.parent.renderTemplates();
                }
                else {
                    util_1.appendChildren(form, this.parent.getEditTemplate()(dummyData, this.parent, 'editSettingsTemplate', editTemplateID, null, null, null, gObj.root));
                }
                var setRules = function () {
                    var columns = _this.parent.getColumns();
                    for (var i = 0; i < columns.length; i++) {
                        if (columns[parseInt(i.toString(), 10)].validationRules) {
                            _this.parent.editModule.formObj.rules[columns[parseInt(i.toString(), 10)].field] =
                                columns[parseInt(i.toString(), 10)].validationRules;
                        }
                    }
                };
                ej2_base_1.updateBlazorTemplate(editTemplateID, 'Template', this.parent.editSettings, true, setRules);
                div.appendChild(form);
                return div;
            }
            var table = this.parent.createElement('table', { className: literals.table, attrs: { cellspacing: '6px', role: 'grid' } });
            var tbody = this.parent.createElement(literals.tbody, { attrs: { role: 'rowgroup' } });
            var cols = gObj.getColumns();
            for (var i = 0; i < cols.length; i++) {
                if (this.parent.editModule.checkColumnIsGrouped(cols[parseInt(i.toString(), 10)]) || cols[parseInt(i.toString(), 10)].commands
                    || cols[parseInt(i.toString(), 10)].commandsTemplate || cols[parseInt(i.toString(), 10)].type === 'checkbox') {
                    continue;
                }
                var tr = this.parent.createElement('tr', { attrs: { role: 'row' } });
                var dataCell = this.parent.createElement('td', {
                    className: literals.rowCell, attrs: {
                        style: 'text-align:' + (this.parent.enableRtl ? 'right' : 'left') + ';width:190px'
                    }
                });
                elements[cols[parseInt(i.toString(), 10)].uid].classList.remove('e-input');
                dataCell.appendChild(elements[cols[parseInt(i.toString(), 10)].uid]);
                tr.appendChild(dataCell);
                tbody.appendChild(tr);
            }
            table.appendChild(tbody);
            form.appendChild(table);
            div.appendChild(form);
            return div;
        };
        DialogEditRender.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.dialogDestroy, this.destroy);
            this.parent.off(events.destroy, this.destroy);
        };
        return DialogEditRender;
    }());
    exports.DialogEditRender = DialogEditRender;
});