Scrollable.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * Scrollable.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * This mixin makes controls scrollable using custom scrollbars.
  12. *
  13. * @-x-less Scrollable.less
  14. * @mixin tinymce.ui.Scrollable
  15. */
  16. define("tinymce/ui/Scrollable", [
  17. "tinymce/ui/DomUtils",
  18. "tinymce/ui/DragHelper"
  19. ], function(DomUtils, DragHelper) {
  20. "use strict";
  21. return {
  22. init: function() {
  23. var self = this;
  24. self.on('repaint', self.renderScroll);
  25. },
  26. renderScroll: function() {
  27. var self = this, margin = 2;
  28. function repaintScroll() {
  29. var hasScrollH, hasScrollV, bodyElm;
  30. function repaintAxis(axisName, posName, sizeName, contentSizeName, hasScroll, ax) {
  31. var containerElm, scrollBarElm, scrollThumbElm;
  32. var containerSize, scrollSize, ratio, rect;
  33. var posNameLower, sizeNameLower;
  34. scrollBarElm = self.getEl('scroll' + axisName);
  35. if (scrollBarElm) {
  36. posNameLower = posName.toLowerCase();
  37. sizeNameLower = sizeName.toLowerCase();
  38. if (self.getEl('absend')) {
  39. DomUtils.css(self.getEl('absend'), posNameLower, self.layoutRect()[contentSizeName] - 1);
  40. }
  41. if (!hasScroll) {
  42. DomUtils.css(scrollBarElm, 'display', 'none');
  43. return;
  44. }
  45. DomUtils.css(scrollBarElm, 'display', 'block');
  46. containerElm = self.getEl('body');
  47. scrollThumbElm = self.getEl('scroll' + axisName + "t");
  48. containerSize = containerElm["client" + sizeName] - (margin * 2);
  49. containerSize -= hasScrollH && hasScrollV ? scrollBarElm["client" + ax] : 0;
  50. scrollSize = containerElm["scroll" + sizeName];
  51. ratio = containerSize / scrollSize;
  52. rect = {};
  53. rect[posNameLower] = containerElm["offset" + posName] + margin;
  54. rect[sizeNameLower] = containerSize;
  55. DomUtils.css(scrollBarElm, rect);
  56. rect = {};
  57. rect[posNameLower] = containerElm["scroll" + posName] * ratio;
  58. rect[sizeNameLower] = containerSize * ratio;
  59. DomUtils.css(scrollThumbElm, rect);
  60. }
  61. }
  62. bodyElm = self.getEl('body');
  63. hasScrollH = bodyElm.scrollWidth > bodyElm.clientWidth;
  64. hasScrollV = bodyElm.scrollHeight > bodyElm.clientHeight;
  65. repaintAxis("h", "Left", "Width", "contentW", hasScrollH, "Height");
  66. repaintAxis("v", "Top", "Height", "contentH", hasScrollV, "Width");
  67. }
  68. function addScroll() {
  69. function addScrollAxis(axisName, posName, sizeName, deltaPosName, ax) {
  70. var scrollStart, axisId = self._id + '-scroll' + axisName, prefix = self.classPrefix;
  71. self.getEl().appendChild(DomUtils.createFragment(
  72. '<div id="' + axisId + '" class="' + prefix + 'scrollbar ' + prefix + 'scrollbar-' + axisName + '">' +
  73. '<div id="' + axisId + 't" class="' + prefix + 'scrollbar-thumb"></div>' +
  74. '</div>'
  75. ));
  76. self.draghelper = new DragHelper(axisId + 't', {
  77. start: function() {
  78. scrollStart = self.getEl('body')["scroll" + posName];
  79. DomUtils.addClass(DomUtils.get(axisId), prefix + 'active');
  80. },
  81. drag: function(e) {
  82. var ratio, hasScrollH, hasScrollV, containerSize, layoutRect = self.layoutRect();
  83. hasScrollH = layoutRect.contentW > layoutRect.innerW;
  84. hasScrollV = layoutRect.contentH > layoutRect.innerH;
  85. containerSize = self.getEl('body')["client" + sizeName] - (margin * 2);
  86. containerSize -= hasScrollH && hasScrollV ? self.getEl('scroll' + axisName)["client" + ax] : 0;
  87. ratio = containerSize / self.getEl('body')["scroll" + sizeName];
  88. self.getEl('body')["scroll" + posName] = scrollStart + (e["delta" + deltaPosName] / ratio);
  89. },
  90. stop: function() {
  91. DomUtils.removeClass(DomUtils.get(axisId), prefix + 'active');
  92. }
  93. });
  94. /*
  95. self.on('click', function(e) {
  96. if (e.target.id == self._id + '-scrollv') {
  97. }
  98. });*/
  99. }
  100. self.addClass('scroll');
  101. addScrollAxis("v", "Top", "Height", "Y", "Width");
  102. addScrollAxis("h", "Left", "Width", "X", "Height");
  103. }
  104. if (self.settings.autoScroll) {
  105. if (!self._hasScroll) {
  106. self._hasScroll = true;
  107. addScroll();
  108. self.on('wheel', function(e) {
  109. var bodyEl = self.getEl('body');
  110. bodyEl.scrollLeft += (e.deltaX || 0) * 10;
  111. bodyEl.scrollTop += e.deltaY * 10;
  112. repaintScroll();
  113. });
  114. DomUtils.on(self.getEl('body'), "scroll", repaintScroll);
  115. }
  116. repaintScroll();
  117. }
  118. }
  119. };
  120. });