@@ -207,31 +207,49 @@ export class TableEditor extends ArrayEditor {
207
207
refreshRowButtons ( ) {
208
208
/* If we currently have minItems items in the array */
209
209
const minItems = this . schema . minItems && this . schema . minItems >= this . rows . length
210
+ /* If we currently have maxItems items in the array */
211
+ const maxItems = this . schema . maxItems && this . schema . maxItems <= this . rows . length
210
212
211
213
let needRowButtons = false
212
214
this . rows . forEach ( ( editor , i ) => {
213
- /* Hide the move down button for the last row */
214
- if ( editor . movedown_button ) {
215
- if ( i === this . rows . length - 1 ) {
216
- editor . movedown_button . style . display = 'none'
215
+ if ( editor . delete_button ) {
216
+ /* Hide the delete button if we have minItems items */
217
+ if ( minItems ) {
218
+ editor . delete_button . style . display = 'none'
217
219
} else {
218
220
needRowButtons = true
219
- editor . movedown_button . style . display = ''
221
+ editor . delete_button . style . display = ''
220
222
}
221
223
}
222
224
223
- /* Hide the delete button if we have minItems items */
224
- if ( editor . delete_button ) {
225
- if ( minItems ) {
226
- editor . delete_button . style . display = 'none'
225
+ if ( editor . copy_button ) {
226
+ /* Hide the copy button if we have maxItems items */
227
+ if ( maxItems ) {
228
+ editor . copy_button . style . display = 'none'
227
229
} else {
228
230
needRowButtons = true
229
- editor . delete_button . style . display = ''
231
+ editor . copy_button . style . display = ''
230
232
}
231
233
}
232
234
233
235
if ( editor . moveup_button ) {
234
- needRowButtons = true
236
+ /* Hide the moveup button for the first row */
237
+ if ( i === 0 ) {
238
+ editor . moveup_button . style . display = 'none'
239
+ } else {
240
+ needRowButtons = true
241
+ editor . moveup_button . style . display = ''
242
+ }
243
+ }
244
+
245
+ if ( editor . movedown_button ) {
246
+ /* Hide the movedown button for the last row */
247
+ if ( i === this . rows . length - 1 ) {
248
+ editor . movedown_button . style . display = 'none'
249
+ } else {
250
+ needRowButtons = true
251
+ editor . movedown_button . style . display = ''
252
+ }
235
253
}
236
254
} )
237
255
@@ -249,49 +267,38 @@ export class TableEditor extends ArrayEditor {
249
267
this . controls_header_cell . style . display = 'none'
250
268
}
251
269
252
- let controlsNeeded = false
253
-
254
270
if ( ! this . value . length ) {
255
- this . delete_last_row_button . style . display = 'none'
256
- this . remove_all_rows_button . style . display = 'none'
257
271
this . table . style . display = 'none'
258
- } else if ( this . value . length === 1 ) {
259
- this . table . style . display = ''
260
- this . remove_all_rows_button . style . display = 'none'
261
-
262
- /* If there are minItems items in the array, or configured to hide the delete_last_row button, hide the delete button beneath the rows */
263
- if ( minItems || this . hide_delete_last_row_buttons ) {
264
- this . delete_last_row_button . style . display = 'none'
265
- } else {
266
- this . delete_last_row_button . style . display = ''
267
- controlsNeeded = true
268
- }
269
272
} else {
270
273
this . table . style . display = ''
271
-
272
- if ( minItems || this . hide_delete_last_row_buttons ) {
273
- this . delete_last_row_button . style . display = 'none'
274
- } else {
275
- this . delete_last_row_button . style . display = ''
276
- controlsNeeded = true
277
- }
278
-
279
- if ( minItems || this . hide_delete_all_rows_buttons ) {
280
- this . remove_all_rows_button . style . display = 'none'
281
- } else {
282
- this . remove_all_rows_button . style . display = ''
283
- controlsNeeded = true
284
- }
285
274
}
286
275
287
- /* If there are maxItems in the array, hide the add button beneath the rows */
288
- if ( ( this . schema . maxItems && this . schema . maxItems <= this . rows . length ) || this . hide_add_button ) {
276
+ let controlsNeeded = false
277
+
278
+ /* If there are maxItems items in the array, or configured to hide the add_row_button button, hide the button beneath the rows */
279
+ if ( maxItems || this . hide_add_button ) {
289
280
this . add_row_button . style . display = 'none'
290
281
} else {
291
282
this . add_row_button . style . display = ''
292
283
controlsNeeded = true
293
284
}
294
285
286
+ /* If there are minItems items in the array, or configured to hide the delete_last_row button, hide the button beneath the rows */
287
+ if ( ! this . value . length || minItems || this . hide_delete_last_row_buttons ) {
288
+ this . delete_last_row_button . style . display = 'none'
289
+ } else {
290
+ this . delete_last_row_button . style . display = ''
291
+ controlsNeeded = true
292
+ }
293
+
294
+ /* If there are minItems items in the array, or configured to hide the remove_all_rows_button button, hide the button beneath the rows */
295
+ if ( this . value . length <= 1 || minItems || this . hide_delete_all_rows_buttons ) {
296
+ this . remove_all_rows_button . style . display = 'none'
297
+ } else {
298
+ this . remove_all_rows_button . style . display = ''
299
+ controlsNeeded = true
300
+ }
301
+
295
302
if ( ! controlsNeeded ) {
296
303
this . controls . style . display = 'none'
297
304
} else {
@@ -316,7 +323,7 @@ export class TableEditor extends ArrayEditor {
316
323
317
324
const controlsHolder = this . rows [ i ] . table_controls
318
325
319
- /* Buttons to delete row, move row up, and move row down */
326
+ /* Buttons to delete row, copy row, move row up, and move row down */
320
327
if ( ! this . hide_delete_buttons ) {
321
328
this . rows [ i ] . delete_button = this . getButton ( '' , 'delete' , this . translate ( 'button_delete_row_title_short' ) )
322
329
this . rows [ i ] . delete_button . classList . add ( 'delete' , 'json-editor-btntype-delete' )
@@ -329,33 +336,54 @@ export class TableEditor extends ArrayEditor {
329
336
return false
330
337
}
331
338
332
- const i = e . currentTarget . getAttribute ( 'data-i' ) * 1
333
- const newval = this . getValue ( ) . filter ( ( row , j ) => j !== i ) /* If this is the one we're deleting */
334
- this . setValue ( newval )
339
+ const j = e . currentTarget . getAttribute ( 'data-i' ) * 1
340
+ const value = this . getValue ( )
341
+
342
+ value . splice ( j , 1 )
343
+
344
+ this . setValue ( value )
335
345
this . onChange ( true )
336
- this . jsoneditor . trigger ( 'deleteRow' , this . rows [ i ] )
346
+ this . jsoneditor . trigger ( 'deleteRow' , this . rows [ j ] )
337
347
} )
338
348
controlsHolder . appendChild ( this . rows [ i ] . delete_button )
339
349
}
340
350
341
- if ( i && ! this . hide_move_buttons ) {
351
+ if ( this . show_copy_button ) {
352
+ this . rows [ i ] . copy_button = this . getButton ( '' , 'copy' , this . translate ( 'button_copy_row_title_short' ) )
353
+ this . rows [ i ] . copy_button . classList . add ( 'copy' , 'json-editor-btntype-copy' )
354
+ this . rows [ i ] . copy_button . setAttribute ( 'data-i' , i )
355
+ this . rows [ i ] . copy_button . addEventListener ( 'click' , e => {
356
+ e . preventDefault ( )
357
+ e . stopPropagation ( )
358
+
359
+ const j = e . currentTarget . getAttribute ( 'data-i' ) * 1
360
+ const value = this . getValue ( )
361
+
362
+ value . splice ( j + 1 , 0 , value [ j ] )
363
+
364
+ this . setValue ( value )
365
+ this . onChange ( true )
366
+ this . jsoneditor . trigger ( 'copyRow' , this . rows [ j + 1 ] )
367
+ } )
368
+ controlsHolder . appendChild ( this . rows [ i ] . copy_button )
369
+ }
370
+
371
+ if ( ! this . hide_move_buttons ) {
342
372
this . rows [ i ] . moveup_button = this . getButton ( '' , 'moveup' , this . translate ( 'button_move_up_title' ) )
343
373
this . rows [ i ] . moveup_button . classList . add ( 'moveup' , 'json-editor-btntype-move' )
344
374
this . rows [ i ] . moveup_button . setAttribute ( 'data-i' , i )
345
375
this . rows [ i ] . moveup_button . addEventListener ( 'click' , e => {
346
376
e . preventDefault ( )
347
377
e . stopPropagation ( )
348
- const i = e . currentTarget . getAttribute ( 'data-i' ) * 1
349
378
350
- if ( i <= 0 ) return
351
- const rows = this . getValue ( )
352
- const tmp = rows [ i - 1 ]
353
- rows [ i - 1 ] = rows [ i ]
354
- rows [ i ] = tmp
379
+ const j = e . currentTarget . getAttribute ( 'data-i' ) * 1
380
+ const value = this . getValue ( )
355
381
356
- this . setValue ( rows )
382
+ value . splice ( j - 1 , 0 , value . splice ( j , 1 ) [ 0 ] )
383
+
384
+ this . setValue ( value )
357
385
this . onChange ( true )
358
- this . jsoneditor . trigger ( 'moveRow' , this . rows [ i - 1 ] )
386
+ this . jsoneditor . trigger ( 'moveRow' , this . rows [ j - 1 ] )
359
387
} )
360
388
controlsHolder . appendChild ( this . rows [ i ] . moveup_button )
361
389
}
@@ -367,16 +395,15 @@ export class TableEditor extends ArrayEditor {
367
395
this . rows [ i ] . movedown_button . addEventListener ( 'click' , e => {
368
396
e . preventDefault ( )
369
397
e . stopPropagation ( )
370
- const i = e . currentTarget . getAttribute ( 'data-i' ) * 1
371
- const rows = this . getValue ( )
372
- if ( i >= rows . length - 1 ) return
373
- const tmp = rows [ i + 1 ]
374
- rows [ i + 1 ] = rows [ i ]
375
- rows [ i ] = tmp
376
-
377
- this . setValue ( rows )
398
+
399
+ const j = e . currentTarget . getAttribute ( 'data-i' ) * 1
400
+ const value = this . getValue ( )
401
+
402
+ value . splice ( j + 1 , 0 , value . splice ( j , 1 ) [ 0 ] )
403
+
404
+ this . setValue ( value )
378
405
this . onChange ( true )
379
- this . jsoneditor . trigger ( 'moveRow' , this . rows [ i + 1 ] )
406
+ this . jsoneditor . trigger ( 'moveRow' , this . rows [ j + 1 ] )
380
407
} )
381
408
controlsHolder . appendChild ( this . rows [ i ] . movedown_button )
382
409
}
0 commit comments