LinkManager = function( options ) { let instance = this; instance.subjectType = options.subjectType; instance.subjectId = options.subjectId; instance.subjectTypeId = 0; instance.targetTypeId = 0; instance.label = options.label; //console.log( "LinkManager options", instance ); if( instance.subjectType == "DOC" || instance.subjectType == "DOCD" ) { instance.subjectTypeId = 30; } instance.height = options.height; instance.partnerLinks = false; instance.partnerType = null; instance.catalogLinks = false; instance.catalogType = null; if( options.partnerType && ( options.partnerType == "OPR" || options.partnerType == "CDR" || options.partnerType == "MFR" ) ) { instance.partnerLinks = true; instance.partnerType = options.partnerType; if( instance.partnerType == "OPR" ) { instance.targetTypeId = 41; if( ! instance.label ) { instance.label = "Operators" } } else if( instance.partnerType == "CDR" ) { instance.targetTypeId = 63; if( ! instance.label ) { instance.label = "Distributors" } } /*else if( instance.partnerType == "MFR" ) { instance.targetTypeId = 42; if( ! instance.label ) { instance.label = "Manufacturers" } }*/ } else if( options.catalogType && ( options.catalogType == "SKU" || options.catalogType == "PRD" ) ) { instance.catalogLinks = true; instance.catalogType = options.catalogType; if( instance.catalogType == "SKU" ) { instance.targetTypeId = 60; if( ! instance.label ) { instance.label = "SKUs" } } else if( instance.catalogType == "PRD" ) { instance.targetTypeId = 59; if( ! instance.label ) { instance.label = "Products (L2s)" } } } this.rootElement = null; } LinkManager.prototype.constructor = LinkManager; LinkManager.prototype.element = function() { let instance = this; if( instance.rootElement ) return instance.rootElement; instance.rootElement = $("
" ).css( { "width" : "100%", "display" : "flex", "flex-direction" : "col" } ) instance.createUI(); return instance.rootElement; } LinkManager.prototype.createUI = function() { let instance = this; if( instance.subjectTypeId == 0 || instance.targetTypeId == 0 || typeof instance.subjectId === "undefined" ) { return; } if( instance.partnerLinks ) { instance.createPartnerDataGrid(); } else if ( instance.catalogLinks ) { instance.createCatalogDataGrid(); } if( instance.dataGrid) { instance.rootElement.append( instance.dataGrid.element()) } } LinkManager.prototype.createPartnerDataGrid = function(){ let instance = this; let dataSource; let columns; let _subjectId = instance.subjectId; let isList = false; if ( isNaN(_subjectId) ) { const parts = _subjectId.split(","); isList = parts.length > 1; } let _mstrAttrId = instance.subjectId; if( isList ) { //_mstrAttrId = 0; } if( instance.partnerType == "OPR" ) { dataSource = Fse.Data.newDataSource( { object : "CRM.operatorList", paginate : true, pageSize: 50, keyField : "operatorId", objectParams : { linkedTo: { mstr_attrTypeId : instance.subjectTypeId, mstr_attrId :_mstrAttrId } } }); //console.log( "dataSource" ); //console.log( dataSource ); columns = [ { dataField : "companyName", caption : "Operator" }, { dataField : "address", caption : "Street" }, { dataField : "city", caption : "City" }, { dataField : "state", caption : "State", width : "auto" }, { dataField : "zipCode", caption : "Zip Code", width : "auto" }, { dataField : "territoryName", caption : "Territory" }, { dataField : "salesRepFullName", caption : "Account Owner" }, { type : "buttons", width : 30, buttons : [ { icon : "remove", hint : "Remove this link", onClick : function( e ) { instance.removeLink( e.row.data.operatorId ); } } ] } ] } else if( instance.partnerType == "CDR" ){ dataSource = Fse.Data.newDataSource( { object : "CRM.distributorList", paginate : true, pageSize: 50, keyField : "cdr_recordId", objectParams : { linkedTo: { mstr_attrTypeId : instance.subjectTypeId, mstr_attrId :_mstrAttrId } } }); columns = [ { dataField : "cdr_dstname", caption : "Distributor" }, { dataField : "cdr_dstaddress1", caption : "Street" }, { dataField : "cdr_dstcity", caption : "City" }, { dataField : "cdr_dststate", caption : "State", width : "auto" }, { dataField : "cdr_dstzip", caption : "Zip Code", width : "auto" }, { dataField : "territoryName", caption : "Territory" }, { dataField : "cmFullName", caption : "Account Owner" }, { type : "buttons", width : 30, buttons : [ { icon : "remove", hint : "Remove this link", onClick : function( e ) { instance.removeLink( e.row.data.cdr_recordId ); } } ] } ] } /*else if( instance.partnerType == "MFR" ){ dataSource = Fse.Data.newDataSource( { object : "BPL.principals", paginate : false, keyField : "mfr_id", objectParams : { linkedTo: { mstr_attrTypeId : instance.subjectTypeId, mstr_attrId :instance.subjectId } } }); columns = [ { dataField : "cdr_dstname", caption : "Distributor" }, { dataField : "cdr_dstaddress1", caption : "Street" }, { dataField : "cdr_dstcity", caption : "City" }, { dataField : "cdr_dststate", caption : "State", width : "auto" }, { dataField : "cdr_dstzip", caption : "Zip Code", width : "auto" }, { dataField : "territoryName", caption : "Territory" }, { dataField : "cmFullName", caption : "Account Owner" }, { type : "buttons", width : 30, buttons : [ { icon : "remove", hint : "Remove this link", onClick : function( e ) { instance.removeLink( e.row.data.cdr_recordId ); } } ] } ] }*/ instance.dataGrid = $("
").dxDataGrid( { dataSource : dataSource, columns : columns, showBorders : false, //height : instance.height ? instance.height : null, // height : 100, width : "100%", scrolling : { mode : "virtual"}, onToolbarPreparing : function( e ) { if(! e.toolbarOptions.items) { e.toolbarOptions.items = []; } if( instance.label ) { e.toolbarOptions.items.push( { location : "before", template : function() { return $("
").text( instance.label ).css( { "font-size" : "larger", "font-weight" : "bold", "margin-left" : "5px" } ); } }) } e.toolbarOptions.items.push( { location : "after", widget : "dxButton", options : { icon : "plus", onClick : function( e ) { let partnerPicker = new PartnerPicker( { partnerTypes : instance.partnerType } ); partnerPicker.show().done( function( partnerData ) { let targetId; if( partnerData.operatorId ) { targetId = partnerData.operatorId; } else if ( partnerData.cdr_recordId ) { targetId = partnerData.cdr_recordId; } instance.addLink( targetId ); }) } } } ) }, }).dxDataGrid( "instance" ); } LinkManager.prototype.createCatalogDataGrid = function(){ let instance = this; let dataSource; let columns; let _subjectId = instance.subjectId; let isList = false; if ( isNaN(_subjectId) ) { const parts = _subjectId.split(","); isList = parts.length > 1; } let _mstrAttrId = instance.subjectId; if( isList ) { //_mstrAttrId = 0; } if( instance.catalogType == "SKU" ) { dataSource = Fse.Data.newDataSource( { object : "PRD.productHierarchyPaths", paginate : true, pageSize : 50, keyField : "catalogId", objectParams : { catalogTypes : "SKU", linkedTo: { mstr_attrTypeId : instance.subjectTypeId, mstr_attrId :_mstrAttrId } } }); columns = [ { dataField: "mfr_name", caption : "Manufacturer", width : 200, visible : Fse.Portal.appConfiguration.STP.ownerType === "BRO"}, { dataField : "catalogCode", caption : "SKU", width : "auto" }, { dataField : "catalogDesc", caption : "Description" }, { dataField : "packSizeDesc", caption : "Pack Size", width : 100 }, { type : "buttons", width : 30, buttons : [ { icon : "remove", hint : "Remove this link", onClick : function( e ) { instance.removeLink( e.row.data.catalogId ); } } ] } ] } else { dataSource = Fse.Data.newDataSource( { object : "PRD.productHierarchyPaths", paginate : true, pageSize : 50, keyField : "catalogId", objectParams : { catalogTypes : "PRD", linkedTo: { mstr_attrTypeId : instance.subjectTypeId, mstr_attrId :_mstrAttrId } } }); columns = [ { dataField: "mfr_name", caption : "Manufacturer", width : 200, visible : Fse.Portal.appConfiguration.STP.ownerType === "BRO"}, { dataField : "catalogCode", caption : "Code", width : "auto" }, { dataField : "catalogDesc", caption : "Description" }, { type : "buttons", width : 30, buttons : [ { icon : "remove", hint : "Remove this link", onClick : function( e ) { instance.removeLink( e.row.data.catalogId ); } } ] } ] } instance.dataGrid = $("
").dxDataGrid( { dataSource : dataSource, columns : columns, showBorders : false, // scrolling : { mode : "virtual" }, //height : instance.height ? instance.height : null, //height : 100, width : "100%", rowAlternationEnabled : true, onToolbarPreparing : function( e ) { if(! e.toolbarOptions.items) { e.toolbarOptions.items = []; } if( instance.label ) { e.toolbarOptions.items.push( { location : "before", template : function() { return $("
").text( instance.label ).css( { "font-size" : "larger", "font-weight" : "bold", "margin-left" : "5px" } ); } }) } e.toolbarOptions.items.push( { location : "after", widget : "dxButton", options : { icon : "plus", onClick : function( e ) { let title = "Select SKUs"; if( instance.catalogType == "PRD" ) { title = "Select Products (L2)"; } let onProductsPicked = function( e ) { let targetIds = []; e.selectedItems.forEach( function( item ) { targetIds.push( item.catalogId ); }) if( targetIds.length ) { instance.addLink( targetIds ); } } let productPicker = new ProductPicker( { title : title, pick : [instance.catalogType], onProductsPicked : onProductsPicked } ); productPicker.show(); } } } ) } } ).dxDataGrid( "instance" ); } LinkManager.prototype.removeLink = function( targetId ) { let instance = this; let dataToSend = { mstr_attrTypeId : instance.subjectTypeId, mstr_attrId : instance.subjectId, child_attrTypeId : instance.targetTypeId, child_attrId : targetId, updateType : "delete" } Fse.Ajax.performAction( { object : "CRM.updateLinks", data : dataToSend }).done( function( response ) { console.log( "response", response ); if( instance.dataGrid ) { instance.dataGrid.refresh(); } $("
").dxToast( { message : "Changes saved successfully", type : "success", position : "bottom center", displayTime : 1000, closeOnClick : true, onHidden : function( e ) { e.component.element().remove(); e.component.dispose(); } }).appendTo( $("body") ).dxToast( "show" ); }) } LinkManager.prototype.addLink = function( targetId ) { let instance = this; if( !Array.isArray( targetId ) ) { targetId = [ targetId ]; } console.log( "adding links to ", targetId ); console.log( "for subject ", instance.subjectTypeId, instance.subjectId ); let dataToSend = []; targetId.forEach( function( id ) { dataToSend.push( { mstr_attrTypeId : instance.subjectTypeId, mstr_attrId : instance.subjectId, child_attrTypeId : instance.targetTypeId, child_attrId : id, updateType : "add" } ); }) if( dataToSend.length == 0 ) { return; } console.log( "dataToSend", dataToSend ); Fse.Ajax.performAction( { object : "CRM.updateLinks", data : dataToSend }).done( function( response ) { console.log( "response", response ); if( instance.dataGrid ) { instance.dataGrid.refresh(); } $("
").dxToast( { message : "Changes saved successfully", type : "success", position : "bottom center", displayTime : 1000, closeOnClick : true, onHidden : function( e ) { e.component.element().remove(); e.component.dispose(); } }).appendTo( $("body") ).dxToast( "show" ); }) }