-
Notifications
You must be signed in to change notification settings - Fork 1
/
cardgen.js
75 lines (65 loc) · 2.55 KB
/
cardgen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Copyright 2017-2024 - Olivier Azeau
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
function repeatByQuantity(items)
{
return items.reduce(function(acc,item) {
var qty = item.quantity ? item.quantity : 1;
while(qty-->0)
acc = acc.concat(item);
return acc;
}, []);
}
function render(items)
{
for(var idx in items) {
var item = items[idx];
var output = item.tmpl.render(item);
$("#cards-output").append(output);
}
$("#cards-output").append("<div class='sep'/><div class='page'><a href='https://creativecommons.org/licenses/by-sa/4.0/deed.fr'>CC-BY-SA 4.0</a> - <a href='https://entrepriseagile.mnt.space/'>https://entrepriseagile.mnt.space/</a> - <a href='https://github.com/Oaz/MonEntrepriseAgile/blob/master/RULES.md'>Règles du jeu</a> - <a href='https://github.com/Oaz/MonEntrepriseAgile'>Code source</a></div><div class='sep'/>");
}
function generateCards(context, definition)
{
if(definition.newpage) {
render(context.remainder);
context = undefined;
}
if(context == undefined)
context = { remainder:[] };
var tmplText = $("#card-definitions").contents().find("."+definition.family).prop('outerHTML');
var tmpl = $.templates(tmplText);
for(var idx in definition.items)
definition.items[idx].tmpl = tmpl;
var itemsToRender = repeatByQuantity(definition.items);
itemsToRender = context.remainder.concat(itemsToRender);
while(itemsToRender.length>definition.cardsByPage) {
var itemSet = itemsToRender.slice(0,definition.cardsByPage);
render(itemSet);
itemsToRender = itemsToRender.slice(definition.cardsByPage);
}
return { remainder:itemsToRender };
}
$(document).ready(function() {
$('#card-definitions').on('load', function(){
var context = undefined;
for(var idx in cards)
context = generateCards(context, cards[idx]);
if(context.remainder)
render(context.remainder);
});
});