From 615c4b4c648fe62b4fb21334e6b5fef1dff52851 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Fri, 4 Dec 2020 11:35:56 +0100 Subject: [PATCH] Fixes #3320 - Problem with Adding new Rows in Tree-Select. --- .../object_manager_attribute.coffee | 22 ++++-- spec/system/system/object_manager_spec.rb | 68 +++++++++++++++++++ 2 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 spec/system/system/object_manager_spec.rb diff --git a/app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee b/app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee index 63a49d9b0..04358f42c 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/object_manager_attribute.coffee @@ -362,6 +362,20 @@ class App.UiElement.object_manager_attribute extends App.UiElement.ApplicationUi for subChild in child.children @buildRow(element, subChild, level + 1) + @findParent: (element, level, mode) -> + parent = $(element).closest('tr') + parent.nextAll().each(-> + if parseInt($(@).find('.js-key').attr('level')) > level && mode is 'first' + parent = $(@) + return true + if parseInt($(@).find('.js-key').attr('level')) >= level && mode is 'last' + parent = $(@) + return true + return false + ) + + return parent + @tree_select: (item, localParams, params, attribute) -> params.data_option ||= {} params.data_option.options ||= [] @@ -374,16 +388,16 @@ class App.UiElement.object_manager_attribute extends App.UiElement.ApplicationUi item.on('click', '.js-addRow', (e) => e.stopPropagation() e.preventDefault() - addRow = $(e.currentTarget).closest('tr') - level = parseInt(addRow.find('.js-key').attr('level')) + level = parseInt($(e.currentTarget).closest('tr').find('.js-key').attr('level')) + addRow = @findParent(e.currentTarget, level, 'first') @buildRow(item, {}, level, addRow) ) item.on('click', '.js-addChild', (e) => e.stopPropagation() e.preventDefault() - addRow = $(e.currentTarget).closest('tr') - level = parseInt(addRow.find('.js-key').attr('level')) + 1 + level = parseInt($(e.currentTarget).closest('tr').find('.js-key').attr('level')) + 1 + addRow = @findParent(e.currentTarget, level, 'last') @buildRow(item, {}, level, addRow) ) diff --git a/spec/system/system/object_manager_spec.rb b/spec/system/system/object_manager_spec.rb new file mode 100644 index 000000000..c9911f7af --- /dev/null +++ b/spec/system/system/object_manager_spec.rb @@ -0,0 +1,68 @@ +require 'rails_helper' + +# https://github.com/zammad/zammad/issues/266 +RSpec.describe 'Admin Panel > Objects', type: :system, authenticated_as: true do + before do + visit '/#system/object_manager' + end + + it 'verifies option creation order of new tree select options' do + + # create new field + page.find('.js-new').click + + # set meta information + fill_in 'Name', with: 'tree1' + fill_in 'Display', with: 'tree1' + page.find('select[name=data_type]').select('Tree Select') + + # create 3 childs + first_add_child = page.first('div.js-addChild') + first_add_child.click + first_add_child.click + first_add_child.click + + # create 1 top level node sibling + page.first('div.js-addRow').click + + # create 3 childs for the new top level node + page.all('div.js-addChild').last.click + page.all('div.js-addChild').last.click + page.all('div.js-addChild').last.click + + # create new top level nodes by first and second top level node + add_rows = page.all('div.js-addRow') + add_rows[0].click + add_rows[4].click + + # add numbers to all inputs to verify order in config later + number = 1 + page.all('input.js-key').each do |input| + input.send_keys(number) + number += 1 + end + + page.find('.js-submit').click + expected_data_options = { 'options' => + [{ 'name' => '1', + 'value' => '1', + 'children' => [{ 'name' => '2', 'value' => '1::2' }, { 'name' => '3', 'value' => '1::3' }, { 'name' => '4', 'value' => '1::4' }] }, + { 'name' => '5', 'value' => '5' }, + { 'name' => '6', + 'value' => '6', + 'children' => + [{ 'name' => '7', + 'value' => '6::7', + 'children' => [{ 'name' => '8', 'value' => '6::7::8', 'children' => [{ 'name' => '9', 'value' => '6::7::8::9' }] }] }] }, + { 'name' => '10', 'value' => '10' }], + 'default' => '', + 'null' => true, + 'relation' => '', + 'nulloption' => true, + 'maxlength' => 255 } + + await_empty_ajax_queue + + expect(ObjectManager::Attribute.last.data_option).to eq(expected_data_options) + end +end