Started coding reader
[ReadingsJQM.git] / js / app / models.js
index d332ac1..e5722c5 100644 (file)
@@ -1,4 +1,3 @@
-// Generated by CoffeeScript 1.3.3
 (function() {
   var url_to_slug;
 
     Book.prototype.render = function() {
       var wlurl;
       wlurl = Readings.config.get('wlurl');
-      return "<li>      <a href=\"reader.html?book_id=" + this.id + "\">        <img src=\"" + wlurl + this.cover + "\">        <h3> " + this.title + "</h3>       </a>     </li>";
+      return "<li>      <a href=\"reader.html?book_id=" + this.id + "\">        <img src=\"" + wlurl + this.cover + "\">        <h3>" + this.title + "</h3>       </a>     </li>";
     };
 
     Book.prototype.group = function() {
       return this.authors;
     };
 
+    Book.prototype.is_local = function() {
+      var html;
+      html = localStorage.getItem("html:" + this.id);
+      return !(html === null);
+    };
+
+    Book.prototype.get_text = function() {
+      var html;
+      if (this._local) {
+        html = localStorage.getItem("html:" + this.id);
+        if (html) return $(html);
+      }
+      return null;
+    };
+
+    Book.prototype.fetch = function(cb) {
+      var _this = this;
+      return $.ajax(Readings.config.get("wlurl" + ("/katalog/lektura/" + this.slug + ".html"), {
+        type: 'get',
+        dataType: 'html',
+        success: function(text) {
+          text = _this.mobilize_html(text);
+          localStorage.setItem("html:" + _this.id, text);
+          _this._local = 1;
+          return _this.db.transaction(function(tx) {
+            return tx.executeSql("UPDATE _local = 1 WHERE id=?", [_this.id], function(tx, rs) {
+              return cb(_this);
+            });
+          });
+        },
+        error: function(er) {
+          throw Error("Error fetching book text slug=" + this.slug + ", " + er);
+        }
+      }));
+    };
+
+    Book.prototype.mobilize_html = function(html) {
+      var book_text;
+      html = $(html);
+      book_text = html.find("#book-id");
+      html.find("#download, #header, #themes")["delete"]();
+      return book_text;
+    };
+
     return Book;
 
   })();