From 49b73de11ad224e314933593728a92e2b2eadef4 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Mon, 28 Mar 2022 20:02:28 +0200 Subject: [PATCH] Fixes #4024 - Tree select value cannot be set to "-" (empty) with Trigger/Scheduler/Core workflow. --- .../_ui_element/_application_selector.coffee | 2 +- .../_ui_element/core_workflow_perform.coffee | 6 +-- .../_ui_element/multiselect_search.coffee | 35 +++++++++++++++++ .../_ui_element/ticket_perform_action.coffee | 2 + .../_ui_element/tree_select_search.coffee | 8 +++- .../form_handler_core_workflow.coffee | 6 ++- public/assets/tests/qunit/form.js | 20 ++++++++++ public/assets/tests/qunit/form_tree_select.js | 1 + spec/system/ticket/create_spec.rb | 38 +++++++++++++++++++ 9 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 app/assets/javascripts/app/controllers/_ui_element/multiselect_search.coffee diff --git a/app/assets/javascripts/app/controllers/_ui_element/_application_selector.coffee b/app/assets/javascripts/app/controllers/_ui_element/_application_selector.coffee index e0b6d95e6..7e12a836c 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/_application_selector.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/_application_selector.coffee @@ -113,7 +113,7 @@ class App.UiElement.ApplicationSelector config.operator = operator elements["#{groupKey}.#{config.name}"] = config - if config.tag == 'select' + if /^(tree_|multi)?select$/.test(config.tag) config.multiple = true if attribute.out_of_office diff --git a/app/assets/javascripts/app/controllers/_ui_element/core_workflow_perform.coffee b/app/assets/javascripts/app/controllers/_ui_element/core_workflow_perform.coffee index 53c048aeb..c22917f07 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/core_workflow_perform.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/core_workflow_perform.coffee @@ -73,7 +73,7 @@ class App.UiElement.core_workflow_perform extends App.UiElement.ApplicationSelec config = _.clone(row) if config.tag is 'boolean' config.tag = 'select' - if /^((multi)?select)$/.test(config.tag) + if /^(tree_|multi)?select$/.test(config.tag) config.multiple = true config.default = undefined if config.type is 'email' || config.type is 'tel' @@ -121,14 +121,14 @@ class App.UiElement.core_workflow_perform extends App.UiElement.ApplicationSelec currentOperator = elementRow.find('.js-operator option:selected').attr('value') name = @buildValueName(elementFull, elementRow, groupAndAttribute, elements, meta, attribute) - if !_.contains(['add_option', 'remove_option', 'set_fixed_to', 'select', 'multiselect', 'execute', 'fill_in', 'fill_in_empty'], currentOperator) + if !_.contains(['add_option', 'remove_option', 'set_fixed_to', 'select', 'execute', 'fill_in', 'fill_in_empty'], currentOperator) elementRow.find('.js-value').addClass('hide').html('') return super(elementFull, elementRow, groupAndAttribute, elements, meta, attribute) @buildValueConfigMultiple: (config, meta) -> - if _.contains(['add_option', 'remove_option', 'set_fixed_to', 'select', 'multiselect'], meta.operator) + if _.contains(['add_option', 'remove_option', 'set_fixed_to', 'select'], meta.operator) config.multiple = true config.nulloption = true else diff --git a/app/assets/javascripts/app/controllers/_ui_element/multiselect_search.coffee b/app/assets/javascripts/app/controllers/_ui_element/multiselect_search.coffee new file mode 100644 index 000000000..02aefad66 --- /dev/null +++ b/app/assets/javascripts/app/controllers/_ui_element/multiselect_search.coffee @@ -0,0 +1,35 @@ +# coffeelint: disable=camel_case_classes +class App.UiElement.multiselect_search extends App.UiElement.ApplicationUiElement + @render: (attribute, params) -> + + # set multiple option + if attribute.multiple + attribute.multiple = 'multiple' + else + attribute.multiple = '' + + delete attribute.filter + + # build options list based on config + @getConfigOptionList(attribute, params) + + # build options list based on relation + @getRelationOptionList(attribute, params) + + # add null selection if needed + @addNullOption(attribute, params) + + # sort attribute.options + @sortOptions(attribute, params) + + # find selected/checked item of list + @selectedOptions(attribute, params) + + # disable item of list + @disabledOptions(attribute, params) + + # filter attributes + @filterOption(attribute, params) + + # return item + $( App.view('generic/select')(attribute: attribute) ) diff --git a/app/assets/javascripts/app/controllers/_ui_element/ticket_perform_action.coffee b/app/assets/javascripts/app/controllers/_ui_element/ticket_perform_action.coffee index 93f185047..1b8cfb7fc 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/ticket_perform_action.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/ticket_perform_action.coffee @@ -319,6 +319,8 @@ class App.UiElement.ticket_perform_action config['value'] = _.clone(attribute.value[groupAndAttribute]['value']) config.multiple = false config.nulloption = config.null + if config.tag is 'multiselect' + config.multiple = true if config.tag is 'checkbox' config.tag = 'select' tagSearch = "#{config.tag}_search" diff --git a/app/assets/javascripts/app/controllers/_ui_element/tree_select_search.coffee b/app/assets/javascripts/app/controllers/_ui_element/tree_select_search.coffee index 7022ad61d..ff35e0274 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/tree_select_search.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/tree_select_search.coffee @@ -6,7 +6,13 @@ class App.UiElement.tree_select_search extends App.UiElement.ApplicationUiElemen attribute = clone(localAttribute) # set multiple option - attribute.multiple = 'multiple' + if attribute.multiple + attribute.multiple = 'multiple' + else + attribute.multiple = '' + + # add null selection if needed + @addNullOption(attribute, params) # sort attribute.options @sortOptions(attribute, params) diff --git a/app/assets/javascripts/app/controllers/ticket_zoom/form_handler_core_workflow.coffee b/app/assets/javascripts/app/controllers/ticket_zoom/form_handler_core_workflow.coffee index 6dbcbb007..ca3b83302 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom/form_handler_core_workflow.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom/form_handler_core_workflow.coffee @@ -191,7 +191,11 @@ class App.FormHandlerCoreWorkflow return if _.isEmpty(data) for field, values of data - form.find('[name="' + field + '"]').val(data[field]) + fieldElement = $("div[data-attribute-name='" + field + "']") + if fieldElement.hasClass('tree_select') + fieldElement.find(".js-option[data-value='" + data[field] + "']").trigger('click') + else + form.find('[name="' + field + '"]').val(data[field]) coreWorkflowParams[classname][field] = data[field] # fill in data in input fields diff --git a/public/assets/tests/qunit/form.js b/public/assets/tests/qunit/form.js index a84d83478..03821c2d3 100644 --- a/public/assets/tests/qunit/form.js +++ b/public/assets/tests/qunit/form.js @@ -1631,3 +1631,23 @@ QUnit.test("Fixes #3909 - Wrong size for textareas in triggers and core workflow }) .finally(done) }); + +QUnit.test("Fixes #4024 - Tree select value cannot be set to \"-\" (empty) with Trigger/Scheduler/Core workflow.", assert => { + $('#qunit').append('

Fixes #4024 - Tree select value cannot be set to "-" (empty) with Trigger/Scheduler/Core workflow.

') + var el = $('#form22') + new App.ControllerForm({ + el: el, + model: { + configure_attributes: [ + { name: '4042_select', display: '4042_select', tag: 'select_search', null: true, nulloption: true, multiple: true, options: { 'a': 'a', 'b': 'b' } }, + { name: '4042_multiselect', display: '4042_multiselect', tag: 'multiselect_search', null: true, nulloption: true, multiple: true, options: { 'a': 'a', 'b': 'b' } }, + { name: '4042_tree_select', display: '4042_tree_select', tag: 'tree_select_search', null: true, nulloption: true, multiple: true, options: [{ 'value': 'a', 'name': 'a'}, { 'value': 'b', 'name': 'b'}] }, + ], + }, + autofocus: true + }); + + assert.equal(el.find('select[name="4042_select"] option[value=""]').text(), '-', '4042_select has nulloption') + assert.equal(el.find('select[name="4042_multiselect"] option[value=""]').text(), '-', '4042_multiselect has nulloption') + assert.equal(el.find('select[name="4042_tree_select"] option[value=""]').text(), '-', '4042_tree_select has nulloption') +}); diff --git a/public/assets/tests/qunit/form_tree_select.js b/public/assets/tests/qunit/form_tree_select.js index 92f709076..441c12ef5 100644 --- a/public/assets/tests/qunit/form_tree_select.js +++ b/public/assets/tests/qunit/form_tree_select.js @@ -203,6 +203,7 @@ QUnit.test("form elements check", assert => { "tag": "tree_select_search", "null": true, "translate": true, + "multiple": true, "value": ['aa::aab', 'bb', 'aa::aac::33'], "options": [ { diff --git a/spec/system/ticket/create_spec.rb b/spec/system/ticket/create_spec.rb index b826787eb..004fe7d06 100644 --- a/spec/system/ticket/create_spec.rb +++ b/spec/system/ticket/create_spec.rb @@ -908,4 +908,42 @@ RSpec.describe 'Ticket Create', type: :system do expect(page).to have_no_text('Test Other Agent') end end + + describe 'Tree select value cannot be set to "-" (empty) with Trigger/Scheduler/Core workflow #4024', authenticated_as: :authenticate, db_strategy: :reset do + let(:field_name) { SecureRandom.uuid } + let(:field) do + create :object_manager_attribute_tree_select, name: field_name, display: field_name, screens: attributes_for(:required_screen) + ObjectManager::Attribute.migration_execute + end + let(:workflow) do + create(:core_workflow, + object: 'Ticket', + condition_selected: { 'ticket.priority_id'=>{ 'operator' => 'is', 'value' => Ticket::Priority.find_by(name: '3 high').id.to_s } }, + perform: { "ticket.#{field_name}" => { 'operator' => 'select', 'select' => 'Incident' } }) + end + let(:workflow2) do + create(:core_workflow, + object: 'Ticket', + condition_selected: { 'ticket.priority_id'=>{ 'operator' => 'is', 'value' => Ticket::Priority.find_by(name: '2 normal').id.to_s } }, + perform: { "ticket.#{field_name}" => { 'operator' => 'select', 'select' => '' } }) + end + + def authenticate + field + workflow + workflow2 + true + end + + before do + visit 'ticket/create' + end + + it 'does select the field value properly' do + page.find('[name=priority_id]').select '3 high' + wait.until { page.find("input[name='#{field_name}']", visible: :all).value == 'Incident' } + page.find('[name=priority_id]').select '2 normal' + wait.until { page.find("input[name='#{field_name}']", visible: :all).value == '' } + end + end end