|
|
|
@ -1,55 +1,80 @@ |
|
|
|
import Backbone from 'backbone'; |
|
|
|
import { Model } from 'common'; |
|
|
|
import { isUndefined } from 'underscore'; |
|
|
|
|
|
|
|
export default Backbone.Model.extend({ |
|
|
|
defaults: { |
|
|
|
type: 'text', // text, number, range, select
|
|
|
|
label: '', |
|
|
|
name: '', |
|
|
|
min: '', |
|
|
|
max: '', |
|
|
|
unit: '', |
|
|
|
step: 1, |
|
|
|
value: '', |
|
|
|
target: '', |
|
|
|
default: '', |
|
|
|
placeholder: '', |
|
|
|
changeProp: 0, |
|
|
|
options: [] |
|
|
|
}, |
|
|
|
|
|
|
|
export default class Trait extends Model { |
|
|
|
initialize() { |
|
|
|
const target = this.get('target'); |
|
|
|
const name = this.get('name'); |
|
|
|
const changeProp = this.get('changeProp'); |
|
|
|
const { target, name, changeProp } = this.attributes; |
|
|
|
!this.get('id') && this.set('id', name); |
|
|
|
|
|
|
|
if (target) { |
|
|
|
this.target = target; |
|
|
|
this.unset('target'); |
|
|
|
const targetEvent = changeProp |
|
|
|
? `change:${name}` |
|
|
|
: `change:attributes:${name}`; |
|
|
|
const targetEvent = changeProp ? `change:${name}` : `change:attributes:${name}`; |
|
|
|
this.listenTo(target, targetEvent, this.targetUpdated); |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get trait id. |
|
|
|
* @returns {String} |
|
|
|
*/ |
|
|
|
getId() { |
|
|
|
return this.get('id'); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the trait type. |
|
|
|
* @returns {String} |
|
|
|
*/ |
|
|
|
getType() { |
|
|
|
return this.get('type'); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get trait name. |
|
|
|
* @returns {String} |
|
|
|
*/ |
|
|
|
getName() { |
|
|
|
return this.get('name'); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Return all the propeties |
|
|
|
* @returns {Object} |
|
|
|
* Get trait label. |
|
|
|
* @param {Object} [opts={}] Options |
|
|
|
* @param {Boolean} [opts.locale=true] Use the locale string from i18n module |
|
|
|
* @returns {String} |
|
|
|
*/ |
|
|
|
getLabel(opts = {}) { |
|
|
|
const { locale = true } = opts; |
|
|
|
const id = this.getId(); |
|
|
|
const name = this.get('label') || this.getName(); |
|
|
|
return (locale && this.em?.t(`traitManager.traits.labels.${id}`)) || name; |
|
|
|
} |
|
|
|
|
|
|
|
setValue(value, opts = {}) { |
|
|
|
const valueOpts = {}; |
|
|
|
if (opts.partial) { |
|
|
|
valueOpts.avoidStore = true; |
|
|
|
} |
|
|
|
this.setTargetValue(value, valueOpts); |
|
|
|
} |
|
|
|
|
|
|
|
props() { |
|
|
|
return this.attributes; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
targetUpdated() { |
|
|
|
const value = this.getTargetValue(); |
|
|
|
this.set({ value }, { fromTarget: 1 }); |
|
|
|
}, |
|
|
|
this.em?.trigger('trait:update', { |
|
|
|
trait: this, |
|
|
|
component: this.target, |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
getValue() { |
|
|
|
return this.getTargetValue(); |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
getTargetValue() { |
|
|
|
const name = this.get('name'); |
|
|
|
@ -63,7 +88,7 @@ export default Backbone.Model.extend({ |
|
|
|
} |
|
|
|
|
|
|
|
return !isUndefined(value) ? value : ''; |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
setTargetValue(value, opts = {}) { |
|
|
|
const target = this.target; |
|
|
|
@ -84,7 +109,7 @@ export default Backbone.Model.extend({ |
|
|
|
attrs[name] = valueToSet; |
|
|
|
target.set('attributes', attrs, opts); |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
setValueFromInput(value, final = 1, opts = {}) { |
|
|
|
const toSet = { value }; |
|
|
|
@ -95,12 +120,8 @@ export default Backbone.Model.extend({ |
|
|
|
this.set('value', '', opts); |
|
|
|
this.set(toSet, opts); |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the initial value of the trait |
|
|
|
* @return {string} |
|
|
|
*/ |
|
|
|
getInitValue() { |
|
|
|
const target = this.target; |
|
|
|
const name = this.get('name'); |
|
|
|
@ -113,4 +134,20 @@ export default Backbone.Model.extend({ |
|
|
|
|
|
|
|
return value || this.get('value') || this.get('default'); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
Trait.prototype.defaults = { |
|
|
|
type: 'text', // text, number, range, select
|
|
|
|
label: '', |
|
|
|
name: '', |
|
|
|
min: '', |
|
|
|
max: '', |
|
|
|
unit: '', |
|
|
|
step: 1, |
|
|
|
value: '', |
|
|
|
target: '', |
|
|
|
default: '', |
|
|
|
placeholder: '', |
|
|
|
changeProp: 0, |
|
|
|
options: [], |
|
|
|
}; |
|
|
|
|