scrollpanel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. 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;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var domhandler_1 = require("../dom/domhandler");
  15. var ScrollPanel = /** @class */ (function () {
  16. function ScrollPanel(el, zone) {
  17. this.el = el;
  18. this.zone = zone;
  19. this.timeoutFrame = function (fn) { return setTimeout(fn, 0); };
  20. }
  21. ScrollPanel.prototype.ngAfterViewInit = function () {
  22. var _this = this;
  23. this.zone.runOutsideAngular(function () {
  24. _this.moveBar();
  25. _this.moveBar = _this.moveBar.bind(_this);
  26. _this.onXBarMouseDown = _this.onXBarMouseDown.bind(_this);
  27. _this.onYBarMouseDown = _this.onYBarMouseDown.bind(_this);
  28. _this.onDocumentMouseMove = _this.onDocumentMouseMove.bind(_this);
  29. _this.onDocumentMouseUp = _this.onDocumentMouseUp.bind(_this);
  30. window.addEventListener('resize', _this.moveBar);
  31. _this.contentViewChild.nativeElement.addEventListener('scroll', _this.moveBar);
  32. _this.contentViewChild.nativeElement.addEventListener('mouseenter', _this.moveBar);
  33. _this.xBarViewChild.nativeElement.addEventListener('mousedown', _this.onXBarMouseDown);
  34. _this.yBarViewChild.nativeElement.addEventListener('mousedown', _this.onYBarMouseDown);
  35. _this.calculateContainerHeight();
  36. _this.initialized = true;
  37. });
  38. };
  39. ScrollPanel.prototype.calculateContainerHeight = function () {
  40. var container = this.containerViewChild.nativeElement;
  41. var content = this.contentViewChild.nativeElement;
  42. var xBar = this.xBarViewChild.nativeElement;
  43. var containerStyles = getComputedStyle(container), xBarStyles = getComputedStyle(xBar), pureContainerHeight = domhandler_1.DomHandler.getHeight(container) - parseInt(xBarStyles['height'], 10);
  44. if (containerStyles['max-height'] != "none" && pureContainerHeight == 0) {
  45. if (content.offsetHeight + parseInt(xBarStyles['height'], 10) > parseInt(containerStyles['max-height'], 10)) {
  46. container.style.height = containerStyles['max-height'];
  47. }
  48. else {
  49. container.style.height = content.offsetHeight + parseFloat(containerStyles.paddingTop) + parseFloat(containerStyles.paddingBottom) + parseFloat(containerStyles.borderTopWidth) + parseFloat(containerStyles.borderBottomWidth) + "px";
  50. }
  51. }
  52. };
  53. ScrollPanel.prototype.moveBar = function () {
  54. var _this = this;
  55. var container = this.containerViewChild.nativeElement;
  56. var content = this.contentViewChild.nativeElement;
  57. /* horizontal scroll */
  58. var xBar = this.xBarViewChild.nativeElement;
  59. var totalWidth = content.scrollWidth;
  60. var ownWidth = content.clientWidth;
  61. var bottom = (container.clientHeight - xBar.clientHeight) * -1;
  62. this.scrollXRatio = ownWidth / totalWidth;
  63. /* vertical scroll */
  64. var yBar = this.yBarViewChild.nativeElement;
  65. var totalHeight = content.scrollHeight;
  66. var ownHeight = content.clientHeight;
  67. var right = (container.clientWidth - yBar.clientWidth) * -1;
  68. this.scrollYRatio = ownHeight / totalHeight;
  69. this.requestAnimationFrame(function () {
  70. if (_this.scrollXRatio >= 1) {
  71. domhandler_1.DomHandler.addClass(xBar, 'ui-scrollpanel-hidden');
  72. }
  73. else {
  74. domhandler_1.DomHandler.removeClass(xBar, 'ui-scrollpanel-hidden');
  75. xBar.style.cssText = 'width:' + Math.max(_this.scrollXRatio * 100, 10) + '%; left:' + (content.scrollLeft / totalWidth) * 100 + '%;bottom:' + bottom + 'px;';
  76. }
  77. if (_this.scrollYRatio >= 1) {
  78. domhandler_1.DomHandler.addClass(yBar, 'ui-scrollpanel-hidden');
  79. }
  80. else {
  81. domhandler_1.DomHandler.removeClass(yBar, 'ui-scrollpanel-hidden');
  82. yBar.style.cssText = 'height:' + Math.max(_this.scrollYRatio * 100, 10) + '%; top: calc(' + (content.scrollTop / totalHeight) * 100 + '% - ' + xBar.clientHeight + 'px);right:' + right + 'px;';
  83. }
  84. });
  85. };
  86. ScrollPanel.prototype.onYBarMouseDown = function (e) {
  87. this.isYBarClicked = true;
  88. this.lastPageY = e.pageY;
  89. domhandler_1.DomHandler.addClass(this.yBarViewChild.nativeElement, 'ui-scrollpanel-grabbed');
  90. domhandler_1.DomHandler.addClass(document.body, 'ui-scrollpanel-grabbed');
  91. document.addEventListener('mousemove', this.onDocumentMouseMove);
  92. document.addEventListener('mouseup', this.onDocumentMouseUp);
  93. e.preventDefault();
  94. };
  95. ScrollPanel.prototype.onXBarMouseDown = function (e) {
  96. this.isXBarClicked = true;
  97. this.lastPageX = e.pageX;
  98. domhandler_1.DomHandler.addClass(this.xBarViewChild.nativeElement, 'ui-scrollpanel-grabbed');
  99. domhandler_1.DomHandler.addClass(document.body, 'ui-scrollpanel-grabbed');
  100. document.addEventListener('mousemove', this.onDocumentMouseMove);
  101. document.addEventListener('mouseup', this.onDocumentMouseUp);
  102. e.preventDefault();
  103. };
  104. ScrollPanel.prototype.onDocumentMouseMove = function (e) {
  105. if (this.isXBarClicked) {
  106. this.onMouseMoveForXBar(e);
  107. }
  108. else if (this.isYBarClicked) {
  109. this.onMouseMoveForYBar(e);
  110. }
  111. else {
  112. this.onMouseMoveForXBar(e);
  113. this.onMouseMoveForYBar(e);
  114. }
  115. };
  116. ScrollPanel.prototype.onMouseMoveForXBar = function (e) {
  117. var _this = this;
  118. var deltaX = e.pageX - this.lastPageX;
  119. this.lastPageX = e.pageX;
  120. this.requestAnimationFrame(function () {
  121. _this.contentViewChild.nativeElement.scrollLeft += deltaX / _this.scrollXRatio;
  122. });
  123. };
  124. ScrollPanel.prototype.onMouseMoveForYBar = function (e) {
  125. var _this = this;
  126. var deltaY = e.pageY - this.lastPageY;
  127. this.lastPageY = e.pageY;
  128. this.requestAnimationFrame(function () {
  129. _this.contentViewChild.nativeElement.scrollTop += deltaY / _this.scrollYRatio;
  130. });
  131. };
  132. ScrollPanel.prototype.scrollTop = function (scrollTop) {
  133. var scrollableHeight = this.contentViewChild.nativeElement.scrollHeight - this.contentViewChild.nativeElement.clientHeight;
  134. scrollTop = scrollTop > scrollableHeight ? scrollableHeight : scrollTop > 0 ? scrollTop : 0;
  135. this.contentViewChild.nativeElement.scrollTop = scrollTop;
  136. };
  137. ScrollPanel.prototype.onDocumentMouseUp = function (e) {
  138. domhandler_1.DomHandler.removeClass(this.yBarViewChild.nativeElement, 'ui-scrollpanel-grabbed');
  139. domhandler_1.DomHandler.removeClass(this.xBarViewChild.nativeElement, 'ui-scrollpanel-grabbed');
  140. domhandler_1.DomHandler.removeClass(document.body, 'ui-scrollpanel-grabbed');
  141. document.removeEventListener('mousemove', this.onDocumentMouseMove);
  142. document.removeEventListener('mouseup', this.onDocumentMouseUp);
  143. this.isXBarClicked = false;
  144. this.isYBarClicked = false;
  145. };
  146. ScrollPanel.prototype.requestAnimationFrame = function (f) {
  147. var frame = window.requestAnimationFrame || this.timeoutFrame;
  148. frame(f);
  149. };
  150. ScrollPanel.prototype.ngOnDestroy = function () {
  151. if (this.initialized) {
  152. window.removeEventListener('resize', this.moveBar);
  153. this.contentViewChild.nativeElement.removeEventListener('scroll', this.moveBar);
  154. this.contentViewChild.nativeElement.removeEventListener('mouseenter', this.moveBar);
  155. this.xBarViewChild.nativeElement.removeEventListener('mousedown', this.onXBarMouseDown);
  156. this.yBarViewChild.nativeElement.removeEventListener('mousedown', this.onYBarMouseDown);
  157. }
  158. };
  159. ScrollPanel.prototype.refresh = function () {
  160. this.moveBar();
  161. };
  162. __decorate([
  163. core_1.Input(),
  164. __metadata("design:type", Object)
  165. ], ScrollPanel.prototype, "style", void 0);
  166. __decorate([
  167. core_1.Input(),
  168. __metadata("design:type", String)
  169. ], ScrollPanel.prototype, "styleClass", void 0);
  170. __decorate([
  171. core_1.ViewChild('container', { static: false }),
  172. __metadata("design:type", core_1.ElementRef)
  173. ], ScrollPanel.prototype, "containerViewChild", void 0);
  174. __decorate([
  175. core_1.ViewChild('content', { static: false }),
  176. __metadata("design:type", core_1.ElementRef)
  177. ], ScrollPanel.prototype, "contentViewChild", void 0);
  178. __decorate([
  179. core_1.ViewChild('xBar', { static: false }),
  180. __metadata("design:type", core_1.ElementRef)
  181. ], ScrollPanel.prototype, "xBarViewChild", void 0);
  182. __decorate([
  183. core_1.ViewChild('yBar', { static: false }),
  184. __metadata("design:type", core_1.ElementRef)
  185. ], ScrollPanel.prototype, "yBarViewChild", void 0);
  186. ScrollPanel = __decorate([
  187. core_1.Component({
  188. selector: 'p-scrollPanel',
  189. template: "\n <div #container [ngClass]=\"'ui-scrollpanel ui-widget ui-widget-content ui-corner-all'\" [ngStyle]=\"style\" [class]=\"styleClass\">\n <div class=\"ui-scrollpanel-wrapper\">\n <div #content class=\"ui-scrollpanel-content\">\n <ng-content></ng-content>\n </div>\n </div>\n <div #xBar class=\"ui-scrollpanel-bar ui-scrollpanel-bar-x\"></div>\n <div #yBar class=\"ui-scrollpanel-bar ui-scrollpanel-bar-y\"></div> \n </div>\n "
  190. }),
  191. __metadata("design:paramtypes", [core_1.ElementRef, core_1.NgZone])
  192. ], ScrollPanel);
  193. return ScrollPanel;
  194. }());
  195. exports.ScrollPanel = ScrollPanel;
  196. var ScrollPanelModule = /** @class */ (function () {
  197. function ScrollPanelModule() {
  198. }
  199. ScrollPanelModule = __decorate([
  200. core_1.NgModule({
  201. imports: [common_1.CommonModule],
  202. exports: [ScrollPanel],
  203. declarations: [ScrollPanel]
  204. })
  205. ], ScrollPanelModule);
  206. return ScrollPanelModule;
  207. }());
  208. exports.ScrollPanelModule = ScrollPanelModule;
  209. //# sourceMappingURL=scrollpanel.js.map