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

162 Zeilen
5,8 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';
2020-04-13 14:17:22 +02:00
import { defaultSelectionType, selectionTypes } from '../lib/wizard-mapper';
2020-04-07 09:54:30 +02:00
import { snakeCase } 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-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-03-24 10:35:46 +01:00
groups: alias('site.groups'),
2020-04-07 09:54:30 +02:00
categories: alias('site.categories'),
2020-04-05 03:37:09 +02:00
showText: computed('activeType', function() { return this.showInput('text') }),
showWizardField: computed('activeType', function() { return this.showInput('wizardField') }),
showUserField: computed('activeType', function() { return this.showInput('userField') }),
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') }),
showComboBox: or('showWizardField', 'showUserField'),
showMultiSelect: or('showCategory', 'showGroup'),
textEnabled: computed('options.textSelection', 'inputType', function() { return this.optionEnabled('textSelection') }),
wizardFieldEnabled: computed('options.wizardFieldSelection', 'inputType', function() { return this.optionEnabled('wizardFieldSelection') }),
userFieldEnabled: 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') }),
userEnabled: computed('options.userSelection', 'inputType', function() { return this.optionEnabled('userSelection') }),
listEnabled: computed('options.listSelection', 'inputType', function() { return this.optionEnabled('listSelection') }),
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-15 07:22:21 +02:00
if (this.activeType && !this[`${this.activeType}Enabled`]) {
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) {
return I18n.t(`admin.wizard.selector.label.${snakeCase(type)}`)
},
2020-04-06 10:36:38 +02:00
@observes('inputType')
2020-03-24 10:35:46 +01:00
resetActiveType() {
this.set('activeType', defaultSelectionType(this.selectorType, this.options));
},
@observes('activeType')
clearValue() {
this.set('value', null);
},
2020-04-05 03:37:09 +02:00
@discourseComputed('activeType')
comboBoxContent(activeType) {
2020-04-13 14:17:22 +02:00
const controller = getOwner(this).lookup('controller:admin-wizards-wizard-show');
2020-04-06 03:54:16 +02:00
let content = controller[`${activeType}s`];
2020-04-13 14:17:22 +02:00
2020-04-10 10:51:01 +02:00
// you can't select the current field in the field context
2020-04-06 03:54:16 +02:00
if (activeType === 'wizardField' && this.options.context === 'field') {
2020-04-10 10:51:01 +02:00
content = content.filter(field => field.id !== controller.currentField.id);
}
// updating certain user fields via the profile update action is not supported
2020-04-10 10:51:01 +02:00
if (activeType === 'userField' &&
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);
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
actions: {
toggleType(type) {
this.set('activeType', type);
2020-04-13 14:17:22 +02:00
this.set('showTypes', false);
2020-04-06 03:54:16 +02:00
},
2020-04-13 14:17:22 +02:00
toggleTypes() {
this.toggleProperty('showTypes');
2020-03-24 10:35:46 +01:00
}
}
})