spiritjs
Version:
The animation toolkit for the web
301 lines (288 loc) • 10.7 kB
JavaScript
"use strict";
exports.__esModule = true;
exports["default"] = void 0;
var _config = _interopRequireDefault(require("../config/config"));
var _utils = require("../utils");
var _timelines = _interopRequireDefault(require("./timelines"));
var _emitter = require("../utils/emitter");
var _errors = require("../utils/errors");
var _events = require("../utils/events");
var _gsap = require("../utils/gsap");
var _dec, _dec2, _dec3, _dec4, _class;
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : 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); }
function _inheritsLoose(t, o) { t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _applyDecoratedDescriptor(i, e, r, n, l) { var a = {}; return Object.keys(n).forEach(function (i) { a[i] = n[i]; }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = !0), a = r.slice().reverse().reduce(function (r, n) { return n(i, e, r) || r; }, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer && (Object.defineProperty(i, e, a), a = null), a; }
/**
* Group.
*/
var Group = (_dec = (0, _emitter.emitChange)(), _dec2 = (0, _emitter.emitChange)(), _dec3 = (0, _emitter.emitChange)(), _dec4 = (0, _emitter.emitChange)(), (_class = /*#__PURE__*/function (_Emitter) {
/**
* Create a group instance.
*
* @param {object} props
*/
function Group(props) {
var _this;
if (props === void 0) {
props = {};
}
_this = _Emitter.call(this) || this;
_this._name = 'untitled';
_this._timeScale = 1;
_this._timelines = new _timelines["default"]();
_this.timeline = null;
if (!props.name || typeof props.name !== 'string' || props.name.trim() === '') {
throw new Error('Cannot create group without a name.');
}
var defaults = {
name: 'untitled',
timeScale: 1,
timelines: new _timelines["default"]()
};
Object.assign(_this, _objectSpread(_objectSpread({}, defaults), props));
return _this;
}
/**
* Get timelines
*
* @returns {Timelines}
*/
_inheritsLoose(Group, _Emitter);
var _proto = Group.prototype;
/**
* Convert group to object
*
* @returns {object}
*/
_proto.toObject = function toObject() {
var name = this.name;
var timeScale = this.timeScale;
var timelines = this.timelines.toArray();
return {
name: name,
timeScale: timeScale,
timelines: timelines
};
};
_proto.reset = function reset() {
var killed = false;
if (this.timeline) {
killed = true;
_utils.gsap.killTimeline(this.timeline);
// reset styles
this.timelines.each(function (tl) {
if (tl.type === 'dom' && tl.transformObject instanceof window.Element) {
if (tl._style) tl.transformObject.setAttribute('style', tl._style);
if (tl._transform) tl.transformObject.setAttribute('transform', '');
}
});
}
return killed;
}
/**
* Resolve transformObject for timelines
*
* @returns {Group}
*/;
_proto.resolve = function resolve() {
this.reset();
var root = this._list && this._list.rootEl ? this._list.rootEl : null;
if (!root) {
return this;
}
var hasUnresolved = false;
this.timelines.each(function (timeline) {
if (timeline.type === 'dom') {
timeline.transformObject = !root ? null : _utils.resolver.resolveElement(root, timeline);
if (timeline.transformObject) {
timeline.path = _utils.xpath.getExpression(timeline.transformObject, root);
}
if (!hasUnresolved && !timeline.transformObject) {
hasUnresolved = true;
}
}
});
this.emit('resolve', {
resolved: this.resolved,
unresolved: this.unresolved
});
if (hasUnresolved) {
if ((0, _utils.debug)()) {
console.warn("Could not resolve all elements for group " + this.name, this.unresolved);
}
this.emit('unresolve', this.unresolved);
}
return this;
}
/**
* Construct GSAP timeline
*
* @param {boolean} resolve elements
* @returns {gsap.timeline}
*/;
_proto.construct = function construct(resolve) {
var _this2 = this;
if (resolve === void 0) {
resolve = false;
}
try {
if (!_utils.gsap.has()) {
if ((0, _utils.debug)()) {
console.warn("Cannot construct group " + this.name + ". GSAP not found.");
}
throw new Error('GSAP cannot be found');
}
if (resolve) {
this.resolve();
}
if (!this.reset()) {
this.timeline = _config["default"].gsap.instance.timeline({
paused: true
}); // eslint-disable-line new-cap
}
this.resolved.each(function (timeline) {
if (timeline.type === 'dom' && timeline.transformObject instanceof window.Element) {
try {
_this2.timeline.add(_utils.gsap.generateTimeline(timeline).play(), 0, 'start');
} catch (err) {
throw new _errors.TimelineError(err.message, timeline.transformObject, err.stack);
}
}
});
this.timeline.timeScale(this.timeScale);
this._duration = this.timeline.duration();
} catch (err) {
err.message = "Could not construct timeline: " + err.message;
throw err;
}
this.emit('construct', this.timeline);
return this.timeline;
};
return _createClass(Group, [{
key: "timelines",
get: function get() {
return this._timelines;
}
/**
* Get unresolved timelines
*
* @returns {Timelines}
*/,
set:
/**
* Set timelines
*
* @param {Timelines} timelines
*/
function set(timelines) {
if (!(timelines instanceof _timelines["default"])) {
timelines = new _timelines["default"](Array.from(timelines));
}
this._timelines = timelines;
}
/**
* Get current timeScale
*
* @returns {number}
*/
}, {
key: "unresolved",
get: function get() {
var timelines = new _timelines["default"]();
this.timelines.each(function (tl) {
return !tl.transformObject && timelines.add(tl);
});
return timelines;
}
/**
* Get resolved timelines
*
* @returns {Timelines}
*/
}, {
key: "resolved",
get: function get() {
var timelines = new _timelines["default"]();
this.timelines.each(function (tl) {
return !!tl.transformObject && timelines.add(tl);
});
return timelines;
}
}, {
key: "timeScale",
get: function get() {
return this._timeScale;
}
/**
* Set timeScale
*
* @param {number} scale
*/,
set: function set(scale) {
if (!(typeof scale === 'number' && Number.isFinite(scale))) {
throw new Error('timeScale needs to be a number');
}
if ((0, _gsap.isGSAPTimeline)(this.timeline)) {
this.timeline.timeScale(scale);
}
this._timeScale = scale;
}
/**
* Get the timeline duration.
* Equal to this.timeline.duration()
*
* @returns {number}
*/
}, {
key: "duration",
get: function get() {
return this.timeline ? this.timeline.duration() : 0;
}
/**
* Set the timeline duration.
* Updates the group timeScale
*
* @param {number} val
*/,
set: function set(val) {
if ((0, _gsap.isGSAPTimeline)(this.timeline)) {
this.timeline.duration(val);
this.timeScale = this.timeline.timeScale();
this._duration = this.timeline.duration();
}
}
/**
* Get name
*
* @returns {string}
*/
}, {
key: "name",
get: function get() {
return this._name;
}
/**
* Set name
*
* @param {string} name
*/,
set: function set(name) {
if (typeof name !== 'string') {
throw new Error('Name needs to be a string');
}
this._name = name;
}
}]);
}(_events.Emitter), (_applyDecoratedDescriptor(_class.prototype, "timelines", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "timelines"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "timeScale", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "timeScale"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "duration", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "duration"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "name", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "name"), _class.prototype)), _class));
Group.fromObject = function (obj) {
return new Group(obj);
};
var _default = exports["default"] = Group;