Merge pull request #1194 from pschaeflein/master

Add properties for template url  & data url
This commit is contained in:
Hugo Bernier 2020-04-06 02:58:42 -04:00 committed by GitHub
commit 2550c81cb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 131 additions and 14 deletions

View File

@ -3,12 +3,14 @@ import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library'; import { Version } from '@microsoft/sp-core-library';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base'; import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import { DisplayMode } from '@microsoft/sp-core-library'; import { DisplayMode } from '@microsoft/sp-core-library';
import { HttpClient, HttpClientConfiguration, HttpClientResponse } from '@microsoft/sp-http';
// Used for property pane // Used for property pane
import { import {
IPropertyPaneConfiguration, IPropertyPaneConfiguration,
PropertyPaneChoiceGroup, PropertyPaneChoiceGroup,
PropertyPaneToggle PropertyPaneToggle,
PropertyPaneTextField
} from '@microsoft/sp-property-pane'; } from '@microsoft/sp-property-pane';
// Used to select which list // Used to select which list
@ -40,15 +42,25 @@ import * as strings from 'AdaptiveCardHostWebPartStrings';
import AdaptiveCardHost from './components/AdaptiveCardHost'; import AdaptiveCardHost from './components/AdaptiveCardHost';
import { IAdaptiveCardHostProps } from './components/IAdaptiveCardHostProps'; import { IAdaptiveCardHostProps } from './components/IAdaptiveCardHostProps';
export type TemplateSourceType = 'json' | 'url';
export type DataSourceType = 'list' | 'json'; export type DataSourceType = 'list' | 'json' | 'url';
export interface IAdaptiveCardHostWebPartProps { export interface IAdaptiveCardHostWebPartProps {
/**
* Either 'json' or 'url'
*/
templateSource: TemplateSourceType;
/** /**
* The JSON Adaptive Cards template * The JSON Adaptive Cards template
*/ */
template: string; template: string;
/**
* The URL to the template json
*/
templateUrl: string;
/** /**
* The static JSON data, if using * The static JSON data, if using
*/ */
@ -60,7 +72,7 @@ export interface IAdaptiveCardHostWebPartProps {
useTemplating: boolean; useTemplating: boolean;
/** /**
* Either 'list' or 'json' * Either 'list' or 'json' or 'url'
*/ */
dataSource: DataSourceType; dataSource: DataSourceType;
@ -73,6 +85,11 @@ export interface IAdaptiveCardHostWebPartProps {
* The view id of the selected view * The view id of the selected view
*/ */
view: string | undefined; view: string | undefined;
/**
* The url of the remote data
*/
dataUrl: string | undefined;
} }
export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdaptiveCardHostWebPartProps> { export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdaptiveCardHostWebPartProps> {
@ -82,6 +99,7 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
private _dataPropertyPaneHelper: any; private _dataPropertyPaneHelper: any;
private _dataJSON: string; private _dataJSON: string;
private _viewSchema: string; private _viewSchema: string;
private _templateJSON: string;
protected async onInit(): Promise<void> { protected async onInit(): Promise<void> {
@ -100,12 +118,17 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
spfxContext: this.context spfxContext: this.context
}); });
await this._loadTemplateFromUrl();
await this._loadDataFromList(); await this._loadDataFromList();
await this._loadDataFromUrl();
} }
public async render(): Promise<void> { public async render(): Promise<void> {
const { template } = this.properties; const { template } = this.properties;
const dataJson: string = this.properties.dataSource === 'list' && this.properties.list && this.properties.view ? this._dataJSON : this.properties.data; const templateJson: string = this.properties.templateSource === 'url' && this.properties.templateUrl ? this._templateJSON: this.properties.template;
const dataJson: string = (this.properties.dataSource === 'list' && this.properties.list && this.properties.view) ||
(this.properties.dataSource === 'url' && this.properties.dataUrl) ? this._dataJSON : this.properties.data;
// The Adaptive Card control does not care where the template and data are coming from. // The Adaptive Card control does not care where the template and data are coming from.
// Pass a valid template JSON and -- if using -- some data JSON // Pass a valid template JSON and -- if using -- some data JSON
@ -113,7 +136,7 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
AdaptiveCardHost, AdaptiveCardHost,
{ {
themeVariant: this._themeVariant, themeVariant: this._themeVariant,
template: template, template: templateJson,
data: dataJson, data: dataJson,
useTemplating: this.properties.useTemplating === true, useTemplating: this.properties.useTemplating === true,
context: this.context, context: this.context,
@ -155,7 +178,7 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
language: PropertyFieldCodeEditorLanguages.JSON language: PropertyFieldCodeEditorLanguages.JSON
}); });
// create a help for data // create a helper for data
this._dataPropertyPaneHelper = codeEditor.PropertyFieldCodeEditor('data', { this._dataPropertyPaneHelper = codeEditor.PropertyFieldCodeEditor('data', {
label: strings.DataJSONFieldLabel, label: strings.DataJSONFieldLabel,
panelTitle: strings.DataPanelTitle, panelTitle: strings.DataPanelTitle,
@ -169,9 +192,12 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
} }
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration { protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
const isTemplateJSONBound: boolean = this.properties.templateSource === 'json';
const isTemplateUrlBound: boolean = this.properties.templateSource === 'url';
const isJSONBound: boolean = this.properties.useTemplating === true && this.properties.dataSource === 'json'; const isDataJSONBound: boolean = this.properties.useTemplating === true && this.properties.dataSource === 'json';
const isListBound: boolean = this.properties.useTemplating === true && this.properties.dataSource === 'list'; const isDataListBound: boolean = this.properties.useTemplating === true && this.properties.dataSource === 'list';
const isDataUrlBound: boolean = this.properties.useTemplating === true && this.properties.dataSource === 'url';
return { return {
pages: [ pages: [
@ -191,7 +217,29 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
moreInfoLinkTarget: "_blank", moreInfoLinkTarget: "_blank",
key: 'adaptiveCardJSONId' key: 'adaptiveCardJSONId'
}), }),
this._templatePropertyPaneHelper PropertyPaneChoiceGroup('templateSource', {
label: strings.TemplateSourceFieldLabel,
options: [
{
key: 'json',
text: strings.TemplateSourceFieldChoiceJSON,
iconProps: {
officeFabricIconFontName: 'Code'
}
},
{
key: 'url',
text: strings.TemplateSourceFieldChoiceUrl,
iconProps: {
officeFabricIconFontName: 'Globe'
}
}
]
}),
isTemplateJSONBound && this._templatePropertyPaneHelper,
isTemplateUrlBound && PropertyPaneTextField('templateUrl', {
label: strings.DataUrlLabel,
})
] ]
}, },
{ {
@ -224,15 +272,22 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
iconProps: { iconProps: {
officeFabricIconFontName: 'CustomList' officeFabricIconFontName: 'CustomList'
}, },
},
{
key: 'url',
text: strings.DataSourceFieldChoiceUrl,
iconProps: {
officeFabricIconFontName: 'Globe'
}
} }
] ]
}), }),
isJSONBound && this._dataPropertyPaneHelper, isDataJSONBound && this._dataPropertyPaneHelper,
isJSONBound && PropertyPaneWebPartInformation({ isDataJSONBound && PropertyPaneWebPartInformation({
description: strings.UseTemplatingDescription, description: strings.UseTemplatingDescription,
key: 'dataInfoId' key: 'dataInfoId'
}), }),
isListBound && PropertyFieldListPicker('list', { isDataListBound && PropertyFieldListPicker('list', {
label: strings.ListFieldLabel, label: strings.ListFieldLabel,
selectedList: this.properties.list, selectedList: this.properties.list,
includeHidden: false, includeHidden: false,
@ -245,7 +300,7 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
deferredValidationTime: 0, deferredValidationTime: 0,
key: 'listPickerFieldId' key: 'listPickerFieldId'
}), }),
isListBound && PropertyFieldViewPicker('view', { isDataListBound && PropertyFieldViewPicker('view', {
label: strings.ViewPropertyFieldLabel, label: strings.ViewPropertyFieldLabel,
context: this.context, context: this.context,
selectedView: this.properties.view, selectedView: this.properties.view,
@ -257,6 +312,9 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
onGetErrorMessage: null, onGetErrorMessage: null,
deferredValidationTime: 0, deferredValidationTime: 0,
key: 'viewPickerFieldId' key: 'viewPickerFieldId'
}),
isDataUrlBound && PropertyPaneTextField('dataUrl', {
label: strings.DataUrlLabel,
}) })
] ]
} }
@ -287,6 +345,16 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
// Render the card // Render the card
this.render(); this.render();
} }
if (propertyPath === 'templateUrl') {
await this._loadTemplateFromUrl();
this.render();
}
if (propertyPath === 'dataUrl'){
await this._loadDataFromUrl();
this.render();
}
} }
/** /**
@ -330,4 +398,41 @@ export default class AdaptiveCardHostWebPart extends BaseClientSideWebPart<IAdap
console.log("Data JSON", this._dataJSON); console.log("Data JSON", this._dataJSON);
} }
} }
/**
* Loads data from a url
*/
private async _loadDataFromUrl(): Promise<void> {
// There is no need to load data if the url is not configured
if (this.properties.dataSource !== 'url' || !this.properties.dataUrl) {
return;
}
this.context.httpClient.get(this.properties.dataUrl, HttpClient.configurations.v1)
.then((response: HttpClientResponse) => {
if (response.ok) {
response.json()
.then((data: any) => {
this._dataJSON = JSON.stringify(data);
});
}
});
}
private async _loadTemplateFromUrl(): Promise<void> {
if (this.properties.templateSource !== 'url' || !this.properties.templateUrl) {
return;
}
this.context.httpClient.get(this.properties.templateUrl, HttpClient.configurations.v1)
.then((response: HttpClientResponse) => {
if (response.ok) {
response.json()
.then((data: any) => {
this._templateJSON = JSON.stringify(data);
});
}
});
}
} }

View File

@ -8,8 +8,14 @@ define([], function () {
DataNeededDescription: "When you use Adaptive Card Templating, you need to provide either static JSON data, or from a SharePoint list.", DataNeededDescription: "When you use Adaptive Card Templating, you need to provide either static JSON data, or from a SharePoint list.",
DataNeededIconText: "You need data!", DataNeededIconText: "You need data!",
ListFieldLabel: "Select a list", ListFieldLabel: "Select a list",
DataUrlLabel: "Data source URL",
TemplateSourceFieldLabel: "Template source",
TemplateSourceFieldChoiceJSON: "JSON",
TemplateSourceFieldChoiceUrl: "URL",
TemplateUrlLabel: "Template url",
DataSourceFieldChoiceList: "List", DataSourceFieldChoiceList: "List",
DataSourceFieldChoiceJSON: "JSON", DataSourceFieldChoiceJSON: "JSON",
DataSourceFieldChoiceUrl: "URL",
DataSourceFieldLabel: "Data source", DataSourceFieldLabel: "Data source",
ViewPropertyFieldLabel: "Select a view", ViewPropertyFieldLabel: "Select a view",
TemplateJsonError: "Invalid template JSON: ", TemplateJsonError: "Invalid template JSON: ",

View File

@ -7,8 +7,14 @@ declare interface IAdaptiveCardHostWebPartStrings {
DataNeededDescription: string; DataNeededDescription: string;
DataNeededIconText: string; DataNeededIconText: string;
ListFieldLabel: string; ListFieldLabel: string;
DataUrlLabel: string;
TemplateSourceFieldLabel: string;
TemplateSourceFieldChoiceJSON: string;
TemplateSourceFieldChoiceUrl: string;
TemplateUrlLabel: string;
DataSourceFieldChoiceList: string; DataSourceFieldChoiceList: string;
DataSourceFieldChoiceJSON: string; DataSourceFieldChoiceJSON: string;
DataSourceFieldChoiceUrl: string;
DataSourceFieldLabel: string; DataSourceFieldLabel: string;
ViewPropertyFieldLabel: string; ViewPropertyFieldLabel: string;
TemplateJsonError: string; TemplateJsonError: string;