@@ -7,11 +7,18 @@ var of = require('./object_factory');
7
7
var Pattern = of . Pattern ;
8
8
var pa = require ( './pattern_assembler' ) ;
9
9
var pattern_assembler = new pa ( ) ;
10
+ var plutils = require ( './utilities' ) ;
10
11
var eol = require ( 'os' ) . EOL ;
11
12
var _ = require ( 'lodash' ) ;
12
13
13
14
var ui_builder = function ( ) {
14
15
16
+ /**
17
+ * Registers the pattern to the patternPaths object for the appropriate patternGroup and basename
18
+ * patternGroup + patternBaseName are what comprise the patternPartial (atoms-colors)
19
+ * @param patternlab - global data store
20
+ * @param pattern - the pattern to add
21
+ */
15
22
function addToPatternPaths ( patternlab , pattern ) {
16
23
if ( ! patternlab . patternPaths ) {
17
24
patternlab . patternPaths = { } ;
@@ -21,11 +28,17 @@ var ui_builder = function () {
21
28
patternlab . patternPaths [ pattern . patternGroup ] = { } ;
22
29
}
23
30
31
+ //only add real patterns
24
32
if ( pattern . isPattern && ! pattern . isDocPattern ) {
25
33
patternlab . patternPaths [ pattern . patternGroup ] [ pattern . patternBaseName ] = pattern . name ;
26
34
}
27
35
}
28
36
37
+ /**
38
+ * Registers the pattern with the viewAllPaths object for the appropriate patternGroup and patternSubGroup
39
+ * @param patternlab - global data store
40
+ * @param pattern - the pattern to add
41
+ */
29
42
function addToViewAllPaths ( patternlab , pattern ) {
30
43
if ( ! patternlab . viewAllPaths ) {
31
44
patternlab . viewAllPaths = { } ;
@@ -39,13 +52,21 @@ var ui_builder = function () {
39
52
patternlab . viewAllPaths [ pattern . patternGroup ] [ pattern . patternSubGroup ] = { } ;
40
53
}
41
54
55
+ //note these retain any number prefixes if present, because these paths match the filesystem
42
56
patternlab . viewAllPaths [ pattern . patternGroup ] [ pattern . patternSubGroup ] = pattern . patternType + '-' + pattern . patternSubType ;
43
57
58
+ //add all if it does not exist yet
44
59
if ( ! patternlab . viewAllPaths [ pattern . patternGroup ] . all ) {
45
- patternlab . viewAllPaths [ pattern . patternGroup ] . all = pattern . patternType ;
60
+ patternlab . viewAllPaths [ pattern . patternGroup ] . all = pattern . patternType ;
46
61
}
47
62
}
48
63
64
+ /**
65
+ * Writes a file to disk, with an optional callback
66
+ * @param filePath - the path to write to with filename
67
+ * @param data - the file contents
68
+ * @param callback - an optional callback
69
+ */
49
70
function writeFile ( filePath , data , callback ) {
50
71
if ( callback ) {
51
72
fs . outputFile ( filePath , data , callback ) ;
@@ -54,10 +75,12 @@ var ui_builder = function () {
54
75
}
55
76
}
56
77
57
- /*
58
- * isPatternExcluded
59
- * returns whether or not the pattern should be excluded from direct rendering or navigation on the front end
60
- */
78
+ /**
79
+ * Returns whether or not the pattern should be excluded from direct rendering or navigation on the front end
80
+ * @param pattern - the pattern to test for inclusion/exclusion
81
+ * @param patternlab - global data store
82
+ * @returns boolean - whether or not the pattern is excluded
83
+ */
61
84
function isPatternExcluded ( pattern , patternlab ) {
62
85
var styleGuideExcludes = patternlab . config . styleGuideExcludes ;
63
86
var isOmitted ;
@@ -94,19 +117,23 @@ var ui_builder = function () {
94
117
return isOmitted ;
95
118
}
96
119
97
- /*
98
- * injectDocumentationBlock
99
- * take the given pattern, fina and construct the view-all pattern block for the group
100
- */
120
+ /**
121
+ * For the given pattern, find or construct the view-all pattern block for the group
122
+ * @param pattern - the pattern to derive our documentation pattern from
123
+ * @param patternlab - global data store
124
+ * @param isSubtypePattern - whether or not this is a subtypePattern or a typePattern (typePatterns not supported yet)
125
+ * @returns the found or created pattern object
126
+ */
101
127
function injectDocumentationBlock ( pattern , patternlab , isSubtypePattern ) {
128
+ //first see if pattern_assembler processed one already
102
129
var docPattern = patternlab . subtypePatterns [ pattern . patternGroup + ( isSubtypePattern ? '-' + pattern . patternSubGroup : '' ) ] ;
103
-
104
130
if ( docPattern ) {
105
131
docPattern . isDocPattern = true ;
106
132
return docPattern ;
107
133
}
108
134
109
- var docPattern = new Pattern . createEmpty (
135
+ //if not, create one now
136
+ docPattern = new Pattern . createEmpty (
110
137
{
111
138
name : pattern . flatPatternPath ,
112
139
patternName : isSubtypePattern ? pattern . patternSubGroup : pattern . patternGroup ,
@@ -123,8 +150,13 @@ var ui_builder = function () {
123
150
return docPattern ;
124
151
}
125
152
153
+ /**
154
+ * Registers flat patterns with the patternTypes object
155
+ * This is a new menu group like atoms
156
+ * @param patternlab - global data store
157
+ * @param pattern - the pattern to register
158
+ */
126
159
function addPatternType ( patternlab , pattern ) {
127
-
128
160
patternlab . patternTypes . push (
129
161
{
130
162
patternTypeLC : pattern . patternGroup . toLowerCase ( ) ,
@@ -136,37 +168,48 @@ var ui_builder = function () {
136
168
) ;
137
169
}
138
170
171
+ /**
172
+ * Return the patternType object for the given pattern. Exits application if not found.
173
+ * @param patternlab - global data store
174
+ * @param pattern - the pattern to derive the pattern Type from
175
+ * @returns the found pattern type object
176
+ */
139
177
function getPatternType ( patternlab , pattern ) {
140
-
141
178
var patternType = _ . find ( patternlab . patternTypes , [ 'patternType' , pattern . patternType ] ) ;
142
179
143
180
if ( ! patternType ) {
144
- console . log ( 'something went wrong looking for patternType' ) ;
181
+ plutils . logRed ( "Could not find patternType" , pattern . patternType , "This is a critical error." ) ;
145
182
process . exit ( 1 ) ;
146
183
}
184
+
147
185
return patternType ;
148
186
}
149
187
188
+ /**
189
+ * Return the patternSubType object for the given pattern. Exits application if not found.
190
+ * @param patternlab - global data store
191
+ * @param pattern - the pattern to derive the pattern subType from
192
+ * @returns the found patternSubType object
193
+ */
150
194
function getPatternSubType ( patternlab , pattern ) {
151
195
var patternType = getPatternType ( patternlab , pattern ) ;
152
-
153
- if ( ! patternType ) {
154
- console . log ( 'something went wrong looking for patternType' ) ;
155
- process . exit ( 1 ) ;
156
- }
157
-
158
196
var patternSubType = _ . find ( patternType . patternTypeItems , [ 'patternSubtype' , pattern . patternSubType ] ) ;
159
197
160
198
if ( ! patternSubType ) {
161
- console . log ( 'something went wrong looking for patternSubType' , pattern . patternType , '-' , pattern . patternSubType ) ;
199
+ plutils . logRed ( "Could not find patternType" , pattern . patternType + '-' + pattern . patternType , "This is a critical error." ) ;
162
200
process . exit ( 1 ) ;
163
201
}
164
202
165
203
return patternSubType ;
166
204
}
167
205
206
+ /**
207
+ * Registers the pattern with the appropriate patternType.patternTypeItems object
208
+ * This is a new menu group like atoms/global
209
+ * @param patternlab - global data store
210
+ * @param pattern - the pattern to register
211
+ */
168
212
function addPatternSubType ( patternlab , pattern ) {
169
-
170
213
var patternType = getPatternType ( patternlab , pattern ) ;
171
214
172
215
patternType . patternTypeItems . push (
@@ -180,6 +223,12 @@ var ui_builder = function () {
180
223
) ;
181
224
}
182
225
226
+ /**
227
+ * Creates a patternSubTypeItem object from a pattern
228
+ * This is a menu item you click on
229
+ * @param pattern - the pattern to derive the subtypeitem from
230
+ * @returns {{patternPartial: string, patternName: (*|string), patternState: string, patternSrcPath: string, patternPath: string} }
231
+ */
183
232
function createPatternSubTypeItem ( pattern ) {
184
233
return {
185
234
patternPartial : pattern . patternPartial ,
@@ -190,6 +239,13 @@ var ui_builder = function () {
190
239
}
191
240
}
192
241
242
+ /**
243
+ * Registers the pattern with the appropriate patternType.patternSubType.patternSubtypeItems array
244
+ * These are the actual menu items you click on
245
+ * @param patternlab - global data store
246
+ * @param pattern - the pattern to derive the subtypeitem from
247
+ * @param createViewAllVariant - whether or not to create the special view all item
248
+ */
193
249
function addPatternSubTypeItem ( patternlab , pattern , createViewAllVariant ) {
194
250
var patternSubType = getPatternSubType ( patternlab , pattern ) ;
195
251
if ( createViewAllVariant ) {
@@ -209,10 +265,15 @@ var ui_builder = function () {
209
265
}
210
266
}
211
267
268
+ /**
269
+ * Registers flat patterns to the appropriate type
270
+ * @param patternlab - global data store
271
+ * @param pattern - the pattern to add
272
+ */
212
273
function addPatternItem ( patternlab , pattern ) {
213
274
var patternType = getPatternType ( patternlab , pattern ) ;
214
275
if ( ! patternType ) {
215
- console . log ( 'something went wrong looking for patternType' , pattern . patternType ) ;
276
+ plutils . logRed ( "Could not find patternType" , pattern . patternType , "This is a critical error." ) ;
216
277
process . exit ( 1 ) ;
217
278
}
218
279
@@ -232,6 +293,12 @@ var ui_builder = function () {
232
293
// return [];
233
294
// }
234
295
296
+ /**
297
+ * Sorts patterns based on name.
298
+ * Will be expanded to use explicit order in the near future
299
+ * @param patternsArray - patterns to sort
300
+ * @returns sorted patterns
301
+ */
235
302
function sortPatterns ( patternsArray ) {
236
303
return patternsArray . sort ( function ( a , b ) {
237
304
@@ -241,16 +308,15 @@ var ui_builder = function () {
241
308
if ( a . name < b . name ) {
242
309
return - 1 ;
243
310
}
244
-
245
- // a must be equal to b
246
311
return 0 ;
247
312
} ) ;
248
313
}
249
314
250
- /*
251
- * groupPatterns
252
- * returns an object representing how the front end styleguide and navigation is structured
253
- */
315
+ /**
316
+ * Returns an object representing how the front end styleguide and navigation is structured
317
+ * @param patternlab - global data store
318
+ * @returns ptterns grouped by type -> subtype like atoms -> global -> pattern, pattern, pattern
319
+ */
254
320
function groupPatterns ( patternlab ) {
255
321
var groupedPatterns = {
256
322
patternGroups : { }
@@ -262,12 +328,14 @@ var ui_builder = function () {
262
328
263
329
_ . forEach ( sortPatterns ( patternlab . patterns ) , function ( pattern ) {
264
330
331
+ //ignore patterns we can omit from rendering directly
265
332
pattern . omitFromStyleguide = isPatternExcluded ( pattern , patternlab ) ;
266
333
if ( pattern . omitFromStyleguide ) { return ; }
267
334
268
335
if ( ! groupedPatterns . patternGroups [ pattern . patternGroup ] ) {
269
- pattern . isSubtypePattern = false ;
336
+
270
337
groupedPatterns . patternGroups [ pattern . patternGroup ] = { } ;
338
+ pattern . isSubtypePattern = false ;
271
339
addPatternType ( patternlab , pattern ) ;
272
340
273
341
//todo: Pattern Type View All and Documentation
@@ -289,6 +357,7 @@ var ui_builder = function () {
289
357
addPatternSubTypeItem ( patternlab , pattern , true ) ;
290
358
291
359
}
360
+
292
361
groupedPatterns . patternGroups [ pattern . patternGroup ] [ pattern . patternSubGroup ] [ pattern . patternBaseName ] = pattern ;
293
362
294
363
addToPatternPaths ( patternlab , pattern ) ;
@@ -297,24 +366,42 @@ var ui_builder = function () {
297
366
addPatternItem ( patternlab , pattern ) ;
298
367
addToPatternPaths ( patternlab , pattern ) ;
299
368
}
369
+
300
370
} ) ;
371
+
301
372
return groupedPatterns ;
302
373
}
303
374
375
+ /**
376
+ * Builds footer HTML from the general footer and user-defined footer
377
+ * @param patternlab - global data store
378
+ * @param patternPartial - the partial key to build this for //todo test for relevancy
379
+ * @returns HTML
380
+ */
304
381
function buildFooterHTML ( patternlab , patternPartial ) {
305
- //set the pattern-specific footer by compiling the general-footer with data, and then adding it to the meta footer
382
+ //first render the general footer
306
383
var footerPartial = pattern_assembler . renderPattern ( patternlab . footer , {
307
384
patternData : JSON . stringify ( {
308
385
patternPartial : patternPartial ,
309
386
} ) ,
310
387
cacheBuster : patternlab . cacheBuster
311
388
} ) ;
389
+
390
+ //then add it to the user footer
312
391
var footerHTML = pattern_assembler . renderPattern ( patternlab . userFoot , {
313
392
patternLabFoot : footerPartial
314
393
} ) ;
315
394
return footerHTML ;
316
395
}
317
396
397
+ /**
398
+ * Takes a set of patterns and builds a viewall HTML page for them
399
+ * Used by the type and subtype viewall sets
400
+ * @param patternlab - global data store
401
+ * @param patterns - the set of patterns to build the viewall page for
402
+ * @param patternPartial - a key used to identify the viewall pager
403
+ * @returns HTML
404
+ */
318
405
function buildViewAllHTML ( patternlab , patterns , patternPartial ) {
319
406
var viewAllHTML = pattern_assembler . renderPattern ( patternlab . viewAll ,
320
407
{
@@ -328,6 +415,13 @@ var ui_builder = function () {
328
415
return viewAllHTML ;
329
416
}
330
417
418
+ /**
419
+ * Constructs viewall pages for each set of grouped patterns
420
+ * @param mainPageHeadHtml - the already built main page HTML
421
+ * @param patternlab - global data store
422
+ * @param styleguidePatterns - the grouped set of patterns
423
+ * @returns every built pattern and set of viewall patterns, so the styleguide can use it
424
+ */
331
425
function buildViewAllPages ( mainPageHeadHtml , patternlab , styleguidePatterns ) {
332
426
var paths = patternlab . config . paths ;
333
427
var patterns = [ ] ;
@@ -343,6 +437,7 @@ var ui_builder = function () {
343
437
344
438
var patternPartial = patternType + '-' + patternSubtype ;
345
439
440
+ //do not create a viewall page for flat patterns
346
441
if ( patternType === patternSubtype ) {
347
442
writeViewAllFile = false ;
348
443
return false ;
@@ -367,6 +462,7 @@ var ui_builder = function () {
367
462
} ) ;
368
463
369
464
465
+ //do not create a viewall page for flat patterns
370
466
if ( ! writeViewAllFile || ! p ) {
371
467
return false ;
372
468
}
@@ -389,7 +485,10 @@ var ui_builder = function () {
389
485
}
390
486
391
487
392
-
488
+ /**
489
+ * Write out our pattern information for use by the front end
490
+ * @param patternlab - global data store
491
+ */
393
492
function exportData ( patternlab ) {
394
493
var annotation_exporter = new ae ( patternlab ) ;
395
494
var paths = patternlab . config . paths ;
@@ -428,6 +527,10 @@ var ui_builder = function () {
428
527
writeFile ( path . resolve ( paths . public . annotations , 'annotations.js' ) , annotations ) ;
429
528
}
430
529
530
+ /**
531
+ * The main entry point for ui_builder
532
+ * @param patternlab - global data store
533
+ */
431
534
function buildFrontend ( patternlab ) {
432
535
433
536
var paths = patternlab . config . paths ;
@@ -454,13 +557,12 @@ var ui_builder = function () {
454
557
} ) ;
455
558
456
559
//build the viewall pages
457
- var patterns = buildViewAllPages ( headerHTML , patternlab , styleguidePatterns ) ;
458
- writeFile ( './all.json' , JSON . stringify ( patterns ) ) ;
560
+ var allPatterns = buildViewAllPages ( headerHTML , patternlab , styleguidePatterns ) ;
459
561
460
562
//build the main styleguide page
461
563
var styleguideHtml = pattern_assembler . renderPattern ( patternlab . viewAll ,
462
564
{
463
- partials : patterns
565
+ partials : allPatterns
464
566
} , {
465
567
patternSection : patternlab . patternSection ,
466
568
patternSectionSubtype : patternlab . patternSectionSubType
0 commit comments