Skip to content

Commit f7f164d

Browse files
author
Brian Muenzenmeyer
committed
Merge pull request #175 from pattern-lab/stylemodifiers
Style Modifier Support
2 parents 016e9ce + b5423e0 commit f7f164d

21 files changed

+443
-197
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
THIS CHANGELOG IS AN ATTEMPT TO DOCUMENT CHANGES TO THIS PROJECT.
22

3+
PL-node-v0.14.0
4+
- ADD: Support for style modifiers
5+
36
PL-node-v0.13.1
47
- FIX: Allow verbose partials for list items
58
- THX: Thanks @e2tha-e

Gruntfile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ module.exports = function(grunt) {
5151
list_item_hunter: {
5252
src: './builder/list_item_hunter.js',
5353
dest: './builder/list_item_hunter.js'
54+
},
55+
style_modifier_hunter: {
56+
src: './builder/style_modifier_hunter.js',
57+
dest: './builder/style_modifier_hunter.js'
5458
}
5559
},
5660
copy: {

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,32 @@ Pattern parameters **do not** currently support the following:
179179

180180
You can read the full documentation on pattern parameters here: [Using Pattern Parameters](http://patternlab.io/docs/pattern-parameters.html)
181181

182+
##### Pattern Style Modifiers
183+
Style Modifiers allow you to create a base pattern that you can easily modify by adding a class name to the pattern partial. Read more about them [here](http://patternlab.io/docs/pattern-stylemodifier.html), including support with pattern parameters. Below is the gist.
184+
185+
The basic syntax is this:
186+
187+
```
188+
{{> atoms-message:error }}
189+
```
190+
191+
This works by using a reserved mustache variable of sorts called {{ styleModifier }} applied to the atoms-message mustache file itself:
192+
193+
```
194+
<div class="message {{ styleModifier }}">{{ message }}</div>
195+
```
196+
197+
Once rendered, it looks like this:
198+
199+
```
200+
<div>
201+
<div class="message error"></div>
202+
</div>
203+
```
204+
205+
206+
207+
182208
##### Pseudo-Patterns
183209
Pseudo-patterns are meant to give developers the ability to build multiple and unique **rendered** patterns off of one base pattern and its mark-up while giving them control over the data that is injected into the base pattern. This feature is especially useful when developing template- and page-style patterns.
184210

builder/lineage_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.13.0 - 2015
2+
* patternlab-node - v0.14.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/list_item_hunter.js

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
/*
2-
* patternlab-node - v0.13.0 - 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-
*/
1+
/*
2+
* patternlab-node - v0.14.0 - 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+
*/
1010

1111
(function () {
1212
"use strict";
@@ -15,81 +15,88 @@
1515

1616
var extend = require('util')._extend,
1717
pa = require('./pattern_assembler'),
18+
smh = require('./style_modifier_hunter'),
1819
mustache = require('mustache'),
1920
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+
style_modifier_hunter = new smh(),
22+
items = [ 'zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty'];
2123

2224
function processListItemPartials(pattern, patternlab){
23-
//find any listitem blocks
24-
var matches = pattern_assembler.find_list_items(pattern, patternlab);
25+
//find any listitem blocks
26+
var matches = pattern_assembler.find_list_items(pattern, patternlab);
2527
if(matches !== null){
2628
matches.forEach(function(liMatch, index, matches){
2729

28-
if(patternlab.config.debug){
29-
console.log('found listItem of size ' + liMatch + ' inside ' + pattern.key);
30-
}
30+
if(patternlab.config.debug){
31+
console.log('found listItem of size ' + liMatch + ' inside ' + pattern.key);
32+
}
3133

32-
//find the boundaries of the block
33-
var loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
34-
var end = liMatch.replace('#', '/');
35-
var patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim();
34+
//find the boundaries of the block
35+
var loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
36+
var end = liMatch.replace('#', '/');
37+
var patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim();
3638

37-
//build arrays that repeat the block, however large we need to
38-
var repeatedBlockTemplate = [];
39-
var repeatedBlockHtml = '';
40-
for(var i = 0; i < items.indexOf(loopNumberString); i++){
41-
repeatedBlockTemplate.push(patternBlock);
42-
}
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+
}
4345

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

48-
//iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
49-
for(var i = 0; i < repeatedBlockTemplate.length; i++){
50+
//iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
51+
for(var i = 0; i < repeatedBlockTemplate.length; i++){
5052

51-
var thisBlockTemplate = repeatedBlockTemplate[i];
52-
var thisBlockHTML = "";
53+
var thisBlockTemplate = repeatedBlockTemplate[i];
54+
var thisBlockHTML = "";
5355

54-
//combine listItem data with pattern data with global data
55-
var itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2"
56-
var globalData = JSON.parse(JSON.stringify(patternlab.data));
57-
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
56+
//combine listItem data with pattern data with global data
57+
var itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2"
58+
var globalData = JSON.parse(JSON.stringify(patternlab.data));
59+
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
5860

59-
var allData = pattern_assembler.merge_data(globalData, localData);
60-
allData = pattern_assembler.merge_data(allData, itemData[i]);
61-
allData.link = extend({}, patternlab.data.link);
61+
var allData = pattern_assembler.merge_data(globalData, localData);
62+
allData = pattern_assembler.merge_data(allData, itemData[i]);
63+
allData.link = extend({}, patternlab.data.link);
6264

63-
//check for partials within the repeated block
64-
var foundPartials = pattern_assembler.find_pattern_partials({ 'template' : thisBlockTemplate });
65+
//check for partials within the repeated block
66+
var foundPartials = pattern_assembler.find_pattern_partials({ 'template' : thisBlockTemplate });
6567

66-
if(foundPartials && foundPartials.length > 0){
68+
if(foundPartials && foundPartials.length > 0){
6769

68-
for(var j = 0; j < foundPartials.length; j++){
70+
for(var j = 0; j < foundPartials.length; j++){
6971

70-
//get the partial
71-
var partialName = foundPartials[j].match(/([\w\-\.\/~]+)/g)[0];
72-
var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab);
72+
//get the partial
73+
var partialName = foundPartials[j].match(/([\w\-\.\/~]+)/g)[0];
74+
var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab);
7375

74-
//replace its reference within the block with the extended template
75-
thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], partialPattern.extendedTemplate);
76-
}
76+
//if partial has style modifier data, replace the styleModifier value
77+
if(pattern.stylePartials && pattern.stylePartials.length > 0){
78+
style_modifier_hunter.consume_style_modifier(partialPattern, foundPartials[j], patternlab);
79+
}
7780

78-
//render with data
79-
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
81+
//replace its reference within the block with the extended template
82+
thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], partialPattern.extendedTemplate);
83+
}
8084

81-
} else{
82-
//just render with mergedData
83-
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
84-
}
85+
//render with data
86+
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
8587

86-
//add the rendered HTML to our string
87-
repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML;
88-
}
88+
} else{
89+
//just render with mergedData
90+
thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
91+
}
8992

90-
//replace the block with our generated HTML
91-
var repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
92-
pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml);
93+
//add the rendered HTML to our string
94+
repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML;
95+
}
96+
97+
//replace the block with our generated HTML
98+
var repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
99+
pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml);
93100

94101
});
95102
}

builder/media_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.13.0 - 2015
2+
* patternlab-node - v0.14.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/object_factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.13.0 - 2015
2+
* patternlab-node - v0.14.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/parameter_hunter.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
2-
* patternlab-node - v0.13.0 - 2015
3-
*
1+
/*
2+
* patternlab-node - v0.14.0 - 2015
3+
*
44
* 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.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
88
*
99
*/
1010

@@ -16,17 +16,18 @@
1616
var extend = require('util')._extend,
1717
pa = require('./pattern_assembler'),
1818
mustache = require('mustache'),
19+
smh = require('./style_modifier_hunter'),
20+
style_modifier_hunter = new smh(),
1921
pattern_assembler = new pa();
2022

2123
function findparameters(pattern, patternlab){
2224

23-
//find the {{> template-name(*) }} within patterns
24-
var matches = pattern.template.match(/{{>([ ]+)?([\w\-\.\/~]+)(\()(.+)(\))([ ]+)?}}/g);
25-
if(matches !== null){
25+
if(pattern.parameteredPartials && pattern.parameteredPartials.length > 0){
2626
//compile this partial immeadiately, essentially consuming it.
27-
matches.forEach(function(pMatch, index, matches){
28-
//find the partial's name
27+
pattern.parameteredPartials.forEach(function(pMatch, index, matches){
28+
//find the partial's name and retrieve it
2929
var partialName = pMatch.match(/([\w\-\.\/~]+)/g)[0];
30+
var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab);
3031

3132
if(patternlab.config.debug){
3233
console.log('found patternParameters for ' + partialName);
@@ -40,13 +41,17 @@
4041
//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
4142
var paramData = eval(paramString);
4243

43-
var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab);
4444
var globalData = JSON.parse(JSON.stringify(patternlab.data));
45-
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
45+
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {}));
4646

4747
var allData = pattern_assembler.merge_data(globalData, localData);
4848
allData = pattern_assembler.merge_data(allData, paramData);
4949

50+
//if partial has style modifier data, replace the styleModifier value
51+
if(pattern.stylePartials && pattern.stylePartials.length > 0){
52+
style_modifier_hunter.consume_style_modifier(partialPattern, pMatch, patternlab);
53+
}
54+
5055
//extend pattern data links into link for pattern link shortcuts to work. we do this locally and globally
5156
allData.link = extend({}, patternlab.data.link);
5257

0 commit comments

Comments
 (0)