@pnp/spfx-property-controls
Version:
Reusable property pane controls for SharePoint Framework solutions
106 lines • 4.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SPColumnPickerService = void 0;
const tslib_1 = require("tslib");
const sp_http_1 = require("@microsoft/sp-http");
const sp_core_library_1 = require("@microsoft/sp-core-library");
const columnPicker_1 = require("../propertyFields/columnPicker");
/**
* Service implementation to get list & list items from current SharePoint site
*/
class SPColumnPickerService {
/**
* Service constructor
*/
constructor(_props, pageContext) {
this.props = _props;
this.context = pageContext;
}
/**
* Gets the collection of column for a selected list
*/
getColumns(displayHiddenColumns) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (sp_core_library_1.Environment.type === sp_core_library_1.EnvironmentType.Local) {
// If the running environment is local, load the data from the mock
return this.getColumnsFromMock();
}
else {
if (this.props.listId === undefined || this.props.listId === "") {
return this.getEmptyColumns();
}
const webAbsoluteUrl = this.props.webAbsoluteUrl ? this.props.webAbsoluteUrl : this.context.pageContext.web.absoluteUrl;
// If the running environment is SharePoint, request the lists REST service
let queryUrl = `${webAbsoluteUrl}/_api/lists(guid'${this.props.listId}')/Fields?$select=Title,Id,InternalName`;
// Check if the orderBy property is provided
if (this.props.orderBy !== null) {
queryUrl += '&$orderby=';
switch (this.props.orderBy) {
case columnPicker_1.PropertyFieldColumnPickerOrderBy.Id:
queryUrl += 'Id';
break;
case columnPicker_1.PropertyFieldColumnPickerOrderBy.Title:
queryUrl += 'Title';
break;
}
// Adds an OData Filter to the list
if (this.props.filter) {
if (displayHiddenColumns)
queryUrl += `&$filter=${encodeURIComponent(this.props.filter)}`;
else
queryUrl += `&$filter=Hidden eq false and ${encodeURIComponent(this.props.filter)}`;
}
else {
if (!displayHiddenColumns)
queryUrl += `&$filter=Hidden eq false`;
}
const response = yield this.context.spHttpClient.get(queryUrl, sp_http_1.SPHttpClient.configurations.v1);
const columns = (yield response.json());
// Check if onColumnsRetrieved callback is defined
if (this.props.onColumnsRetrieved) {
//Call onColumnsRetrieved
const lr = this.props.onColumnsRetrieved(columns.value);
let output;
//Conditional checking to see of PromiseLike object or array
if (lr instanceof Array) {
output = lr;
}
else {
output = yield lr;
}
columns.value = output;
}
return columns;
}
}
});
}
/**
* Returns an empty column for when a list isn't selected
*/
getEmptyColumns() {
return new Promise((resolve) => {
const listData = {
value: []
};
resolve(listData);
});
}
/**
* Returns 3 fake SharePoint Columns for the Mock mode
*/
getColumnsFromMock() {
return new Promise((resolve) => {
const listData = {
value: [
{ Title: 'Mock Column One', Id: '3bacd87b-b7df-439a-bb20-4d4d13523431', InternalName: 'MockColumnOne' },
{ Title: 'Mock Column Two', Id: '5e37c820-e2cb-49f7-93f5-14003c07788b', InternalName: 'Mock_x0020_Column_x0020_Two' },
{ Title: 'Mock Column Three', Id: '5fda7245-c4a7-403b-adc1-8bd8b481b4ee', InternalName: 'MockColumnThree' }
]
};
resolve(listData);
});
}
}
exports.SPColumnPickerService = SPColumnPickerService;
//# sourceMappingURL=SPColumnPickerService.js.map