Browse Source

Refactor PropertyView and make computed function configurable

pull/187/head
Artur Arseniev 9 years ago
parent
commit
b74cb9c877
  1. 2
      dist/css/grapes.min.css
  2. 8
      index.html
  3. 10
      src/domain_abstract/ui/InputNumber.js
  4. 2
      src/editor/index.js
  5. 18
      src/style_manager/config/config.js
  6. 4
      src/style_manager/model/PropertyFactory.js
  7. 2
      src/style_manager/view/PropertyCompositeView.js
  8. 16
      src/style_manager/view/PropertyFileView.js
  9. 5
      src/style_manager/view/PropertyIntegerView.js
  10. 20
      src/style_manager/view/PropertyStackView.js
  11. 78
      src/style_manager/view/PropertyView.js
  12. 7
      src/styles/scss/main.scss

2
dist/css/grapes.min.css

File diff suppressed because one or more lines are too long

8
index.html

@ -1320,6 +1320,14 @@
console.log('STORE ', e); console.log('STORE ', e);
}) })
editor.on('styleManager:change:text-shadow', function(view) {
var model = view.model;
let targetValue = view.getTargetValue({ignoreDefault: 1});
let computedValue = view.getComputedValue();
let defaultValue = view.getDefaultValue();
console.log('Style of ', model.get('property'), 'Target: ', targetValue, 'Computed:', computedValue, 'Default:', defaultValue);
})
editor.render(); editor.render();
</script> </script>
</body> </body>

10
src/domain_abstract/ui/InputNumber.js

@ -62,6 +62,7 @@ module.exports = Backbone.View.extend({
handleChange(e) { handleChange(e) {
e.stopPropagation(); e.stopPropagation();
this.setValue(this.getInputEl().value); this.setValue(this.getInputEl().value);
this.elementUpdated();
}, },
/** /**
@ -71,6 +72,14 @@ module.exports = Backbone.View.extend({
e.stopPropagation(); e.stopPropagation();
var value = this.getUnitEl().value; var value = this.getUnitEl().value;
this.model.set('unit', value); this.model.set('unit', value);
this.elementUpdated();
},
/**
* Fired when the element of the property is updated
*/
elementUpdated() {
this.model.trigger('el:change');
}, },
/** /**
@ -185,6 +194,7 @@ module.exports = Backbone.View.extend({
var value = this.prValue - 1; var value = this.prValue - 1;
this.model.set('value', value, {avoidStore: 1}) this.model.set('value', value, {avoidStore: 1})
.set('value', value + 1); .set('value', value + 1);
this.elementUpdated();
} }
}, },

2
src/editor/index.js

@ -36,6 +36,8 @@
* component:update:{propertyName} - Listen any property change * component:update:{propertyName} - Listen any property change
* component:styleUpdate - Triggered when the style of the component is updated * component:styleUpdate - Triggered when the style of the component is updated
* component:styleUpdate:{propertyName} - Listen for a specific style property change * component:styleUpdate:{propertyName} - Listen for a specific style property change
* styleManager:change - Triggered on style property change from new selected component, the view of the property is passed as an argument to the callback
* styleManager:change:{propertyName} - As above but for a specific style property
* storage:load - Triggered when something was loaded from the storage, loaded object passed as an argumnet * storage:load - Triggered when something was loaded from the storage, loaded object passed as an argumnet
* storage:store - Triggered when something is stored to the storage, stored object passed as an argumnet * storage:store - Triggered when something is stored to the storage, stored object passed as an argumnet
* canvasScroll - Triggered when the canvas is scrolled * canvasScroll - Triggered when the canvas is scrolled

18
src/style_manager/config/config.js

@ -9,4 +9,22 @@ module.exports = {
// Hide the property in case it's not stylable for the // Hide the property in case it's not stylable for the
// selected component (each component has 'stylable' property) // selected component (each component has 'stylable' property)
hideNotStylable: true, hideNotStylable: true,
// Highlight changed properties of the selected component
highlightChanged: true,
// Highlight computed properties of the selected component
highlightComputed: true,
// Show computed properties of the selected component, if this value
// is set to false, highlightComputed will not take effect
showComputed: true,
// Properties which are valid to be shown as computed
// (Identified as inherited properties: https://developer.mozilla.org/en-US/docs/Web/CSS/inheritance)
validComputed: ['border-collapse', 'border-spacing', 'caption-side', 'color', 'cursor', 'direction', 'empty-cells',
'font-family', 'font-size', 'font-style', 'font-variant', 'font-weight', 'font-size-adjust', 'font-stretch', 'font',
'letter-spacing', 'line-height', 'list-style-image', 'list-style-position', 'list-style-type', 'list-style', 'orphans',
'quotes', 'tab-size', 'text-align', 'text-align-last', 'text-decoration-color', 'text-indent', 'text-justify',
'text-shadow', 'text-transform', 'visibility', 'white-space', 'widows', 'word-break', 'word-spacing', 'word-wrap'],
}; };

4
src/style_manager/model/PropertyFactory.js

@ -95,9 +95,9 @@ module.exports = () => ({
} }
// Defaults // Defaults
switch(prop){ switch(prop) {
case 'float': case 'background-color': case 'float': case 'background-color':
case 'background-image': case 'background-image': case 'text-shadow':
obj.defaults = 'none'; obj.defaults = 'none';
break; break;
case 'display': case 'display':

2
src/style_manager/view/PropertyCompositeView.js

@ -99,7 +99,7 @@ module.exports = PropertyView.extend({
this.props.each((prop, index) => { this.props.each((prop, index) => {
str += prop.get('defaults') + prop.get('unit') + ' '; str += prop.get('defaults') + prop.get('unit') + ' ';
}); });
return str.replace(/ +$/,''); return this.model.get('defaults') || str.replace(/ +$/,'');
}, },
/** /**

16
src/style_manager/view/PropertyFileView.js

@ -41,14 +41,15 @@ module.exports = PropertyView.extend({
this.$previewBox = this.$el.find('#' + this.pfx + 'preview-box'); this.$previewBox = this.$el.find('#' + this.pfx + 'preview-box');
} }
if(!this.componentValue || this.componentValue == this.defaultValue)
this.setPreviewView(0);
else
this.setPreviewView(1);
this.setValue(this.componentValue, 0); this.setValue(this.componentValue, 0);
}, },
setValue(value, f) {
PropertyView.prototype.setValue.apply(this, arguments);
this.setPreviewView(value && value != this.getDefaultValue());
this.setPreview(value);
},
/** /**
* Change visibility of the preview box * Change visibility of the preview box
* @param bool Visibility * @param bool Visibility
@ -84,11 +85,6 @@ module.exports = PropertyView.extend({
this.$preview.css('background-image', "url(" + url + ")"); this.$preview.css('background-image', "url(" + url + ")");
}, },
/** @inheritdoc */
setValue(value, f) {
PropertyView.prototype.setValue.apply(this, arguments);
this.setPreview(value);
},
/** @inheritdoc */ /** @inheritdoc */
renderTemplate() { renderTemplate() {

5
src/style_manager/view/PropertyIntegerView.js

@ -6,7 +6,8 @@ module.exports = PropertyView.extend({
initialize(options) { initialize(options) {
PropertyView.prototype.initialize.apply(this, arguments); PropertyView.prototype.initialize.apply(this, arguments);
this.listenTo( this.model ,'change:unit', this.valueChanged); this.listenTo(this.model, 'change:unit', this.valueChanged);
this.listenTo(this.model, 'el:change', this.elementUpdated);
}, },
/** /**
@ -35,8 +36,6 @@ module.exports = PropertyView.extend({
renderTemplate() {}, renderTemplate() {},
setValue(value) { setValue(value) {
if(this.model.get('property') == 'font-size')
console.log('set value int', value);
this.input.setValue(value, {silent: 1}); this.input.setValue(value, {silent: 1});
}, },

20
src/style_manager/view/PropertyStackView.js

@ -28,18 +28,13 @@ module.exports = PropertyCompositeView.extend({
* so we gonna check all props and fine if there is some differences. * so we gonna check all props and fine if there is some differences.
* */ * */
targetUpdated(...args) { targetUpdated(...args) {
if(!this.model.get('detached')) if (!this.model.get('detached')) {
PropertyCompositeView.prototype.targetUpdated.apply(this, args); PropertyCompositeView.prototype.targetUpdated.apply(this, args);
else { } else {
this.checkVisibility(); this.checkVisibility();
this.refreshLayers();
/*
this.model.get('properties').each(function(prop) {
console.log(prop.get('property'), ' - ', prop.get('value'));
});
*/
} }
this.refreshLayers();
}, },
/** /**
@ -115,7 +110,7 @@ module.exports = PropertyCompositeView.extend({
// If detached the value in this case is stacked, eg. substack-prop: 1px, 2px, 3px... // If detached the value in this case is stacked, eg. substack-prop: 1px, 2px, 3px...
if (this.model.get('detached')) { if (this.model.get('detached')) {
var targetValue = propView.getTargetValue(); var targetValue = propView.getTargetValue({ignoreCustomValue: 1});
var valist = (targetValue + '').split(','); var valist = (targetValue + '').split(',');
result = valist[layerIndex]; result = valist[layerIndex];
result = result ? result.trim() : propView.getDefaultValue(); result = result ? result.trim() : propView.getDefaultValue();
@ -283,7 +278,10 @@ module.exports = PropertyCompositeView.extend({
fieldName = 'values'; fieldName = 'values';
a = this.getLayersFromTarget(); a = this.getLayersFromTarget();
} else { } else {
var v = this.getComponentValue(); //var v = this.getComponentValue();
var v = this.getTargetValue();
var vDef = this.getDefaultValue();
v = v == vDef ? '' : v;
if (v) { if (v) {
// Remove spaces inside functions: // Remove spaces inside functions:
// eg: // eg:

78
src/style_manager/view/PropertyView.js

@ -14,7 +14,9 @@ module.exports = Backbone.View.extend({
</div> </div>
</div>`), </div>`),
events: {'change': 'valueUpdated'}, events: {
'change': 'valueUpdated'
},
initialize(o) { initialize(o) {
this.config = o.config || {}; this.config = o.config || {};
@ -92,8 +94,15 @@ module.exports = Backbone.View.extend({
* Fired when the input value is updated * Fired when the input value is updated
*/ */
valueUpdated() { valueUpdated() {
if(this.$input) this.model.set('value', this.getInputValue());
this.model.set('value', this.getInputValue()); this.elementUpdated();
},
/**
* Fired when the element of the property is updated
*/
elementUpdated() {
this.model.set('status', 'updated');
}, },
/** /**
@ -109,41 +118,44 @@ module.exports = Backbone.View.extend({
let targetValue = this.getTargetValue({ignoreDefault: 1}); let targetValue = this.getTargetValue({ignoreDefault: 1});
let defaultValue = this.getDefaultValue(); let defaultValue = this.getDefaultValue();
let computedValue = this.getComputedValue(); let computedValue = this.getComputedValue();
const config = this.config;
const em = config.em;
const model = this.model;
if (targetValue) { if (targetValue) {
value = targetValue; value = targetValue;
status = 'updated';
} else if (computedValue && computedValue != defaultValue) { if (config.highlightChanged) {
status = 'updated';
}
} else if (computedValue && config.showComputed &&
computedValue != defaultValue) {
value = computedValue; value = computedValue;
status = 'computed';
if (config.highlightComputed) {
status = 'computed';
}
} else { } else {
value = defaultValue; value = defaultValue;
status = ''; status = '';
} }
//value = this.tryFetchFromFunction(value);
this.setValue(value, 1); this.setValue(value, 1);
this.model.set('status', status); this.model.set('status', status);
/* if (em) {
get targetValue em.trigger('styleManager:change', this);
if targetValue em.trigger(`styleManager:change:${model.get('property')}`, this);
setValue(targetValue) }
setStatus('updated');
elseif computedValue != defaultValue
setValue(computedValue)
setStatus('computed');
else
setValue(defaultValue)
setStatus('');
*/
/* /*
if(this.getTarget()) { if(this.getTarget()) {
if(!this.sameValue()){ if(!this.sameValue()){
this.renderInputRequest(); this.renderInputRequest();
} }
} }*/
*/
}, },
checkVisibility() { checkVisibility() {
@ -171,11 +183,10 @@ module.exports = Backbone.View.extend({
* same of the value of the model * same of the value of the model
* *
* @return {Boolean} * @return {Boolean}
* * * */
sameValue() { sameValue() {
return this.getComponentValue() == this.getValueForTarget(); return this.getComponentValue() == this.getValueForTarget();
}, },
*/
/** /**
@ -226,6 +237,7 @@ module.exports = Backbone.View.extend({
var result; var result;
var model = this.model; var model = this.model;
var target = this.getTarget(); var target = this.getTarget();
var customFetchValue = this.customValue;
if (!target) { if (!target) {
return result; return result;
@ -237,6 +249,15 @@ module.exports = Backbone.View.extend({
result = this.getDefaultValue(); result = this.getDefaultValue();
} }
if (typeof customFetchValue == 'function' && !opts.ignoreCustomValue) {
let index = model.collection.indexOf(model);
let customValue = customFetchValue(this, index);
if (customValue) {
result = customValue;
}
}
return result; return result;
}, },
@ -256,7 +277,9 @@ module.exports = Backbone.View.extend({
*/ */
getComputedValue() { getComputedValue() {
let computed = this.propTarget.computed; let computed = this.propTarget.computed;
return computed && computed[this.model.get('property')]; const valid = this.config.validComputed;
const property = this.model.get('property');
return computed && valid.indexOf(property) >= 0 && computed[property];
}, },
/** /**
@ -274,9 +297,10 @@ module.exports = Backbone.View.extend({
return value; return value;
} }
var start = value.indexOf("(") + 1; var valueStr = value + '';
var end = value.lastIndexOf(")"); var start = valueStr.indexOf("(") + 1;
return value.substring(start, end); var end = valueStr.lastIndexOf(")");
return valueStr.substring(start, end);
}, },
/** /**

7
src/styles/scss/main.scss

@ -88,17 +88,18 @@ $fontV: 20;//random(1000)
transform: $v; transform: $v;
} }
//.#{$app-prefix}fonts {} $colorActive: #71b7f1;
$colorWarn: #ffca6f;
.#{$app-prefix}active { .#{$app-prefix}active {
&-color { &-color {
color: $colorBlue; color: $colorActive;
} }
} }
.#{$app-prefix}warn { .#{$app-prefix}warn {
&-color { &-color {
color: $colorYell; color: $colorWarn;
} }
} }

Loading…
Cancel
Save