@@ -5,7 +5,7 @@ import { editors } from './editors/index.js'
55import { templates } from './templates/index.js'
66import { iconlibs } from './iconlibs/index.js'
77import { themes } from './themes/index.js'
8- import { extend , each , getShadowParent } from './utilities.js'
8+ import { extend , getShadowParent , hasOwnProperty } from './utilities.js'
99
1010export class JSONEditor {
1111 constructor ( element , options = { } ) {
@@ -74,7 +74,7 @@ export class JSONEditor {
7474 this . root . postBuild ( )
7575
7676 /* Starting data */
77- if ( this . options . hasOwnProperty ( 'startval' ) ) this . root . setValue ( this . options . startval )
77+ if ( hasOwnProperty ( this . options , 'startval' ) ) this . root . setValue ( this . options . startval )
7878
7979 this . validation_results = this . validator . validate ( this . root . getValue ( ) )
8080 this . root . showValidationErrors ( this . validation_results )
@@ -191,24 +191,18 @@ export class JSONEditor {
191191 }
192192
193193 getEditorsRules ( ) {
194- const rules = { }
195-
196- each ( JSONEditor . defaults . editors , ( i , editorClass ) => editorClass . rules && extend ( rules , editorClass . rules ) )
197-
198- return rules
194+ const extendRule = ( rules , editorClass ) => editorClass . rules ? extend ( rules , editorClass . rules ) : rules
195+ return Object . values ( JSONEditor . defaults . editors ) . reduce ( extendRule , { } )
199196 }
200197
201198 getEditorClass ( schema ) {
202199 let classname
203200
204201 schema = this . expandSchema ( schema )
205202
206- each ( JSONEditor . defaults . resolvers , ( i , resolver ) => {
207- const tmp = resolver ( schema )
208- if ( tmp && JSONEditor . defaults . editors [ tmp ] ) {
209- classname = tmp
210- return false
211- }
203+ JSONEditor . defaults . resolvers . find ( resolver => {
204+ classname = resolver ( schema )
205+ return classname && JSONEditor . defaults . editors [ classname ]
212206 } )
213207
214208 if ( ! classname ) throw new Error ( `Unknown editor for schema ${ JSON . stringify ( schema ) } ` )
@@ -229,23 +223,21 @@ export class JSONEditor {
229223 if ( this . firing_change ) return
230224 this . firing_change = true
231225
232- const self = this
233-
234226 window . requestAnimationFrame ( ( ) => {
235- self . firing_change = false
236- if ( ! self . ready ) return
227+ this . firing_change = false
228+ if ( ! this . ready ) return
237229
238230 /* Validate and cache results */
239- self . validation_results = self . validator . validate ( self . root . getValue ( ) )
231+ this . validation_results = this . validator . validate ( this . root . getValue ( ) )
240232
241- if ( self . options . show_errors !== 'never' ) {
242- self . root . showValidationErrors ( self . validation_results )
233+ if ( this . options . show_errors !== 'never' ) {
234+ this . root . showValidationErrors ( this . validation_results )
243235 } else {
244- self . root . showValidationErrors ( [ ] )
236+ this . root . showValidationErrors ( [ ] )
245237 }
246238
247239 /* Fire change event */
248- self . trigger ( 'change' )
240+ this . trigger ( 'change' )
249241 } )
250242
251243 return this
@@ -374,28 +366,24 @@ export class JSONEditor {
374366 const sheet = styleTag . sheet ? styleTag . sheet : styleTag . styleSheet
375367 const qualifier = this . element . nodeName . toLowerCase ( )
376368
377- for ( var selector in rules ) {
378- if ( ! rules . hasOwnProperty ( selector ) ) continue
379- var sel = qualifier + '[data-theme="' + themeName + '"] ' + selector
369+ Object . keys ( rules ) . forEach ( selector => {
370+ const sel = `${ qualifier } [data-theme="${ themeName } "] ${ selector } `
380371
381372 // all browsers, except IE before version 9
382373 if ( sheet . insertRule ) sheet . insertRule ( sel + ' {' + decodeURIComponent ( rules [ selector ] ) + '}' , 0 )
383374 // Internet Explorer before version 9
384375 else if ( sheet . addRule ) sheet . addRule ( sel , decodeURIComponent ( rules [ selector ] ) , 0 )
385- }
376+ } )
386377 }
387378
388379 addNewStyleRulesToShadowRoot ( themeName , rules , shadowRoot ) {
389380 const qualifier = this . element . nodeName . toLowerCase ( )
390381 let cssText = ''
391382
392- for ( var selector in rules ) {
393- if ( ! rules . hasOwnProperty ( selector ) ) continue
394- var sel = qualifier + '[data-theme="' + themeName + '"] ' + selector
395-
383+ Object . keys ( rules ) . forEach ( selector => {
384+ const sel = `${ qualifier } [data-theme="${ themeName } "] ${ selector } `
396385 cssText += sel + ' {' + decodeURIComponent ( rules [ selector ] ) + '}' + '\n'
397- }
398-
386+ } )
399387 const styleSheet = new CSSStyleSheet ( )
400388 styleSheet . replaceSync ( cssText )
401389 shadowRoot . adoptedStyleSheets = [ ...shadowRoot . adoptedStyleSheets , styleSheet ]
0 commit comments