forked from cloudflarearchive/backgrid-select2-cell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackgrid-select2-cell.js
139 lines (110 loc) · 3.42 KB
/
backgrid-select2-cell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
backgrid-select2-cell
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT @license.
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(["underscore", "backgrid"], factory);
} else if (typeof exports === 'object') {
// CommonJS
require("select2");
module.exports = factory(require("underscore"),
require("backgrid"));
} else {
// Browser globals
factory(root._, root.Backgrid);
}
}(this, function (_, Backgrid) {
"use strict";
var exports = {};
/**
Select2CellEditor is a cell editor that renders a `select2` select box
instead of the default `<select>` HTML element.
See:
- [Select2](http://ivaynberg.github.com/select2/)
@class Backgrid.Extension.Select2CellEditor
@extends Backgrid.SelectCellEditor
*/
var Select2CellEditor =
exports.Select2CellEditor =
Backgrid.Extension.Select2CellEditor = Backgrid.SelectCellEditor.extend({
/** @property */
events: {
"change": "save"
},
/** @property */
select2Options: {
openOnEnter: false
},
initialize: function () {
Backgrid.SelectCellEditor.prototype.initialize.apply(this, arguments);
this.close = _.bind(this.close, this);
},
/**
Sets the options for `select2`. Called by the parent Select2Cell during
edit mode.
*/
setSelect2Options: function (options) {
this.select2Options = _.extend(options || {});
},
/**
Renders a `select2` select box instead of the default `<select>` HTML
element using the supplied options from #select2Options.
@chainable
*/
render: function () {
Backgrid.SelectCellEditor.prototype.render.apply(this, arguments);
this.$el.select2(this.select2Options);
return this;
},
/**
Attach event handlers to the select2 box and focus it.
*/
postRender: function () {
var self = this;
self.$el.on("select2:close", function (e) {
e.type = "blur";
self.close(e);
}).select2("focus");
},
remove: function () {
this.$el.select2("destroy");
return Backgrid.SelectCellEditor.prototype.remove.apply(this, arguments);
}
});
/**
Select2Cell is a cell class that renders a `select2` select box during edit
mode.
@class Backgrid.Extension.Select2Cell
@extends Backgrid.SelectCell
*/
exports.Select2Cell = Backgrid.Extension.Select2Cell = Backgrid.SelectCell.extend({
/** @property */
className: "select2-cell",
/** @property */
editor: Select2CellEditor,
/** @property */
select2Options: null,
/**
Initializer.
@param {Object} options
@param {Backbone.Model} options.model
@param {Backgrid.Column} options.column
@param {Object} [options.select2Options]
@throws {TypeError} If `optionsValues` is undefined.
*/
initialize: function (options) {
Backgrid.SelectCell.prototype.initialize.apply(this, arguments);
this.select2Options = options.select2Options || this.select2Options;
this.listenTo(this.model, "backgrid:edit", function (model, column, cell, editor) {
if (column.get("name") == this.column.get("name")) {
editor.setSelect2Options(this.select2Options);
}
});
}
});
return exports;
}));