ported to Web SQL
[ReadingsJQM.git] / js / app / list.js
diff --git a/js/app/list.js b/js/app/list.js
new file mode 100644 (file)
index 0000000..7b0b0a5
--- /dev/null
@@ -0,0 +1,98 @@
+// Generated by CoffeeScript 1.3.3
+(function() {
+
+  $.fn.Readings.list = function(opts) {
+    return this.each(function() {
+      var list, render,
+        _this = this;
+      $('[data-role=header] h1').text(opts.title);
+      list = $('[data-role=listview]', this);
+      console.log("lst1 " + list.length);
+      if (!opts.filter) {
+        $(".ui-listview-filter").hide();
+      }
+      render = function(objs) {
+        var divider, last_divider, obj, _i, _len;
+        console.log("lst " + list.length);
+        list.empty();
+        last_divider = null;
+        for (_i = 0, _len = objs.length; _i < _len; _i++) {
+          obj = objs[_i];
+          if (opts.dividers) {
+            divider = obj.group();
+            if (last_divider !== divider) {
+              list.append("<li data-role=\"list-divider\">" + divider + "</li>");
+              last_divider = divider;
+            }
+          }
+          list.append(obj.render());
+        }
+        return list.listview('refresh');
+      };
+      if (opts.sql) {
+        Readings.catalogue.db.transaction(function(tx) {
+          return tx.executeSql(opts.sql, opts.params, function(tx, rs) {
+            var i, objs, _i, _ref;
+            objs = [];
+            for (i = _i = 0, _ref = rs.rows.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
+              objs.push(opts.mapper(rs.rows.item(i)));
+            }
+            return render(objs);
+          }, function(tx, err) {
+            window.last_error = err;
+            return alert("SQL Error while fetching list contents: " + err.message);
+          });
+        });
+      }
+      if (opts.url) {
+        $.ajax({
+          url: opts.url,
+          contentType: "json",
+          success: function(data) {
+            return render($.map(data, opts.mapper));
+          }
+        });
+      }
+      if (opts.fetch) {
+        return opts.fetch(render);
+      }
+    });
+  };
+
+  $.fn.Readings.TagList = function(category) {
+    return this.each(function() {
+      var list;
+      $('[data-role=header] h1').text(Readings.config.get('categories')[category]);
+      list = $('[data-role=listview]', this);
+      if (Readings.config.get('show_filter').indexOf(category) < 0) {
+        $(".ui-listview-filter").hide();
+      }
+      return $.ajax({
+        url: Readings.config.get('wlurl') + ("/api/" + category),
+        contentType: "json",
+        success: function(data) {
+          var last_separator, separator, show_separator, t, tags, _i, _len;
+          tags = $.map(data, function(rec) {
+            return new Readings.Tag(rec, category);
+          });
+          list.empty();
+          last_separator = null;
+          show_separator = Readings.config.get('show_dividers').indexOf(category) >= 0;
+          for (_i = 0, _len = tags.length; _i < _len; _i++) {
+            t = tags[_i];
+            if (show_separator) {
+              separator = t.group();
+              if (last_separator !== separator) {
+                list.append("<li data-role=\"list-divider\">" + separator + "</li>");
+                last_separator = separator;
+              }
+            }
+            list.append(t.render());
+          }
+          return list.listview('refresh');
+        }
+      });
+    });
+  };
+
+}).call(this);