-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompositeMultiFieldWidget.js
390 lines (313 loc) · 12.8 KB
/
CompositeMultiFieldWidget.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* @class CQ.form.CompositeMultiFieldWidget
* @extends CQ.Ext.CompositeField
* This Widget extends the feature of multifield and add support for composite and nested multifield.
*
* @Supports
Rich Text Editor,
Pathfield,
Textfield,
Textarea,
Radio,
Radiogroup,
Checkbox,
Checkboxgroup,
Dropdown,
Dialogfieldset,
Image Drag and Drop feature restricted to Pathfield and RTE only.
*
* @constructor
* Creates a new CustomMultiFieldWidget.
* @param {Object} config - The config object
* @xtype customMultiFieldWidget
*
* @author Naushad
*
*/
compositeMultiFieldWidget = {};
CQ.form.CompositeMultiFieldWidget = CQ.Ext.extend(CQ.form.CompositeField, {
/**
* @cfg {Hidden} hiddenField
* Hidden Field path where values to be stored
*/
hiddenField: null,
totalPadding: 0,
/** private
* overriding CQ.Ext.Component#constructor
*/
constructor: function(config) {
config = config || {};
if (!config.layout) {
config.layout = 'form';
config.padding = '10px';
config.anchor = "100%";
//config.typeHint="String";
}
var defaults = {
"border": false,
"layout": "form",
"anchor": "100%",
"style": "padding:5px;",
};
config = CQ.Util.applyDefaults(config, defaults);
CQ.form.CompositeMultiFieldWidget.superclass.constructor.call(this, config);
},
/** private
* overriding CQ.Ext.Component#initComponent constructor
*/
initComponent: function() {
CQ.form.CompositeMultiFieldWidget.superclass.initComponent.call(this);
this.hiddenField = new CQ.Ext.form.Hidden({
name: this.name
});
this.add(this.hiddenField);
//considering L-left and R-right padding as passed in the constructor config
this.totalPadding = 2 * 10 + 2 * 5;
var dialog = this.findParentByType('dialog'),
compositeMultifield = this.findParentByType('multifield');
dialog.on('loadcontent', function() {
if (_.isEmpty(compositeMultifield.dropTargets)) {
compositeMultifield.dropTargets = [];
}
compositeMultifield.dropTargets = compositeMultifield.dropTargets.concat(this.getDropTargets());
_.each(compositeMultifield.dropTargets, function(target) {
if (!target.highlight) {
return;
}
var dialogZIndex = parseInt(dialog.el.getStyle("z-index"), 10);
if (!isNaN(dialogZIndex)) {
target.highlight.zIndex = dialogZIndex + 1;
}
});
}, this);
dialog.on('beforesubmit', function() {
var jsonValue = this.getJsonData();
if (jsonValue) {
this.updateHidden(jsonValue);
}
}, this);
if (dialog.compositeMultifieldInit) {
return;
}
var tabPanel = compositeMultifield.findParentByType("tabpanel");
if (tabPanel) {
tabPanel.on("tabchange", function(panel) {
panel.doLayout();
});
}
dialog.on('hide', function() {
var editable = CQ.utils.WCM.getEditables()[this.path];
//remove from dialog cache
delete editable.dialogs[CQ.wcm.EditBase.EDIT];
delete CQ.WCM.getDialogs()["editdialog-" + this.path];
}, dialog);
dialog.compositeMultifieldInit = true;
},
// overriding CQ.form.CompositeField#setValue
setValue: function(value) {
if (typeof value === 'string') {
value = JSON.parse(value);
value = CQ.Ext.isArray(value) ? value[0] : value;
}
this.items.each(function(item, index) {
try {
//snippet for setting values of cqinclude dialogfieldset or simple dialogfieldset
if (CQ.Ext.isFunction(item.findByType)) {
var fieldset = null;
if (item.isXType("dialogfieldset")) {
fieldset = item;
} else {
fieldset = item.findByType("dialogfieldset");
fieldset = CQ.Ext.isArray(fieldset) ? fieldset[0] : fieldset;
}
if (!_.isEmpty(fieldset) && CQ.Ext.isFunction(item.ownerCt.setDialogFieldSetValues)) {
item.ownerCt.setDialogFieldSetValues(fieldset, value);
return;
}
}
//handle other fields
var xType = item.xtype;
if (xType == "label" || xType == "hidden" || !item.hasOwnProperty("key")) {
return;
}
var key = item.key.replace('./', '');
item.setValue(value[key]);
item.setWidth(item.width);
item.fireEvent('loadcontent', this);
} catch (e) {
CQ.Log.debug("CQ.form.CompositeMultiFieldWidget#setValue: " + e.message);
}
}, this);
},
setDialogFieldSetValues: function(fieldset, collections) {
if (fieldset.key) {
var fieldsetKey = fieldset.key.replace('./', '');
var fieldsetvals = collections[fieldsetKey];
fieldset.items.each(function(item, index /*, length*/ ) {
var key = item.key.replace('./', '');
item.setValue(fieldsetvals[key]);
item.setWidth(item.width);
});
}
},
getJsonData: function() {
var values = [];
var fieldValues = this.getValue();
if (!CQ.Ext.isEmpty(fieldValues) && Object.keys(fieldValues).length > 0) {
values.push(JSON.stringify(fieldValues));
}
return values;
},
/**
* Overriding CQ.form.CompositeField#getValue
* Returns the normalized data value (undefined or emptyText will be
* returned as ""). To return the raw value see {@link #getValue}.
* @return {Mixed} value The field value
*/
getValue: function() {
return this.getFieldValues();
},
/**
* @private
* Returns the data value which may or may not be a valid, defined value.
* To return a normalized value see {@link #getValue}.
* @return {Mixed} value The field value
*/
getFieldValues: function() {
var collections = {};
this.items.each(function(item, index /*, length*/ ) {
try {
//snippet for getting values of cq-include as dialogfieldset or simple dialogfieldset
if (CQ.Ext.isFunction(item.findByType)) {
var fieldset = null;
if (item.isXType("dialogfieldset")) {
fieldset = item;
} else {
fieldset = item.findByType("dialogfieldset");
fieldset = CQ.Ext.isArray(fieldset) ? fieldset[0] : fieldset;
}
if (!_.isEmpty(fieldset) && CQ.Ext.isFunction(item.ownerCt.getDialogFieldSetValues)) {
collections = item.ownerCt.getDialogFieldSetValues(fieldset, collections);
return;
}
}
var xType = item.getXType();
if (xType == "label" || xType == "hidden" || !item.hasOwnProperty("key")) {
return;
}
collections = this.ownerCt.getFieldValue(item, collections);
} catch (e) {
CQ.Log.debug("CQ.form.CompositeMultiFieldWidget#getFieldValues: " + e.message);
}
});
return collections;
},
getFieldValue: function(item, collections) {
var value = item.getValue() || '';
var key = item.key.replace('./', '');
//this is required to get value of check-box group
if (item.isXType("checkboxgroup") && CQ.Ext.isArray(value) && Object.keys(value).length > 0) {
var multiValue = new Array();
value.forEach(function(i) {
multiValue.push(i.getValue());
});
value = multiValue;
}
collections[key] = value;
return collections;
},
getDialogFieldSetValues: function(fieldset, collections) {
if (fieldset.key) {
var fieldsetVals = {};
var fieldsetKey = fieldset.key.replace('./', '');
fieldset.items.each(function(item, index /*, length*/ ) {
fieldsetVals = this.findParentByType("compositeMultiFieldWidget").getFieldValue(item, fieldsetVals);
});
collections[fieldsetKey] = fieldsetVals;
}
return collections;
},
// private hidden function
updateHidden: function(value) {
this.hiddenField.setValue(value);
},
validate: function() {
var valid = true;
this.items.each(function(i) {
if (!i.hasOwnProperty("key") || i.disabled) {
return;
}
if (!i.isVisible()) {
i.allowBlank = true;
return;
}
if (CQ.Ext.isFunction(i.validate) && !i.validate()) {
valid = false;
}
});
return valid;
},
//get drop targets - Hints - ACS common
getDropTargets: function() {
var targets = [],
t;
this.items.each(function() {
if (!this.getDropTargets) {
return;
}
t = this.getDropTargets();
if (_.isEmpty(t)) {
return;
}
targets = targets.concat(t);
});
return targets;
},
// private overriding CQ.Ext.Panel#onResize
// this is needed for resizing rte beacuse in MF/nested-MF,
// all events of rte are not fired and dimensions are not set properly
onResize: function(adjWidth, adjHeight, rawWidth, rawHeight) {
if (adjWidth > 0) {
for (var i = 0; i < this.items.length; i++) {
try {
var item = this.items.get(i);
var xType = item.getXType();
if (xType == "label" || xType == "hidden") {
return;
}
var fieldLabelMaxWidth = item.label.getWidth() > item.ownerCt.labelWidth ? item.label.getWidth() : item.ownerCt.labelWidth;
var availableFieldWidth = adjWidth - (fieldLabelMaxWidth + this.totalPadding);
if (item.isXType("multifield")) {
// check if multifield width has been set
// if Y, check if it's width is lt available fieldwidth, then set that width
// otherwise set with the available fieldWidth
// this will trigger resize event for nested multifield
if (item.width && item.width < availableFieldWidth) {
item.setWidth(item.width - 2);
} else {
item.setWidth(availableFieldWidth - 2);
}
} else {
var fieldHeight = item.editorHeight || item.getHeight();
// check if individual field width is lt the available fieldWidth
// if Y, then set the user supplied width, otherwise go for available width
// here is catch-
// widget doesn't get resized if there is no difference between last and current dimension
// so, forcing it to resize by differentiating it's width by 2
if (item.width && item.width < availableFieldWidth) {
item.setWidth(item.width - 2);
} else {
item.setWidth(availableFieldWidth - 2);
}
item.setHeight(fieldHeight);
}
} catch (e) {
CQ.Log.debug("CQ.form.CompositeMultiFieldWidget#onResize: " + e.message);
}
}
CQ.form.CompositeMultiFieldWidget.superclass.onResize.call(this, arguments);
}
},
});
// register compositeMultiFieldWidget xtype
CQ.Ext.reg('compositeMultiFieldWidget', CQ.form.CompositeMultiFieldWidget);