src/backgrid_modules/grid.js   A
last analyzed

Complexity

Total Complexity 17
Complexity/F 1.89

Size

Lines of Code 236
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 1
dl 0
loc 236
rs 10
wmc 17
mnd 1
bc 17
fnc 9
bpm 1.8888
cpm 1.8888
noi 11

8 Functions

Rating   Name   Duplication   Size   Complexity  
B Backbone.View.extend.render 0 23 4
B Backbone.View.extend.initialize 0 39 4
A Backbone.View.extend.insertColumn 0 4 1
A Backbone.View.extend.removeRow 0 4 1
A Backbone.View.extend.remove 0 6 1
A Backbone.View.extend.sort 0 4 1
A Backbone.View.extend.insertRow 0 4 1
A Backbone.View.extend.removeColumn 0 4 1
1
import Backbone from 'backbone';
0 ignored issues
show
introduced by
Definition for rule 'keyword-spacing' was not found
Loading history...
2
import _ from 'underscore';
3
import $ from 'jquery';
4
import {
5
  Header
6
} from './header.js';
7
8
import {
9
  Body
10
} from './body.js';
11
12
import {
13
  Footer
14
} from './footer.js';
15
16
import {
17
  Columns
18
} from './columns.js';
19
20
/*
21
  backgrid
22
  http://github.com/wyuenho/backgrid
23
24
  Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
25
  Licensed under the MIT license.
26
*/
27
28
/**
29
   Grid represents a data grid that has a header, body and an optional footer.
30
31
   By default, a Grid treats each model in a collection as a row, and each
32
   attribute in a model as a column. To render a grid you must provide a list of
33
   column metadata and a collection to the Grid constructor. Just like any
34
   Backbone.View class, the grid is rendered as a DOM node fragment when you
35
   call render().
36
37
       var grid = Backgrid.Grid({
38
         columns: [{ name: "id", label: "ID", type: "string" },
39
          // ...
40
         ],
41
         collections: books
42
       });
43
44
       $("#table-container").append(grid.render().el);
45
46
   Optionally, if you want to customize the rendering of the grid's header and
47
   footer, you may choose to extend Backgrid.Header and Backgrid.Footer, and
48
   then supply that class or an instance of that class to the Grid constructor.
49
   See the documentation for Header and Footer for further details.
50
51
       var grid = Backgrid.Grid({
52
         columns: [{ name: "id", label: "ID", type: "string" }],
53
         collections: books,
54
         header: Backgrid.Header.extend({
55
              //...
56
         }),
57
         footer: Backgrid.Paginator
58
       });
59
60
   Finally, if you want to override how the rows are rendered in the table body,
61
   you can supply a Body subclass as the `body` attribute that uses a different
62
   Row class.
63
64
   @class Backgrid.Grid
65
   @extends Backbone.View
66
67
   See:
68
69
   - Backgrid.Column
70
   - Backgrid.Header
71
   - Backgrid.Body
72
   - Backgrid.Row
73
   - Backgrid.Footer
74
*/
75
var Grid = Backbone.View.extend({
76
77
  /** @property */
78
  tagName: "table",
79
80
  /** @property */
81
  className: "backgrid",
82
83
  /** @property */
84
  header: Header,
85
86
  /** @property */
87
  body: Body,
88
89
  /** @property */
90
  footer: null,
91
92
  /**
93
     Initializes a Grid instance.
94
95
     @param {Object} options
96
     @param {Backbone.Collection.<Backgrid.Columns>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
97
     @param {Backbone.Collection} options.collection The collection of tabular model data to display.
98
     @param {string} [options.caption=string] An optional caption to be added to the table.
99
     @param {Backgrid.Header} [options.header=Backgrid.Header] An optional Header class to override the default.
100
     @param {Backgrid.Body} [options.body=Backgrid.Body] An optional Body class to override the default.
101
     @param {Backgrid.Row} [options.row=Backgrid.Row] An optional Row class to override the default.
102
     @param {Backgrid.Footer} [options.footer=Backgrid.Footer] An optional Footer class.
103
   */
104
  initialize: function (options) {
105
    // Convert the list of column objects here first so the subviews don't have
106
    // to.
107
    if (!(options.columns instanceof Backbone.Collection)) {
108
      options.columns = new Columns(options.columns || this.columns);
109
    }
110
    this.columns = options.columns;
111
112
    this.caption = options.caption;
113
114
    var filteredOptions = _.omit(options, ["el", "id", "attributes",
115
      "className", "tagName", "events"
116
    ]);
117
118
    // must construct body first so it listens to backgrid:sort first
119
    this.body = options.body || this.body;
120
    this.body = new this.body(filteredOptions);
0 ignored issues
show
introduced by
A constructor name should not start with a lowercase letter.
Loading history...
121
122
    this.header = options.header || this.header;
123
    if (this.header) {
124
      this.header = new this.header(filteredOptions);
0 ignored issues
show
introduced by
A constructor name should not start with a lowercase letter.
Loading history...
125
    }
126
127
    this.footer = options.footer || this.footer;
128
    if (this.footer) {
129
      this.footer = new this.footer(filteredOptions);
0 ignored issues
show
introduced by
A constructor name should not start with a lowercase letter.
Loading history...
130
    }
131
132
    this.listenTo(this.columns, "reset", function () {
133
      if (this.header) {
134
        this.header = new(this.header.remove().constructor)(filteredOptions);
0 ignored issues
show
introduced by
Unary word operator "new" must be followed by whitespace.
Loading history...
135
      }
136
      this.body = new(this.body.remove().constructor)(filteredOptions);
0 ignored issues
show
introduced by
Unary word operator "new" must be followed by whitespace.
Loading history...
137
      if (this.footer) {
138
        this.footer = new(this.footer.remove().constructor)(filteredOptions);
0 ignored issues
show
introduced by
Unary word operator "new" must be followed by whitespace.
Loading history...
139
      }
140
      this.render();
141
    });
142
  },
143
144
  /**
145
     Delegates to Backgrid.Body#insertRow.
146
   */
147
  insertRow: function () {
148
    this.body.insertRow.apply(this.body, arguments);
149
    return this;
150
  },
151
152
  /**
153
     Delegates to Backgrid.Body#removeRow.
154
   */
155
  removeRow: function () {
156
    this.body.removeRow.apply(this.body, arguments);
157
    return this;
158
  },
159
160
  /**
161
     Delegates to Backgrid.Columns#add for adding a column. Subviews can listen
162
     to the `add` event from their internal `columns` if rerendering needs to
163
     happen.
164
165
     @param {Object} [options] Options for `Backgrid.Columns#add`.
0 ignored issues
show
Documentation introduced by
The parameter options does not exist. Did you maybe forget to remove this comment?
Loading history...
166
   */
167
  insertColumn: function () {
168
    this.columns.add.apply(this.columns, arguments);
169
    return this;
170
  },
171
172
  /**
173
     Delegates to Backgrid.Columns#remove for removing a column. Subviews can
174
     listen to the `remove` event from the internal `columns` if rerendering
175
     needs to happen.
176
177
     @param {Object} [options] Options for `Backgrid.Columns#remove`.
0 ignored issues
show
Documentation introduced by
The parameter options does not exist. Did you maybe forget to remove this comment?
Loading history...
178
   */
179
  removeColumn: function () {
180
    this.columns.remove.apply(this.columns, arguments);
181
    return this;
182
  },
183
184
  /**
185
     Delegates to Backgrid.Body#sort.
186
   */
187
  sort: function () {
188
    this.body.sort.apply(this.body, arguments);
189
    return this;
190
  },
191
192
  /**
193
     Renders the grid's caption, then header, then footer, then finally the body. Triggers a
194
     Backbone `backgrid:rendered` event along with a reference to the grid when
195
     the it has successfully been rendered.
196
   */
197
  render: function () {
198
    this.$el.empty();
199
200
    if (this.caption) {
201
      this.$el.append($("<caption>").text(this.caption));
202
    }
203
204
    if (this.header) {
205
      this.$el.append(this.header.render().$el);
206
    }
207
208
    if (this.footer) {
209
      this.$el.append(this.footer.render().$el);
210
    }
211
212
    this.$el.append(this.body.render().$el);
213
214
    this.delegateEvents();
215
216
    this.trigger("backgrid:rendered", this);
217
218
    return this;
219
  },
220
221
  /**
222
     Clean up this grid and its subviews.
223
224
     @chainable
225
   */
226
  remove: function () {
227
    this.header && this.header.remove.apply(this.header, arguments);
0 ignored issues
show
introduced by
Expected an assignment or function call and instead saw an expression.
Loading history...
228
    this.body.remove.apply(this.body, arguments);
229
    this.footer && this.footer.remove.apply(this.footer, arguments);
0 ignored issues
show
introduced by
Expected an assignment or function call and instead saw an expression.
Loading history...
230
    return Backbone.View.prototype.remove.apply(this, arguments);
231
  }
232
233
});
234
export {
235
  Grid
236
};
237