all files / grid/actions/ toolbar.js

96.65% Statements 173/179
91.57% Branches 76/83
100% Functions 23/23
96.65% Lines 173/179
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 269 270 271 272 273 274 275 276   63× 63×   63× 63× 63× 63×   64× 64×   64× 640× 640× 640× 640×         64×                   64×       64×   34×   62× 59×       59×   59× 59× 59×     64× 64×           64× 64× 64×       64× 64×             63×   64× 64× 64× 64× 64× 64×     739× 739× 739× 739× 739× 739× 739× 739× 739× 378× 251× 251×     127× 127×       361× 118× 118×     243× 243×     739× 739×   64× 64× 64×     64× 274× 274×     272× 272×       64×     273×   1479× 3696× 3696× 3696× 3526×       70× 70× 70× 70× 70×     70×     10× 10×   19× 19×     20× 20×             70× 14×     16× 14×             136×     64×     60×     64×   63× 63× 63× 63× 63× 63× 63×   61× 59×     7982×        
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-navigations", "../base/constant", "../base/util", "../services/focus-strategy"], function (require, exports, ej2_base_1, ej2_base_2, ej2_navigations_1, events, util_1, focus_strategy_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Toolbar = (function () {
        function Toolbar(parent, serviceLocator) {
            this.predefinedItems = {};
            this.items = ['Add', 'Edit', 'Update', 'Delete', 'Cancel', 'Print', 'Search',
                'ColumnChooser', 'PdfExport', 'ExcelExport', 'CsvExport', 'WordExport'];
            this.parent = parent;
            this.gridID = parent.element.id;
            this.serviceLocator = serviceLocator;
            this.addEventListener();
        }
        Toolbar.prototype.render = function () {
            this.l10n = this.serviceLocator.getService('localization');
            var preItems = ['Add', 'Edit', 'Update', 'Delete', 'Cancel', 'Print',
                'PdfExport', 'ExcelExport', 'WordExport', 'CsvExport'];
            for (var _i = 0, preItems_1 = preItems; _i < preItems_1.length; _i++) {
                var item = preItems_1[_i];
                var itemStr = item.toLowerCase();
                var localeName = itemStr[0].toUpperCase() + itemStr.slice(1);
                this.predefinedItems[item] = {
                    id: this.gridID + '_' + itemStr, prefixIcon: 'e-' + itemStr,
                    text: this.l10n.getConstant(localeName), tooltipText: this.l10n.getConstant(localeName)
                };
            }
            this.predefinedItems.Search = {
                id: this.gridID + '_search',
                template: '<div class="e-input-group e-search" role="search">\
            <input id="' + this.gridID + '_searchbar" class="e-input" name="input" type="search" \
            placeholder= \"' + this.l10n.getConstant('Search') + '\"/>\
            <span id="' + this.gridID + '_searchbutton" class="e-input-group-icon e-search-icon e-icons" \
            tabindex="-1" title="' + this.l10n.getConstant('Search') + '" aria-label= "search"></span> \
            </div>',
                tooltipText: this.l10n.getConstant('Search'), align: 'Right', cssClass: 'e-search-wrapper'
            };
            this.predefinedItems.ColumnChooser = {
                id: this.gridID + '_' + 'columnchooser', cssClass: 'e-cc e-ccdiv e-cc-toolbar', suffixIcon: 'e-' + 'columnchooser-btn',
                text: this.l10n.getConstant('Columnchooser'), tooltipText: this.l10n.getConstant('Columnchooser'), align: 'Right',
            };
            this.createToolbar();
        };
        Toolbar.prototype.getToolbar = function () {
            return this.toolbar.element;
        };
        Toolbar.prototype.destroy = function () {
            if (!this.toolbar.isDestroyed) {
                Iif (!this.toolbar.element) {
                    this.parent.destroyTemplate(['toolbarTemplate']);
                }
                else {
                    this.toolbar.destroy();
                }
                this.unWireEvent();
                this.removeEventListener();
                ej2_base_2.remove(this.element);
            }
        };
        Toolbar.prototype.createToolbar = function () {
            var items = this.getItems();
            this.toolbar = new ej2_navigations_1.Toolbar({
                items: items,
                clicked: this.toolbarClickHandler.bind(this),
                enablePersistence: this.parent.enablePersistence,
                enableRtl: this.parent.enableRtl
            });
            var viewStr = 'viewContainerRef';
            var registerTemp = 'registeredTemplate';
            Iif (this.parent[viewStr]) {
                this.toolbar[registerTemp] = {};
                this.toolbar[viewStr] = this.parent[viewStr];
            }
            this.element = ej2_base_2.createElement('div', { id: this.gridID + '_toolbarItems' });
            if (this.parent.toolbarTemplate) {
                Eif (typeof (this.parent.toolbarTemplate) === 'string') {
                    this.toolbar.appendTo(this.parent.toolbarTemplate);
                    this.element = this.toolbar.element;
                }
                else {
                    util_1.appendChildren(this.element, util_1.templateCompiler(this.parent.toolbarTemplate)({}, this.parent, 'toolbarTemplate'));
                }
            }
            else {
                this.toolbar.appendTo(this.element);
            }
            this.parent.element.insertBefore(this.element, this.parent.getHeaderContent());
            this.searchElement = this.element.querySelector('#' + this.gridID + '_searchbar');
            this.wireEvent();
            this.refreshToolbarItems();
            Eif (this.parent.searchSettings) {
                this.updateSearchBox();
            }
        };
        Toolbar.prototype.refreshToolbarItems = function (args) {
            var gObj = this.parent;
            var enableItems = [];
            var disableItems = [];
            var edit = gObj.editSettings;
            var hasData = gObj.currentViewData && gObj.currentViewData.length;
            edit.allowAdding ? enableItems.push(this.gridID + '_add') : disableItems.push(this.gridID + '_add');
            edit.allowEditing && hasData ? enableItems.push(this.gridID + '_edit') : disableItems.push(this.gridID + '_edit');
            edit.allowDeleting && hasData ? enableItems.push(this.gridID + '_delete') : disableItems.push(this.gridID + '_delete');
            if (gObj.editSettings.mode === 'Batch') {
                if (gObj.element.querySelectorAll('.e-updatedtd').length && (edit.allowAdding || edit.allowEditing)) {
                    enableItems.push(this.gridID + '_update');
                    enableItems.push(this.gridID + '_cancel');
                }
                else {
                    disableItems.push(this.gridID + '_update');
                    disableItems.push(this.gridID + '_cancel');
                }
            }
            else {
                if (gObj.isEdit && (edit.allowAdding || edit.allowEditing)) {
                    enableItems = [this.gridID + '_update', this.gridID + '_cancel'];
                    disableItems = [this.gridID + '_add', this.gridID + '_edit', this.gridID + '_delete'];
                }
                else {
                    disableItems.push(this.gridID + '_update');
                    disableItems.push(this.gridID + '_cancel');
                }
            }
            this.enableItems(enableItems, true);
            this.enableItems(disableItems, false);
        };
        Toolbar.prototype.getItems = function () {
            var items = [];
            var toolbarItems = this.parent.toolbar || [];
            Iif (typeof (this.parent.toolbar) === 'string') {
                return [];
            }
            for (var _i = 0, toolbarItems_1 = toolbarItems; _i < toolbarItems_1.length; _i++) {
                var item = toolbarItems_1[_i];
                switch (typeof item) {
                    case 'number':
                        items.push(this.getItemObject(this.items[item]));
                        break;
                    case 'string':
                        items.push(this.getItemObject(item));
                        break;
                    default:
                        items.push(this.getItem(item));
                }
            }
            return items;
        };
        Toolbar.prototype.getItem = function (itemObject) {
            var item = this.predefinedItems[itemObject.text];
            return item ? ej2_base_1.extend(item, item, itemObject) : itemObject;
        };
        Toolbar.prototype.getItemObject = function (itemName) {
            return this.predefinedItems[itemName] || { text: itemName, id: this.gridID + '_' + itemName };
        };
        Toolbar.prototype.enableItems = function (items, isEnable) {
            for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
                var item = items_1[_i];
                var element = this.element.querySelector('#' + item);
                if (element) {
                    this.toolbar.enableItems(element.parentElement, isEnable);
                }
            }
        };
        Toolbar.prototype.toolbarClickHandler = function (args) {
            var gObj = this.parent;
            var gID = this.gridID;
            ej2_base_1.extend(args, { cancel: false });
            gObj.trigger(events.toolbarClick, args);
            Iif (args.cancel) {
                return;
            }
            switch (!ej2_base_1.isNullOrUndefined(args.item) && args.item.id) {
                case gID + '_print':
                    gObj.print();
                    break;
                case gID + '_edit':
                    gObj.startEdit();
                    break;
                case gID + '_update':
                    gObj.endEdit();
                    break;
                case gID + '_cancel':
                    gObj.closeEdit();
                    break;
                case gID + '_add':
                    gObj.addRecord();
                    break;
                case gID + '_delete':
                    gObj.deleteRecord();
                    break;
                case gID + '_search':
                    if (args.originalEvent.target.id === gID + '_searchbutton') {
                        this.search();
                    }
                    break;
                case gID + '_columnchooser':
                    var tarElement = this.parent.element.querySelector('.e-ccdiv');
                    var y = tarElement.getBoundingClientRect().top;
                    var x = tarElement.getBoundingClientRect().left;
                    var targetEle = args.originalEvent.target;
                    y = tarElement.getBoundingClientRect().top + tarElement.offsetTop;
                    gObj.createColumnchooser(x, y, targetEle);
                    break;
            }
        };
        Toolbar.prototype.modelChanged = function (e) {
            if (e.module === 'edit') {
                this.refreshToolbarItems();
            }
        };
        Toolbar.prototype.onPropertyChanged = function (e) {
            if (e.module !== this.getModuleName() || !this.parent.toolbar) {
                return;
            }
            if (this.element) {
                ej2_base_2.remove(this.element);
            }
            this.render();
        };
        Toolbar.prototype.keyUpHandler = function (e) {
            if (e.keyCode === 13) {
                this.search();
            }
        };
        Toolbar.prototype.search = function () {
            this.parent.search(this.searchElement.value);
        };
        Toolbar.prototype.updateSearchBox = function () {
            if (this.searchElement) {
                this.searchElement.value = this.parent.searchSettings.key;
            }
        };
        Toolbar.prototype.wireEvent = function () {
            if (this.searchElement) {
                this.searchBoxObj = new focus_strategy_1.SearchBox(this.searchElement);
                ej2_base_1.EventHandler.add(this.searchElement, 'keyup', this.keyUpHandler, this);
                this.searchBoxObj.wireEvent();
            }
        };
        Toolbar.prototype.unWireEvent = function () {
            if (this.searchElement) {
                ej2_base_1.EventHandler.remove(this.searchElement, 'keyup', this.keyUpHandler);
                this.searchBoxObj.unWireEvent();
            }
        };
        Toolbar.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.initialEnd, this.render, this);
            this.parent.on(events.uiUpdate, this.onPropertyChanged, this);
            this.parent.on(events.inBoundModelChanged, this.updateSearchBox.bind(this));
            this.parent.on(events.modelChanged, this.refreshToolbarItems, this);
            this.parent.on(events.toolbarRefresh, this.refreshToolbarItems, this);
            this.parent.on(events.inBoundModelChanged, this.modelChanged, this);
            this.parent.on(events.dataBound, this.refreshToolbarItems, this);
        };
        Toolbar.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.initialEnd, this.render);
            this.parent.off(events.uiUpdate, this.onPropertyChanged);
            this.parent.off(events.inBoundModelChanged, this.updateSearchBox);
            this.parent.off(events.modelChanged, this.refreshToolbarItems);
            this.parent.off(events.toolbarRefresh, this.refreshToolbarItems);
            this.parent.off(events.inBoundModelChanged, this.modelChanged);
            this.parent.off(events.dataBound, this.refreshToolbarItems);
        };
        Toolbar.prototype.getModuleName = function () {
            return 'toolbar';
        };
        return Toolbar;
    }());
    exports.Toolbar = Toolbar;
});