all files / spreadsheet/integrations/ image.js

90.56% Statements 163/180
88.28% Branches 128/145
63.16% Functions 12/19
92.09% Lines 163/177
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   881× 881× 881×   881× 881× 881× 881× 881×   881×       881×                             881× 12× 12× 12× 12× 12×                         60× 60×   60×   60× 60× 60× 60× 60×   52×   52× 26×   52×   51× 51× 51× 34× 34× 34×   34×     51× 10×   51×               51× 51× 38×   51× 51×   51× 25×     25×                                               23× 23× 23× 23× 23× 23× 20× 20× 20× 20×         16× 16×   20× 20× 20× 20×   20× 20× 17× 17× 17×     19×           22× 22× 22× 22× 22× 24× 22×     22× 22× 16×     881× 881×   126650×        
define(["require", "exports", "../common/event", "../common/event", "../common/index", "@syncfusion/ej2-base", "../../workbook/index", "../../workbook/index"], function (require, exports, event_1, event_2, index_1, ej2_base_1, index_2, index_3) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SpreadsheetImage = (function () {
        function SpreadsheetImage(parent) {
            this.parent = parent;
            this.addEventListener();
            this.renderImageUpload();
        }
        SpreadsheetImage.prototype.addEventListener = function () {
            this.parent.on(event_2.insertImage, this.insertImage, this);
            this.parent.on(event_2.refreshImgCellObj, this.refreshImgCellObj, this);
            this.parent.on(event_1.createImageElement, this.createImageElement, this);
            this.parent.on(event_1.deleteImage, this.deleteImage, this);
            this.parent.on(event_1.refreshImagePosition, this.refreshInsDelImagePosition, this);
        };
        SpreadsheetImage.prototype.renderImageUpload = function () {
            var uploadBox = this.parent.element.appendChild(this.parent.createElement('input', {
                id: this.parent.element.id + '_imageUpload', styles: 'display: none;',
                attrs: { type: 'file', accept: '.image, .jpg, .png, .gif ,jpeg', name: 'fileUpload' }
            }));
            uploadBox.onchange = this.imageSelect.bind(this);
        };
        SpreadsheetImage.prototype.imageSelect = function (args) {
            var file = args.target.files[0];
            if (!file) {
                return;
            }
            if (file.type.includes('image')) {
                this.insertImage({ file: file });
            }
            else {
                this.parent.serviceLocator.getService(index_1.dialog).show({ content: this.parent.serviceLocator.getService('spreadsheetLocale').getConstant('UnsupportedFile'),
                    width: '300' });
            }
            args.target.value = '';
        };
        SpreadsheetImage.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(event_2.insertImage, this.insertImage);
                this.parent.off(event_2.refreshImgCellObj, this.refreshImgCellObj);
                this.parent.off(event_1.createImageElement, this.createImageElement);
                this.parent.off(event_1.deleteImage, this.deleteImage);
                this.parent.off(event_1.refreshImagePosition, this.refreshInsDelImagePosition);
            }
        };
        SpreadsheetImage.prototype.insertImage = function (args, range) {
            var _this = this;
            this.binaryStringVal(args).then(function (src) { return _this.createImageElement({ options: { src: src }, range: range, isPublic: true }); });
        };
        SpreadsheetImage.prototype.binaryStringVal = function (args) {
            return new Promise(function (resolve, reject) {
                var reader = new FileReader();
                reader.readAsDataURL(args.file);
                reader.onload = function () { return resolve(reader.result); };
                reader.onerror = function (error) { return reject(error); };
            });
        };
        SpreadsheetImage.prototype.createImageElement = function (args) {
            var lastIndex = args.range ? args.range.lastIndexOf('!') : 0;
            var range = args.range ? (lastIndex > 0) ? args.range.substring(lastIndex + 1) : args.range
                : this.parent.getActiveSheet().selectedRange;
            var sheetIndex = (args.range && lastIndex > 0) ?
                index_2.getSheetIndex(this.parent, args.range.substring(0, lastIndex)) : this.parent.activeSheetIndex;
            var overlayObj = this.parent.serviceLocator.getService(index_1.overlay);
            var id = args.options.id ? args.options.id : ej2_base_1.getUniqueID(this.parent.element.id + '_overlay_picture_');
            var indexes = index_3.getRangeIndexes(range);
            var sheet = ej2_base_1.isUndefined(sheetIndex) ? this.parent.getActiveSheet() : this.parent.sheets[sheetIndex];
            if (this.parent.element.querySelector("#" + id)) {
                return;
            }
            var eventArgs = { requestType: 'beforeInsertImage', range: sheet.name + '!' + range, imageData: args.options.src,
                sheetIndex: sheetIndex };
            if (args.isPublic) {
                this.parent.notify('actionBegin', { eventArgs: eventArgs, action: 'beforeInsertImage' });
            }
            if (eventArgs.cancel) {
                return;
            }
            var overlayProps = overlayObj.insertOverlayElement(id, range, sheetIndex);
            overlayProps.element.style.backgroundImage = 'url(\'' + args.options.src + '\')';
            if (args.options.height || args.options.left) {
                overlayProps.element.style.height = args.options.height + 'px';
                overlayProps.element.style.width = args.options.width + 'px';
                if (!ej2_base_1.isNullOrUndefined(args.options.top)) {
                    overlayProps.element.style.top = Number(index_1.addDPRValue(args.options.top).toFixed(2)) + 'px';
                }
                if (!ej2_base_1.isNullOrUndefined(args.options.left)) {
                    overlayProps.element.style.left = Number(index_1.addDPRValue(args.options.left).toFixed(2)) + 'px';
                }
            }
            if (sheet.frozenRows || sheet.frozenColumns) {
                overlayObj.adjustFreezePaneSize(args.options, overlayProps.element, range);
            }
            var imgData = {
                src: args.options.src, id: id, height: parseFloat(overlayProps.element.style.height.replace('px', '')),
                width: parseFloat(overlayProps.element.style.width.replace('px', '')), top: sheet.frozenRows || sheet.frozenColumns ?
                    (indexes[0] ? index_2.getRowsHeight(sheet, 0, indexes[0] - 1) : 0) : (ej2_base_1.isNullOrUndefined(args.options.top) ?
                    overlayProps.top : args.options.top), left: sheet.frozenRows || sheet.frozenColumns ?
                    (indexes[1] ? index_2.getColumnsWidth(sheet, 0, indexes[1] - 1) : 0) : (ej2_base_1.isNullOrUndefined(args.options.left) ?
                    overlayProps.left : args.options.left)
            };
            this.parent.setUsedRange(indexes[0], indexes[1]);
            if (args.isPublic || args.isUndoRedo) {
                this.parent.notify(index_3.setImage, { options: [imgData], range: sheet.name + '!' + range });
            }
            var currCell = index_2.getCell(indexes[0], indexes[1], sheet);
            if (!currCell.image[currCell.image.length - 1].id) {
                currCell.image[currCell.image.length - 1].id = imgData.id;
            }
            if (!args.isUndoRedo && args.isPublic) {
                eventArgs = { requestType: 'insertImage', range: sheet.name + '!' + range, imageHeight: args.options.height ?
                        args.options.height : 300, imageWidth: args.options.width ? args.options.width : 400, imageData: args.options.src, id: id,
                    sheetIndex: sheetIndex };
                this.parent.notify('actionComplete', { eventArgs: eventArgs, action: 'insertImage' });
            }
        };
        SpreadsheetImage.prototype.refreshInsDelImagePosition = function (args) {
            var count = args.count;
            var sheetIdx = args.sheetIdx;
            var sheet = this.parent.sheets[sheetIdx];
            var pictureElements;
            var currCellObj = index_2.getCell(args.rowIdx, args.colIdx, sheet);
            var imageLen = currCellObj.image.length;
            var top;
            var left;
            for (var i = 0; i < imageLen; i++) {
                pictureElements = document.getElementById(currCellObj.image[i].id);
                top = (args.type === 'Row') ? (args.status === 'insert') ? currCellObj.image[i].top + (count * 20) :
                    currCellObj.image[i].top - (count * 20) : currCellObj.image[i].top;
                left = (args.type === 'Column') ? (args.status === 'insert') ? currCellObj.image[i].left + (count * 64) :
                    currCellObj.image[i].left - (count * 64) : currCellObj.image[i].left;
                currCellObj.image[i].top = top;
                currCellObj.image[i].left = left;
                Eif (pictureElements) {
                    pictureElements.style.top = top + 'px';
                    pictureElements.style.left = left + 'px';
                }
            }
        };
        SpreadsheetImage.prototype.refreshImgCellObj = function (args) {
            var sheetIndex = ej2_base_1.isUndefined(args.sheetIdx) ? this.parent.activeSheetIndex : args.sheetIdx;
            var sheet = index_2.getSheet(this.parent, sheetIndex);
            var prevCellObj = index_2.getCell(args.prevRowIdx, args.prevColIdx, sheet);
            var currCellObj = index_2.getCell(args.currentRowIdx, args.currentColIdx, sheet);
            var prevCellImg = prevCellObj ? prevCellObj.image : [];
            var prevImgObj;
            var currImgObj;
            var prevCellImgLen = (prevCellImg && prevCellImg.length) ? prevCellImg.length : 0;
            if (prevCellObj && prevCellObj.image && prevCellImg.length > 0) {
                for (var i = 0; i < prevCellImgLen; i++) {
                    Eif (prevCellImg[i] && prevCellImg[i].id === args.id) {
                        prevImgObj = prevCellImg[i];
                        prevImgObj.height = args.currentHeight;
                        prevImgObj.width = args.currentWidth;
                        prevImgObj.top = args.currentTop;
                        prevImgObj.left = args.currentLeft;
                        prevCellImg.splice(i, 1);
                    }
                }
                if (currCellObj && currCellObj.image) {
                    currImgObj = currCellObj.image;
                    Eif (prevImgObj) {
                        currImgObj.push(prevImgObj);
                    }
                }
                if (currImgObj) {
                    index_2.setCell(args.currentRowIdx, args.currentColIdx, sheet, { image: currImgObj }, true);
                }
                else {
                    index_2.setCell(args.currentRowIdx, args.currentColIdx, sheet, { image: [prevImgObj] }, true);
                }
                Eif (args.requestType === 'imageRefresh' && !args.isUndoRedo) {
                    var eventArgs = {
                        requestType: 'imageRefresh', currentRowIdx: args.currentRowIdx, currentColIdx: args.currentColIdx,
                        prevRowIdx: args.prevRowIdx, prevColIdx: args.prevColIdx, prevTop: args.prevTop, prevLeft: args.prevLeft,
                        currentTop: args.currentTop, currentLeft: args.currentLeft, currentHeight: args.currentHeight,
                        currentWidth: args.currentWidth, prevHeight: args.prevHeight, prevWidth: args.prevWidth,
                        id: args.id, sheetIdx: this.parent.activeSheetIndex
                    };
                    this.parent.notify('actionComplete', { eventArgs: eventArgs, action: 'imageRefresh' });
                }
            }
        };
        SpreadsheetImage.prototype.deleteImage = function (args) {
            var sheet = args.sheet || this.parent.getActiveSheet();
            var pictureElements = document.getElementById(args.id);
            var rowIdx = args.rowIdx;
            var colIdx = args.colIdx;
            var address;
            if (pictureElements) {
                Eif (args.rowIdx === undefined && args.colIdx === undefined) {
                    var imgTop = void 0;
                    var imgleft = void 0;
                    if (sheet.frozenRows || sheet.frozenColumns) {
                        var clientRect = pictureElements.getBoundingClientRect();
                        imgTop = { clientY: clientRect.top };
                        imgleft = { clientX: clientRect.left };
                        Eif (clientRect.top < this.parent.getColumnHeaderContent().getBoundingClientRect().bottom) {
                            imgTop.target = this.parent.getColumnHeaderContent();
                        }
                        if (clientRect.left < this.parent.getRowHeaderContent().getBoundingClientRect().right) {
                            imgleft.target = this.parent.getRowHeaderTable();
                        }
                    }
                    else {
                        imgTop = { clientY: parseFloat(pictureElements.style.top), isImage: true };
                        imgleft = { clientX: parseFloat(pictureElements.style.left), isImage: true };
                    }
                    this.parent.notify(event_2.getRowIdxFromClientY, imgTop);
                    this.parent.notify(event_1.getColIdxFromClientX, imgleft);
                    rowIdx = imgTop.clientY;
                    colIdx = imgleft.clientX;
                }
                address = sheet.name + '!' + index_2.getCellAddress(rowIdx, colIdx);
                if (!args.preventEventTrigger) {
                    var eventArgs = { address: address, cancel: false };
                    this.parent.notify(index_2.beginAction, { action: 'deleteImage', eventArgs: eventArgs });
                    if (eventArgs.cancel) {
                        return;
                    }
                }
                document.getElementById(args.id).remove();
            }
            else Eif (!args.sheet) {
                var rangeVal = args.range ? args.range.lastIndexOf('!') > 0 ? args.range.substring(args.range.lastIndexOf('!') + 1)
                    : args.range : this.parent.getActiveSheet().selectedRange;
                var sheetIndex = args.range && args.range.lastIndexOf('!') > 0 ?
                    index_2.getSheetIndex(this.parent, args.range.substring(0, args.range.lastIndexOf('!'))) :
                    this.parent.activeSheetIndex;
                var index = index_3.getRangeIndexes(rangeVal);
                rowIdx = index[0];
                colIdx = index[1];
                sheet = this.parent.sheets[sheetIndex];
            }
            var cellObj = index_2.getCell(rowIdx, colIdx, sheet);
            var prevCellImg = (cellObj && cellObj.image) ? cellObj.image : [];
            var imgLength = prevCellImg.length;
            var image = {};
            for (var i = imgLength - 1; i >= 0; i--) {
                if (prevCellImg[i].id === args.id) {
                    image = prevCellImg.splice(i, 1)[0];
                }
            }
            index_2.setCell(rowIdx, colIdx, sheet, { image: prevCellImg }, true);
            if (!args.preventEventTrigger) {
                this.parent.notify(event_1.completeAction, { action: 'deleteImage', eventArgs: { address: address, id: image.id, imageData: image.src, imageWidth: image.width, imageHeight: image.height, cancel: false }, preventAction: args.isUndoRedo });
            }
        };
        SpreadsheetImage.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        SpreadsheetImage.prototype.getModuleName = function () {
            return 'spreadsheetImage';
        };
        return SpreadsheetImage;
    }());
    exports.SpreadsheetImage = SpreadsheetImage;
});