Skip to content

Commit b7e2c98

Browse files
committed
listItems blocks are only parsed when they contain a partial themselves. now they work for markup, global listitems, local listitems, mustache, and partials
fixes #177
1 parent bc2460e commit b7e2c98

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

builder/list_item_hunter.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
var loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
3636
var end = liMatch.replace('#', '/');
3737
var patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim();
38-
3938
//build arrays that repeat the block, however large we need to
4039
var repeatedBlockTemplate = [];
4140
var repeatedBlockHtml = '';
@@ -45,7 +44,7 @@
4544

4645
//check for a local listitems.json file
4746
var listData = JSON.parse(JSON.stringify(patternlab.listitems));
48-
listData = pattern_assembler.merge_data(listData, pattern.patternSpecificListJson);
47+
listData = pattern_assembler.merge_data(listData, pattern.listitems);
4948

5049
//iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
5150
for(var i = 0; i < repeatedBlockTemplate.length; i++){
@@ -59,7 +58,7 @@
5958
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
6059

6160
var allData = pattern_assembler.merge_data(globalData, localData);
62-
allData = pattern_assembler.merge_data(allData, itemData[i]);
61+
allData = pattern_assembler.merge_data(allData, itemData != undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
6362
allData.link = extend({}, patternlab.data.link);
6463

6564
//check for partials within the repeated block

builder/pattern_assembler.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
}
4242

4343
function findListItems(pattern){
44-
var matches = pattern.template.match(/({{#( )?)(listItems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g);
44+
var matches = pattern.template.match(/({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g);
4545
return matches;
4646
}
4747

@@ -134,7 +134,8 @@
134134
//look for a listitems.json file for this template
135135
try {
136136
var listJsonFileName = patternlab.config.patterns.source + currentPattern.subdir + '/' + currentPattern.fileName + ".listitems.json";
137-
currentPattern.patternSpecificListJson = fs.readJSONSync(listJsonFileName.substring(2));
137+
currentPattern.listitems = fs.readJSONSync(listJsonFileName.substring(2));
138+
buildListItems(currentPattern);
138139
if(patternlab.config.debug){
139140
console.log('found pattern-specific listitems.json for ' + currentPattern.key);
140141
}
@@ -198,7 +199,7 @@
198199
console.log('found partials for ' + currentPattern.key);
199200
}
200201

201-
//find any listItem partials
202+
//find any listItem blocks
202203
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
203204

204205
//determine if the template contains any pattern parameters. if so they must be immediately consumed
@@ -234,6 +235,9 @@
234235

235236
}
236237

238+
} else{
239+
//find any listItem blocks that within the pattern, even if there are no partials
240+
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
237241
}
238242

239243
//find pattern lineage
@@ -293,25 +297,25 @@
293297
return obj2;
294298
}
295299

296-
function buildListItems(patternlab){
300+
function buildListItems(container){
297301
//combine all list items into one structure
298302
var list = [];
299-
for (var item in patternlab.listitems) {
300-
if( patternlab.listitems.hasOwnProperty(item)) {
301-
list.push(patternlab.listitems[item]);
303+
for (var item in container.listitems) {
304+
if( container.listitems.hasOwnProperty(item)) {
305+
list.push(container.listitems[item]);
302306
}
303307
}
304-
patternlab.listItemArray = shuffle(list);
308+
container.listItemArray = shuffle(list);
305309

306-
for(var i = 1; i <= patternlab.listItemArray.length; i++){
310+
for(var i = 1; i <= container.listItemArray.length; i++){
307311
var tempItems = [];
308312
if( i === 1){
309-
tempItems.push(patternlab.listItemArray[0]);
310-
patternlab.listitems['' + i ] = tempItems;
313+
tempItems.push(container.listItemArray[0]);
314+
container.listitems['' + i ] = tempItems;
311315
} else{
312316
for(var c = 1; c <= i; c++){
313-
tempItems.push(patternlab.listItemArray[c - 1]);
314-
patternlab.listitems['' + i ] = tempItems;
317+
tempItems.push(container.listItemArray[c - 1]);
318+
container.listitems['' + i ] = tempItems;
315319
}
316320
}
317321
}

source/_data/listitems.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -779,4 +779,4 @@
779779
},
780780
"seconds": "56"
781781
}
782-
}
782+
}

test/list_item_hunter_tests.js

+46-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,51 @@
4141

4242
//act
4343
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
44-
44+
45+
//assert
46+
test.equals(currentPattern.extendedTemplate, "FooBar" );
47+
48+
test.done();
49+
},
50+
51+
'process_list_item_partials listitems with lowercase name' : function(test){
52+
//arrange
53+
//setup current pattern from what we would have during execution
54+
var currentPattern = {
55+
"template": "{{#listitems.two}}{{ title }}{{/listitems.two}}",
56+
"extendedTemplate" : "{{#listitems.two}}{{ title }}{{/listitems.two}}",
57+
"key": "test-patternName",
58+
"jsonFileData" : {}
59+
};
60+
61+
var patternlab = {
62+
"listitems": {
63+
"1": [
64+
{
65+
"title": "Foo"
66+
}
67+
],
68+
"2": [
69+
{
70+
"title": "Foo"
71+
},
72+
{
73+
"title": "Bar"
74+
}
75+
]
76+
},
77+
"data": {
78+
"link": {},
79+
"partials": []
80+
},
81+
"config": {"debug": false}
82+
};
83+
84+
var list_item_hunter = new lih();
85+
86+
//act
87+
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
88+
4589
//assert
4690
test.equals(currentPattern.extendedTemplate, "FooBar" );
4791

@@ -93,7 +137,7 @@
93137

94138
//act
95139
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
96-
140+
97141
//assert
98142
test.equals(currentPattern.extendedTemplate, "FooBar" );
99143

0 commit comments

Comments
 (0)