forked from mleibman/SlickGrid
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathslick.compositeeditor.js
209 lines (168 loc) · 5.34 KB
/
slick.compositeeditor.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
(function ($) {
$.extend(true, window, {
Slick: {
CompositeEditor: CompositeEditor
}
});
/***
* A composite SlickGrid editor factory.
* Generates an editor that is composed of multiple editors for given columns.
* Individual editors are provided given containers instead of the original cell.
* Validation will be performed on all editors individually and the results will be aggregated into one
* validation result.
*
*
* The returned editor will have its prototype set to CompositeEditor, so you can use the "instanceof" check.
*
* NOTE: This doesn't work for detached editors since they will be created and positioned relative to the
* active cell and not the provided container.
*
* @namespace Slick
* @class CompositeEditor
* @constructor
* @param columns {Array} Column definitions from which editors will be pulled.
* @param containers {Array} Container HTMLElements in which editors will be placed.
* @param options {Object} Options hash:
* validationFailedMsg - A generic failed validation message set on the aggregated validation resuls.
* hide - A function to be called when the grid asks the editor to hide itself.
* show - A function to be called when the grid asks the editor to show itself.
* position - A function to be called when the grid asks the editor to reposition itself.
* destroy - A function to be called when the editor is destroyed.
*/
function CompositeEditor(columns, containers, options) {
var defaultOptions = {
validationFailedMsg: "Some of the fields have failed validation",
show: null,
hide: null,
position: null,
destroy: null
};
var noop = function () {
};
var firstInvalidEditor;
options = $.extend({}, defaultOptions, options);
function getContainerBox(i) {
var c = containers[i];
var offset = $(c).offset();
var w = $(c).width();
var h = $(c).height();
return {
top: offset.top,
left: offset.left,
bottom: offset.top + h,
right: offset.left + w,
width: w,
height: h,
visible: true
};
}
function editor(args) {
var editors = [];
function init() {
var newArgs = {};
var idx = columns.length;
while (idx--) {
if (columns[idx].editor) {
newArgs = $.extend({}, args);
newArgs.container = containers[idx];
newArgs.column = columns[idx];
newArgs.position = getContainerBox(idx);
newArgs.commitChanges = noop;
newArgs.cancelChanges = noop;
editors[idx] = new (columns[idx].editor)(newArgs);
}
}
}
this.destroy = function () {
var idx = editors.length;
while (idx--) {
editors[idx].destroy();
}
if (options.destroy) options.destroy();
};
this.focus = function () {
// if validation has failed, set the focus to the first invalid editor
(firstInvalidEditor || editors[0]).focus();
};
this.isValueChanged = function () {
var idx = editors.length;
while (idx--) {
if (editors[idx].isValueChanged()) {
return true;
}
}
return false;
};
this.serializeValue = function () {
var serializedValue = [];
var idx = editors.length;
while (idx--) {
serializedValue[idx] = editors[idx].serializeValue();
}
return serializedValue;
};
this.applyValue = function (item, state) {
var idx = editors.length;
while (idx--) {
editors[idx].applyValue(item, state[idx]);
}
};
this.loadValue = function (item) {
var idx = editors.length;
while (idx--) {
editors[idx].loadValue(item);
}
};
this.validate = function () {
var validationResults;
var errors = [];
firstInvalidEditor = null;
var idx = editors.length;
while (idx--) {
validationResults = editors[idx].validate();
if (!validationResults.valid) {
firstInvalidEditor = editors[idx];
errors.push({
index: idx,
editor: editors[idx],
container: containers[idx],
msg: validationResults.msg
});
}
}
if (errors.length) {
return {
valid: false,
msg: options.validationFailedMsg,
errors: errors
};
} else {
return {
valid: true,
msg: ""
};
}
};
function showOrHide(action) {
var idx = editors.length;
while (idx--) {
if (editors[idx][action]) editors[idx][action]();
}
if (options[action]) options[action]();
}
this.hide = function () {
showOrHide('hide');
};
this.show = function () {
showOrHide('show')
};
this.position = function (box) {
if(options.position) options.position(box);
};
init();
}
// so we can do "editor instanceof Slick.CompositeEditor
editor.prototype = this;
return editor;
}
})(jQuery);