spiritjs
Version:
The animation toolkit for the web
181 lines (180 loc) • 7.93 kB
JavaScript
;
exports.__esModule = true;
exports["default"] = void 0;
var _props = _interopRequireDefault(require("./props"));
var _utils = require("../utils");
var _emitter = require("../utils/emitter");
var _events = require("../utils/events");
var _evalmap = _interopRequireDefault(require("./evalmap"));
var _dec, _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; }
/**
* Timeline.
*/
var Timeline = (_dec = (0, _emitter.emitChange)(), (_class = /*#__PURE__*/function (_Emitter) {
/**
* Create new Timeline instance
*
* @param {string} type default = dom
* @param {HTMLElement|object} transformObject
* @param {Array|Props|object} props
* @param {string|null} path
* @param {string|null} id
* @param {string|null} label
*/
function Timeline(type, transformObject, props, path, id, label) {
var _this;
if (type === void 0) {
type = 'dom';
}
if (transformObject === void 0) {
transformObject = null;
}
if (props === void 0) {
props = new _props["default"]();
}
if (path === void 0) {
path = null;
}
if (id === void 0) {
id = null;
}
if (label === void 0) {
label = null;
}
_this = _Emitter.call(this) || this;
/**
* Timeline type.
* Can be "dom" or "object"
*
* @type {string}
*/
_this.type = 'dom';
/**
* Object to apply transforms on.
* If type is "dom" it refers to a HTMLElement else a plain javascript object
*
* @type {HTMLElement|Object}
*/
_this._transformObject = null;
/**
* Defined label representing this timeline node.
*
* @type {string|null}
*/
_this.label = null;
/**
* XPath of element, normalized by group element.
* Only relevant if type is "dom"
*
* @type {string|null}
*/
_this.path = null;
/**
* Identifier to select element. Override the path for resolving transformObject.
* By default the id is set on element attribute [data-spirit-id].
*
* @type {string|null}
*/
_this.id = null;
/**
* Properties for this timeline.
*
* @type {Props}
*/
_this.props = null;
Object.assign(_this, {
type: type,
props: props instanceof _props["default"] ? props : new _props["default"](props),
label: label,
path: path,
id: id
});
_this.transformObject = transformObject;
return _this;
}
_inheritsLoose(Timeline, _Emitter);
var _proto = Timeline.prototype;
_proto.validate = function validate() {
if (this.type === 'dom' && _utils.context.isBrowser() && this.transformObject && !(this.transformObject instanceof window.Element)) {
throw new Error('transformObject needs to be an element');
}
if (this.type === 'object' && this.transformObject && !_utils.is.isObject(this.transformObject)) {
throw new Error('transformObject needs to be an object');
}
};
_proto.toObject = function toObject(ignoreEval) {
if (ignoreEval === void 0) {
ignoreEval = false;
}
var obj = {
type: this.type,
transformObject: this.transformObject,
props: this.props.toObject(ignoreEval),
label: this.label,
path: this.path,
id: this.id
};
Object.keys(obj).forEach(function (key) {
if (!obj[key]) {
delete obj[key];
}
});
return obj;
};
_proto.destroy = function destroy() {
if (this.props instanceof _props["default"]) {
this.props.each(function (tr) {
return tr.destroy();
});
}
};
return _createClass(Timeline, [{
key: "transformObject",
get: function get() {
return this._transformObject;
},
set: function set(transformObject) {
this._transformObject = transformObject;
this.validate();
if (transformObject && this.props instanceof _props["default"]) {
var thisMapper = this.props.mappings.find(function (mapping) {
return String(mapping.regex) === '/this/g';
});
thisMapper ? thisMapper.map = transformObject : this.props.mappings.push(new _evalmap["default"](/this/g, transformObject));
this.props.mappings = [].concat(this.props.mappings);
}
if (this.type === 'dom' && transformObject instanceof window.Element) {
if (!this._style) this._style = transformObject.getAttribute('style');
if (!this._transform) {
this._transform = transformObject.getAttribute('transform');
transformObject.removeAttribute('transform');
}
}
}
}]);
}(_events.Emitter), (_applyDecoratedDescriptor(_class.prototype, "transformObject", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "transformObject"), _class.prototype)), _class));
Timeline.fromObject = function (obj) {
if (!_utils.is.isObject(obj)) {
throw new Error('Object is invalid.');
}
var args = _utils.convert.objectToArray(obj).filter(function (arg) {
return arg !== undefined;
});
args = _objectSpread({
type: args.type || 'dom',
props: {}
}, _utils.convert.arrayToObject(args));
return new Timeline(args.type, args.transformObject, args.props, args.path || undefined, args.id || undefined, args.label || undefined);
};
var _default = exports["default"] = Timeline;