all files / common/ h-scroll.js

99.38% Statements 320/322
97.33% Branches 146/150
100% Functions 46/46
99.37% Lines 314/316
18 statements, 7 functions, 29 branches Ignored     
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              186×   186× 186× 186× 186× 186× 186× 186× 186× 154× 154×   186× 186× 10×     186× 186× 186× 178×       186×   209× 176× 176×     33×     186× 186× 186× 186× 186× 186× 665× 665×   186× 186× 186× 186× 186×     731×   171× 171× 171× 171× 171× 171× 171× 171× 16×   171× 714× 714×   171× 151×   171× 171× 171× 171× 163×     171× 171× 171× 171×                 178× 178× 178× 178× 178× 178× 178× 178× 178× 178× 178× 178× 178× 178× 178× 178× 27× 27×   178×                       186× 186× 364× 364× 364× 364× 364× 364×   364×     23×   5627×   5627×     11693×   5656× 5656× 5656× 25×   5656×   5656× 5654× 16×     5638×                   22×   24× 24× 24×     22×   24× 24× 48× 48× 24×     24×   15×   24× 24×     24×   5990× 121×     5869×   5990×     5656× 5656× 5656× 5634× 5634×   22× 13×     22× 378× 378× 378× 18× 18×     360× 360×   378× 22×     356× 356× 356×     22×   16× 16× 16×   16×       21× 12× 12× 12×           21×   40× 40× 40× 40× 40× 40× 40× 40× 40× 23×   40× 26×   26× 22×       26×     22×     40× 14×   26×     19× 19×       23× 25× 25×   23× 23×                      
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/* istanbul ignore next */ 
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1, ej2_base_2, ej2_base_3) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var CLS_ROOT = 'e-hscroll';
    var CLS_RTL = 'e-rtl';
    var CLS_DISABLE = 'e-overlay';
    var CLS_HSCROLLBAR = 'e-hscroll-bar';
    var CLS_HSCROLLCON = 'e-hscroll-content';
    var CLS_NAVARROW = 'e-nav-arrow';
    var CLS_NAVRIGHTARROW = 'e-nav-right-arrow';
    var CLS_NAVLEFTARROW = 'e-nav-left-arrow';
    var CLS_HSCROLLNAV = 'e-scroll-nav';
    var CLS_HSCROLLNAVRIGHT = 'e-scroll-right-nav';
    var CLS_HSCROLLNAVLEFT = 'e-scroll-left-nav';
    var CLS_DEVICE = 'e-scroll-device';
    var CLS_OVERLAY = 'e-scroll-overlay';
    var CLS_RIGHTOVERLAY = 'e-scroll-right-overlay';
    var CLS_LEFTOVERLAY = 'e-scroll-left-overlay';
    var OVERLAY_MAXWID = 40;
    var HScroll = (function (_super) {
        __extends(HScroll, _super);
        function HScroll(options, element) {
            return _super.call(this, options, element) || this;
        }
        HScroll.prototype.preRender = function () {
            this.browser = ej2_base_2.Browser.info.name;
            this.browserCheck = this.browser === 'mozilla';
            this.isDevice = ej2_base_2.Browser.isDevice;
            this.customStep = true;
            var element = this.element;
            this.ieCheck = this.browser === 'edge' || this.browser === 'msie';
            this.initialize();
            if (element.id === '') {
                element.id = ej2_base_1.getUniqueID('hscroll');
                this.uniqueId = true;
            }
            element.style.display = 'block';
            if (this.enableRtl) {
                element.classList.add(CLS_RTL);
            }
        };
        HScroll.prototype.render = function () {
            this.touchModule = new ej2_base_1.Touch(this.element, { scroll: this.touchHandler.bind(this), swipe: this.swipeHandler.bind(this) });
            ej2_base_1.EventHandler.add(this.scrollEle, 'scroll', this.scrollHandler, this);
            if (!this.isDevice) {
                this.createNavIcon(this.element);
            }
            else {
                this.element.classList.add(CLS_DEVICE);
                this.createOverlay(this.element);
            }
            this.setScrollState();
        };
        HScroll.prototype.setScrollState = function () {
            if (ej2_base_3.isNullOrUndefined(this.scrollStep) || this.scrollStep < 0) {
                this.scrollStep = this.scrollEle.offsetWidth;
                this.customStep = false;
            }
            else {
                this.customStep = true;
            }
        };
        HScroll.prototype.initialize = function () {
            var scrollEle = this.createElement('div', { className: CLS_HSCROLLCON });
            var scrollDiv = this.createElement('div', { className: CLS_HSCROLLBAR });
            scrollDiv.setAttribute('tabindex', '-1');
            var ele = this.element;
            var innerEle = [].slice.call(ele.children);
            for (var _i = 0, innerEle_1 = innerEle; _i < innerEle_1.length; _i++) {
                var ele_1 = innerEle_1[_i];
                scrollEle.appendChild(ele_1);
            }
            scrollDiv.appendChild(scrollEle);
            ele.appendChild(scrollDiv);
            scrollDiv.style.overflowX = 'hidden';
            this.scrollEle = scrollDiv;
            this.scrollItems = scrollEle;
        };
        HScroll.prototype.getPersistData = function () {
            var keyEntity = ['scrollStep'];
            return this.addOnPersist(keyEntity);
        };
        HScroll.prototype.getModuleName = function () {
            return 'hScroll';
        };
        HScroll.prototype.destroy = function () {
            var ele = this.element;
            ele.style.display = '';
            ele.classList.remove(CLS_ROOT);
            ele.classList.remove(CLS_DEVICE);
            ele.classList.remove(CLS_RTL);
            var nav = ej2_base_1.selectAll('.e-' + ele.id + '_nav.' + CLS_HSCROLLNAV, ele);
            var overlay = ej2_base_1.selectAll('.' + CLS_OVERLAY, ele);
            [].slice.call(overlay).forEach(function (ele) {
                ej2_base_2.detach(ele);
            });
            for (var _i = 0, _a = [].slice.call(this.scrollItems.children); _i < _a.length; _i++) {
                var elem = _a[_i];
                ele.appendChild(elem);
            }
            if (this.uniqueId) {
                this.element.removeAttribute('id');
            }
            ej2_base_2.detach(this.scrollEle);
            Eif (nav.length > 0) {
                ej2_base_2.detach(nav[0]);
                if (!ej2_base_3.isNullOrUndefined(nav[1])) {
                    ej2_base_2.detach(nav[1]);
                }
            }
            ej2_base_1.EventHandler.remove(this.scrollEle, 'scroll', this.scrollHandler);
            this.touchModule.destroy();
            this.touchModule = null;
            _super.prototype.destroy.call(this);
        };
        HScroll.prototype.disable = function (value) {
            var navEles = ej2_base_1.selectAll('.e-scroll-nav:not(.' + CLS_DISABLE + ')', this.element);
            if (value) {
                this.element.classList.add(CLS_DISABLE);
            }
            else {
                this.element.classList.remove(CLS_DISABLE);
            }
            [].slice.call(navEles).forEach(function (el) {
                el.setAttribute('tabindex', !value ? '0' : '-1');
            });
        };
        HScroll.prototype.createOverlay = function (element) {
            var id = element.id.concat('_nav');
            var rightOverlayEle = this.createElement('div', { className: CLS_OVERLAY + ' ' + CLS_RIGHTOVERLAY });
            var clsRight = 'e-' + element.id.concat('_nav ' + CLS_HSCROLLNAV + ' ' + CLS_HSCROLLNAVRIGHT);
            var rightEle = this.createElement('div', { id: id.concat('_right'), className: clsRight });
            var navItem = this.createElement('div', { className: CLS_NAVRIGHTARROW + ' ' + CLS_NAVARROW + ' e-icons' });
            rightEle.appendChild(navItem);
            var leftEle = this.createElement('div', { className: CLS_OVERLAY + ' ' + CLS_LEFTOVERLAY });
            if (this.ieCheck) {
                rightEle.classList.add('e-ie-align');
            }
            element.appendChild(rightOverlayEle);
            element.appendChild(rightEle);
            element.insertBefore(leftEle, element.firstChild);
            this.eventBinding([rightEle]);
        };
        HScroll.prototype.createNavIcon = function (element) {
            var id = element.id.concat('_nav');
            var clsRight = 'e-' + element.id.concat('_nav ' + CLS_HSCROLLNAV + ' ' + CLS_HSCROLLNAVRIGHT);
            var rightAttributes = { 'role': 'button', 'id': id.concat('_right'), 'aria-label': 'Scroll right' };
            var nav = this.createElement('div', { className: clsRight, attrs: rightAttributes });
            nav.setAttribute('aria-disabled', 'false');
            var navItem = this.createElement('div', { className: CLS_NAVRIGHTARROW + ' ' + CLS_NAVARROW + ' e-icons' });
            var clsLeft = 'e-' + element.id.concat('_nav ' + CLS_HSCROLLNAV + ' ' + CLS_HSCROLLNAVLEFT);
            var leftAttributes = { 'role': 'button', 'id': id.concat('_left'), 'aria-label': 'Scroll left' };
            var navEle = this.createElement('div', { className: clsLeft + ' ' + CLS_DISABLE, attrs: leftAttributes });
            navEle.setAttribute('aria-disabled', 'true');
            var navLeftItem = this.createElement('div', { className: CLS_NAVLEFTARROW + ' ' + CLS_NAVARROW + ' e-icons' });
            navEle.appendChild(navLeftItem);
            nav.appendChild(navItem);
            element.appendChild(nav);
            element.insertBefore(navEle, element.firstChild);
            if (this.ieCheck) {
                nav.classList.add('e-ie-align');
                navEle.classList.add('e-ie-align');
            }
            this.eventBinding([nav, navEle]);
        };
        HScroll.prototype.onKeyPress = function (e) {
            var _this = this;
            if (e.key === 'Enter') {
                var timeoutFun_1 = function () {
                    _this.keyTimeout = true;
                    _this.eleScrolling(10, e.target, true);
                };
                this.keyTimer = window.setTimeout(function () {
                    timeoutFun_1();
                }, 100);
            }
        };
        HScroll.prototype.onKeyUp = function (e) {
            if (e.key !== 'Enter') {
                return;
            }
            Eif (this.keyTimeout) {
                this.keyTimeout = false;
            }
            else {
                e.target.click();
            }
            clearTimeout(this.keyTimer);
        };
        HScroll.prototype.eventBinding = function (ele) {
            var _this = this;
            [].slice.call(ele).forEach(function (el) {
                new ej2_base_1.Touch(el, { tapHold: _this.tabHoldHandler.bind(_this), tapHoldThreshold: 500 });
                el.addEventListener('keydown', _this.onKeyPress.bind(_this));
                el.addEventListener('keyup', _this.onKeyUp.bind(_this));
                el.addEventListener('mouseup', _this.repeatScroll.bind(_this));
                el.addEventListener('touchend', _this.repeatScroll.bind(_this));
                el.addEventListener('contextmenu', function (e) {
                    e.preventDefault();
                });
                ej2_base_1.EventHandler.add(el, 'click', _this.clickEventHandler, _this);
            });
        };
        HScroll.prototype.repeatScroll = function () {
            clearInterval(this.timeout);
        };
        HScroll.prototype.tabHoldHandler = function (e) {
            var _this = this;
            var trgt = e.originalEvent.target;
            trgt = this.contains(trgt, CLS_HSCROLLNAV) ? trgt.firstElementChild : trgt;
            var scrollDis = 10;
            var timeoutFun = function () {
                _this.eleScrolling(scrollDis, trgt, true);
            };
            this.timeout = window.setInterval(function () {
                timeoutFun();
            }, 50);
        };
        HScroll.prototype.contains = function (ele, className) {
            return ele.classList.contains(className);
        };
        HScroll.prototype.eleScrolling = function (scrollDis, trgt, isContinuous) {
            var rootEle = this.element;
            var classList = trgt.classList;
            if (classList.contains(CLS_HSCROLLNAV)) {
                classList = trgt.querySelector('.' + CLS_NAVARROW).classList;
            }
            if (this.contains(rootEle, CLS_RTL) && this.browserCheck) {
                scrollDis = -scrollDis;
            }
            if ((!this.contains(rootEle, CLS_RTL) || this.browserCheck) || this.ieCheck) {
                if (classList.contains(CLS_NAVRIGHTARROW)) {
                    this.frameScrollRequest(scrollDis, 'add', isContinuous);
                }
                else {
                    this.frameScrollRequest(scrollDis, '', isContinuous);
                }
            }
            else {
                Eif (classList.contains(CLS_NAVLEFTARROW)) {
                    this.frameScrollRequest(scrollDis, 'add', isContinuous);
                }
                else {
                    this.frameScrollRequest(scrollDis, '', isContinuous);
                }
            }
        };
        HScroll.prototype.clickEventHandler = function (e) {
            this.eleScrolling(this.scrollStep, e.target, false);
        };
        HScroll.prototype.swipeHandler = function (e) {
            var swipeEle = this.scrollEle;
            var distance;
            if (e.velocity <= 1) {
                distance = e.distanceX / (e.velocity * 10);
            }
            else {
                distance = e.distanceX / e.velocity;
            }
            var start = 0.5;
            var animate = function () {
                var step = Math.sin(start);
                if (step <= 0) {
                    window.cancelAnimationFrame(step);
                }
                else {
                    if (e.swipeDirection === 'Left') {
                        swipeEle.scrollLeft += distance * step;
                    }
                    else if (e.swipeDirection === 'Right') {
                        swipeEle.scrollLeft -= distance * step;
                    }
                    start -= 0.5;
                    window.requestAnimationFrame(animate);
                }
            };
            animate();
        };
        HScroll.prototype.scrollUpdating = function (scrollVal, action) {
            if (action === 'add') {
                this.scrollEle.scrollLeft += scrollVal;
            }
            else {
                this.scrollEle.scrollLeft -= scrollVal;
            }
            if (this.enableRtl && this.scrollEle.scrollLeft > 0) {
                this.scrollEle.scrollLeft = 0;
            }
        };
        HScroll.prototype.frameScrollRequest = function (scrollVal, action, isContinuous) {
            var _this = this;
            var step = 10;
            if (isContinuous) {
                this.scrollUpdating(scrollVal, action);
                return;
            }
            if (!this.customStep) {
                [].slice.call(ej2_base_1.selectAll('.' + CLS_OVERLAY, this.element)).forEach(function (el) {
                    scrollVal -= el.offsetWidth;
                });
            }
            var animate = function () {
                var scrollValue;
                var scrollStep;
                if (_this.contains(_this.element, CLS_RTL) && _this.browserCheck) {
                    scrollValue = -scrollVal;
                    scrollStep = -step;
                }
                else {
                    scrollValue = scrollVal;
                    scrollStep = step;
                }
                if (scrollValue < step) {
                    window.cancelAnimationFrame(scrollStep);
                }
                else {
                    _this.scrollUpdating(scrollStep, action);
                    scrollVal -= scrollStep;
                    window.requestAnimationFrame(animate);
                }
            };
            animate();
        };
        HScroll.prototype.touchHandler = function (e) {
            var ele = this.scrollEle;
            var distance = e.distanceX;
            if ((this.ieCheck) && this.contains(this.element, CLS_RTL)) {
                distance = -distance;
            }
            if (e.scrollDirection === 'Left') {
                ele.scrollLeft = ele.scrollLeft + distance;
            }
            else if (e.scrollDirection === 'Right') {
                ele.scrollLeft = ele.scrollLeft - distance;
            }
        };
        HScroll.prototype.arrowDisabling = function (addDisable, removeDisable) {
            if (this.isDevice) {
                var arrowEle = ej2_base_3.isNullOrUndefined(addDisable) ? removeDisable : addDisable;
                var arrowIcon = arrowEle.querySelector('.' + CLS_NAVARROW);
                if (ej2_base_3.isNullOrUndefined(addDisable)) {
                    ej2_base_3.classList(arrowIcon, [CLS_NAVRIGHTARROW], [CLS_NAVLEFTARROW]);
                }
                else {
                    ej2_base_3.classList(arrowIcon, [CLS_NAVLEFTARROW], [CLS_NAVRIGHTARROW]);
                }
            }
            else if (addDisable && removeDisable) {
                addDisable.classList.add(CLS_DISABLE);
                addDisable.setAttribute('aria-disabled', 'true');
                addDisable.removeAttribute('tabindex');
                removeDisable.classList.remove(CLS_DISABLE);
                removeDisable.setAttribute('aria-disabled', 'false');
                removeDisable.setAttribute('tabindex', '0');
            }
            this.repeatScroll();
        };
        HScroll.prototype.scrollHandler = function (e) {
            var target = e.target;
            var width = target.offsetWidth;
            var rootEle = this.element;
            var navLeftEle = this.element.querySelector('.' + CLS_HSCROLLNAVLEFT);
            var navRightEle = this.element.querySelector('.' + CLS_HSCROLLNAVRIGHT);
            var leftOverlay = this.element.querySelector('.' + CLS_LEFTOVERLAY);
            var rightOverlay = this.element.querySelector('.' + CLS_RIGHTOVERLAY);
            var scrollLeft = target.scrollLeft;
            if (scrollLeft <= 0) {
                scrollLeft = -scrollLeft;
            }
            if (this.isDevice) {
                if (this.enableRtl && !(this.browserCheck || this.ieCheck)) {
                    leftOverlay = this.element.querySelector('.' + CLS_RIGHTOVERLAY);
                    rightOverlay = this.element.querySelector('.' + CLS_LEFTOVERLAY);
                }
                if (scrollLeft < OVERLAY_MAXWID) {
                    leftOverlay.style.width = scrollLeft + 'px';
                }
                else {
                    leftOverlay.style.width = '40px';
                }
                if ((target.scrollWidth - Math.ceil(width + scrollLeft)) < OVERLAY_MAXWID) {
                    rightOverlay.style.width = (target.scrollWidth - Math.ceil(width + scrollLeft)) + 'px';
                }
                else {
                    rightOverlay.style.width = '40px';
                }
            }
            if (scrollLeft === 0) {
                this.arrowDisabling(navLeftEle, navRightEle);
            }
            else if (Math.ceil(width + scrollLeft + .1) >= target.scrollWidth) {
                this.arrowDisabling(navRightEle, navLeftEle);
            }
            else {
                var disEle = this.element.querySelector('.' + CLS_HSCROLLNAV + '.' + CLS_DISABLE);
                if (disEle) {
                    disEle.classList.remove(CLS_DISABLE);
                    disEle.setAttribute('aria-disabled', 'false');
                    disEle.setAttribute('tabindex', '0');
                }
            }
        };
        HScroll.prototype.onPropertyChanged = function (newProp, oldProp) {
            for (var _i = 0, _a = Object.keys(newProp); _i < _a.length; _i++) {
                var prop = _a[_i];
                switch (prop) {
                    case 'scrollStep':
                        this.setScrollState();
                        break;
                    case 'enableRtl':
                        newProp.enableRtl ? this.element.classList.add(CLS_RTL) : this.element.classList.remove(CLS_RTL);
                        break;
                }
            }
        };
        __decorate([
            ej2_base_2.Property(null)
        ], HScroll.prototype, "scrollStep", void 0);
        HScroll = __decorate([
            ej2_base_2.NotifyPropertyChanges
        ], HScroll);
        return HScroll;
    }(ej2_base_1.Component));
    exports.HScroll = HScroll;
});