1
|
|
|
import Backbone from 'backbone'; |
|
|
|
|
2
|
|
|
|
3
|
|
|
import { |
4
|
|
|
Columns |
5
|
|
|
} from './columns.js'; |
6
|
|
|
/* |
7
|
|
|
backgrid |
8
|
|
|
http://github.com/wyuenho/backgrid |
9
|
|
|
|
10
|
|
|
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors |
11
|
|
|
Licensed under the MIT license. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
Row is a simple container view that takes a model instance and a list of |
16
|
|
|
column metadata describing how each of the model's attribute is to be |
17
|
|
|
rendered, and apply the appropriate cell to each attribute. |
18
|
|
|
|
19
|
|
|
@class Backgrid.Row |
20
|
|
|
@extends Backbone.View |
21
|
|
|
*/ |
22
|
|
|
var Row = Backbone.View.extend({ |
23
|
|
|
|
24
|
|
|
/** @property */ |
25
|
|
|
tagName: "tr", |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
Initializes a row view instance. |
29
|
|
|
|
30
|
|
|
@param {Object} options |
31
|
|
|
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata. |
32
|
|
|
@param {Backbone.Model} options.model The model instance to render. |
33
|
|
|
|
34
|
|
|
@throws {TypeError} If options.columns or options.model is undefined. |
35
|
|
|
*/ |
36
|
|
|
initialize: function (options) { |
37
|
|
|
|
38
|
|
|
var columns = this.columns = options.columns; |
39
|
|
|
if (!(columns instanceof Backbone.Collection)) { |
40
|
|
|
columns = this.columns = new Columns(columns); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
var cells = this.cells = []; |
44
|
|
|
for (var i = 0; i < columns.length; i++) { |
45
|
|
|
cells.push(this.makeCell(columns.at(i), options)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
this.listenTo(columns, "add", function (column, columns) { |
|
|
|
|
49
|
|
|
var i = columns.indexOf(column); |
|
|
|
|
50
|
|
|
var cell = this.makeCell(column, options); |
51
|
|
|
cells.splice(i, 0, cell); |
52
|
|
|
|
53
|
|
|
var $el = this.$el; |
54
|
|
|
if (i === 0) { |
55
|
|
|
$el.prepend(cell.render().$el); |
56
|
|
|
} else if (i === columns.length - 1) { |
57
|
|
|
$el.append(cell.render().$el); |
58
|
|
|
} else { |
59
|
|
|
$el.children().eq(i).before(cell.render().$el); |
60
|
|
|
} |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
this.listenTo(columns, "remove", function (column, columns, opts) { |
|
|
|
|
64
|
|
|
cells[opts.index].remove(); |
65
|
|
|
cells.splice(opts.index, 1); |
66
|
|
|
}); |
67
|
|
|
}, |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
Factory method for making a cell. Used by #initialize internally. Override |
71
|
|
|
this to provide an appropriate cell instance for a custom Row subclass. |
72
|
|
|
|
73
|
|
|
@protected |
74
|
|
|
|
75
|
|
|
@param {Backgrid.Column} column |
76
|
|
|
@param {Object} options The options passed to #initialize. |
|
|
|
|
77
|
|
|
|
78
|
|
|
@return {Backgrid.Cell} |
79
|
|
|
*/ |
80
|
|
|
makeCell: function (column) { |
81
|
|
|
return new(column.get("cell"))({ |
|
|
|
|
82
|
|
|
column: column, |
83
|
|
|
model: this.model |
84
|
|
|
}); |
85
|
|
|
}, |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
Renders a row of cells for this row's model. |
89
|
|
|
*/ |
90
|
|
|
render: function () { |
91
|
|
|
this.$el.empty(); |
92
|
|
|
|
93
|
|
|
var fragment = document.createDocumentFragment(); |
94
|
|
|
for (var i = 0; i < this.cells.length; i++) { |
95
|
|
|
fragment.appendChild(this.cells[i].render().el); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
this.el.appendChild(fragment); |
99
|
|
|
|
100
|
|
|
this.delegateEvents(); |
101
|
|
|
|
102
|
|
|
return this; |
103
|
|
|
}, |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
Clean up this row and its cells. |
107
|
|
|
|
108
|
|
|
@chainable |
109
|
|
|
*/ |
110
|
|
|
remove: function () { |
111
|
|
|
for (var i = 0; i < this.cells.length; i++) { |
112
|
|
|
var cell = this.cells[i]; |
113
|
|
|
cell.remove.apply(cell, arguments); |
114
|
|
|
} |
115
|
|
|
return Backbone.View.prototype.remove.apply(this, arguments); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
}); |
119
|
|
|
export { |
120
|
|
|
Row |
121
|
|
|
}; |
122
|
|
|
|