Added page contributors webpart (#272)

This commit is contained in:
Stéphane Magne (Infeeny) 2017-08-07 15:56:43 +02:00 committed by Vesa Juvonen
parent 3717fc284c
commit 59a2283d92
27 changed files with 636 additions and 0 deletions

View File

@ -0,0 +1,25 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
# change these settings to your own preference
indent_style = space
indent_size = 2
# we recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
[{package,bower}.json]
indent_style = space
indent_size = 2

View File

@ -0,0 +1 @@
* text=auto

View File

@ -0,0 +1,32 @@
# Logs
logs
*.log
npm-debug.log*
# Dependency directories
node_modules
# Build generated files
dist
lib
solution
temp
*.sppkg
# Coverage directory used by tools like istanbul
coverage
# OSX
.DS_Store
# Visual Studio files
.ntvs_analysis.dat
.vs
bin
obj
# Resx Generated Code
*.resx.ts
# Styles Generated Code
*.scss.ts

View File

@ -0,0 +1,14 @@
# Folders
.vscode
coverage
node_modules
sharepoint
src
temp
# Files
*.csproj
.git*
.yo-rc.json
gulpfile.js
tsconfig.json

View File

@ -0,0 +1,8 @@
{
"@microsoft/generator-sharepoint": {
"version": "1.1.1",
"libraryName": "react-pagecontributors",
"libraryId": "1c18830a-4c18-4b82-a571-77863b19c66d",
"environment": "spo"
}
}

View File

@ -0,0 +1,60 @@
# title of the sample
## Summary
Displays page contributors in reverse chronological order.
![Organisation Chart for the current user running in SharePoint](./assets/pagecontributors_inaction.png)
![Organisation Chart for the current user running in local Workbench](./assets/pagecontributors_mockup.png)
## Used SharePoint Framework Version
![drop](https://img.shields.io/badge/version-GA-green.svg)
## Applies to
* [SharePoint Framework](https:/dev.office.com/sharepoint)
* [Office 365 tenant](https://dev.office.com/sharepoint/docs/spfx/set-up-your-development-environment)
## Prerequisites
PnP-JS-Core
## Solution
Solution|Author(s)
--------|---------
PageContributors | Stéphane Magne ([@SPParse](https://twitter.com/SPParse))
## Version history
Version|Date|Comments
-------|----|--------
1.0|July 27, 2017|Initial release
## Disclaimer
**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.**
---
## Minimal Path to Awesome
- Clone this repository
- in the command line run:
- `npm install`
- `gulp serve`
## Features
Settings :
1. The maximum number of contributors to show
2. The size of their icon
3. A page URL to display contributors from an other page than the current one.
This Web Part illustrates the following concepts on top of the SharePoint Framework:
- Office UI Fabric
- React
- Pnp JS Core
<img src="https://telemetry.sharepointpnp.com/sp-dev-fx-webparts/samples/react-pagecontributors" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

View File

@ -0,0 +1,13 @@
{
"entries": [
{
"entry": "./lib/webparts/pageContributors/PageContributorsWebPart.js",
"manifest": "./src/webparts/pageContributors/PageContributorsWebPart.manifest.json",
"outputPath": "./dist/page-contributors.bundle.js"
}
],
"externals": {},
"localizedResources": {
"pageContributorsStrings": "webparts/pageContributors/loc/{locale}.js"
}
}

View File

@ -0,0 +1,3 @@
{
"deployCdnPath": "temp/deploy"
}

View File

@ -0,0 +1,6 @@
{
"workingDir": "./temp/deploy/",
"account": "<!-- STORAGE ACCOUNT NAME -->",
"container": "react-pagecontributors",
"accessKey": "<!-- ACCESS KEY -->"
}

View File

@ -0,0 +1,10 @@
{
"solution": {
"name": "react-pagecontributors-client-side-solution",
"id": "1c18830a-4c18-4b82-a571-77863b19c66d",
"version": "1.0.0.0"
},
"paths": {
"zippedPackage": "solution/react-pagecontributors.sppkg"
}
}

View File

@ -0,0 +1,9 @@
{
"port": 4321,
"initialPage": "https://localhost:5432/workbench",
"https": true,
"api": {
"port": 5432,
"entryPath": "node_modules/@microsoft/sp-webpart-workbench/lib/api/"
}
}

View File

@ -0,0 +1,45 @@
{
// Display errors as warnings
"displayAsWarning": true,
// The TSLint task may have been configured with several custom lint rules
// before this config file is read (for example lint rules from the tslint-microsoft-contrib
// project). If true, this flag will deactivate any of these rules.
"removeExistingRules": true,
// When true, the TSLint task is configured with some default TSLint "rules.":
"useDefaultConfigAsBase": false,
// Since removeExistingRules=true and useDefaultConfigAsBase=false, there will be no lint rules
// which are active, other than the list of rules below.
"lintConfig": {
// Opt-in to Lint rules which help to eliminate bugs in JavaScript
"rules": {
"class-name": false,
"export-name": false,
"forin": false,
"label-position": false,
"member-access": true,
"no-arg": false,
"no-console": false,
"no-construct": false,
"no-duplicate-case": true,
"no-duplicate-variable": true,
"no-eval": false,
"no-function-expression": true,
"no-internal-module": true,
"no-shadowed-variable": true,
"no-switch-case-fall-through": true,
"no-unnecessary-semicolons": true,
"no-unused-expression": true,
"no-unused-imports": true,
"no-use-before-declare": true,
"no-with-statement": true,
"semicolon": true,
"trailing-comma": false,
"typedef": false,
"typedef-whitespace": false,
"use-named-parameter": true,
"valid-typeof": true,
"variable-name": false,
"whitespace": false
}
}
}

View File

@ -0,0 +1,3 @@
{
"cdnBasePath": "<!-- PATH TO CDN -->"
}

View File

@ -0,0 +1,6 @@
'use strict';
const gulp = require('gulp');
const build = require('@microsoft/sp-build-web');
build.initialize(gulp);

View File

@ -0,0 +1,34 @@
{
"name": "react-pagecontributors",
"version": "0.0.1",
"private": true,
"engines": {
"node": ">=0.10.0"
},
"dependencies": {
"@microsoft/sp-core-library": "~1.1.0",
"@microsoft/sp-webpart-base": "~1.1.1",
"@types/react": "0.14.46",
"@types/react-addons-shallow-compare": "0.14.17",
"@types/react-addons-test-utils": "0.14.15",
"@types/react-addons-update": "0.14.14",
"@types/react-dom": "0.14.18",
"@types/webpack-env": ">=1.12.1 <1.14.0",
"react": "15.4.2",
"react-dom": "15.4.2",
"sp-pnp-js": "^2.0.7"
},
"devDependencies": {
"@microsoft/sp-build-web": "~1.1.0",
"@microsoft/sp-module-interfaces": "~1.1.0",
"@microsoft/sp-webpart-workbench": "~1.1.0",
"gulp": "~3.9.1",
"@types/chai": ">=3.4.34 <3.6.0",
"@types/mocha": ">=2.2.33 <2.6.0"
},
"scripts": {
"build": "gulp bundle",
"clean": "gulp clean",
"test": "gulp test"
}
}

View File

@ -0,0 +1,6 @@
import { PersonaSize } from "office-ui-fabric-react/lib/index";
export interface IPageContributorsWebPartProps {
personaSize: PersonaSize;
numberOfFaces: number;
pageUrl: string;
}

View File

@ -0,0 +1,22 @@
{
"$schema": "../../../node_modules/@microsoft/sp-module-interfaces/lib/manifestSchemas/jsonSchemas/clientSideComponentManifestSchema.json",
"id": "dfb5229a-c1c3-4e98-870b-c40dd6e4ec4b",
"alias": "PageContributorsWebPart",
"componentType": "WebPart",
"version": "1.0.0",
"manifestVersion": 2,
"safeWithCustomScriptDisabled": false,
"preconfiguredEntries": [{
"groupId": "dfb5229a-c1c3-4e98-870b-c40dd6e4ec4b",
"group": { "default": "Under Development" },
"title": { "default": "Page Contributors" },
"description": { "default": "Displays page contributors in reverse chronological order" },
"officeFabricIconFontName": "People",
"properties": {
"numberOfFaces": 5,
"personaSize": 3
}
}]
}

View File

@ -0,0 +1,79 @@
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneSlider,
PropertyPaneDropdown,
PropertyPaneTextField
} from '@microsoft/sp-webpart-base';
import * as strings from 'pageContributorsStrings';
import PageContributors from './components/PageContributors';
import { IPageContributorsWebPartProps } from './IPageContributorsWebPartProps';
import { PersonaSize } from "office-ui-fabric-react/lib/index";
import pnp from 'sp-pnp-js';
export default class PagecontributionWebPart extends BaseClientSideWebPart<IPageContributorsWebPartProps> {
public onInit(): Promise<void> {
return super.onInit().then(_ => {
pnp.setup({
spfxContext: this.context
});
});
}
public render(): void {
ReactDom.render(
React.createElement(PageContributors, {
personaSize: this.properties.personaSize,
numberOfFaces: this.properties.numberOfFaces,
pageUrl: this.properties.pageUrl
})
, this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.PropertyPaneBasicGroupName,
groupFields: [
PropertyPaneSlider('numberOfFaces', {
label: strings.PropertyPaneNbPersonasText,
min: 1,
max: 20
}),
PropertyPaneDropdown('personaSize', {
label: strings.PropertyPanePersonaSizeText,
options: [
{ key: PersonaSize.tiny, text: strings.PropertyPaneIconsSizeTiny },
{ key: PersonaSize.extraExtraSmall, text: strings.PropertyPaneIconsSizeEES },
{ key: PersonaSize.extraSmall, text: strings.PropertyPaneIconsSizeES },
{ key: PersonaSize.small, text: strings.PropertyPaneIconsSizeS },
{ key: PersonaSize.regular, text: strings.PropertyPaneIconsSizeR },
{ key: PersonaSize.large, text: strings.PropertyPaneIconsSizeL },
{ key: PersonaSize.extraLarge, text: strings.PropertyPaneIconsSizeEL },
],
selectedKey: this.properties.personaSize
}),
PropertyPaneTextField('pageUrl',{
label: strings.PropertyPanePageUrlText
})
]
}
]
}
]
};
}
}

View File

@ -0,0 +1,7 @@
import { PersonaSize } from "office-ui-fabric-react/lib";
export interface IPageContributorsProps {
personaSize: PersonaSize;
numberOfFaces: number;
pageUrl: string;
}

View File

@ -0,0 +1,188 @@
import * as React from 'react';
import * as strings from 'pageContributorsStrings';
import { PersonaSize, IFacepileProps, Facepile, OverflowButtonType, Spinner, MessageBar, MessageBarType } from 'office-ui-fabric-react/lib/index';
import {
Environment,
EnvironmentType
} from '@microsoft/sp-core-library';
import pnp from 'sp-pnp-js';
import { IPageContributorsProps } from "./IPageContributorsProps";
export class PageContributor {
public displayName: string;
public imageUrl: string;
public constructor(userField) {
this.displayName = userField.Title;
this.imageUrl = `/_layouts/15/userphoto.aspx?size=M&accountname=${userField.Email}`;
}
}
function RemoveDuplicates(contributors: PageContributor[]): PageContributor[] {
if (!contributors || !contributors.length)
return [] as PageContributor[];
let results: PageContributor[] = [];
contributors.forEach(contributor => {
let alreadyExists = false;
results.forEach(result => {
if (result.displayName == contributor.displayName) {
alreadyExists = true;
}
});
if (!alreadyExists) {
results.push(contributor);
}
});
return results;
}
export class PageContributorsMockData {
private static _items: PageContributor[] = [
{ displayName: 'Stéphane Magne', imageUrl: null },
{ displayName: 'Brangelina Pitt', imageUrl: null },
{ displayName: 'Pika Chu', imageUrl: null },
{ displayName: 'Rhino Faringite', imageUrl: null },
{ displayName: 'Nobert Lenoir', imageUrl: null }
];
public static get(): Promise<PageContributor[]> {
return new Promise<PageContributor[]>((resolve) => {
setTimeout(() => {
resolve(PageContributorsMockData._items);
}, 2000);
});
}
}
export class PageContributorsService {
public static getPageContributors(pageServerRelativeUrl: string): Promise<PageContributor[]> {
return new Promise<PageContributor[]>((resolve, reject) => {
pnp.sp.web.getFileByServerRelativeUrl(pageServerRelativeUrl)
.select('ModifiedBy')
.expand('ModifiedBy')
.get()
.then(file => {
pnp.sp.web.getFileByServerRelativeUrl(pageServerRelativeUrl)
.versions
.orderBy('Created')
.select('ID, Created, CreatedBy')
.expand('CreatedBy')
.top(100)
.get().then(versions => {
let history = versions.map((version) => {
return new PageContributor(version.CreatedBy);
});
history.unshift(new PageContributor(file.ModifiedBy));
history = RemoveDuplicates(history);
resolve(history);
});
})
.catch(error => reject(error));
});
}
}
export interface IPageContributorsState {
contributors: PageContributor[];
loading: boolean;
error: string;
}
function Loading(props){
if (!props.show){
return null;
}
return (
<div style={{ margin: '0 auto' }}><Spinner label={strings.Loading} /></div>
);
}
function ErrorMessage(props){
if (!props.message){
return null;
}
return (
<MessageBar messageBarType={MessageBarType.error}>{props.message}</MessageBar>
);
}
export default class PageContributors extends React.Component<IPageContributorsProps, IPageContributorsState> {
constructor(props, state) {
super(props);
this.state = {
contributors: [] as PageContributor[],
loading: true,
error: null
};
}
public componentDidMount(): void {
this._loadUsers();
}
public render(): React.ReactElement<null> {
let facepileProps: IFacepileProps = {
personaSize: this.props.personaSize,
maxDisplayablePersonas: this.props.numberOfFaces,
overflowButtonType: OverflowButtonType.descriptive,
overflowButtonProps: {
},
personas: this.state.contributors.map((contributor) => {
return { personaName: contributor.displayName, imageUrl: contributor.imageUrl };
})
};
const contributors: JSX.Element = this.state.contributors.length ? <Facepile {...facepileProps} /> : <div />;
return (
<div>
<Loading show={this.state.loading}/>
<ErrorMessage message={this.state.error}/>
{contributors}
</div>
);
}
public componentDidUpdate(prevProps: IPageContributorsProps, prevState: IPageContributorsState, prevContext: any): void {
if (this.props.pageUrl !== prevProps.pageUrl) {
this._loadUsers();
}
}
private _loadUsers(): void {
if (Environment.type === EnvironmentType.Local) {
PageContributorsMockData.get().then((response) => {
this._setContributors(response);
});
}
else if (Environment.type == EnvironmentType.SharePoint ||
Environment.type == EnvironmentType.ClassicSharePoint) {
PageContributorsService.getPageContributors(this.props.pageUrl || location.pathname).then(
(response) => {
this._setContributors(response);
},
error => this._showError(error)
);
}
}
private _setContributors(contributors: PageContributor[]) {
this.setState((prevState: IPageContributorsState, currentProps: IPageContributorsProps): IPageContributorsState => {
prevState.loading = false;
prevState.error = null;
prevState.contributors = contributors;
return prevState;
});
}
private _showError(error: any) {
this.setState((prevState: IPageContributorsState, currentProps: IPageContributorsProps): IPageContributorsState => {
prevState.loading = false;
prevState.error = `${error.status} ${error.message} : ${error.stack}`;
prevState.contributors = [];
return prevState;
});
}
}

View File

@ -0,0 +1,17 @@
define([], function() {
return {
"PropertyPaneDescription": "Displays page contributors in reverse chronological order",
"PropertyPaneNbPersonasText": "Maximum number of persons to display",
"PropertyPaneBasicGroupName": "General",
"PropertyPanePersonaSizeText": "Icons size",
"PropertyPaneIconsSizeTiny": "tiny",
"PropertyPaneIconsSizeEES": "extraExtraSmall",
"PropertyPaneIconsSizeES": "extraSmall",
"PropertyPaneIconsSizeS": "small",
"PropertyPaneIconsSizeR": "regular",
"PropertyPaneIconsSizeL": "large",
"PropertyPaneIconsSizeEL": "extraLarge",
"PropertyPanePageUrlText": "Page server relative URL (leave empty for current page)",
"Loading": "Loading..."
}
});

View File

@ -0,0 +1,20 @@
declare interface IPageContributorsStrings {
PropertyPaneDescription: string;
PropertyPaneNbPersonasText: string;
PropertyPaneBasicGroupName: string;
PropertyPanePersonaSizeText: string;
PropertyPaneIconsSizeTiny: string;
PropertyPaneIconsSizeEES: string;
PropertyPaneIconsSizeES: string;
PropertyPaneIconsSizeS: string;
PropertyPaneIconsSizeR: string;
PropertyPaneIconsSizeL: string;
PropertyPaneIconsSizeEL: string;
PropertyPanePageUrlText: string;
Loading: string;
}
declare module 'pageContributorsStrings' {
const strings: IPageContributorsStrings;
export = strings;
}

View File

@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es5",
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"jsx": "react",
"declaration": true,
"sourceMap": true,
"experimentalDecorators": true,
"types": [
"es6-promise",
"es6-collections",
"webpack-env"
]
}
}

View File

@ -0,0 +1,11 @@
// Type definitions for Microsoft ODSP projects
// Project: ODSP
/* Global definition for UNIT_TEST builds
Code that is wrapped inside an if(UNIT_TEST) {...}
block will not be included in the final bundle when the
--ship flag is specified */
declare const UNIT_TEST: boolean;
/* Global defintion for SPO builds */
declare const DATACENTER: boolean;

View File

@ -0,0 +1 @@
/// <reference path="@ms/odsp.d.ts" />