diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 2777f4ef..654fea42 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -1,5 +1,5 @@ import { default as computed, observes, on } from 'discourse-common/utils/decorators'; -import { equal, not } from "@ember/object/computed"; +import { equal, not, or } from "@ember/object/computed"; import { generateSelectKitContent } from '../lib/custom-wizard'; export default Ember.Component.extend({ @@ -34,25 +34,35 @@ export default Ember.Component.extend({ let options = { hasOutput: true, enableConnectors: true, - allowWizard: true, - allowUser: true + wizardFieldSelection: true, + userFieldSelection: true } - if (isCategory) { - options.allowUser = 'key,value'; - options.allowCategory = 'output'; - } - - if (isGroup) { - options.allowUser = 'key,value'; - options.allowGroup = 'output'; - } - - if (isTag) { - options.allowUser = 'key,value'; - options.allowTag = 'output'; + if (isCategory || isGroup || isTag) { + options.userFieldSelection = 'key,value'; + options[`${this.field.type}Selection`] = 'output'; } return options; - } + }, + + canFilter: or('isCategory', 'isTag', 'isGroup'), + + @computed('field.type') + filterOptions(fieldType) { + if (!this.canFilter) return {}; + + let options = { + hasOutput: true, + enableConnectors: true, + wizardFieldSelection: 'key,value', + userFieldSelection: 'key,value', + textDisabled: 'output' + } + + options[`${this.field.type}Selection`] = 'output'; + options[`${this.field.type}AllowMultiple`] = true; + + return options; + }, }); diff --git a/assets/javascripts/discourse/components/wizard-custom-input-chooser.js.es6 b/assets/javascripts/discourse/components/wizard-custom-input-chooser.js.es6 deleted file mode 100644 index 9dd44751..00000000 --- a/assets/javascripts/discourse/components/wizard-custom-input-chooser.js.es6 +++ /dev/null @@ -1,52 +0,0 @@ -import { alias, equal } from "@ember/object/computed"; -import { computed } from "@ember/object"; -import { - default as discourseComputed, - observes -} from "discourse-common/utils/decorators"; - -export default Ember.Component.extend({ - @observes('activeType') - clearValue() { - this.set('value', null); - }, - - @discourseComputed('customPlaceholder') - textPlaceholder(customPlaceholder) { - return customPlaceholder || 'admin.wizard.text'; - }, - - showText: equal('activeType', 'text'), - - showInput(type) { - return this.activeType === type && this[`${type}Enabled`]; - }, - - showWizard: computed('activeType', function() { return this.showInput('wizard') }), - showUser: computed('activeType', function() { return this.showInput('user') }), - showCategory: computed('activeType', function() { return this.showInput('category') }), - showTag: computed('activeType', function() { return this.showInput('tag') }), - showGroup: computed('activeType', function() { return this.showInput('group') }), - - optionEnabled(type) { - const options = this.options; - if (!options) return false; - - const option = options[type]; - if (option === true) return true; - if (typeof option !== 'string') return false; - return option.split(',').indexOf(this.inputType) > -1; - }, - - wizardEnabled: computed('options.allowWizard', function() { return this.optionEnabled('allowWizard') }), - userEnabled: computed('options.allowUser', function() { return this.optionEnabled('allowUser') }), - categoryEnabled: computed('options.allowCategory', function() { return this.optionEnabled('allowCategory') }), - tagEnabled: computed('options.allowTag', function() { return this.optionEnabled('allowTag') }), - groupEnabled: computed('options.allowGroup', function() { return this.optionEnabled('allowGroup') }), - - actions: { - toggleType(type) { - this.set('activeType', type); - } - } -}) \ No newline at end of file diff --git a/assets/javascripts/discourse/components/wizard-custom-input-pair.js.es6 b/assets/javascripts/discourse/components/wizard-custom-input-pair.js.es6 index e2045b64..26a90c47 100644 --- a/assets/javascripts/discourse/components/wizard-custom-input-pair.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-input-pair.js.es6 @@ -3,7 +3,7 @@ import { gt, or, alias } from "@ember/object/computed"; import { computed, observes} from "@ember/object"; export default Ember.Component.extend({ - classNames: 'pair', + classNameBindings: [':input-pair', 'hasConnector::no-connector'], connectors: connectors, hasConnector: or('options.enableConnectors', 'connectorKey'), firstPair: gt('pair.index', 0), diff --git a/assets/javascripts/discourse/components/wizard-custom-input-selector.js.es6 b/assets/javascripts/discourse/components/wizard-custom-input-selector.js.es6 new file mode 100644 index 00000000..7c1067aa --- /dev/null +++ b/assets/javascripts/discourse/components/wizard-custom-input-selector.js.es6 @@ -0,0 +1,71 @@ +import { alias, equal } from "@ember/object/computed"; +import { computed } from "@ember/object"; +import { + default as discourseComputed, + observes +} from "discourse-common/utils/decorators"; +import { defaultSelectionType } from '../lib/custom-wizard'; + +export default Ember.Component.extend({ + classNames: 'input-selector', + groups: alias('site.groups'), + categories: computed(function() { + return this.site.categories.map(c => ({ id: c.id, name: c.name })); + }), + + @observes('options.@each') + resetActiveType() { + this.set('activeType', defaultSelectionType(this.selectorType, this.options)); + }, + + @observes('activeType') + clearValue() { + this.set('value', null); + }, + + @discourseComputed('customPlaceholder') + textPlaceholder(customPlaceholder) { + return customPlaceholder || 'admin.wizard.text'; + }, + + showText: equal('activeType', 'text'), + + showInput(type) { + return this.activeType === type && this[`${type}Enabled`] && !this[`${type}Disabled`]; + }, + + showWizard: computed('activeType', function() { return this.showInput('wizard') }), + showUser: computed('activeType', function() { return this.showInput('user') }), + showCategory: computed('activeType', function() { return this.showInput('category') }), + showTag: computed('activeType', function() { return this.showInput('tag') }), + showGroup: computed('activeType', function() { return this.showInput('group') }), + + optionEnabled(type) { + const options = this.options; + if (!options) return false; + + const option = options[type]; + + if (option === true) return true; + if (typeof option !== 'string') return false; + + const types = [this.selectorType, this.inputType]; + + console.log('running', types, option) + + return option.split(',').filter(o => types.indexOf(o) !== -1).length + }, + + textDisabled: computed('options.textDisabled', 'inputType', function() { return this.optionEnabled('textDisabled') }), + wizardEnabled: computed('options.wizardFieldSelection', 'inputType', function() { return this.optionEnabled('wizardFieldSelection') }), + userEnabled: computed('options.userFieldSelection', 'inputType', function() { return this.optionEnabled('userFieldSelection') }), + categoryEnabled: computed('options.categorySelection', 'inputType', function() { return this.optionEnabled('categorySelection') }), + tagEnabled: computed('options.tagSelection', 'inputType', function() { return this.optionEnabled('tagSelection') }), + groupEnabled: computed('options.groupSelection', 'inputType', function() { return this.optionEnabled('groupSelection') }), + + actions: { + toggleType(type) { + this.set('activeType', type); + } + } +}) \ No newline at end of file diff --git a/assets/javascripts/discourse/components/wizard-custom-input.js.es6 b/assets/javascripts/discourse/components/wizard-custom-input.js.es6 index 0c69a9c3..0858fb7c 100644 --- a/assets/javascripts/discourse/components/wizard-custom-input.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-input.js.es6 @@ -1,12 +1,48 @@ -import { newPair } from '../lib/custom-wizard'; +import { + newPair, + generateSelectKitContent, + defaultInputType +} from '../lib/custom-wizard'; +import { + default as discourseComputed, + on +} from 'discourse-common/utils/decorators'; import { computed } from "@ember/object"; +import { alias } from "@ember/object/computed"; export default Ember.Component.extend({ - classNames: 'custom-input', - outputConnector: computed(function() { - return I18n.t(this.outputConnectorKey || 'admin.wizard.output.connector').toLowerCase(); + classNameBindings: [':custom-input', 'type'], + inputType: alias('input.type'), + outputConnector: computed('inputTypes', function() { + const key = this.outputConnectorKey || `admin.wizard.input.${this.type}.output`; + return I18n.t(key).toLowerCase(); }), + @on('init') + setDefaults() { + if (!this.type) this.set('type', defaultInputType(this.options)); + }, + + @discourseComputed + inputTypes() { + return ['conditional', 'assignment'].map((type) => { + return { + id: type, + name: I18n.t(`admin.wizard.input.${type}.prefix`) + } + }); + }, + + @discourseComputed('options.hasOutput', 'input.type') + hasPairs(hasOutput, inputType) { + return !hasOutput || inputType === 'conditional'; + }, + + @discourseComputed('input.type') + hasOutputConnector(inputType) { + return inputType === 'conditional'; + }, + actions: { addPair() { const pairs = this.get('input.pairs'); diff --git a/assets/javascripts/discourse/components/wizard-custom-inputs.js.es6 b/assets/javascripts/discourse/components/wizard-field-mapper.js.es6 similarity index 93% rename from assets/javascripts/discourse/components/wizard-custom-inputs.js.es6 rename to assets/javascripts/discourse/components/wizard-field-mapper.js.es6 index 7468744a..a0b352f0 100644 --- a/assets/javascripts/discourse/components/wizard-custom-inputs.js.es6 +++ b/assets/javascripts/discourse/components/wizard-field-mapper.js.es6 @@ -3,7 +3,7 @@ import { on } from 'discourse-common/utils/decorators'; import { newInput } from '../lib/custom-wizard'; export default Ember.Component.extend({ - classNames: 'custom-inputs', + classNames: 'field-mapper', actions: { add() { diff --git a/assets/javascripts/discourse/lib/custom-wizard.js.es6 b/assets/javascripts/discourse/lib/custom-wizard.js.es6 index cc7ba277..90c7036c 100644 --- a/assets/javascripts/discourse/lib/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/lib/custom-wizard.js.es6 @@ -53,28 +53,68 @@ const actionTypes = [ 'open_composer' ]; +const selectionTypes = [ + 'text', + 'wizardField', + 'userField', + 'group', + 'category', + 'tag' +] + +const inputTypes = [ + 'pair', + 'conditional', + 'assignment' +] + +function defaultInputType(options = {}) { + return options.hasOutput ? 'conditional' : 'pair'; +} + +function defaultSelectionType(inputType, options = {}) { + const textDisabled = options.textDisabled; + let type = 'text'; + + if (textDisabled === true || + ((typeof textDisabled == 'string') && textDisabled.indexOf(inputType) > -1)) { + + for (let t of selectionTypes) { + let inputTypes = options[`${t}Selection`]; + + if (inputTypes === true || + ((typeof inputTypes == 'string') && inputTypes.indexOf(inputType) > -1)) { + + type = t; + break; + } + } + } + + return type; +} + function newInput(options = {}) { - let params = { + let params = { + type: defaultInputType(options), pairs: Ember.A([newPair({ index: 0, pairCount: 1 })]) } if (options.hasOutput) { - params['output'] = ''; - params['output_type'] = 'text'; + params['output_type'] = defaultSelectionType('output', options); } return Ember.Object.create(params); } function newPair(options = {}) { - console.log('newPair: ', options) let params = { index: options.index, pairCount: options.pairCount, key: '', - key_type: 'text', + key_type: defaultSelectionType('text', options), value: '', - value_type: 'text', + value_type: defaultSelectionType('text', options), connector: 'eq' } @@ -86,6 +126,8 @@ export { profileFields, actionTypes, generateName, + defaultInputType, + defaultSelectionType, connectors, newInput, newPair diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 2e639b46..7f6085de 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -170,12 +170,12 @@ {{#if createTopic}}