3
http://github.com/wyuenho/backgrid
5
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
6
Licensed under the MIT @license.
9
(function (window, $, _, Backbone, Backgrid) {
12
Renders a form with a text area and a save button in a modal dialog.
14
@class Backgrid.Extension.TextareaEditor
15
@extends Backgrid.CellEditor
17
var TextareaEditor = Backgrid.Extension.TextareaEditor = Backgrid.CellEditor.extend({
23
className: "modal hide fade",
25
/** @property {function(Object, ?Object=): string} template */
26
template: _.template('<form><div class="modal-header"><button type="button" class="close" data-dismiss="modal">×</button><h3><%- column.get("label") %></h3></div><div class="modal-body"><textarea cols="<%= cols %>" rows="<%= rows %>"><%- content %></textarea></div><div class="modal-footer"><input class="btn" type="submit" value="Save"/></div></form>'),
36
"keydown textarea": "clearError",
37
"submit": "saveOrCancel",
38
"hide": "saveOrCancel",
44
@property {Object} modalOptions The options passed to Bootstrap's modal
52
Renders a modal form dialog with a textarea, submit button and a close button.
55
this.$el.html($(this.template({
59
content: this.formatter.fromRaw(this.model.get(this.column.get("name")))
62
this.delegateEvents();
64
this.$el.modal(this.modalOptions);
70
Event handler. Saves the text in the text area to the model when
71
submitting. When cancelling, if the text area is dirty, a confirmation
72
dialog will pop up. If the user clicks confirm, the text will be saved to
75
Triggers a Backbone `backgrid:error` event from the model along with the
76
model, column and the existing value as the parameters if the value
81
saveOrCancel: function (e) {
82
if (e && e.type == "submit") {
87
var model = this.model;
88
var column = this.column;
89
var val = this.$el.find("textarea").val();
90
var newValue = this.formatter.toRaw(val);
92
if (_.isUndefined(newValue)) {
93
model.trigger("backgrid:error", model, column, val);
100
else if (!e || e.type == "submit" ||
102
newValue !== (this.model.get(this.column.get("name")) || '').replace(/\r/g, '') &&
103
window.confirm("Would you like to save your changes?"))) {
105
model.set(column.get("name"), newValue);
106
this.$el.modal("hide");
108
else if (e.type != "hide") this.$el.modal("hide");
112
Clears the error class on the parent cell.
114
clearError: _.debounce(function () {
115
if (!_.isUndefined(this.formatter.toRaw(this.$el.find("textarea").val()))) {
116
this.$el.parent().removeClass("error");
121
Triggers a `backgrid:edited` event along with the cell editor as the
122
parameter after the modal is hidden.
126
close: function (e) {
127
var model = this.model;
128
model.trigger("backgrid:edited", model, this.column,
129
new Backgrid.Command(e));
133
Focuses the textarea when the modal is shown.
136
this.$el.find("textarea").focus();
142
TextCell is a string cell type that renders a form with a text area in a
143
modal dialog instead of an input box editor. It is best suited for entering
144
a large body of text.
146
@class Backgrid.Extension.TextCell
147
@extends Backgrid.StringCell
149
var TextCell = Backgrid.Extension.TextCell = Backgrid.StringCell.extend({
152
className: "text-cell",
155
editor: TextareaEditor
159
}(window, jQuery, _, Backbone, Backgrid));