@enact/sandstone
Version:
Large-screen/TV support library for Enact, containing a variety of UI components.
244 lines (238 loc) • 9.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FadeAndSlideArranger = exports.CrossFadeArranger = exports.BasicArranger = void 0;
var _ViewManager = require("@enact/ui/ViewManager");
var _excluded = ["duration", "reverse"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var quadInOut = 'cubic-bezier(0.455, 0.030, 0.515, 0.955)';
var animationOptions = {
easing: quadInOut
};
// Batches animations together so that all views start/end at the same time
var callbacks = [];
var idle = function idle(callback) {
callbacks.push(callback);
if (callbacks.length === 1) {
var idleCallback = function idleCallback() {
callbacks.forEach(function (fn) {
return fn();
});
callbacks.length = 0;
};
if (typeof window !== 'undefined' && typeof window.requestIdleCallback === 'function') {
window.requestIdleCallback(idleCallback, {
timeout: 400
});
} else {
setTimeout(idleCallback);
}
}
};
var AnimateOnIdle = /*#__PURE__*/function () {
function AnimateOnIdle(node, keyframes, _ref) {
var _this = this;
var duration = _ref.duration,
reverse = _ref.reverse,
options = _objectWithoutProperties(_ref, _excluded);
_classCallCheck(this, AnimateOnIdle);
this.animation = null;
// used to "fill" when the animation completes
this.keyframes = [keyframes[reverse ? keyframes.length - 1 : 0], keyframes[reverse ? 0 : keyframes.length - 1]];
this._onfinish = null;
this._oncancel = null;
this._reverse = false;
this._playState = 'idle';
this.fill(node, this.keyframes[0]);
this.handleFinish = function () {
_this._playState = 'finished';
_this.fill(node, _this.keyframes[1]);
if (_this._onfinish) _this._onfinish();
};
this.handleCancel = function () {
_this._playState = 'finished';
_this.fill(node, _this.keyframes[0]);
if (_this._oncancel) _this._oncancel();
};
idle(function () {
// if the animation was finished/cancelled before the idle callback occurs, bail out
if (_this._playState === 'finished') return;
_this.animation = node.animate(keyframes, _objectSpread({
duration: duration,
direction: reverse ? 'reverse' : 'normal',
fill: 'none'
}, options));
_this.animation.onfinish = _this.handleFinish;
_this.animation.oncancel = _this.handleCancel;
if (_this._reverse) {
_this.animation.reverse();
}
});
}
_createClass(AnimateOnIdle, [{
key: "fill",
value: function fill(node, keyframe) {
// NOTE: this is naive atm to only address transform. We can extend it later.
node.style.transform = keyframe.transform;
}
}, {
key: "playState",
get: function get() {
return this.animation ? this.animation.playState : this._playState;
}
}, {
key: "onfinish",
set: function set(value) {
this._onfinish = value;
}
}, {
key: "oncancel",
set: function set(value) {
this._oncancel = value;
}
}, {
key: "finish",
value: function finish() {
if (this.animation) {
this.animation.finish();
} else {
this.handleFinish();
}
}
}, {
key: "cancel",
value: function cancel() {
if (this.animation) {
this.animation.cancel();
} else {
this.handleCancel();
}
}
}, {
key: "reverse",
value: function reverse() {
// swap the first/last keyframe so we fill the correct frame
this.keyframes.reverse();
if (this.animation) {
this.animation.reverse();
} else {
// do we need to account for re-reversing? seems unlikely but a possibility perhaps
this._reverse = true;
}
}
}]);
return AnimateOnIdle;
}();
var deferArrange = function deferArrange(config, keyframes, options) {
var node = config.node,
duration = config.duration,
reverse = config.reverse;
return new AnimateOnIdle(node, keyframes, _objectSpread({
duration: duration,
reverse: reverse
}, options));
};
/**
* Arranger that slides panels in from the right and out to the left.
*
* @type {Arranger}
* @private
*/
var BasicArranger = exports.BasicArranger = {
enter: function enter(config) {
return deferArrange(config, [{
transform: "translateX(".concat(config.rtl ? '-' : '', "100%)"),
offset: 0
}, {
transform: 'none',
offset: 1
}], animationOptions);
},
leave: function leave(config) {
return deferArrange(config, [{
transform: 'none',
offset: 0
}, {
transform: "translateX(".concat(config.rtl ? '' : '-', "100%)"),
offset: 1
}], animationOptions);
}
};
/**
* Arranger that slides panels in from the right and out to the left.
*
* @type {Arranger}
* @private
*/
var FadeAndSlideArranger = exports.FadeAndSlideArranger = {
enter: function enter(config) {
return (0, _ViewManager.arrange)(config, [{
transform: "translateX(".concat(config.rtl ? '-' : '', "100%)"),
opacity: 0,
offset: 0
}, {
opacity: 0,
offset: 0.5
}, {
transform: 'none',
opacity: 1,
offset: 1
}], animationOptions);
},
leave: function leave(config) {
return (0, _ViewManager.arrange)(config, [{
transform: 'none',
opacity: 1,
offset: 0
}, {
opacity: 0,
offset: 0.5
}, {
transform: "translateX(".concat(config.rtl ? '' : '-', "100%)"),
opacity: 0,
offset: 1
}], animationOptions);
}
};
/**
* Arranger that cross fade between panels.
*
* @type {Arranger}
* @private
*/
var CrossFadeArranger = exports.CrossFadeArranger = {
enter: function enter(config) {
return (0, _ViewManager.arrange)(config, [{
opacity: 0,
offset: 0
}, {
opacity: 0,
offset: 0.5
}, {
opacity: 1,
offset: 1
}], animationOptions);
},
leave: function leave(config) {
return (0, _ViewManager.arrange)(config, [{
opacity: 1,
offset: 0
}, {
opacity: 0,
offset: 0.5
}, {
opacity: 0,
offset: 1
}], animationOptions);
}
};