src/backgrid_modules/row.js   A
last analyzed

Complexity

Total Complexity 12
Complexity/F 2

Size

Lines of Code 121
Function Count 6

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 121
rs 10
wmc 12
mnd 2
bc 13
fnc 6
bpm 2.1666
cpm 2
noi 6

4 Functions

Rating   Name   Duplication   Size   Complexity  
B Backbone.View.extend.initialize 0 32 3
A Backbone.View.extend.makeCell 0 6 1
A Backbone.View.extend.remove 0 7 2
A Backbone.View.extend.render 0 14 2
1
import Backbone from 'backbone';
0 ignored issues
show
introduced by
Definition for rule 'keyword-spacing' was not found
Loading history...
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) {
0 ignored issues
show
introduced by
"columns" is already declared in the upper scope.
Loading history...
49
      var i = columns.indexOf(column);
0 ignored issues
show
introduced by
"i" is already declared in the upper scope.
Loading history...
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) {
0 ignored issues
show
introduced by
"columns" is already declared in the upper scope.
Loading history...
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.
0 ignored issues
show
Documentation introduced by
The parameter options does not exist. Did you maybe forget to remove this comment?
Loading history...
77
78
     @return {Backgrid.Cell}
79
  */
80
  makeCell: function (column) {
81
    return new(column.get("cell"))({
0 ignored issues
show
introduced by
Unary word operator "new" must be followed by whitespace.
Loading history...
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