ported to Web SQL
[ReadingsJQM.git] / js / app / list.js
1 // Generated by CoffeeScript 1.3.3
2 (function() {
3
4   $.fn.Readings.list = function(opts) {
5     return this.each(function() {
6       var list, render,
7         _this = this;
8       $('[data-role=header] h1').text(opts.title);
9       list = $('[data-role=listview]', this);
10       console.log("lst1 " + list.length);
11       if (!opts.filter) {
12         $(".ui-listview-filter").hide();
13       }
14       render = function(objs) {
15         var divider, last_divider, obj, _i, _len;
16         console.log("lst " + list.length);
17         list.empty();
18         last_divider = null;
19         for (_i = 0, _len = objs.length; _i < _len; _i++) {
20           obj = objs[_i];
21           if (opts.dividers) {
22             divider = obj.group();
23             if (last_divider !== divider) {
24               list.append("<li data-role=\"list-divider\">" + divider + "</li>");
25               last_divider = divider;
26             }
27           }
28           list.append(obj.render());
29         }
30         return list.listview('refresh');
31       };
32       if (opts.sql) {
33         Readings.catalogue.db.transaction(function(tx) {
34           return tx.executeSql(opts.sql, opts.params, function(tx, rs) {
35             var i, objs, _i, _ref;
36             objs = [];
37             for (i = _i = 0, _ref = rs.rows.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
38               objs.push(opts.mapper(rs.rows.item(i)));
39             }
40             return render(objs);
41           }, function(tx, err) {
42             window.last_error = err;
43             return alert("SQL Error while fetching list contents: " + err.message);
44           });
45         });
46       }
47       if (opts.url) {
48         $.ajax({
49           url: opts.url,
50           contentType: "json",
51           success: function(data) {
52             return render($.map(data, opts.mapper));
53           }
54         });
55       }
56       if (opts.fetch) {
57         return opts.fetch(render);
58       }
59     });
60   };
61
62   $.fn.Readings.TagList = function(category) {
63     return this.each(function() {
64       var list;
65       $('[data-role=header] h1').text(Readings.config.get('categories')[category]);
66       list = $('[data-role=listview]', this);
67       if (Readings.config.get('show_filter').indexOf(category) < 0) {
68         $(".ui-listview-filter").hide();
69       }
70       return $.ajax({
71         url: Readings.config.get('wlurl') + ("/api/" + category),
72         contentType: "json",
73         success: function(data) {
74           var last_separator, separator, show_separator, t, tags, _i, _len;
75           tags = $.map(data, function(rec) {
76             return new Readings.Tag(rec, category);
77           });
78           list.empty();
79           last_separator = null;
80           show_separator = Readings.config.get('show_dividers').indexOf(category) >= 0;
81           for (_i = 0, _len = tags.length; _i < _len; _i++) {
82             t = tags[_i];
83             if (show_separator) {
84               separator = t.group();
85               if (last_separator !== separator) {
86                 list.append("<li data-role=\"list-divider\">" + separator + "</li>");
87                 last_separator = separator;
88               }
89             }
90             list.append(t.render());
91           }
92           return list.listview('refresh');
93         }
94       });
95     });
96   };
97
98 }).call(this);