Started coding reader
[ReadingsJQM.git] / js / app / models.js
1 (function() {
2   var url_to_slug;
3
4   url_to_slug = function(url) {
5     var slug_in_url;
6     slug_in_url = /([a-z0-9-]+)[/]?$/;
7     return slug_in_url.exec(url)[1];
8   };
9
10   Readings.Book = (function() {
11
12     function Book(record) {
13       $.extend(this, record);
14     }
15
16     Book.prototype.render = function() {
17       var wlurl;
18       wlurl = Readings.config.get('wlurl');
19       return "<li>      <a href=\"reader.html?book_id=" + this.id + "\">        <img src=\"" + wlurl + this.cover + "\">        <h3>" + this.title + "</h3>       </a>     </li>";
20     };
21
22     Book.prototype.group = function() {
23       return this.authors;
24     };
25
26     Book.prototype.is_local = function() {
27       var html;
28       html = localStorage.getItem("html:" + this.id);
29       return !(html === null);
30     };
31
32     Book.prototype.get_text = function() {
33       var html;
34       if (this._local) {
35         html = localStorage.getItem("html:" + this.id);
36         if (html) return $(html);
37       }
38       return null;
39     };
40
41     Book.prototype.fetch = function(cb) {
42       var _this = this;
43       return $.ajax(Readings.config.get("wlurl" + ("/katalog/lektura/" + this.slug + ".html"), {
44         type: 'get',
45         dataType: 'html',
46         success: function(text) {
47           text = _this.mobilize_html(text);
48           localStorage.setItem("html:" + _this.id, text);
49           _this._local = 1;
50           return _this.db.transaction(function(tx) {
51             return tx.executeSql("UPDATE _local = 1 WHERE id=?", [_this.id], function(tx, rs) {
52               return cb(_this);
53             });
54           });
55         },
56         error: function(er) {
57           throw Error("Error fetching book text slug=" + this.slug + ", " + er);
58         }
59       }));
60     };
61
62     Book.prototype.mobilize_html = function(html) {
63       var book_text;
64       html = $(html);
65       book_text = html.find("#book-id");
66       html.find("#download, #header, #themes")["delete"]();
67       return book_text;
68     };
69
70     return Book;
71
72   })();
73
74   Readings.Tag = (function() {
75
76     function Tag(record, category) {
77       this.category = category;
78       $.extend(this, record);
79     }
80
81     Tag.prototype.render = function() {
82       return "<li><a href=\"books.html?tag_id=" + this.id + "\">" + this.name + "</a></li>";
83     };
84
85     Tag.prototype.group = function() {
86       return this.sort_key[0].toUpperCase();
87     };
88
89     return Tag;
90
91   })();
92
93 }).call(this);