1
|
|
|
import Backbone from 'backbone'; |
|
|
|
|
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()) { |
|
|
|
|
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")) { |
|
|
|
|
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(); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
return this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
export { |
128
|
|
|
InputCellEditor |
129
|
|
|
}; |
130
|
|
|
|