Участник
|
Цитата:
Сообщение от Ion
Спасибо, нашел в Main_system_library методы generateWordTemplateFlyout, runWordTemplate - все то что нужно!
Вот эти функции, по сути если кому-то надо будет, по можно просто подпилить функцию generateExcelTemplateFlyout и добавить нужные фильтры.
X++: var CustomXrmCore;
(function(CustomXrmCore) {
var Commands;
(function(Commands) {
var ExportLibrary = function() {
function ExportLibrary() {
var _this = this;
this.generateWordTemplateFlyout = function(commandProperties, entityTypeName, isForm, gridControl) {
var documentType = 2
, ribbonRelationshipType = -1
, contextType = 0
, currentEntityTypeName = entityTypeName;
if (isForm)
contextType = 1;
else {
if (entityTypeName == "activitypointer" && gridControl != null && gridControl.getGrid() != null) {
var selectedRows = gridControl.getGrid().getSelectedRows();
if (selectedRows != null && selectedRows.getLength() == 1) {
var selectedEntity = selectedRows.get(0).data.entity;
if (selectedEntity != null)
currentEntityTypeName = selectedEntity.getEntityName()
}
}
if (typeof gridControl.getRelationship === "function") {
contextType = 2;
var relationship = gridControl.getRelationship();
if (relationship !== null && relationship.relationshipType === 0)
ribbonRelationshipType = 0;
else if (relationship !== null && relationship.relationshipType === 1)
ribbonRelationshipType = 1
}
}
var deferred1 = XrmCore.InternalUtilities.ExportUtilities.retrieveSystemDocumentTemplates(currentEntityTypeName, documentType)
, deferred2 = XrmCore.InternalUtilities.ExportUtilities.retrievePersonalDocumentTemplates(currentEntityTypeName, documentType)
, menu = Promise.all([deferred1, deferred2]).then(function(_a) {
var systemTemplates = _a[0]
, personalTemplates = _a[1];
ExportLibrary._systemTemplates = systemTemplates;
ExportLibrary._personalTemplates = personalTemplates;
return _this.buildMenu(systemTemplates, personalTemplates, entityTypeName, isForm, contextType, ribbonRelationshipType, documentType)
});
commandProperties["PopulationXML"] = menu;
return
}
;
this.generateExcelTemplateFlyout = function(commandProperties, entityTypeName, gridControl) {
var isForm = false
, documentType = 1
, contextType = 0
, ribbonRelationshipType = -1
, currentEntityTypeName = entityTypeName;
if (entityTypeName == "activitypointer" && gridControl != null && gridControl.getEntityName() != null)
currentEntityTypeName = gridControl.getEntityName();
if (typeof gridControl.getRelationship === "function") {
contextType = 2;
var relationship = gridControl.getRelationship();
if (relationship !== null && relationship.relationshipType === 0)
ribbonRelationshipType = 0;
else if (relationship !== null && relationship.relationshipType === 1)
ribbonRelationshipType = 1
}
var deferred1 = XrmCore.InternalUtilities.ExportUtilities.retrieveSystemDocumentTemplates(currentEntityTypeName, documentType)
, deferred2 = XrmCore.InternalUtilities.ExportUtilities.retrievePersonalDocumentTemplates(currentEntityTypeName, documentType)
, menu = Promise.all([deferred1, deferred2]).then(function(_a) {
var systemTemplates = _a[0]
, personalTemplates = _a[1];
ExportLibrary._systemTemplates = systemTemplates;
ExportLibrary._personalTemplates = personalTemplates;
return _this.buildMenu(systemTemplates, personalTemplates, entityTypeName, isForm, contextType, ribbonRelationshipType, documentType)
});
commandProperties["PopulationXML"] = menu;
return
};
this.openDocTemplateDialog = function(dialogName, documentType, gridControl, isForm) {
var width = 512
, height = 300
, docDialog = "DocumentTemplateUpload";
if (documentType == 2 && dialogName === "DownloadTemplate") {
width = 850;
height = 650
}
if (dialogName === "DownloadTemplate")
docDialog = "DocumentTemplateDownload";
var params = {};
if (isForm)
params["Entity_Name"] = gridControl.entityReference.entityType;
else {
var viewId = gridControl.getViewSelector().getCurrentView().id.replace("{", "").replace("}", "")
, currentView = gridControl.getViewSelector().getCurrentView().name
, fetchXmlData = gridControl.getFetchXml();
params["Entity_Name"] = gridControl.getEntityName();
params["View_Id"] = viewId;
params["current_view"] = currentView;
params["fetch_xmldata"] = fetchXmlData
}
params["DocTemplateFile_Type"] = documentType;
params["DocTemplate_ExpectedFileType"] = documentType == 1 ? 3 : documentType;
params["DocTemplate_Type"] = 9941;
Xrm.Navigation.openDialog(docDialog, {
width: width,
height: height,
position: 1
}, params).then(function(result) {
if (result.parameters && result.parameters["Is_DocumentTemplate_Uploaded"] != undefined && result.parameters["DocumentTemplate_index"] != undefined)
if (result.parameters["Is_DocumentTemplate_Uploaded"] == 1) {
var entityFormOptions = {};
entityFormOptions["entityName"] = "personaldocumenttemplate";
entityFormOptions["entityId"] = result.parameters["DocumentTemplate_index"];
var actionDescriptor = {
actionLabel: Xrm.Utility.getResourceString("Main_system_library", "View_Personal_Document_Template"),
eventHandler: function() {
Xrm.Navigation.openForm(entityFormOptions)
}
};
Xrm.UI.addGlobalNotification(1, 1, Xrm.Utility.getResourceString("Main_system_library", "DocTemplate_Successfully_Uploaded"), null, actionDescriptor)
}
Xrm.Utility.closeProgressIndicator()
});
return
}
;
this.runWordTemplate = function(commandProperties, entityTypeCode, gridControl, selectedRecords) {
var isForm = false
, records = [];
if (typeof selectedRecords === "string") {
records = [selectedRecords];
isForm = true
} else
records = [selectedRecords[0].Id];
var documentType = 2
, openMode = {
openMode: 2
}
, personalTemplateKeyword = "WordTemplates.Menu.PersonalWordTemplates."
, sourceControlId = commandProperties["SourceControlId"];
if (sourceControlId == "ViewAllMyTemplatesId") {
_this.navigateToPersonalDocumentTemplates("recordlist", "personaldocumenttemplate");
return
}
var templateEntityName = "documenttemplate";
if (sourceControlId.indexOf(personalTemplateKeyword) !== -1)
templateEntityName = "personaldocumenttemplate";
var templateId = sourceControlId.split(/[. ]+/).pop();
if (templateId === "UploadTemplate" || templateId === "DownloadTemplate") {
_this.openDocTemplateDialog(templateId, documentType, gridControl, isForm);
return
}
var templateRef = {
id: "{" + templateId + "}",
entityType: templateEntityName
}
, actualEntityTypeCode = isForm ? entityTypeCode : Xrm.Internal.getEntityCode(selectedRecords[0].TypeName)
, recordsString = JSON.stringify(records)
, exportWordDocumentRequest = new XrmCore.SdkContracts.ExportWordDocumentRequest(actualEntityTypeCode,templateRef,recordsString);
Xrm.Utility.showProgressIndicator(Xrm.Utility.getResourceString("Main_system_library", "ExportToDocx_ProgressDialog_Header"));
var deferred = Xrm.WebApi.online.execute(exportWordDocumentRequest).then(function(res) {
return res.json()
}).then(function(odataResponse) {
var template = undefined;
if (templateEntityName === "documenttemplate")
template = ExportLibrary._systemTemplates.filter(function(obj) {
return obj.Id === templateId
})[0];
else
template = ExportLibrary._personalTemplates.filter(function(obj) {
return obj.Id === templateId
})[0];
var file = {
fileContent: odataResponse.WordFile,
fileName: XrmCore.InternalUtilities.ExportUtilities.generateExportFileName(template.Name, documentType),
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
};
Xrm.UI.addGlobalNotification(1, 1, Xrm.Utility.getResourceString("Main_system_library", "Menu_Label_Group_Word_SucessfullyDownloaded"), null, null);
return Xrm.Navigation.openFile(file, openMode)
}, function(errorResponse) {
Xrm.Utility.closeProgressIndicator();
console.log(errorResponse);
var errorMessage = "";
if (errorResponse.innerror && errorResponse.innerror.message) {
if (errorResponse.innerror.message != "")
errorMessage = errorResponse.innerror.message
} else
errorMessage = errorResponse.message;
var errorDialogOptions = {
message: errorMessage,
errorCode: errorResponse.errorCode
};
Xrm.Navigation.openErrorDialog(errorDialogOptions)
}).then(function(res) {
console.log(res);
Xrm.Utility.closeProgressIndicator()
});
return
}
;
this.navigateToPersonalDocumentTemplates = function(pageType, entityName) {
var personalDocTemplatePageInput = {
pageType: pageType,
entityName: entityName
};
Xrm.Navigation.navigateTo(personalDocTemplatePageInput)
}
;
this.buildMenu = function(systemTemplates, personalTemplates, entityTypeName, isForm, contextType, ribbonRelationshipType, documentType) {
var menuId = XrmCore.InternalUtilities.ExportUtilities.determineRibbonMenuId(entityTypeName, isForm, contextType, ribbonRelationshipType, documentType, false)
, onlineMenuId = XrmCore.InternalUtilities.ExportUtilities.determineRibbonMenuId(entityTypeName, isForm, contextType, ribbonRelationshipType, documentType, true)
, commandId = XrmCore.InternalUtilities.ExportUtilities.determinRibbonCommandId(entityTypeName, isForm, contextType, ribbonRelationshipType, documentType, false)
, systemButtonsControl = systemTemplates != null ? XrmCore.InternalUtilities.ExportUtilities.buildControlsFromTemplates(menuId, onlineMenuId, commandId, systemTemplates, true, documentType) : null
, personalButtonsControl = personalTemplates != null ? XrmCore.InternalUtilities.ExportUtilities.buildControlsFromTemplates(menuId, onlineMenuId, commandId, personalTemplates, false, documentType) : null
, createcommandId = XrmCore.InternalUtilities.ExportUtilities.determinCreateRibbonCommandId(entityTypeName, isForm, contextType, documentType, true)
, createTemplates = XrmCore.InternalUtilities.ExportUtilities.retrieveCreateTemplates()
, createuploadDownloadButtonsControl = XrmCore.InternalUtilities.ExportUtilities.buildControlsForCreate(menuId, createcommandId, createTemplates, false, documentType)
, menuSection1 = XrmCore.InternalUtilities.ExportUtilities.buildMenuSection(menuId, false, documentType, true, 10, createuploadDownloadButtonsControl)
, menuSection2 = systemButtonsControl != null ? XrmCore.InternalUtilities.ExportUtilities.buildMenuSection(menuId, true, documentType, false, 20, systemButtonsControl) : null
, menuSection3 = personalButtonsControl != null ? XrmCore.InternalUtilities.ExportUtilities.buildMenuSection(menuId, false, documentType, false, 30, personalButtonsControl) : null
, menuSections = [];
menuSections.push(menuSection1);
menuSection2 != null && menuSections.push(menuSection2);
menuSection3 != null && menuSections.push(menuSection3);
var menu = XrmCore.InternalUtilities.ExportUtilities.buildMenu(menuId, menuSections);
return menu
}
return {
RunWordTemplate: runWordTemplate,
GenerateWordTemplateFlyout: generateWordTemplateFlyout
}
}
ExportLibrary = ExportLibrary();
ExportLibrary._systemTemplates = [];
ExportLibrary._personalTemplates = [];
return ExportLibrary;
}();
Commands.ExportLibrary = ExportLibrary
}
)(Commands = CustomXrmCore.Commands || (CustomXrmCore.Commands = {}))
}
)(CustomXrmCore || (CustomXrmCore = {}));
|