Skip to content

Commit

Permalink
pattern groups no longer need to be prefixed with two integers and a …
Browse files Browse the repository at this point in the history
…hyphen

closes pattern-lab#388
  • Loading branch information
bmuenzenmeyer committed Jul 20, 2016
1 parent b2242e5 commit c35e4ba
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 44 deletions.
8 changes: 4 additions & 4 deletions core/lib/object_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var extend = require('util')._extend;
var Pattern = function (relPath, data) {
// We expect relPath to be the path of the pattern template, relative to the
// root of the pattern tree. Parse out the path parts and save the useful ones.
var pathObj = path.parse(relPath);
this.relPath = relPath; // '00-atoms/00-global/00-colors.mustache'
var pathObj = path.parse(path.normalize(relPath));
this.relPath = path.normalize(relPath); // '00-atoms/00-global/00-colors.mustache'
this.fileName = pathObj.name; // '00-colors'
this.subdir = pathObj.dir; // '00-atoms/00-global'
this.fileExtension = pathObj.ext; // '.mustache'
Expand All @@ -31,10 +31,10 @@ var Pattern = function (relPath, data) {

// calculated path from the root of the public directory to the generated html
// file for this pattern
this.patternLink = this.name + '/' + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'
this.patternLink = this.name + path.sep + this.name + '.html'; // '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html'

// the top-level pattern group this pattern belongs to. 'atoms'
this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1);
this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, '');

// the sub-group this pattern belongs to.
this.patternSubGroup = path.basename(this.subdir).replace(/^\d*-/, ''); // 'global'
Expand Down
40 changes: 18 additions & 22 deletions core/lib/ui_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ var eol = require('os').EOL;

// PRIVATE FUNCTIONS

function addToPatternPaths(patternlab, patternTypeName, pattern) {
//this is messy, could use a refactor.
if (!patternlab.patternPaths[patternTypeName]) {
patternlab.patternPaths[patternTypeName] = {};
function addToPatternPaths(patternlab, pattern) {
if (!patternlab.patternPaths[pattern.patternGroup]) {
patternlab.patternPaths[pattern.patternGroup] = {};
}
patternlab.patternPaths[patternTypeName][pattern.patternBaseName] = pattern.subdir.replace(/\\/g, '/') + "/" + pattern.fileName.replace('~', '-');
patternlab.patternPaths[pattern.patternGroup][pattern.patternBaseName] = pattern.name;
}

//todo: refactor this as a method on the pattern object itself once we merge dev with pattern-engines branch
Expand Down Expand Up @@ -91,9 +90,6 @@ function buildNavigation(patternlab) {

var pattern = patternlab.patterns[i];

//todo: check if this is already available
var patternTypeName = pattern.name.replace(/\\/g, '-').split('-')[1];

//exclude any named defaultPattern from the navigation.
//this is meant to be a homepage that is not navigable
if (pattern.patternPartial === patternlab.config.defaultPattern) {
Expand All @@ -102,7 +98,7 @@ function buildNavigation(patternlab) {
}

//add to patternPaths before continuing
addToPatternPaths(patternlab, patternTypeName, pattern);
addToPatternPaths(patternlab, pattern);

continue;
}
Expand Down Expand Up @@ -134,17 +130,17 @@ function buildNavigation(patternlab) {
patternSubTypeItem.patternPartial = pattern.patternPartial;

//check if the patternType already exists
var patternTypeIndex = patternlab.patternTypeIndex.indexOf(patternTypeName);
var patternTypeIndex = patternlab.patternTypeIndex.indexOf(pattern.patternGroup);
if (patternTypeIndex === -1) {
//add the patternType
var patternType = new of.oPatternType(patternTypeName);
var patternType = new of.oPatternType(pattern.patternGroup);

//add patternPath and viewAllPath
patternlab.patternPaths[patternTypeName] = patternlab.patternPaths[patternTypeName] || {};
patternlab.viewAllPaths[patternTypeName] = {};
patternlab.patternPaths[pattern.patternGroup] = patternlab.patternPaths[pattern.patternGroup] || {};
patternlab.viewAllPaths[pattern.patternGroup] = {};

//test whether the pattern structure is flat or not - usually due to a template or page
flatPatternItem = patternSubTypeName === patternTypeName;
flatPatternItem = patternSubTypeName === pattern.patternGroup;

//assume the patternSubType does not exist.
patternSubType = new of.oPatternSubType(patternSubTypeName);
Expand All @@ -160,7 +156,7 @@ function buildNavigation(patternlab) {
patternType.patternItems.push(patternSubTypeItem);

//add to patternPaths
addToPatternPaths(patternlab, patternTypeName, pattern);
addToPatternPaths(patternlab, pattern);

} else {

Expand All @@ -170,21 +166,21 @@ function buildNavigation(patternlab) {
patternSubType.patternSubtypeItemsIndex.push(patternSubTypeItemName);

//add to patternPaths
addToPatternPaths(patternlab, patternTypeName, pattern);
addToPatternPaths(patternlab, pattern);

//add the view all PatternSubTypeItem
viewAllPatternSubTypeItem = new of.oPatternSubTypeItem("View All");
viewAllPatternSubTypeItem.patternPath = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length) + "/index.html";
viewAllPatternSubTypeItem.patternPartial = "viewall-" + pattern.patternGroup;

patternType.patternItems.push(viewAllPatternSubTypeItem);
patternlab.viewAllPaths[patternTypeName].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length);
patternlab.viewAllPaths[pattern.patternGroup].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length);

}

//add the patternType.
patternlab.patternTypes.push(patternType);
patternlab.patternTypeIndex.push(patternTypeName);
patternlab.patternTypeIndex.push(pattern.patternGroup);

//done

Expand All @@ -198,15 +194,15 @@ function buildNavigation(patternlab) {
}

//test whether the pattern structure is flat or not - usually due to a template or page
flatPatternItem = patternSubTypeName === patternTypeName;
flatPatternItem = patternSubTypeName === pattern.patternGroup;

//if it is flat - we should not add the pattern to patternPaths
if (flatPatternItem) {
//add the patternSubType to patternItems
patternType.patternItems.push(patternSubTypeItem);

//add to patternPaths
addToPatternPaths(patternlab, patternTypeName, pattern);
addToPatternPaths(patternlab, pattern);

} else {

Expand Down Expand Up @@ -242,11 +238,11 @@ function buildNavigation(patternlab) {
}

// just add to patternPaths
addToPatternPaths(patternlab, patternTypeName, pattern);
addToPatternPaths(patternlab, pattern);
}
}

patternlab.viewAllPaths[patternTypeName][pattern.patternSubGroup] = pattern.flatPatternPath;
patternlab.viewAllPaths[pattern.patternGroup][pattern.patternSubGroup] = pattern.flatPatternPath;
}
return patternTypeIndex;
}
Expand Down
1 change: 0 additions & 1 deletion test/lineage_hunter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ exports['lineage hunter '] = {
]
};

lineage_hunter.find_lineage(currentPattern, patternlab);
lineage_hunter.find_lineage(currentPattern, patternlab);

test.equals(currentPattern.lineageIndex.length, 1);
Expand Down
12 changes: 6 additions & 6 deletions test/list_item_hunter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,24 @@

'process_list_item_partials finds verbose partials and outputs repeated renders' : function(test){
var pattern1 = createFakeListPattern({
"template": "{{#listItems.one}}{{> 00-atoms/00-test/00-foo.mustache }}{{/listItems.one}}",
"extendedTemplate" : "{{#listItems.one}}{{> 00-atoms/00-test/00-foo.mustache }}{{/listItems.one}}",
"template": "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}",
"extendedTemplate" : "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}",
"key": "test-patternName1"
});

var pattern2 = createFakeListPattern({
"template": "{{#listItems.two}}{{> 00-atoms/00-test/00-bar.mustache }}{{/listItems.two}}",
"extendedTemplate" : "{{#listItems.two}}{{> 00-atoms/00-test/00-bar.mustache }}{{/listItems.two}}",
"template": "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}",
"extendedTemplate" : "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}",
"key": "test-patternName2"
});

var patternlab = createFakePatternLab({
"patterns": [
Pattern.create('00-atoms/00-test/00-foo.mustache', null, {
Pattern.create('00-test/00-foo.mustache', null, {
"template": "{{ title }}",
"extendedTemplate": "{{ title }}"
}),
Pattern.create('00-atoms/00-test/00-bar.mustache', null, {
Pattern.create('00-test/00-bar.mustache', null, {
"template": "{{ title }}",
"extendedTemplate": "{{ title }}"
})
Expand Down
23 changes: 18 additions & 5 deletions test/object_factory_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@

var of = require('../core/lib/object_factory');
var Pattern = require('../core/lib/object_factory').Pattern;
var path = require('path');

exports['Pattern initialization'] = {
'test Pattern initializes correctly' : function (test) {
var p = new Pattern('00-atoms/00-global/00-colors.mustache', { d: 123});
test.equals(p.relPath, '00-atoms/00-global/00-colors.mustache');
test.equals(p.relPath, '00-atoms' + path.sep + '00-global' + path.sep + '00-colors.mustache');
test.equals(p.name, '00-atoms-00-global-00-colors');
test.equals(p.subdir, '00-atoms/00-global');
test.equals(p.subdir, '00-atoms' + path.sep + '00-global');
test.equals(p.fileName, '00-colors');
test.equals(p.fileExtension, '.mustache');
test.equals(p.jsonFileData.d, 123);
test.equals(p.patternBaseName, 'colors');
test.equals(p.patternName, 'Colors');
test.equals(p.patternLink, '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html');
test.equals(p.patternLink, '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.html');
test.equals(p.patternGroup, 'atoms');
test.equals(p.patternSubGroup, 'global');
test.equals(p.flatPatternPath, '00-atoms-00-global');
Expand All @@ -31,15 +32,15 @@
},
'test Pattern with one-directory subdir works as expected' : function (test) {
var p = new Pattern('00-atoms/00-colors.mustache', { d: 123});
test.equals(p.relPath, '00-atoms/00-colors.mustache');
test.equals(p.relPath, '00-atoms' + path.sep + '00-colors.mustache');
test.equals(p.name, '00-atoms-00-colors');
test.equals(p.subdir, '00-atoms');
test.equals(p.fileName, '00-colors');
test.equals(p.fileExtension, '.mustache');
test.equals(p.jsonFileData.d, 123);
test.equals(p.patternBaseName, 'colors');
test.equals(p.patternName, 'Colors');
test.equals(p.patternLink, '00-atoms-00-colors/00-atoms-00-colors.html');
test.equals(p.patternLink, '00-atoms-00-colors' + path.sep + '00-atoms-00-colors.html');
test.equals(p.patternGroup, 'atoms');
test.equals(p.flatPatternPath, '00-atoms');
test.equals(p.patternPartial, 'atoms-colors');
Expand All @@ -50,6 +51,18 @@
test.equals(p.lineageRIndex.length, 0);
test.done();
},
'test Pattern with no numbers in pattern group works as expected' : function (test) {
var p = new Pattern('atoms/colors.mustache', { d: 123});
test.equals(p.relPath, 'atoms' + path.sep + 'colors.mustache');
test.equals(p.name, 'atoms-colors');
test.equals(p.subdir, 'atoms');
test.equals(p.fileName, 'colors');
test.equals(p.patternLink, 'atoms-colors' + path.sep + 'atoms-colors.html');
test.equals(p.patternGroup, 'atoms');
test.equals(p.flatPatternPath, 'atoms');
test.equals(p.patternPartial, 'atoms-colors');
test.done();
},
'test Pattern capitalizes patternDisplayName correctly' : function(test){
var p = new Pattern('00-atoms/00-global/00-colors-alt.mustache', { d: 123});
test.equals(p.patternBaseName, 'colors-alt');
Expand Down
12 changes: 6 additions & 6 deletions test/pattern_assembler_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

//act

pattern_assembler.process_pattern_recursive('00-test/04-group.mustache', pl, {});
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '04-group.mustache', pl, {});

//assert
var expectedValue = '<div class="test_group"> <span class="test_base test_1"> {{message}} </span> <span class="test_base test_2"> {{message}} </span> <span class="test_base test_3"> {{message}} </span> <span class="test_base test_4"> {{message}} </span> </div>';
Expand Down Expand Up @@ -135,7 +135,7 @@
pattern_assembler.addPattern(groupPattern, pl);

//act
pattern_assembler.process_pattern_recursive('00-test/10-multiple-classes-numeric.mustache', pl, {});
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '10-multiple-classes-numeric.mustache', pl, {});

//assert
var expectedValue = '<div class="test_group"> <span class="test_base foo1"> {{message}} </span> <span class="test_base foo1 foo2"> {{message}} </span> <span class="test_base foo1 foo2"> bar </span> </div>';
Expand Down Expand Up @@ -174,7 +174,7 @@
pattern_assembler.addPattern(mixedPattern, pl);

//act
pattern_assembler.process_pattern_recursive('00-test/06-mixed.mustache', pl, {});
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '06-mixed.mustache', pl, {});

//assert. here we expect {{styleModifier}} to be in the first group, since it was not replaced by anything. rendering with data will then remove this (correctly)
var expectedValue = '<div class="test_group"> <span class="test_base {{styleModifier}}"> {{message}} </span> <span class="test_base test_2"> {{message}} </span> <span class="test_base test_3"> {{message}} </span> <span class="test_base test_4"> {{message}} </span> </div>';
Expand Down Expand Up @@ -213,7 +213,7 @@
pattern_assembler.addPattern(bookendPattern, pl);

//act
pattern_assembler.process_pattern_recursive('00-test/09-bookend.mustache', pl, {});
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '09-bookend.mustache', pl, {});

//assert. here we expect {{styleModifier}} to be in the first and last group, since it was not replaced by anything. rendering with data will then remove this (correctly)
var expectedValue = '<div class="test_group"> <span class="test_base {{styleModifier}}"> {{message}} </span> <span class="test_base test_2"> {{message}} </span> <span class="test_base test_3"> {{message}} </span> <span class="test_base {{styleModifier}}"> {{message}} </span> </div>';
Expand Down Expand Up @@ -255,7 +255,7 @@
pattern_assembler.addPattern(mixedPattern, pl);

//act
pattern_assembler.process_pattern_recursive('00-test/07-mixed-params.mustache', pl, {});
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '07-mixed-params.mustache', pl, {});

//assert. here we expect {{styleModifier}} to be in the first span, since it was not replaced by anything. rendering with data will then remove this (correctly)
var expectedValue = '<div class="test_group"> <span class="test_base {{styleModifier}}"> {{message}} </span> <span class="test_base test_2"> 2 </span> <span class="test_base test_3"> 3 </span> <span class="test_base test_4"> 4 </span> </div>';
Expand Down Expand Up @@ -296,7 +296,7 @@
pattern_assembler.addPattern(bookendPattern, pl);

//act
pattern_assembler.process_pattern_recursive('00-test/08-bookend-params.mustache', pl, {});
pattern_assembler.process_pattern_recursive('00-test' + path.sep + '08-bookend-params.mustache', pl, {});

//assert. here we expect {{styleModifier}} to be in the first and last span, since it was not replaced by anything. rendering with data will then remove this (correctly)
var expectedValue = '<div class="test_group"> <span class="test_base {{styleModifier}}"> {{message}} </span> <span class="test_base test_2"> 2 </span> <span class="test_base test_3"> 3 </span> <span class="test_base {{styleModifier}}"> {{message}} </span> </div>';
Expand Down

0 comments on commit c35e4ba

Please sign in to comment.