2018-08-24 18:05:27 +02:00
|
|
|
import { Settings, Globals, Reflection, ReactComponents, MonkeyPatch, Cache } from 'modules';
|
2018-08-17 16:05:23 +02:00
|
|
|
import { VueInjector } from 'ui';
|
|
|
|
|
|
|
|
import AutocompleteComponent from './components/common/Autocomplete.vue';
|
|
|
|
import { Utils } from 'common';
|
|
|
|
|
2018-08-22 20:07:06 +02:00
|
|
|
export default new class Autocomplete {
|
2018-08-17 16:05:23 +02:00
|
|
|
|
|
|
|
get sets() {
|
2018-08-22 20:07:06 +02:00
|
|
|
return this._sets || (this._sets = new Map());
|
2018-08-17 16:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2019-03-10 17:58:24 +01:00
|
|
|
this.cta = await ReactComponents.getComponent('ChannelTextArea');
|
2018-08-22 20:07:06 +02:00
|
|
|
MonkeyPatch('BD:Autocomplete', this.cta.component.prototype).after('render', this.channelTextAreaAfterRender.bind(this));
|
2018-08-17 16:05:23 +02:00
|
|
|
this.initialized = true;
|
|
|
|
}
|
|
|
|
|
2018-08-19 22:13:23 +02:00
|
|
|
get latestComponent() {
|
|
|
|
return this._latestComponent;
|
|
|
|
}
|
|
|
|
|
2018-08-17 16:05:23 +02:00
|
|
|
channelTextAreaAfterRender(component, args, retVal) {
|
2018-08-19 19:34:32 +02:00
|
|
|
const inner = Utils.findInReactTree(retVal, filter => filter && filter.className && filter.className.includes('inner'));
|
2018-08-19 22:13:23 +02:00
|
|
|
|
2018-08-19 19:34:32 +02:00
|
|
|
if (!inner || !inner.children) return;
|
2018-08-17 16:05:23 +02:00
|
|
|
inner.children.splice(0, 0, VueInjector.createReactElement(AutocompleteComponent, {
|
2018-08-19 19:34:32 +02:00
|
|
|
controller: this,
|
2018-08-19 22:13:23 +02:00
|
|
|
_insertText: component.insertText.bind(component)
|
2018-08-17 16:05:23 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
add(prefix, controller) {
|
|
|
|
if (!this.initialized) this.init();
|
2018-08-22 20:07:06 +02:00
|
|
|
if (this.validPrefix(prefix)) return;
|
|
|
|
this.sets.set(prefix, controller);
|
2018-08-17 16:05:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 00:56:57 +02:00
|
|
|
remove(prefix) {
|
2018-08-22 20:07:06 +02:00
|
|
|
this.sets.delete(prefix);
|
2018-08-20 00:56:57 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 16:05:23 +02:00
|
|
|
validPrefix(prefix) {
|
2018-08-22 20:07:06 +02:00
|
|
|
return this.sets.has(prefix);
|
2018-08-17 16:05:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 17:36:22 +02:00
|
|
|
toggle(prefix, sterm, event) {
|
|
|
|
const controller = this.sets.get(prefix);
|
|
|
|
return controller && controller.toggle && controller.toggle(sterm, event);
|
2018-08-22 18:49:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 16:05:23 +02:00
|
|
|
items(prefix, sterm) {
|
|
|
|
if (!this.validPrefix(prefix)) return [];
|
2018-08-22 20:07:06 +02:00
|
|
|
return this.sets.get(prefix).acsearch(sterm);
|
2018-08-17 16:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|