From 60e67326b54624172176dd7d10635790afd54f1a Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Sat, 28 Mar 2020 19:18:37 -0400 Subject: [PATCH 01/13] Add copy button to arrays in table format --- CHANGELOG.md | 1 + src/defaults.js | 4 ++++ src/editors/table.js | 45 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 809a39635..f6da65a30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Fix of #701 - editors/number.js and editors/integer.js don't change values when validation is failed - Fix of #716 - add ignore for allOf to fall in line with existing ignores of anyOf/oneOf for additionalProperties validation - Fix of #714 - Checkboxes inside object tables duplicate labels from heading +- Added copy button to arrays in table format ### 2.0.0-dev - Fix of #643 - Allow use of themes not compiled directly into the build diff --git a/src/defaults.js b/src/defaults.js index df8d705e2..249280601 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -225,6 +225,10 @@ languages.en = { */ button_delete_row_title_short: 'Delete', /** + * Title on Copy Row buttons, short version (no parameter with the object title) + */ + button_copy_row_title_short: 'Copy', + /** * Title on Collapse buttons */ button_collapse: 'Collapse', diff --git a/src/editors/table.js b/src/editors/table.js index 3498c0788..1b3d822b6 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -207,6 +207,8 @@ export class TableEditor extends ArrayEditor { refreshRowButtons () { /* If we currently have minItems items in the array */ const minItems = this.schema.minItems && this.schema.minItems >= this.rows.length + /* If we currently have maxItems items in the array */ + const maxItems = this.schema.maxItems && this.schema.maxItems <= this.rows.length let needRowButtons = false this.rows.forEach((editor, i) => { @@ -230,6 +232,16 @@ export class TableEditor extends ArrayEditor { } } + /* Hide the copy button if we have maxItems items */ + if (editor.copy_button) { + if (maxItems) { + editor.copy_button.style.display = 'none' + } else { + needRowButtons = true + editor.copy_button.style.display = '' + } + } + if (editor.moveup_button) { needRowButtons = true } @@ -259,7 +271,7 @@ export class TableEditor extends ArrayEditor { this.table.style.display = '' this.remove_all_rows_button.style.display = 'none' - /* If there are minItems items in the array, or configured to hide the delete_last_row button, hide the delete button beneath the rows */ + /* If there are minItems items in the array, or configured to hide the delete_last_row button, hide the button beneath the rows */ if (minItems || this.hide_delete_last_row_buttons) { this.delete_last_row_button.style.display = 'none' } else { @@ -269,6 +281,7 @@ export class TableEditor extends ArrayEditor { } else { this.table.style.display = '' + /* If there are minItems items in the array, or configured to hide the delete_last_row button, hide the button beneath the rows */ if (minItems || this.hide_delete_last_row_buttons) { this.delete_last_row_button.style.display = 'none' } else { @@ -276,6 +289,7 @@ export class TableEditor extends ArrayEditor { controlsNeeded = true } + /* If there are minItems items in the array, or configured to hide the remove_all_rows_button button, hide the button beneath the rows */ if (minItems || this.hide_delete_all_rows_buttons) { this.remove_all_rows_button.style.display = 'none' } else { @@ -284,8 +298,8 @@ export class TableEditor extends ArrayEditor { } } - /* If there are maxItems in the array, hide the add button beneath the rows */ - if ((this.schema.maxItems && this.schema.maxItems <= this.rows.length) || this.hide_add_button) { + /* If there are maxItems items in the array, or configured to hide the add_row_button button, hide the button beneath the rows */ + if (maxItems || this.hide_add_button) { this.add_row_button.style.display = 'none' } else { this.add_row_button.style.display = '' @@ -316,7 +330,7 @@ export class TableEditor extends ArrayEditor { const controlsHolder = this.rows[i].table_controls - /* Buttons to delete row, move row up, and move row down */ + /* Buttons to delete row, copy row, move row up, and move row down */ if (!this.hide_delete_buttons) { this.rows[i].delete_button = this.getButton('', 'delete', this.translate('button_delete_row_title_short')) this.rows[i].delete_button.classList.add('delete', 'json-editor-btntype-delete') @@ -338,6 +352,29 @@ export class TableEditor extends ArrayEditor { controlsHolder.appendChild(this.rows[i].delete_button) } + if (this.show_copy_button) { + self.rows[i].copy_button = this.getButton('', 'copy', this.translate('button_copy_row_title_short')) + self.rows[i].copy_button.classList.add('copy', 'json-editor-btntype-copy') + self.rows[i].copy_button.setAttribute('data-i', i) + self.rows[i].copy_button.addEventListener('click', function (e) { + const value = self.getValue() + e.preventDefault() + e.stopPropagation() + const i = this.getAttribute('data-i') * 1 + + each(value, (j, row) => { + if (j === i) { + value.push(row) + } + }) + + self.setValue(value) + self.refreshValue(true) + self.onChange(true) + }) + controlsHolder.appendChild(self.rows[i].copy_button) + } + if (i && !this.hide_move_buttons) { this.rows[i].moveup_button = this.getButton('', 'moveup', this.translate('button_move_up_title')) this.rows[i].moveup_button.classList.add('moveup', 'json-editor-btntype-move') From 5a50ca64ccdcb801cd7bc2504c3fae2f33fd3c2f Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Sat, 28 Mar 2020 21:08:42 -0400 Subject: [PATCH 02/13] Handle moveup_button with same logic as movedown_button --- src/editors/table.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/editors/table.js b/src/editors/table.js index 1b3d822b6..a14ad96e3 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -242,8 +242,14 @@ export class TableEditor extends ArrayEditor { } } + /* Hide the move up button for the first row */ if (editor.moveup_button) { - needRowButtons = true + if (i === 0) { + editor.moveup_button.style.display = 'none' + } else { + needRowButtons = true + editor.moveup_button.style.display = '' + } } }) @@ -375,7 +381,7 @@ export class TableEditor extends ArrayEditor { controlsHolder.appendChild(self.rows[i].copy_button) } - if (i && !this.hide_move_buttons) { + if (!this.hide_move_buttons) { this.rows[i].moveup_button = this.getButton('', 'moveup', this.translate('button_move_up_title')) this.rows[i].moveup_button.classList.add('moveup', 'json-editor-btntype-move') this.rows[i].moveup_button.setAttribute('data-i', i) From f91f3e79bf4e3342d517805f2a191ab46ae16082 Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Sat, 28 Mar 2020 21:11:33 -0400 Subject: [PATCH 03/13] Improve maintainability of refreshRowButtons() --- src/editors/table.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/editors/table.js b/src/editors/table.js index a14ad96e3..d36efb38b 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -212,16 +212,6 @@ export class TableEditor extends ArrayEditor { let needRowButtons = false this.rows.forEach((editor, i) => { - /* Hide the move down button for the last row */ - if (editor.movedown_button) { - if (i === this.rows.length - 1) { - editor.movedown_button.style.display = 'none' - } else { - needRowButtons = true - editor.movedown_button.style.display = '' - } - } - /* Hide the delete button if we have minItems items */ if (editor.delete_button) { if (minItems) { @@ -251,6 +241,16 @@ export class TableEditor extends ArrayEditor { editor.moveup_button.style.display = '' } } + + /* Hide the move down button for the last row */ + if (editor.movedown_button) { + if (i === self.rows.length - 1) { + editor.movedown_button.style.display = 'none' + } else { + needRowButtons = true + editor.movedown_button.style.display = '' + } + } }) /* Show/hide controls column in table */ From 01404c1791974b559138c4061a7cdc71ffe5a46b Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Sat, 28 Mar 2020 21:31:25 -0400 Subject: [PATCH 04/13] Improve maintainability of refreshRowButtons() --- src/editors/table.js | 57 +++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/src/editors/table.js b/src/editors/table.js index d36efb38b..ebf9664d7 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -212,8 +212,8 @@ export class TableEditor extends ArrayEditor { let needRowButtons = false this.rows.forEach((editor, i) => { - /* Hide the delete button if we have minItems items */ if (editor.delete_button) { + /* Hide the delete button if we have minItems items */ if (minItems) { editor.delete_button.style.display = 'none' } else { @@ -222,8 +222,8 @@ export class TableEditor extends ArrayEditor { } } - /* Hide the copy button if we have maxItems items */ if (editor.copy_button) { + /* Hide the copy button if we have maxItems items */ if (maxItems) { editor.copy_button.style.display = 'none' } else { @@ -232,8 +232,8 @@ export class TableEditor extends ArrayEditor { } } - /* Hide the move up button for the first row */ if (editor.moveup_button) { + /* Hide the move up button for the first row */ if (i === 0) { editor.moveup_button.style.display = 'none' } else { @@ -242,8 +242,8 @@ export class TableEditor extends ArrayEditor { } } - /* Hide the move down button for the last row */ if (editor.movedown_button) { + /* Hide the move down button for the last row */ if (i === self.rows.length - 1) { editor.movedown_button.style.display = 'none' } else { @@ -267,43 +267,14 @@ export class TableEditor extends ArrayEditor { this.controls_header_cell.style.display = 'none' } - let controlsNeeded = false - if (!this.value.length) { - this.delete_last_row_button.style.display = 'none' - this.remove_all_rows_button.style.display = 'none' this.table.style.display = 'none' - } else if (this.value.length === 1) { - this.table.style.display = '' - this.remove_all_rows_button.style.display = 'none' - - /* If there are minItems items in the array, or configured to hide the delete_last_row button, hide the button beneath the rows */ - if (minItems || this.hide_delete_last_row_buttons) { - this.delete_last_row_button.style.display = 'none' - } else { - this.delete_last_row_button.style.display = '' - controlsNeeded = true - } } else { this.table.style.display = '' - - /* If there are minItems items in the array, or configured to hide the delete_last_row button, hide the button beneath the rows */ - if (minItems || this.hide_delete_last_row_buttons) { - this.delete_last_row_button.style.display = 'none' - } else { - this.delete_last_row_button.style.display = '' - controlsNeeded = true - } - - /* If there are minItems items in the array, or configured to hide the remove_all_rows_button button, hide the button beneath the rows */ - if (minItems || this.hide_delete_all_rows_buttons) { - this.remove_all_rows_button.style.display = 'none' - } else { - this.remove_all_rows_button.style.display = '' - controlsNeeded = true - } } + let controlsNeeded = false + /* If there are maxItems items in the array, or configured to hide the add_row_button button, hide the button beneath the rows */ if (maxItems || this.hide_add_button) { this.add_row_button.style.display = 'none' @@ -312,6 +283,22 @@ export class TableEditor extends ArrayEditor { controlsNeeded = true } + /* If there are minItems items in the array, or configured to hide the delete_last_row button, hide the button beneath the rows */ + if (!this.value.length || minItems || this.hide_delete_last_row_buttons) { + this.delete_last_row_button.style.display = 'none' + } else { + this.delete_last_row_button.style.display = '' + controlsNeeded = true + } + + /* If there are minItems items in the array, or configured to hide the remove_all_rows_button button, hide the button beneath the rows */ + if (this.value.length <= 1 || minItems || this.hide_delete_all_rows_buttons) { + this.remove_all_rows_button.style.display = 'none' + } else { + this.remove_all_rows_button.style.display = '' + controlsNeeded = true + } + if (!controlsNeeded) { this.controls.style.display = 'none' } else { From ab6595422cb8fe3abb1c7492a0bc8719ad7d7c1e Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Sun, 29 Mar 2020 06:10:53 -0400 Subject: [PATCH 05/13] Update array tests --- tests/codeceptjs/editors/array_test.js | 12 ++++++++++-- tests/pages/table-move-events.html | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/codeceptjs/editors/array_test.js b/tests/codeceptjs/editors/array_test.js index dd1e59490..965191ebf 100644 --- a/tests/codeceptjs/editors/array_test.js +++ b/tests/codeceptjs/editors/array_test.js @@ -17,7 +17,7 @@ Scenario('should trigger array (table) editing triggers', async (I) => { assert.equal(value, '["A","B"]'); I.amAcceptingPopups(); - I.click('.json-editor-btn-moveup'); + I.click('//button[contains(@class, "json-editor-btn-moveup") and @data-i="1"]'); I.seeInPopup('moveRow'); I.acceptPopup(); I.click('.get-value'); @@ -25,13 +25,21 @@ Scenario('should trigger array (table) editing triggers', async (I) => { assert.equal(value, '["B","A"]'); I.amAcceptingPopups(); - I.click('.json-editor-btn-movedown'); + I.click('//button[contains(@class, "json-editor-btn-movedown") and @data-i="0"]'); I.seeInPopup('moveRow'); I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); assert.equal(value, '["A","B"]'); + I.amAcceptingPopups(); + I.click('//button[contains(@class, "json-editor-btn-copy") and @data-i="1"]'); + I.seeInPopup('moveRow'); + I.acceptPopup(); + I.click('.get-value'); + value = await I.grabValueFrom('.debug'); + assert.equal(value, '["B","A","A"]'); + I.amAcceptingPopups(); I.click('.json-editor-btntype-add'); I.seeInPopup('addRow'); diff --git a/tests/pages/table-move-events.html b/tests/pages/table-move-events.html index 013ee8d42..e683045aa 100644 --- a/tests/pages/table-move-events.html +++ b/tests/pages/table-move-events.html @@ -23,27 +23,28 @@ } }; var editor = new JSONEditor(container, { - schema: schema + schema: schema, + enable_array_copy: true }); document.querySelector('.get-value').addEventListener('click', function () { debug.value = JSON.stringify(editor.getValue()); }); editor.setValue(["A","B"]); editor.on('moveRow', function () { - alert('moveRow') - console.log('moveRow') + alert('moveRow'); + console.log('moveRow'); }); editor.on('addRow', function () { - alert('addRow') - console.log('addRow') + alert('addRow'); + console.log('addRow'); }); editor.on('deleteRow', function () { - alert('deleteRow') - console.log('deleteRow') + alert('deleteRow'); + console.log('deleteRow'); }); editor.on('deleteAllRows', function () { - alert('deleteAllRows') - console.log('deleteAllRows') + alert('deleteAllRows'); + console.log('deleteAllRows'); }); From 266fede96cb2648d1934086d037f3ef55499f91e Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Sun, 29 Mar 2020 07:18:40 -0400 Subject: [PATCH 06/13] Improve maintainability of addRow() --- src/editors/table.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/editors/table.js b/src/editors/table.js index ebf9664d7..8b961eece 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -350,10 +350,10 @@ export class TableEditor extends ArrayEditor { self.rows[i].copy_button.classList.add('copy', 'json-editor-btntype-copy') self.rows[i].copy_button.setAttribute('data-i', i) self.rows[i].copy_button.addEventListener('click', function (e) { - const value = self.getValue() e.preventDefault() e.stopPropagation() const i = this.getAttribute('data-i') * 1 + const value = self.getValue() each(value, (j, row) => { if (j === i) { @@ -400,6 +400,7 @@ export class TableEditor extends ArrayEditor { const i = e.currentTarget.getAttribute('data-i') * 1 const rows = this.getValue() if (i >= rows.length - 1) return + const tmp = rows[i + 1] rows[i + 1] = rows[i] rows[i] = tmp From f0463f2eec890d282b2767951c86ef09d77bba36 Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Sun, 29 Mar 2020 07:19:10 -0400 Subject: [PATCH 07/13] Add 'copyRow' trigger --- src/editors/table.js | 1 + tests/codeceptjs/editors/array_test.js | 2 +- tests/pages/table-move-events.html | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/editors/table.js b/src/editors/table.js index 8b961eece..b8d5807dc 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -364,6 +364,7 @@ export class TableEditor extends ArrayEditor { self.setValue(value) self.refreshValue(true) self.onChange(true) + self.jsoneditor.trigger('copyRow', value[value.length - 1]) }) controlsHolder.appendChild(self.rows[i].copy_button) } diff --git a/tests/codeceptjs/editors/array_test.js b/tests/codeceptjs/editors/array_test.js index 965191ebf..abf41f6f5 100644 --- a/tests/codeceptjs/editors/array_test.js +++ b/tests/codeceptjs/editors/array_test.js @@ -34,7 +34,7 @@ Scenario('should trigger array (table) editing triggers', async (I) => { I.amAcceptingPopups(); I.click('//button[contains(@class, "json-editor-btn-copy") and @data-i="1"]'); - I.seeInPopup('moveRow'); + I.seeInPopup('copyRow'); I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); diff --git a/tests/pages/table-move-events.html b/tests/pages/table-move-events.html index e683045aa..e968aaa47 100644 --- a/tests/pages/table-move-events.html +++ b/tests/pages/table-move-events.html @@ -30,6 +30,10 @@ debug.value = JSON.stringify(editor.getValue()); }); editor.setValue(["A","B"]); + editor.on('copyRow', function () { + alert('copyRow'); + console.log('copyRow'); + }); editor.on('moveRow', function () { alert('moveRow'); console.log('moveRow'); From 4e2fa317ec73c6fbc3881d41c746104022c05231 Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Sun, 29 Mar 2020 07:37:59 -0400 Subject: [PATCH 08/13] Fix array test --- tests/codeceptjs/editors/array_test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/codeceptjs/editors/array_test.js b/tests/codeceptjs/editors/array_test.js index abf41f6f5..8596262f9 100644 --- a/tests/codeceptjs/editors/array_test.js +++ b/tests/codeceptjs/editors/array_test.js @@ -38,7 +38,7 @@ Scenario('should trigger array (table) editing triggers', async (I) => { I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["B","A","A"]'); + assert.equal(value, '["A","B","B"]'); I.amAcceptingPopups(); I.click('.json-editor-btntype-add'); @@ -46,7 +46,7 @@ Scenario('should trigger array (table) editing triggers', async (I) => { I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["A","B",""]'); + assert.equal(value, '["A","B","B",""]'); // This test will fail when using Puppeteer due to the way Puppeteer handles popups. // Puppeteer apparently only sees the text in the last popup, so it doesn't see the @@ -62,7 +62,7 @@ Scenario('should trigger array (table) editing triggers', async (I) => { I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["A","B"]'); + assert.equal(value, '["A","B","B"]'); // This test will fail when using Puppeteer due to the way Puppeteer handles popups. I.amAcceptingPopups(); From 739b06fc3bbdc0b54a2a2e9be806a81b3808b679 Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Tue, 7 Apr 2020 06:31:30 -0400 Subject: [PATCH 09/13] Replace 'self' with 'this' --- src/editors/table.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/editors/table.js b/src/editors/table.js index b8d5807dc..9ccea2ff6 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -244,7 +244,7 @@ export class TableEditor extends ArrayEditor { if (editor.movedown_button) { /* Hide the move down button for the last row */ - if (i === self.rows.length - 1) { + if (i === this.rows.length - 1) { editor.movedown_button.style.display = 'none' } else { needRowButtons = true @@ -346,14 +346,14 @@ export class TableEditor extends ArrayEditor { } if (this.show_copy_button) { - self.rows[i].copy_button = this.getButton('', 'copy', this.translate('button_copy_row_title_short')) - self.rows[i].copy_button.classList.add('copy', 'json-editor-btntype-copy') - self.rows[i].copy_button.setAttribute('data-i', i) - self.rows[i].copy_button.addEventListener('click', function (e) { + this.rows[i].copy_button = this.getButton('', 'copy', this.translate('button_copy_row_title_short')) + this.rows[i].copy_button.classList.add('copy', 'json-editor-btntype-copy') + this.rows[i].copy_button.setAttribute('data-i', i) + this.rows[i].copy_button.addEventListener('click', function (e) { e.preventDefault() e.stopPropagation() const i = this.getAttribute('data-i') * 1 - const value = self.getValue() + const value = this.getValue() each(value, (j, row) => { if (j === i) { @@ -361,12 +361,12 @@ export class TableEditor extends ArrayEditor { } }) - self.setValue(value) - self.refreshValue(true) - self.onChange(true) - self.jsoneditor.trigger('copyRow', value[value.length - 1]) + this.setValue(value) + this.refreshValue(true) + this.onChange(true) + this.jsoneditor.trigger('copyRow', value[value.length - 1]) }) - controlsHolder.appendChild(self.rows[i].copy_button) + controlsHolder.appendChild(this.rows[i].copy_button) } if (!this.hide_move_buttons) { From 716751d051cad1157948345f5bbfc38ed59a669c Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Tue, 7 Apr 2020 06:37:32 -0400 Subject: [PATCH 10/13] Update comments --- src/editors/table.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/editors/table.js b/src/editors/table.js index 9ccea2ff6..655096970 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -233,7 +233,7 @@ export class TableEditor extends ArrayEditor { } if (editor.moveup_button) { - /* Hide the move up button for the first row */ + /* Hide the moveup button for the first row */ if (i === 0) { editor.moveup_button.style.display = 'none' } else { @@ -243,7 +243,7 @@ export class TableEditor extends ArrayEditor { } if (editor.movedown_button) { - /* Hide the move down button for the last row */ + /* Hide the movedown button for the last row */ if (i === this.rows.length - 1) { editor.movedown_button.style.display = 'none' } else { From 6f5a40048b71a9513f973d56ab32c7986b6c3aea Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Tue, 7 Apr 2020 06:41:04 -0400 Subject: [PATCH 11/13] Remove use of each() --- src/editors/table.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/editors/table.js b/src/editors/table.js index 655096970..b29281c80 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -355,7 +355,7 @@ export class TableEditor extends ArrayEditor { const i = this.getAttribute('data-i') * 1 const value = this.getValue() - each(value, (j, row) => { + value.forEach((row, j) => { if (j === i) { value.push(row) } From 4af03ab9b95b273607a959423fe66ecac59d7233 Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Tue, 7 Apr 2020 06:58:11 -0400 Subject: [PATCH 12/13] Update for standards --- src/editors/table.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/editors/table.js b/src/editors/table.js index b29281c80..28d404463 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -349,10 +349,10 @@ export class TableEditor extends ArrayEditor { this.rows[i].copy_button = this.getButton('', 'copy', this.translate('button_copy_row_title_short')) this.rows[i].copy_button.classList.add('copy', 'json-editor-btntype-copy') this.rows[i].copy_button.setAttribute('data-i', i) - this.rows[i].copy_button.addEventListener('click', function (e) { + this.rows[i].copy_button.addEventListener('click', e => { e.preventDefault() e.stopPropagation() - const i = this.getAttribute('data-i') * 1 + const i = e.currentTarget.getAttribute('data-i') * 1 const value = this.getValue() value.forEach((row, j) => { From c36e3483f5292cba366f5c8edb7eaee966712dd9 Mon Sep 17 00:00:00 2001 From: Jonathan Elchison Date: Thu, 16 Apr 2020 21:18:05 -0400 Subject: [PATCH 13/13] Make copied row appear immediately below current row --- src/editors/table.js | 51 ++++++++++++-------------- tests/codeceptjs/editors/array_test.js | 19 ++++++---- tests/pages/table-move-events.html | 2 +- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/editors/table.js b/src/editors/table.js index 28d404463..7fcd53643 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -336,11 +336,14 @@ export class TableEditor extends ArrayEditor { return false } - const i = e.currentTarget.getAttribute('data-i') * 1 - const newval = this.getValue().filter((row, j) => j !== i) /* If this is the one we're deleting */ - this.setValue(newval) + const j = e.currentTarget.getAttribute('data-i') * 1 + const value = this.getValue() + + value.splice(j, 1) + + this.setValue(value) this.onChange(true) - this.jsoneditor.trigger('deleteRow', this.rows[i]) + this.jsoneditor.trigger('deleteRow', this.rows[j]) }) controlsHolder.appendChild(this.rows[i].delete_button) } @@ -352,19 +355,15 @@ export class TableEditor extends ArrayEditor { this.rows[i].copy_button.addEventListener('click', e => { e.preventDefault() e.stopPropagation() - const i = e.currentTarget.getAttribute('data-i') * 1 + + const j = e.currentTarget.getAttribute('data-i') * 1 const value = this.getValue() - value.forEach((row, j) => { - if (j === i) { - value.push(row) - } - }) + value.splice(j + 1, 0, value[j]) this.setValue(value) - this.refreshValue(true) this.onChange(true) - this.jsoneditor.trigger('copyRow', value[value.length - 1]) + this.jsoneditor.trigger('copyRow', this.rows[j + 1]) }) controlsHolder.appendChild(this.rows[i].copy_button) } @@ -376,17 +375,15 @@ export class TableEditor extends ArrayEditor { this.rows[i].moveup_button.addEventListener('click', e => { e.preventDefault() e.stopPropagation() - const i = e.currentTarget.getAttribute('data-i') * 1 - if (i <= 0) return - const rows = this.getValue() - const tmp = rows[i - 1] - rows[i - 1] = rows[i] - rows[i] = tmp + const j = e.currentTarget.getAttribute('data-i') * 1 + const value = this.getValue() - this.setValue(rows) + value.splice(j - 1, 0, value.splice(j, 1)[0]) + + this.setValue(value) this.onChange(true) - this.jsoneditor.trigger('moveRow', this.rows[i - 1]) + this.jsoneditor.trigger('moveRow', this.rows[j - 1]) }) controlsHolder.appendChild(this.rows[i].moveup_button) } @@ -398,17 +395,15 @@ export class TableEditor extends ArrayEditor { this.rows[i].movedown_button.addEventListener('click', e => { e.preventDefault() e.stopPropagation() - const i = e.currentTarget.getAttribute('data-i') * 1 - const rows = this.getValue() - if (i >= rows.length - 1) return - const tmp = rows[i + 1] - rows[i + 1] = rows[i] - rows[i] = tmp + const j = e.currentTarget.getAttribute('data-i') * 1 + const value = this.getValue() - this.setValue(rows) + value.splice(j + 1, 0, value.splice(j, 1)[0]) + + this.setValue(value) this.onChange(true) - this.jsoneditor.trigger('moveRow', this.rows[i + 1]) + this.jsoneditor.trigger('moveRow', this.rows[j + 1]) }) controlsHolder.appendChild(this.rows[i].movedown_button) } diff --git a/tests/codeceptjs/editors/array_test.js b/tests/codeceptjs/editors/array_test.js index 8596262f9..a2ad37d25 100644 --- a/tests/codeceptjs/editors/array_test.js +++ b/tests/codeceptjs/editors/array_test.js @@ -12,9 +12,12 @@ Scenario('should trigger array (table) editing triggers', async (I) => { I.amOnPage('table-move-events.html'); I.seeElement('[data-schemapath="root.0"]'); I.seeElement('[data-schemapath="root.1"]'); + I.seeElement('[data-schemapath="root.2"]'); + I.seeElement('[data-schemapath="root.3"]'); + I.seeElement('[data-schemapath="root.4"]'); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["A","B"]'); + assert.equal(value, '["A","B","C","D","E"]'); I.amAcceptingPopups(); I.click('//button[contains(@class, "json-editor-btn-moveup") and @data-i="1"]'); @@ -22,23 +25,23 @@ Scenario('should trigger array (table) editing triggers', async (I) => { I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["B","A"]'); + assert.equal(value, '["B","A","C","D","E"]'); I.amAcceptingPopups(); - I.click('//button[contains(@class, "json-editor-btn-movedown") and @data-i="0"]'); + I.click('//button[contains(@class, "json-editor-btn-movedown") and @data-i="1"]'); I.seeInPopup('moveRow'); I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["A","B"]'); + assert.equal(value, '["B","C","A","D","E"]'); I.amAcceptingPopups(); - I.click('//button[contains(@class, "json-editor-btn-copy") and @data-i="1"]'); + I.click('//button[contains(@class, "json-editor-btn-copy") and @data-i="2"]'); I.seeInPopup('copyRow'); I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["A","B","B"]'); + assert.equal(value, '["B","C","A","A","D","E"]'); I.amAcceptingPopups(); I.click('.json-editor-btntype-add'); @@ -46,7 +49,7 @@ Scenario('should trigger array (table) editing triggers', async (I) => { I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["A","B","B",""]'); + assert.equal(value, '["B","C","A","A","D","E",""]'); // This test will fail when using Puppeteer due to the way Puppeteer handles popups. // Puppeteer apparently only sees the text in the last popup, so it doesn't see the @@ -62,7 +65,7 @@ Scenario('should trigger array (table) editing triggers', async (I) => { I.acceptPopup(); I.click('.get-value'); value = await I.grabValueFrom('.debug'); - assert.equal(value, '["A","B","B"]'); + assert.equal(value, '["B","C","A","A","D","E"]'); // This test will fail when using Puppeteer due to the way Puppeteer handles popups. I.amAcceptingPopups(); diff --git a/tests/pages/table-move-events.html b/tests/pages/table-move-events.html index e968aaa47..f3a368e1e 100644 --- a/tests/pages/table-move-events.html +++ b/tests/pages/table-move-events.html @@ -29,7 +29,7 @@ document.querySelector('.get-value').addEventListener('click', function () { debug.value = JSON.stringify(editor.getValue()); }); - editor.setValue(["A","B"]); + editor.setValue(["A","B","C","D","E"]); editor.on('copyRow', function () { alert('copyRow'); console.log('copyRow');