Skip to content

Commit 6f5ede8

Browse files
committed
initial list item support
closes #92
1 parent 97b3e25 commit 6f5ede8

File tree

7 files changed

+161
-23
lines changed

7 files changed

+161
-23
lines changed

Gruntfile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ module.exports = function(grunt) {
4747
pseudopattern_hunter: {
4848
src: './builder/pseudopattern_hunter.js',
4949
dest: './builder/pseudopattern_hunter.js'
50+
},
51+
list_item_hunter: {
52+
src: './builder/list_item_hunter.js',
53+
dest: './builder/list_item_hunter.js'
5054
}
5155
},
5256
copy: {

builder/list_item_hunter.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* patternlab-node - v0.10.1 - 2015
3+
*
4+
* Brian Muenzenmeyer, and the web community.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
8+
*
9+
*/
10+
11+
(function () {
12+
"use strict";
13+
14+
var list_item_hunter = function(){
15+
16+
var extend = require('util')._extend,
17+
pa = require('./pattern_assembler'),
18+
mustache = require('mustache'),
19+
pattern_assembler = new pa(),
20+
items = [ 'zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty'];
21+
22+
function processListItemPartials(pattern, patternlab){
23+
//find any listitem blocks
24+
var matches = pattern_assembler.find_list_items(pattern, patternlab);
25+
if(matches !== null){
26+
matches.forEach(function(liMatch, index, matches){
27+
28+
if(patternlab.config.debug){
29+
console.log('found listItem of size ' + liMatch + ' inside ' + pattern.key);
30+
}
31+
32+
//find the boundaries of the block
33+
var loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
34+
35+
//find the boundaries of the block
36+
var end = liMatch.replace('#', '/');
37+
var patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim();
38+
39+
//build arrays that repeat the block, however large we need to
40+
var repeatedBlockTemplate = [];
41+
var repeatedBlockHtml = '';
42+
for(var i = 0; i < items.indexOf(loopNumberString); i++){
43+
repeatedBlockTemplate.push(patternBlock);
44+
}
45+
46+
//iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
47+
for(var i = 0; i < repeatedBlockTemplate.length; i++){
48+
49+
var thisBlockTemplate = repeatedBlockTemplate[i];
50+
var thisBlockHTML = "";
51+
52+
//combine listItem data with pattern data with global data
53+
var itemData = patternlab.listitems['' + items.indexOf(loopNumberString)]; //this is a propety
54+
var globalData = JSON.parse(JSON.stringify(patternlab.data));
55+
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
56+
57+
var allData = pattern_assembler.merge_data(globalData, localData);
58+
allData = pattern_assembler.merge_data(allData, itemData[i]);
59+
allData.link = extend({}, patternlab.data.link);
60+
61+
//check for partials within the repeated block
62+
var foundPartials = pattern_assembler.find_pattern_partials({ 'template' : thisBlockTemplate });
63+
64+
if(foundPartials.length > 0){
65+
66+
for(var j = 0; j < foundPartials.length; j++){
67+
//add the lineage once for the parent pattern
68+
//TODO
69+
70+
//get the partial
71+
var partialName = foundPartials[j].match(/([a-z-]+)/ig)[0];
72+
var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab);
73+
74+
//replace its reference within the block with the extended template
75+
thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], partialPattern.extendedTemplate);
76+
77+
}
78+
79+
//render with data
80+
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
81+
82+
} else{
83+
//just render with mergedData
84+
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
85+
}
86+
87+
//add the rendered HTML to our string
88+
repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML;
89+
}
90+
91+
//replace the block with our generated HTML
92+
var repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
93+
pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml);
94+
95+
});
96+
}
97+
}
98+
99+
return {
100+
process_list_item_partials: function(pattern, patternlab){
101+
processListItemPartials(pattern, patternlab);
102+
}
103+
};
104+
105+
};
106+
107+
module.exports = list_item_hunter;
108+
109+
}());

builder/object_factory.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
this.fileName = filename.substring(0, filename.indexOf('.'));
1616
this.subdir = subdir;
1717
this.name = subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName; //this is the unique name with the subDir
18-
this.data = data || null;
19-
this.jsonFileData = {};
18+
this.jsonFileData = data || {};
2019
this.patternName = this.fileName.substring(this.fileName.indexOf('-') + 1); //this is the display name for the ui
2120
this.patternLink = this.name + '/' + this.name + '.html';
2221
this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1);

builder/parameter_hunter.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
//find the {{> template-name(*) }} within patterns
2424
var matches = pattern.template.match(/{{>([ ]+)?([A-Za-z0-9-]+)(\()(.+)(\))([ ]+)?}}/g);
2525
if(matches !== null){
26+
//compile this partial immeadiately, essentially consuming it.
2627
matches.forEach(function(pMatch, index, matches){
2728
//find the partial's name
28-
var partialName = pMatch.match(/([a-z-]+)/ig)[0]
29+
var partialName = pMatch.match(/([a-z-]+)/ig)[0];
2930

3031
if(patternlab.config.debug){
3132
console.log('found patternParameters for ' + partialName);
@@ -39,20 +40,17 @@
3940
//do no evil. there is no good way to do this that I can think of without using a split, which then makes commas and colons special characters and unusable within the pattern params
4041
var paramData = eval(paramString);
4142

42-
//compile this partial immeadiately, essentially consuming it.
4343
var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab);
44-
var existingData = pattern.data ? JSON.parse(JSON.stringify(pattern.data)) : JSON.parse(JSON.stringify(patternlab.data));
44+
var globalData = JSON.parse(JSON.stringify(patternlab.data));
45+
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
4546

46-
//merge paramData with any other data that exists.
47-
for (var prop in paramData) {
48-
if (existingData.hasOwnProperty(prop)) {
49-
existingData[prop] = paramData[prop];
50-
}
51-
}
47+
var allData = pattern_assembler.merge_data(globalData, localData);
48+
allData = pattern_assembler.merge_data(allData, paramData);
5249

5350
//extend pattern data links into link for pattern link shortcuts to work. we do this locally and globally
54-
existingData.link = extend({}, patternlab.data.link);
55-
var renderedPartial = pattern_assembler.renderPattern(partialPattern.extendedTemplate, existingData, patternlab.partials);
51+
allData.link = extend({}, patternlab.data.link);
52+
53+
var renderedPartial = pattern_assembler.renderPattern(partialPattern.extendedTemplate, allData, patternlab.partials);
5654

5755
//remove the parameter from the partial and replace it with the rendered partial + paramData
5856
pattern.extendedTemplate = pattern.extendedTemplate.replace(pMatch, renderedPartial);

builder/pattern_assembler.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,27 @@
1313

1414
var pattern_assembler = function(){
1515

16+
function isObjectEmpty(obj) {
17+
for(var prop in obj) {
18+
if(obj.hasOwnProperty(prop))
19+
return false;
20+
}
21+
22+
return true;
23+
}
24+
25+
1626
//find and return any {{> template-name }} within pattern
1727
function findPartials(pattern){
1828
var matches = pattern.template.match(/{{>([ ])?([A-Za-z0-9-]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g);
1929
return matches;
2030
}
2131

32+
function findListItems(pattern){
33+
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);
34+
return matches;
35+
}
36+
2237
function setState(pattern, patternlab){
2338
if(patternlab.config.patternStates[pattern.patternName]){
2439
pattern.patternState = patternlab.config.patternStates[pattern.patternName];
@@ -86,10 +101,12 @@
86101
lh = require('./lineage_hunter'),
87102
ph = require('./parameter_hunter'),
88103
pph = require('./pseudopattern_hunter'),
104+
lih = require('./list_item_hunter'),
89105
path = require('path');
90106

91107
var parameter_hunter = new ph(),
92108
lineage_hunter = new lh(),
109+
list_item_hunter = new lih(),
93110
pseudopattern_hunter = new pph();
94111

95112
currentPattern.extendedTemplate = currentPattern.template;
@@ -102,6 +119,10 @@
102119
if(patternlab.config.debug){
103120
console.log('found partials for ' + currentPattern.key);
104121
}
122+
123+
//find any listItem partials
124+
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
125+
105126
//determine if the template contains any pattern parameters. if so they must be immediately consumed
106127
parameter_hunter.find_parameters(currentPattern, patternlab);
107128

@@ -166,15 +187,17 @@
166187
}
167188
patternlab.listItemArray = shuffle(list);
168189

169-
for(var i = 1; i <= 12; i++){
170-
var top = i;
171-
var c = 1;
190+
for(var i = 1; i <= patternlab.listItemArray.length; i++){
172191
var tempItems = [];
173-
while(c <= top){
174-
tempItems.push(patternlab.listItemArray[c]);
175-
c++;
192+
if( i === 1){
193+
tempItems.push(patternlab.listItemArray[0]);
194+
patternlab.listitems['' + i ] = tempItems;
195+
} else{
196+
for(var c = 1; c <= i; c++){
197+
tempItems.push(patternlab.listItemArray[c - 1]);
198+
patternlab.listitems['' + i ] = tempItems;
199+
}
176200
}
177-
patternlab.listitems['' + i ] = tempItems;
178201
}
179202
}
180203

@@ -188,6 +211,9 @@
188211
find_pattern_partials: function(pattern){
189212
return findPartials(pattern);
190213
},
214+
find_list_items: function(pattern){
215+
return findListItems(pattern)
216+
},
191217
setPatternState: function(pattern, patternlab){
192218
setState(pattern, patternlab);
193219
},
@@ -211,6 +237,9 @@
211237
},
212238
combine_listItems: function(patternlab){
213239
buildListItems(patternlab);
240+
},
241+
is_object_empty: function(obj){
242+
return isObjectEmpty(obj);
214243
}
215244
};
216245

builder/patternlab.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ var patternlab_engine = function () {
9494
//render the pattern, but first consolidate any data we may have
9595
var allData = JSON.parse(JSON.stringify(patternlab.data));
9696
allData = pattern_assembler.merge_data(allData, pattern.jsonFileData);
97-
allData = pattern_assembler.merge_data(allData, pattern.data);
9897

9998
pattern.patternPartial = pattern_assembler.renderPattern(pattern.extendedTemplate, allData);
10099

source/_patterns/02-organisms/02-comments/00-comment-thread.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<h2 class="section-title">59 Comments</h2>
44
{{> molecules-comment-form }}
55
<ul class="comment-list">
6-
{{#listItems.twelve}}
6+
{{#listItems.five}}
77
{{> molecules-single-comment }}
8-
{{/listItems.twelve}}
8+
{{/listItems.five}}
99
</ul>
1010
</div>
1111
{{> molecules-pagination }}

0 commit comments

Comments
 (0)