").addClass(FORM_GROUP_CONTENT_CLASS).appendTo($group);
if (item.groupContentTemplate) {
var data = {
formData: this.option("formData"),
component: this
};
item.groupContentTemplate.render({
model: data,
container: (0, _dom.getPublicElement)($groupContent)
})
} else {
var layoutManager = this._renderLayoutManager(this._tryGetItemsForTemplate(item), $groupContent, {
colCount: item.colCount,
colCountByScreen: item.colCountByScreen,
alignItemLabels: item.alignItemLabels,
cssItemClass: item.cssItemClass
});
var colCount = layoutManager._getColCount();
if ((0, _array.inArray)(colCount, this._groupsColCount) === -1) {
this._groupsColCount.push(colCount)
}
$group.addClass(GROUP_COL_COUNT_CLASS + colCount)
}
},
_renderLayoutManager: function(items, $rootElement, options) {
var $element = (0, _renderer2.default)("
");
var that = this;
var config = that._getLayoutManagerConfig(items, options);
var baseColCountByScreen = {
lg: options.colCount,
md: options.colCount,
sm: options.colCount,
xs: 1
};
that._cachedColCountOptions.push({
colCountByScreen: (0, _extend.extend)(baseColCountByScreen, options.colCountByScreen)
});
$element.appendTo($rootElement);
var instance = that._createComponent($element, "dxLayoutManager", config);
instance.on("autoColCountChanged", function() {
that._refresh()
});
that._cachedLayoutManagers.push(instance);
return instance
},
_getValidationGroup: function() {
return this.option("validationGroup") || this
},
_getLayoutManagerConfig: function(items, options) {
var that = this;
var baseConfig = {
form: that,
validationGroup: that._getValidationGroup(),
showRequiredMark: that.option("showRequiredMark"),
showOptionalMark: that.option("showOptionalMark"),
requiredMark: that.option("requiredMark"),
optionalMark: that.option("optionalMark"),
requiredMessage: that.option("requiredMessage"),
screenByWidth: that.option("screenByWidth"),
layoutData: that.option("formData"),
labelLocation: that.option("labelLocation"),
customizeItem: that.option("customizeItem"),
minColWidth: that.option("minColWidth"),
showColonAfterLabel: that.option("showColonAfterLabel"),
onEditorEnterKey: that.option("onEditorEnterKey"),
onFieldDataChanged: function(args) {
if (!that._isDataUpdating) {
that._triggerOnFieldDataChanged(args)
}
},
validationBoundary: that.option("scrollingEnabled") ? that.$element() : void 0
};
return (0, _extend.extend)(baseConfig, {
items: items,
onContentReady: function(args) {
that._itemsRunTimeInfo.addItemsOrExtendFrom(args.component._itemsRunTimeInfo);
options.onContentReady && options.onContentReady(args)
},
colCount: options.colCount,
alignItemLabels: options.alignItemLabels,
cssItemClass: options.cssItemClass,
colCountByScreen: options.colCountByScreen,
onLayoutChanged: options.onLayoutChanged,
width: options.width
})
},
_createComponent: function($element, type, config) {
var that = this;
config = config || {};
that._extendConfig(config, {
readOnly: that.option("readOnly")
});
return that.callBase($element, type, config)
},
_attachSyncSubscriptions: function() {
var that = this;
that.on("optionChanged", function(args) {
var optionFullName = args.fullName;
if ("formData" === optionFullName) {
if (!(0, _type.isDefined)(args.value)) {
that._options.formData = args.value = {}
}
that._triggerOnFieldDataChangedByDataSet(args.value)
}
if (that._cachedLayoutManagers.length) {
(0, _iterator.each)(that._cachedLayoutManagers, function(index, layoutManager) {
if ("formData" === optionFullName) {
that._isDataUpdating = true;
layoutManager.option("layoutData", args.value);
that._isDataUpdating = false
}
if ("readOnly" === args.name || "disabled" === args.name) {
layoutManager.option(optionFullName, args.value)
}
})
}
})
},
_optionChanged: function(args) {
var rootNameOfComplexOption = this._getRootLevelOfExpectedComplexOption(args.fullName, ["formData", "items"]);
if (rootNameOfComplexOption) {
this._customHandlerOfComplexOption(args, rootNameOfComplexOption);
return
}
switch (args.name) {
case "formData":
if (!this.option("items")) {
this._invalidate()
} else {
if ((0, _type.isEmptyObject)(args.value)) {
this._resetValues()
}
}
break;
case "items":
case "colCount":
case "onFieldDataChanged":
case "onEditorEnterKey":
case "labelLocation":
case "alignItemLabels":
case "showColonAfterLabel":
case "customizeItem":
case "alignItemLabelsInAllGroups":
case "showRequiredMark":
case "showOptionalMark":
case "requiredMark":
case "optionalMark":
case "requiredMessage":
case "scrollingEnabled":
case "formID":
case "colCountByScreen":
case "screenByWidth":
case "stylingMode":
this._invalidate();
break;
case "showValidationSummary":
this._renderValidationSummary();
break;
case "minColWidth":
if ("auto" === this.option("colCount")) {
this._invalidate()
}
break;
case "readOnly":
break;
case "width":
this.callBase(args);
this._rootLayoutManager.option(args.name, args.value);
this._alignLabels(this._rootLayoutManager, this._rootLayoutManager.isSingleColumnMode());
break;
case "visible":
this.callBase(args);
if (args.value) {
(0, _dom.triggerShownEvent)(this.$element())
}
break;
case "validationGroup":
_validation_engine2.default.removeGroup(args.previousValue || this);
this._invalidate();
break;
default:
this.callBase(args)
}
},
_getRootLevelOfExpectedComplexOption: function(fullOptionName, expectedRootNames) {
var splitFullName = fullOptionName.split(".");
var result;
if (splitFullName.length > 1) {
var rootOptionName = splitFullName[0];
for (var i = 0; i < expectedRootNames.length; i++) {
if (rootOptionName.search(expectedRootNames[i]) !== -1) {
result = expectedRootNames[i]
}
}
}
return result
},
_tryCreateItemOptionAction: function(optionName, item, value, previousValue) {
return (0, _uiForm6.default)(optionName, {
item: item,
value: value,
previousValue: previousValue,
itemsRunTimeInfo: this._itemsRunTimeInfo
})
},
_tryExecuteItemOptionAction: function(action) {
return action && action.tryExecute()
},
_customHandlerOfComplexOption: function(args, rootOptionName) {
var nameParts = args.fullName.split(".");
var value = args.value;
if ("items" === rootOptionName) {
var itemPath = this._getItemPath(nameParts);
var item = this.option(itemPath);
var optionNameWithoutPath = args.fullName.replace(itemPath + ".", "");
var simpleOptionName = optionNameWithoutPath.split(".")[0].replace(/\[\d+]/, "");
var itemAction = this._tryCreateItemOptionAction(simpleOptionName, item, item[simpleOptionName], args.previousValue);
if (!this._tryExecuteItemOptionAction(itemAction) && item) {
this._changeItemOption(item, optionNameWithoutPath, value);
var items = this._generateItemsFromData(this.option("items"));
this.option("items", items)
}
}
if ("formData" === rootOptionName) {
var dataField = nameParts.slice(1).join(".");
var editor = this.getEditor(dataField);
if (editor) {
editor.option("value", value)
} else {
this._triggerOnFieldDataChanged({
dataField: dataField,
value: value
})
}
}
},
_getItemPath: function(nameParts) {
var itemPath = nameParts[0];
for (var i = 1; i < nameParts.length; i++) {
if (nameParts[i].search("items|tabs") !== -1) {
itemPath += "." + nameParts[i]
} else {
break
}
}
return itemPath
},
_triggerOnFieldDataChanged: function(args) {
this._createActionByOption("onFieldDataChanged")(args)
},
_triggerOnFieldDataChangedByDataSet: function(data) {
var that = this;
if (data && (0, _type.isObject)(data)) {
(0, _iterator.each)(data, function(dataField, value) {
that._triggerOnFieldDataChanged({
dataField: dataField,
value: value
})
})
}
},
_updateFieldValue: function(dataField, value) {
if ((0, _type.isDefined)(this.option("formData"))) {
var editor = this.getEditor(dataField);
this.option("formData." + dataField, value);
if (editor) {
var editorValue = editor.option("value");
if (editorValue !== value) {
editor.option("value", value)
}
}
}
},
_generateItemsFromData: function(items) {
var formData = this.option("formData");
var result = [];
if (!items && (0, _type.isDefined)(formData)) {
(0, _iterator.each)(formData, function(dataField) {
result.push({
dataField: dataField
})
})
}
if (items) {
(0, _iterator.each)(items, function(index, item) {
if ((0, _type.isObject)(item)) {
result.push(item)
} else {
result.push({
dataField: item
})
}
})
}
return result
},
_getItemByField: function(field, items) {
var that = this;
var fieldParts = (0, _type.isObject)(field) ? field : that._getFieldParts(field);
var fieldName = fieldParts.fieldName;
var fieldPath = fieldParts.fieldPath;
var resultItem;
if (items.length) {
(0, _iterator.each)(items, function(index, item) {
var itemType = item.itemType;
if (fieldPath.length) {
var path = fieldPath.slice();
item = that._getItemByFieldPath(path, fieldName, item)
} else {
if ("group" === itemType && !(item.caption || item.name) || "tabbed" === itemType && !item.name) {
var subItemsField = that._getSubItemField(itemType);
item.items = that._generateItemsFromData(item.items);
item = that._getItemByField({
fieldName: fieldName,
fieldPath: fieldPath
}, item[subItemsField])
}
}
if (that._isExpectedItem(item, fieldName)) {
resultItem = item;
return false
}
})
}
return resultItem
},
_getFieldParts: function(field) {
var fieldSeparator = ".";
var fieldName = field;
var separatorIndex = fieldName.indexOf(fieldSeparator);
var resultPath = [];
while (separatorIndex !== -1) {
resultPath.push(fieldName.substr(0, separatorIndex));
fieldName = fieldName.substr(separatorIndex + 1);
separatorIndex = fieldName.indexOf(fieldSeparator)
}
return {
fieldName: fieldName,
fieldPath: resultPath.reverse()
}
},
_getItemByFieldPath: function(path, fieldName, item) {
var that = this;
var itemType = item.itemType;
var subItemsField = that._getSubItemField(itemType);
var isItemWithSubItems = "group" === itemType || "tabbed" === itemType || item.title;
var result;
do {
if (isItemWithSubItems) {
var name = item.name || item.caption || item.title;
var isGroupWithName = (0, _type.isDefined)(name);
var nameWithoutSpaces = that._getTextWithoutSpaces(name);
var pathNode;
item[subItemsField] = that._generateItemsFromData(item[subItemsField]);
if (isGroupWithName) {
pathNode = path.pop()
}
if (!path.length) {
result = that._getItemByField(fieldName, item[subItemsField]);
if (result) {
break
}
}
if (!isGroupWithName || isGroupWithName && nameWithoutSpaces === pathNode) {
if (path.length) {
result = that._searchItemInEverySubItem(path, fieldName, item[subItemsField])
}
}
} else {
break
}
} while (path.length && !(0, _type.isDefined)(result));
return result
},
_getSubItemField: function(itemType) {
return "tabbed" === itemType ? "tabs" : "items"
},
_searchItemInEverySubItem: function(path, fieldName, items) {
var that = this;
var result;
(0, _iterator.each)(items, function(index, groupItem) {
result = that._getItemByFieldPath(path.slice(), fieldName, groupItem);
if (result) {
return false
}
});
if (!result) {
result = false
}
return result
},
_getTextWithoutSpaces: function(text) {
return text ? text.replace(/\s/g, "") : void 0
},
_isExpectedItem: function(item, fieldName) {
return item && (item.dataField === fieldName || item.name === fieldName || this._getTextWithoutSpaces(item.title) === fieldName || "group" === item.itemType && this._getTextWithoutSpaces(item.caption) === fieldName)
},
_changeItemOption: function(item, option, value) {
if ((0, _type.isObject)(item)) {
item[option] = value
}
},
_dimensionChanged: function() {
var currentScreenFactor = this._getCurrentScreenFactor();
if (this._lastMarkupScreenFactor !== currentScreenFactor) {
if (this._isColCountChanged(this._lastMarkupScreenFactor, currentScreenFactor)) {
this._targetScreenFactor = currentScreenFactor;
this._refresh();
this._targetScreenFactor = void 0
}
this._lastMarkupScreenFactor = currentScreenFactor
}
},
_isColCountChanged: function(oldScreenSize, newScreenSize) {
var isChanged = false;
(0, _iterator.each)(this._cachedColCountOptions, function(index, item) {
if (item.colCountByScreen[oldScreenSize] !== item.colCountByScreen[newScreenSize]) {
isChanged = true;
return false
}
});
return isChanged
},
_refresh: function() {
var editorSelector = "." + FOCUSED_STATE_CLASS + " input, ." + FOCUSED_STATE_CLASS + " textarea";
_events_engine2.default.trigger(this.$element().find(editorSelector), "change");
this.callBase()
},
_resetValues: function() {
this._itemsRunTimeInfo.each(function(_, itemRunTimeInfo) {
if ((0, _type.isDefined)(itemRunTimeInfo.widgetInstance) && (0, _type.isDefined)(itemRunTimeInfo.item) && "button" !== itemRunTimeInfo.item.itemType) {
itemRunTimeInfo.widgetInstance.reset();
itemRunTimeInfo.widgetInstance.option("isValid", true)
}
});
_validation_engine2.default.resetGroup(this._getValidationGroup())
},
_updateData: function(data, value, isComplexData) {
var that = this;
var _data = isComplexData ? value : data;
if ((0, _type.isObject)(_data)) {
(0, _iterator.each)(_data, function(dataField, fieldValue) {
that._updateData(isComplexData ? data + "." + dataField : dataField, fieldValue, (0, _type.isObject)(fieldValue))
})
} else {
if ((0, _type.isString)(data)) {
that._updateFieldValue(data, value)
}
}
},
registerKeyHandler: function(key, handler) {
this.callBase(key, handler);
this._itemsRunTimeInfo.each(function(_, itemRunTimeInfo) {
if ((0, _type.isDefined)(itemRunTimeInfo.widgetInstance)) {
itemRunTimeInfo.widgetInstance.registerKeyHandler(key, handler)
}
})
},
_focusTarget: function() {
return this.$element().find("." + FIELD_ITEM_CONTENT_CLASS + " [tabindex]").first()
},
_visibilityChanged: function(visible) {
if (visible && _browser2.default.msie) {
this._refresh()
}
},
_dispose: function() {
_validation_engine2.default.removeGroup(this._getValidationGroup());
this.callBase()
},
resetValues: function() {
this._resetValues()
},
updateData: function(data, value) {
this._updateData(data, value)
},
getEditor: function(dataField) {
return this._itemsRunTimeInfo.findWidgetInstanceByDataField(dataField) || this._itemsRunTimeInfo.findWidgetInstanceByName(dataField)
},
getButton: function(name) {
return this._itemsRunTimeInfo.findWidgetInstanceByName(name)
},
updateDimensions: function() {
var that = this;
var deferred = new _deferred.Deferred;
if (that._scrollable) {
that._scrollable.update().done(function() {
deferred.resolveWith(that)
})
} else {
deferred.resolveWith(that)
}
return deferred.promise()
},
itemOption: function(id, option, value) {
var _this2 = this;
var items = this._generateItemsFromData(this.option("items"));
var item = this._getItemByField(id, items);
switch (arguments.length) {
case 1:
return item;
case 3:
var itemAction = this._tryCreateItemOptionAction(option, item, value, item[option]);
this._changeItemOption(item, option, value);
if (!this._tryExecuteItemOptionAction(itemAction)) {
this.option("items", items)
}
break;
default:
if ((0, _type.isObject)(option)) {
var allowUpdateItems;
(0, _iterator.each)(option, function(optionName, optionValue) {
var itemAction = _this2._tryCreateItemOptionAction(optionName, item, optionValue, item[optionName]);
_this2._changeItemOption(item, optionName, optionValue);
if (!allowUpdateItems && !_this2._tryExecuteItemOptionAction(itemAction)) {
allowUpdateItems = true
}
});
allowUpdateItems && this.option("items", items)
}
}
},
validate: function() {
return _validation_engine2.default.validateGroup(this._getValidationGroup())
},
getItemID: function(name) {
return "dx_" + this.option("formID") + "_" + (name || new _guid2.default)
},
getTargetScreenFactor: function() {
return this._targetScreenFactor
}
});
(0, _component_registrator2.default)("dxForm", Form);
module.exports = Form;