UNPKG

8.73 kBMarkdownView Raw
1## Getting started
2
3#### Table of contents
4
5- [Quick start](#quick-start)
6- [Initializing](#initializing)
7 - [form.json](#formjson)
8 - [form.json types](#formjson-types)
9- [Advanced configuration](#advanced-configuration)
10 - [Sorts](#teamdirectorysorts)
11 - [Validators](#teamdirectoryvalidators)
12 - [Normalizers](#teamdirectorynormalizers)
13 - [Listing Template](#teamdirectorylistingtemplate)
14 - [Stats Template](#teamdirectorystatstemplate)
15- [Events](#events)
16
17### Quick start
18
19```html
20<div id='app'></div>
21<script src='../dist/team-directory.js'></script>
22
23<script>
24TeamDirectory(document.getElementById('app'), {
25 GitHubToken: 'TOKENHERE', // A users GitHub token
26 account: 'mapbox', // GitHub org or account name
27 repo: 'team-directory', // The repository team and form data is found in
28 team: 'team.json', // Team data filname
29 form: 'data/form.json' // Form data filename
30});
31</script>
32```
33
34FIll out the values above with your own credentials. An example configuration
35can be found [here](https://github.com/mapbox/team-directory/blob/master/index.html).
36
37### Initializing
38
39___`TeamDirectory(el, options)`___
40- `el` (String or HTMLElement) of the container element the application should populate.
41- `options` are as follows:
42
43| Option | Value | Required | Default value | Description |
44| --- | --- | --- | --- | --- |
45| GitHubToken | String | &#x2713; | | A users GitHub token |
46| account | String | &#x2713; | | GitHub organization or account name |
47| repo | String | &#x2713; | | The repository where team & form documents are located |
48| team | String | &#x2713; | | the path and filename in `repo` where team data is written out to |
49| form | String | &#x2713; | | the path and filename in `repo` where form data is read from |
50| branch | String | | | Specify a specific branch found in `repo` |
51| filterKeys | Array | | `['github']` | An array of string keys that must correspond to a key property found in the form data. If an array is passed the two values are concatenated together (i.e. `['github', ['fname', 'lname']]`) |
52
53#### form.json
54
55Team Directory is designed for you to provide your own form data to meet your
56own needs. An example of a [form.json can be found here](https://github.com/mapbox/team-directory/blob/master/data/form.json) but at it's most basic the structure looks
57like this:
58
59```json
60{
61 "Basic information": [{
62 "key": "github",
63 "label": "Github username",
64 "required": true
65 }, {
66 "key": "birthday",
67 "label": "Birthday",
68 "type": "date"
69 }]
70}
71```
72
73_A couple notes:_
74
75- __`Basic information`__ is the section name the form field belongs to. You can use any
76arbitrary name here or break form fields into any number of sections.
77- __GitHub__ is required. This is used in the verification process to insure no
78duplicate users is created.
79
80As you can see from the example json above, each form field is represented
81as an object with specific key/value pairings. There are a few as follows:
82
83
84| Option | Value | Required | Description |
85| --- | --- | --- | --- |
86| key | String | &#x2713; | A unique key name |
87| label | String | | Form label shown above the field element |
88| required | Boolean | | If a form field is required the form won't submit for the user until a value has been passed. |
89| fields | string | &#x2713; for some type attributes | Specific to checkboxes and radios, fields are an array of objects with `key` and `label` properties |
90| type | string | | If this value isnt provided, it defaults to 'text' See below for form types and their structures |
91
92#### form.json types
93
94##### `add`
95
96```json
97{
98 "key": "other-links",
99 "label": "Other links",
100 "type": "add"
101}
102```
103
104A unique form field for adding multiple entries.
105
106
107##### `textarea`
108
109```json
110{
111 "key": "adress",
112 "label": "Home address",
113 "type": "textarea"
114{
115```
116
117`textarea` input type.
118
119##### `checkbox`
120
121```json
122{
123 "key": "teams",
124 "label": "Teams (check all that apply)",
125 "required": true,
126 "type": "checkbox",
127 "fields": [{
128 "key": "business",
129 "label": "Business"
130 }, {
131 "key": "design",
132 "label": "Design"
133 }, {
134 "key": "engineering",
135 "label": "Engineering"
136 }, {
137 "key": "operations",
138 "label": "Operations"
139 }, {
140 "key": "support",
141 "label": "Support"
142 }]
143}
144```
145
146Checkbox input type.
147
148##### `radio`
149
150```json
151{
152 "key": "office",
153 "label": "Office",
154 "type": "radio",
155 "fields": [{
156 "key": "dc",
157 "label": "DC"
158 }, {
159 "key": "sf",
160 "label": "SF"
161 }, {
162 "key": "ayacucho",
163 "label": "Peru"
164 }, {
165 "key": "bengaluru",
166 "label": "India"
167 }]
168}
169```
170
171Radio input type
172
173##### `number`
174
175```json
176{
177 "key": "call",
178 "label": "Mobile number",
179 "type": "number"
180}
181```
182
183Number input type.
184
185##### `date`
186
187```json
188}
189 "key": "birthday",
190 "label": "Birthday",
191 "type": "date"
192}
193```
194
195Date input type.
196
197### Advanced configuration
198
199If you provide your own custom form data, you'll likely want to override
200existing functionality to suit your needs.
201
202#### `TeamDirectory.sorts`
203
204Provide your own custom sorting on the listings page. `sorts` should equal an
205array of objects with `key` & `sort` pairings. `Key` must must correspond to a
206key attribute in the form data and the `sort` function should return the sorted
207array when complete.
208
209```js
210var directions = TeamDirectory(document.getElementById('app'), options);
211
212directions.sorts = [{
213 key: 'date',
214 sort: function(team) {
215 return team.sort((a, b) => {
216 return new Date(b.birthday).getTime() - new Date(a.birthday).getTime();
217 });
218 }
219 }, {
220 key: 'name',
221 return team.sort((a, b) => {
222 return a.localeCompare(b);
223 });
224 }
225}];
226```
227
228#### `TeamDirectory.validators`
229
230Custom validation that's called before a team member is created or updated.
231The validators function is passed two arguments: `obj` The team member object &
232`callback` A function that's called in your code with either a string messsage
233describing a validation error found or `null` (no error found). Team member
234data will not be submitted until validation passes.
235
236```js
237var directions = TeamDirectory(document.getElementById('app'), options);
238
239directions.validators = function(obj, callback) {
240 if (obj.office === 'other' && !obj.city) {
241 return callback('If the office selected is other, please enter your city');
242 }
243
244 // No validation errors if it gets here
245 return callback(null);
246});
247```
248
249#### `TeamDirectory.normalizers`
250
251Format/normalize fields a user before its submitted. The normalizer function is
252passed two arguments: `obj` The team member object & `callback` A function
253that's called at the end of the function containing the new normalized/formatted
254user object. Team member data will not be submitted until this callback is called.
255
256```js
257var directions = TeamDirectory(document.getElementById('app'), options);
258
259directions.normalization = function(obj, callback) {
260 return callback(obj.map(function(data) {
261
262 // Remove any capitalization from an entered username.
263 data.username = data.username.toLowerCase();
264 return data;
265 });
266});
267```
268
269#### `TeamDirectory.listingTemplate`
270
271Create a custom listing template for team members. The listingTemplate is a
272function passed one argument: `obj` the current user in a list drawn out to
273the main page. The function must return [jsx template](https://facebook.github.io/jsx/).
274
275```js
276var directions = TeamDirectory(document.getElementById('app'), options);
277
278directions.listingTemplate = function(obj) {
279 var fullName = obj.fname + ' ' + obj.lname;
280
281 return (
282 <div>
283 <strong>{fullName}</strong>
284 <em>{obj.birthday}</em>
285 </div>
286 );
287});
288```
289
290#### `TeamDirectory.statsTemplate`
291
292Evaluate team user data and present a template of found statistics. The
293statsTemplate is passed one argument: `team` the team array of users. The
294function must return [jsx template](https://facebook.github.io/jsx/). If no
295statsTemplate is provided, the teamStats link and modal will not be present
296on the listing page.
297
298```js
299var directions = TeamDirectory(document.getElementById('app'), options);
300
301directions.statsTemplate = function(team) {
302 var length = team.length;
303 var phones = team.filter(function(member) {
304 return member.phone;
305 }).length;
306
307 return (
308 <div>
309 <h2>Team stats</h2>
310 <p>There are {length} total team members and {phones} have phones.
311 </div>
312 );
313});
314```
315
316### Events
317
318___`TeamDirectory.on(type, function)`___
319
320Clients can subscribe to events that happen in the application.
321
322```js
323var directions = TeamDirectory(document.getElementById('app'), options);
324
325// Get team data when it's available on the page
326directions.on('load', function(ev) {
327 console.log(ev.team);
328});
329```
330
331Available types are as follows:
332
333- `load`
334- `user.created`
335- `user.updated`
336- `user.removed`