Browse Source

Up trait model

pull/5214/head
Artur Arseniev 3 years ago
parent
commit
a871f54411
  1. 10
      src/trait_manager/index.ts
  2. 57
      src/trait_manager/model/Trait.ts

10
src/trait_manager/index.ts

@ -11,6 +11,7 @@ import TraitColorView from './view/TraitColorView';
import TraitButtonView from './view/TraitButtonView';
import EditorModel from '../editor/model/Editor';
import Component from '../dom_components/model/Component';
import Trait from './model/Trait';
export const evAll = 'trait';
export const evPfx = `${evAll}:`;
@ -89,11 +90,14 @@ export default class TraitManager extends Module<TraitManagerConfig & { pStylePr
this.__trgCustom();
}
getSelected() {
return this.model.get('component') || null;
getSelected(): Component | undefined {
return this.model.get('component');
}
getCurrent() {
/**
* Get traits from the currently selected component.
*/
getCurrent(): Trait[] {
return this.model.get('traits') || [];
}

57
src/trait_manager/model/Trait.ts

@ -1,9 +1,10 @@
import { isUndefined } from 'underscore';
import { isString, isUndefined } from 'underscore';
import { Model, SetOptions } from '../../common';
import Component from '../../dom_components/model/Component';
import Editor from '../../editor';
import EditorModel from '../../editor/model/Editor';
import TraitView from '../view/TraitView';
import { isDef } from '../../utils/mixins';
/** @private */
export interface TraitProperties {
@ -55,6 +56,11 @@ export interface TraitProperties {
setValue?: (props: { value: any; editor: Editor; trait: Trait; component: Component; partial: boolean }) => void;
}
type TraitOption = {
id: string;
label?: string;
};
/**
* @typedef Trait
* @property {String} id Trait id, eg. `my-trait-id`.
@ -187,6 +193,55 @@ export default class Trait extends Model<TraitProperties> {
}
}
/**
* Get default value.
*/
getDefault() {
return this.get('default');
}
/**
* Get trait options.
*/
getOptions(): TraitOption[] {
return (this.get('options') as TraitOption[]) || [];
}
/**
* Get current selected option or by id.
* @param {String} [id] Option id.
* @returns {Object | null}
*/
getOption(id?: string): TraitOption | undefined {
const idSel = isDef(id) ? id : this.getValue();
return this.getOptions().filter(o => this.getOptionId(o) === idSel)[0];
}
/**
* Get the option id from the option object.
* @param {Object} option Option object
* @returns {String} Option id
*/
getOptionId(option: TraitOption) {
return option.id || (option as any).value;
}
/**
* Get option label.
* @param {String|Object} id Option id or the option object
* @param {Object} [opts={}] Options
* @param {Boolean} [opts.locale=true] Use the locale string from i18n module
* @returns {String} Option label
*/
getOptionLabel(id: string | TraitOption, opts: { locale?: boolean } = {}): string {
const { locale = true } = opts;
const option = (isString(id) ? this.getOption(id) : id)!;
const optId = this.getOptionId(option);
const label = option.label || (option as any).name || optId;
const propName = this.getName();
return (locale && this.em?.t(`traitManager.traits.options.${propName}.${optId}`)) || label;
}
props() {
return this.attributes;
}

Loading…
Cancel
Save