all files / grid/actions/ column-chooser.js

93.04% Statements 294/316
79.84% Branches 99/124
97.56% Functions 40/41
92.99% Lines 292/314
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463   10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10×     10×     10× 10×     20× 20× 20× 10×     32×     25×     11×   10× 10× 10× 10× 10× 10×   21× 11×   10× 10× 10× 10× 10×   10× 10× 10×   26× 26×                                       26×   23× 13× 13×                                             21× 21×   15×       15× 15× 15× 14× 14×       15× 15× 15× 15×       15× 15×   11× 11×   85×   17×   10× 10× 10× 10× 10× 10× 10×   10× 10×                                               10× 10×   18× 18× 18×       18×   18× 18× 18× 18× 18× 18× 18× 18× 18× 18× 18×                       15×                     14× 14× 14× 11× 11× 11×         14× 12× 12× 12× 12×         14× 14×   10× 10× 10×   20× 11×       18× 18× 18× 11× 11× 11×           11× 11× 11×     11× 11× 11× 11× 11×   11×   30× 30× 133× 133×   30×   10× 10× 10× 40× 40× 40× 40× 34× 34×           40×   119× 119× 119×   133× 133× 133× 133× 133× 119× 119× 119× 119× 119× 119×         17×   11×   14×             7967×        
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-data", "@syncfusion/ej2-base", "../base/constant", "@syncfusion/ej2-popups", "../base/util", "@syncfusion/ej2-buttons", "../services/focus-strategy"], function (require, exports, ej2_base_1, ej2_data_1, ej2_base_2, events, ej2_popups_1, util_1, ej2_buttons_1, focus_strategy_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ColumnChooser = (function () {
        function ColumnChooser(parent, serviceLocator) {
            this.showColumn = [];
            this.hideColumn = [];
            this.isDlgOpen = false;
            this.dlghide = false;
            this.initialOpenDlg = true;
            this.stateChangeColumns = [];
            this.isInitialOpen = false;
            this.isCustomizeOpenCC = false;
            this.cBoxTrue = ej2_buttons_1.createCheckBox(true, { checked: true, label: ' ' });
            this.cBoxFalse = ej2_buttons_1.createCheckBox(true, { checked: false, label: ' ' });
            this.parent = parent;
            this.serviceLocator = serviceLocator;
            this.addEventListener();
            this.cBoxTrue.insertBefore(ej2_base_1.createElement('input', {
                className: 'e-chk-hidden e-cc e-cc-chbox', attrs: { type: 'checkbox' }
            }), this.cBoxTrue.firstChild);
            this.cBoxFalse.insertBefore(ej2_base_1.createElement('input', {
                className: 'e-chk-hidden e-cc e-cc-chbox', attrs: { 'type': 'checkbox' }
            }), this.cBoxFalse.firstChild);
            this.cBoxFalse.querySelector('.e-frame').classList.add('e-uncheck');
            if (this.parent.enableRtl) {
                ej2_base_1.addClass([this.cBoxTrue, this.cBoxFalse], ['e-rtl']);
            }
        }
        ColumnChooser.prototype.destroy = function () {
            this.removeEventListener();
            this.unWireEvents();
            if (!ej2_base_1.isNullOrUndefined(this.dlgObj) && this.dlgObj.element && !this.dlgObj.isDestroyed) {
                this.dlgObj.destroy();
            }
        };
        ColumnChooser.prototype.rtlUpdate = function () {
            if (this.parent.enableRtl) {
                ej2_base_1.addClass(this.innerDiv.querySelectorAll('.e-checkbox-wrapper'), ['e-rtl']);
            }
            else {
                ej2_base_1.removeClass(this.innerDiv.querySelectorAll('.e-checkbox-wrapper'), ['e-rtl']);
            }
        };
        ColumnChooser.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.click, this.clickHandler, this);
            this.parent.on(events.uiUpdate, this.enableAfterRenderEle, this);
            this.parent.on(events.initialEnd, this.render, this);
            this.parent.addEventListener(events.dataBound, this.hideDialog.bind(this));
            this.parent.on(events.destroy, this.destroy, this);
            this.parent.on(events.rtlUpdated, this.rtlUpdate, this);
        };
        ColumnChooser.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.click, this.clickHandler);
            this.parent.off(events.initialEnd, this.render);
            this.parent.off(events.destroy, this.destroy);
            this.parent.off(events.uiUpdate, this.enableAfterRenderEle);
            this.parent.off(events.rtlUpdated, this.rtlUpdate);
        };
        ColumnChooser.prototype.render = function () {
            this.l10n = this.serviceLocator.getService('localization');
            this.renderDlgContent();
            this.getShowHideService = this.serviceLocator.getService('showHideService');
        };
        ColumnChooser.prototype.clickHandler = function (e) {
            var targetElement = e.target;
            if (!this.isCustomizeOpenCC) {
                Eif (!ej2_base_1.isNullOrUndefined(ej2_base_2.closest(targetElement, '.e-cc')) || !ej2_base_1.isNullOrUndefined(ej2_base_2.closest(targetElement, '.e-cc-toolbar'))) {
                    Iif (targetElement.classList.contains('e-columnchooser-btn') || targetElement.classList.contains('e-cc-toolbar')) {
                        if ((this.initialOpenDlg && this.dlgObj.visible) || !this.isDlgOpen) {
                            this.isDlgOpen = true;
                            return;
                        }
                    }
                    else Iif (targetElement.classList.contains('e-cc-cancel')) {
                        targetElement.parentElement.querySelector('.e-ccsearch').value = '';
                        this.columnChooserSearch('');
                        this.removeCancelIcon();
                    }
                }
                else {
                    if (!ej2_base_1.isNullOrUndefined(this.dlgObj) && this.dlgObj.visible && !targetElement.classList.contains('e-toolbar-items')) {
                        this.dlgObj.hide();
                        this.clearActions();
                        this.refreshCheckboxState();
                        this.isDlgOpen = false;
                    }
                }
            }
            this.rtlUpdate();
        };
        ColumnChooser.prototype.hideDialog = function () {
            if (!ej2_base_1.isNullOrUndefined(this.dlgObj) && this.dlgObj.visible) {
                this.dlgObj.hide();
                this.isDlgOpen = false;
            }
        };
        ColumnChooser.prototype.renderColumnChooser = function (x, y, target) {
            if (!this.dlgObj.visible) {
                var pos = { X: null, Y: null };
                var args1 = {
                    requestType: 'beforeOpenColumnChooser', element: this.parent.element,
                    columns: this.getColumns(), cancel: false
                };
                this.parent.trigger(events.beforeOpenColumnChooser, args1);
                Iif (args1.cancel) {
                    return;
                }
                this.refreshCheckboxState();
                this.dlgObj.dataBind();
                this.dlgObj.element.style.maxHeight = '430px';
                var elementVisible = this.dlgObj.element.style.display;
                this.dlgObj.element.style.display = 'block';
                var newpos = ej2_popups_1.calculateRelativeBasedPosition(ej2_base_2.closest(target, '.e-toolbar-item'), this.dlgObj.element);
                this.dlgObj.element.style.display = elementVisible;
                this.dlgObj.element.style.top = newpos.top + ej2_base_2.closest(target, '.e-cc-toolbar').getBoundingClientRect().height + 'px';
                var dlgWidth = 250;
                if (!ej2_base_1.isNullOrUndefined(ej2_base_2.closest(target, '.e-bigger'))) {
                    this.dlgObj.width = 258;
                }
                Iif (ej2_base_1.Browser.isDevice) {
                    this.dlgObj.target = document.body;
                    this.dlgObj.position = { X: 'center', Y: 'center' };
                    this.dlgObj.refreshPosition();
                    this.dlgObj.open = this.mOpenDlg.bind(this);
                }
                else {
                    if (this.parent.enableRtl) {
                        this.dlgObj.element.style.left = target.offsetLeft + 'px';
                    }
                    else {
                        this.dlgObj.element.style.left = ((newpos.left - dlgWidth) + ej2_base_2.closest(target, '.e-cc-toolbar').clientWidth) + 2 + 'px';
                    }
                }
                this.removeCancelIcon();
                this.dlgObj.show();
            }
            else {
                this.hideDialog();
                this.addcancelIcon();
            }
            this.rtlUpdate();
        };
        ColumnChooser.prototype.openColumnChooser = function (X, Y) {
            this.isCustomizeOpenCC = true;
            if (this.dlgObj.visible) {
                this.hideDialog();
                return;
            }
            if (!this.isInitialOpen) {
                this.dlgObj.content = this.renderChooserList();
            }
            else {
                this.refreshCheckboxState();
            }
            this.dlgObj.dataBind();
            this.dlgObj.position = { X: 'center', Y: 'center' };
            if (ej2_base_1.isNullOrUndefined(X)) {
                this.dlgObj.position = { X: 'center', Y: 'center' };
                this.dlgObj.refreshPosition();
            }
            else {
                this.dlgObj.element.style.top = '';
                this.dlgObj.element.style.left = '';
                this.dlgObj.element.style.top = Y + 'px';
                this.dlgObj.element.style.left = X + 'px';
            }
            this.dlgObj.beforeOpen = this.customDialogOpen.bind(this);
            this.dlgObj.show();
            this.isInitialOpen = true;
            this.dlgObj.beforeClose = this.customDialogClose.bind(this);
        };
        ColumnChooser.prototype.enableAfterRenderEle = function (e) {
            Eif (e.module === this.getModuleName() && e.enable) {
                this.render();
            }
        };
        ColumnChooser.prototype.customDialogOpen = function () {
            var searchElement = this.dlgObj.content.querySelector('input.e-ccsearch');
            ej2_base_2.EventHandler.add(searchElement, 'keyup', this.columnChooserManualSearch, this);
        };
        ColumnChooser.prototype.customDialogClose = function () {
            var searchElement = this.dlgObj.content.querySelector('input.e-ccsearch');
            ej2_base_2.EventHandler.remove(searchElement, 'keyup', this.columnChooserManualSearch);
        };
        ColumnChooser.prototype.getColumns = function () {
            var columns = this.parent.getColumns().filter(function (column) { return (column.type !== 'checkbox'
                && column.showInColumnChooser === true) || (column.type === 'checkbox' && column.field !== undefined); });
            return columns;
        };
        ColumnChooser.prototype.renderDlgContent = function () {
            var y;
            this.dlgDiv = ej2_base_1.createElement('div', { className: 'e-ccdlg e-cc', id: this.parent.element.id + '_ccdlg' });
            this.parent.element.appendChild(this.dlgDiv);
            var xpos = this.parent.element.getBoundingClientRect().width - 250;
            var dialoPos = this.parent.enableRtl ? 'left' : 'right';
            var tarElement = this.parent.element.querySelector('.e-ccdiv');
            if (!ej2_base_1.isNullOrUndefined(tarElement)) {
                y = tarElement.getBoundingClientRect().top;
            }
            var pos = { X: null, Y: null };
            this.dlgObj = new ej2_popups_1.Dialog({
                header: this.l10n.getConstant('ChooseColumns'),
                showCloseIcon: false,
                closeOnEscape: false,
                locale: this.parent.locale,
                visible: false,
                enableRtl: this.parent.enableRtl,
                target: document.getElementById(this.parent.element.id),
                buttons: [{
                        click: this.confirmDlgBtnClick.bind(this),
                        buttonModel: {
                            content: this.l10n.getConstant('OKButton'), isPrimary: true,
                            cssClass: 'e-cc e-cc_okbtn',
                        }
                    },
                    {
                        click: this.clearActions.bind(this),
                        buttonModel: { cssClass: 'e-flat e-cc e-cc-cnbtn', content: this.l10n.getConstant('CancelButton') }
                    }],
                content: this.renderChooserList(),
                width: 250,
                cssClass: 'e-cc',
                animationSettings: { effect: 'None' },
            });
            this.dlgObj.appendTo(this.dlgDiv);
            this.wireEvents();
        };
        ColumnChooser.prototype.renderChooserList = function () {
            this.mainDiv = ej2_base_1.createElement('div', { className: 'e-main-div e-cc' });
            var searchDiv = ej2_base_1.createElement('div', { className: 'e-cc-searchdiv e-cc e-input-group' });
            var ccsearchele = ej2_base_1.createElement('input', {
                className: 'e-ccsearch e-cc e-input',
                attrs: { placeholder: this.l10n.getConstant('Search') }
            });
            var ccsearchicon = ej2_base_1.createElement('span', { className: 'e-ccsearch-icon e-icons e-cc e-input-group-icon',
                attrs: { title: this.l10n.getConstant('Search') } });
            var conDiv = ej2_base_1.createElement('div', { className: 'e-cc-contentdiv' });
            this.innerDiv = ej2_base_1.createElement('div', { className: 'e-innerdiv e-cc' });
            searchDiv.appendChild(ccsearchele);
            searchDiv.appendChild(ccsearchicon);
            this.searchBoxObj = new focus_strategy_1.SearchBox(ccsearchele);
            var innerDivContent = this.refreshCheckboxList(this.parent.getColumns());
            this.innerDiv.appendChild(innerDivContent);
            conDiv.appendChild(this.innerDiv);
            this.mainDiv.appendChild(searchDiv);
            this.mainDiv.appendChild(conDiv);
            return this.mainDiv;
        };
        ColumnChooser.prototype.confirmDlgBtnClick = function (args) {
            this.stateChangeColumns = [];
            if (!ej2_base_1.isNullOrUndefined(args)) {
                if (this.hideColumn.length) {
                    this.columnStateChange(this.hideColumn, false);
                }
                if (this.showColumn.length) {
                    this.columnStateChange(this.showColumn, true);
                }
                var params = {
                    requestType: 'columnstate', element: this.parent.element,
                    columns: this.stateChangeColumns, dialogInstance: this.dlgObj
                };
                this.parent.trigger(events.actionComplete, params);
                this.getShowHideService.setVisible(this.stateChangeColumns);
                this.clearActions();
                this.parent.notify(events.tooltipDestroy, { module: 'edit' });
            }
        };
        ColumnChooser.prototype.columnStateChange = function (stateColumns, state) {
            for (var index = 0; index < stateColumns.length; index++) {
                var colUid = stateColumns[index];
                var currentCol = this.parent.getColumnByUid(colUid);
                currentCol.visible = state;
                this.stateChangeColumns.push(currentCol);
            }
        };
        ColumnChooser.prototype.clearActions = function () {
            this.hideColumn = [];
            this.showColumn = [];
            this.hideDialog();
            this.addcancelIcon();
        };
        ColumnChooser.prototype.checkstatecolumn = function (isChecked, coluid) {
            if (isChecked) {
                Iif (this.hideColumn.indexOf(coluid) !== -1) {
                    this.hideColumn.splice(this.hideColumn.indexOf(coluid), 1);
                }
                if (this.showColumn.indexOf(coluid) === -1) {
                    this.showColumn.push(coluid);
                }
            }
            else {
                Iif (this.showColumn.indexOf(coluid) !== -1) {
                    this.showColumn.splice(this.showColumn.indexOf(coluid), 1);
                }
                Eif (this.hideColumn.indexOf(coluid) === -1) {
                    this.hideColumn.push(coluid);
                }
            }
        };
        ColumnChooser.prototype.columnChooserSearch = function (searchVal) {
            var clearSearch = false;
            var fltrCol;
            if (searchVal === '') {
                this.removeCancelIcon();
                fltrCol = this.getColumns();
                clearSearch = true;
            }
            else {
                fltrCol = new ej2_data_1.DataManager(this.getColumns()).executeLocal(new ej2_data_1.Query()
                    .where('headerText', 'startswith', searchVal, true));
            }
            if (fltrCol.length) {
                this.innerDiv.innerHTML = ' ';
                this.innerDiv.classList.remove('e-ccnmdiv');
                this.innerDiv.appendChild(this.refreshCheckboxList(fltrCol, searchVal));
                if (!clearSearch) {
                    this.addcancelIcon();
                }
            }
            else {
                var nMatchele = ej2_base_1.createElement('span', { className: 'e-cc e-nmatch' });
                nMatchele.innerHTML = this.l10n.getConstant('Matchs');
                this.innerDiv.innerHTML = ' ';
                this.innerDiv.appendChild(nMatchele);
                this.innerDiv.classList.add('e-ccnmdiv');
            }
            this.flag = true;
            this.stopTimer();
        };
        ColumnChooser.prototype.wireEvents = function () {
            ej2_base_2.EventHandler.add(this.dlgObj.element, 'click', this.checkBoxClickHandler, this);
            ej2_base_2.EventHandler.add(this.searchBoxObj.searchBox, 'keyup', this.columnChooserManualSearch, this);
            this.searchBoxObj.wireEvent();
        };
        ColumnChooser.prototype.unWireEvents = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            Eif (this.dlgObj.element) {
                ej2_base_2.EventHandler.remove(this.dlgObj.element, 'click', this.checkBoxClickHandler);
            }
            ej2_base_2.EventHandler.remove(this.searchBoxObj.searchBox, 'keyup', this.columnChooserManualSearch);
            this.searchBoxObj.unWireEvent();
        };
        ColumnChooser.prototype.checkBoxClickHandler = function (e) {
            var checkstate;
            var elem = util_1.parentsUntil(e.target, 'e-checkbox-wrapper');
            if (elem) {
                util_1.toogleCheckbox(elem.parentElement);
                elem.querySelector('.e-chk-hidden').focus();
                if (elem.querySelector('.e-check')) {
                    checkstate = true;
                }
                else Eif (elem.querySelector('.e-uncheck')) {
                    checkstate = false;
                }
                else {
                    return;
                }
                var columnUid = util_1.parentsUntil(elem, 'e-ccheck').getAttribute('uid');
                this.checkstatecolumn(checkstate, columnUid);
                this.refreshCheckboxButton();
            }
        };
        ColumnChooser.prototype.refreshCheckboxButton = function () {
            var searchValue = this.dlgObj.element.querySelector('.e-cc.e-input').value;
            var selected = this.innerDiv.querySelectorAll('.e-check').length;
            var btn = this.dlgDiv.querySelector('.e-footer-content').querySelector('.e-btn').ej2_instances[0];
            btn.disabled = false;
            if (selected === 0 && searchValue === '') {
                btn.disabled = true;
            }
            btn.dataBind();
        };
        ColumnChooser.prototype.refreshCheckboxList = function (gdCol, searchVal) {
            this.ulElement = ej2_base_1.createElement('ul', { className: 'e-ccul-ele e-cc' });
            for (var i = 0; i < gdCol.length; i++) {
                var columns = gdCol[i];
                this.renderCheckbox(columns);
            }
            return this.ulElement;
        };
        ColumnChooser.prototype.refreshCheckboxState = function () {
            this.dlgObj.element.querySelector('.e-cc.e-input').value = '';
            this.columnChooserSearch('');
            for (var i = 0; i < this.parent.element.querySelectorAll('.e-cc-chbox').length; i++) {
                var element = this.parent.element.querySelectorAll('.e-cc-chbox')[i];
                var columnUID = util_1.parentsUntil(element, 'e-ccheck').getAttribute('uid');
                var column = this.parent.getColumnByUid(columnUID);
                if (column.visible) {
                    element.checked = true;
                    this.checkState(element.parentElement.querySelector('.e-icons'), true);
                }
                else {
                    element.checked = false;
                    this.checkState(element.parentElement.querySelector('.e-icons'), false);
                }
            }
        };
        ColumnChooser.prototype.checkState = function (element, state) {
            state ? ej2_base_1.classList(element, ['e-check'], ['e-uncheck']) : ej2_base_1.classList(element, ['e-uncheck'], ['e-check']);
        };
        ColumnChooser.prototype.createCheckBox = function (label, checked, uid) {
            var cbox = checked ? this.cBoxTrue.cloneNode(true) : this.cBoxFalse.cloneNode(true);
            cbox.querySelector('.e-label').innerHTML = label;
            return util_1.createCboxWithWrap(uid, cbox, 'e-ccheck');
        };
        ColumnChooser.prototype.renderCheckbox = function (column) {
            var cclist;
            var hideColState;
            var showColState;
            var checkBoxObj;
            if (column.showInColumnChooser) {
                cclist = ej2_base_1.createElement('li', { className: 'e-cclist e-cc', styles: 'list-style:None', id: 'e-ccli_' + column.uid });
                hideColState = this.hideColumn.indexOf(column.uid) === -1 ? false : true;
                showColState = this.showColumn.indexOf(column.uid) === -1 ? false : true;
                var cccheckboxlist = this.createCheckBox(column.headerText, (column.visible && !hideColState) || showColState, column.uid);
                cclist.appendChild(cccheckboxlist);
                this.ulElement.appendChild(cclist);
            }
        };
        ColumnChooser.prototype.columnChooserManualSearch = function (e) {
            this.addcancelIcon();
            this.searchValue = e.target.value;
            var proxy = this;
            this.stopTimer();
            this.startTimer(e);
        };
        ColumnChooser.prototype.startTimer = function (e) {
            var proxy = this;
            var interval = !proxy.flag && e.keyCode !== 13 ? 500 : 0;
            this.timer = window.setInterval(function () { proxy.columnChooserSearch(proxy.searchValue); }, interval);
        };
        ColumnChooser.prototype.stopTimer = function () {
            window.clearInterval(this.timer);
        };
        ColumnChooser.prototype.addcancelIcon = function () {
            this.dlgDiv.querySelector('.e-cc.e-ccsearch-icon').classList.add('e-cc-cancel');
        };
        ColumnChooser.prototype.removeCancelIcon = function () {
            this.dlgDiv.querySelector('.e-cc.e-ccsearch-icon').classList.remove('e-cc-cancel');
        };
        ColumnChooser.prototype.mOpenDlg = function () {
            if (ej2_base_1.Browser.isDevice) {
                this.dlgObj.element.querySelector('.e-cc-searchdiv').classList.remove('e-input-focus');
                this.dlgObj.element.querySelectorAll('.e-cc-chbox')[0].focus();
            }
        };
        ColumnChooser.prototype.getModuleName = function () {
            return 'columnChooser';
        };
        return ColumnChooser;
    }());
    exports.ColumnChooser = ColumnChooser;
});