src/backgrid_modules/column.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 2

Size

Lines of Code 226
Function Count 5

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 226
rs 10
wmc 10
mnd 2
bc 6
fnc 5
bpm 1.2
cpm 2
noi 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
A _.each 0 9 1
A Backbone.Model.extend.initialize 0 20 2
A Backbone.Model.extend.sortValue 0 9 3
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
4
import {
5
  Backgrid
6
} from './core.js';
7
import {
8
  HeaderCell
9
} from './celltypes/header.js';
10
/*
11
  backgrid
12
  http://github.com/wyuenho/backgrid
13
14
  Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
15
  Licensed under the MIT license.
16
*/
17
18
/**
19
   A Column is a placeholder for column metadata.
20
21
   You usually don't need to create an instance of this class yourself as a
22
   collection of column instances will be created for you from a list of column
23
   attributes in the Backgrid.js view class constructors.
24
25
   @class Backgrid.Column
26
   @extends Backbone.Model
27
*/
28
var Column = Backbone.Model.extend({
29
30
  /**
31
     @cfg {Object} defaults Column defaults. To override any of these default
32
     values, you can either change the prototype directly to override
33
     Column.defaults globally or extend Column and supply the custom class to
34
     Backgrid.Grid:
35
36
         // Override Column defaults globally
37
         Column.prototype.defaults.sortable = false;
38
39
         // Override Column defaults locally
40
         var MyColumn = Column.extend({
41
           defaults: _.defaults({
42
             editable: false
43
           }, Column.prototype.defaults)
44
         });
45
46
         var grid = new Backgrid.Grid(columns: new Columns([{...}, {...}], {
47
           model: MyColumn
48
         }));
49
50
     @cfg {string} [defaults.name] The default name of the model attribute.
51
52
     @cfg {string} [defaults.label] The default label to show in the header.
53
54
     @cfg {string|Backgrid.Cell} [defaults.cell] The default cell type. If this
55
     is a string, the capitalized form will be used to look up a cell class in
56
     Backbone, i.e.: string => StringCell. If a Cell subclass is supplied, it is
57
     initialized with a hash of parameters. If a Cell instance is supplied, it
58
     is used directly.
59
60
     @cfg {string|Backgrid.HeaderCell} [defaults.headerCell] The default header
61
     cell type.
62
63
     @cfg {boolean|string|function(): boolean} [defaults.sortable=true] Whether
64
     this column is sortable. If the value is a string, a method will the same
65
     name will be looked up from the column instance to determine whether the
66
     column should be sortable. The method's signature must be `function
67
     (Backgrid.Column, Backbone.Model): boolean`.
68
69
     @cfg {boolean|string|function(): boolean} [defaults.editable=true] Whether
70
     this column is editable. If the value is a string, a method will the same
71
     name will be looked up from the column instance to determine whether the
72
     column should be editable. The method's signature must be `function
73
     (Backgrid.Column, Backbone.Model): boolean`.
74
75
     @cfg {boolean|string|function(): boolean} [defaults.renderable=true]
76
     Whether this column is renderable. If the value is a string, a method will
77
     the same name will be looked up from the column instance to determine
78
     whether the column should be renderable. The method's signature must be
79
     `function (Backrid.Column, Backbone.Model): boolean`.
80
81
     @cfg {Backgrid.CellFormatter | Object | string} [defaults.formatter] The
82
     formatter to use to convert between raw model values and user input.
83
84
     @cfg {"toggle"|"cycle"} [defaults.sortType="cycle"] Whether sorting will
85
     toggle between ascending and descending order, or cycle between insertion
86
     order, ascending and descending order.
87
88
     @cfg {(function(Backbone.Model, string): *) | string} [defaults.sortValue]
89
     The function to use to extract a value from the model for comparison during
90
     sorting. If this value is a string, a method with the same name will be
91
     looked up from the column instance.
92
93
     @cfg {"ascending"|"descending"|null} [defaults.direction=null] The initial
94
     sorting direction for this column. The default is ordered by
95
     Backbone.Model.cid, which usually means the collection is ordered by
96
     insertion order.
97
  */
98
  defaults: {
99
    name: undefined,
100
    label: undefined,
101
    sortable: true,
102
    editable: true,
103
    renderable: true,
104
    formatter: undefined,
105
    sortType: "cycle",
106
    sortValue: undefined,
107
    direction: null,
108
    cell: undefined,
109
    headerCell: undefined
110
  },
111
112
  /**
113
     Initializes this Column instance.
114
115
     @param {Object} attrs
116
117
     @param {string} attrs.name The model attribute this column is responsible
118
     for.
119
120
     @param {string|Backgrid.Cell} attrs.cell The cell type to use to render
121
     this column.
122
123
     @param {string} [attrs.label]
124
125
     @param {string|Backgrid.HeaderCell} [attrs.headerCell]
126
127
     @param {boolean|string|function(): boolean} [attrs.sortable=true]
128
129
     @param {boolean|string|function(): boolean} [attrs.editable=true]
130
131
     @param {boolean|string|function(): boolean} [attrs.renderable=true]
132
133
     @param {Backgrid.CellFormatter | Object | string} [attrs.formatter]
134
135
     @param {"toggle"|"cycle"}  [attrs.sortType="cycle"]
136
137
     @param {(function(Backbone.Model, string): *) | string} [attrs.sortValue]
0 ignored issues
show
Documentation introduced by
The parameter attrs.sortValue does not exist. Did you maybe forget to remove this comment?
Loading history...
138
139
     @throws {TypeError} If attrs.cell or attrs.options are not supplied.
140
141
     @throws {ReferenceError} If formatter is a string but a formatter class of
142
     said name cannot be found in the Backgrid module.
143
144
     See:
145
146
     - Backgrid.Column.defaults
147
     - Backgrid.Cell
148
     - Backgrid.CellFormatter
149
   */
150
  initialize: function () {
151
    if (!this.has("label")) {
152
      this.set({
153
        label: this.get("name")
154
      }, {
155
        silent: true
156
      });
157
    }
158
159
    var headerCell = Backgrid.resolveNameToClass(this.get("headerCell"), "HeaderCell");
160
161
    var cell = Backgrid.resolveNameToClass(this.get("cell"), "Cell");
162
163
    this.set({
164
      cell: cell,
165
      headerCell: headerCell
166
    }, {
167
      silent: true
168
    });
169
  },
170
171
  /**
172
     Returns an appropriate value extraction function from a model for sorting.
173
174
     If the column model contains an attribute `sortValue`, if it is a string, a
175
     method from the column instance identifified by the `sortValue` string is
176
     returned. If it is a function, it it returned as is. If `sortValue` isn't
177
     found from the column model's attributes, a default value extraction
178
     function is returned which will compare according to the natural order of
179
     the value's type.
180
181
     @return {function(Backbone.Model, string): *}
182
   */
183
  sortValue: function () {
184
    var sortValue = this.get("sortValue");
185
    if (_.isString(sortValue)) return this[sortValue];
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
introduced by
Expected { after 'if' condition.
Loading history...
186
    else if (_.isFunction(sortValue)) return sortValue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
introduced by
Expected { after 'if' condition.
Loading history...
187
188
    return function (model, colName) {
189
      return model.get(colName);
190
    };
191
  }
192
193
  /**
194
     @member Backgrid.Column
195
     @protected
196
     @method sortable
197
     @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
198
  */
199
200
  /**
201
     @member Backgrid.Column
202
     @protected
203
     @method editable
204
     @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
205
  */
206
207
  /**
208
     @member Backgrid.Column
209
     @protected
210
     @method renderable
211
     @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
212
  */
213
});
214
215
_.each(["sortable", "renderable", "editable"], function (key) {
216
  Column.prototype[key] = function () {
217
    var value = this.get(key);
218
    if (_.isString(value)) return this[value];
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
introduced by
Expected { after 'if' condition.
Loading history...
219
    else if (_.isFunction(value)) return value;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
introduced by
Expected { after 'if' condition.
Loading history...
220
221
    return !!value;
222
  };
223
});
224
export {
225
  Column
226
};
227