@@ -10,6 +10,7 @@ const list_item_hunter = function () {
10
10
const logger = require ( './log' ) ;
11
11
const parseLink = require ( './parseLink' ) ;
12
12
const getPartial = require ( './get' ) ;
13
+ const render = require ( './render' ) ;
13
14
14
15
const style_modifier_hunter = new smh ( ) ;
15
16
const items = [ 'zero' , 'one' , 'two' , 'three' , 'four' , 'five' , 'six' , 'seven' , 'eight' , 'nine' , 'ten' , 'eleven' , 'twelve' , 'thirteen' , 'fourteen' , 'fifteen' , 'sixteen' , 'seventeen' , 'eighteen' , 'nineteen' , 'twenty' ] ;
@@ -19,123 +20,141 @@ const list_item_hunter = function () {
19
20
const matches = pattern . findListItems ( ) ;
20
21
21
22
if ( matches !== null ) {
22
- matches . forEach ( function ( liMatch ) {
23
23
24
- logger . debug ( `found listItem of size ${ liMatch } inside ${ pattern . patternPartial } ` ) ;
24
+ return matches . reduce ( ( previousMatchPromise , liMatch ) => {
25
25
26
- //find the boundaries of the block
27
- const loopNumberString = liMatch . split ( '.' ) [ 1 ] . split ( '}' ) [ 0 ] . trim ( ) ;
28
- const end = liMatch . replace ( '#' , '/' ) ;
29
- const patternBlock = pattern . template . substring ( pattern . template . indexOf ( liMatch ) + liMatch . length , pattern . template . indexOf ( end ) ) . trim ( ) ;
26
+ return previousMatchPromise . then ( ( ) => {
27
+ logger . debug ( `found listItem of size ${ liMatch } inside ${ pattern . patternPartial } ` ) ;
30
28
31
- //build arrays that repeat the block, however large we need to
32
- const repeatedBlockTemplate = [ ] ;
29
+ //find the boundaries of the block
30
+ const loopNumberString = liMatch . split ( '.' ) [ 1 ] . split ( '}' ) [ 0 ] . trim ( ) ;
31
+ const end = liMatch . replace ( '#' , '/' ) ;
32
+ const patternBlock = pattern . template . substring ( pattern . template . indexOf ( liMatch ) + liMatch . length , pattern . template . indexOf ( end ) ) . trim ( ) ;
33
33
34
- //let repeatedBlockHtml = ''; //todo
35
- for ( let i = 0 ; i < items . indexOf ( loopNumberString ) ; i ++ ) {
34
+ //build arrays that repeat the block, however large we need to
35
+ const repeatedBlockTemplate = [ ] ;
36
36
37
- logger . debug ( `list item(s) in pattern ${ pattern . patternPartial } , adding ${ patternBlock } to repeatedBlockTemplate` ) ;
38
- repeatedBlockTemplate . push ( patternBlock ) ;
39
- }
37
+ //what we will eventually replace our template's listitems block with
38
+ let repeatedBlockHtml = '' ;
40
39
41
- //check for a local listitems.json file
42
- let listData ;
43
- try {
44
- listData = jsonCopy ( patternlab . listitems , 'config.paths.source.data listitems' ) ;
45
- } catch ( err ) {
46
- logger . warning ( `There was an error parsing JSON for ${ pattern . relPath } ` ) ;
47
- logger . warning ( err ) ;
48
- }
40
+ for ( let i = 0 ; i < items . indexOf ( loopNumberString ) ; i ++ ) {
49
41
50
- listData = _ . merge ( listData , pattern . listitems ) ;
51
- listData = parseLink ( patternlab , listData , 'listitems.json + any pattern listitems.json' ) ;
52
-
53
- //iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
54
- for ( let i = 0 ; i < repeatedBlockTemplate . length ; i ++ ) {
55
-
56
- let thisBlockTemplate = repeatedBlockTemplate [ i ] ;
57
-
58
- //let thisBlockHTML = ""; //todo
42
+ logger . debug ( `list item(s) in pattern ${ pattern . patternPartial } , adding ${ patternBlock } to repeatedBlockTemplate` ) ;
43
+ repeatedBlockTemplate . push ( patternBlock ) ;
44
+ }
59
45
60
- //combine listItem data with pattern data with global data
61
- const itemData = listData [ '' + items . indexOf ( loopNumberString ) ] ; //this is a property like "2"
62
- let globalData ;
63
- let localData ;
46
+ //check for a local listitems.json file
47
+ let listData ;
64
48
try {
65
- globalData = jsonCopy ( patternlab . data , 'config.paths.source.data global data' ) ;
66
- localData = jsonCopy ( pattern . jsonFileData , `${ pattern . patternPartial } data` ) ;
49
+ listData = jsonCopy ( patternlab . listitems , 'config.paths.source.data listitems' ) ;
67
50
} catch ( err ) {
68
51
logger . warning ( `There was an error parsing JSON for ${ pattern . relPath } ` ) ;
69
52
logger . warning ( err ) ;
70
53
}
71
54
72
- let allData = _ . merge ( globalData , localData ) ;
73
- allData = _ . merge ( allData , itemData !== undefined ? itemData [ i ] : { } ) ; //itemData could be undefined if the listblock contains no partial, just markup
74
- allData . link = extend ( { } , patternlab . data . link ) ;
55
+ listData = _ . merge ( listData , pattern . listitems ) ;
56
+ listData = parseLink ( patternlab , listData , 'listitems.json + any pattern listitems.json' ) ;
75
57
76
- //check for partials within the repeated block
77
- const foundPartials = Pattern . createEmpty ( { 'template' : thisBlockTemplate } ) . findPartials ( ) ;
58
+ //iterate over each copied block, rendering its contents
59
+ const allBlocks = repeatedBlockTemplate . reduce ( ( previousPromise , currentBlockTemplate , index ) => {
78
60
79
- if ( foundPartials && foundPartials . length > 0 ) {
61
+ let thisBlockTemplate = currentBlockTemplate ;
80
62
81
- for ( let j = 0 ; j < foundPartials . length ; j ++ ) {
63
+ return previousPromise . then ( ( ) => {
82
64
83
- //get the partial
84
- const partialName = foundPartials [ j ] . match ( / ( [ \w \- \. \/ ~ ] + ) / g) [ 0 ] ;
85
- const partialPattern = getPartial ( partialName , patternlab ) ;
86
-
87
- //create a copy of the partial so as to not pollute it after the get_pattern_by_key call.
88
- let cleanPartialPattern ;
65
+ //combine listItem data with pattern data with global data
66
+ const itemData = listData [ '' + items . indexOf ( loopNumberString ) ] ; //this is a property like "2"
67
+ let globalData ;
68
+ let localData ;
89
69
try {
90
- cleanPartialPattern = JSON . parse ( JSON . stringify ( partialPattern ) ) ;
91
- cleanPartialPattern = jsonCopy ( partialPattern , `partial pattern ${ partialName } ` ) ;
70
+ globalData = jsonCopy ( patternlab . data , 'config.paths.source.data global data' ) ;
71
+ localData = jsonCopy ( pattern . jsonFileData , `${ pattern . patternPartial } data ` ) ;
92
72
} catch ( err ) {
93
73
logger . warning ( `There was an error parsing JSON for ${ pattern . relPath } ` ) ;
94
74
logger . warning ( err ) ;
95
75
}
96
76
97
- //if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #356
98
- cleanPartialPattern . extendedTemplate = cleanPartialPattern . template ;
77
+ let allData = _ . merge ( globalData , localData ) ;
78
+ allData = _ . merge ( allData , itemData !== undefined ? itemData [ index ] : { } ) ; //itemData could be undefined if the listblock contains no partial, just markup
79
+ allData . link = extend ( { } , patternlab . data . link ) ;
99
80
100
- //if partial has style modifier data, replace the styleModifier value
101
- if ( foundPartials [ j ] . indexOf ( ':' ) > - 1 ) {
102
- style_modifier_hunter . consume_style_modifier ( cleanPartialPattern , foundPartials [ j ] , patternlab ) ;
103
- }
81
+ //check for partials within the repeated block
82
+ const foundPartials = Pattern . createEmpty ( { 'template' : thisBlockTemplate } ) . findPartials ( ) ;
104
83
105
- //replace its reference within the block with the extended template
106
- thisBlockTemplate = thisBlockTemplate . replace ( foundPartials [ j ] , cleanPartialPattern . extendedTemplate ) ;
107
- }
84
+ let renderPromise = undefined ;
108
85
109
- //render with data
110
- //TODO
111
- //thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
86
+ if ( foundPartials && foundPartials . length > 0 ) {
112
87
113
- } else {
88
+ for ( let j = 0 ; j < foundPartials . length ; j ++ ) {
114
89
115
- //just render with mergedData
116
- //TODO
117
- //thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
118
- }
90
+ //get the partial
91
+ const partialName = foundPartials [ j ] . match ( / ( [ \w \- \. \/ ~ ] + ) / g) [ 0 ] ;
92
+ const partialPattern = getPartial ( partialName , patternlab ) ;
93
+
94
+ //create a copy of the partial so as to not pollute it after the get_pattern_by_key call.
95
+ let cleanPartialPattern ;
96
+ try {
97
+ cleanPartialPattern = JSON . parse ( JSON . stringify ( partialPattern ) ) ;
98
+ cleanPartialPattern = jsonCopy ( partialPattern , `partial pattern ${ partialName } ` ) ;
99
+ } catch ( err ) {
100
+ logger . warning ( `There was an error parsing JSON for ${ pattern . relPath } ` ) ;
101
+ logger . warning ( err ) ;
102
+ }
103
+
104
+ //if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #356
105
+ cleanPartialPattern . extendedTemplate = cleanPartialPattern . template ;
106
+
107
+ //if partial has style modifier data, replace the styleModifier value
108
+ if ( foundPartials [ j ] . indexOf ( ':' ) > - 1 ) {
109
+ style_modifier_hunter . consume_style_modifier ( cleanPartialPattern , foundPartials [ j ] , patternlab ) ;
110
+ }
111
+
112
+ //replace its reference within the block with the extended template
113
+ thisBlockTemplate = thisBlockTemplate . replace ( foundPartials [ j ] , cleanPartialPattern . extendedTemplate ) ;
114
+ }
115
+
116
+ //render with data
117
+ renderPromise = render ( Pattern . createEmpty ( { 'template' : thisBlockTemplate } ) , allData , patternlab . partials ) ;
118
+ } else {
119
+ //just render with mergedData
120
+ renderPromise = render ( Pattern . createEmpty ( { 'template' : thisBlockTemplate } ) , allData , patternlab . partials ) ;
121
+ }
122
+
123
+ return renderPromise . then ( ( thisBlockHTML ) => {
124
+
125
+ //add the rendered HTML to our string
126
+ repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML ;
127
+ } ) . catch ( ( reason ) => {
128
+ logger . error ( reason ) ;
129
+ } ) ;
130
+ } ) . catch ( ( reason ) => {
131
+ logger . error ( reason ) ;
132
+ } ) ;
133
+ } , Promise . resolve ( ) ) ;
119
134
120
- //add the rendered HTML to our string
121
- //repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML; //todo
122
- }
135
+ return allBlocks . then ( ( ) => {
123
136
124
- //replace the block with our generated HTML
125
- //const repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
137
+ //replace the block with our generated HTML
138
+ const repeatingBlock = pattern . extendedTemplate . substring ( pattern . extendedTemplate . indexOf ( liMatch ) , pattern . extendedTemplate . indexOf ( end ) + end . length ) ;
139
+ pattern . extendedTemplate = pattern . extendedTemplate . replace ( repeatingBlock , repeatedBlockHtml ) ;
126
140
127
- //pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml); //todo
141
+ //update the extendedTemplate in the partials object in case this pattern is consumed later
142
+ patternlab . partials [ pattern . patternPartial ] = pattern . extendedTemplate ;
143
+ } ) . catch ( ( reason ) => {
144
+ logger . error ( reason ) ;
145
+ } ) ;
146
+ } ) ;
128
147
129
- //update the extendedTemplate in the partials object in case this pattern is consumed later
130
- patternlab . partials [ pattern . patternPartial ] = pattern . extendedTemplate ;
148
+ } , Promise . resolve ( ) ) ;
131
149
132
- } ) ;
150
+ } else {
151
+ return Promise . resolve ( ) ;
133
152
}
134
153
}
135
154
136
155
return {
137
156
process_list_item_partials : function ( pattern , patternlab ) {
138
- processListItemPartials ( pattern , patternlab ) ;
157
+ return processListItemPartials ( pattern , patternlab ) ;
139
158
}
140
159
} ;
141
160
} ;
0 commit comments