-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultifield-view.ts
72 lines (62 loc) · 1.76 KB
/
multifield-view.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { extend } from 'lodash';
import {
CompositeView,
CollectionView,
ViewOptions as BViewOptions,
} from '../core/view';
import AddButton from './add-button-view';
import multifieldTemplate from './multifield-template';
/**
* A Multifield takes care of showing an "add" button as a final row. It defers
* the management of the actual rows to a subview, which is expected to have
* the type below.
*/
export interface RowManagingView extends CollectionView {
addRow(): void;
}
/**
* For the above reason, the subview in question must be passed as a
* constructor argument.
*/
export interface ViewOptions extends BViewOptions {
collectionView: RowManagingView;
}
/**
* A simple composition: rowmanager with zero or more rows on top,
* add button below.
*/
export default class Multifield extends CompositeView {
collectionView: RowManagingView;
addButton: AddButton;
constructor(options: ViewOptions) {
super(options);
}
initialize(options: ViewOptions): void {
this.collectionView = options.collectionView;
this.collectionView.$el.addClass('field');
this.listenTo(this.collectionView, 'all', this.trigger);
this.addButton = new AddButton().on(
'click', this.collectionView.addRow, this.collectionView
);
this.render();
}
activate(): this {
this.collectionView.activate();
return this;
}
renderContainer(): this {
this.$el.html(this.template({}));
return this;
}
}
extend(Multifield.prototype, {
className: 'rit-multifield',
template: multifieldTemplate,
subviews: [{
view: 'collectionView',
method: 'prepend',
}, {
view: 'addButton',
selector: '.rit-add-row',
}],
});