Browse Source

Improve Color Input

pull/330/head
Artur Arseniev 9 years ago
parent
commit
9d8fbe28e3
  1. 4
      src/domain_abstract/ui/Input.js
  2. 53
      src/domain_abstract/ui/InputColor.js
  3. 5
      src/style_manager/view/PropertyColorView.js
  4. 15
      src/style_manager/view/PropertyView.js

4
src/domain_abstract/ui/Input.js

@ -41,14 +41,14 @@ module.exports = Backbone.View.extend({
// Generally I get silent when I need to reflect data to view without // Generally I get silent when I need to reflect data to view without
// reupdating the target // reupdating the target
if(opt.silent) { if(opt.silent) {
this.handleModelChange(); this.handleModelChange(model, value, opt);
} }
}, },
/** /**
* Updates the view when the model is changed * Updates the view when the model is changed
* */ * */
handleModelChange() { handleModelChange(model, value, opts) {
this.getInputEl().value = this.model.get('value'); this.getInputEl().value = this.model.get('value');
}, },

53
src/domain_abstract/ui/InputColor.js

@ -15,31 +15,36 @@ module.exports = Input.extend({
initialize(opts) { initialize(opts) {
Input.prototype.initialize.apply(this, arguments); Input.prototype.initialize.apply(this, arguments);
var ppfx = this.ppfx; var ppfx = this.ppfx;
this.colorCls = ppfx + 'field-color-picker'; this.colorCls = `${ppfx}field-color-picker`;
this.inputClass = ppfx + 'field ' + ppfx + 'field-color'; this.inputClass = `${ppfx}field ${ppfx}field-color`;
this.colorHolderClass = ppfx + 'field-colorp-c'; this.colorHolderClass = `${ppfx}field-colorp-c`;
},
this.listenTo(this.model, 'change:value', this.handleModelChange); /**
* Set value to the model
* @param {string} val
* @param {Object} opts
*/
setValue(val, opts = {}) {
const model = this.model;
const value = val || model.get('defaults');
const inputEl = this.getInputEl();
const colorEl = this.getColorEl();
const valueClr = value != 'none' ? value : '';
inputEl.value = value;
colorEl.get(0).style.backgroundColor = valueClr;
if (opts.targetUpdate) {
colorEl.spectrum('set', valueClr);
this.noneColor = value == 'none';
}
}, },
/** /**
* Updates the view when the model is changed * Updates the view when the model is changed
* */ * */
handleModelChange(mdl, vl, opts = {}) { handleModelChange(model, value, opts) {
Input.prototype.handleModelChange.apply(this, arguments); this.setValue(value, opts);
var value = this.model.get('value');
var colorEl = this.getColorEl();
// If no color selected I will set white for the picker
value = value === 'none' ? '#fff' : value;
colorEl.get(0).style.backgroundColor = value;
// Prevent usuless palette updating
if (!opts.avoidStore) {
console.log('COlor to set', value, mdl, vl, opts);
colorEl.spectrum('set', value);
}
}, },
/** /**
@ -47,7 +52,8 @@ module.exports = Input.extend({
* @return {HTMLElement} * @return {HTMLElement}
*/ */
getColorEl() { getColorEl() {
if(!this.colorEl) { if (!this.colorEl) {
const self = this;
var model = this.model; var model = this.model;
var colorEl = $('<div>', {class: this.colorCls}); var colorEl = $('<div>', {class: this.colorCls});
var cpStyle = colorEl.get(0).style; var cpStyle = colorEl.get(0).style;
@ -65,7 +71,6 @@ module.exports = Input.extend({
let changed = 0; let changed = 0;
let previousСolor; let previousСolor;
colorEl.spectrum({ colorEl.spectrum({
//color: "#f00"
appendTo: elToAppend || 'body', appendTo: elToAppend || 'body',
maxSelectionSize: 8, maxSelectionSize: 8,
showPalette: true, showPalette: true,
@ -84,15 +89,17 @@ module.exports = Input.extend({
cpStyle.backgroundColor = cl; cpStyle.backgroundColor = cl;
model.set('value', '', {avoidStore: 1}); model.set('value', '', {avoidStore: 1});
model.set('value', cl); model.set('value', cl);
colorEl.spectrum('set', cl); self.noneColor = 0;
}, },
show(color) { show(color) {
changed = 0; changed = 0;
console.log('Show color', color, getColor(color));
previousСolor = getColor(color); previousСolor = getColor(color);
}, },
hide(color) { hide(color) {
if (!changed && previousСolor) { if (!changed && previousСolor) {
if (self.noneColor) {
previousСolor = '';
}
cpStyle.backgroundColor = previousСolor; cpStyle.backgroundColor = previousСolor;
colorEl.spectrum('set', previousСolor); colorEl.spectrum('set', previousСolor);
model.set('value', previousСolor, {avoidStore: 1}); model.set('value', previousСolor, {avoidStore: 1});

5
src/style_manager/view/PropertyColorView.js

@ -7,8 +7,9 @@ module.exports = require('./PropertyIntegerView').extend({
this.className += ` ${this.pfx}file`; this.className += ` ${this.pfx}file`;
}, },
setValue(value) { setValue(value, opts = {}) {
this.inputInst.setValue(value, {silent: 1}); opts = Object.assign({}, opts, {silent: 1});
this.inputInst.setValue(value, opts);
}, },
onRender() { onRender() {

15
src/style_manager/view/PropertyView.js

@ -195,7 +195,7 @@ module.exports = Backbone.View.extend({
} }
model.set('value', value, {silent: 1}); model.set('value', value, {silent: 1});
this.setValue(value, 1); this.setValue(value, {targetUpdate: 1});
model.set('status', status); model.set('status', status);
if (em) { if (em) {
@ -398,18 +398,11 @@ module.exports = Backbone.View.extend({
* @param {Boolean} force * @param {Boolean} force
* @private * @private
* */ * */
setValue(value, force) { setValue(value, opts = {}) {
const model = this.model; const model = this.model;
const f = force === 0 ? 0 : 1; let val = value || model.get('value') || model.getDefaultValue();
const def = model.getDefaultValue();
let v = model.get('value') || def;
if (value || f) {
v = value;
}
const input = this.getInputEl(); const input = this.getInputEl();
input && (input.value = v); input && (input.value = val);
}, },
getInputEl() { getInputEl() {

Loading…
Cancel
Save