Nessuna descrizione

ios-swipe-animator.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import _Object$keys from 'babel-runtime/core-js/object/keys';
  2. import _WeakMap from 'babel-runtime/core-js/weak-map';
  3. import _extends from 'babel-runtime/helpers/extends';
  4. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  5. import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
  6. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  7. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  8. import _get from 'babel-runtime/helpers/get';
  9. import _createClass from 'babel-runtime/helpers/createClass';
  10. import _inherits from 'babel-runtime/helpers/inherits';
  11. /*
  12. Copyright 2013-2015 ASIAL CORPORATION
  13. Licensed under the Apache License, Version 2.0 (the "License");
  14. you may not use this file except in compliance with the License.
  15. You may obtain a copy of the License at
  16. http://www.apache.org/licenses/LICENSE-2.0
  17. Unless required by applicable law or agreed to in writing, software
  18. distributed under the License is distributed on an "AS IS" BASIS,
  19. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. See the License for the specific language governing permissions and
  21. limitations under the License.
  22. */
  23. import NavigatorAnimator from './animator';
  24. import util from '../../ons/util';
  25. import animit from '../../ons/animit';
  26. /**
  27. * Abstract swipe animator for iOS navigator transition.
  28. */
  29. var IOSSwipeNavigatorAnimator = function (_NavigatorAnimator) {
  30. _inherits(IOSSwipeNavigatorAnimator, _NavigatorAnimator);
  31. _createClass(IOSSwipeNavigatorAnimator, null, [{
  32. key: 'swipeable',
  33. get: function get() {
  34. return true;
  35. }
  36. }]);
  37. function IOSSwipeNavigatorAnimator() {
  38. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  39. var _ref$durationRestore = _ref.durationRestore,
  40. durationRestore = _ref$durationRestore === undefined ? 0.1 : _ref$durationRestore,
  41. _ref$durationSwipe = _ref.durationSwipe,
  42. durationSwipe = _ref$durationSwipe === undefined ? 0.15 : _ref$durationSwipe,
  43. _ref$timingSwipe = _ref.timingSwipe,
  44. timingSwipe = _ref$timingSwipe === undefined ? 'linear' : _ref$timingSwipe,
  45. rest = _objectWithoutProperties(_ref, ['durationRestore', 'durationSwipe', 'timingSwipe']);
  46. _classCallCheck(this, IOSSwipeNavigatorAnimator);
  47. var _this = _possibleConstructorReturn(this, (IOSSwipeNavigatorAnimator.__proto__ || _Object$getPrototypeOf(IOSSwipeNavigatorAnimator)).call(this, _extends({}, rest)));
  48. if (_this.constructor === IOSSwipeNavigatorAnimator) {
  49. util.throwAbstract();
  50. }
  51. _this.durationRestore = durationRestore;
  52. _this.durationSwipe = durationSwipe;
  53. _this.timingSwipe = timingSwipe;
  54. _this.optSwipe = { timing: timingSwipe, duration: durationSwipe };
  55. _this.optRestore = { timing: timingSwipe, duration: durationRestore };
  56. _this.swipeShadow = util.createElement('<div style="position: absolute; height: 100%; width: 12px; right: 100%; top: 0; bottom: 0; z-index: -1;' + 'background: linear-gradient(to right, transparent 0, rgba(0,0,0,.04) 40%, rgba(0,0,0,.12) 80%, rgba(0,0,0,.16) 100%);"></div>');
  57. _this.isDragStart = true;
  58. return _this;
  59. }
  60. _createClass(IOSSwipeNavigatorAnimator, [{
  61. key: '_decompose',
  62. value: function _decompose() {
  63. util.throwMember();
  64. }
  65. }, {
  66. key: '_shouldAnimateToolbar',
  67. value: function _shouldAnimateToolbar() {
  68. util.throwMember();
  69. }
  70. }, {
  71. key: '_calculateDelta',
  72. value: function _calculateDelta() {
  73. util.throwMember();
  74. }
  75. }, {
  76. key: '_dragStartSetup',
  77. value: function _dragStartSetup(enterPage, leavePage) {
  78. this.isDragStart = false;
  79. // Avoid content clicks
  80. this.unblock = _get(IOSSwipeNavigatorAnimator.prototype.__proto__ || _Object$getPrototypeOf(IOSSwipeNavigatorAnimator.prototype), 'block', this).call(this, leavePage);
  81. // Mask
  82. enterPage.parentElement.insertBefore(this.backgroundMask, enterPage);
  83. // Decomposition
  84. this.target = {
  85. enter: util.findToolbarPage(enterPage) || enterPage,
  86. leave: util.findToolbarPage(leavePage) || leavePage
  87. };
  88. this.decomp = {
  89. enter: this._decompose(this.target.enter),
  90. leave: this._decompose(this.target.leave)
  91. };
  92. // Animation values
  93. this.delta = this._calculateDelta(leavePage, this.decomp.leave);
  94. this.shouldAnimateToolbar = this._shouldAnimateToolbar(this.target.enter, this.target.leave);
  95. // Shadow && styles
  96. if (this.shouldAnimateToolbar) {
  97. this.swipeShadow.style.top = this.decomp.leave.toolbar.offsetHeight + 'px';
  98. this.target.leave.appendChild(this.swipeShadow);
  99. this._saveStyle(this.target.enter, this.target.leave);
  100. } else {
  101. leavePage.appendChild(this.swipeShadow);
  102. this._saveStyle(enterPage, leavePage);
  103. }
  104. leavePage.classList.add('overflow-visible');
  105. this.overflowElement = leavePage;
  106. this.decomp.leave.content.classList.add('content-swiping');
  107. }
  108. }, {
  109. key: 'translate',
  110. value: function translate(distance, maxWidth, enterPage, leavePage) {
  111. this.isSwiping = true;
  112. if (enterPage.style.display === 'none') {
  113. enterPage.style.display = '';
  114. }
  115. if (this.isDragStart) {
  116. this.maxWidth = maxWidth;
  117. this._dragStartSetup(enterPage, leavePage);
  118. }
  119. var swipeRatio = (distance - maxWidth) / maxWidth;
  120. if (this.shouldAnimateToolbar) {
  121. animit.runAll(
  122. /* Enter page */
  123. animit([this.decomp.enter.content, this.decomp.enter.bottomToolbar, this.decomp.enter.background]).queue({
  124. transform: 'translate3d(' + swipeRatio * 25 + '%, 0, 0)',
  125. opacity: 1 + swipeRatio * 10 / 100 // 0.9 -> 1
  126. }), animit(this.decomp.enter.toolbarCenter).queue({
  127. transform: 'translate3d(' + this.delta.title * swipeRatio + 'px, 0, 0)',
  128. opacity: 1 + swipeRatio // 0 -> 1
  129. }), animit(this.decomp.enter.backButtonLabel).queue({
  130. opacity: 1 + swipeRatio * 10 / 100, // 0.9 -> 1
  131. transform: 'translate3d(' + this.delta.label * swipeRatio + 'px, 0, 0)'
  132. }), animit(this.decomp.enter.other).queue({
  133. opacity: 1 + swipeRatio // 0 -> 1
  134. }),
  135. /* Leave page */
  136. animit([this.decomp.leave.content, this.decomp.leave.bottomToolbar, this.decomp.leave.background, this.swipeShadow]).queue({
  137. transform: 'translate3d(' + distance + 'px, 0, 0)'
  138. }), animit(this.decomp.leave.toolbar).queue({
  139. opacity: -1 * swipeRatio // 1 -> 0
  140. }), animit(this.decomp.leave.toolbarCenter).queue({
  141. transform: 'translate3d(' + (1 + swipeRatio) * 125 + '%, 0, 0)'
  142. }), animit(this.decomp.leave.backButtonLabel).queue({
  143. opacity: -1 * swipeRatio, // 1 -> 0
  144. transform: 'translate3d(' + this.delta.title * (1 + swipeRatio) + 'px, 0, 0)'
  145. }),
  146. /* Other */
  147. animit(this.swipeShadow).queue({
  148. opacity: -1 * swipeRatio // 1 -> 0
  149. }));
  150. } else {
  151. animit.runAll(animit(leavePage).queue({
  152. transform: 'translate3d(' + distance + 'px, 0, 0)'
  153. }), animit(enterPage).queue({
  154. transform: 'translate3d(' + swipeRatio * 25 + '%, 0, 0)',
  155. opacity: 1 + swipeRatio * 10 / 100 // 0.9 -> 1
  156. }), animit(this.swipeShadow).queue({
  157. opacity: -1 * swipeRatio // 1 -> 0
  158. }));
  159. }
  160. }
  161. }, {
  162. key: 'restore',
  163. value: function restore(enterPage, leavePage, callback) {
  164. var _this2 = this;
  165. if (this.isDragStart) {
  166. return;
  167. }
  168. if (this.shouldAnimateToolbar) {
  169. animit.runAll(
  170. /* Enter page */
  171. animit([this.decomp.enter.content, this.decomp.enter.bottomToolbar, this.decomp.enter.background]).queue({
  172. transform: 'translate3d(-25%, 0, 0)',
  173. opacity: 0.9
  174. }, this.optRestore), animit(this.decomp.enter.toolbarCenter).queue({
  175. transform: 'translate3d(-' + this.delta.title + 'px, 0, 0)',
  176. transition: 'opacity ' + this.durationRestore + 's linear, transform ' + this.durationRestore + 's ' + this.timingSwipe,
  177. opacity: 0
  178. }), animit(this.decomp.enter.backButtonLabel).queue({
  179. transform: 'translate3d(-' + this.delta.label + 'px, 0, 0)'
  180. }, this.optRestore), animit(this.decomp.enter.other).queue({
  181. opacity: 0
  182. }, this.optRestore),
  183. /* Leave page */
  184. animit([this.decomp.leave.content, this.decomp.leave.bottomToolbar, this.decomp.leave.background, this.swipeShadow]).queue({
  185. transform: 'translate3d(0, 0, 0)'
  186. }, this.optRestore), animit(this.decomp.leave.toolbar).queue({
  187. opacity: 1
  188. }, this.optRestore), animit(this.decomp.leave.toolbarCenter).queue({
  189. transform: 'translate3d(0, 0, 0)'
  190. }, this.optRestore), animit(this.decomp.leave.backButtonLabel).queue({
  191. opacity: 1,
  192. transform: 'translate3d(0, 0, 0)',
  193. transition: 'opacity ' + this.durationRestore + 's linear, transform ' + this.durationRestore + 's ' + this.timingSwipe
  194. }),
  195. /* Other */
  196. animit(this.swipeShadow).queue({
  197. opacity: 0
  198. }, this.optRestore).queue(function (done) {
  199. _this2._reset(_this2.target.enter, _this2.target.leave);
  200. enterPage.style.display = 'none';
  201. callback && callback();
  202. done();
  203. }));
  204. } else {
  205. animit.runAll(animit(enterPage).queue({
  206. transform: 'translate3D(-25%, 0, 0)',
  207. opacity: 0.9
  208. }, this.optRestore), animit(leavePage).queue({
  209. transform: 'translate3D(0, 0, 0)'
  210. }, this.optRestore).queue(function (done) {
  211. _this2._reset(enterPage, leavePage);
  212. enterPage.style.display = 'none';
  213. callback && callback();
  214. done();
  215. }));
  216. }
  217. }
  218. }, {
  219. key: 'popSwipe',
  220. value: function popSwipe(enterPage, leavePage, callback) {
  221. var _this3 = this;
  222. if (this.isDragStart) {
  223. return;
  224. }
  225. if (this.shouldAnimateToolbar) {
  226. animit.runAll(
  227. /* Enter page */
  228. animit([this.decomp.enter.content, this.decomp.enter.bottomToolbar, this.decomp.enter.background]).queue({
  229. transform: 'translate3d(0, 0, 0)',
  230. opacity: 1
  231. }, this.optSwipe), animit(this.decomp.enter.toolbarCenter).queue({
  232. transform: 'translate3d(0, 0, 0)',
  233. transition: 'opacity ' + this.durationSwipe + 's linear, transform ' + this.durationSwipe + 's ' + this.timingSwipe,
  234. opacity: 1
  235. }), animit(this.decomp.enter.backButtonLabel).queue({
  236. transform: 'translate3d(0, 0, 0)'
  237. }, this.optSwipe), animit(this.decomp.enter.other).queue({
  238. opacity: 1
  239. }, this.optSwipe),
  240. /* Leave page */
  241. animit([this.decomp.leave.content, this.decomp.leave.bottomToolbar, this.decomp.leave.background]).queue({
  242. transform: 'translate3d(100%, 0, 0)'
  243. }, this.optSwipe), animit(this.decomp.leave.toolbar).queue({
  244. opacity: 0
  245. }, this.optSwipe), animit(this.decomp.leave.toolbarCenter).queue({
  246. transform: 'translate3d(125%, 0, 0)'
  247. }, this.optSwipe), animit(this.decomp.leave.backButtonLabel).queue({
  248. opacity: 0,
  249. transform: 'translate3d(' + this.delta.title + 'px, 0, 0)',
  250. transition: 'opacity ' + this.durationSwipe + 's linear, transform ' + this.durationSwipe + 's ' + this.timingSwipe
  251. }),
  252. /* Other */
  253. animit(this.swipeShadow).queue({
  254. opacity: 0,
  255. transform: 'translate3d(' + this.maxWidth + 'px, 0, 0)'
  256. }, this.optSwipe).queue(function (done) {
  257. _this3._reset(_this3.target.enter, _this3.target.leave);
  258. callback && callback();
  259. done();
  260. }));
  261. } else {
  262. animit.runAll(animit(enterPage).queue({
  263. transform: 'translate3D(0, 0, 0)',
  264. opacity: 1.0
  265. }, this.optSwipe), animit(leavePage).queue({
  266. transform: 'translate3D(100%, 0, 0)'
  267. }, this.optSwipe).queue(function (done) {
  268. _this3._reset(enterPage, leavePage);
  269. callback && callback();
  270. done();
  271. }));
  272. }
  273. }
  274. }, {
  275. key: '_saveStyle',
  276. value: function _saveStyle() {
  277. var _this4 = this;
  278. this._savedStyle = new _WeakMap();
  279. var save = function save(el) {
  280. return _this4._savedStyle.set(el, el.getAttribute('style'));
  281. };
  282. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  283. args[_key] = arguments[_key];
  284. }
  285. args.forEach(save);
  286. _Object$keys(this.decomp).forEach(function (p) {
  287. _Object$keys(_this4.decomp[p]).forEach(function (k) {
  288. (_this4.decomp[p][k] instanceof Array ? _this4.decomp[p][k] : [_this4.decomp[p][k]]).forEach(save);
  289. });
  290. });
  291. }
  292. }, {
  293. key: '_restoreStyle',
  294. value: function _restoreStyle() {
  295. var _this5 = this;
  296. var restore = function restore(el) {
  297. _this5._savedStyle.get(el) === null ? el.removeAttribute('style') : el.setAttribute('style', _this5._savedStyle.get(el));
  298. _this5._savedStyle.delete(el);
  299. };
  300. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  301. args[_key2] = arguments[_key2];
  302. }
  303. args.forEach(restore);
  304. _Object$keys(this.decomp).forEach(function (p) {
  305. _Object$keys(_this5.decomp[p]).forEach(function (k) {
  306. (_this5.decomp[p][k] instanceof Array ? _this5.decomp[p][k] : [_this5.decomp[p][k]]).forEach(restore);
  307. });
  308. });
  309. }
  310. }, {
  311. key: '_reset',
  312. value: function _reset() {
  313. this.isSwiping = false;
  314. this._savedStyle && this._restoreStyle.apply(this, arguments);
  315. this.unblock && this.unblock();
  316. this.swipeShadow.remove();
  317. this.backgroundMask.remove();
  318. this.overflowElement.classList.remove('overflow-visible');
  319. this.decomp.leave.content.classList.remove('content-swiping');
  320. this.decomp = this.target = this.overflowElement = this._savedStyle = null;
  321. this.isDragStart = true;
  322. }
  323. }]);
  324. return IOSSwipeNavigatorAnimator;
  325. }(NavigatorAnimator);
  326. export default IOSSwipeNavigatorAnimator;