all files / block-manager/plugins/menus/ blockaction-menu.js

100% Statements 157/157
93.42% Branches 71/76
100% Functions 28/28
100% Lines 157/157
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   1390× 1390× 1390× 1390×   1390× 1390× 1390× 1390× 1390× 1390×   1390× 1390× 1390× 1390× 1390× 1390×   1390× 1390× 1390×   1390× 1390×           1390×   1390× 1390× 1390× 5560×     747× 747×   747× 303×   444× 444×     67× 67× 67× 40×     67× 67× 34×         34×   33× 33×     34×     33×         33×   32× 32× 32× 32× 32× 32× 32× 32×         33×       37×       152× 152× 152×     37× 37× 37× 37× 37× 37×     37× 37×   41×   38× 38×     37× 37× 148× 148× 148×   37× 37×   37×   37× 37×   37×   148×     16×       19× 19× 19× 19×         10× 10×   10× 10×                     18× 18×     18×     171×   1390× 1390× 1390× 1390× 1390×   1390×        
define(["require", "exports", "@syncfusion/ej2-base", "../../../common/utils/block", "../../../common/constant", "../../../common/utils/common", "../../../common/constant"], function (require, exports, ej2_base_1, block_1, constant_1, common_1, constants) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BlockActionMenuModule = (function () {
        function BlockActionMenuModule(manager) {
            this.isPopupOpened = false;
            this.shortcutMap = new Map();
            this.parent = manager;
            this.addEventListeners();
        }
        BlockActionMenuModule.prototype.addEventListeners = function () {
            this.parent.observer.on(constant_1.events.keydown, this.onKeyDown, this);
            this.parent.observer.on('actionMenuCreated', this.handleMenuCreated, this);
            this.parent.observer.on('popupWidthChanged', this.handlePopupWidthChanges, this);
            this.parent.observer.on('popupHeightChanged', this.handlePopupHeightChanges, this);
            this.parent.observer.on('blockActionsMenuSelect', this.handleBlockActionMenuSelect, this);
            this.parent.observer.on(constant_1.events.destroy, this.destroy, this);
        };
        BlockActionMenuModule.prototype.removeEventListeners = function () {
            this.parent.observer.off(constant_1.events.keydown, this.onKeyDown);
            this.parent.observer.off('actionMenuCreated', this.handleMenuCreated);
            this.parent.observer.off('popupWidthChanged', this.handlePopupWidthChanges);
            this.parent.observer.off('popupHeightChanged', this.handlePopupHeightChanges);
            this.parent.observer.off('blockActionsMenuSelect', this.handleBlockActionMenuSelect);
            this.parent.observer.off(constant_1.events.destroy, this.destroy);
        };
        BlockActionMenuModule.prototype.handleMenuCreated = function () {
            this.menuWrapperElement = this.parent.rootEditorElement.querySelector("." + constants.BLOCKACTION_MENUBAR_CLS);
            this.init();
            this.buildShortcutMap();
        };
        BlockActionMenuModule.prototype.init = function () {
            var popupElement = this.parent.rootEditorElement.querySelector('#' + this.parent.rootEditorElement.id + constants.BLOCKACTION_POPUP_ID);
            var args = {
                element: popupElement,
                content: this.menuWrapperElement,
                width: this.parent.blockActionMenuSettings.popupWidth,
                height: this.parent.blockActionMenuSettings.popupHeight
            };
            this.popupObj = this.parent.popupRenderer.renderPopup(args);
        };
        BlockActionMenuModule.prototype.buildShortcutMap = function () {
            var _this = this;
            this.shortcutMap.clear();
            this.parent.blockActionMenuSettings.items.forEach(function (item) {
                _this.shortcutMap.set(item.shortcut.toLowerCase(), item);
            });
        };
        BlockActionMenuModule.prototype.onKeyDown = function (e) {
            var normalizedKey = common_1.getNormalizedKey(e);
            var isTable = this.parent.currentFocusedBlock &&
                this.parent.currentFocusedBlock.closest("." + constants.TABLE_BLOCK_CLS);
            if (!normalizedKey || isTable) {
                return;
            }
            var actionItem = this.shortcutMap.get(normalizedKey);
            if (actionItem) {
                e.preventDefault();
                this.handleBlockActions(actionItem, this.parent.currentFocusedBlock, e);
            }
        };
        BlockActionMenuModule.prototype.toggleBlockActionPopup = function (shouldHide, e) {
            var _this = this;
            setTimeout(function () {
                if (_this.parent && _this.parent.inlineToolbarModule) {
                    _this.parent.inlineToolbarModule.hideInlineToolbar();
                }
            }, 10);
            Eif (this.popupObj) {
                if (shouldHide) {
                    var closeEventArgs = {
                        event: e,
                        items: this.parent.blockActionMenuSettings.items,
                        cancel: false,
                        callback: function (args) {
                            if (args.cancel) {
                                return;
                            }
                            _this.popupObj.hide();
                            _this.isPopupOpened = false;
                        }
                    };
                    this.parent.observer.notify('blockActionsMenuClose', closeEventArgs);
                }
                else {
                    var openEventArgs = {
                        event: e,
                        items: this.parent.blockActionMenuSettings.items,
                        cancel: false,
                        callback: function (args) {
                            if (args.cancel) {
                                return;
                            }
                            _this.toggleDisabledItems(_this.parent.currentHoveredBlock);
                            _this.popupObj.show();
                            _this.isPopupOpened = true;
                            setTimeout(function () {
                                var items = _this.popupObj.element.querySelectorAll('.e-menu-item');
                                Eif (items.length > 0) {
                                    items[0].focus();
                                    items[0].classList.add('e-focused');
                                }
                            });
                        }
                    };
                    this.parent.observer.notify('blockActionsMenuOpen', openEventArgs);
                }
            }
        };
        BlockActionMenuModule.prototype.getParentBlock = function (parentId) {
            return block_1.getBlockModelById(parentId, this.parent.getEditorBlocks());
        };
        BlockActionMenuModule.prototype.isFirstChildBlock = function (block, parentBlock) {
            var children = parentBlock.properties.children;
            return (children.length > 0 && children[0].id === block.id);
        };
        BlockActionMenuModule.prototype.isLastChildBlock = function (block, parentBlock) {
            var children = parentBlock.properties.children;
            return (children.length > 0 && children[children.length - 1].id === block.id);
        };
        BlockActionMenuModule.prototype.toggleMenuItemClass = function (itemId, disable) {
            var listElement = this.popupObj.element.querySelector("#" + itemId);
            Eif (listElement) {
                listElement.classList.toggle(constants.DISABLED_CLS, disable);
            }
        };
        BlockActionMenuModule.prototype.getBlockPositionInfo = function (blockElement) {
            var allBlocks = this.parent.getEditorBlocks();
            var currentBlock = block_1.getBlockModelById(blockElement.id, allBlocks);
            var currentBlockParent = this.getParentBlock(currentBlock.parentId);
            var currentBlockIndex = block_1.getBlockIndexById(blockElement.id, allBlocks);
            var isFirstBlock = currentBlockIndex === 0;
            var isLastBlock = currentBlockIndex === (currentBlockParent
                ? currentBlockParent.properties.children.length - 1
                : allBlocks.length - 1);
            var hasOnlyOneBlock = allBlocks.length === 1;
            return { currentBlock: currentBlock, currentBlockParent: currentBlockParent, isFirstBlock: isFirstBlock, isLastBlock: isLastBlock, hasOnlyOneBlock: hasOnlyOneBlock };
        };
        BlockActionMenuModule.prototype.toggleDisabledItems = function (blockElement) {
            if (!blockElement) {
                return;
            }
            var selectedBlocks = this.parent.editorMethods.getSelectedBlocks();
            if (selectedBlocks && selectedBlocks.length > 1) {
                for (var _i = 0, _a = this.parent.blockActionMenuSettings.items; _i < _a.length; _i++) {
                    var item = _a[_i];
                    this.toggleMenuItemClass(item.id, true);
                }
                return;
            }
            var _b = this.getBlockPositionInfo(blockElement), currentBlock = _b.currentBlock, currentBlockParent = _b.currentBlockParent, isFirstBlock = _b.isFirstBlock, isLastBlock = _b.isLastBlock, hasOnlyOneBlock = _b.hasOnlyOneBlock;
            for (var _c = 0, _d = this.parent.blockActionMenuSettings.items; _c < _d.length; _c++) {
                var item = _d[_c];
                var disable = item.disabled;
                switch (item.id) {
                    case 'moveup':
                        disable = hasOnlyOneBlock || isFirstBlock;
                        if (currentBlockParent && this.isFirstChildBlock(currentBlock, currentBlockParent)) {
                            disable = true;
                        }
                        break;
                    case 'movedown':
                        disable = hasOnlyOneBlock || isLastBlock;
                        if (currentBlockParent && this.isLastChildBlock(currentBlock, currentBlockParent)) {
                            disable = true;
                        }
                        break;
                }
                this.toggleMenuItemClass(item.id, disable);
            }
        };
        BlockActionMenuModule.prototype.handleBlockActionMenuSelect = function (args) {
            this.handleBlockActions(args.item, this.parent.currentHoveredBlock, args.event);
        };
        BlockActionMenuModule.prototype.handlePopupWidthChanges = function (data) {
            this.popupObj.width = this.parent.blockActionMenuSettings.popupWidth = data.value.toString();
        };
        BlockActionMenuModule.prototype.handlePopupHeightChanges = function (data) {
            this.popupObj.height = this.parent.blockActionMenuSettings.popupWidth = data.value.toString();
        };
        BlockActionMenuModule.prototype.handleBlockActions = function (item, blockElement, e) {
            var selectedItem = item.label.replace(' ', '').toLowerCase();
            var toBlockElement;
            var toBlockModel;
            switch (selectedItem) {
                case 'duplicate':
                    this.parent.execCommand({ command: 'DuplicateBlock', state: {
                            blockElement: blockElement, direction: 'below'
                        } });
                    break;
                case 'delete': {
                    var adjacentBlock = (blockElement.nextElementSibling || blockElement.previousElementSibling);
                    if (adjacentBlock) {
                        this.parent.setFocusAndUIForNewBlock(adjacentBlock);
                    }
                    this.parent.execCommand({ command: 'DeleteBlock', state: { blockElement: blockElement } });
                    break;
                }
                case 'moveup':
                case 'movedown': {
                    this.toggleDisabledItems(blockElement);
                    if (!blockElement || this.isItemDisabled(item.id)) {
                        return;
                    }
                    toBlockElement = (selectedItem === 'moveup' ? blockElement.previousElementSibling : blockElement.nextElementSibling);
                    Eif (toBlockElement) {
                        toBlockModel = block_1.getBlockModelById(toBlockElement.id, this.parent.getEditorBlocks());
                        this.parent.execCommand({ command: 'MoveBlock', state: {
                                fromBlockIds: [blockElement.id],
                                toBlockId: toBlockElement.id
                            } });
                    }
                    break;
                }
            }
            var currentBlockModel = block_1.getBlockModelById(blockElement.id, this.parent.getEditorBlocks());
            if ((currentBlockModel && block_1.isListTypeBlock(currentBlockModel.blockType)) ||
                (toBlockModel && block_1.isListTypeBlock(toBlockModel.blockType))) {
                this.parent.listPlugin.recalculateMarkersForListItems();
            }
            this.toggleBlockActionPopup(true, e);
        };
        BlockActionMenuModule.prototype.isItemDisabled = function (itemId) {
            var listElement = this.popupObj.element.querySelector('#' + itemId);
            return listElement && listElement.classList.contains(constants.DISABLED_CLS);
        };
        BlockActionMenuModule.prototype.isPopupOpen = function () {
            return this.isPopupOpened;
        };
        BlockActionMenuModule.prototype.destroy = function () {
            this.removeEventListeners();
            Eif (this.popupObj) {
                this.popupObj.destroy();
                ej2_base_1.detach(this.popupObj.element);
                this.popupObj = null;
            }
            this.shortcutMap = null;
        };
        return BlockActionMenuModule;
    }());
    exports.BlockActionMenuModule = BlockActionMenuModule;
});