Browse Source

Fix color field input

up-style-manager
Artur Arseniev 5 years ago
parent
commit
1e743de177
  1. 12
      src/domain_abstract/ui/Input.js
  2. 41
      src/domain_abstract/ui/InputColor.js
  3. 2
      src/style_manager/model/PropertyStack.js
  4. 12
      src/style_manager/view/PropertyColorView.js
  5. 433
      src/utils/ColorPicker.js
  6. 10
      test/specs/style_manager/model/Properties.js

12
src/domain_abstract/ui/Input.js

@ -4,7 +4,7 @@ const $ = Backbone.$;
export default Backbone.View.extend({ export default Backbone.View.extend({
events: { events: {
change: 'handleChange' change: 'handleChange',
}, },
template() { template() {
@ -24,7 +24,7 @@ export default Backbone.View.extend({
this.opts = opts; this.opts = opts;
this.ppfx = ppfx; this.ppfx = ppfx;
this.em = opts.target || {}; this.em = opts.target || {};
this.listenTo(this.model, 'change:value', this.handleModelChange); !opts.onChange && this.listenTo(this.model, 'change:value', this.handleModelChange);
}, },
/** /**
@ -74,11 +74,7 @@ export default Backbone.View.extend({
if (!this.inputEl) { if (!this.inputEl) {
const { model, opts } = this; const { model, opts } = this;
const type = opts.type || 'text'; const type = opts.type || 'text';
const plh = const plh = model.get('placeholder') || model.get('defaults') || model.get('default') || '';
model.get('placeholder') ||
model.get('defaults') ||
model.get('default') ||
'';
this.inputEl = $(`<input type="${type}" placeholder="${plh}">`); this.inputEl = $(`<input type="${type}" placeholder="${plh}">`);
} }
@ -92,5 +88,5 @@ export default Backbone.View.extend({
el.html(this.template()); el.html(this.template());
el.find(`.${this.holderClass()}`).append(this.getInputEl()); el.find(`.${this.holderClass()}`).append(this.getInputEl());
return this; return this;
} },
}); });

41
src/domain_abstract/ui/InputColor.js

@ -39,7 +39,16 @@ export default Input.extend({
this.colorEl.spectrum('destroy'); this.colorEl.spectrum('destroy');
}, },
handleChange(e) {
e.stopPropagation();
const { value } = e.target;
if (isUndefined(value)) return;
this.__onInputChange(value);
},
__onInputChange(val) { __onInputChange(val) {
const { model, opts } = this;
const { onChange } = opts;
let value = val; let value = val;
const colorEl = this.getColorEl(); const colorEl = this.getColorEl();
@ -51,7 +60,7 @@ export default Input.extend({
color && (value = color); color && (value = color);
} }
this.model.set({ value }, { fromInput: 1 }); onChange ? onChange(value) : model.set({ value }, { fromInput: 1 });
}, },
/** /**
@ -60,8 +69,8 @@ export default Input.extend({
* @param {Object} opts * @param {Object} opts
*/ */
setValue(val, opts = {}) { setValue(val, opts = {}) {
const model = this.model; const { model } = this;
const def = model.get('defaults'); const def = !isUndefined(opts.def) ? opts.def : model.get('defaults');
const value = !isUndefined(val) ? val : !isUndefined(def) ? def : ''; const value = !isUndefined(val) ? val : !isUndefined(def) ? def : '';
const inputEl = this.getInputEl(); const inputEl = this.getInputEl();
const colorEl = this.getColorEl(); const colorEl = this.getColorEl();
@ -82,15 +91,15 @@ export default Input.extend({
*/ */
getColorEl() { getColorEl() {
if (!this.colorEl) { if (!this.colorEl) {
const { em, model } = this; const { em, model, opts } = this;
const self = this; const self = this;
const ppfx = this.ppfx; const ppfx = this.ppfx;
const { onChange } = opts;
var colorEl = $(`<div class="${this.ppfx}field-color-picker"></div>`); var colorEl = $(`<div class="${this.ppfx}field-color-picker"></div>`);
var cpStyle = colorEl.get(0).style; var cpStyle = colorEl.get(0).style;
var elToAppend = em && em.config ? em.config.el : ''; var elToAppend = em && em.config ? em.config.el : '';
var colorPickerConfig = var colorPickerConfig = (em && em.getConfig && em.getConfig('colorPicker')) || {};
(em && em.getConfig && em.getConfig('colorPicker')) || {};
let changed = 0; let changed = 0;
let previousColor; let previousColor;
@ -113,30 +122,34 @@ export default Input.extend({
move(color) { move(color) {
const cl = getColor(color); const cl = getColor(color);
cpStyle.backgroundColor = cl; cpStyle.backgroundColor = cl;
model.setValueFromInput(cl, 0); onChange ? onChange(cl, true) : model.setValueFromInput(cl, 0);
}, },
change(color) { change(color) {
changed = 1; changed = 1;
const cl = getColor(color); const cl = getColor(color);
cpStyle.backgroundColor = cl; cpStyle.backgroundColor = cl;
model.setValueFromInput(0, 0); // for UndoManager if (onChange) {
model.setValueFromInput(cl); onChange(cl);
} else {
model.setValueFromInput(0, 0); // for UndoManager
model.setValueFromInput(cl);
}
self.noneColor = 0; self.noneColor = 0;
}, },
show(color) { show(color) {
changed = 0; changed = 0;
previousColor = getColor(color); previousColor = onChange ? model.getValue({ noDefault: true }) : getColor(color);
}, },
hide(color) { hide(color) {
if (!changed && previousColor) { if (!changed && (previousColor || onChange)) {
if (self.noneColor) { if (self.noneColor) {
previousColor = ''; previousColor = '';
} }
cpStyle.backgroundColor = previousColor; cpStyle.backgroundColor = previousColor;
colorEl.spectrum('set', previousColor); colorEl.spectrum('set', previousColor);
model.setValueFromInput(previousColor, 0); onChange ? onChange(previousColor, true) : model.setValueFromInput(previousColor, 0);
} }
} },
}); });
if (em && em.on) { if (em && em.on) {
@ -156,5 +169,5 @@ export default Input.extend({
// This will make the color input available on render // This will make the color input available on render
this.getColorEl(); this.getColorEl();
return this; return this;
} },
}); });

2
src/style_manager/model/PropertyStack.js

@ -285,7 +285,7 @@ export default class PropertyStack extends PropertyComposite {
values && values &&
this.getProperties().forEach(prop => { this.getProperties().forEach(prop => {
const value = values[prop.getId()]; const value = values[prop.getId()];
!isUndefined(value) && prop.upValue(value, { ...opts, __up: true }); prop.__getFullValue() !== value && prop.upValue(value, { ...opts, __up: true });
}); });
!noEvent && sm.__trgEv(sm.events.layerSelect, { property: this }); !noEvent && sm.__trgEv(sm.events.layerSelect, { property: this });

12
src/style_manager/view/PropertyColorView.js

@ -2,9 +2,8 @@ import PropertyNumberView from './PropertyNumberView';
import InputColor from 'domain_abstract/ui/InputColor'; import InputColor from 'domain_abstract/ui/InputColor';
export default PropertyNumberView.extend({ export default PropertyNumberView.extend({
setValue(value, opts = {}) { setValue(value) {
opts = { ...opts, silent: 1 }; this.inputInst.setValue(value, { fromTarget: 1, def: this.model.getDefaultValue() });
this.inputInst.setValue(value, opts);
}, },
remove() { remove() {
@ -14,10 +13,15 @@ export default PropertyNumberView.extend({
['inputInst', '$color'].forEach(i => (this[i] = {})); ['inputInst', '$color'].forEach(i => (this[i] = {}));
}, },
__handleChange(value, partial) {
this.model.upValue(value, { partial });
},
onRender() { onRender() {
if (!this.inputInst) { if (!this.inputInst) {
this.__handleChange = this.__handleChange.bind(this);
const { ppfx, model, em, el } = this; const { ppfx, model, em, el } = this;
const inputColor = new InputColor({ target: em, model, ppfx }); const inputColor = new InputColor({ target: em, model, ppfx, onChange: this.__handleChange });
const input = inputColor.render(); const input = inputColor.render();
el.querySelector(`.${ppfx}fields`).appendChild(input.el); el.querySelector(`.${ppfx}fields`).appendChild(input.el);
this.input = input.inputEl.get(0); this.input = input.inputEl.get(0);

433
src/utils/ColorPicker.js

File diff suppressed because it is too large

10
test/specs/style_manager/model/Properties.js

@ -76,7 +76,7 @@ describe('StyleManager properties logic', () => {
}); });
test('Rule selected', () => { test('Rule selected', () => {
expect(obj.getLastSelected()).toBe(rule1); expect(obj.getSelected()).toBe(rule1);
}); });
test('Properties correctly reflect on rule update', () => { test('Properties correctly reflect on rule update', () => {
@ -293,7 +293,7 @@ describe('StyleManager properties logic', () => {
`)[0]; `)[0];
dv.select('tablet'); dv.select('tablet');
obj.__upSel(); obj.__upSel();
expect(obj.getLastSelected()).toBe(rule2); expect(obj.getSelected()).toBe(rule2);
expect(obj.getSelectedParents()).toEqual([rule1]); expect(obj.getSelectedParents()).toEqual([rule1]);
expect(compTypeProp.hasValue()).toBe(true); expect(compTypeProp.hasValue()).toBe(true);
@ -322,7 +322,7 @@ describe('StyleManager properties logic', () => {
`)[0]; `)[0];
dv.select('tablet'); dv.select('tablet');
obj.__upSel(); obj.__upSel();
expect(obj.getLastSelected()).toBe(rule2); expect(obj.getSelected()).toBe(rule2);
expect(compTypeProp.hasValue({ noParent: true })).toBe(true); expect(compTypeProp.hasValue({ noParent: true })).toBe(true);
[ [
[propATest, '11px'], [propATest, '11px'],
@ -336,7 +336,7 @@ describe('StyleManager properties logic', () => {
dv.select('desktop'); dv.select('desktop');
obj.__upSel(); obj.__upSel();
expect(obj.getLastSelected()).toBe(rule1); expect(obj.getSelected()).toBe(rule1);
expect(obj.getSelectedParents()).toEqual([]); expect(obj.getSelectedParents()).toEqual([]);
[ [
[propATest, ''], [propATest, ''],
@ -718,7 +718,7 @@ describe('StyleManager properties logic', () => {
`)[0]; `)[0];
dv.select('tablet'); dv.select('tablet');
obj.__upSel(); obj.__upSel();
expect(obj.getLastSelected()).toBe(rule2); expect(obj.getSelected()).toBe(rule2);
expect(obj.getSelectedParents()).toEqual([rule1]); expect(obj.getSelectedParents()).toEqual([rule1]);
expect(compTypeProp.hasValue()).toBe(true); expect(compTypeProp.hasValue()).toBe(true);

Loading…
Cancel
Save