Browse Source

Change to to use classes

pull/4219/head
Alex 5 years ago
parent
commit
d2f3404c1c
  1. 16
      src/canvas/view/FramesView.js
  2. 11
      src/dom_components/view/ToolbarView.js
  3. 37
      src/domain_abstract/ui/Input.js
  4. 22
      src/domain_abstract/ui/InputColor.js
  5. 62
      src/domain_abstract/ui/InputNumber.js
  6. 52
      src/domain_abstract/view/DomainViews.js
  7. 17
      src/trait_manager/index.js
  8. 14
      src/trait_manager/view/TraitButtonView.js
  9. 12
      src/trait_manager/view/TraitCheckboxView.js
  10. 8
      src/trait_manager/view/TraitColorView.js
  11. 12
      src/trait_manager/view/TraitNumberView.js
  12. 19
      src/trait_manager/view/TraitSelectView.js
  13. 87
      src/trait_manager/view/TraitView.js
  14. 17
      src/trait_manager/view/TraitsView.js

16
src/canvas/view/FramesView.js

@ -1,21 +1,21 @@
import DomainViews from 'domain_abstract/view/DomainViews'; import DomainViews from 'domain_abstract/view/DomainViews';
import FrameWrapView from './FrameWrapView'; import FrameWrapView from './FrameWrapView';
export default DomainViews.extend({ export default class FramesView extends DomainViews {
itemView: FrameWrapView, constructor(opts = {}, config) {
autoAdd: 1, super(opts, config, true);
init() {
this.listenTo(this.collection, 'reset', this.render); this.listenTo(this.collection, 'reset', this.render);
}, }
onRemoveBefore(items, opts) { onRemoveBefore(items, opts) {
items.forEach(item => item.remove(opts)); items.forEach(item => item.remove(opts));
}, }
onRender() { onRender() {
const { config, $el } = this; const { config, $el } = this;
const { em } = config; const { em } = config;
em && $el.attr({ class: `${em.getConfig('stylePrefix')}frames` }); em && $el.attr({ class: `${em.getConfig('stylePrefix')}frames` });
} }
}); }
FramesView.prototype.itemView = FrameWrapView;

11
src/dom_components/view/ToolbarView.js

@ -1,11 +1,12 @@
import DomainViews from 'domain_abstract/view/DomainViews'; import DomainViews from 'domain_abstract/view/DomainViews';
import ToolbarButtonView from './ToolbarButtonView'; import ToolbarButtonView from './ToolbarButtonView';
export default DomainViews.extend({ export default class ToolbarView extends DomainViews {
itemView: ToolbarButtonView, constructor(opts = {}, config) {
super(opts, config);
initialize(opts = {}) {
this.config = { editor: opts.editor || '', em: opts.em }; this.config = { editor: opts.editor || '', em: opts.em };
this.listenTo(this.collection, 'reset', this.render); this.listenTo(this.collection, 'reset', this.render);
} }
}); }
ToolbarView.prototype.itemView = ToolbarButtonView;

37
src/domain_abstract/ui/Input.js

@ -2,37 +2,34 @@ import Backbone from 'backbone';
const $ = Backbone.$; const $ = Backbone.$;
export default Backbone.View.extend({ export default class Input extends Backbone.View {
events: {
change: 'handleChange',
},
template() { template() {
return `<span class="${this.holderClass()}"></span>`; return `<span class="${this.holderClass()}"></span>`;
}, }
inputClass() { inputClass() {
return `${this.ppfx}field`; return `${this.ppfx}field`;
}, }
holderClass() { holderClass() {
return `${this.ppfx}input-holder`; return `${this.ppfx}input-holder`;
}, }
initialize(opts = {}) { constructor(opts = {}) {
super(opts);
const ppfx = opts.ppfx || ''; const ppfx = opts.ppfx || '';
this.opts = opts; this.opts = opts;
this.ppfx = ppfx; this.ppfx = ppfx;
this.em = opts.target || {}; this.em = opts.target || {};
!opts.onChange && this.listenTo(this.model, 'change:value', this.handleModelChange); !opts.onChange && this.listenTo(this.model, 'change:value', this.handleModelChange);
}, }
/** /**
* Fired when the element of the property is updated * Fired when the element of the property is updated
*/ */
elementUpdated() { elementUpdated() {
this.model.trigger('el:change'); this.model.trigger('el:change');
}, }
/** /**
* Set value to the input element * Set value to the input element
@ -43,14 +40,14 @@ export default Backbone.View.extend({
let val = value || model.get('defaults'); let val = value || model.get('defaults');
const input = this.getInputEl(); const input = this.getInputEl();
input && (input.value = val); input && (input.value = val);
}, }
/** /**
* Updates the view when the model is changed * Updates the view when the model is changed
* */ * */
handleModelChange(model, value, opts) { handleModelChange(model, value, opts) {
this.setValue(value, opts); this.setValue(value, opts);
}, }
/** /**
* Handled when the view is changed * Handled when the view is changed
@ -60,11 +57,11 @@ export default Backbone.View.extend({
const value = this.getInputEl().value; const value = this.getInputEl().value;
this.__onInputChange(value); this.__onInputChange(value);
this.elementUpdated(); this.elementUpdated();
}, }
__onInputChange(value) { __onInputChange(value) {
this.model.set({ value }, { fromInput: 1 }); this.model.set({ value }, { fromInput: 1 });
}, }
/** /**
* Get the input element * Get the input element
@ -79,7 +76,7 @@ export default Backbone.View.extend({
} }
return this.inputEl.get(0); return this.inputEl.get(0);
}, }
render() { render() {
this.inputEl = null; this.inputEl = null;
@ -88,5 +85,9 @@ 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;
}, }
}); }
Input.prototype.events = {
change: 'handleChange',
};

22
src/domain_abstract/ui/InputColor.js

@ -12,7 +12,7 @@ const getColor = color => {
return name || cl.replace(/ /g, ''); return name || cl.replace(/ /g, '');
}; };
export default Input.extend({ export default class InputColor extends Input {
template() { template() {
const ppfx = this.ppfx; const ppfx = this.ppfx;
return ` return `
@ -23,28 +23,28 @@ export default Input.extend({
</div> </div>
</div> </div>
`; `;
}, }
inputClass() { inputClass() {
const ppfx = this.ppfx; const ppfx = this.ppfx;
return `${ppfx}field ${ppfx}field-color`; return `${ppfx}field ${ppfx}field-color`;
}, }
holderClass() { holderClass() {
return `${this.ppfx}input-holder`; return `${this.ppfx}input-holder`;
}, }
remove() { remove() {
Input.prototype.remove.apply(this, arguments); Input.prototype.remove.apply(this, arguments);
this.colorEl.spectrum('destroy'); this.colorEl.spectrum('destroy');
}, }
handleChange(e) { handleChange(e) {
e.stopPropagation(); e.stopPropagation();
const { value } = e.target; const { value } = e.target;
if (isUndefined(value)) return; if (isUndefined(value)) return;
this.__onInputChange(value); this.__onInputChange(value);
}, }
__onInputChange(val) { __onInputChange(val) {
const { model, opts } = this; const { model, opts } = this;
@ -61,7 +61,7 @@ export default Input.extend({
} }
onChange ? onChange(value) : model.set({ value }, { fromInput: 1 }); onChange ? onChange(value) : model.set({ value }, { fromInput: 1 });
}, }
/** /**
* Set value to the model * Set value to the model
@ -83,7 +83,7 @@ export default Input.extend({
colorEl.spectrum('set', valueClr); colorEl.spectrum('set', valueClr);
this.noneColor = value == 'none'; this.noneColor = value == 'none';
} }
}, }
/** /**
* Get the color input element * Get the color input element
@ -162,12 +162,12 @@ export default Input.extend({
this.colorEl = colorEl; this.colorEl = colorEl;
} }
return this.colorEl; return this.colorEl;
}, }
render() { render() {
Input.prototype.render.call(this); Input.prototype.render.call(this);
// 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;
}, }
}); }

62
src/domain_abstract/ui/InputNumber.js

@ -5,16 +5,7 @@ import Input from './Input';
const $ = Backbone.$; const $ = Backbone.$;
export default Input.extend({ export default class InputNumber extends Input {
events: {
'change input': 'handleChange',
'change select': 'handleUnitChange',
'click [data-arrow-up]': 'upArrowClick',
'click [data-arrow-down]': 'downArrowClick',
'mousedown [data-arrows]': 'downIncrement',
keydown: 'handleKeyDown',
},
template() { template() {
const ppfx = this.ppfx; const ppfx = this.ppfx;
return ` return `
@ -25,19 +16,19 @@ export default Input.extend({
<div class="${ppfx}field-arrow-d" data-arrow-down></div> <div class="${ppfx}field-arrow-d" data-arrow-down></div>
</div> </div>
`; `;
}, }
inputClass() { inputClass() {
const ppfx = this.ppfx; const ppfx = this.ppfx;
return this.opts.contClass || `${ppfx}field ${ppfx}field-integer`; return this.opts.contClass || `${ppfx}field ${ppfx}field-integer`;
}, }
initialize(opts = {}) { constructor(opts = {}) {
Input.prototype.initialize.apply(this, arguments); super(opts);
bindAll(this, 'moveIncrement', 'upIncrement'); bindAll(this, 'moveIncrement', 'upIncrement');
this.doc = document; this.doc = document;
this.listenTo(this.model, 'change:unit', this.handleModelChange); this.listenTo(this.model, 'change:unit', this.handleModelChange);
}, }
/** /**
* Set value to the model * Set value to the model
@ -61,7 +52,7 @@ export default Input.extend({
if (opt.silent) { if (opt.silent) {
this.handleModelChange(); this.handleModelChange();
} }
}, }
/** /**
* Handled when the view is changed * Handled when the view is changed
@ -70,7 +61,7 @@ export default Input.extend({
e.stopPropagation(); e.stopPropagation();
this.setValue(this.getInputEl().value); this.setValue(this.getInputEl().value);
this.elementUpdated(); this.elementUpdated();
}, }
/** /**
* Handled when the view is changed * Handled when the view is changed
@ -80,7 +71,7 @@ export default Input.extend({
var value = this.getUnitEl().value; var value = this.getUnitEl().value;
this.model.set('unit', value); this.model.set('unit', value);
this.elementUpdated(); this.elementUpdated();
}, }
/** /**
* Handled when user uses keyboard * Handled when user uses keyboard
@ -95,14 +86,14 @@ export default Input.extend({
e.preventDefault(); e.preventDefault();
this.downArrowClick(); this.downArrowClick();
} }
}, }
/** /**
* Fired when the element of the property is updated * Fired when the element of the property is updated
*/ */
elementUpdated() { elementUpdated() {
this.model.trigger('el:change'); this.model.trigger('el:change');
}, }
/** /**
* Updates the view when the model is changed * Updates the view when the model is changed
@ -112,7 +103,7 @@ export default Input.extend({
this.getInputEl().value = model.get('value'); this.getInputEl().value = model.get('value');
const unitEl = this.getUnitEl(); const unitEl = this.getUnitEl();
unitEl && (unitEl.value = model.get('unit') || ''); unitEl && (unitEl.value = model.get('unit') || '');
}, }
/** /**
* Get the unit element * Get the unit element
@ -138,7 +129,7 @@ export default Input.extend({
} }
return this.unitEl; return this.unitEl;
}, }
/** /**
* Invoked when the up arrow is clicked * Invoked when the up arrow is clicked
@ -149,7 +140,7 @@ export default Input.extend({
let value = parseFloat(model.get('value')); let value = parseFloat(model.get('value'));
this.setValue(this.normalizeValue(value + step)); this.setValue(this.normalizeValue(value + step));
this.elementUpdated(); this.elementUpdated();
}, }
/** /**
* Invoked when the down arrow is clicked * Invoked when the down arrow is clicked
@ -160,7 +151,7 @@ export default Input.extend({
const value = parseFloat(model.get('value')); const value = parseFloat(model.get('value'));
this.setValue(this.normalizeValue(value - step)); this.setValue(this.normalizeValue(value - step));
this.elementUpdated(); this.elementUpdated();
}, }
/** /**
* Change easily integer input value with click&drag method * Change easily integer input value with click&drag method
@ -176,7 +167,7 @@ export default Input.extend({
this.current = { y: e.pageY, val: value }; this.current = { y: e.pageY, val: value };
on(this.doc, 'mousemove', this.moveIncrement); on(this.doc, 'mousemove', this.moveIncrement);
on(this.doc, 'mouseup', this.upIncrement); on(this.doc, 'mouseup', this.upIncrement);
}, }
/** While the increment is clicked, moving the mouse will update input value /** While the increment is clicked, moving the mouse will update input value
* @param Object * @param Object
@ -193,7 +184,7 @@ export default Input.extend({
this.prValue = value; this.prValue = value;
model.set({ value, unit }, { avoidStore: 1 }); model.set({ value, unit }, { avoidStore: 1 });
return false; return false;
}, }
/** /**
* Stop moveIncrement method * Stop moveIncrement method
@ -209,7 +200,7 @@ export default Input.extend({
model.set('value', value, { avoidStore: 1 }).set('value', value + step); model.set('value', value, { avoidStore: 1 }).set('value', value + step);
this.elementUpdated(); this.elementUpdated();
} }
}, }
normalizeValue(value, defValue = 0) { normalizeValue(value, defValue = 0) {
const model = this.model; const model = this.model;
@ -228,7 +219,7 @@ export default Input.extend({
} }
return stepDecimals ? parseFloat(value.toFixed(stepDecimals)) : value; return stepDecimals ? parseFloat(value.toFixed(stepDecimals)) : value;
}, }
/** /**
* Validate input value * Validate input value
@ -281,7 +272,7 @@ export default Input.extend({
value: val, value: val,
unit, unit,
}; };
}, }
render() { render() {
Input.prototype.render.call(this); Input.prototype.render.call(this);
@ -289,5 +280,14 @@ export default Input.extend({
const unit = this.getUnitEl(); const unit = this.getUnitEl();
unit && this.$el.find(`.${this.ppfx}field-units`).get(0).appendChild(unit); unit && this.$el.find(`.${this.ppfx}field-units`).get(0).appendChild(unit);
return this; return this;
}, }
}); }
InputNumber.prototype.events = {
'change input': 'handleChange',
'change select': 'handleUnitChange',
'click [data-arrow-up]': 'upArrowClick',
'click [data-arrow-down]': 'downArrowClick',
'mousedown [data-arrows]': 'downIncrement',
keydown: 'handleKeyDown',
};

52
src/domain_abstract/view/DomainViews.js

@ -1,25 +1,20 @@
import { includes } from 'underscore'; import { includes } from 'underscore';
import Backbone from 'backbone'; import Backbone from 'backbone';
export default Backbone.View.extend({ export default class DomainViews extends Backbone.View {
// Default view
itemView: '',
// Defines the View per type // Defines the View per type
itemsView: '', itemsView = '';
itemType: 'type', itemType = 'type';
autoAdd: 0, reuseView = false;
initialize(opts = {}, config) { constructor(opts = {}, config, autoAdd = false) {
super(opts);
this.config = config || opts.config || {}; this.config = config || opts.config || {};
this.autoAdd && this.listenTo(this.collection, 'add', this.addTo); autoAdd && this.listenTo(this.collection, 'add', this.addTo);
this.items = []; this.items = [];
this.init(); }
},
init() {},
/** /**
* Add new model to the collection * Add new model to the collection
@ -28,14 +23,14 @@ export default Backbone.View.extend({
* */ * */
addTo(model) { addTo(model) {
this.add(model); this.add(model);
}, }
itemViewNotFound(type) { itemViewNotFound(type) {
const { config, ns } = this; const { config, ns } = this;
const { em } = config; const { em } = config;
const warn = `${ns ? `[${ns}]: ` : ''}'${type}' type not found`; const warn = `${ns ? `[${ns}]: ` : ''}'${type}' type not found`;
em && em.logWarning(warn); em && em.logWarning(warn);
}, }
/** /**
* Render new model inside the view * Render new model inside the view
@ -67,7 +62,7 @@ export default Backbone.View.extend({
'text', 'text',
'time', 'time',
'url', 'url',
'week' 'week',
]; ];
var frag = fragment || null; var frag = fragment || null;
var itemView = this.itemView; var itemView = this.itemView;
@ -76,11 +71,7 @@ export default Backbone.View.extend({
if (itemsView[typeField]) { if (itemsView[typeField]) {
itemView = itemsView[typeField]; itemView = itemsView[typeField];
} else if ( } else if (typeField && !itemsView[typeField] && !includes(inputTypes, typeField)) {
typeField &&
!itemsView[typeField] &&
!includes(inputTypes, typeField)
) {
this.itemViewNotFound(typeField); this.itemViewNotFound(typeField);
} }
@ -95,7 +86,7 @@ export default Backbone.View.extend({
if (frag) frag.appendChild(rendered); if (frag) frag.appendChild(rendered);
else this.$el.append(rendered); else this.$el.append(rendered);
}, }
render() { render() {
var frag = document.createDocumentFragment(); var frag = document.createDocumentFragment();
@ -103,19 +94,19 @@ export default Backbone.View.extend({
this.$el.empty(); this.$el.empty();
if (this.collection.length) if (this.collection.length)
this.collection.each(function(model) { this.collection.each(function (model) {
this.add(model, frag); this.add(model, frag);
}, this); }, this);
this.$el.append(frag); this.$el.append(frag);
this.onRender(); this.onRender();
return this; return this;
}, }
onRender() {}, onRender() {}
onRemoveBefore() {}, onRemoveBefore() {}
onRemove() {}, onRemove() {}
remove(opts = {}) { remove(opts = {}) {
const { items } = this; const { items } = this;
@ -123,7 +114,7 @@ export default Backbone.View.extend({
this.clearItems(); this.clearItems();
Backbone.View.prototype.remove.apply(this, arguments); Backbone.View.prototype.remove.apply(this, arguments);
this.onRemove(items, opts); this.onRemove(items, opts);
}, }
clearItems() { clearItems() {
const items = this.items || []; const items = this.items || [];
@ -131,4 +122,7 @@ export default Backbone.View.extend({
// items.forEach(item => item.remove()); // items.forEach(item => item.remove());
// this.items = []; // this.items = [];
} }
}); }
// Default view
DomainViews.prototype.itemView = '';

17
src/trait_manager/index.js

@ -142,14 +142,15 @@ export default () => {
let { view } = this; let { view } = this;
const config = this.getConfig(); const config = this.getConfig();
const el = view && view.el; const el = view && view.el;
view = new TraitsView({ view = new TraitsView(
el, {
collection: [], el,
editor: config.em, collection: [],
config, editor: config.em,
}); config,
view.itemsView = this.getTypes(); },
view.updatedCollection(); this.getTypes()
);
this.view = view; this.view = view;
return view.el; return view.el;
}, },

14
src/trait_manager/view/TraitButtonView.js

@ -1,14 +1,14 @@
import { isString } from 'underscore'; import { isString } from 'underscore';
import TraitView from './TraitView'; import TraitView from './TraitView';
export default TraitView.extend({ export default class TraitButtonView extends TraitView {
eventCapture: ['click button'], eventCapture = ['click button'];
templateInput: '', templateInput = '';
onChange() { onChange() {
this.handleClick(); this.handleClick();
}, }
handleClick() { handleClick() {
const { model, em } = this; const { model, em } = this;
@ -21,13 +21,13 @@ export default TraitView.extend({
command(em.get('Editor'), model); command(em.get('Editor'), model);
} }
} }
}, }
renderLabel() { renderLabel() {
if (this.model.get('label')) { if (this.model.get('label')) {
TraitView.prototype.renderLabel.apply(this, arguments); TraitView.prototype.renderLabel.apply(this, arguments);
} }
}, }
getInputEl() { getInputEl() {
const { model, ppfx } = this; const { model, ppfx } = this;
@ -39,4 +39,4 @@ export default TraitView.extend({
}">${label}</button>`; }">${label}</button>`;
return input; return input;
} }
}); }

12
src/trait_manager/view/TraitCheckboxView.js

@ -1,15 +1,15 @@
import { isUndefined } from 'underscore'; import { isUndefined } from 'underscore';
import TraitView from './TraitView'; import TraitView from './TraitView';
export default TraitView.extend({ export default class TraitCheckboxView extends TraitView {
appendInput: 0, appendInput = false;
templateInput() { templateInput() {
const { ppfx, clsField } = this; const { ppfx, clsField } = this;
return `<label class="${clsField}" data-input> return `<label class="${clsField}" data-input>
<i class="${ppfx}chk-icon"></i> <i class="${ppfx}chk-icon"></i>
</label>`; </label>`;
}, }
/** /**
* Fires when the input is changed * Fires when the input is changed
@ -18,7 +18,7 @@ export default TraitView.extend({
onChange() { onChange() {
const value = this.getInputElem().checked; const value = this.getInputElem().checked;
this.model.set('value', this.getCheckedValue(value)); this.model.set('value', this.getCheckedValue(value));
}, }
getCheckedValue(checked) { getCheckedValue(checked) {
let result = checked; let result = checked;
@ -33,7 +33,7 @@ export default TraitView.extend({
} }
return result; return result;
}, }
/** /**
* Returns input element * Returns input element
@ -67,4 +67,4 @@ export default TraitView.extend({
return el; return el;
} }
}); }

8
src/trait_manager/view/TraitColorView.js

@ -1,8 +1,8 @@
import TraitView from './TraitView'; import TraitView from './TraitView';
import InputColor from 'domain_abstract/ui/InputColor'; import InputColor from 'domain_abstract/ui/InputColor';
export default TraitView.extend({ export default class TraitColorView extends TraitView {
templateInput: '', templateInput = '';
/** /**
* Returns input element * Returns input element
@ -17,7 +17,7 @@ export default TraitView.extend({
model, model,
target: this.config.em, target: this.config.em,
contClass: this.ppfx + 'field-color', contClass: this.ppfx + 'field-color',
ppfx: this.ppfx ppfx: this.ppfx,
}); });
const input = inputColor.render(); const input = inputColor.render();
input.setValue(value, { fromTarget: 1 }); input.setValue(value, { fromTarget: 1 });
@ -26,4 +26,4 @@ export default TraitView.extend({
return this.input; return this.input;
} }
}); }

12
src/trait_manager/view/TraitNumberView.js

@ -2,14 +2,12 @@ import TraitView from './TraitView';
import { isUndefined } from 'underscore'; import { isUndefined } from 'underscore';
import InputNumber from 'domain_abstract/ui/InputNumber'; import InputNumber from 'domain_abstract/ui/InputNumber';
export default TraitView.extend({ export default class TraitNumberView extends TraitView {
getValueForTarget() { getValueForTarget() {
const { model } = this; const { model } = this;
const { value, unit } = model.attributes; const { value, unit } = model.attributes;
return !isUndefined(value) && value !== '' return !isUndefined(value) && value !== '' ? value + unit : model.get('default');
? value + unit }
: model.get('default');
},
/** /**
* Returns input element * Returns input element
@ -24,7 +22,7 @@ export default TraitView.extend({
contClass: `${ppfx}field-int`, contClass: `${ppfx}field-int`,
type: 'number', type: 'number',
model: model, model: model,
ppfx ppfx,
}); });
this.input = inputNumber.render(); this.input = inputNumber.render();
this.$input = this.input.inputEl; this.$input = this.input.inputEl;
@ -35,4 +33,4 @@ export default TraitView.extend({
} }
return this.input; return this.input;
} }
}); }

19
src/trait_manager/view/TraitSelectView.js

@ -4,10 +4,11 @@ import TraitView from './TraitView';
const $ = Backbone.$; const $ = Backbone.$;
export default TraitView.extend({ export default class TraitSelectView extends TraitView {
init() { constructor(o = {}) {
super(o);
this.listenTo(this.model, 'change:options', this.rerender); this.listenTo(this.model, 'change:options', this.rerender);
}, }
templateInput() { templateInput() {
const { ppfx, clsField } = this; const { ppfx, clsField } = this;
@ -17,7 +18,7 @@ export default TraitView.extend({
<div class="${ppfx}d-s-arrow"></div> <div class="${ppfx}d-s-arrow"></div>
</div> </div>
</div>`; </div>`;
}, }
/** /**
* Returns input element * Returns input element
@ -41,15 +42,11 @@ export default TraitView.extend({
value = el; value = el;
} else { } else {
name = el.name || el.label || el.value; name = el.name || el.label || el.value;
value = `${isUndefined(el.value) ? el.id : el.value}`.replace( value = `${isUndefined(el.value) ? el.id : el.value}`.replace(/"/g, '&quot;');
/"/g,
'&quot;'
);
style = el.style ? el.style.replace(/"/g, '&quot;') : ''; style = el.style ? el.style.replace(/"/g, '&quot;') : '';
attrs += style ? ` style="${style}"` : ''; attrs += style ? ` style="${style}"` : '';
} }
const resultName = const resultName = em.t(`traitManager.traits.options.${propName}.${value}`) || name;
em.t(`traitManager.traits.options.${propName}.${value}`) || name;
input += `<option value="${value}"${attrs}>${resultName}</option>`; input += `<option value="${value}"${attrs}>${resultName}</option>`;
values.push(value); values.push(value);
}); });
@ -63,4 +60,4 @@ export default TraitView.extend({
return this.$input.get(0); return this.$input.get(0);
} }
}); }

87
src/trait_manager/view/TraitView.js

@ -4,28 +4,29 @@ import { capitalize } from 'utils/mixins';
const $ = Backbone.$; const $ = Backbone.$;
export default Backbone.View.extend({ export default class TraitView extends Backbone.View {
events: {}, events = {};
eventCapture: ['change'], // protected eventCapture = ['change'];
appendInput: 1, appendInput = true;
attributes() { attributes() {
return this.model.get('attributes'); return this.model.get('attributes');
}, }
templateLabel() { templateLabel() {
const { ppfx } = this; const { ppfx } = this;
const label = this.getLabel(); const label = this.getLabel();
return `<div class="${ppfx}label" title="${label}">${label}</div>`; return `<div class="${ppfx}label" title="${label}">${label}</div>`;
}, }
templateInput() { templateInput() {
const { clsField } = this; const { clsField } = this;
return `<div class="${clsField}" data-input></div>`; return `<div class="${clsField}" data-input></div>`;
}, }
initialize(o = {}) { constructor(o = {}) {
super(o);
const { config = {} } = o; const { config = {} } = o;
const { model, eventCapture } = this; const { model, eventCapture } = this;
const { target } = model; const { target } = model;
@ -39,7 +40,7 @@ export default Backbone.View.extend({
this.clsField = `${ppfx}field ${ppfx}field-${type}`; this.clsField = `${ppfx}field ${ppfx}field-${type}`;
[ [
['change:value', this.onValueChange], ['change:value', this.onValueChange],
['remove', this.removeView] ['remove', this.removeView],
].forEach(([event, clb]) => { ].forEach(([event, clb]) => {
model.off(event, clb); model.off(event, clb);
this.listenTo(model, event, clb); this.listenTo(model, event, clb);
@ -51,26 +52,26 @@ export default Backbone.View.extend({
eventCapture.forEach(event => (this.events[event] = 'onChange')); eventCapture.forEach(event => (this.events[event] = 'onChange'));
this.delegateEvents(); this.delegateEvents();
this.init(); this.init();
}, }
getClbOpts() { getClbOpts() {
return { return {
component: this.target, component: this.target,
trait: this.model, trait: this.model,
elInput: this.getInputElem() elInput: this.getInputElem(),
}; };
}, }
removeView() { removeView() {
this.remove(); this.remove();
this.removed(); this.removed();
}, }
init() {}, init() {}
removed() {}, removed() {}
onRender() {}, onRender() {}
onUpdate() {}, onUpdate() {}
onEvent() {}, onEvent() {}
/** /**
* Fires when the input is changed * Fires when the input is changed
@ -83,18 +84,18 @@ export default Backbone.View.extend({
} }
this.onEvent({ this.onEvent({
...this.getClbOpts(), ...this.getClbOpts(),
event event,
}); });
}, }
getValueForTarget() { getValueForTarget() {
return this.model.get('value'); return this.model.get('value');
}, }
setInputValue(value) { setInputValue(value) {
const el = this.getInputElem(); const el = this.getInputElem();
el && (el.value = value); el && (el.value = value);
}, }
/** /**
* On change callback * On change callback
@ -108,7 +109,7 @@ export default Backbone.View.extend({
const val = this.getValueForTarget(); const val = this.getValueForTarget();
model.setTargetValue(val, opts); model.setTargetValue(val, opts);
} }
}, }
/** /**
* Render label * Render label
@ -124,12 +125,12 @@ export default Backbone.View.extend({
this.createLabel({ this.createLabel({
label, label,
component: target, component: target,
trait: this trait: this,
}) || ''; }) || '';
} }
$el.find('[data-label]').append(tpl); $el.find('[data-label]').append(tpl);
}, }
/** /**
* Returns label for the input * Returns label for the input
@ -139,18 +140,15 @@ export default Backbone.View.extend({
getLabel() { getLabel() {
const { em } = this; const { em } = this;
const { label, name } = this.model.attributes; const { label, name } = this.model.attributes;
return ( return em.t(`traitManager.traits.labels.${name}`) || capitalize(label || name).replace(/-/g, ' ');
em.t(`traitManager.traits.labels.${name}`) || }
capitalize(label || name).replace(/-/g, ' ')
);
},
/** /**
* Returns current target component * Returns current target component
*/ */
getComponent() { getComponent() {
return this.target; return this.target;
}, }
/** /**
* Returns input element * Returns input element
@ -187,14 +185,12 @@ export default Backbone.View.extend({
this.$input = input; this.$input = input;
} }
return this.$input.get(0); return this.$input.get(0);
}, }
getInputElem() { getInputElem() {
const { input, $input } = this; const { input, $input } = this;
return ( return input || ($input && $input.get && $input.get(0)) || this.getElInput();
input || ($input && $input.get && $input.get(0)) || this.getElInput() }
);
},
getModelValue() { getModelValue() {
let value; let value;
@ -210,11 +206,11 @@ export default Backbone.View.extend({
} }
return !isUndefined(value) ? value : ''; return !isUndefined(value) ? value : '';
}, }
getElInput() { getElInput() {
return this.elInput; return this.elInput;
}, }
/** /**
* Renders input * Renders input
@ -227,9 +223,7 @@ export default Backbone.View.extend({
let tpl = model.el; let tpl = model.el;
if (!tpl) { if (!tpl) {
tpl = this.createInput tpl = this.createInput ? this.createInput(this.getClbOpts()) : this.getInputEl();
? this.createInput(this.getClbOpts())
: this.getInputEl();
} }
if (isString(tpl)) { if (isString(tpl)) {
@ -241,21 +235,21 @@ export default Backbone.View.extend({
} }
model.el = this.elInput; model.el = this.elInput;
}, }
hasLabel() { hasLabel() {
const { label } = this.model.attributes; const { label } = this.model.attributes;
return !this.noLabel && label !== false; return !this.noLabel && label !== false;
}, }
rerender() { rerender() {
this.model.el = null; this.model.el = null;
this.render(); this.render();
}, }
postUpdate() { postUpdate() {
this.onUpdate(this.getClbOpts()); this.onUpdate(this.getClbOpts());
}, }
render() { render() {
const { $el, pfx, ppfx, model } = this; const { $el, pfx, ppfx, model } = this;
@ -283,4 +277,5 @@ export default Backbone.View.extend({
this.onRender(this.getClbOpts()); this.onRender(this.getClbOpts());
return this; return this;
} }
}); }
TraitView.prototype.eventCapture = ['change'];

17
src/trait_manager/view/TraitsView.js

@ -1,12 +1,12 @@
import DomainViews from 'domain_abstract/view/DomainViews'; import DomainViews from 'domain_abstract/view/DomainViews';
import TraitView from './TraitView'; import TraitView from './TraitView';
export default DomainViews.extend({ export default class TraitsView extends DomainViews {
ns: 'Traits', reuseView = true;
itemView: TraitView,
reuseView: 1,
initialize(o = {}) { constructor(o = {}, itemsView) {
super(o);
this.itemView = itemsView;
const config = o.config || {}; const config = o.config || {};
const pfx = config.stylePrefix || ''; const pfx = config.stylePrefix || '';
const em = o.editor; const em = o.editor;
@ -16,7 +16,8 @@ export default DomainViews.extend({
this.ppfx = config.pStylePrefix || ''; this.ppfx = config.pStylePrefix || '';
this.className = `${pfx}traits`; this.className = `${pfx}traits`;
this.listenTo(em, 'component:toggled', this.updatedCollection); this.listenTo(em, 'component:toggled', this.updatedCollection);
}, this.updatedCollection();
}
/** /**
* Update view collection * Update view collection
@ -29,4 +30,6 @@ export default DomainViews.extend({
this.collection = comp ? comp.get('traits') : []; this.collection = comp ? comp.get('traits') : [];
this.render(); this.render();
} }
}); }
TraitView.prototype.itemView = TraitView;

Loading…
Cancel
Save