From b5fce333f6a7dac13852b77f11f16bfb7ce617b8 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 7 Apr 2020 17:54:30 +1000 Subject: [PATCH] wip --- .../components/wizard-custom-field.js.es6 | 16 +++++++------- .../components/wizard-mapper-input.js.es6 | 3 ++- .../components/wizard-mapper-selector.js.es6 | 9 ++------ .../discourse/controllers/admin-wizard.js.es6 | 15 +++++++------ .../discourse/lib/wizard-mapper.js.es6 | 2 +- .../javascripts/discourse/lib/wizard.js.es6 | 12 ++++++++--- .../components/wizard-custom-step.hbs | 1 + .../components/wizard-mapper-input.hbs | 2 +- .../components/wizard-mapper-selector.hbs | 1 - assets/javascripts/wizard-custom.js | 2 +- .../wizard/initializers/custom.js.es6 | 6 ++++++ .../components/wizard-field-dropdown.hbs | 13 ++++++------ assets/stylesheets/common/wizard-mapper.scss | 13 ++++++++---- controllers/custom_wizard/wizard.rb | 3 +-- lib/custom_wizard/builder.rb | 7 +++++-- lib/custom_wizard/mapper.rb | 21 +++++++++++++------ lib/custom_wizard/wizard.rb | 8 +++---- .../custom_wizard/wizard_field_serializer.rb | 4 ++++ 18 files changed, 86 insertions(+), 52 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 035208c5..a7bf7576 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -56,20 +56,22 @@ export default Component.extend({ prefillOptions(fieldType) { let options = { wizardFieldSelection: true, - textSelection: 'key,value', + textSelection: true, userFieldSelection: 'key,value', context: 'field' } let outputSelectionType = { - category: 'category', - tag: 'tag', - group: 'group', - dropdown: 'text' + category: ['category'], + tag: ['tag'], + group: ['group'], + dropdown: ['list'] }[fieldType]; - options[`${outputSelectionType}Selection`] = 'output'; - options.outputDefaultSelection = outputSelectionType; + outputSelectionType.forEach(function(type) { + options[`${type}Selection`] = 'output'; + options.outputDefaultSelection = type; + }); return options; }, diff --git a/assets/javascripts/discourse/components/wizard-mapper-input.js.es6 b/assets/javascripts/discourse/components/wizard-mapper-input.js.es6 index 3c786026..35fbd334 100644 --- a/assets/javascripts/discourse/components/wizard-mapper-input.js.es6 +++ b/assets/javascripts/discourse/components/wizard-mapper-input.js.es6 @@ -1,5 +1,5 @@ import { computed, set } from "@ember/object"; -import { alias, equal, or } from "@ember/object/computed"; +import { alias, equal, or, not } from "@ember/object/computed"; import { newPair, connectorContent, inputTypesContent } from '../lib/wizard-mapper'; import Component from "@ember/component"; import { observes } from "discourse-common/utils/decorators"; @@ -13,6 +13,7 @@ export default Component.extend({ isValidation: equal('inputType', 'validation'), hasOutput: or('isConditional', 'isAssignment'), hasPairs: or('isConditional', 'isAssociation', 'isValidation'), + canAddPair: not('isAssignment'), connectors: computed(function() { return connectorContent('output', this.input.type, this.options) }), inputTypes: computed(function() { return inputTypesContent(this.options) }), diff --git a/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 b/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 index 833e6634..e421f3bb 100644 --- a/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 +++ b/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6 @@ -3,14 +3,14 @@ import { computed } from "@ember/object"; import { default as discourseComputed, observes, on } from "discourse-common/utils/decorators"; import { getOwner } from 'discourse-common/lib/get-owner'; import { defaultSelectionType, selectionTypes } from '../lib/wizard-mapper'; -import { snakeCase, selectKitContent } from '../lib/wizard'; +import { snakeCase } from '../lib/wizard'; import Component from "@ember/component"; import { bind } from "@ember/runloop"; export default Component.extend({ classNames: 'mapper-selector', groups: alias('site.groups'), - categories: computed(function() { return selectKitContent(this.site.categories) }), + categories: alias('site.categories'), showText: computed('activeType', function() { return this.showInput('text') }), showWizardField: computed('activeType', function() { return this.showInput('wizardField') }), showUserField: computed('activeType', function() { return this.showInput('userField') }), @@ -67,11 +67,6 @@ export default Component.extend({ return I18n.t(`admin.wizard.selector.label.${snakeCase(type)}`) }, - @discourseComputed('showTypes') - typeSelectorIcon(showTypes) { - return showTypes ? 'chevron-down' : 'chevron-right'; - }, - @observes('inputType') resetActiveType() { this.set('activeType', defaultSelectionType(this.selectorType, this.options)); diff --git a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 index ae9c832b..9d82b4e0 100644 --- a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 @@ -15,13 +15,16 @@ export default Controller.extend({ @observes('currentStep') resetCurrentObjects() { const currentStep = this.currentStep; - const fields = currentStep.fields; - const actions = currentStep.actions; - this.setProperties({ - currentField: fields.length ? fields[0] : null, - currentAction: actions.length ? actions[0] : null - }); + if (currentStep) { + const fields = currentStep.fields; + const actions = currentStep.actions; + + this.setProperties({ + currentField: fields.length ? fields[0] : null, + currentAction: actions.length ? actions[0] : null + }); + } scheduleOnce('afterRender', () => ($("body").addClass('admin-wizard'))); }, diff --git a/assets/javascripts/discourse/lib/wizard-mapper.js.es6 b/assets/javascripts/discourse/lib/wizard-mapper.js.es6 index eb6b1344..ebb280e3 100644 --- a/assets/javascripts/discourse/lib/wizard-mapper.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-mapper.js.es6 @@ -52,7 +52,7 @@ function defaultConnector(connectorType, inputType, opts = {}) { function connectorContent(connectorType, inputType, opts) { let connector = opts[`${connectorType}Connector`]; - if (!connector && connectorType === 'output') { + if ((!connector && connectorType === 'output') || inputType === 'association') { connector = defaultConnector(connectorType, inputType, opts); } diff --git a/assets/javascripts/discourse/lib/wizard.js.es6 b/assets/javascripts/discourse/lib/wizard.js.es6 index c4772f8c..6bd6e62b 100644 --- a/assets/javascripts/discourse/lib/wizard.js.es6 +++ b/assets/javascripts/discourse/lib/wizard.js.es6 @@ -139,6 +139,13 @@ const mappedProperties = { ] } +const advancedFieldTypes = [ + 'category', + 'tag', + 'group', + 'dropdown' +] + const advancedFieldProperties = [ 'prefill', 'content' @@ -161,12 +168,11 @@ const advancedProperties = { 'required_data', 'permitted_params' ], - field: advancedFieldProperties.reduce( + field: advancedFieldTypes.reduce( function(map, type) { - console.log(map, type); map[type] = advancedFieldProperties; if (type === 'category') { - map.push('property'); + map[type].push('property'); } return map; }, {} diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs index 6f680d19..edc79bcd 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-step.hbs @@ -74,6 +74,7 @@ inputs=step.permitted_params options=(hash pairConnector='set' + inputTypes='association' keyPlaceholder='admin.wizard.param_key' valuePlaceholder='admin.wizard.submission_key' context='step' diff --git a/assets/javascripts/discourse/templates/components/wizard-mapper-input.hbs b/assets/javascripts/discourse/templates/components/wizard-mapper-input.hbs index 2dea8b69..8e2ee7de 100644 --- a/assets/javascripts/discourse/templates/components/wizard-mapper-input.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-mapper-input.hbs @@ -14,7 +14,7 @@ removePair=(action 'removePair')}} {{/each}} - {{#if hasOutput}} + {{#if canAddPair}} {{d-icon 'plus'}} diff --git a/assets/javascripts/discourse/templates/components/wizard-mapper-selector.hbs b/assets/javascripts/discourse/templates/components/wizard-mapper-selector.hbs index 49d29c22..b0c0d8fd 100644 --- a/assets/javascripts/discourse/templates/components/wizard-mapper-selector.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-mapper-selector.hbs @@ -2,7 +2,6 @@ {{#if hasTypes}} {{activeTypeLabel}} - {{d-icon typeSelectorIcon class="type-selector-icon"}} {{#if showTypes}} diff --git a/assets/javascripts/wizard-custom.js b/assets/javascripts/wizard-custom.js index 3a8382e7..40bcfd9c 100644 --- a/assets/javascripts/wizard-custom.js +++ b/assets/javascripts/wizard-custom.js @@ -82,7 +82,7 @@ //= require discourse/components/d-button //= require discourse/components/composer-editor //= require discourse/components/d-editor -//= require discourse/components/popup-input-tip +//= require discourse/components/input-tip //= require discourse/components/emoji-picker //= require discourse/templates/components/conditional-loading-spinner diff --git a/assets/javascripts/wizard/initializers/custom.js.es6 b/assets/javascripts/wizard/initializers/custom.js.es6 index 0738244c..48c50204 100644 --- a/assets/javascripts/wizard/initializers/custom.js.es6 +++ b/assets/javascripts/wizard/initializers/custom.js.es6 @@ -277,5 +277,11 @@ export default { return valid; } }); + + WizardFieldDropdown.reopen({ + didInsertElement() { + console.log(this.field) + } + }) } }; diff --git a/assets/javascripts/wizard/templates/components/wizard-field-dropdown.hbs b/assets/javascripts/wizard/templates/components/wizard-field-dropdown.hbs index 999ace4e..cde88a50 100644 --- a/assets/javascripts/wizard/templates/components/wizard-field-dropdown.hbs +++ b/assets/javascripts/wizard/templates/components/wizard-field-dropdown.hbs @@ -1,6 +1,7 @@ -{{combo-box elementId=field.id - class=fieldClass - value=field.value - content=field.content - nameProperty="label" - tabindex="9"}} \ No newline at end of file +{{combo-box + class=fieldClass + value=field.value + content=field.content + options=(hash + none="select_kit.default_header_text" + )}} \ No newline at end of file diff --git a/assets/stylesheets/common/wizard-mapper.scss b/assets/stylesheets/common/wizard-mapper.scss index 8322749f..15dfafd1 100644 --- a/assets/stylesheets/common/wizard-mapper.scss +++ b/assets/stylesheets/common/wizard-mapper.scss @@ -20,8 +20,9 @@ > .mapper-connector.single { width: min-content; - margin-bottom: 10px; - height: 20px + margin-bottom: 5px; + height: 20px; + border: 2px solid $primary-low; } } @@ -56,7 +57,7 @@ position: absolute; right: -14px; top: 0px; - padding: 2px 5px; + padding: 2px 4px; transform: translateY(-50%); background: $secondary; border-radius: 50%; @@ -66,7 +67,7 @@ } .add-mapper-input { - display: block; + display: inline-block; } .mapper-connector { @@ -86,6 +87,10 @@ align-items: center; justify-content: center; } + + .connector-single { + padding: 0 5px; + } } .mapper-selector { diff --git a/controllers/custom_wizard/wizard.rb b/controllers/custom_wizard/wizard.rb index 19e550ce..ebe6cd55 100644 --- a/controllers/custom_wizard/wizard.rb +++ b/controllers/custom_wizard/wizard.rb @@ -25,8 +25,7 @@ class CustomWizard::WizardController < ::ApplicationController builder_opts[:reset] = params[:reset] || builder.wizard.restart_on_revisit if builder.wizard.present? - wizard = builder.build(builder_opts, params) - render_serialized(wizard, ::CustomWizardSerializer) + render_serialized(builder.build(builder_opts, params), ::CustomWizardSerializer) else render json: { error: I18n.t('wizard.none') } end diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index 47e19400..25d29d83 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -244,7 +244,7 @@ class CustomWizard::Builder with_type: true } ).perform - + if content[:type] == 'association' content[:result] = content[:result].map do |item| { @@ -265,7 +265,10 @@ class CustomWizard::Builder CustomWizard::Mapper.new( inputs: prefill, user: @wizard.user, - data: @submissions.last + data: @submissions.last, + opts: { + debug: true + } ).perform end end diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index bde23b5c..ef341ffa 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -63,12 +63,12 @@ class CustomWizard::Mapper end end end - + perform_result end def build_result(result, type) - if opts[:with_type] + if @opts[:with_type] { type: type, result: result @@ -83,12 +83,13 @@ class CustomWizard::Mapper pairs.each do |pair| key = map_field(pair['key'], pair['key_type']) - operator = map_operator(pair['connector']) + connector = pair['connector'] + operator = map_operator(connector) value = interpolate(map_field(pair['value'], pair['value_type'])) - value = "/#{value}/" if pair['connector'] == 'regex' - + value = Regexp.new(value) if connector == 'regex' + begin - failed = true unless key.public_send(operator, value) + failed = !cast_result(key.public_send(operator, value), connector) rescue NoMethodError # end @@ -97,6 +98,14 @@ class CustomWizard::Mapper !failed end + def cast_result(result, connector) + if connector == 'regex' + result == 0 ? true : false + else + result + end + end + def map_pairs(pairs) result = [] diff --git a/lib/custom_wizard/wizard.rb b/lib/custom_wizard/wizard.rb index 17fc4277..35657494 100644 --- a/lib/custom_wizard/wizard.rb +++ b/lib/custom_wizard/wizard.rb @@ -142,10 +142,10 @@ class CustomWizard::Wizard return true if mapper.blank? mapper.all? do |m| - if m.type === 'assignment' - GroupUser.exists?(group_id: m.result, user_id: user.id) - elsif m.type === 'validation' - mapper.result + if m[:type] === 'assignment' + GroupUser.exists?(group_id: m[:result], user_id: user.id) + elsif m[:type] === 'validation' + m[:result] else true end diff --git a/serializers/custom_wizard/wizard_field_serializer.rb b/serializers/custom_wizard/wizard_field_serializer.rb index ef8dfb8f..62022c05 100644 --- a/serializers/custom_wizard/wizard_field_serializer.rb +++ b/serializers/custom_wizard/wizard_field_serializer.rb @@ -45,4 +45,8 @@ class CustomWizardFieldSerializer < ::WizardFieldSerializer def content object.content end + + def include_choices? + object.choices.present? + end end \ No newline at end of file