Browse Source

Refactor PropertyComposite in class

up-style-manager
Artur Arseniev 5 years ago
parent
commit
397f9fc179
  1. 110
      src/style_manager/model/PropertyComposite.js

110
src/style_manager/model/PropertyComposite.js

@ -1,33 +1,35 @@
import { isString, isUndefined } from 'underscore'; import { isString, isUndefined } from 'underscore';
import Property from './Property'; import Property from './Property';
export default Property.extend({ export default class PropertyComposite extends Property {
defaults: { defaults() {
...Property.prototype.defaults, return {
// 'background' is a good example where to make a difference ...Property.getDefaults(),
// between detached and not // 'background' is a good example where to make a difference
// // between detached and not
// - NOT detached (default) //
// background: url(..) no-repeat center ...; // - NOT detached (default)
// - Detached // background: url(..) no-repeat center ...;
// background-image: url(); // - Detached
// background-repeat: repeat; // background-image: url();
// ... // background-repeat: repeat;
detached: 0, // ...
detached: 0,
// Array of sub properties
properties: [], // Array of sub properties
properties: [],
// Separator to use to split property values (only for not detached properties)
separator: ' ', // Separator to use to split property values (only for not detached properties)
separator: ' ',
// Separator to use to join property values (only for not detached properties)
join: null, // Separator to use to join property values (only for not detached properties)
join: null,
fromStyle: null,
fromStyle: null,
toStyle: null,
}, toStyle: null,
};
}
initialize(props = {}, opts = {}) { initialize(props = {}, opts = {}) {
Property.callParentInit(Property, this, props, opts); Property.callParentInit(Property, this, props, opts);
@ -38,7 +40,7 @@ export default Property.extend({
this.listenTo(properties, 'change', this.__upProperties); this.listenTo(properties, 'change', this.__upProperties);
this.listenTo(this, 'change:value', this.updateValues); this.listenTo(this, 'change:value', this.updateValues);
Property.callInit(this, props, opts); Property.callInit(this, props, opts);
}, }
__upProperties(p, opts = {}) { __upProperties(p, opts = {}) {
if (!this.__hasCustom() || opts.__up || opts.__clearIn) return; if (!this.__hasCustom() || opts.__up || opts.__clearIn) return;
@ -47,7 +49,7 @@ export default Property.extend({
if (parentProp) return parentProp.__upProperties(this, opts); if (parentProp) return parentProp.__upProperties(this, opts);
this.__upTargetsStyleProps(opts, p); this.__upTargetsStyleProps(opts, p);
}, }
__upTargetsStyleProps(opts = {}, prop) { __upTargetsStyleProps(opts = {}, prop) {
let style = this.getStyleFromProps(); let style = this.getStyleFromProps();
@ -58,16 +60,16 @@ export default Property.extend({
} }
this.__upTargetsStyle(style, opts); this.__upTargetsStyle(style, opts);
}, }
_up(props, opts = {}) { _up(props, opts = {}) {
this.__setProperties(this.__getSplitValue(props.value), opts); this.__setProperties(this.__getSplitValue(props.value), opts);
return Property.prototype._up.call(this, props, opts); return Property.prototype._up.call(this, props, opts);
}, }
getStyle(opts) { getStyle(opts) {
return this.getStyleFromProps(opts); return this.getStyleFromProps(opts);
}, }
/** /**
* Get style object from current properties * Get style object from current properties
@ -115,18 +117,18 @@ export default Property.extend({
return res; return res;
}, {}) }, {})
: style; : style;
}, }
__getFullValue(opts = {}) { __getFullValue(opts = {}) {
if (this.isDetached() || opts.__clear) return ''; if (this.isDetached() || opts.__clear) return '';
return this.getStyleFromProps()[this.getName()] || ''; return this.getStyleFromProps()[this.getName()] || '';
}, }
__getJoin() { __getJoin() {
const join = this.get('join'); const join = this.get('join');
return isString(join) ? join : this.get('separator'); return isString(join) ? join : this.get('separator');
}, }
__styleHasProps(style = {}) { __styleHasProps(style = {}) {
const name = this.getName(); const name = this.getName();
@ -134,18 +136,18 @@ export default Property.extend({
const nameProps = props.map(prop => prop.getName()); const nameProps = props.map(prop => prop.getName());
const allNameProps = [name, ...nameProps]; const allNameProps = [name, ...nameProps];
return allNameProps.some(prop => !isUndefined(style[prop]) && style[prop] !== ''); return allNameProps.some(prop => !isUndefined(style[prop]) && style[prop] !== '');
}, }
__splitValue(value, sep) { __splitValue(value, sep) {
return value return value
.split(sep) .split(sep)
.map(value => value.trim()) .map(value => value.trim())
.filter(Boolean); .filter(Boolean);
}, }
__splitStyleName(style, name, sep) { __splitStyleName(style, name, sep) {
return this.__splitValue(style[name] || '', sep); return this.__splitValue(style[name] || '', sep);
}, }
__getSplitValue(value = '') { __getSplitValue(value = '') {
const props = this.getProperties(); const props = this.getProperties();
@ -169,7 +171,7 @@ export default Property.extend({
}); });
return result; return result;
}, }
__getPropsFromStyle(style = {}) { __getPropsFromStyle(style = {}) {
if (!this.__styleHasProps(style)) return null; if (!this.__styleHasProps(style)) return null;
@ -191,23 +193,23 @@ export default Property.extend({
} }
return result; return result;
}, }
__setProperties(values = {}, opts = {}) { __setProperties(values = {}, opts = {}) {
this.getProperties().forEach(prop => { this.getProperties().forEach(prop => {
const value = values[prop.getId()]; const value = values[prop.getId()];
prop.__getFullValue() !== value && prop.upValue(value, opts); prop.__getFullValue() !== value && prop.upValue(value, opts);
}); });
}, }
clear() { clear() {
this.getProperties().map(p => p.clear({ __clearIn: !this.isDetached() })); this.getProperties().map(p => p.clear({ __clearIn: !this.isDetached() }));
return Property.prototype.clear.call(this); return Property.prototype.clear.call(this);
}, }
hasValue(opts) { hasValue(opts) {
return this.getProperties().some(prop => prop.hasValue(opts)); return this.getProperties().some(prop => prop.hasValue(opts));
}, }
/** /**
* Get current values of properties * Get current values of properties
@ -221,7 +223,7 @@ export default Property.extend({
res[key] = `${prop.__getFullValue()}`; res[key] = `${prop.__getFullValue()}`;
return res; return res;
}, {}); }, {});
}, }
/** /**
* Clear the value * Clear the value
@ -231,7 +233,7 @@ export default Property.extend({
clearValue(opts = {}) { clearValue(opts = {}) {
this.get('properties').each(property => property.clearValue()); this.get('properties').each(property => property.clearValue());
return Property.prototype.clearValue.apply(this, arguments); return Property.prototype.clearValue.apply(this, arguments);
}, }
/** /**
* Update property values * Update property values
@ -248,7 +250,7 @@ export default Property.extend({
// There some issue with UndoManager // There some issue with UndoManager
//property.setValue(value, 0, {fromParent: 1}); //property.setValue(value, 0, {fromParent: 1});
}); });
}, }
/** /**
* Split by sperator but avoid it inside parenthesis * Split by sperator but avoid it inside parenthesis
@ -256,7 +258,7 @@ export default Property.extend({
*/ */
getSplitSeparator() { getSplitSeparator() {
return new RegExp(`${this.get('separator')}(?![^\\(]*\\))`); return new RegExp(`${this.get('separator')}(?![^\\(]*\\))`);
}, }
/** /**
* Returns default value * Returns default value
@ -274,7 +276,7 @@ export default Property.extend({
const properties = this.get('properties'); const properties = this.get('properties');
properties.each((prop, index) => (value += `${prop.getDefaultValue()} `)); properties.each((prop, index) => (value += `${prop.getDefaultValue()} `));
return value.trim(); return value.trim();
}, }
getFullValue() { getFullValue() {
if (this.get('detached')) { if (this.get('detached')) {
@ -282,7 +284,7 @@ export default Property.extend({
} }
return this.get('properties').getFullValue(); return this.get('properties').getFullValue();
}, }
/** /**
* Get property at some index * Get property at some index
@ -291,17 +293,17 @@ export default Property.extend({
*/ */
getPropertyAt(index) { getPropertyAt(index) {
return this.get('properties').at(index); return this.get('properties').at(index);
}, }
isDetached() { isDetached() {
return !!this.get('detached'); return !!this.get('detached');
}, }
getProperties() { getProperties() {
return [...this.get('properties').models]; return [...this.get('properties').models];
}, }
getProperty(id) { getProperty(id) {
return this.get('properties').filter(prop => prop.get('id') === id)[0] || null; return this.get('properties').filter(prop => prop.get('id') === id)[0] || null;
}, }
}); }

Loading…
Cancel
Save