f31e8557b22948ba59d3f9d09c2625d55234bed8
[ReadingsJQM.git] / js / app / TagList.js
1 // Generated by CoffeeScript 1.3.3
2 (function() {
3
4   Readings.Tag = (function() {
5
6     function Tag(record, category) {
7       this.category = category;
8       this.href = record.href;
9       this.name = record.name;
10       this.slug = $.grep(this.href.split('/'), function(e) {
11         return e !== "";
12       });
13     }
14
15     Tag.prototype.render = function() {
16       return "<li><a href=\"#\">" + this.name + "</a></li>";
17     };
18
19     Tag.prototype.group = function() {
20       if (this.category === 'authors') {
21         return this.name.split(' ').slice(-1)[0][0].toUpperCase();
22       } else {
23         return this.name[0].toUpperCase();
24       }
25     };
26
27     return Tag;
28
29   })();
30
31   $.fn.Readings.list = function(opts) {
32     return this.each(function() {
33       var list;
34       $('[data-role=header] h1').text(opts.title);
35       list = $('[data-role=listview]', this);
36       if (!opts.filter) {
37         $(".ui-listview-filter").hide();
38       }
39       return $.ajax({
40         url: opts.url,
41         contentType: "json",
42         success: function(data) {
43           var divider, last_divider, obj, objs, _i, _len;
44           objs = $.map(data, opts.mapper);
45           list.empty();
46           last_divider = null;
47           for (_i = 0, _len = objs.length; _i < _len; _i++) {
48             obj = objs[_i];
49             if (opts.dividers) {
50               divider = obj.group();
51               if (last_divider !== divider) {
52                 list.append("<li data-role=\"list-divider\">" + divider + "</li>");
53                 last_divider = divider;
54               }
55             }
56             list.append(obj.render());
57           }
58           return list.listview('refresh');
59         }
60       });
61     });
62   };
63
64   $.fn.Readings.TagList = function(category) {
65     return this.each(function() {
66       var list;
67       $('[data-role=header] h1').text(Readings.config.get('categories')[category]);
68       list = $('[data-role=listview]', this);
69       if (Readings.config.get('show_filter').indexOf(category) < 0) {
70         $(".ui-listview-filter").hide();
71       }
72       return $.ajax({
73         url: Readings.config.get('wlurl') + ("/api/" + category),
74         contentType: "json",
75         success: function(data) {
76           var last_separator, separator, show_separator, t, tags, _i, _len;
77           tags = $.map(data, function(rec) {
78             return new Readings.Tag(rec, category);
79           });
80           list.empty();
81           last_separator = null;
82           show_separator = Readings.config.get('show_dividers').indexOf(category) >= 0;
83           for (_i = 0, _len = tags.length; _i < _len; _i++) {
84             t = tags[_i];
85             if (show_separator) {
86               separator = t.group();
87               if (last_separator !== separator) {
88                 list.append("<li data-role=\"list-divider\">" + separator + "</li>");
89                 last_separator = separator;
90               }
91             }
92             list.append(t.render());
93           }
94           return list.listview('refresh');
95         }
96       });
97     });
98   };
99
100 }).call(this);