").addClass( "FlexFieldManager" )
instance.rootElement.append( instance.dataGrid.element() )
return instance.rootElement;
}
FlexFieldManager.prototype.addField = function() {
let instance = this;
let form = null;
let popup = $("
").dxPopup( {
title : "Add Field",
height : "auto",
width : 500,
contentTemplate : function( ) {
let items = [];
if ( instance.flexType == "form" ) {
items.push( {
dataField : "mappedField",
label : {
text : "CRM Mapping"
},
editorType : "dxSelectBox",
editorOptions : {
showClearButton : true,
disabled : true,
placeholder : "select CRM field",
displayExpr : "fieldLabel",
valueExpr : "fieldName",
// TODO: this search is not working, it may be something with the datasource
searchEnabled : true,
searchExpr : "fieldLabel",
searchMode : "contains"
}
})
}
items.push(
{ dataField : "label", label : { text : "Label" }, isRequired: true, editorOptions : { placeholder : "user facing label for this field" } },
{ dataField : "type", label : { text : "Type" },
editorType : "dxSelectBox",
editorOptions : {
placeholder : "select field type",
dataSource : {
store : {
type : "array",
data : [
{ type : "CHECKBOX" },
{ type : "COMMENT" },
{ type : "FILE" },
{ type : "RADIO" },
{ type : "SELECT" },
{ type : "TEXT" },
{ type : "TEXTAREA" }
],
key : "type"
}
},
displayExpr : "type",
valueExpr : "type"
}
},
{
dataField : "defaultFieldOptions.format",
label : { text : "Response Format" },
editorType : "dxSelectBox",
editorOptions : {
dataSource : {
store : {
type : "array",
data : [
{ text : "String", value : "string" },
{ text : "Email", value : "email" },
{ text : "Website", value : "url" },
{ text : "Date", value : "date" },
{ text : "Decimal", value : "decimal" },
{ text : "Phone", value : "phone" }
],
keyField : "value"
}
},
displayExpr : "text",
valueExpr : "value"
}
}
/*,
{ dataField : "name", label : { text : "Name" }, editorOptions : {
maxLength : 50,
placeholder : "AUTO or unique field name, letters and numbers only",
onValueChanged : function( e ) {
if( e.value && e.value != e.value.toUpperCase() ) {
e.component.option( "value", e.value.toUpperCase() )
}
} },
validationRules : [
{
type : "required"
},
{
type : "pattern",
pattern : "^[A-Z][A-Z0-9]*$",
message : "Field name must start with a letter and contain only letters and numbers"
},
{
type : "async",
validationCallback : function( params ) {
let d = $.Deferred();
if( params.value == "AUTO" ) {
d.resolve();
return d;
}
Fse.Ajax.performAction( {
object : "WRK.validateFlexFieldName",
data : { name : params.value, domain : instance.domain },
}).done( function( result ) {
if( result.status ) {
d.resolve();
} else {
d.reject( result.message );
}
}).fail( function() {
d.reject( "Validation Error" )
})
return d;
}
}
]
}
*/
);
items.push( {
dataField : "required",
label : { text : "Required" },
editorType : "dxSwitch"
})
form = $("
").dxForm( {
formData : {
type : "TEXT",
defaultFieldOptions : { format : "string" },
name : "AUTO"
},
onFieldDataChanged : function( e ) {
if( e.dataField == "mappedField" ) {
let updateFormData = {
type : "TEXT",
defaultFieldOptions : { format : "string" }
};
if( e.value ) {
let mappedFieldEditor = e.component.getEditor( "mappedField" );
let selectedField = mappedFieldEditor.option( "selectedItem" );
updateFormData.type = selectedField.fieldType;
if( selectedField.defaultFieldOptions ) {
updateFormData.defaultFieldOptions = selectedField.defaultFieldOptions;
}
if( selectedField.formLabel ) {
updateFormData.label = selectedField.formLabel;
}
}
e.component.updateData( updateFormData );
}
if( e.dataField == "type" ) {
if( e.value == "TEXT" ) {
e.component.getEditor( "defaultFieldOptions.format" ).option( { disabled : false } );
} else {
e.component.getEditor( "defaultFieldOptions.format" ).option( { disabled : true } );
}
}
},
items : items
}).dxForm("instance");
// this is being set here because when it was done with the initial field config the search didn't work correctly
let contactMappingFieldsStore = Fse.Data.newDataSource( { object : "WRK.contactMappingFields", paginate : false, keyField : "fieldName" } ).store();
contactMappingFieldsStore.load().done( function( contactMappingFields ) {
let editor = form.getEditor( "mappedField" );
editor.option( {
dataSource : {
store : {
type : "array",
data : contactMappingFields,
key : "fieldName"
}
},
disabled : false
})
})
return form.element();
},
hideOnOutsideClick : true,
onHidden : function(e ) {
e.component.element().remove();
e.component.dispose();
},
toolbarItems : [
{ toolbar : "bottom",
location : "after",
widget : "dxButton",
options : {
text : "Add Field",
type : "default",
onClick : function( e ) {
let vr = form.validate();
let okayToSave = $.Deferred();
if( vr.status === "pending" ) {
vr.complete.then( function( res ) {
if( res.isValid ) {
okayToSave.resolve();
} else {
console.log( res );
}
})
} else if( vr.isValid ) {
okayToSave.resolve();
};
okayToSave.done( function() {
let saveData = form.option( "formData" );
saveData.fieldId = 0;
saveData.domain = instance.domain;
saveData.category = instance.category;
console.log( "Saving" );
Fse.Ajax.performAction( {
object : "WRK.saveFlexField",
data : saveData,
}).done( function( result ) {
console.log( "Saved" );
popup.hide();
instance.dataGrid.refresh().done( function() {
instance.dataGrid.collapseAll( -1 );
instance.dataGrid.expandRow( result.fieldId );
});
})
})
}
}
}
]
}).dxPopup( "instance" );
popup.element().appendTo( $("body") );
popup.show();
}
FlexFieldManager.prototype.deleteField = function( data) {
let instance = this;
Fse.Ajax.performAction( {
object : "WRK.deleteFlexField",
data : { fieldId : data.fieldId, name : data.name, domain : instance.domain },
}).done( function( result ) {
console.log( "deleted" );
instance.dataGrid.refresh()
})
}