Browse Source

Move SectorsView to TS

pull/4849/head
Artur Arseniev 4 years ago
parent
commit
3b16026556
  1. 8
      src/style_manager/view/SectorView.ts
  2. 35
      src/style_manager/view/SectorsView.ts
  3. 2
      src/utils/dom.ts

8
src/style_manager/view/SectorView.ts

@ -4,18 +4,18 @@ import html from '../../utils/html';
import Sector from '../model/Sector'; import Sector from '../model/Sector';
import PropertiesView from './PropertiesView'; import PropertiesView from './PropertiesView';
type Config = { export type SectorViewConfig = {
em: EditorModel; em: EditorModel;
stylePrefix?: string; stylePrefix?: string;
pStylePrefix?: string;
}; };
export default class SectorView extends View<Sector> { export default class SectorView extends View<Sector> {
em: EditorModel; em: EditorModel;
config: Config; config: SectorViewConfig;
pfx: string; pfx: string;
constructor(o: { config: Config }) { constructor(o: { config: SectorViewConfig; model?: Sector }) {
// @ts-ignore
super(o); super(o);
const config = o.config || {}; const config = o.config || {};
const { model } = this; const { model } = this;

35
src/style_manager/view/SectorsView.js → src/style_manager/view/SectorsView.ts

@ -1,29 +1,42 @@
import { View } from '../../common'; import { View } from '../../common';
import { appendAtIndex } from '../../utils/dom'; import { appendAtIndex } from '../../utils/dom';
import SectorView from './SectorView'; import Sector from '../model/Sector';
import SectorView, { SectorViewConfig } from './SectorView';
export default class SectorsView extends View { export default class SectorsView extends View {
initialize(o = {}) { pfx: string;
const { module, config = {} } = o; ppfx: string;
config: SectorViewConfig;
module: any;
constructor(o: { module?: any; config?: SectorViewConfig } = {}) {
// @ts-ignore
super(o);
const { module, config } = o;
const coll = this.collection; const coll = this.collection;
this.pfx = config.stylePrefix || ''; this.pfx = config?.stylePrefix || '';
this.ppfx = config.pStylePrefix || ''; this.ppfx = config?.pStylePrefix || '';
this.config = config; this.config = config!;
this.module = module; this.module = module!;
this.listenTo(coll, 'add', this.addTo); this.listenTo(coll, 'add', this.addTo);
this.listenTo(coll, 'reset', this.render); this.listenTo(coll, 'reset', this.render);
} }
remove() { remove() {
View.prototype.remove.apply(this, arguments); View.prototype.remove.apply(this, arguments as any);
['config', 'module', 'em'].forEach(i => (this[i] = {})); ['config', 'module', 'em'].forEach(
i =>
// @ts-ignore
(this[i] = {})
);
return this;
} }
addTo(model, c, opts = {}) { addTo(model: Sector, c: any, opts = {}) {
this.addToCollection(model, null, opts); this.addToCollection(model, null, opts);
} }
addToCollection(model, fragmentEl, opts = {}) { addToCollection(model: Sector, fragmentEl: DocumentFragment | null, opts: { at?: number } = {}) {
const { config, el } = this; const { config, el } = this;
const appendTo = fragmentEl || el; const appendTo = fragmentEl || el;
const rendered = new SectorView({ model, config }).render().el; const rendered = new SectorView({ model, config }).render().el;

2
src/utils/dom.ts

@ -40,7 +40,7 @@ export const replaceWith = (oldEl: HTMLElement, newEl: HTMLElement) => {
oldEl.parentNode?.replaceChild(newEl, oldEl); oldEl.parentNode?.replaceChild(newEl, oldEl);
}; };
export const appendAtIndex = (parent: HTMLElement, child: ChildHTML, index?: number) => { export const appendAtIndex = (parent: HTMLElement | DocumentFragment, child: ChildHTML, index?: number) => {
const { childNodes } = parent; const { childNodes } = parent;
const total = childNodes.length; const total = childNodes.length;
const at = isUndefined(index) ? total : index; const at = isUndefined(index) ? total : index;

Loading…
Cancel
Save