Skip to content

Commit 4645d55

Browse files
Merge pull request #759 from pattern-lab/dev
Pattern Lab Node 3.0.0 Alpha 6
2 parents da38be4 + 1955516 commit 4645d55

File tree

71 files changed

+6377
-5493
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+6377
-5493
lines changed

core/index.js

Lines changed: 415 additions & 0 deletions
Large diffs are not rendered by default.

core/lib/addPattern.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
3+
const _ = require('lodash');
4+
5+
const logger = require('./log');
6+
7+
module.exports = function (pattern, patternlab) {
8+
//add the link to the global object
9+
if (!patternlab.data.link) {
10+
patternlab.data.link = {};
11+
}
12+
patternlab.data.link[pattern.patternPartial] = '/patterns/' + pattern.patternLink;
13+
14+
//only push to array if the array doesn't contain this pattern
15+
var isNew = true;
16+
for (var i = 0; i < patternlab.patterns.length; i++) {
17+
//so we need the identifier to be unique, which patterns[i].relPath is
18+
if (pattern.relPath === patternlab.patterns[i].relPath) {
19+
//if relPath already exists, overwrite that element
20+
patternlab.patterns[i] = pattern;
21+
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate || pattern.template;
22+
isNew = false;
23+
break;
24+
}
25+
}
26+
27+
// if the pattern is new, we must register it with various data structures!
28+
if (isNew) {
29+
30+
logger.debug(`found new pattern ${pattern.patternPartial}`);
31+
32+
// do global registration
33+
if (pattern.isPattern) {
34+
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate || pattern.template;
35+
36+
// do plugin-specific registration
37+
pattern.registerPartial();
38+
} else {
39+
patternlab.partials[pattern.patternPartial] = pattern.patternDesc;
40+
}
41+
42+
//patterns sorted by name so the patterntype and patternsubtype is adhered to for menu building
43+
patternlab.patterns.splice(_.sortedIndexBy(patternlab.patterns, pattern, 'name'), 0, pattern);
44+
patternlab.graph.add(pattern);
45+
}
46+
};

core/lib/asset_copy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const asset_copier = () => {
8989

9090
//watch for changes and copy
9191
assetWatcher.on('addDir', (p) => {
92-
const destination = path.resolve(basePath, dir.public + '/' + path.basename(p));
92+
const destination = path.resolve(basePath, dir.public);
9393
copyFile(p, destination, copyOptions);
9494
}).on('add', (p) => {
9595
const destination = path.resolve(basePath, dir.public + '/' + path.basename(p));

core/lib/buildFooter.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
3+
const jsonCopy = require('./json_copy');
4+
const logger = require('./log');
5+
const of = require('./object_factory');
6+
const Pattern = of.Pattern;
7+
8+
let render = require('./render'); //eslint-disable-line prefer-const
9+
10+
/**
11+
* Builds footer HTML from the general footer and user-defined footer
12+
* @param patternlab - global data store
13+
* @param patternPartial - the partial key to build this for, either viewall-patternPartial or a viewall-patternType-all
14+
* @returns A promise which resolves with the HTML
15+
*/
16+
module.exports = function (patternlab, patternPartial) {
17+
//first render the general footer
18+
return render(Pattern.createEmpty({extendedTemplate: patternlab.footer}), {
19+
patternData: JSON.stringify({
20+
patternPartial: patternPartial,
21+
}),
22+
cacheBuster: patternlab.cacheBuster
23+
}).then(footerPartial => {
24+
25+
let allFooterData;
26+
try {
27+
allFooterData = jsonCopy(patternlab.data, 'config.paths.source.data plus patterns data');
28+
} catch (err) {
29+
logger.warning('There was an error parsing JSON for patternlab.data');
30+
logger.warning(err);
31+
}
32+
allFooterData.patternLabFoot = footerPartial;
33+
34+
return render(patternlab.userFoot, allFooterData);
35+
}).catch(reason => {
36+
console.log(reason);
37+
logger.error('Error building buildFooterHTML');
38+
});
39+
};

core/lib/buildListItems.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
3+
const _ = require('lodash');
4+
5+
module.exports = function (container) {
6+
//combine all list items into one structure
7+
var list = [];
8+
for (var item in container.listitems) {
9+
if (container.listitems.hasOwnProperty(item)) {
10+
list.push(container.listitems[item]);
11+
}
12+
}
13+
container.listItemArray = _.shuffle(list);
14+
15+
for (var i = 1; i <= container.listItemArray.length; i++) {
16+
var tempItems = [];
17+
if (i === 1) {
18+
tempItems.push(container.listItemArray[0]);
19+
container.listitems['' + i ] = tempItems;
20+
} else {
21+
for (var c = 1; c <= i; c++) {
22+
tempItems.push(container.listItemArray[c - 1]);
23+
container.listitems['' + i ] = tempItems;
24+
}
25+
}
26+
}
27+
};

core/lib/decompose.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use strict";
2+
3+
const logger = require('./log');
4+
const lh = require('./lineage_hunter');
5+
const lih = require('./list_item_hunter');
6+
const addPattern = require('./addPattern');
7+
const expandPartials = require('./expandPartials');
8+
9+
const lineage_hunter = new lh();
10+
const list_item_hunter = new lih();
11+
12+
/**
13+
* A helper that unravels a pattern looking for partials or listitems to unravel.
14+
* The goal is really to convert pattern.template into pattern.extendedTemplate
15+
* @param pattern - the pattern to decompose
16+
* @param patternlab - global data store
17+
* @param ignoreLineage - whether or not to hunt for lineage for this pattern
18+
*/
19+
module.exports = function (pattern, patternlab, ignoreLineage) {
20+
21+
//set the extendedTemplate to operate on later if we find partials to replace
22+
if (!pattern.extendedTemplate) {
23+
pattern.extendedTemplate = pattern.template;
24+
}
25+
26+
//find any listItem blocks that within the pattern, even if there are no partials
27+
const listItemPromise = list_item_hunter.process_list_item_partials(pattern, patternlab);
28+
29+
const expandPartialPromise = expandPartials(pattern, patternlab);
30+
31+
let lineagePromise;
32+
33+
//find pattern lineage
34+
if (!ignoreLineage) {
35+
lineagePromise = Promise.resolve(lineage_hunter.find_lineage(pattern, patternlab));
36+
} else {
37+
lineagePromise = Promise.resolve();
38+
}
39+
40+
const addPromise = Promise.resolve(() => {
41+
//add to patternlab object so we can look these up later.
42+
addPattern(pattern, patternlab);
43+
});
44+
45+
return Promise.all([listItemPromise, expandPartialPromise, lineagePromise, addPromise])
46+
.catch(reason => {
47+
logger.error(reason);
48+
});
49+
};

core/lib/expandPartials.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use strict";
2+
3+
const logger = require('./log');
4+
const ph = require('./parameter_hunter');
5+
const smh = require('./style_modifier_hunter');
6+
const jsonCopy = require('./json_copy');
7+
const getPartial = require('./get');
8+
9+
const parameter_hunter = new ph();
10+
const style_modifier_hunter = new smh();
11+
12+
module.exports = function (currentPattern, patternlab) {
13+
14+
const processRecursive = require('./processRecursive');
15+
16+
//find how many partials there may be for the given pattern
17+
const foundPatternPartials = currentPattern.findPartials();
18+
19+
// expand any partials present in this pattern; that is, drill down into
20+
// the template and replace their calls in this template with rendered
21+
// results
22+
if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {
23+
24+
logger.debug(`found partials for ${currentPattern.patternPartial}`);
25+
26+
// determine if the template contains any pattern parameters. if so they
27+
// must be immediately consumed
28+
return parameter_hunter.find_parameters(currentPattern, patternlab).then(() => {
29+
30+
//do something with the regular old partials
31+
foundPatternPartials.forEach((foundPartial) => {
32+
33+
var partial = currentPattern.findPartial(foundPartial);
34+
var partialPattern = getPartial(partial, patternlab);
35+
36+
//recurse through nested partials to fill out this extended template.
37+
return processRecursive(partialPattern.relPath, patternlab).then(() => { //eslint-disable-line no-loop-func
38+
39+
//complete assembly of extended template
40+
//create a copy of the partial so as to not pollute it after the getPartial call.
41+
var cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partial}`);
42+
43+
//if partial has style modifier data, replace the styleModifier value
44+
if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) {
45+
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartial, patternlab);
46+
}
47+
48+
//this is what we came here for
49+
logger.debug(`within ${currentPattern.patternPartial}, replacing extendedTemplate partial ${foundPartial} with ${cleanPartialPattern.patternPartial}'s extendedTemplate`);
50+
51+
currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPartial, cleanPartialPattern.extendedTemplate);
52+
53+
// update the extendedTemplate in the partials object in case this
54+
// pattern is consumed later
55+
patternlab.partials[currentPattern.patternPartial] = currentPattern.extendedTemplate;
56+
57+
return Promise.resolve();
58+
}).catch(reason => {
59+
console.log(reason);
60+
logger.error(reason);
61+
});
62+
});
63+
64+
}).catch(reason => {
65+
console.log(reason);
66+
logger.error(reason);
67+
});
68+
}
69+
return Promise.resolve();
70+
};

core/lib/exportData.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
3+
const eol = require('os').EOL;
4+
const path = require('path');
5+
const _ = require('lodash');
6+
7+
const ae = require('./annotation_exporter');
8+
9+
let fs = require('fs-extra'); //eslint-disable-line prefer-const
10+
11+
/**
12+
* Write out our pattern information for use by the front end
13+
* @param patternlab - global data store
14+
*/
15+
module.exports = function (patternlab) {
16+
17+
const annotation_exporter = new ae(patternlab);
18+
19+
const paths = patternlab.config.paths;
20+
21+
//write out the data
22+
let output = '';
23+
24+
//config
25+
output += 'var config = ' + JSON.stringify(patternlab.config) + ';\n';
26+
27+
//ishControls
28+
output += 'var ishControls = {"ishControlsHide":' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;
29+
30+
//navItems
31+
output += 'var navItems = {"patternTypes": ' + JSON.stringify(patternlab.patternTypes) + ', "ishControlsHide": ' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;
32+
33+
//patternPaths
34+
output += 'var patternPaths = ' + JSON.stringify(patternlab.patternPaths) + ';' + eol;
35+
36+
//viewAllPaths
37+
output += 'var viewAllPaths = ' + JSON.stringify(patternlab.viewAllPaths) + ';' + eol;
38+
39+
//plugins
40+
output += 'var plugins = ' + JSON.stringify(patternlab.plugins || []) + ';' + eol;
41+
42+
//smaller config elements
43+
output += 'var defaultShowPatternInfo = ' + (patternlab.config.defaultShowPatternInfo ? patternlab.config.defaultShowPatternInfo : 'false') + ';' + eol;
44+
output += 'var defaultPattern = "' + (patternlab.config.defaultPattern ? patternlab.config.defaultPattern : 'all') + '";' + eol;
45+
46+
//annotations
47+
const annotationsJSON = annotation_exporter.gather();
48+
const annotations = 'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
49+
fs.outputFileSync(path.resolve(paths.public.annotations, 'annotations.js'), annotations);
50+
51+
//write all output to patternlab-data
52+
fs.outputFileSync(path.resolve(paths.public.data, 'patternlab-data.js'), output);
53+
return output;
54+
};

core/lib/get.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
3+
const logger = require('./log');
4+
5+
module.exports = function (partialName, patternlab) {
6+
//look for exact partial matches
7+
for (var i = 0; i < patternlab.patterns.length; i++) {
8+
if (patternlab.patterns[i].patternPartial === partialName) {
9+
return patternlab.patterns[i];
10+
}
11+
}
12+
13+
//else look by verbose syntax
14+
for (var i = 0; i < patternlab.patterns.length; i++) {
15+
switch (partialName) {
16+
case patternlab.patterns[i].relPath:
17+
return patternlab.patterns[i];
18+
case patternlab.patterns[i].verbosePartial:
19+
return patternlab.patterns[i];
20+
}
21+
}
22+
23+
//return the fuzzy match if all else fails
24+
for (var i = 0; i < patternlab.patterns.length; i++) {
25+
var partialParts = partialName.split('-'),
26+
partialType = partialParts[0],
27+
partialNameEnd = partialParts.slice(1).join('-');
28+
29+
if (patternlab.patterns[i].patternPartial.split('-')[0] === partialType && patternlab.patterns[i].patternPartial.indexOf(partialNameEnd) > -1) {
30+
return patternlab.patterns[i];
31+
}
32+
}
33+
logger.warning('Could not find pattern referenced with partial syntax ' + partialName + '. This can occur when a pattern was renamed, moved, or no longer exists but it still referenced within a different template or within data as a link.');
34+
return undefined;
35+
};

core/lib/lineage_hunter.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
'use strict';
22
const extend = require("util")._extend;
3+
const getPartial = require('./get');
34
const logger = require('./log');
45

56
const lineage_hunter = function () {
67

78
function findlineage(pattern, patternlab) {
89

9-
const pa = require('./pattern_assembler');
10-
const pattern_assembler = new pa();
11-
1210
// As we are adding edges from pattern to ancestor patterns, ensure it is known to the graph
1311
patternlab.graph.add(pattern);
1412

15-
1613
//find the {{> template-name }} within patterns
1714
const matches = pattern.findPartials();
1815
if (matches !== null) {
1916
matches.forEach(function (match) {
2017
//get the ancestorPattern
21-
const ancestorPattern = pattern_assembler.getPartial(pattern.findPartial(match), patternlab);
18+
const ancestorPattern = getPartial(pattern.findPartial(match), patternlab);
2219

2320
if (ancestorPattern && pattern.lineageIndex.indexOf(ancestorPattern.patternPartial) === -1) {
2421
//add it since it didnt exist
@@ -99,8 +96,6 @@ const lineage_hunter = function () {
9996

10097
//find all lineage - patterns being consumed by this one
10198
for (let h = 0; h < lineage.length; h++) {
102-
// Not needed, the graph already knows the concrete pattern
103-
// let lineagePattern = pattern_assembler.getPartial(lineageIndex[h], patternlab);
10499
setPatternState('fromFuture', lineage[h], pattern, patternlab.graph);
105100
}
106101
}

0 commit comments

Comments
 (0)