-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
record.js
359 lines (308 loc) · 10.1 KB
/
record.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* @api
*/
define([
'underscore',
'mageUtils',
'uiLayout',
'uiCollection'
], function (_, utils, layout, Collection) {
'use strict';
return Collection.extend({
defaults: {
active: true,
hasChanges: false,
fields: [],
errorsCount: 0,
fieldTmpl: 'ui/grid/editing/field',
rowTmpl: 'ui/grid/editing/row',
templates: {
fields: {
base: {
parent: '${ $.$data.record.name }',
name: '${ $.$data.column.index }',
provider: '${ $.$data.record.name }',
dataScope: 'data.${ $.$data.column.index }',
imports: {
disabled: '${ $.$data.record.parentName }:fields.${ $.$data.column.index }.disabled'
},
isEditor: true
},
text: {
component: 'Magento_Ui/js/form/element/abstract',
template: 'ui/form/element/input'
},
date: {
component: 'Magento_Ui/js/form/element/date',
template: 'ui/form/element/date',
dateFormat: 'MMM d, y h:mm:ss a'
},
select: {
component: 'Magento_Ui/js/form/element/select',
template: 'ui/form/element/select',
options: '${ JSON.stringify($.$data.column.options) }'
}
}
},
listens: {
elems: 'updateFields',
data: 'updateState'
},
imports: {
onColumnsUpdate: '${ $.columnsProvider }:elems'
},
modules: {
columns: '${ $.columnsProvider }',
editor: '${ $.editorProvider }'
}
},
/**
* Initializes record component.
*
* @returns {Record} Chainable.
*/
initialize: function () {
_.bindAll(this, 'countErrors');
utils.limit(this, 'updateState', 10);
return this._super();
},
/**
* Initializes observable properties.
*
* @returns {Record} Chainable.
*/
initObservable: function () {
this._super()
.track('errorsCount hasChanges')
.observe('active fields');
return this;
},
/**
* Adds listeners on a field.
*
* @returns {Record} Chainable.
*/
initElement: function (field) {
field.on('error', this.countErrors);
return this._super();
},
/**
* Creates new instance of a field.
*
* @param {Column} column - Column instance which contains field definition.
* @returns {Record} Chainable.
*/
initField: function (column) {
var field = this.buildField(column);
layout([field]);
return this;
},
/**
* Builds fields' configuration described in a provided column.
*
* @param {Column} column - Column instance which contains field definition.
* @returns {Object} Complete fields' configuration.
*/
buildField: function (column) {
var fields = this.templates.fields,
field = column.editor;
if (_.isObject(field) && field.editorType) {
field = utils.extend({}, fields[field.editorType], field);
} else if (_.isString(field)) {
field = fields[field];
}
field = utils.extend({}, fields.base, field);
return utils.template(field, {
record: this,
column: column
}, true, true);
},
/**
* Creates fields for the specfied columns.
*
* @param {Array} columns - An array of column instances.
* @returns {Record} Chainable.
*/
createFields: function (columns) {
columns.forEach(function (column) {
if (column.editor && !this.hasChild(column.index)) {
this.initField(column);
}
}, this);
return this;
},
/**
* Returns instance of a column found by provided index.
*
* @param {String} index - Index of a column (e.g. 'title').
* @returns {Column}
*/
getColumn: function (index) {
return this.columns().getChild(index);
},
/**
* Returns records' current data object.
*
* @returns {Object}
*/
getData: function () {
return this.filterData(this.data);
},
/**
* Returns saved records' data. Data will be processed
* with a 'filterData' and 'normalizeData' methods.
*
* @returns {Object} Saved records' data.
*/
getSavedData: function () {
var editor = this.editor(),
savedData = editor.getRowData(this.index);
savedData = this.filterData(savedData);
return this.normalizeData(savedData);
},
/**
* Replaces current records' data with the provided one.
*
* @param {Object} data - New records data.
* @param {Boolean} [partial=false] - Flag that defines whether
* to completely replace current data or to extend it.
* @returns {Record} Chainable.
*/
setData: function (data, partial) {
var currentData = partial ? this.data : {};
data = this.normalizeData(data);
data = utils.extend({}, currentData, data);
this.set('data', data)
.updateState();
return this;
},
/**
* Filters provided object extracting from it values
* that can be matched with an existing fields.
*
* @param {Object} data - Object to be processed.
* @returns {Object}
*/
filterData: function (data) {
var fields = _.pluck(this.elems(), 'index');
_.each(this.preserveFields, function (enabled, field) {
if (enabled && !_.contains(fields, field)) {
fields.push(field);
}
});
return _.pick(data, fields);
},
/**
* Parses values of a provided object with
* a 'normalizeData' method of a corresponding field.
*
* @param {Object} data - Data to be processed.
* @returns {Object}
*/
normalizeData: function (data) {
var index;
this.elems.each(function (elem) {
index = elem.index;
if (data.hasOwnProperty(index)) {
data[index] = elem.normalizeData(data[index]);
}
});
return data;
},
/**
* Clears values of all fields.
*
* @returns {Record} Chainable.
*/
clear: function () {
this.elems.each('clear');
return this;
},
/**
* Validates all of the available fields.
*
* @returns {Array} An array with validatation results.
*/
validate: function () {
return this.elems.map('validate');
},
/**
* Checks if all fields are valid.
*
* @returns {Boolean}
*/
isValid: function () {
return _.every(this.validate(), 'valid');
},
/**
* Counts total errors ammount accros all fields.
*
* @returns {Number}
*/
countErrors: function () {
var errorsCount = this.elems.filter('error').length;
this.errorsCount = errorsCount;
return errorsCount;
},
/**
* Returns difference between current data and its'
* initial state, retrieved from the records collection.
*
* @returns {Object} Object with changes descriptions.
*/
checkChanges: function () {
var savedData = this.getSavedData(),
data = this.normalizeData(this.getData());
return utils.compare(savedData, data);
},
/**
* Updates 'fields' array filling it with available edtiors
* or with column instances if associated field is not present.
*
* @returns {Record} Chainable.
*/
updateFields: function () {
var fields;
fields = this.columns().elems.map(function (column) {
return this.getChild(column.index) || column;
}, this);
this.fields(fields);
return this;
},
/**
* Updates state of a 'hasChanges' property.
*
* @returns {Record} Chainable.
*/
updateState: function () {
var diff = this.checkChanges(),
changed = {};
this.hasChanges = !diff.equal;
changed[this.index] = this.data;
this.editor().set('changed', [changed]);
return this;
},
/**
* Checks if provided column is an actions column.
*
* @param {Column} column - Column to be checked.
* @returns {Boolean}
*/
isActionsColumn: function (column) {
return column.dataType === 'actions';
},
/**
* Listener of columns provider child array changes.
*
* @param {Array} columns - Modified child elements array.
*/
onColumnsUpdate: function (columns) {
this.createFields(columns)
.updateFields();
}
});
});