Browse Source

Refactor code manager files to classes

pull/4234/head
Artur Arseniev 5 years ago
parent
commit
1da4729142
  1. 49
      src/code_manager/model/CodeMirrorEditor.js
  2. 26
      src/code_manager/model/CssGenerator.js
  3. 2
      src/code_manager/model/HtmlGenerator.js
  4. 10
      src/code_manager/model/JsGenerator.js
  5. 17
      src/code_manager/model/JsonGenerator.js
  6. 4
      src/code_manager/view/EditorView.js

49
src/code_manager/model/CodeMirrorEditor.js

@ -1,6 +1,6 @@
import { bindAll } from 'underscore'; import { bindAll } from 'underscore';
import Backbone from 'backbone'; import { Model } from '../../common';
import { hasWin } from 'utils/mixins'; import { hasWin } from '../../utils/mixins';
let CodeMirror; let CodeMirror;
@ -11,40 +11,39 @@ if (hasWin()) {
require('codemirror-formatting'); require('codemirror-formatting');
} }
export default Backbone.Model.extend({ export default class CodeMirrorEditor extends Model {
CodeMirror, defaults() {
return {
defaults: { input: '',
input: '', label: '',
label: '', codeName: '',
codeName: '', theme: 'hopscotch',
theme: 'hopscotch', readOnly: true,
readOnly: true, lineNumbers: true,
lineNumbers: true };
}, }
/** @inheritdoc */
init(el) { init(el) {
bindAll(this, 'onChange'); bindAll(this, 'onChange');
this.editor = CodeMirror.fromTextArea(el, { this.editor = CodeMirror.fromTextArea(el, {
dragDrop: false, dragDrop: false,
lineWrapping: true, lineWrapping: true,
mode: this.get('codeName'), mode: this.get('codeName'),
...this.attributes ...this.attributes,
}); });
this.element = el; this.element = el;
this.editor.on('change', this.onChange); this.editor.on('change', this.onChange);
return this; return this;
}, }
onChange() { onChange() {
this.trigger('update', this); this.trigger('update', this);
}, }
getEditor() { getEditor() {
return this.editor; return this.editor;
}, }
/** /**
* The element where the viewer is attached * The element where the viewer is attached
@ -52,7 +51,7 @@ export default Backbone.Model.extend({
*/ */
getElement() { getElement() {
return this.element; return this.element;
}, }
/** /**
* Set the element which contains the viewer attached. * Set the element which contains the viewer attached.
@ -64,7 +63,7 @@ export default Backbone.Model.extend({
setElement(el) { setElement(el) {
this.element = el; this.element = el;
return this; return this;
}, }
/** /**
* Refresh the viewer * Refresh the viewer
@ -73,7 +72,7 @@ export default Backbone.Model.extend({
refresh() { refresh() {
this.getEditor().refresh(); this.getEditor().refresh();
return this; return this;
}, }
/** /**
* Focus the viewer * Focus the viewer
@ -82,12 +81,12 @@ export default Backbone.Model.extend({
focus() { focus() {
this.getEditor().focus(); this.getEditor().focus();
return this; return this;
}, }
getContent() { getContent() {
const ed = this.getEditor(); const ed = this.getEditor();
return ed && ed.getValue(); return ed && ed.getValue();
}, }
/** @inheritdoc */ /** @inheritdoc */
setContent(v, opts = {}) { setContent(v, opts = {}) {
@ -103,4 +102,6 @@ export default Backbone.Model.extend({
!opts.noRefresh && setTimeout(() => this.refresh()); !opts.noRefresh && setTimeout(() => this.refresh());
} }
}); }
CodeMirrorEditor.prototype.CodeMirror = CodeMirror;

26
src/code_manager/model/CssGenerator.js

@ -1,6 +1,6 @@
import Backbone from 'backbone';
import { bindAll, isUndefined, each } from 'underscore'; import { bindAll, isUndefined, each } from 'underscore';
import { hasWin } from 'utils/mixins'; import { Model } from '../../common';
import { hasWin } from '../../utils/mixins';
const maxValue = Number.MAX_VALUE; const maxValue = Number.MAX_VALUE;
@ -9,12 +9,12 @@ export const getMediaLength = mediaQuery => {
return !length ? '' : length[0]; return !length ? '' : length[0];
}; };
export default Backbone.Model.extend({ export default class CssGenerator extends Model {
initialize() { initialize() {
bindAll(this, 'sortRules'); bindAll(this, 'sortRules');
this.compCls = []; this.compCls = [];
this.ids = []; this.ids = [];
}, }
/** /**
* Get CSS from a component * Get CSS from a component
@ -39,7 +39,7 @@ export default Backbone.Model.extend({
const components = model.components(); const components = model.components();
components.each(model => (code += this.buildFromModel(model, opts))); components.each(model => (code += this.buildFromModel(model, opts)));
return code; return code;
}, }
build(model, opts = {}) { build(model, opts = {}) {
const { json } = opts; const { json } = opts;
@ -110,7 +110,7 @@ export default Backbone.Model.extend({
} }
return json ? codeJson.filter(r => r) : code; return json ? codeJson.filter(r => r) : code;
}, }
/** /**
* Get CSS from the rule model * Get CSS from the rule model
@ -141,7 +141,7 @@ export default Backbone.Model.extend({
} }
return result; return result;
}, }
/** /**
* Get matched rules of a component * Get matched rules of a component
@ -174,7 +174,7 @@ export default Backbone.Model.extend({
result = result.filter((rule, i) => result.indexOf(rule) === i); result = result.filter((rule, i) => result.indexOf(rule) === i);
return result; return result;
}, }
/** /**
* Get the numeric length of the media query string * Get the numeric length of the media query string
@ -186,7 +186,7 @@ export default Backbone.Model.extend({
if (!length) return maxValue; if (!length) return maxValue;
return parseFloat(length[1]); return parseFloat(length[1]);
}, }
/** /**
* Return a sorted array from media query object * Return a sorted array from media query object
@ -202,7 +202,7 @@ export default Backbone.Model.extend({
const right = isMobFirst ? b.key : a.key; const right = isMobFirst ? b.key : a.key;
return this.getQueryLength(left) - this.getQueryLength(right); return this.getQueryLength(left) - this.getQueryLength(right);
}); });
}, }
sortRules(a, b) { sortRules(a, b) {
const getKey = rule => rule.get('mediaText'); const getKey = rule => rule.get('mediaText');
@ -210,7 +210,7 @@ export default Backbone.Model.extend({
const left = isMobFirst ? getKey(a) : getKey(b); const left = isMobFirst ? getKey(a) : getKey(b);
const right = isMobFirst ? getKey(b) : getKey(a); const right = isMobFirst ? getKey(b) : getKey(a);
return this.getQueryLength(left) - this.getQueryLength(right); return this.getQueryLength(left) - this.getQueryLength(right);
}, }
/** /**
* Return passed selector without states * Return passed selector without states
@ -223,5 +223,5 @@ export default Backbone.Model.extend({
.split(' ') .split(' ')
.map(item => item.split(':')[0]) .map(item => item.split(':')[0])
.join(' '); .join(' ');
}, }
}); }

2
src/code_manager/model/HtmlGenerator.js

@ -1,4 +1,4 @@
import { Model } from 'backbone'; import { Model } from '../../common';
export default class HTMLGenerator extends Model { export default class HTMLGenerator extends Model {
build(model, opts = {}) { build(model, opts = {}) {

10
src/code_manager/model/JsGenerator.js

@ -1,12 +1,12 @@
import { extend } from 'underscore'; import { extend } from 'underscore';
import Backbone from 'backbone'; import { Model } from '../../common';
function isFunctionEmpty(fn) { function isFunctionEmpty(fn) {
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
} }
export default Backbone.Model.extend({ export default class JsGenerator extends Model {
mapModel(model) { mapModel(model) {
var code = ''; var code = '';
var script = model.get('script-export') || model.get('script'); var script = model.get('script-export') || model.get('script');
@ -49,7 +49,7 @@ export default Backbone.Model.extend({
}, this); }, this);
return code; return code;
}, }
build(model) { build(model) {
this.mapJs = {}; this.mapJs = {};
@ -88,5 +88,5 @@ export default Backbone.Model.extend({
} }
return code; return code;
}, }
}); }

17
src/code_manager/model/JsonGenerator.js

@ -1,23 +1,22 @@
import { each } from 'underscore'; import { each } from 'underscore';
import Backbone from 'backbone'; import { Model, Collection } from '../../common';
export default Backbone.Model.extend({ export default class JsonGenerator extends Model {
/** @inheritdoc */
build(model) { build(model) {
var json = model.toJSON(); var json = model.toJSON();
this.beforeEach(json); this.beforeEach(json);
each( each(
json, json,
function(v, attr) { function (v, attr) {
var obj = json[attr]; var obj = json[attr];
if (obj instanceof Backbone.Model) { if (obj instanceof Model) {
json[attr] = this.build(obj); json[attr] = this.build(obj);
} else if (obj instanceof Backbone.Collection) { } else if (obj instanceof Collection) {
var coll = obj; var coll = obj;
json[attr] = []; json[attr] = [];
if (coll.length) { if (coll.length) {
coll.each(function(el, index) { coll.each(function (el, index) {
json[attr][index] = this.build(el); json[attr][index] = this.build(el);
}, this); }, this);
} }
@ -27,7 +26,7 @@ export default Backbone.Model.extend({
); );
return json; return json;
}, }
/** /**
* Execute on each object * Execute on each object
@ -36,4 +35,4 @@ export default Backbone.Model.extend({
beforeEach(obj) { beforeEach(obj) {
delete obj.status; delete obj.status;
} }
}); }

4
src/code_manager/view/EditorView.js

@ -1,5 +1,5 @@
import html from 'utils/html'; import { View } from '../../common';
import { View } from 'backbone'; import html from '../../utils/html';
export default class EditorView extends View { export default class EditorView extends View {
template({ pfx, codeName, label }) { template({ pfx, codeName, label }) {

Loading…
Cancel
Save