all files / common/period-selector/ period-selector.js

89.51% Statements 239/267
75.53% Branches 142/188
100% Functions 24/24
89.81% Lines 238/265
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   127× 127× 127× 127× 127× 127× 127× 110× 110×     127× 17× 17× 17× 17× 17× 17× 17×     110× 110× 110× 110× 110× 104×     127× 127×   110× 110× 110×   127×   127× 127× 127× 17× 17×     110× 110×   127× 12×   127×             127×   127× 127× 127× 127× 127× 127× 127× 127× 127× 127× 127× 127× 127× 570×   127× 110×   127× 127× 127×   127×     127× 110× 110×   110× 110×       127× 127×     33×   127× 127× 127× 570× 114× 114× 114× 19×   114× 114× 114× 114× 114× 108×         127× 13× 13×   127×     127× 127× 127× 127× 127×                   127× 127× 127× 127× 127× 127×     127× 127×         127× 127× 127×       127×         25× 15×   15×                         15×         15× 15×                 127×     540×     267×       13× 13× 13× 24× 24×     24×     24×     24×     24×     24×       13×   127× 127× 127× 127× 127× 110× 21×     21× 21×     110× 110×         110× 110×     110× 16×         16× 16×       127×   137× 738×   137× 124× 124×     33× 33× 33× 33× 109× 33× 33× 33× 109×   109× 10× 10× 10×       33× 10×   33× 10×   33×           32×                         31×                   33× 23×   33× 23× 23×             140× 140×                 129× 129×   140×   589×          
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-navigations", "@syncfusion/ej2-calendars", "../../common/utils/helper", "@syncfusion/ej2-svg-base"], function (require, exports, ej2_base_1, ej2_navigations_1, ej2_calendars_1, helper_1, ej2_svg_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PeriodSelector = (function () {
        function PeriodSelector(control) {
            this.control = {};
            this.isDatetimeCategory = false;
            this.sortedData = [];
            this.startValue = 0;
            this.endValue = 0;
            this.rootControl = control;
            if (this.rootControl.getModuleName() === 'stockChart') {
                this.sortedData = this.rootControl.sortedData;
                this.isDatetimeCategory = this.rootControl.isDateTimeCategory;
            }
        }
        PeriodSelector.prototype.setControlValues = function (control) {
            if (control.getModuleName() === 'rangeNavigator') {
                this.control.periods = this.rootControl.periodSelectorSettings.periods;
                this.control.seriesXMax = control.chartSeries.xMax;
                this.control.seriesXMin = control.chartSeries.xMin;
                this.control.rangeSlider = control.rangeSlider;
                this.control.rangeNavigatorControl = control;
                this.control.endValue = control.endValue;
                this.control.startValue = control.startValue;
            }
            else {
                this.control.periods = this.rootControl.periods;
                this.control.endValue = this.control.seriesXMax = control.seriesXMax;
                this.control.startValue = this.control.seriesXMin = control.seriesXMin;
                this.control.rangeNavigatorControl = this.rootControl.rangeNavigator;
                if (this.control.rangeNavigatorControl) {
                    this.control.rangeSlider = this.rootControl.rangeNavigator.rangeSlider;
                }
            }
            this.control.element = control.element;
            this.control.disableRangeSelector = control.disableRangeSelector;
        };
        PeriodSelector.prototype.appendSelector = function (options, x) {
            Iif (x === void 0) { x = 0; }
            this.renderSelectorElement(null, options, x);
            this.renderSelector();
        };
        PeriodSelector.prototype.renderSelectorElement = function (control, options, x) {
            this.periodSelectorSize = control ? this.periodSelectorSize : new ej2_svg_base_1.Rect(x, this.rootControl.titleSize.height ?
                this.rootControl.titleSize.height : 10, options.width, options.height);
            var thumbSize;
            var element;
            if (control) {
                thumbSize = control.themeStyle.thumbWidth;
                element = control.element;
            }
            else {
                thumbSize = options.thumbSize;
                element = options.element;
            }
            if (helper_1.getElement(element.id + '_Secondary_Element')) {
                ej2_base_1.remove(helper_1.getElement(element.id + '_Secondary_Element'));
            }
            this.periodSelectorDiv = ej2_base_1.createElement('div', {
                id: element.id + '_Secondary_Element',
                styles: 'width: ' + (this.periodSelectorSize.width - thumbSize) + 'px;height: ' +
                    this.periodSelectorSize.height + 'px;top:' +
                    this.periodSelectorSize.y + 'px;left:' +
                    (this.periodSelectorSize.x + thumbSize / 2) + 'px; position: absolute'
            });
            element.appendChild(this.periodSelectorDiv);
        };
        PeriodSelector.prototype.renderSelector = function () {
            var _this = this;
            this.setControlValues(this.rootControl);
            var enableCustom = true;
            var controlId = this.control.element.id;
            var selectorElement = ej2_base_1.createElement('div', { id: controlId + '_selector' });
            var buttons = this.control.periods;
            var selector = this.updateCustomElement();
            var buttonStyles = 'text-transform: none; text-overflow: unset';
            var isStringTemplate = 'isStringTemplate';
            var dateRangeId = controlId + 'customRange';
            var selectedPeriod;
            this.periodSelectorDiv.appendChild(selectorElement);
            for (var i = 0; i < buttons.length; i++) {
                selector.push({ align: 'Left', text: buttons[i].text });
            }
            if (this.rootControl.getModuleName() === 'stockChart') {
                enableCustom = this.rootControl.enableCustomRange;
            }
            Eif (enableCustom) {
                this.calendarId = controlId + '_calendar';
                selector.push({ template: '<button id=' + this.calendarId + '></button>', align: 'Right' });
            }
            var selctorArgs = {
                selector: selector, name: 'RangeSelector', cancel: false, enableCustomFormat: true, content: 'Date Range'
            };
            if (this.rootControl.getModuleName() === 'stockChart') {
                Eif (this.rootControl.exportType.length) {
                    var exportElement = ej2_base_1.createElement('button', { id: controlId + '_export', styles: buttonStyles,
                        className: 'e-dropdown-btn e-btn e-flat' });
                    exportElement.innerText = 'Export';
                    selector.push({ template: exportElement,
                        align: 'Right' });
                }
            }
            this.rootControl.trigger('selectorRender', selctorArgs);
            this.toolbar = new ej2_navigations_1.Toolbar({
                items: selctorArgs.selector, height: this.periodSelectorSize.height,
                clicked: function (args) {
                    _this.buttonClick(args, _this.control);
                }, created: function () {
                    _this.nodes = _this.toolbar.element.querySelectorAll('.e-toolbar-left')[0];
                    Eif (ej2_base_1.isNullOrUndefined(_this.selectedIndex)) {
                        buttons.map(function (period, index) {
                            if (period.selected && _this.selectedPeriod !== null) {
                                selectedPeriod = period;
                                _this.control.startValue = _this.changedRange(period.intervalType, _this.control.endValue, period.interval).getTime();
                                if (_this.isDatetimeCategory) {
                                    _this.control.startValue = _this.rootControl.getModuleName() !== 'stockChart' ? _this.findStartValue(_this.control.startValue, _this.control.endValue) : _this.rootControl.startValue;
                                }
                                _this.control.startValue = (period.text && period.text.toLowerCase() === 'all') ? _this.control.seriesXMin : _this.control.startValue;
                                _this.control.endValue = (period.text && period.text.toLowerCase() === 'all') ? _this.control.seriesXMax : _this.control.endValue;
                                _this.selectedIndex = (_this.nodes.childNodes.length - buttons.length) + index;
                                var slider = _this.control.rangeSlider;
                                if (slider) {
                                    slider.selectedPeriod = period.text;
                                }
                            }
                        });
                    }
                    if (!selectedPeriod && _this.rootControl.getModuleName() !== 'stockChart') {
                        var selectedIndex = _this.findSelectedIndex(_this.control.startValue, _this.control.endValue, buttons);
                        _this.selectedIndex = selectedIndex ? selectedIndex : _this.selectedIndex;
                    }
                    _this.setSelectedStyle(_this.selectedIndex);
                }
            });
            this.toolbar[isStringTemplate] = true;
            this.toolbar.appendTo(selectorElement);
            this.triggerChange = true;
            Eif (enableCustom) {
                this.datePicker = new ej2_calendars_1.DateRangePicker({
                    min: this.isDatetimeCategory ? new Date(this.sortedData[this.control.seriesXMin]) : new Date(this.control.seriesXMin),
                    max: this.isDatetimeCategory ? new Date(this.sortedData[this.control.seriesXMax]) : new Date(this.control.seriesXMax),
                    format: 'dd/MM/yyyy', placeholder: 'Select a range',
                    showClearButton: false,
                    startDate: this.isDatetimeCategory ? new Date(this.sortedData[Math.floor(this.control.startValue)]) :
                        new Date(this.control.startValue),
                    endDate: this.isDatetimeCategory ? new Date(this.sortedData[Math.floor(this.control.endValue)]) :
                        new Date(this.control.endValue),
                    created: function () {
                        Eif (selctorArgs.enableCustomFormat) {
                            var datePicker = document.getElementsByClassName('e-date-range-wrapper');
                            var datePickerElement = void 0;
                            for (var i = 0; i < datePicker.length; i++) {
                                Eif (datePicker[i].children[0].id.indexOf(controlId) !== -1) {
                                    datePickerElement = datePicker[i];
                                }
                            }
                            datePickerElement.style.display = 'none';
                            var element = ej2_base_1.createElement('div', {
                                id: dateRangeId,
                                className: 'e-control e-btn e-dropdown-btn e-flat',
                                styles: 'font-size: 14px; font-weight: 500; text-transform: none '
                            });
                            element.innerText = selctorArgs.content;
                            datePickerElement.insertAdjacentElement('afterend', element);
                            helper_1.getElement(dateRangeId).insertAdjacentElement('afterbegin', (ej2_base_1.createElement('span', {
                                id: controlId + 'dateIcon', className: 'e-input-group-icon e-range-icon e-btn-icon e-icons',
                                styles: 'font-size: 16px; min-height: 0px; margin: -3px 0 0 0; outline: none; min-width: 30px'
                            })));
                            document.getElementById(dateRangeId).onclick = function () {
                                _this.datePicker.show(helper_1.getElement(dateRangeId));
                            };
                        }
                    },
                    change: function (args) {
                        if (_this.triggerChange) {
                            if (_this.isDatetimeCategory) {
                                _this.startValue = args.startDate.getTime();
                                _this.endValue = args.endDate.getTime();
                                _this.findPeriodValue(_this.startValue, _this.endValue);
                            }
                            Iif (_this.control.rangeSlider && args.event) {
                                if (_this.rootControl.getModuleName() !== 'stockChart') {
                                    _this.control.rangeNavigatorControl.startValue = _this.isDatetimeCategory ? _this.startValue :
                                        args.startDate.getTime();
                                    _this.control.rangeNavigatorControl.endValue = _this.isDatetimeCategory ? _this.endValue :
                                        args.endDate.getTime();
                                    _this.selectedIndex = undefined;
                                    _this.selectedPeriod = null;
                                    _this.control.rangeNavigatorControl.refresh();
                                }
                                _this.control.rangeSlider.performAnimation(_this.isDatetimeCategory ? _this.startValue : args.startDate.getTime(), _this.isDatetimeCategory ?
                                    _this.endValue : args.endDate.getTime(), _this.control.rangeNavigatorControl);
                            }
                            else Iif (args.event) {
                                _this.rootControl.rangeChanged(_this.isDatetimeCategory ? _this.startValue :
                                    args.startDate.getTime(), _this.isDatetimeCategory ? _this.endValue :
                                    args.endDate.getTime());
                            }
                            _this.nodes = _this.toolbar.element.querySelectorAll('.e-toolbar-left')[0];
                            Iif (!_this.rootControl.resizeTo && _this.control.rangeSlider && _this.control.rangeSlider.isDrag) {
                                for (var i = 0, length_1 = _this.nodes.childNodes.length; i < length_1; i++) {
                                    _this.nodes.childNodes[i].childNodes[0].classList.remove('e-active');
                                    _this.nodes.childNodes[i].childNodes[0].classList.remove('e-active');
                                }
                            }
                        }
                    }
                });
                this.datePicker.appendTo('#' + this.calendarId);
            }
        };
        PeriodSelector.prototype.findPeriodValue = function (startValue, endValue) {
            for (var index = 0; index < (this.sortedData).length; index++) {
                if ((this.sortedData[index]) >= startValue) {
                    this.startValue = index;
                    break;
                }
            }
            for (var index = this.sortedData.length - 1; index >= 0; index--) {
                if ((this.sortedData[index]) <= endValue) {
                    this.endValue = index;
                    break;
                }
            }
        };
        PeriodSelector.prototype.findSelectedIndex = function (startDate, endDate, buttons) {
            var daysDiffence = (endDate - startDate) / (1000 * 60 * 60 * 24);
            var selectedIndex;
            for (var i = 0; i < buttons.length; i++) {
                var period = buttons[i];
                Iif (period.intervalType === 'Years' && daysDiffence / 365 === period.interval) {
                    selectedIndex = i;
                }
                else Iif (period.intervalType === 'Months' && (daysDiffence / 30 === period.interval || daysDiffence / 31 === period.interval)) {
                    selectedIndex = i;
                }
                else Iif (period.intervalType === 'Days' && daysDiffence === period.interval) {
                    selectedIndex = i;
                }
                else Iif (period.intervalType === 'Weeks' && daysDiffence / 7 === period.interval) {
                    selectedIndex = i;
                }
                else Iif (period.intervalType === 'Hours' && daysDiffence * 24 === period.interval) {
                    selectedIndex = i;
                }
                else Iif (period.intervalType === 'Seconds' && (daysDiffence * 24 * 3600) === period.interval) {
                    selectedIndex = i;
                }
            }
            return selectedIndex;
        };
        PeriodSelector.prototype.updateCustomElement = function () {
            var selector = [];
            var controlId = this.rootControl.element.id;
            var buttonStyles = 'text-transform: none; text-overflow: unset';
            var className = 'e-dropdown-btn e-btn e-flat';
            if (this.rootControl.getModuleName() === 'stockChart') {
                if (this.rootControl.seriesType.length) {
                    var SeriesElement = ej2_base_1.createElement('button', { id: controlId + '_seriesType',
                        styles: buttonStyles,
                        className: className });
                    SeriesElement.innerText = 'Series';
                    selector.push({ template: SeriesElement,
                        align: 'Left' });
                }
                Eif (this.rootControl.indicatorType.length) {
                    var indicatorElement = ej2_base_1.createElement('button', {
                        id: controlId + '_indicatorType',
                        styles: buttonStyles,
                        className: className
                    });
                    indicatorElement.innerText = 'Indicators';
                    selector.push({ template: indicatorElement,
                        align: 'Left' });
                }
                if (this.rootControl.trendlineType.length) {
                    var trendlineElement = ej2_base_1.createElement('button', {
                        id: controlId + '_trendType',
                        styles: buttonStyles,
                        className: className
                    });
                    trendlineElement.innerText = 'Trendline';
                    selector.push({ template: trendlineElement,
                        align: 'Left' });
                }
            }
            return selector;
        };
        PeriodSelector.prototype.setSelectedStyle = function (selectedIndex) {
            for (var i = 0, length_2 = this.nodes.childNodes.length; i < length_2; i++) {
                this.nodes.childNodes[i].childNodes[0].classList.remove('e-active');
            }
            if (!ej2_base_1.isNullOrUndefined(selectedIndex)) {
                this.nodes.childNodes[selectedIndex].childNodes[0].classList.add('e-flat');
                this.nodes.childNodes[selectedIndex].childNodes[0].classList.add('e-active');
            }
        };
        PeriodSelector.prototype.buttonClick = function (args, control) {
            var _this = this;
            var clickedEle = args.item;
            var slider = this.control.rangeSlider;
            var buttons = this.control.periods;
            var button = buttons.filter(function (btn) { return (btn.text === clickedEle.text); });
            var updatedStart;
            var updatedEnd;
            buttons.map(function (period, index) {
                if (period.selected && _this.rootControl.getModuleName() !== 'stockChart') {
                    period.selected = false;
                }
                if (period.text === args.item.text) {
                    _this.selectedIndex = (_this.nodes.childNodes.length - buttons.length) + index;
                    Eif (_this.rootControl.getModuleName() !== 'stockChart') {
                        period.selected = true;
                    }
                }
            });
            if (args.item.text !== '') {
                this.setSelectedStyle(this.selectedIndex);
            }
            if (slider && clickedEle.text) {
                slider.selectedPeriod = clickedEle.text;
            }
            if (clickedEle.text.toLowerCase() === 'all') {
                updatedStart = control.seriesXMin;
                updatedEnd = control.seriesXMax;
                Eif (slider) {
                    slider.performAnimation(updatedStart, updatedEnd, this.control.rangeNavigatorControl);
                }
                else {
                    this.rootControl.rangeChanged(updatedStart, updatedEnd);
                }
            }
            else if (clickedEle.text.toLowerCase() === 'ytd') {
                Eif (slider) {
                    updatedStart = this.isDatetimeCategory ?
                        new Date(new Date(this.sortedData[Math.floor(slider.currentEnd)]).getFullYear().toString()).getTime() :
                        new Date(new Date(slider.currentEnd).getFullYear().toString()).getTime();
                    updatedStart = this.isDatetimeCategory ? this.findStartValue(updatedStart, slider.currentEnd) : updatedStart;
                    updatedEnd = slider.currentEnd;
                    slider.performAnimation(updatedStart, updatedEnd, this.control.rangeNavigatorControl);
                }
                else {
                    updatedStart = this.isDatetimeCategory ? new Date(new Date(this.sortedData[Math.floor(this.rootControl.currentEnd)]).getFullYear().toString()).getTime() :
                        new Date(new Date(this.rootControl.currentEnd).getFullYear().toString()).getTime();
                    updatedStart = this.isDatetimeCategory ? this.findStartValue(updatedStart, this.rootControl.currentEnd) :
                        updatedStart;
                    updatedEnd = this.rootControl.currentEnd;
                    this.rootControl.rangeChanged(updatedStart, updatedEnd);
                }
            }
            else if (clickedEle.text.toLowerCase() !== '') {
                Eif (slider) {
                    updatedStart = this.changedRange(button[0].intervalType, slider.currentEnd, button[0].interval).getTime();
                    updatedStart = this.isDatetimeCategory ? this.findStartValue(updatedStart, slider.currentEnd) : updatedStart;
                    updatedEnd = slider.currentEnd;
                    slider.performAnimation(updatedStart, updatedEnd, this.control.rangeNavigatorControl);
                }
                else {
                    updatedStart = this.changedRange(button[0].intervalType, this.rootControl.currentEnd, button[0].interval).getTime();
                    updatedStart = this.isDatetimeCategory ? this.findStartValue(updatedStart, this.rootControl.currentEnd) :
                        updatedStart;
                    updatedEnd = this.rootControl.currentEnd;
                    this.rootControl.rangeChanged(updatedStart, updatedEnd);
                }
            }
            if (this.rootControl.getModuleName() === 'stockChart') {
                this.rootControl.zoomChange = false;
            }
            if (helper_1.getElement(this.calendarId + '_popup') && !ej2_base_1.Browser.isDevice) {
                var element = helper_1.getElement(this.calendarId + '_popup');
                element.querySelectorAll('.e-range-header')[0].style.display = 'none';
            }
        };
        PeriodSelector.prototype.findStartValue = function (startValue, endValue) {
            for (var index = Math.floor(endValue); index >= 0; index--) {
                Iif (this.sortedData[index] <= startValue) {
                    return (index + 1);
                }
            }
            return 0;
        };
        PeriodSelector.prototype.changedRange = function (type, end, interval) {
            var result = this.isDatetimeCategory ? new Date(this.sortedData[Math.floor(end)]) : new Date(end);
            switch (type) {
                case 'Quarter':
                    result.setMonth(result.getMonth() - (3 * interval));
                    break;
                case 'Months':
                    result.setMonth(result.getMonth() - interval);
                    break;
                case 'Weeks':
                    result.setDate(result.getDate() - (interval * 7));
                    break;
                case 'Days':
                    result.setDate(result.getDate() - interval);
                    break;
                case 'Hours':
                    result.setHours(result.getHours() - interval);
                    break;
                case 'Minutes':
                    result.setMinutes(result.getMinutes() - interval);
                    break;
                case 'Seconds':
                    result.setSeconds(result.getSeconds() - interval);
                    break;
                default:
                    result.setFullYear(result.getFullYear() - interval);
                    break;
            }
            return result;
        };
        PeriodSelector.prototype.getModuleName = function () {
            return 'PeriodSelector';
        };
        PeriodSelector.prototype.destroy = function () {
        };
        return PeriodSelector;
    }());
    exports.PeriodSelector = PeriodSelector;
});