Skip to content

Commit dd0f585

Browse files
author
Riccardo Di Benedetto
committed
clean code
1 parent 1e74287 commit dd0f585

File tree

8 files changed

+44
-102
lines changed

8 files changed

+44
-102
lines changed

config/karma.conf.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module.exports = function (config) {
1919
// preprocess matching files before serving them to the browser
2020
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
2121
preprocessors: {
22-
'./src/core.js': [ 'webpack', 'sourcemap' ], // Same as entrypoint specified in webpack.config.js
23-
'./tests/unit/**/*.spec.js': [ 'webpack', 'sourcemap']
22+
'./src/core.js': ['webpack', 'sourcemap'], // Same as entrypoint specified in webpack.config.js
23+
'./tests/unit/**/*.spec.js': ['webpack', 'sourcemap']
2424
},
2525

2626
// list of files / patterns to exclude
@@ -63,7 +63,7 @@ module.exports = function (config) {
6363

6464
// start these browsers
6565
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
66-
browsers: ['ChromeHeadless'],
66+
browsers: ['ChromeHeadless', 'Chrome'],
6767

6868
customLaunchers: {
6969
Chrome_with_debugging: {

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"title": "JSONEditor",
44
"description": "JSON Schema based editor",
55
"version": "2.2.1",
6-
"main": "dist/nonmin/jsoneditor.js",
6+
"main": "dist/jsoneditor.js",
77
"author": {
88
"name": "Jeremy Dorn",
99
"email": "jeremy@jeremydorn.com",
@@ -26,7 +26,7 @@
2626
"prepare": "npm run build",
2727
"clean": "npm run clean.css.js",
2828
"clean.css.js": "find ./src -name *.css.js -type f -delete",
29-
"build": "npm run build.nonmin",
29+
"build": "npm run build.prod && npm run build.nonmin",
3030
"build.prod": "webpack --config config/webpack.prod.js --progress --profile --bail",
3131
"build.nonmin": "webpack --config config/webpack.nonmin.js --progress --profile --bail",
3232
"build.dev": "webpack --config config/webpack.dev.js --progress --profile --bail",
@@ -45,8 +45,7 @@
4545
"cp:fulltest": "codeceptjs run --steps",
4646
"cp:fulltestmocha": "codeceptjs run --steps --reporter mochawesome",
4747
"eslint.fix": "eslint -c ./.eslintrc src/**/*.js --fix",
48-
"eslint": "eslint -c ./.eslintrc src/**/*.js",
49-
"my-test": "jasmine ./tests/unit/core.spec.js"
48+
"eslint": "eslint -c ./.eslintrc src/**/*.js"
5049
},
5150
"license": "MIT",
5251
"engines": {

src/core.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ export class JSONEditor {
2626
const themeName = this.options.theme || JSONEditor.defaults.theme
2727
const themeClass = JSONEditor.defaults.themes[themeName]
2828

29-
this.MAX_RECURSIONS = this.options.maxRecurions
29+
this.MAX_RECURSIONS = this.options.maxRecursions
3030

31-
console.log('Current options ', this.options)
3231
/* Load editors and selected theme style rules */
3332
if (!themeClass) throw new Error(`Unknown theme ${themeName}`)
3433
this.element.setAttribute('data-theme', themeName)
@@ -73,9 +72,11 @@ export class JSONEditor {
7372
required: true,
7473
container: this.root_container
7574
})
75+
7676
this.root.preBuild()
7777
this.root.build()
7878
this.root.postBuild()
79+
7980
/* Starting data */
8081
if (hasOwnProperty(this.options, 'startval')) this.root.setValue(this.options.startval)
8182

@@ -214,10 +215,10 @@ export class JSONEditor {
214215
return JSONEditor.defaults.editors[classname]
215216
}
216217

217-
createEditor (editorClass, options, recursion = 0, onRecursionStopCallback) {
218+
createEditor (editorClass, options, recursion = 0) {
218219
options = extend({}, editorClass.options || {}, options)
219220
// eslint-disable-next-line new-cap
220-
return new editorClass(options, JSONEditor.defaults, recursion, onRecursionStopCallback)
221+
return new editorClass(options, JSONEditor.defaults, recursion)
221222
}
222223

223224
onChange () {

src/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ const options = {
285285
upload,
286286
prompt_before_delete: true,
287287
useDefault: true,
288-
maxRecurions: 99999
288+
maxRecursions: 99999
289289
}
290290

291291
/* This assignment was previously in index.js but makes more sense here */

src/editors/object.js

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ import { AbstractEditor } from '../editor.js'
22
import { extend, trigger, hasOwnProperty } from '../utilities.js'
33

44
export class ObjectEditor extends AbstractEditor {
5-
constructor (a, b, recursion, stopRecursionCallback = () => {}) {
6-
super(a, b)
7-
this._recursion = recursion
8-
this._stopRecursionCallback = stopRecursionCallback
5+
constructor (options, defaults, recursions) {
6+
super(options, defaults)
7+
this.currentRecursions = recursions
98
this.collapsed = null
10-
this.collapseOnStopRecursion = this.collapseOnStopRecursion.bind(this)
11-
console.log('jsoneditor MAX_RECURSIONS', this.jsoneditor.MAX_RECURSIONS)
129
}
1310

1411
getDefault () {
@@ -385,7 +382,6 @@ export class ObjectEditor extends AbstractEditor {
385382

386383
/* If the object should be rendered as a table row */
387384
if (this.options.table_row) {
388-
console.log('OPTION TABLE ROW ', this._recursion)
389385
Object.entries(this.schema.properties).forEach(([key, schema]) => {
390386
const editor = this.jsoneditor.getEditorClass(schema)
391387
this.editors[key] = this.jsoneditor.createEditor(editor, {
@@ -395,7 +391,7 @@ export class ObjectEditor extends AbstractEditor {
395391
parent: this,
396392
compact: true,
397393
required: true
398-
}, this._recursion + 1, this.collapseOnStopRecursion)
394+
}, this.currentRecursions + 1)
399395
this.editors[key].preBuild()
400396

401397
const width = this.editors[key].options.hidden ? 0 : (this.editors[key].options.grid_columns || this.editors[key].getNumColumns())
@@ -522,7 +518,6 @@ export class ObjectEditor extends AbstractEditor {
522518
}
523519

524520
build () {
525-
console.log('>>>>>>>>>>>>>>>>> LINKED CORRECTLY ')
526521
const isCategoriesFormat = (this.format === 'categories')
527522
this.rows = []
528523
this.active_tab = null
@@ -720,15 +715,8 @@ export class ObjectEditor extends AbstractEditor {
720715
}
721716

722717
/* Show/Hide button */
723-
console.log('BUILD ', this._recursion, this.collapsed)
724-
if (this.collapsed === true) {
725-
this.editor_holder.style.display = 'none'
726-
this.collapse_control = this.getButton('', 'expand', this.translate('button_expand'))
727-
} else {
728-
this.collapsed = false
729-
this.collapse_control = this.getButton('', 'collapse', this.translate('button_collapse'))
730-
}
731-
718+
this.collapsed = false
719+
this.collapse_control = this.getButton('', 'collapse', this.translate('button_collapse'))
732720
this.collapse_control.style.margin = '0 10px 0 0'
733721
this.collapse_control.classList.add('json-editor-btntype-toggle')
734722
this.title.insertBefore(this.collapse_control, this.title.childNodes[0])
@@ -985,13 +973,11 @@ export class ObjectEditor extends AbstractEditor {
985973
this.editors[name].register()
986974
/* New property */
987975
} else {
988-
console.log('NAME ', !this.canHaveAdditionalProperties(), name)
989976
if (!this.canHaveAdditionalProperties() && (!this.schema.properties || !this.schema.properties[name])) {
990977
return
991978
}
992979

993980
const schema = this.getPropertySchema(name)
994-
console.log('SCHEMA ', schema)
995981
if (typeof schema.propertyOrder !== 'number') {
996982
/* if the propertyOrder undefined, then set a smart default value. */
997983
schema.propertyOrder = Object.keys(this.editors).length + 1000
@@ -1002,10 +988,10 @@ export class ObjectEditor extends AbstractEditor {
1002988

1003989
this.editors[name] = this.jsoneditor.createEditor(editor, {
1004990
jsoneditor: this.jsoneditor,
1005-
schema: this._recursion >= this.jsoneditor.MAX_RECURSIONS ? { type: schema.type || null } : schema,
991+
schema: this.currentRecursions >= this.jsoneditor.MAX_RECURSIONS ? { type: schema.type || null } : schema,
1006992
path: `${this.path}.${name}`,
1007993
parent: this
1008-
}, this._recursion + 1, this.collapseOnStopRecursion)
994+
}, this.currentRecursions + 1)
1009995
this.editors[name].preBuild()
1010996

1011997
if (!prebuildOnly) {
@@ -1019,11 +1005,6 @@ export class ObjectEditor extends AbstractEditor {
10191005
}
10201006

10211007
this.cached_editors[name] = this.editors[name]
1022-
1023-
if (this._recursion >= this.jsoneditor.MAX_RECURSIONS) {
1024-
console.info('Stop recursion')
1025-
this._stopRecursionCallback()
1026-
}
10271008
}
10281009

10291010
/* If we're only prebuilding the editors, don't refresh values */
@@ -1033,13 +1014,6 @@ export class ObjectEditor extends AbstractEditor {
10331014
}
10341015
}
10351016

1036-
collapseOnStopRecursion () {
1037-
this.collapsed = true
1038-
if (this._recursion !== 0) {
1039-
this._stopRecursionCallback()
1040-
}
1041-
}
1042-
10431017
onOutsideModalClick (e) {
10441018
if (this.addproperty_holder && !this.addproperty_holder.contains(e.target) && this.adding_property) {
10451019
e.preventDefault()

src/editors/select.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export class SelectEditor extends AbstractEditor {
66
const correctValue = initial && value === null && this.value === undefined ? 'undefined' : value
77
/* Sanitize value before setting it */
88
let sanitized = this.typecast(correctValue || '')
9-
console.log('SETVALUE ', 'CURRENT ', this.value, 'SET ', value, 'INITIAL ', initial, 'SANITIZED', sanitized)
109

1110
if (!this.enum_values.includes(sanitized)) sanitized = this.enum_values[0]
1211

@@ -54,9 +53,7 @@ export class SelectEditor extends AbstractEditor {
5453
if (!this.dependenciesFulfilled) {
5554
return undefined
5655
}
57-
const value = this.typecast(this.value)
58-
console.log('GET VALUE ', value, this.value)
59-
return value
56+
return this.typecast(this.value)
6057
}
6158

6259
preBuild () {
@@ -174,7 +171,6 @@ export class SelectEditor extends AbstractEditor {
174171
this.setInputAttributes([])
175172

176173
this.input.addEventListener('change', (e) => {
177-
console.log('SELECT CHANGE ')
178174
e.preventDefault()
179175
e.stopPropagation()
180176
this.onInputChange()
@@ -183,9 +179,8 @@ export class SelectEditor extends AbstractEditor {
183179
this.control = this.theme.getFormControl(this.label, this.input, this.description, this.infoButton)
184180
this.container.appendChild(this.control)
185181

186-
console.log('ENUM VALUES ', this.enum_values)
187182
this.value = this.enum_values[0]
188-
console.log('SET ', this.value)
183+
189184
/* Any special formatting that needs to happen after the input is added to the dom */
190185
window.requestAnimationFrame(() => {
191186
if (this.input.parentNode) this.afterInputReady()

tests/unit/core.spec.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,4 @@ describe('JSONEditor', function () {
9494
editor = new JSONEditor(element, { schema: schema })
9595
expect(editor).toBeTruthy()
9696
})
97-
98-
it('please do not crash', () => {
99-
editor = new JSONEditor(element, { schema: recursionTest })
100-
expect(editor).toBeTruthy()
101-
})
10297
})
103-
104-
const recursionTest = {
105-
'definitions': {
106-
'JsonArray': {
107-
'type': 'object',
108-
'properties': {
109-
'asJsonArray': {
110-
'$ref': '#/definitions/JsonArray'
111-
}
112-
},
113-
'title': 'JsonArray'
114-
}
115-
},
116-
'type': 'object',
117-
'properties': {
118-
'asJsonArray': {
119-
'$ref': '#/definitions/JsonArray'
120-
}
121-
},
122-
'title': 'JsonArray'
123-
}

tests/unit/editors/object.spec.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { JSONEditor } from '../../../src/core'
2-
import {ObjectEditor} from '../../../src/editors/object.js'
32

43
const fixture = [
54
{
@@ -78,30 +77,30 @@ describe('Object Editor', () => {
7877
})
7978
})
8079

81-
it('please object do not crash', () => {
80+
it('with option maxRecursions 0, should create an editor (schema with recursion on object properties)', () => {
8281
editor = new JSONEditor(element, {
83-
schema: recursionTest
82+
schema: {
83+
definitions: {
84+
JsonArray: {
85+
type: 'object',
86+
properties: {
87+
asJsonArray: {
88+
$ref: '#/definitions/JsonArray'
89+
}
90+
},
91+
title: 'JsonArray'
92+
}
93+
},
94+
type: 'object',
95+
properties: {
96+
asJsonArray: {
97+
$ref: '#/definitions/JsonArray'
98+
}
99+
},
100+
title: 'JsonArray'
101+
},
102+
maxRecursions: 0
84103
})
85104
expect(editor).toBeTruthy()
86105
})
87106
})
88-
const recursionTest = {
89-
'definitions': {
90-
'JsonArray': {
91-
'type': 'object',
92-
'properties': {
93-
'asJsonArray': {
94-
'$ref': '#/definitions/JsonArray'
95-
}
96-
},
97-
'title': 'JsonArray'
98-
}
99-
},
100-
'type': 'object',
101-
'properties': {
102-
'asJsonArray': {
103-
'$ref': '#/definitions/JsonArray'
104-
}
105-
},
106-
'title': 'JsonArray'
107-
}

0 commit comments

Comments
 (0)