0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 01:40:29 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/wizard-mapper-selector.js.es6

227 Zeilen
7,6 KiB
Text

2020-04-06 03:54:16 +02:00
import { alias, or, gt } from "@ember/object/computed";
2020-03-24 10:35:46 +01:00
import { computed } from "@ember/object";
2020-04-06 10:36:38 +02:00
import { default as discourseComputed, observes, on } from "discourse-common/utils/decorators";
2020-03-29 09:49:33 +02:00
import { getOwner } from 'discourse-common/lib/get-owner';
import { defaultSelectionType, selectionTypes } from '../lib/wizard-mapper';
import { snakeCase, generateName, userProperties } from '../lib/wizard';
2020-04-05 03:37:09 +02:00
import Component from "@ember/component";
2020-04-15 07:22:21 +02:00
import { bind, later } from "@ember/runloop";
2020-05-28 05:06:06 +02:00
import I18n from "I18n";
2020-03-24 10:35:46 +01:00
2020-04-05 03:37:09 +02:00
export default Component.extend({
2020-04-11 08:22:12 +02:00
classNameBindings: [':mapper-selector', 'activeType'],
2020-04-05 03:37:09 +02:00
showText: computed('activeType', function() { return this.showInput('text') }),
showWizardField: computed('activeType', function() { return this.showInput('wizardField') }),
showWizardAction: computed('activeType', function() { return this.showInput('wizardAction') }),
2020-04-05 03:37:09 +02:00
showUserField: computed('activeType', function() { return this.showInput('userField') }),
showUserFieldOptions: computed('activeType', function() { return this.showInput('userFieldOptions') }),
2020-04-05 03:37:09 +02:00
showCategory: computed('activeType', function() { return this.showInput('category') }),
showTag: computed('activeType', function() { return this.showInput('tag') }),
showGroup: computed('activeType', function() { return this.showInput('group') }),
showUser: computed('activeType', function() { return this.showInput('user') }),
showList: computed('activeType', function() { return this.showInput('list') }),
textEnabled: computed('options.textSelection', 'inputType', function() { return this.optionEnabled('textSelection') }),
wizardFieldEnabled: computed('options.wizardFieldSelection', 'inputType', function() { return this.optionEnabled('wizardFieldSelection') }),
wizardActionEnabled: computed('options.wizardActionSelection', 'inputType', function() { return this.optionEnabled('wizardActionSelection') }),
2020-04-05 03:37:09 +02:00
userFieldEnabled: computed('options.userFieldSelection', 'inputType', function() { return this.optionEnabled('userFieldSelection') }),
userFieldOptionsEnabled: computed('options.userFieldOptionsSelection', 'inputType', function() { return this.optionEnabled('userFieldOptionsSelection') }),
2020-04-05 03:37:09 +02:00
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') }),
userEnabled: computed('options.userSelection', 'inputType', function() { return this.optionEnabled('userSelection') }),
listEnabled: computed('options.listSelection', 'inputType', function() { return this.optionEnabled('listSelection') }),
groups: alias('site.groups'),
categories: alias('site.categories'),
showComboBox: or('showWizardField', 'showWizardAction', 'showUserField', 'showUserFieldOptions'),
showMultiSelect: or('showCategory', 'showGroup'),
2020-04-06 03:54:16 +02:00
hasTypes: gt('selectorTypes.length', 1),
2020-04-13 14:17:22 +02:00
showTypes: false,
2020-04-05 03:37:09 +02:00
2020-04-06 03:54:16 +02:00
didInsertElement() {
2020-04-23 04:28:31 +02:00
if (!this.activeType || (this.activeType && !this[`${this.activeType}Enabled`])) {
2020-04-15 07:22:21 +02:00
later(() => this.resetActiveType());
}
2020-04-06 03:54:16 +02:00
$(document).on("click", bind(this, this.documentClick));
},
willDestroyElement() {
$(document).off("click", bind(this, this.documentClick));
},
documentClick(e) {
2020-04-11 08:22:12 +02:00
if (this._state == "destroying") return;
2020-04-06 03:54:16 +02:00
let $target = $(e.target);
2020-04-13 14:17:22 +02:00
if (!$target.parents('.type-selector').length && this.showTypes) {
this.set('showTypes', false);
2020-04-06 03:54:16 +02:00
}
2020-04-05 03:37:09 +02:00
},
2020-03-24 10:35:46 +01:00
2020-03-29 09:49:33 +02:00
@discourseComputed
2020-04-06 03:54:16 +02:00
selectorTypes() {
return selectionTypes.filter(type => (this[`${type}Enabled`]))
.map(type => ({ type, label: this.typeLabel(type) }));
2020-04-01 07:03:26 +02:00
},
2020-04-06 03:54:16 +02:00
@discourseComputed('activeType')
activeTypeLabel(activeType) {
return this.typeLabel(activeType);
2020-03-29 09:49:33 +02:00
},
2020-04-06 03:54:16 +02:00
typeLabel(type) {
2020-04-23 04:28:31 +02:00
return type ? I18n.t(`admin.wizard.selector.label.${snakeCase(type)}`) : null;
2020-04-06 03:54:16 +02:00
},
comboBoxAllowAny: or('showWizardField', 'showWizardAction'),
@discourseComputed
showController() {
return getOwner(this).lookup('controller:admin-wizards-wizard-show');
},
@discourseComputed(
'activeType',
'showController.wizardFields.[]',
'showController.wizard.actions.[]',
'showController.userFields.[]',
'showController.currentField.id',
'showController.currentAction.id'
)
comboBoxContent(
activeType,
wizardFields,
wizardActions,
userFields,
currentFieldId,
currentActionId
) {
let content;
if (activeType === 'wizardField') {
content = wizardFields;
if (this.options.context === 'field') {
content = content.filter(field => field.id !== currentFieldId);
}
}
if (activeType === 'wizardAction') {
content = wizardActions.map(a => ({
id: a.id,
label: `${generateName(a.type)} (${a.id})`,
type: a.type
}));
if (this.options.context === 'action') {
content = content.filter(a => a.id !== currentActionId);
}
2020-04-10 10:51:01 +02:00
}
2020-04-20 14:26:34 +02:00
if (activeType === 'userField') {
content = userProperties.map((f) => ({
id: f,
name: generateName(f)
})).concat((userFields || []));
2020-04-10 10:51:01 +02:00
if (this.options.context === 'action' &&
this.inputType === 'association' &&
this.selectorType === 'key') {
const excludedFields = ['username','email', 'trust_level'];
content = content.filter(userField => excludedFields.indexOf(userField.id) === -1);
}
}
if (activeType === 'userFieldOptions') {
content = userFields;
2020-04-06 03:54:16 +02:00
}
return content;
2020-03-24 10:35:46 +01:00
},
2020-04-05 03:37:09 +02:00
@discourseComputed('activeType')
multiSelectContent(activeType) {
return {
category: this.categories,
group: this.groups,
list: ''
}[activeType];
2020-03-24 10:35:46 +01:00
},
2020-04-06 03:54:16 +02:00
@discourseComputed('activeType', 'inputType')
placeholderKey(activeType, inputType) {
2020-04-05 03:37:09 +02:00
if (activeType === 'text' && this.options[`${this.selectorType}Placeholder`]) {
return this.options[`${this.selectorType}Placeholder`];
2020-04-06 03:54:16 +02:00
} else {
return `admin.wizard.selector.placeholder.${snakeCase(activeType)}`;
}
2020-04-05 03:37:09 +02:00
},
@discourseComputed('activeType')
multiSelectOptions(activeType) {
let result = {
2020-04-06 03:54:16 +02:00
none: this.placeholderKey
2020-04-05 03:37:09 +02:00
};
if (activeType === 'list') {
result.allowAny = true;
}
return result;
},
2020-03-24 10:35:46 +01:00
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;
2020-04-05 03:37:09 +02:00
return option.split(',').filter(option => {
return [this.selectorType, this.inputType].indexOf(option) !== -1;
}).length;
2020-03-24 10:35:46 +01:00
},
2020-04-05 03:37:09 +02:00
showInput(type) {
return this.activeType === type && this[`${type}Enabled`];
},
2020-03-24 10:35:46 +01:00
changeValue(value) {
this.set('value', value);
this.onUpdate('selector', this.activeType);
},
@observes('inputType')
resetActiveType() {
this.set('activeType', defaultSelectionType(this.selectorType, this.options));
2020-04-20 11:41:13 +02:00
},
2020-03-24 10:35:46 +01:00
actions: {
toggleType(type) {
this.set('activeType', type);
2020-04-13 14:17:22 +02:00
this.set('showTypes', false);
this.set('value', null);
this.onUpdate('selector');
2020-04-06 03:54:16 +02:00
},
2020-04-13 14:17:22 +02:00
toggleTypes() {
this.toggleProperty('showTypes');
},
changeValue(value) {
this.changeValue(value);
},
changeInputValue(event) {
this.changeValue(event.target.value);
},
changeUserValue(previousValue, value) {
this.changeValue(value);
2020-03-24 10:35:46 +01:00
}
}
})