data helpers, typings

This commit is contained in:
Aliaxei Terentiev 2016-11-09 13:55:05 -08:00
parent b0e6fdb417
commit 3738bd7bfd
6 changed files with 220 additions and 0 deletions

View File

@ -0,0 +1,30 @@
/**
* Represents SharePoint View object
*/
export interface ISPView {
Title: string;
Id: string;
ListId: string;
}
/**
* Represents SharePoint List object
*/
export interface ISPList {
Title: string;
Id: string;
}
/**
* Represents SharePoint REST service response for /_api/web/lists service call
*/
export interface ISPLists {
value: ISPList[];
}
/**
* Represents SharePoint REST service response for /_api/web/lists('id')/views service call
*/
export interface ISPViews {
value: ISPView[];
}

View File

@ -0,0 +1,15 @@
import * as SP from '../typings/SP';
/**
* Data Helpers interface
*/
export interface IDataHelper {
/**
* API to get lists from the source
*/
getLists(): Promise<ISPList[]>;
/**
* API to get views from the source
*/
getViews(listId: string): Promise<ISPView[]>;
}

View File

@ -0,0 +1,38 @@
import { ISPList, ISPView } from '../common/SPEntities';
import { IDataHelper } from './DataHelperBase';
/**
* MOCK data helper. Gets data from hardcoded values
*/
export class DataHelperMock implements IDataHelper {
/**
* hardcoded collection of lists
*/
private static _lists: ISPList[] = [{ Title: 'Test 1', Id: '1' }, { Title: 'Test 2', Id: '2' }, { Title: 'Test 3', Id: '3' }];
/**
* hardcoded collection of views
*/
private static _views: ISPView[] = [{ Title: 'All Items', Id: '1', ListId: '1' }, { Title: 'Demo', Id: '2', ListId: '1' }, { Title: 'All Items', Id: '1', ListId: '2' }, { Title: 'All Items', Id: '1', ListId: '3' }];
/**
* API to get lists from the source
*/
public getLists(): Promise<ISPList[]> {
return new Promise<ISPList[]>((resolve) => {
resolve(DataHelperMock._lists); // returning all the lists
});
}
/**
* API to get views from the source
*/
public getViews(listId: string): Promise<ISPView[]> {
return new Promise<ISPView[]>((resolve) => {
// filtering views based on list id
const result: ISPView[] = DataHelperMock._views.filter((value, index, array) => {
return value.ListId === listId;
});
resolve(result);
});
}
}

View File

@ -0,0 +1,83 @@
import {
IWebPartContext
} from '@microsoft/sp-webpart-base';
import { ISPList, ISPView, ISPLists, ISPViews } from '../common/SPEntities';
import { IDataHelper } from './DataHelperBase';
/**
* List with views interface
*/
interface ISPListWithViews extends ISPList {
/**
* List Views
*/
Views: ISPView[];
}
/**
* SharePoint Data Helper class.
* Gets information from current web
*/
export class DataHelperSP implements IDataHelper {
/**
* Web part context
*/
public context: IWebPartContext;
/**
* Loaded lists
*/
private _lists: ISPListWithViews[];
/**
* ctor
*/
public constructor(_context: IWebPartContext) {
this.context = _context;
}
/**
* API to get lists from the source
*/
public getLists(): Promise<ISPList[]> {
return this.context.httpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/web/lists?$filter=Hidden eq false`) // sending the request to SharePoint REST API
.then((response: Response) => { // httpClient.get method returns a response object where json method creates a Promise of getting result
return response.json();
}).then((response: ISPLists) => { // response is an ISPLists object
return response.value;
});
}
/**
* API to get views from the source
*/
public getViews(listId: string): Promise<ISPView[]> {
if (listId && listId == '-1' || listId == '0')
return new Promise<ISPView[]>((resolve) => {
resolve(new Array<ISPView>());
});
//
// trying to get views from cache
//
const lists: ISPListWithViews[] = this._lists && this._lists.length && this._lists.filter((value, index, array) => { return value.Id === listId; });
if (lists && lists.length) {
return new Promise<ISPView[]>((resolve) => {
resolve(lists[0].Views);
});
}
else {
return this.context.httpClient.get(this.context.pageContext.web.absoluteUrl + '/_api/web/lists(\'' + listId + '\')/views') // requesting views from SharePoint REST API
.then((response: Response) => { // httpClient.get method returns a response object where json method creates a Promise of getting result
return response.json();
}).then((response: ISPViews) => { // response is an ISPViews object
var views = response.value;
if (!this._lists || !this._lists.length)
this._lists = new Array<ISPListWithViews>();
this._lists.push({ Id: listId, Title: '', Views: views });
return views;
});
}
}
}

View File

@ -0,0 +1,25 @@
import {
IWebPartContext
} from '@microsoft/sp-webpart-base';
import { EnvironmentType } from '@microsoft/sp-client-base';
import { IDataHelper } from './DataHelperBase';
import { DataHelperMock } from './DataHelperMock';
import { DataHelperSP } from './DataHelperSP';
/**
* Factory object to create data helper based on current EnvironmentType
*/
export class DataHelpersFactory {
/**
* API to create data helper
* @context: web part context
*/
public static createDataHelper(context: IWebPartContext): IDataHelper {
if (context.environment.type === EnvironmentType.Local) {
return new DataHelperMock();
}
else {
return new DataHelperSP(context);
}
}
}

View File

@ -0,0 +1,29 @@
declare namespace SP {
export class ClientContext {
static get_current(): ClientContext;
load(obj: any, ...rest: string[]): void;
executeQueryAsyncCallback(success: (result: any) => void, error: (error: any) => void);
}
namespace Taxonomy {
export class TaxonomySession {
static getTaxonomySession(spContext: ClientRect): TaxonomySession;
get_termStores(): ITermStoreCollection;
}
export interface ITermStore {
get_groups(): ITermGroupCollection;
}
export interface ITermStoreCollection {
get_item(index: number): ITermStore;
}
export interface ITermGroup {
}
export interface ITermGroupCollection {
get_item(index: number): ITermGroup;
}
}
}