Browse Source

Add getCategoryLabel to Traits

pull/5642/head
Artur Arseniev 2 years ago
parent
commit
cb6d047696
  1. 4
      src/abstract/ModuleCategory.ts
  2. 2
      src/common/index.ts
  3. 25
      src/trait_manager/model/Trait.ts
  4. 4
      src/trait_manager/view/TraitsView.ts

4
src/abstract/ModuleCategory.ts

@ -41,4 +41,8 @@ export default class Category extends Model<CategoryProperties> {
getId() {
return this.get('id')!;
}
getLabel() {
return this.get('label')!;
}
}

2
src/common/index.ts

@ -11,6 +11,8 @@ export type AddOptions = Backbone.AddOptions & { temporary?: boolean };
export type DisableOptions = { fromMove?: boolean };
export type LocaleOptions = { locale?: boolean };
export type RemoveOptions = Backbone.Silenceable;
export type EventHandler = Backbone.EventHandler;

25
src/trait_manager/model/Trait.ts

@ -1,11 +1,11 @@
import { isString, isUndefined } from 'underscore';
import { Model, SetOptions } from '../../common';
import { LocaleOptions, 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';
import { CategoryProperties } from '../../abstract/ModuleCategory';
import Category, { CategoryProperties } from '../../abstract/ModuleCategory';
import Traits from './Traits';
/** @private */
@ -128,6 +128,11 @@ export default class Trait extends Model<TraitProperties> {
return this.collection as unknown as Traits;
}
get category(): Category | undefined {
const cat = this.get('category');
return cat instanceof Category ? cat : undefined;
}
setTarget(target: Component) {
if (target) {
const { name, changeProp, value: initValue, getValue } = this.attributes;
@ -265,7 +270,7 @@ export default class Trait extends Model<TraitProperties> {
* @param {Boolean} [opts.locale=true] Use the locale string from i18n module
* @returns {String} Option label
*/
getOptionLabel(id: string | TraitOption, opts: { locale?: boolean } = {}): string {
getOptionLabel(id: string | TraitOption, opts: LocaleOptions = {}): string {
const { locale = true } = opts;
const option = (isString(id) ? this.getOption(id) : id)!;
const optId = this.getOptionId(option);
@ -274,6 +279,20 @@ export default class Trait extends Model<TraitProperties> {
return (locale && this.em?.t(`traitManager.traits.options.${propName}.${optId}`)) || label;
}
/**
* Get category label.
* @param {Object} [opts={}] Options.
* @param {Boolean} [opts.locale=true] Use the locale string from i18n module.
* @returns {String}
*/
getCategoryLabel(opts: LocaleOptions = {}): string {
const { em, category } = this;
const { locale = true } = opts;
const catId = category?.getId();
const catLabel = category?.getLabel();
return (locale && em?.t(`traitManager.categories.${catId}`)) || catLabel || '';
}
props() {
return this.attributes;
}

4
src/trait_manager/view/TraitsView.ts

@ -1,13 +1,11 @@
import { isObject, isString } from 'underscore';
import TraitManager from '..';
import Categories from '../../abstract/ModuleCategories';
import CategoryView from '../../abstract/ModuleCategoryView';
import DomainViews from '../../domain_abstract/view/DomainViews';
import EditorModel from '../../editor/model/Editor';
import Trait from '../model/Trait';
import Traits from '../model/Traits';
import { TraitManagerConfigModule } from '../types';
import TraitView from './TraitView';
import Traits from '../model/Traits';
interface TraitsViewProps {
el?: HTMLElement;

Loading…
Cancel
Save