|
1 |
| -/* |
2 |
| - * patternlab-node - v1.1.3 - 2016 |
3 |
| - * |
| 1 | +/* |
| 2 | + * patternlab-node - v1.1.3 - 2016 |
| 3 | + * |
4 | 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. |
| 5 | + * Licensed under the MIT license. |
| 6 | + * |
| 7 | + * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice. |
8 | 8 | *
|
9 | 9 | */
|
10 | 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 |
| - smh = require('./style_modifier_hunter'), |
19 |
| - mustache = require('mustache'), |
20 |
| - pattern_assembler = new pa(), |
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']; |
23 |
| - |
24 |
| - function processListItemPartials(pattern, patternlab){ |
25 |
| - //find any listitem blocks |
26 |
| - var matches = pattern_assembler.find_list_items(pattern, patternlab); |
27 |
| - if(matches !== null){ |
28 |
| - matches.forEach(function(liMatch, index, matches){ |
29 |
| - |
30 |
| - if(patternlab.config.debug){ |
31 |
| - console.log('found listItem of size ' + liMatch + ' inside ' + pattern.key); |
32 |
| - } |
33 |
| - |
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(); |
38 |
| - //build arrays that repeat the block, however large we need to |
39 |
| - var repeatedBlockTemplate = []; |
40 |
| - var repeatedBlockHtml = ''; |
41 |
| - for(var i = 0; i < items.indexOf(loopNumberString); i++){ |
42 |
| - repeatedBlockTemplate.push(patternBlock); |
43 |
| - } |
44 |
| - |
45 |
| - //check for a local listitems.json file |
46 |
| - var listData = JSON.parse(JSON.stringify(patternlab.listitems)); |
47 |
| - listData = pattern_assembler.merge_data(listData, pattern.listitems); |
48 |
| - |
49 |
| - //iterate over each copied block, rendering its contents along with pattenlab.listitems[i] |
50 |
| - for(var i = 0; i < repeatedBlockTemplate.length; i++){ |
51 |
| - |
52 |
| - var thisBlockTemplate = repeatedBlockTemplate[i]; |
53 |
| - var thisBlockHTML = ""; |
54 |
| - |
55 |
| - //combine listItem data with pattern data with global data |
56 |
| - var itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2" |
57 |
| - var globalData = JSON.parse(JSON.stringify(patternlab.data)); |
58 |
| - var localData = JSON.parse(JSON.stringify(pattern.jsonFileData)); |
59 |
| - |
60 |
| - var allData = pattern_assembler.merge_data(globalData, localData); |
61 |
| - allData = pattern_assembler.merge_data(allData, itemData != undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup |
62 |
| - allData.link = extend({}, patternlab.data.link); |
63 |
| - |
64 |
| - //check for partials within the repeated block |
65 |
| - var foundPartials = pattern_assembler.find_pattern_partials({ 'template' : thisBlockTemplate }); |
66 |
| - |
67 |
| - if(foundPartials && foundPartials.length > 0){ |
68 |
| - |
69 |
| - for(var j = 0; j < foundPartials.length; j++){ |
70 |
| - |
71 |
| - //get the partial |
72 |
| - var partialName = foundPartials[j].match(/([\w\-\.\/~]+)/g)[0]; |
73 |
| - var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab); |
74 |
| - |
75 |
| - //create a copy of the partial so as to not pollute it after the get_pattern_by_key call. |
76 |
| - var cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern)); |
77 |
| - |
78 |
| - //if partial has style modifier data, replace the styleModifier value |
79 |
| - if(foundPartials[j].indexOf(':') > -1){ |
80 |
| - style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartials[j], patternlab); |
81 |
| - } |
82 |
| - |
83 |
| - //replace its reference within the block with the extended template |
84 |
| - thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], cleanPartialPattern.extendedTemplate); |
85 |
| - } |
86 |
| - |
87 |
| - //render with data |
88 |
| - thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials); |
89 |
| - |
90 |
| - } else{ |
91 |
| - //just render with mergedData |
92 |
| - thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials); |
93 |
| - } |
94 |
| - |
95 |
| - //add the rendered HTML to our string |
96 |
| - repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML; |
97 |
| - } |
98 |
| - |
99 |
| - //replace the block with our generated HTML |
100 |
| - var repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length); |
101 |
| - pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml); |
102 |
| - |
103 |
| - //update the extendedTemplate in the partials object in case this pattern is consumed later |
104 |
| - patternlab.partials[pattern.key] = pattern.extendedTemplate; |
105 |
| - |
106 |
| - }); |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - return { |
111 |
| - process_list_item_partials: function(pattern, patternlab){ |
112 |
| - processListItemPartials(pattern, patternlab); |
113 |
| - } |
114 |
| - }; |
115 |
| - |
116 |
| - }; |
117 |
| - |
118 |
| - module.exports = list_item_hunter; |
119 | 11 |
|
120 |
| -}()); |
| 12 | +"use strict"; |
| 13 | + |
| 14 | +var list_item_hunter = function () { |
| 15 | + |
| 16 | + var extend = require('util')._extend, |
| 17 | + pa = require('./pattern_assembler'), |
| 18 | + smh = require('./style_modifier_hunter'), |
| 19 | + pattern_assembler = new pa(), |
| 20 | + style_modifier_hunter = new smh(), |
| 21 | + items = [ 'zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty']; |
| 22 | + |
| 23 | + function processListItemPartials(pattern, patternlab) { |
| 24 | + //find any listitem blocks |
| 25 | + var matches = pattern_assembler.find_list_items(pattern, patternlab); |
| 26 | + if (matches !== null) { |
| 27 | + matches.forEach(function (liMatch) { |
| 28 | + |
| 29 | + if (patternlab.config.debug) { |
| 30 | + console.log('found listItem of size ' + liMatch + ' inside ' + pattern.key); |
| 31 | + } |
| 32 | + |
| 33 | + //find the boundaries of the block |
| 34 | + var loopNumberString = liMatch.split('.')[1].split('}')[0].trim(); |
| 35 | + var end = liMatch.replace('#', '/'); |
| 36 | + var patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim(); |
| 37 | + //build arrays that repeat the block, however large we need to |
| 38 | + var repeatedBlockTemplate = []; |
| 39 | + var repeatedBlockHtml = ''; |
| 40 | + var i; // for loops |
| 41 | + |
| 42 | + for (i = 0; i < items.indexOf(loopNumberString); i++) { |
| 43 | + repeatedBlockTemplate.push(patternBlock); |
| 44 | + } |
| 45 | + |
| 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.listitems); |
| 49 | + |
| 50 | + //iterate over each copied block, rendering its contents along with pattenlab.listitems[i] |
| 51 | + for (i = 0; i < repeatedBlockTemplate.length; i++) { |
| 52 | + var thisBlockTemplate = repeatedBlockTemplate[i]; |
| 53 | + var thisBlockHTML = ""; |
| 54 | + |
| 55 | + //combine listItem data with pattern data with global data |
| 56 | + var itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2" |
| 57 | + var globalData = JSON.parse(JSON.stringify(patternlab.data)); |
| 58 | + var localData = JSON.parse(JSON.stringify(pattern.jsonFileData)); |
| 59 | + |
| 60 | + var allData = pattern_assembler.merge_data(globalData, localData); |
| 61 | + allData = pattern_assembler.merge_data(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup |
| 62 | + allData.link = extend({}, patternlab.data.link); |
| 63 | + |
| 64 | + //check for partials within the repeated block |
| 65 | + var foundPartials = pattern_assembler.find_pattern_partials({ 'template' : thisBlockTemplate }); |
| 66 | + |
| 67 | + if (foundPartials && foundPartials.length > 0) { |
| 68 | + for (var j = 0; j < foundPartials.length; j++) { |
| 69 | + //get the partial |
| 70 | + var partialName = foundPartials[j].match(/([\w\-\.\/~]+)/g)[0]; |
| 71 | + var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab); |
| 72 | + |
| 73 | + //create a copy of the partial so as to not pollute it after the get_pattern_by_key call. |
| 74 | + var cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern)); |
| 75 | + |
| 76 | + //if partial has style modifier data, replace the styleModifier value |
| 77 | + if (foundPartials[j].indexOf(':') > -1) { |
| 78 | + style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartials[j], patternlab); |
| 79 | + } |
| 80 | + |
| 81 | + //replace its reference within the block with the extended template |
| 82 | + thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], cleanPartialPattern.extendedTemplate); |
| 83 | + } |
| 84 | + |
| 85 | + //render with data |
| 86 | + thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials); |
| 87 | + } else { |
| 88 | + //just render with mergedData |
| 89 | + thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials); |
| 90 | + } |
| 91 | + |
| 92 | + //add the rendered HTML to our string |
| 93 | + repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML; |
| 94 | + } |
| 95 | + |
| 96 | + //replace the block with our generated HTML |
| 97 | + var repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length); |
| 98 | + pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml); |
| 99 | + |
| 100 | + //update the extendedTemplate in the partials object in case this pattern is consumed later |
| 101 | + patternlab.partials[pattern.key] = pattern.extendedTemplate; |
| 102 | + |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return { |
| 108 | + process_list_item_partials: function (pattern, patternlab) { |
| 109 | + processListItemPartials(pattern, patternlab); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | +}; |
| 114 | + |
| 115 | +module.exports = list_item_hunter; |
0 commit comments