|
| 1 | +/* |
| 2 | + * patternlab-node - v0.11.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 | + */ |
| 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 | + var end = liMatch.replace('#', '/'); |
| 35 | + var patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim(); |
| 36 | + |
| 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 | + } |
| 43 | + |
| 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); |
| 47 | + |
| 48 | + //iterate over each copied block, rendering its contents along with pattenlab.listitems[i] |
| 49 | + for(var i = 0; i < repeatedBlockTemplate.length; i++){ |
| 50 | + |
| 51 | + var thisBlockTemplate = repeatedBlockTemplate[i]; |
| 52 | + var thisBlockHTML = ""; |
| 53 | + |
| 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)); |
| 58 | + |
| 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); |
| 62 | + |
| 63 | + //check for partials within the repeated block |
| 64 | + var foundPartials = pattern_assembler.find_pattern_partials({ 'template' : thisBlockTemplate }); |
| 65 | + |
| 66 | + if(foundPartials && foundPartials.length > 0){ |
| 67 | + |
| 68 | + for(var j = 0; j < foundPartials.length; j++){ |
| 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 | + //render with data |
| 79 | + thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials); |
| 80 | + |
| 81 | + } else{ |
| 82 | + //just render with mergedData |
| 83 | + thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials); |
| 84 | + } |
| 85 | + |
| 86 | + //add the rendered HTML to our string |
| 87 | + repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML; |
| 88 | + } |
| 89 | + |
| 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 | + |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + process_list_item_partials: function(pattern, patternlab){ |
| 100 | + processListItemPartials(pattern, patternlab); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + }; |
| 105 | + |
| 106 | + module.exports = list_item_hunter; |
| 107 | + |
| 108 | +}()); |
0 commit comments