Browse Source

Move JsGenerator to TS

pull/4746/head
Artur Arseniev 4 years ago
parent
commit
b1271ad7d5
  1. 42
      src/code_manager/model/JsGenerator.ts

42
src/code_manager/model/JsGenerator.js → src/code_manager/model/JsGenerator.ts

@ -1,25 +1,35 @@
import { extend } from 'underscore'; import { extend } from 'underscore';
import { Model } from '../../common'; import { Model } from '../../common';
import Component from '../../dom_components/model/Component';
function isFunctionEmpty(fn) { function isFunctionEmpty(fn: string) {
const content = fn.toString().match(/\{([\s\S]*)\}/m)[1]; // content between first and last { } const content = fn.toString().match(/\{([\s\S]*)\}/m)?.[1] || ''; // content between first and last { }
return content.replace(/^\s*\/\/.*$/gm, '').trim().length === 0; // remove comments return content.replace(/^\s*\/\/.*$/gm, '').trim().length === 0; // remove comments
} }
type MapJsItem = {
ids: string[];
code: string;
props?: Record<string, any>;
};
export default class JsGenerator extends Model { export default class JsGenerator extends Model {
mapModel(model) { mapJs!: Record<string, MapJsItem>;
var code = '';
var script = model.get('script-export') || model.get('script'); mapModel(model: Component) {
var type = model.get('type'); let code = '';
var comps = model.get('components'); const script = model.get('script-export') || model.get('script');
var id = model.getId(); const type = model.get('type');
const comps = model.get('components');
const id = model.getId();
if (script) { if (script) {
// If the component has scripts we need to expose his ID // If the component has scripts we need to expose his ID
var attr = model.get('attributes'); let attr = model.get('attributes');
attr = extend({}, attr, { id }); attr = extend({}, attr, { id });
model.set('attributes', attr, { silent: 1 }); model.set('attributes', attr, { silent: true });
var scrStr = model.getScriptString(script); // @ts-ignore
const scrStr = model.getScriptString(script);
const scrProps = model.get('script-props'); const scrProps = model.get('script-props');
// If the script was updated, I'll put its code in a separate container // If the script was updated, I'll put its code in a separate container
@ -35,23 +45,23 @@ export default class JsGenerator extends Model {
if (mapType) { if (mapType) {
mapType.ids.push(id); mapType.ids.push(id);
if (props) mapType.props[id] = props; if (props) mapType.props![id] = props;
} else { } else {
const res = { ids: [id], code: scrStr }; const res: MapJsItem = { ids: [id], code: scrStr };
if (props) res.props = { [id]: props }; if (props) res.props = { [id]: props };
this.mapJs[type] = res; this.mapJs[type] = res;
} }
} }
} }
comps.each(function (model) { comps.forEach((model: Component) => {
code += this.mapModel(model); code += this.mapModel(model);
}, this); });
return code; return code;
} }
build(model) { build(model: Component) {
this.mapJs = {}; this.mapJs = {};
this.mapModel(model); this.mapModel(model);
let code = ''; let code = '';
Loading…
Cancel
Save