src/backgrid_modules/editors/input_cell.js   A
last analyzed

Complexity

Total Complexity 16
Complexity/F 4

Size

Lines of Code 129
Function Count 4

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 129
rs 10
wmc 16
mnd 2
bc 11
fnc 4
bpm 2.75
cpm 4
noi 6

4 Functions

Rating   Name   Duplication   Size   Complexity  
A CellEditor.extend.render 0 5 1
A CellEditor.extend.initialize 0 7 2
D CellEditor.extend.saveOrCancel 0 31 9
A CellEditor.extend.postRender 0 10 4
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 {
4
  Backgrid
5
} from '../core.js';
6
7
import {
8
  CellEditor
9
} from './cell.js';
10
import {
11
  Command
12
} from '../command.js';
13
/**
14
   InputCellEditor the cell editor type used by most core cell types. This cell
15
   editor renders a text input box as its editor. The input will render a
16
   placeholder if the value is empty on supported browsers.
17
18
   @class Backgrid.InputCellEditor
19
   @extends Backgrid.CellEditor
20
*/
21
var InputCellEditor = CellEditor.extend({
22
23
  /** @property */
24
  tagName: "input",
25
26
  /** @property */
27
  attributes: {
28
    type: "text"
29
  },
30
31
  /** @property */
32
  events: {
33
    "blur": "saveOrCancel",
34
    "keydown": "saveOrCancel"
35
  },
36
37
  /**
38
     Initializer. Removes this `el` from the DOM when a `done` event is
39
     triggered.
40
41
     @param {Object} options
42
     @param {Backgrid.CellFormatter} options.formatter
43
     @param {Backgrid.Column} options.column
44
     @param {Backbone.Model} options.model
45
     @param {string} [options.placeholder]
46
  */
47
  initialize: function (options) {
48
    InputCellEditor.__super__.initialize.apply(this, arguments);
49
50
    if (options.placeholder) {
51
      this.$el.attr("placeholder", options.placeholder);
52
    }
53
  },
54
55
  /**
56
     Renders a text input with the cell value formatted for display, if it
57
     exists.
58
  */
59
  render: function () {
60
    var model = this.model;
61
    this.$el.val(this.formatter.fromRaw(model.get(this.column.get("name")), model));
62
    return this;
63
  },
64
65
  /**
66
     If the key pressed is `enter`, `tab`, `up`, or `down`, converts the value
67
     in the editor to a raw value for saving into the model using the formatter.
68
69
     If the key pressed is `esc` the changes are undone.
70
71
     If the editor goes out of focus (`blur`) but the value is invalid, the
72
     event is intercepted and cancelled so the cell remains in focus pending for
73
     further action. The changes are saved otherwise.
74
75
     Triggers a Backbone `backgrid:edited` event from the model when successful,
76
     and `backgrid:error` if the value cannot be converted. Classes listening to
77
     the `error` event, usually the Cell classes, should respond appropriately,
78
     usually by rendering some kind of error feedback.
79
80
     @param {Event} e
81
  */
82
  saveOrCancel: function (e) {
83
84
    var formatter = this.formatter;
85
    var model = this.model;
86
    var column = this.column;
87
88
    var command = new Command(e);
89
    var blurred = e.type === "blur";
90
91
    if (command.moveUp() || command.moveDown() || command.moveLeft() || command.moveRight() ||
92
      command.save() || blurred) {
93
94
      e.preventDefault();
95
      e.stopPropagation();
96
97
      var val = this.$el.val();
98
      var newValue = formatter.toRaw(val, model);
99
      if (_.isUndefined(newValue)) {
100
        model.trigger("backgrid:error", model, column, val);
101
      } else {
102
        model.set(column.get("name"), newValue);
103
        model.trigger("backgrid:edited", model, column, command);
104
      }
105
    }
106
    // esc
107
    else if (command.cancel()) {
0 ignored issues
show
Coding Style introduced by
Closing curly brace does not appear on the same line as the subsequent block.
Loading history...
108
      // undo
109
      e.stopPropagation();
110
      model.trigger("backgrid:edited", model, column, command);
111
    }
112
  },
113
114
  postRender: function (model, column) {
115
    if (column == null || column.get("name") == this.column.get("name")) {
0 ignored issues
show
introduced by
Use ‘===’ to compare with ‘null’.
Loading history...
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
116
      // move the cursor to the end on firefox if text is right aligned
117
      if (this.$el.css("text-align") === "right") {
118
        var val = this.$el.val();
119
        this.$el.focus().val(null).val(val);
120
      } else this.$el.focus();
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 'else'.
Loading history...
121
    }
122
    return this;
123
  }
124
125
});
126
127
export {
128
  InputCellEditor
129
};
130