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

151 Zeilen
5,3 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-05 03:37:09 +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-06 03:54:16 +02:00
import { bind } 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-01 07:03:26 +02:00
classNames: 'mapper-selector',
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),
showTypes: false,
2020-04-05 03:37:09 +02:00
2020-04-06 03:54:16 +02:00
didInsertElement() {
$(document).on("click", bind(this, this.documentClick));
},
willDestroyElement() {
$(document).off("click", bind(this, this.documentClick));
},
documentClick(e) {
let $element = $(this.element);
let $target = $(e.target);
if (!$target.hasClass('type-selector-icon') &&
$target.closest($element).length < 1 &&
this._state !== "destroying") {
this.set("showTypes", false);
}
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-06 03:54:16 +02:00
const controller = getOwner(this).lookup('controller:admin-wizard');
let content = controller[`${activeType}s`];
if (activeType === 'wizardField' && this.options.context === 'field') {
const currentField = controller.currentField;
content = content.filter(field => field.id !== currentField.id);
}
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-06 03:54:16 +02:00
this.set('showTypes', false);
},
toggleTypes() {
this.toggleProperty('showTypes')
2020-03-24 10:35:46 +01:00
}
}
})