UNPKG

spiritjs

Version:

The animation toolkit for the web

192 lines (149 loc) 6.2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; var _list = require('../list/list'); var _list2 = _interopRequireDefault(_list); var _param = require('./param'); var _param2 = _interopRequireDefault(_param); var _utils = require('../utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * List of params. * * @fires Params#change:list * @fires Params#add * @fires Params#remove * * Bubble events from items * * @fires Params#change * @fires Params#change:prop * @fires Params#change:value */ var Params = function (_List) { _inherits(Params, _List); /** * Create params. * * @param {Array|object} params */ function Params() { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; _classCallCheck(this, Params); if (_utils.is.isObject(params)) { params = _utils.convert.objectToArray(params); } var _this = _possibleConstructorReturn(this, (Params.__proto__ || Object.getPrototypeOf(Params)).call(this, params, _param2.default)); _this.duplicates = { prop: 'prop' }; _this._mappings = []; return _this; } /** * Get mappings for these transitions * * @returns {Array} */ _createClass(Params, [{ key: 'get', /** * Get param by prop * * @param {string} prop * @returns {Param} */ value: function get(prop) { return this._list.find(function (p) { return p.prop === prop; }); } /** * Contains param with prop * * @param {string} prop * @returns {boolean} */ }, { key: 'haveProp', value: function haveProp(prop) { return this._list.filter(function (p) { return p.prop === prop; }).length > 0; } /** * Add param * * @param {*|Array} p * @returns {*} */ }, { key: 'add', value: function add(p) { var _this2 = this; if (_utils.is.isObject(p) && !(p instanceof _param2.default) && Object.keys(p).length > 1) { p = _utils.convert.objectToArray(p); } var affected = _get(Params.prototype.__proto__ || Object.getPrototypeOf(Params.prototype), 'add', this).call(this, p); var exec = function exec(param) { param.mappings = [].concat(_toConsumableArray(_this2.mappings)); }; Array.isArray(affected) ? affected.forEach(exec) : exec(affected); return affected; } /** * Remove param * * @param {*|Array} p */ }, { key: 'remove', value: function remove(p) { var affected = _get(Params.prototype.__proto__ || Object.getPrototypeOf(Params.prototype), 'remove', this).call(this, p); var exec = function exec(param) { param.mappings = []; }; Array.isArray(affected) ? affected.forEach(exec) : exec(affected); return affected; } /** * Convert params to an object * * @returns {object} */ }, { key: 'toObject', value: function toObject() { return this._list.reduce(function (obj, param) { return _extends({}, obj, param.toObject()); }, {}); } }, { key: 'mappings', get: function get() { return this._mappings; } /** * Set mappings for these transitions * * @param {Array} mappings */ , set: function set(mappings) { this._mappings = mappings; this.each(function (p) { p.mappings = [].concat(_toConsumableArray(mappings)); }); } }]); return Params; }(_list2.default); Params.Events = ['change:list', 'add', 'remove', 'change', 'change:prop', 'change:value']; exports.default = Params; module.exports = exports['default'];