all files / document-editor-container/ribbon/picture-format-tab/ size-group.js

40.27% Statements 60/149
12.16% Branches 9/74
36.36% Functions 8/22
40.27% Lines 60/149
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268   14× 14× 14× 14× 14× 14×     14× 14× 14×   14× 14×                                                                                           14× 14× 14× 14× 14× 14×   14× 14× 14× 14× 14× 14×                                                                                                                                                                                                                                                                       14× 14×               14×               14×       14×       14× 28× 28×     14× 14× 14×   14× 14×        
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-inputs", "@syncfusion/ej2-ribbon", "../ribbon-base/ribbon-constants"], function (require, exports, ej2_base_1, ej2_inputs_1, ej2_ribbon_1, ribbon_constants_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.SIZE_GROUP = '_size_group';
    exports.WIDTH_BOX_ID = '_width_box';
    exports.HEIGHT_BOX_ID = '_height_box';
    exports.ASPECT_RATIO_ID = '_aspect_ratio';
    var SizeGroup = (function () {
        function SizeGroup(container) {
            this.isMaintainAspectRatio = false;
            this.isInitialized = false;
            this.container = container;
            this.localObj = new ej2_base_1.L10n('documenteditorcontainer', this.container.defaultLocale, this.container.locale);
            this.isInitialized = false;
            this.templateContainer = ej2_base_1.createElement('div', {
                styles: 'position: absolute; visibility: hidden; height: 0; width: 0; overflow: hidden;'
            });
            document.body.appendChild(this.templateContainer);
            this.createWidthTemplate();
            this.createHeightTemplate();
        }
        SizeGroup.prototype.getSizeGroup = function () {
            var id = this.container.element.id + '_pictureformat';
            return {
                header: this.localObj.getConstant('Size'),
                id: id + exports.SIZE_GROUP,
                enableGroupOverflow: true,
                overflowHeader: this.localObj.getConstant('Size'),
                collections: [
                    {
                        items: [
                            {
                                keyTip: 'W',
                                type: ej2_ribbon_1.RibbonItemType.Template,
                                allowedSizes: ej2_ribbon_1.RibbonItemSize.Small,
                                itemTemplate: '#' + this.container.element.id + ribbon_constants_1.RIBBON_ID + '_pictureformat_width',
                                ribbonTooltipSettings: {
                                    title: this.localObj.getConstant('Width'),
                                    content: this.localObj.getConstant('Adjust image width')
                                }
                            },
                            {
                                keyTip: 'H',
                                type: ej2_ribbon_1.RibbonItemType.Template,
                                itemTemplate: '#' + this.container.element.id + ribbon_constants_1.RIBBON_ID + '_pictureformat_height',
                                ribbonTooltipSettings: {
                                    title: this.localObj.getConstant('Height'),
                                    content: this.localObj.getConstant('Adjust image height')
                                }
                            },
                            {
                                type: ej2_ribbon_1.RibbonItemType.CheckBox,
                                keyTip: 'X',
                                checkBoxSettings: {
                                    label: this.localObj.getConstant('Aspect ratio'),
                                    checked: false,
                                    change: this.onAspectRatioBtnClick.bind(this)
                                },
                                id: id + exports.ASPECT_RATIO_ID,
                                ribbonTooltipSettings: {
                                    title: this.localObj.getConstant('Aspect Ratio'),
                                    content: this.localObj.getConstant('Maintain aspect ratio when resizing')
                                }
                            }
                        ]
                    }
                ]
            };
        };
        SizeGroup.prototype.createWidthTemplate = function () {
            var script = document.createElement('script');
            script.id = this.container.element.id + ribbon_constants_1.RIBBON_ID + '_pictureformat_width';
            script.setAttribute('type', 'text/x-template');
            script.innerHTML = "\n        <div class=\"e-de-ctnr-picture-ribbon-segment e-de-ctnr-picture-format-tab\"\n            title=\"" + this.localObj.getConstant('Width') + "\">\n            <div class=\"e-de-picture-label-container\">\n                <span class=\"e-de-ribbon-property-label\">" + this.localObj.getConstant('Width') + ":</span>\n            </div>\n            <input id=\"" + (this.container.element.id + exports.WIDTH_BOX_ID) + "\" class=\"e-textbox\" />\n        </div>\n    ";
            document.head.appendChild(script);
            this.container.ribbon.numericTextBoxCollection.add(this.container.element.id + exports.WIDTH_BOX_ID, 'pictureFormatTab');
        };
        SizeGroup.prototype.createHeightTemplate = function () {
            var script = document.createElement('script');
            script.id = this.container.element.id + ribbon_constants_1.RIBBON_ID + '_pictureformat_height';
            script.setAttribute('type', 'text/x-template');
            script.innerHTML = "\n            <div class=\"e-de-ctnr-picture-ribbon-segment e-de-ctnr-picture-format-tab\" \n                title=\"" + this.localObj.getConstant('Height') + "\">\n                <div class=\"e-de-picture-label-container\">\n                    <span class=\"e-de-ribbon-property-label\">" + this.localObj.getConstant('Height') + ":</span>\n                </div>\n                <input id=\"" + (this.container.element.id + exports.HEIGHT_BOX_ID) + "\" class=\"e-textbox\"/>\n            </div>\n        ";
            document.head.appendChild(script);
            this.container.ribbon.numericTextBoxCollection.add(this.container.element.id + exports.HEIGHT_BOX_ID, 'pictureFormatTab');
        };
        SizeGroup.prototype.initializeNumericTextBoxes = function () {
            if (this.isInitialized) {
                return;
            }
            this.isInitialized = true;
            this.initializeWidthNumericBox();
            this.initializeHeightNumericBox();
        };
        SizeGroup.prototype.initializeWidthNumericBox = function () {
            var _this = this;
            var element = document.getElementById(this.container.element.id + exports.WIDTH_BOX_ID);
            if (!element) {
                return;
            }
            this.widthNumericBox = new ej2_inputs_1.NumericTextBox({
                min: 0,
                max: 23500,
                value: 0,
                showSpinButton: false,
                format: 'n0',
                decimals: 2,
                width: '100px',
                change: this.onWidthChange.bind(this)
            });
            this.widthNumericBox.appendTo(element);
            element.addEventListener('keydown', function (e) {
                if (e.key === 'Enter') {
                    setTimeout(function () {
                        _this.applyImageWidth();
                    }, 30);
                }
            });
            element.addEventListener('blur', this.applyImageWidth.bind(this));
        };
        SizeGroup.prototype.initializeHeightNumericBox = function () {
            var _this = this;
            var element = document.getElementById(this.container.element.id + exports.HEIGHT_BOX_ID);
            if (!element) {
                return;
            }
            this.heightNumericBox = new ej2_inputs_1.NumericTextBox({
                min: 0,
                max: 23500,
                value: 0,
                showSpinButton: false,
                format: 'n0',
                decimals: 2,
                width: '100px',
                change: this.onHeightChange.bind(this)
            });
            this.heightNumericBox.appendTo(element);
            element.addEventListener('keydown', function (e) {
                if (e.key === 'Enter') {
                    setTimeout(function () {
                        _this.applyImageHeight();
                    }, 30);
                }
            });
            element.addEventListener('blur', this.applyImageHeight.bind(this));
        };
        SizeGroup.prototype.onWidthChange = function () {
            this.applyImageWidth();
        };
        SizeGroup.prototype.onHeightChange = function () {
            this.applyImageHeight();
        };
        SizeGroup.prototype.onAspectRatioBtnClick = function (args) {
            this.isMaintainAspectRatio = args.checked;
        };
        SizeGroup.prototype.updateSizeProperties = function () {
            var imageFormat = this.container.documentEditor.selectionModule.imageFormat;
            if (this.widthNumericBox && this.heightNumericBox) {
                this.widthNumericBox.value = imageFormat.width;
                this.heightNumericBox.value = imageFormat.height;
            }
        };
        SizeGroup.prototype.applyImageWidth = function () {
            if (!this.widthNumericBox || !this.heightNumericBox) {
                return;
            }
            if (!this.isMaintainAspectRatio) {
                var width = this.widthNumericBox.value;
                var height = this.heightNumericBox.value;
                if (width > this.widthNumericBox.max) {
                    width = this.widthNumericBox.max;
                }
                if (height > this.heightNumericBox.max) {
                    height = this.heightNumericBox.max;
                }
                if (!(width === null || height === null)) {
                    this.container.documentEditor.selectionModule.imageFormat.resize(width, height);
                }
            }
            else if (this.isMaintainAspectRatio) {
                var width = this.widthNumericBox.value;
                if (width > this.widthNumericBox.max) {
                    width = this.widthNumericBox.max;
                }
                var imageFormat = this.container.documentEditor.selectionModule.imageFormat;
                var ratio = width / imageFormat.width;
                var height = this.heightNumericBox.value * ratio;
                this.heightNumericBox.value = height;
                if (!(width === null || height === null)) {
                    imageFormat.resize(width, height);
                }
            }
        };
        SizeGroup.prototype.applyImageHeight = function () {
            if (!this.widthNumericBox || !this.heightNumericBox) {
                return;
            }
            if (!this.isMaintainAspectRatio) {
                var width = this.widthNumericBox.value;
                var height = this.heightNumericBox.value;
                if (!(width === null || height === null)) {
                    this.container.documentEditor.selectionModule.imageFormat.resize(width, height);
                }
            }
            else if (this.isMaintainAspectRatio) {
                var height = this.heightNumericBox.value;
                var imageFormat = this.container.documentEditor.selectionModule.imageFormat;
                var ratio = height / imageFormat.height;
                var width = this.widthNumericBox.value * ratio;
                this.widthNumericBox.value = width;
                if (!(width === null || height === null)) {
                    imageFormat.resize(width, height);
                }
            }
        };
        SizeGroup.prototype.resetInitializationState = function () {
            this.isInitialized = false;
            if (this.widthNumericBox) {
                this.widthNumericBox.destroy();
                this.widthNumericBox = undefined;
            }
            if (this.heightNumericBox) {
                this.heightNumericBox.destroy();
                this.heightNumericBox = undefined;
            }
        };
        SizeGroup.prototype.destroy = function () {
            this.isInitialized = false;
            Iif (this.widthNumericBox) {
                var widthElement = document.getElementById(this.container.element.id + exports.WIDTH_BOX_ID);
                if (widthElement) {
                    ej2_base_1.EventHandler.clearEvents(widthElement);
                }
                this.widthNumericBox.destroy();
                this.widthNumericBox = undefined;
            }
            Iif (this.heightNumericBox) {
                var heightElement = document.getElementById(this.container.element.id + exports.HEIGHT_BOX_ID);
                if (heightElement) {
                    ej2_base_1.EventHandler.clearEvents(heightElement);
                }
                this.heightNumericBox.destroy();
                this.heightNumericBox = undefined;
            }
            Iif (this.aspectRatioBtn) {
                this.aspectRatioBtn.destroy();
                this.aspectRatioBtn = undefined;
            }
            var elementsToRemove = [
                document.getElementById(this.container.element.id + ribbon_constants_1.RIBBON_ID + '_pictureformat_width'),
                document.getElementById(this.container.element.id + ribbon_constants_1.RIBBON_ID + '_pictureformat_height')
            ];
            elementsToRemove.forEach(function (element) {
                Eif (element && element.parentNode) {
                    element.parentNode.removeChild(element);
                }
            });
            Eif (this.templateContainer && this.templateContainer.parentNode) {
                this.templateContainer.parentNode.removeChild(this.templateContainer);
                this.templateContainer = undefined;
            }
            this.container = undefined;
            this.localObj = undefined;
        };
        return SizeGroup;
    }());
    exports.SizeGroup = SizeGroup;
});