A platform for community discussion. Free, open, simple.
FEATURE: Ability to re-order objects in site/theme setting type objects form (#34828)
Mark VanLandingham7ec6ffe30844
diff --git a/app/assets/javascripts/admin/addon/components/schema-setting/editor.gjs b/app/assets/javascripts/admin/addon/components/schema-setting/editor.gjsindex e8e5fd728c6..41fb3d33bc1 100644--- a/app/assets/javascripts/admin/addon/components/schema-setting/editor.gjs+++ b/app/assets/javascripts/admin/addon/components/schema-setting/editor.gjs@@ -3,7 +3,7 @@ import { tracked } from "@glimmer/tracking";import { fn } from "@ember/helper";import { action } from "@ember/object";import { service } from "@ember/service";-import { gt } from "truth-helpers";+import { gt, not } from "truth-helpers";import DButton from "discourse/components/d-button";import { popupAjaxError } from "discourse/lib/ajax-error";import { cloneJSON } from "discourse/lib/object";@@ -225,6 +225,44 @@ export default class SchemaSettingNewEditor extends Component {}}+ @action+ moveUp() {+ if (this.canMoveUp) {+ this.#swapAdjacentItems(this.activeIndex, this.activeIndex - 1);+ this.activeIndex = this.activeIndex - 1;+ }+ }++ @action+ moveDown() {+ if (this.canMoveDown) {+ this.#swapAdjacentItems(this.activeIndex, this.activeIndex + 1);+ this.activeIndex = this.activeIndex + 1;+ }+ }++ #swapAdjacentItems(fromIndex, toIndex) {+ const item = this.activeData[fromIndex];+ const fromCallback = this.inputFieldObserver[fromIndex];+ const toCallback = this.inputFieldObserver[toIndex];++ // Move the data+ this.activeData.removeAt(fromIndex);+ this.activeData.insertAt(toIndex, item);++ // Swap the observer callbacks to match new positions+ this.inputFieldObserver[toIndex] = fromCallback;+ this.inputFieldObserver[fromIndex] = toCallback;+ }++ get canMoveUp() {+ return this.activeIndex > 0;+ }++ get canMoveDown() {+ return this.activeIndex < this.activeData.length - 1;+ }+@actionsaveChanges() {this.saveButtonDisabled = true;@@ -293,13 +331,30 @@ export default class SchemaSettingNewEditor extends Component {/>{{/each}}- {{#if (gt this.fields.length 0)}}+ <div class="schema-setting-editor__field-actions">+ <DButton+ @action={{this.moveUp}}+ @icon="chevron-up"+ @disabled={{not this.canMoveUp}}+ @ariaLabel={{i18n "admin.customize.schema.move_up"}}+ class="btn-default schema-setting-editor__move-up-btn"+ /><DButton- @action={{this.removeItem}}- @icon="trash-can"- class="btn-danger schema-setting-editor__remove-btn"+ @action={{this.moveDown}}+ @icon="chevron-down"+ @disabled={{not this.canMoveDown}}+ @ariaLabel={{i18n "admin.customize.schema.move_down"}}+ class="btn-default schema-setting-editor__move-down-btn"/>- {{/if}}++ {{#if (gt this.fields.length 0)}}+ <DButton+ @action={{this.removeItem}}+ @icon="trash-can"+ class="btn-danger schema-setting-editor__remove-btn"+ />+ {{/if}}+ </div></div></div></div>diff --git a/app/assets/javascripts/discourse/tests/integration/components/admin-schema-setting/editor-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/admin-schema-setting/editor-test.gjsindex 4c9cbd23005..b6450a60681 100644--- a/app/assets/javascripts/discourse/tests/integration/components/admin-schema-setting/editor-test.gjs+++ b/app/assets/javascripts/discourse/tests/integration/components/admin-schema-setting/editor-test.gjs@@ -83,6 +83,8 @@ class InputFieldsFromDOM {const TOP_LEVEL_ADD_BTN = ".schema-setting-editor__tree-add-button.--root";const REMOVE_ITEM_BTN = ".schema-setting-editor__remove-btn";+const MOVE_UP_BTN = ".schema-setting-editor__move-up-btn";+const MOVE_DOWN_BTN = ".schema-setting-editor__move-down-btn";module("Integration | Admin | Themes | Component | schema-setting/editor",@@ -1712,6 +1714,114 @@ module(assert.dom(inputFields.fields.name.inputElement).hasValue("item 1");assert.dom(".--back-btn").doesNotExist();});++ test("move buttons reorder root level items correctly", async function (assert) {+ const setting = schemaAndData(1);++ await render(+ <template>+ <AdminSchemaSettingEditor+ @id="1"+ @setting={{setting}}+ @schema={{setting.objects_schema}}+ @routeToRedirect="adminCustomizeThemes.show"+ />+ </template>+ );++ const tree = new TreeFromDOM();++ // Verify initial order+ assert.dom(tree.nodes[0].textElement).hasText("item 1");+ assert.dom(tree.nodes[1].textElement).hasText("item 2");++ // Test move up: Select second item and move up+ await click(tree.nodes[1].element);+ tree.refresh();+ await click(MOVE_UP_BTN);+ tree.refresh();++ assert.dom(tree.nodes[0].textElement).hasText("item 2");+ assert.dom(tree.nodes[1].textElement).hasText("item 1");++ // Test move down: Select first item (was originally second) and move down+ await click(tree.nodes[0].element);+ tree.refresh();+ await click(MOVE_DOWN_BTN);+ tree.refresh();++ // Should be back to original order+ assert.dom(tree.nodes[0].textElement).hasText("item 1");+ assert.dom(tree.nodes[1].textElement).hasText("item 2");+ });++ test("move buttons are disabled appropriately for root level items", async function (assert) {+ const setting = schemaAndData(1);++ await render(+ <template>+ <AdminSchemaSettingEditor+ @id="1"+ @setting={{setting}}+ @schema={{setting.objects_schema}}+ @routeToRedirect="adminCustomizeThemes.show"+ />+ </template>+ );++ const tree = new TreeFromDOM();++ // First item should have move up disabled+ await click(tree.nodes[0].element);+ tree.refresh();++ assert.dom(MOVE_UP_BTN).isDisabled();+ assert.dom(MOVE_DOWN_BTN).isNotDisabled();++ // Last item should have move down disabled+ await click(tree.nodes[1].element);+ tree.refresh();++ assert.dom(MOVE_UP_BTN).isNotDisabled();+ assert.dom(MOVE_DOWN_BTN).isDisabled();+ });++ test("move up button reorders nested items correctly", async function (assert) {+ const setting = schemaAndData(1);++ await render(+ <template>+ <AdminSchemaSettingEditor+ @id="1"+ @setting={{setting}}+ @schema={{setting.objects_schema}}+ @routeToRedirect="adminCustomizeThemes.show"+ />+ </template>+ );++ const tree = new TreeFromDOM();++ // Navigate to nested level+ await click(tree.nodes[0].children[1].element);+ tree.refresh();++ // Verify initial order of nested items+ assert.dom(tree.nodes[0].textElement).hasText("child 1-1");+ assert.dom(tree.nodes[1].textElement).hasText("child 1-2");++ // Select second nested item+ await click(tree.nodes[1].element);+ tree.refresh();++ // Click move up button+ await click(MOVE_UP_BTN);+ tree.refresh();++ // Verify nested items are reordered+ assert.dom(tree.nodes[0].textElement).hasText("child 1-2");+ assert.dom(tree.nodes[1].textElement).hasText("child 1-1");+ });});@@ -2228,5 +2338,74 @@ module(.dom(inputFields.fields.name.descriptionElement).hasText("Description for level 2");});++ test("move buttons reorder items correctly", async function (assert) {+ const setting = schemaAndData(1, SCHEMA_MODES.SITE_SETTING);+ await render(+ <template>+ <AdminSchemaSettingEditor+ @id="1"+ @setting={{setting}}+ @schema={{setting.schema}}+ @routeToRedirect="adminPlugins.show.settings"+ />+ </template>+ );++ const tree = new TreeFromDOM();++ // Verify initial order+ assert.dom(tree.nodes[0].textElement).hasText("item 1");+ assert.dom(tree.nodes[1].textElement).hasText("item 2");++ // Test move up: Select second item and move up+ await click(tree.nodes[1].element);+ tree.refresh();+ await click(MOVE_UP_BTN);+ tree.refresh();++ assert.dom(tree.nodes[0].textElement).hasText("item 2");+ assert.dom(tree.nodes[1].textElement).hasText("item 1");++ // Test move down: Select first item (was originally second) and move down+ await click(tree.nodes[0].element);+ tree.refresh();+ await click(MOVE_DOWN_BTN);+ tree.refresh();++ // Should be back to original order+ assert.dom(tree.nodes[0].textElement).hasText("item 1");+ assert.dom(tree.nodes[1].textElement).hasText("item 2");+ });++ test("move buttons are disabled appropriately", async function (assert) {+ const setting = schemaAndData(1, SCHEMA_MODES.SITE_SETTING);+ await render(+ <template>+ <AdminSchemaSettingEditor+ @id="1"+ @setting={{setting}}+ @schema={{setting.schema}}+ @routeToRedirect="adminPlugins.show.settings"+ />+ </template>+ );++ const tree = new TreeFromDOM();++ // First item should have move up disabled+ await click(tree.nodes[0].element);+ tree.refresh();++ assert.dom(MOVE_UP_BTN).isDisabled();+ assert.dom(MOVE_DOWN_BTN).isNotDisabled();++ // Last item should have move down disabled+ await click(tree.nodes[1].element);+ tree.refresh();++ assert.dom(MOVE_UP_BTN).isNotDisabled();+ assert.dom(MOVE_DOWN_BTN).isDisabled();+ });});diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 83d23cc59c1..2b11a6e6f39 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -6660,7 +6660,7 @@ en:description: "Settings that allow themes to have greater control over the full site experience by overriding a small subset of core site settings."themes:title: "Themes"- description: 'Site-wide themes that change the overall look and feel of your site for all users.'+ description: "Site-wide themes that change the overall look and feel of your site for all users."themes_intro: "Install a new theme to get started, or create your own from scratch using these resources."new_theme: "New theme"back: "Back to themes"@@ -7107,6 +7107,8 @@ en:schema:title: "Edit %{name} setting"back_button: "Back to %{name}"+ move_up: "Move up"+ move_down: "Move down"fields:required: "*required"groups: