Skip to content

Commit

Permalink
Factor out reusable select2 picker (#426 #455)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Apr 22, 2021
1 parent a168909 commit 1a80fec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
28 changes: 28 additions & 0 deletions frontend/src/forms/select2-picker-view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { extend } from 'lodash';
import 'select2';

import BasePicker from './base-picker-view';

/**
* A very thin wrapper around `BasePicker` that enables the select2 plugin.
*/
export default class Select2PickerView extends BasePicker {
beforeRender(): this {
this.$('select').select2('destroy');
return this;
}

afterRender(): this {
this.$('select').select2();
return this;
}

remove(): this {
this.$('select').select2('destroy');
return super.remove();
}
}

extend(Select2PickerView.prototype, {
className: 'readit-picker',
});
12 changes: 3 additions & 9 deletions frontend/src/panel-annotation/annotation-edit-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Node from '../common-rdf/node';
import Graph from '../common-rdf/graph';

import ItemEditor from '../item-edit/item-edit-view';
import PickerView from '../forms/base-picker-view';
import PickerView from '../forms/select2-picker-view';
import FilteredCollection from '../common-adapters/filtered-collection';
import ItemGraph from '../common-adapters/item-graph';
import ClassPickerView from '../forms/ontology-class-picker-view';
Expand Down Expand Up @@ -50,12 +50,7 @@ export default class AnnotationEditView extends CompositeView<FlatItem> {
initialize() {
this.itemOptions = new ItemGraph();
this.itemOptions.comparator = this.sortOptions;
this.itemPicker = new PickerView({
collection: this.itemOptions,
className: '',
});
// Replace Bulma select by select2 select. TODO: make this less hacky.
this.itemPicker.$('select').width('95%').select2();
this.itemPicker = new PickerView({ collection: this.itemOptions });
this.classPicker = new ClassPickerView({
collection: getOntologyClasses(),
preselection: this.model.get('class'),
Expand Down Expand Up @@ -112,7 +107,6 @@ export default class AnnotationEditView extends CompositeView<FlatItem> {

remove(): this {
if (this.validator) this.validator.destroy();
this.itemPicker.$('select').select2('destroy');
super.remove();
return this;
}
Expand Down Expand Up @@ -263,7 +257,7 @@ export default class AnnotationEditView extends CompositeView<FlatItem> {
if (isBlank(this.model.underlying)) {
// Remove the placeholder.
this.collection.underlying.remove(this.model.underlying);
}
}
explorerChannel.trigger('annotationEditView:close', this);
return this;
}
Expand Down

0 comments on commit 1a80fec

Please sign in to comment.