perfect-scrollbar.directive.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /**
  2. * @fileoverview added by tsickle
  3. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  4. */
  5. import PerfectScrollbar from 'perfect-scrollbar';
  6. import ResizeObserver from 'resize-observer-polyfill';
  7. import { Subject, fromEvent } from 'rxjs';
  8. import { auditTime, takeUntil } from 'rxjs/operators';
  9. import { PLATFORM_ID } from '@angular/core';
  10. import { isPlatformBrowser } from '@angular/common';
  11. import { NgZone, Inject, Optional, ElementRef, Directive, Input, Output, EventEmitter, KeyValueDiffers } from '@angular/core';
  12. import { Geometry, Position } from './perfect-scrollbar.interfaces';
  13. import { PERFECT_SCROLLBAR_CONFIG, PerfectScrollbarConfig, PerfectScrollbarEvents } from './perfect-scrollbar.interfaces';
  14. var PerfectScrollbarDirective = /** @class */ (function () {
  15. function PerfectScrollbarDirective(zone, differs, elementRef, platformId, defaults) {
  16. this.zone = zone;
  17. this.differs = differs;
  18. this.elementRef = elementRef;
  19. this.platformId = platformId;
  20. this.defaults = defaults;
  21. this.instance = null;
  22. this.ro = null;
  23. this.timeout = null;
  24. this.animation = null;
  25. this.configDiff = null;
  26. this.ngDestroy = new Subject();
  27. this.disabled = false;
  28. this.psScrollY = new EventEmitter();
  29. this.psScrollX = new EventEmitter();
  30. this.psScrollUp = new EventEmitter();
  31. this.psScrollDown = new EventEmitter();
  32. this.psScrollLeft = new EventEmitter();
  33. this.psScrollRight = new EventEmitter();
  34. this.psYReachEnd = new EventEmitter();
  35. this.psYReachStart = new EventEmitter();
  36. this.psXReachEnd = new EventEmitter();
  37. this.psXReachStart = new EventEmitter();
  38. }
  39. /**
  40. * @return {?}
  41. */
  42. PerfectScrollbarDirective.prototype.ngOnInit = /**
  43. * @return {?}
  44. */
  45. function () {
  46. var _this = this;
  47. if (!this.disabled && isPlatformBrowser(this.platformId)) {
  48. /** @type {?} */
  49. var config_1 = new PerfectScrollbarConfig(this.defaults);
  50. config_1.assign(this.config); // Custom configuration
  51. this.zone.runOutsideAngular((/**
  52. * @return {?}
  53. */
  54. function () {
  55. _this.instance = new PerfectScrollbar(_this.elementRef.nativeElement, config_1);
  56. }));
  57. if (!this.configDiff) {
  58. this.configDiff = this.differs.find(this.config || {}).create();
  59. this.configDiff.diff(this.config || {});
  60. }
  61. this.zone.runOutsideAngular((/**
  62. * @return {?}
  63. */
  64. function () {
  65. _this.ro = new ResizeObserver((/**
  66. * @return {?}
  67. */
  68. function () {
  69. _this.update();
  70. }));
  71. if (_this.elementRef.nativeElement.children[0]) {
  72. _this.ro.observe(_this.elementRef.nativeElement.children[0]);
  73. }
  74. _this.ro.observe(_this.elementRef.nativeElement);
  75. }));
  76. this.zone.runOutsideAngular((/**
  77. * @return {?}
  78. */
  79. function () {
  80. PerfectScrollbarEvents.forEach((/**
  81. * @param {?} eventName
  82. * @return {?}
  83. */
  84. function (eventName) {
  85. /** @type {?} */
  86. var eventType = eventName.replace(/([A-Z])/g, (/**
  87. * @param {?} c
  88. * @return {?}
  89. */
  90. function (c) { return "-" + c.toLowerCase(); }));
  91. fromEvent(_this.elementRef.nativeElement, eventType)
  92. .pipe(auditTime(20), takeUntil(_this.ngDestroy))
  93. .subscribe((/**
  94. * @param {?} event
  95. * @return {?}
  96. */
  97. function (event) {
  98. _this[eventName].emit(event);
  99. }));
  100. }));
  101. }));
  102. }
  103. };
  104. /**
  105. * @return {?}
  106. */
  107. PerfectScrollbarDirective.prototype.ngOnDestroy = /**
  108. * @return {?}
  109. */
  110. function () {
  111. var _this = this;
  112. if (isPlatformBrowser(this.platformId)) {
  113. this.ngDestroy.next();
  114. this.ngDestroy.complete();
  115. if (this.ro) {
  116. this.ro.disconnect();
  117. }
  118. if (this.timeout && typeof window !== 'undefined') {
  119. window.clearTimeout(this.timeout);
  120. }
  121. this.zone.runOutsideAngular((/**
  122. * @return {?}
  123. */
  124. function () {
  125. if (_this.instance) {
  126. _this.instance.destroy();
  127. }
  128. }));
  129. this.instance = null;
  130. }
  131. };
  132. /**
  133. * @return {?}
  134. */
  135. PerfectScrollbarDirective.prototype.ngDoCheck = /**
  136. * @return {?}
  137. */
  138. function () {
  139. if (!this.disabled && this.configDiff && isPlatformBrowser(this.platformId)) {
  140. /** @type {?} */
  141. var changes = this.configDiff.diff(this.config || {});
  142. if (changes) {
  143. this.ngOnDestroy();
  144. this.ngOnInit();
  145. }
  146. }
  147. };
  148. /**
  149. * @param {?} changes
  150. * @return {?}
  151. */
  152. PerfectScrollbarDirective.prototype.ngOnChanges = /**
  153. * @param {?} changes
  154. * @return {?}
  155. */
  156. function (changes) {
  157. if (changes['disabled'] && !changes['disabled'].isFirstChange() && isPlatformBrowser(this.platformId)) {
  158. if (changes['disabled'].currentValue !== changes['disabled'].previousValue) {
  159. if (changes['disabled'].currentValue === true) {
  160. this.ngOnDestroy();
  161. }
  162. else if (changes['disabled'].currentValue === false) {
  163. this.ngOnInit();
  164. }
  165. }
  166. }
  167. };
  168. /**
  169. * @return {?}
  170. */
  171. PerfectScrollbarDirective.prototype.ps = /**
  172. * @return {?}
  173. */
  174. function () {
  175. return this.instance;
  176. };
  177. /**
  178. * @return {?}
  179. */
  180. PerfectScrollbarDirective.prototype.update = /**
  181. * @return {?}
  182. */
  183. function () {
  184. var _this = this;
  185. if (typeof window !== 'undefined') {
  186. if (this.timeout) {
  187. window.clearTimeout(this.timeout);
  188. }
  189. this.timeout = window.setTimeout((/**
  190. * @return {?}
  191. */
  192. function () {
  193. if (!_this.disabled && _this.configDiff) {
  194. try {
  195. _this.zone.runOutsideAngular((/**
  196. * @return {?}
  197. */
  198. function () {
  199. if (_this.instance) {
  200. _this.instance.update();
  201. }
  202. }));
  203. }
  204. catch (error) {
  205. // Update can be finished after destroy so catch errors
  206. }
  207. }
  208. }), 0);
  209. }
  210. };
  211. /**
  212. * @param {?=} prefix
  213. * @return {?}
  214. */
  215. PerfectScrollbarDirective.prototype.geometry = /**
  216. * @param {?=} prefix
  217. * @return {?}
  218. */
  219. function (prefix) {
  220. if (prefix === void 0) { prefix = 'scroll'; }
  221. return new Geometry(this.elementRef.nativeElement[prefix + 'Left'], this.elementRef.nativeElement[prefix + 'Top'], this.elementRef.nativeElement[prefix + 'Width'], this.elementRef.nativeElement[prefix + 'Height']);
  222. };
  223. /**
  224. * @param {?=} absolute
  225. * @return {?}
  226. */
  227. PerfectScrollbarDirective.prototype.position = /**
  228. * @param {?=} absolute
  229. * @return {?}
  230. */
  231. function (absolute) {
  232. if (absolute === void 0) { absolute = false; }
  233. if (!absolute && this.instance) {
  234. return new Position(this.instance.reach.x || 0, this.instance.reach.y || 0);
  235. }
  236. else {
  237. return new Position(this.elementRef.nativeElement.scrollLeft, this.elementRef.nativeElement.scrollTop);
  238. }
  239. };
  240. /**
  241. * @param {?=} direction
  242. * @return {?}
  243. */
  244. PerfectScrollbarDirective.prototype.scrollable = /**
  245. * @param {?=} direction
  246. * @return {?}
  247. */
  248. function (direction) {
  249. if (direction === void 0) { direction = 'any'; }
  250. /** @type {?} */
  251. var element = this.elementRef.nativeElement;
  252. if (direction === 'any') {
  253. return element.classList.contains('ps--active-x') ||
  254. element.classList.contains('ps--active-y');
  255. }
  256. else if (direction === 'both') {
  257. return element.classList.contains('ps--active-x') &&
  258. element.classList.contains('ps--active-y');
  259. }
  260. else {
  261. return element.classList.contains('ps--active-' + direction);
  262. }
  263. };
  264. /**
  265. * @param {?} x
  266. * @param {?=} y
  267. * @param {?=} speed
  268. * @return {?}
  269. */
  270. PerfectScrollbarDirective.prototype.scrollTo = /**
  271. * @param {?} x
  272. * @param {?=} y
  273. * @param {?=} speed
  274. * @return {?}
  275. */
  276. function (x, y, speed) {
  277. if (!this.disabled) {
  278. if (y == null && speed == null) {
  279. this.animateScrolling('scrollTop', x, speed);
  280. }
  281. else {
  282. if (x != null) {
  283. this.animateScrolling('scrollLeft', x, speed);
  284. }
  285. if (y != null) {
  286. this.animateScrolling('scrollTop', y, speed);
  287. }
  288. }
  289. }
  290. };
  291. /**
  292. * @param {?} x
  293. * @param {?=} speed
  294. * @return {?}
  295. */
  296. PerfectScrollbarDirective.prototype.scrollToX = /**
  297. * @param {?} x
  298. * @param {?=} speed
  299. * @return {?}
  300. */
  301. function (x, speed) {
  302. this.animateScrolling('scrollLeft', x, speed);
  303. };
  304. /**
  305. * @param {?} y
  306. * @param {?=} speed
  307. * @return {?}
  308. */
  309. PerfectScrollbarDirective.prototype.scrollToY = /**
  310. * @param {?} y
  311. * @param {?=} speed
  312. * @return {?}
  313. */
  314. function (y, speed) {
  315. this.animateScrolling('scrollTop', y, speed);
  316. };
  317. /**
  318. * @param {?=} offset
  319. * @param {?=} speed
  320. * @return {?}
  321. */
  322. PerfectScrollbarDirective.prototype.scrollToTop = /**
  323. * @param {?=} offset
  324. * @param {?=} speed
  325. * @return {?}
  326. */
  327. function (offset, speed) {
  328. this.animateScrolling('scrollTop', (offset || 0), speed);
  329. };
  330. /**
  331. * @param {?=} offset
  332. * @param {?=} speed
  333. * @return {?}
  334. */
  335. PerfectScrollbarDirective.prototype.scrollToLeft = /**
  336. * @param {?=} offset
  337. * @param {?=} speed
  338. * @return {?}
  339. */
  340. function (offset, speed) {
  341. this.animateScrolling('scrollLeft', (offset || 0), speed);
  342. };
  343. /**
  344. * @param {?=} offset
  345. * @param {?=} speed
  346. * @return {?}
  347. */
  348. PerfectScrollbarDirective.prototype.scrollToRight = /**
  349. * @param {?=} offset
  350. * @param {?=} speed
  351. * @return {?}
  352. */
  353. function (offset, speed) {
  354. /** @type {?} */
  355. var left = this.elementRef.nativeElement.scrollWidth -
  356. this.elementRef.nativeElement.clientWidth;
  357. this.animateScrolling('scrollLeft', left - (offset || 0), speed);
  358. };
  359. /**
  360. * @param {?=} offset
  361. * @param {?=} speed
  362. * @return {?}
  363. */
  364. PerfectScrollbarDirective.prototype.scrollToBottom = /**
  365. * @param {?=} offset
  366. * @param {?=} speed
  367. * @return {?}
  368. */
  369. function (offset, speed) {
  370. /** @type {?} */
  371. var top = this.elementRef.nativeElement.scrollHeight -
  372. this.elementRef.nativeElement.clientHeight;
  373. this.animateScrolling('scrollTop', top - (offset || 0), speed);
  374. };
  375. /**
  376. * @param {?} qs
  377. * @param {?=} offset
  378. * @param {?=} speed
  379. * @return {?}
  380. */
  381. PerfectScrollbarDirective.prototype.scrollToElement = /**
  382. * @param {?} qs
  383. * @param {?=} offset
  384. * @param {?=} speed
  385. * @return {?}
  386. */
  387. function (qs, offset, speed) {
  388. /** @type {?} */
  389. var element = this.elementRef.nativeElement.querySelector(qs);
  390. if (element) {
  391. /** @type {?} */
  392. var elementPos = element.getBoundingClientRect();
  393. /** @type {?} */
  394. var scrollerPos = this.elementRef.nativeElement.getBoundingClientRect();
  395. if (this.elementRef.nativeElement.classList.contains('ps--active-x')) {
  396. /** @type {?} */
  397. var currentPos = this.elementRef.nativeElement['scrollLeft'];
  398. /** @type {?} */
  399. var position = elementPos.left - scrollerPos.left + currentPos;
  400. this.animateScrolling('scrollLeft', position + (offset || 0), speed);
  401. }
  402. if (this.elementRef.nativeElement.classList.contains('ps--active-y')) {
  403. /** @type {?} */
  404. var currentPos = this.elementRef.nativeElement['scrollTop'];
  405. /** @type {?} */
  406. var position = elementPos.top - scrollerPos.top + currentPos;
  407. this.animateScrolling('scrollTop', position + (offset || 0), speed);
  408. }
  409. }
  410. };
  411. /**
  412. * @private
  413. * @param {?} target
  414. * @param {?} value
  415. * @param {?=} speed
  416. * @return {?}
  417. */
  418. PerfectScrollbarDirective.prototype.animateScrolling = /**
  419. * @private
  420. * @param {?} target
  421. * @param {?} value
  422. * @param {?=} speed
  423. * @return {?}
  424. */
  425. function (target, value, speed) {
  426. var _this = this;
  427. if (this.animation) {
  428. window.cancelAnimationFrame(this.animation);
  429. this.animation = null;
  430. }
  431. if (!speed || typeof window === 'undefined') {
  432. this.elementRef.nativeElement[target] = value;
  433. }
  434. else if (value !== this.elementRef.nativeElement[target]) {
  435. /** @type {?} */
  436. var newValue_1 = 0;
  437. /** @type {?} */
  438. var scrollCount_1 = 0;
  439. /** @type {?} */
  440. var oldTimestamp_1 = performance.now();
  441. /** @type {?} */
  442. var oldValue_1 = this.elementRef.nativeElement[target];
  443. /** @type {?} */
  444. var cosParameter_1 = (oldValue_1 - value) / 2;
  445. /** @type {?} */
  446. var step_1 = (/**
  447. * @param {?} newTimestamp
  448. * @return {?}
  449. */
  450. function (newTimestamp) {
  451. scrollCount_1 += Math.PI / (speed / (newTimestamp - oldTimestamp_1));
  452. newValue_1 = Math.round(value + cosParameter_1 + cosParameter_1 * Math.cos(scrollCount_1));
  453. // Only continue animation if scroll position has not changed
  454. if (_this.elementRef.nativeElement[target] === oldValue_1) {
  455. if (scrollCount_1 >= Math.PI) {
  456. _this.animateScrolling(target, value, 0);
  457. }
  458. else {
  459. _this.elementRef.nativeElement[target] = newValue_1;
  460. // On a zoomed out page the resulting offset may differ
  461. oldValue_1 = _this.elementRef.nativeElement[target];
  462. oldTimestamp_1 = newTimestamp;
  463. _this.animation = window.requestAnimationFrame(step_1);
  464. }
  465. }
  466. });
  467. window.requestAnimationFrame(step_1);
  468. }
  469. };
  470. PerfectScrollbarDirective.decorators = [
  471. { type: Directive, args: [{
  472. selector: '[perfectScrollbar]',
  473. exportAs: 'ngxPerfectScrollbar'
  474. },] }
  475. ];
  476. /** @nocollapse */
  477. PerfectScrollbarDirective.ctorParameters = function () { return [
  478. { type: NgZone },
  479. { type: KeyValueDiffers },
  480. { type: ElementRef },
  481. { type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] },
  482. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [PERFECT_SCROLLBAR_CONFIG,] }] }
  483. ]; };
  484. PerfectScrollbarDirective.propDecorators = {
  485. disabled: [{ type: Input }],
  486. config: [{ type: Input, args: ['perfectScrollbar',] }],
  487. psScrollY: [{ type: Output }],
  488. psScrollX: [{ type: Output }],
  489. psScrollUp: [{ type: Output }],
  490. psScrollDown: [{ type: Output }],
  491. psScrollLeft: [{ type: Output }],
  492. psScrollRight: [{ type: Output }],
  493. psYReachEnd: [{ type: Output }],
  494. psYReachStart: [{ type: Output }],
  495. psXReachEnd: [{ type: Output }],
  496. psXReachStart: [{ type: Output }]
  497. };
  498. return PerfectScrollbarDirective;
  499. }());
  500. export { PerfectScrollbarDirective };
  501. if (false) {
  502. /**
  503. * @type {?}
  504. * @private
  505. */
  506. PerfectScrollbarDirective.prototype.instance;
  507. /**
  508. * @type {?}
  509. * @private
  510. */
  511. PerfectScrollbarDirective.prototype.ro;
  512. /**
  513. * @type {?}
  514. * @private
  515. */
  516. PerfectScrollbarDirective.prototype.timeout;
  517. /**
  518. * @type {?}
  519. * @private
  520. */
  521. PerfectScrollbarDirective.prototype.animation;
  522. /**
  523. * @type {?}
  524. * @private
  525. */
  526. PerfectScrollbarDirective.prototype.configDiff;
  527. /**
  528. * @type {?}
  529. * @private
  530. */
  531. PerfectScrollbarDirective.prototype.ngDestroy;
  532. /** @type {?} */
  533. PerfectScrollbarDirective.prototype.disabled;
  534. /** @type {?} */
  535. PerfectScrollbarDirective.prototype.config;
  536. /** @type {?} */
  537. PerfectScrollbarDirective.prototype.psScrollY;
  538. /** @type {?} */
  539. PerfectScrollbarDirective.prototype.psScrollX;
  540. /** @type {?} */
  541. PerfectScrollbarDirective.prototype.psScrollUp;
  542. /** @type {?} */
  543. PerfectScrollbarDirective.prototype.psScrollDown;
  544. /** @type {?} */
  545. PerfectScrollbarDirective.prototype.psScrollLeft;
  546. /** @type {?} */
  547. PerfectScrollbarDirective.prototype.psScrollRight;
  548. /** @type {?} */
  549. PerfectScrollbarDirective.prototype.psYReachEnd;
  550. /** @type {?} */
  551. PerfectScrollbarDirective.prototype.psYReachStart;
  552. /** @type {?} */
  553. PerfectScrollbarDirective.prototype.psXReachEnd;
  554. /** @type {?} */
  555. PerfectScrollbarDirective.prototype.psXReachStart;
  556. /**
  557. * @type {?}
  558. * @private
  559. */
  560. PerfectScrollbarDirective.prototype.zone;
  561. /**
  562. * @type {?}
  563. * @private
  564. */
  565. PerfectScrollbarDirective.prototype.differs;
  566. /** @type {?} */
  567. PerfectScrollbarDirective.prototype.elementRef;
  568. /**
  569. * @type {?}
  570. * @private
  571. */
  572. PerfectScrollbarDirective.prototype.platformId;
  573. /**
  574. * @type {?}
  575. * @private
  576. */
  577. PerfectScrollbarDirective.prototype.defaults;
  578. }
  579. //# sourceMappingURL=perfect-scrollbar.directive.js.map