").dxPopup( {
title : "Create Sub-Objective",
width : 600,
height : "auto",
onHidden : function( e ) {
e.component.element().remove();
},
hideOnOutsideClick : true,
contentTemplate : function() {
form = $("
").dxForm( {
formData : {
objName : parentObjective.objName,
objDescription : parentObjective.objDescription,
parentObjectiveId : parentObjective.objectiveId,
},
items : [
{ dataField : "territoryId", label : { text : "Territory" }, isRequired : true,
editorType : "dxSelectBox",
editorOptions : {
// dataSource : Fse.Data.newDataSource( { object : "TER.salesTerritories", keyField : "TerritoryID", filter : [ "localMarket", "=", 1 ] }),
disabled : true,
displayExpr : "territoryName",
valueExpr : "TerritoryID",
searchEnabled : true,
showClearButton : false,
placeholder : "Select Territory"
}
},
{ dataField : "objName", label : { text : "Name" }, isRequired : true, editorOptions : { maxLength : 50 } },
{ dataField : "objManagerId", label : { text : "Manager" }, editorType : "dxSelectBox", isRequired : true, editorOptions : {
placeholder : "Select Manager...",
searchEnabled : true,
searchMode : "contains",
searchExpr : "fullName",
dataSource : Fse.Data.newDataSource( { object : "FSPRO.members", key : "fspro_userId" } ),
displayExpr : "fullName",
valueExpr : "fspro_userId"
}},
{ dataField : "objManager2Id", label : { text : "Manager #2" }, editorType : "dxSelectBox", isRequired : false, editorOptions : {
showClearButton : true,
placeholder : "Select Manager #2...",
searchEnabled : true,
searchMode : "contains",
searchExpr : "fullName",
dataSource : Fse.Data.newDataSource( { object : "FSPRO.members", key : "fspro_userId" } ),
displayExpr : "fullName",
valueExpr : "fspro_userId"
}},
{ dataField : "objManager3Id", label : { text : "Manager #3" }, editorType : "dxSelectBox", isRequired : false, editorOptions : {
showClearButton : true,
placeholder : "Select Manager #3...",
searchEnabled : true,
searchMode : "contains",
searchExpr : "fullName",
dataSource : Fse.Data.newDataSource( { object : "FSPRO.members", key : "fspro_userId" } ),
displayExpr : "fullName",
valueExpr : "fspro_userId"
}},
{ dataField : "totalQuota", label : { text : quotaLabel }, editorType : "dxNumberBox", isRequired : true, editorOptions : { minValue : 0, showSpinButtons : false } },
{ dataField : "objDescription", label : { text : "Directions" }, isRequired : true, editorType : "dxTextArea", editorOptions : { maxLength : 500, height : "4.5em" } }
],
onFieldDataChanged : function( e ) {
if( e.dataField == "territoryId" ) {
if( e.value ) {
let territory = e.component.getEditor( "territoryId" ).option( "selectedItem" );
if( territory.primaryManager ) {
let useInternalSalesId = Fse.Portal.getConfiguration( "BOM.defaultSubObjectiveNameSuffix") == "internalSalesId";
let suffix = territory.internalSalesId;
if( ! useInternalSalesId ) {
suffix = territory.shortTerritoryName;
if( ! suffix ) {
suffix = territory.territoryName;
}
}
e.component.updateData( {
"objName" : `${parentObjective.objName} - ${suffix}`,
"objManagerId" : territory.primaryManager,
});
}
}
}
}
}).dxForm( "instance" );
let existingChildObjectives = {};
let childObjectivesDataSource = Fse.Data.newDataSource( {
object : "BOM.objectives",
filter : [ "parentObjectiveId", "=", parentObjective.objectiveId ],
paginate : false,
key : "objectiveId"
});
childObjectivesDataSource.load().done( function( childObjectives ) {
childObjectives.forEach( function( co ) {
existingChildObjectives[co.territoryId] = co;
})
console.log( "Child Objectives", existingChildObjectives );
territoryFilter = [ "localMarket", "=", 1 ];
if( parentObjective.territoryPath ) {
territoryFilter = [ [ "territoryPath", "startswith", parentObjective.territoryPath ], "and", territoryFilter ];
}
let territoryDataSource = Fse.Data.newDataSource( {
object : "TER.salesTerritories",
key : "TerritoryID",
filter : territoryFilter,
paginate : false,
map : function( data ) {
data.disabled = existingChildObjectives[data.TerritoryID] ? true : false
return data;
}
});
form.getEditor( "territoryId" ).option( { disabled : false, dataSource : territoryDataSource } );
})
return form.element();
},
toolbarItems : [
{
toolbar : "bottom",
location : "after",
widget : "dxButton",
options : {
text : "Create",
type : "default",
onClick : function( e ) {
let vr = form.validate();
if( ! vr.isValid ) return;
Fse.Ajax.performAction( { object : "BOM.createSubObjective", data : form.option( "formData" ) } ).done( function( createResult ) {
console.log( "BOM.createSubObjective", createResult );
instance.objectiveEditor.showSubObjectives( true );
})
popup.hide();
}
}
}
],
onShown : function( e ) {
form.getEditor( "territoryId" ).focus();
}
}).appendTo( $("body") ).dxPopup("instance");
popup.show();
}
ObjectiveManagement.prototype.createObjective = function ( createData ) {
let instance = this;
instance.updateToolbar();
instance.detailSocket.empty();
let oe = new ObjectiveEditor( createData, {
onCancel : function( e ) {
if( e.instance.data.objectiveId > 0 ) {
e.instance.refresh();
} else {
let backToList = true;
if( e.instance.isDirty()) {
backToList = confirm( "Discard changes?" );
}
if( backToList ) instance.showList();
}
}
} );
instance.detailSocket.append( oe.element() );
instance.multiView.option( "selectedIndex", 1 );
}
ObjectiveManagement.prototype.openObjective = function( objective, options ) {
let instance = this;
instance.updateToolbar( objective );
let editorOptions = $.extend( true, {}, options );
editorOptions.openSubObjective = function( objective, options ) {
console.log( "openSubObjective", objective );
instance.updateSubObjectiveToolbar( objective );
instance.subObjectiveEditor = new ObjectiveEditor( objective, options );
instance.subObjectiveSocket.empty().append( instance.subObjectiveEditor.element() );
instance.multiView.option( "selectedIndex", 2 );
};
instance.objectiveEditor = new ObjectiveEditor( objective, editorOptions );
instance.detailSocket.empty().append( instance.objectiveEditor.element() );
instance.multiView.option( "selectedIndex", 1 );
}
ObjectiveManagement.editBundles = function( currentBundleId ) {
let editPromise = $.Deferred();
let changesMade = 0;
let popup = $("
").dxPopup( {
title : "Manage Objective Groups",
width : 600,
height : "auto",
contentTemplate : function() {
let dataGrid = $("
").dxDataGrid( {
dataSource : null,
disabled : true,
keyExpr : "bundleId",
editing : {
allowAdding : true,
allowUpdating : true,
allowDeleting: function( options) {
if( options.row.data.bundleId == currentBundleId ) return false;
return options.row.data.objectiveCount ? false : true;
},
mode : "row",
startEditAction : "click",
useIcons : true,
},
selection : {
mode : "single",
showCheckBoxesMode : "always"
},
onInitNewRow : function( e ) {
e.data.active = 'Y';
console.log( "onInitNewRow", e );
},
onRowInserted : function( e ) {
console.log( "onRowInserted", e );
},
onSaving : function( e ) {
console.log( "onSaving", e );
const change = e.changes[0];
if (change) {
e.cancel = true;
// loadPanel.show();
let dataToSave = null;
if( change.type == "update" || change.type == "remove" ) {
e.component.byKey( change.key ).done( function( rowData ) {
dataToSave = $.extend( true, {}, rowData, change.data );
if( change.type == "remove" ) dataToSave.delete = true;
})
} else {
dataToSave = change.data;
}
// console.log( "dataToSave", dataToSave );
Fse.Ajax.performAction( { object : "BOM.saveObjectiveBundle", data : dataToSave } )
.then( function( result ) {
let bundleDataSource = e.component.option( "dataSource" );
if( change.type == "insert" ) {
change.key = result.bundleId;
change.data.bundleId = result.bundleId;
}
bundleDataSource = DevExpress.data.applyChanges( bundleDataSource, [ change ], { keyExpr : "bundleId" } );
e.component.option( { dataSource : bundleDataSource, editing : { editingRowKey : null, changes : []} } );
changesMade++;
})
}
},
columns : [
{ dataField : "bundleName", caption : "Name" },
{ dataField : "active", caption : "Active", width : 60,
lookup : {
dataSource : { store : { type : "array", data : [ { "text" : "Y" }, { "text" : "N" } ], key : "text" }},
displayExpr : "text", valueExpr : "text"
}
},
{ dataField : "objectiveCount", caption : "Objectives", dataType : "number", width : 80, format : "fixedPoint", allowEditing : false }
],
showBorders : true,
scrolling : { mode : "virtual" },
height : 400
}).dxDataGrid( "instance" );
// dataSource option is set to a simple array in order to facilitate in place editing
let bundleDataSource = Fse.Data.newDataSource( { object : "BOM.objectiveBundles", key : "bundleId", paginate : false, objectParams : { includeStats : true } } );
bundleDataSource.load().done( function( bundles ) {
dataGrid.option( { "dataSource" : bundles, "disabled" : false } )
})
return dataGrid.element();
},
onHidden : function( e ) {
e.component.element().remove();
editPromise.resolve( changesMade )
}
}).dxPopup("instance");
popup.element().appendTo( $("body") )
popup.show();
return editPromise;
}