66c9f1cc8d423b33e70f40bfb6404cf63e356b4f
[mkws-moved-to-github.git] / src / mkws-handlebars.js
1 // Handlebars helpers
2
3 Handlebars.registerHelper('mkws-json', function(obj) {
4   return $.toJSON(obj);
5 });
6
7
8 // This is intended to handle paragraphs from Wikipedia, hence the
9 // rather hacky code to remove numbered references.
10 //
11 Handlebars.registerHelper('mkws-paragraphs', function(obj) {
12   var acc = [];
13   for (var i = 0; i < obj.length; i++) {
14     acc.push('<p>', obj[i].replace(/\[[0-9,]+\]/g, ''), '</p>');
15   }
16   return acc.join('');
17 });
18
19
20 Handlebars.registerHelper('mkws-translate', function(s) {
21   return mkws.M(s);
22 });
23
24
25 // We need {{mkws-attr '@name'}} because Handlebars can't parse {{@name}}
26 Handlebars.registerHelper('mkws-attr', function(attrName) {
27   return this[attrName];
28 });
29
30
31 /*
32  * Use as follows: {{#mkws-if-any NAME1 having="NAME2"}}
33  * Applicable when NAME1 is the name of an array
34  * The guarded code runs only if at least one element of the NAME1
35  * array has a subelement called NAME2.
36  */
37 Handlebars.registerHelper('mkws-if-any', function(items, options) {
38   var having = options.hash.having;
39   for (var i in items) {
40     var item = items[i]
41     if (!having || item[having]) {
42       return options.fn(this);
43     }
44   }
45   return "";
46 });
47
48
49 Handlebars.registerHelper('mkws-first', function(items, options) {
50   var having = options.hash.having;
51   for (var i in items) {
52     var item = items[i]
53     if (!having || item[having]) {
54       return options.fn(item);
55     }
56   }
57   return "";
58 });
59
60
61 Handlebars.registerHelper('mkws-commaList', function(items, options) {
62   var out = "";
63
64   for (var i in items) {
65     if (i > 0) out += ", ";
66     out += options.fn(items[i])
67   }
68
69   return out;
70 });
71
72
73 Handlebars.registerHelper('mkws-index1', function(obj) {
74   return obj.data.index + 1;
75 });