No Description

reveal-animator.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _createClass from 'babel-runtime/helpers/createClass';
  4. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  5. import _get from 'babel-runtime/helpers/get';
  6. import _inherits from 'babel-runtime/helpers/inherits';
  7. /*
  8. Copyright 2013-2015 ASIAL CORPORATION
  9. Licensed under the Apache License, Version 2.0 (the "License");
  10. you may not use this file except in compliance with the License.
  11. You may obtain a copy of the License at
  12. http://www.apache.org/licenses/LICENSE-2.0
  13. Unless required by applicable law or agreed to in writing, software
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. */
  19. import contentReady from '../../ons/content-ready';
  20. import styler from '../../ons/styler';
  21. import animit from '../../ons/animit';
  22. import SplitterAnimator from './animator.js';
  23. var RevealSplitterAnimator = function (_SplitterAnimator) {
  24. _inherits(RevealSplitterAnimator, _SplitterAnimator);
  25. function RevealSplitterAnimator() {
  26. _classCallCheck(this, RevealSplitterAnimator);
  27. return _possibleConstructorReturn(this, (RevealSplitterAnimator.__proto__ || _Object$getPrototypeOf(RevealSplitterAnimator)).apply(this, arguments));
  28. }
  29. _createClass(RevealSplitterAnimator, [{
  30. key: '_getSlidingElements',
  31. value: function _getSlidingElements() {
  32. var slidingElements = [this._content, this._mask];
  33. if (this._oppositeSide && this._oppositeSide.mode === 'split') {
  34. slidingElements.push(this._oppositeSide);
  35. }
  36. return slidingElements;
  37. }
  38. }, {
  39. key: 'activate',
  40. value: function activate(sideElement) {
  41. _get(RevealSplitterAnimator.prototype.__proto__ || _Object$getPrototypeOf(RevealSplitterAnimator.prototype), 'activate', this).call(this, sideElement);
  42. if (sideElement.mode === 'collapse') {
  43. this._setStyles(sideElement);
  44. }
  45. }
  46. }, {
  47. key: 'deactivate',
  48. value: function deactivate() {
  49. this._side && this._unsetStyles(this._side);
  50. _get(RevealSplitterAnimator.prototype.__proto__ || _Object$getPrototypeOf(RevealSplitterAnimator.prototype), 'deactivate', this).call(this);
  51. }
  52. }, {
  53. key: '_setStyles',
  54. value: function _setStyles(sideElement) {
  55. styler(sideElement, {
  56. left: sideElement.side === 'right' ? 'auto' : 0,
  57. right: sideElement.side === 'right' ? 0 : 'auto',
  58. zIndex: 0,
  59. backgroundColor: 'black',
  60. transform: this._generateBehindPageStyle(0).container.transform,
  61. display: 'none'
  62. });
  63. var splitter = sideElement.parentElement;
  64. contentReady(splitter, function () {
  65. return splitter.content && styler(splitter.content, { boxShadow: '0 0 12px 0 rgba(0, 0, 0, 0.2)' });
  66. });
  67. }
  68. }, {
  69. key: '_unsetStyles',
  70. value: function _unsetStyles(sideElement) {
  71. styler.clear(sideElement, 'left right zIndex backgroundColor display');
  72. if (sideElement._content) {
  73. sideElement._content.style.opacity = '';
  74. }
  75. // Check if the other side needs the common styles
  76. if (!this._oppositeSide || this._oppositeSide.mode === 'split') {
  77. sideElement.parentElement.content && styler.clear(sideElement.parentElement.content, 'boxShadow');
  78. }
  79. }
  80. }, {
  81. key: '_generateBehindPageStyle',
  82. value: function _generateBehindPageStyle(distance) {
  83. var max = this.maxWidth;
  84. var behindDistance = (distance - max) / max * 10;
  85. behindDistance = isNaN(behindDistance) ? 0 : Math.max(Math.min(behindDistance, 0), -10);
  86. var behindTransform = 'translate3d(' + (this.minus ? -1 : 1) * behindDistance + '%, 0, 0)';
  87. var opacity = 1 + behindDistance / 100;
  88. return {
  89. content: {
  90. opacity: opacity
  91. },
  92. container: {
  93. transform: behindTransform
  94. }
  95. };
  96. }
  97. }, {
  98. key: 'translate',
  99. value: function translate(distance) {
  100. this._side.style.display = '';
  101. this._side.style.zIndex = 1;
  102. this.maxWidth = this.maxWidth || this._getMaxWidth();
  103. var menuStyle = this._generateBehindPageStyle(Math.min(distance, this.maxWidth));
  104. if (!this._slidingElements) {
  105. this._slidingElements = this._getSlidingElements();
  106. }
  107. this._mask.style.display = 'block'; // Avoid content clicks
  108. animit.runAll(animit(this._slidingElements).queue({
  109. transform: 'translate3d(' + (this.minus + distance) + 'px, 0, 0)'
  110. }), animit(this._side._content).queue(menuStyle.content), animit(this._side).queue(menuStyle.container));
  111. }
  112. /**
  113. * @param {Function} done
  114. */
  115. }, {
  116. key: 'open',
  117. value: function open(done) {
  118. var _this2 = this;
  119. this._side.style.display = '';
  120. this._side.style.zIndex = 1;
  121. this.maxWidth = this.maxWidth || this._getMaxWidth();
  122. var menuStyle = this._generateBehindPageStyle(this.maxWidth);
  123. this._slidingElements = this._getSlidingElements();
  124. setTimeout(function () {
  125. // Fix: Time to update previous translate3d after changing style.display
  126. animit.runAll(animit(_this2._slidingElements).wait(_this2.delay).queue({
  127. transform: 'translate3d(' + (_this2.minus + _this2.maxWidth) + 'px, 0, 0)'
  128. }, _this2.def), animit(_this2._mask).wait(_this2.delay).queue({
  129. display: 'block'
  130. }), animit(_this2._side._content).wait(_this2.delay).queue(menuStyle.content, _this2.def), animit(_this2._side).wait(_this2.delay).queue(menuStyle.container, _this2.def).queue(function (callback) {
  131. _this2._slidingElements = null;
  132. callback();
  133. done && done();
  134. }));
  135. }, 1000 / 60);
  136. }
  137. /**
  138. * @param {Function} done
  139. */
  140. }, {
  141. key: 'close',
  142. value: function close(done) {
  143. var _this3 = this;
  144. var menuStyle = this._generateBehindPageStyle(0);
  145. this._slidingElements = this._getSlidingElements();
  146. animit.runAll(animit(this._slidingElements).wait(this.delay).queue({
  147. transform: 'translate3d(0, 0, 0)'
  148. }, this.def), animit(this._mask).wait(this.delay).queue({
  149. display: 'none'
  150. }), animit(this._side._content).wait(this.delay).queue(menuStyle.content, this.def), animit(this._side).wait(this.delay).queue(menuStyle.container, this.def).queue(function (callback) {
  151. _this3._slidingElements = null;
  152. _this3._side.style.zIndex = 0;
  153. _this3._side.style.display = 'none';
  154. _this3._side._content.style.opacity = '';
  155. done && done();
  156. callback();
  157. }));
  158. }
  159. }, {
  160. key: '_getMaxWidth',
  161. value: function _getMaxWidth() {
  162. return this._side.offsetWidth;
  163. }
  164. }]);
  165. return RevealSplitterAnimator;
  166. }(SplitterAnimator);
  167. export default RevealSplitterAnimator;