/
ListView.js
81 lines (68 loc) · 2.1 KB
/
ListView.js
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
73
74
75
76
77
78
79
80
81
import Marionette from 'backbone.marionette';
import _ from 'underscore';
import {CollectionView, SortableCollectionView} from 'pageflow/ui';
import {ListItemView} from './ListItemView';
import template from '../templates/list.jst';
import blankSlateTemplate from '../templates/listBlankSlate.jst';
/**
* A generic list view with items consisting of a thumbnail, text and
* possibly some buttons or a navigation arrow.
*
* Models inside the collection must implement the following methods:
*
* @param {Backbone.Collection} options.collection
*
* @param {Object} options
*
* @param {string} options.label
* Text of the label to display above the list.
*
* @param {boolean} [options.highlight=false]
*
* @param {boolean} [options.sortable=false]
*
* @param {string|function} [options.itemDescription]
*
* @param {string|function} [options.itemTypeName]
*
* @param {string|function} [options.itemTypeDescription]
*
* @param {string|function} [options.itemIsInvalid]
*
* @param {function} [options.onEdit]
*
* @param {function} [options.onRemove]
*
* @class
*/
export const ListView = Marionette.ItemView.extend({
template,
className: 'list',
ui: {
label: '.list_label',
items: '.list_items'
},
onRender: function() {
var collectionViewConstructor = this.options.sortable ?
SortableCollectionView :
CollectionView;
this.subview(new collectionViewConstructor({
el: this.ui.items,
collection: this.collection,
itemViewConstructor: ListItemView,
itemViewOptions: _.extend({
description: this.options.itemDescription,
typeName: this.options.itemTypeName,
typeDescription: this.options.itemTypeDescription,
isInvalid: this.options.itemIsInvalid
}, _(this.options).pick('onEdit', 'onRemove', 'highlight')),
blankSlateViewConstructor: Marionette.ItemView.extend({
tagName: 'li',
className: 'list_blank_slate',
template: blankSlateTemplate
})
}));
this.ui.label.text(this.options.label);
this.$el.toggleClass('with_type_pictogram', !!this.options.itemTypeName);
}
});