1 | import extend from 'extend' |
||
2 | |||
3 | /** |
||
4 | * This helper divide an array into groups of objects and repeat the structure |
||
5 | * Usage : |
||
6 | * |
||
7 | *{{#eachgroup products '[{"group":"my","qty":"1"},{"group":"ty","qty":"3"}]'}} |
||
8 | * {{my.0.title}} <img src="{{my.0.cover_273x273}}" width="40"><br/> |
||
9 | * {{ty.0.title}} <img src="{{ty.0.cover_273x273}}" width="40"><br/> |
||
10 | * {{ty.1.title}} <img src="{{ty.1.cover_273x273}}" width="40"><br/> |
||
11 | * {{ty.2.title}} <img src="{{ty.2.cover_273x273}}" width="40"><br/> |
||
12 | *{{/eachgroup}} |
||
13 | * |
||
14 | * @param {[type]} context The array to parse |
||
15 | * @param {[type]} id the groups of groups and qty |
||
16 | * @param {[type]} options |
||
17 | * @return {[type]} html |
||
18 | */ |
||
19 | export default function eachGroup(context, groups, options) { |
||
20 | var fn = options.fn, |
||
21 | inverse = options.inverse |
||
22 | var ret = '' |
||
23 | var group = [] |
||
24 | function getLeaf(node) { |
||
25 | if (node.group && node.qty) { |
||
26 | return node |
||
27 | } else { |
||
28 | try { |
||
29 | return getLeaf(node[Object.keys(node)[0]]) |
||
30 | } catch (e) { |
||
31 | return null |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | |||
36 | if (typeof groups !== 'undefined') { |
||
37 | if (typeof groups === 'string') { |
||
38 | groups = JSON.parse(groups) |
||
39 | } |
||
40 | if (context && context.length > 0) { |
||
41 | for ( |
||
42 | var i = 0, |
||
43 | newCount = 0, |
||
44 | j = context.length, |
||
45 | distribIndex = 0, |
||
46 | k = 0, |
||
47 | iteration = 0; |
||
48 | i < j; |
||
49 | i++, newCount++ |
||
50 | ) { |
||
51 | var o = getLeaf(groups[Object.keys(groups)[distribIndex]]) |
||
52 | if (o == null) break |
||
53 | var first = newCount % o['qty'] === 0 |
||
54 | |||
55 | // Adding index, isFirst, isLast, isNewGroup to each record |
||
56 | context[i] = extend( |
||
57 | true, |
||
58 | { |
||
59 | index: i, |
||
60 | isFirst: first, |
||
61 | isNewGroup: first && newCount > 0, |
||
62 | isLast: i === context.length - 1 |
||
63 | }, |
||
64 | context[i] |
||
65 | ) |
||
66 | |||
67 | if (first) { |
||
68 | if (newCount > 0) { |
||
69 | // if the last group has been reached, I reinit the distribIndex |
||
70 | // and iterate on the result array |
||
71 | if (distribIndex === Object.keys(groups).length - 1) { |
||
72 | distribIndex = 0 |
||
73 | var ar = [] |
||
74 | } else { |
||
75 | distribIndex++ |
||
76 | } |
||
77 | iteration++ |
||
78 | newCount = 0 |
||
79 | k++ |
||
80 | } |
||
81 | |||
82 | o = getLeaf(groups[Object.keys(groups)[distribIndex]]) |
||
83 | var key = o['group'] |
||
84 | if (group[iteration] == null) group.push([]) |
||
85 | if (group[iteration][key] == null) { |
||
86 | group[iteration][key] = [] |
||
87 | } |
||
88 | } |
||
89 | group[iteration][key].push(context[i]) |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
90 | } |
||
91 | context = group |
||
92 | } |
||
93 | |||
94 | if (context) { |
||
95 | for (var i = 0, j = context.length, k = 0; i < j; i++) { |
||
96 | ret += fn(context[i]) |
||
97 | } |
||
98 | } else { |
||
99 | ret = inverse(this) |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return ret |
||
104 | } |
||
105 |