Skip to content

Commit d9d075a

Browse files
committed
jsdoc comments for ui_builder
1 parent 866eeaa commit d9d075a

File tree

1 file changed

+137
-35
lines changed

1 file changed

+137
-35
lines changed

core/lib/ui_builder.js

Lines changed: 137 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ var of = require('./object_factory');
77
var Pattern = of.Pattern;
88
var pa = require('./pattern_assembler');
99
var pattern_assembler = new pa();
10+
var plutils = require('./utilities');
1011
var eol = require('os').EOL;
1112
var _ = require('lodash');
1213

1314
var ui_builder = function () {
1415

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+
*/
1522
function addToPatternPaths(patternlab, pattern) {
1623
if (!patternlab.patternPaths) {
1724
patternlab.patternPaths = {};
@@ -21,11 +28,17 @@ var ui_builder = function () {
2128
patternlab.patternPaths[pattern.patternGroup] = {};
2229
}
2330

31+
//only add real patterns
2432
if (pattern.isPattern && !pattern.isDocPattern) {
2533
patternlab.patternPaths[pattern.patternGroup][pattern.patternBaseName] = pattern.name;
2634
}
2735
}
2836

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+
*/
2942
function addToViewAllPaths(patternlab, pattern) {
3043
if (!patternlab.viewAllPaths) {
3144
patternlab.viewAllPaths = {};
@@ -39,13 +52,21 @@ var ui_builder = function () {
3952
patternlab.viewAllPaths[pattern.patternGroup][pattern.patternSubGroup] = {};
4053
}
4154

55+
//note these retain any number prefixes if present, because these paths match the filesystem
4256
patternlab.viewAllPaths[pattern.patternGroup][pattern.patternSubGroup] = pattern.patternType + '-' + pattern.patternSubType;
4357

58+
//add all if it does not exist yet
4459
if (!patternlab.viewAllPaths[pattern.patternGroup].all) {
45-
patternlab.viewAllPaths[pattern.patternGroup].all = pattern.patternType ;
60+
patternlab.viewAllPaths[pattern.patternGroup].all = pattern.patternType;
4661
}
4762
}
4863

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+
*/
4970
function writeFile(filePath, data, callback) {
5071
if (callback) {
5172
fs.outputFile(filePath, data, callback);
@@ -54,10 +75,12 @@ var ui_builder = function () {
5475
}
5576
}
5677

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+
*/
6184
function isPatternExcluded(pattern, patternlab) {
6285
var styleGuideExcludes = patternlab.config.styleGuideExcludes;
6386
var isOmitted;
@@ -94,19 +117,23 @@ var ui_builder = function () {
94117
return isOmitted;
95118
}
96119

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+
*/
101127
function injectDocumentationBlock(pattern, patternlab, isSubtypePattern) {
128+
//first see if pattern_assembler processed one already
102129
var docPattern = patternlab.subtypePatterns[pattern.patternGroup + (isSubtypePattern ? '-' + pattern.patternSubGroup : '')];
103-
104130
if (docPattern) {
105131
docPattern.isDocPattern = true;
106132
return docPattern;
107133
}
108134

109-
var docPattern = new Pattern.createEmpty(
135+
//if not, create one now
136+
docPattern = new Pattern.createEmpty(
110137
{
111138
name: pattern.flatPatternPath,
112139
patternName: isSubtypePattern ? pattern.patternSubGroup : pattern.patternGroup,
@@ -123,8 +150,13 @@ var ui_builder = function () {
123150
return docPattern;
124151
}
125152

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+
*/
126159
function addPatternType(patternlab, pattern) {
127-
128160
patternlab.patternTypes.push(
129161
{
130162
patternTypeLC: pattern.patternGroup.toLowerCase(),
@@ -136,37 +168,48 @@ var ui_builder = function () {
136168
);
137169
}
138170

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+
*/
139177
function getPatternType(patternlab, pattern) {
140-
141178
var patternType = _.find(patternlab.patternTypes, ['patternType', pattern.patternType]);
142179

143180
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.");
145182
process.exit(1);
146183
}
184+
147185
return patternType;
148186
}
149187

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+
*/
150194
function getPatternSubType(patternlab, pattern) {
151195
var patternType = getPatternType(patternlab, pattern);
152-
153-
if (!patternType) {
154-
console.log('something went wrong looking for patternType');
155-
process.exit(1);
156-
}
157-
158196
var patternSubType = _.find(patternType.patternTypeItems, ['patternSubtype', pattern.patternSubType]);
159197

160198
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.");
162200
process.exit(1);
163201
}
164202

165203
return patternSubType;
166204
}
167205

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+
*/
168212
function addPatternSubType(patternlab, pattern) {
169-
170213
var patternType = getPatternType(patternlab, pattern);
171214

172215
patternType.patternTypeItems.push(
@@ -180,6 +223,12 @@ var ui_builder = function () {
180223
);
181224
}
182225

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+
*/
183232
function createPatternSubTypeItem(pattern) {
184233
return {
185234
patternPartial: pattern.patternPartial,
@@ -190,6 +239,13 @@ var ui_builder = function () {
190239
}
191240
}
192241

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+
*/
193249
function addPatternSubTypeItem(patternlab, pattern, createViewAllVariant) {
194250
var patternSubType = getPatternSubType(patternlab, pattern);
195251
if (createViewAllVariant) {
@@ -209,10 +265,15 @@ var ui_builder = function () {
209265
}
210266
}
211267

268+
/**
269+
* Registers flat patterns to the appropriate type
270+
* @param patternlab - global data store
271+
* @param pattern - the pattern to add
272+
*/
212273
function addPatternItem(patternlab, pattern) {
213274
var patternType = getPatternType(patternlab, pattern);
214275
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.");
216277
process.exit(1);
217278
}
218279

@@ -232,6 +293,12 @@ var ui_builder = function () {
232293
// return [];
233294
// }
234295

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+
*/
235302
function sortPatterns(patternsArray) {
236303
return patternsArray.sort(function (a, b) {
237304

@@ -241,16 +308,15 @@ var ui_builder = function () {
241308
if (a.name < b.name) {
242309
return -1;
243310
}
244-
245-
// a must be equal to b
246311
return 0;
247312
});
248313
}
249314

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+
*/
254320
function groupPatterns(patternlab) {
255321
var groupedPatterns = {
256322
patternGroups: {}
@@ -262,12 +328,14 @@ var ui_builder = function () {
262328

263329
_.forEach(sortPatterns(patternlab.patterns), function (pattern) {
264330

331+
//ignore patterns we can omit from rendering directly
265332
pattern.omitFromStyleguide = isPatternExcluded(pattern, patternlab);
266333
if (pattern.omitFromStyleguide) { return; }
267334

268335
if (!groupedPatterns.patternGroups[pattern.patternGroup]) {
269-
pattern.isSubtypePattern = false;
336+
270337
groupedPatterns.patternGroups[pattern.patternGroup] = {};
338+
pattern.isSubtypePattern = false;
271339
addPatternType(patternlab, pattern);
272340

273341
//todo: Pattern Type View All and Documentation
@@ -289,6 +357,7 @@ var ui_builder = function () {
289357
addPatternSubTypeItem(patternlab, pattern, true);
290358

291359
}
360+
292361
groupedPatterns.patternGroups[pattern.patternGroup][pattern.patternSubGroup][pattern.patternBaseName] = pattern;
293362

294363
addToPatternPaths(patternlab, pattern);
@@ -297,24 +366,42 @@ var ui_builder = function () {
297366
addPatternItem(patternlab, pattern);
298367
addToPatternPaths(patternlab, pattern);
299368
}
369+
300370
});
371+
301372
return groupedPatterns;
302373
}
303374

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+
*/
304381
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
306383
var footerPartial = pattern_assembler.renderPattern(patternlab.footer, {
307384
patternData: JSON.stringify({
308385
patternPartial: patternPartial,
309386
}),
310387
cacheBuster: patternlab.cacheBuster
311388
});
389+
390+
//then add it to the user footer
312391
var footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, {
313392
patternLabFoot : footerPartial
314393
});
315394
return footerHTML;
316395
}
317396

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+
*/
318405
function buildViewAllHTML(patternlab, patterns, patternPartial) {
319406
var viewAllHTML = pattern_assembler.renderPattern(patternlab.viewAll,
320407
{
@@ -328,6 +415,13 @@ var ui_builder = function () {
328415
return viewAllHTML;
329416
}
330417

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+
*/
331425
function buildViewAllPages(mainPageHeadHtml, patternlab, styleguidePatterns) {
332426
var paths = patternlab.config.paths;
333427
var patterns = [];
@@ -343,6 +437,7 @@ var ui_builder = function () {
343437

344438
var patternPartial = patternType + '-' + patternSubtype;
345439

440+
//do not create a viewall page for flat patterns
346441
if (patternType === patternSubtype) {
347442
writeViewAllFile = false;
348443
return false;
@@ -367,6 +462,7 @@ var ui_builder = function () {
367462
});
368463

369464

465+
//do not create a viewall page for flat patterns
370466
if (!writeViewAllFile || !p) {
371467
return false;
372468
}
@@ -389,7 +485,10 @@ var ui_builder = function () {
389485
}
390486

391487

392-
488+
/**
489+
* Write out our pattern information for use by the front end
490+
* @param patternlab - global data store
491+
*/
393492
function exportData(patternlab) {
394493
var annotation_exporter = new ae(patternlab);
395494
var paths = patternlab.config.paths;
@@ -428,6 +527,10 @@ var ui_builder = function () {
428527
writeFile(path.resolve(paths.public.annotations, 'annotations.js'), annotations);
429528
}
430529

530+
/**
531+
* The main entry point for ui_builder
532+
* @param patternlab - global data store
533+
*/
431534
function buildFrontend(patternlab) {
432535

433536
var paths = patternlab.config.paths;
@@ -454,13 +557,12 @@ var ui_builder = function () {
454557
});
455558

456559
//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);
459561

460562
//build the main styleguide page
461563
var styleguideHtml = pattern_assembler.renderPattern(patternlab.viewAll,
462564
{
463-
partials: patterns
565+
partials: allPatterns
464566
}, {
465567
patternSection: patternlab.patternSection,
466568
patternSectionSubtype: patternlab.patternSectionSubType

0 commit comments

Comments
 (0)