Started coding reader
[ReadingsJQM.git] / js / app / models.coffee
1
2
3 url_to_slug = (url) ->
4   slug_in_url = /([a-z0-9-]+)[/]?$/
5   slug_in_url.exec(url)[1]
6
7 class Readings.Book
8   constructor: (record) ->
9     $.extend(this, record)
10
11   render: ->
12     wlurl = Readings.config.get 'wlurl'
13     "<li>
14       <a href=\"reader.html?book_id=#{@id}\">
15         <img src=\"#{wlurl}#{@cover}\">
16         <h3>#{@title}</h3>
17        </a>
18      </li>"
19
20   group: ->
21     @authors
22
23   is_local: ->
24     html = localStorage.getItem("html:#{@id}")
25     return not (html == null)
26
27   get_text: ->
28     if @_local
29       html = localStorage.getItem("html:#{@id}")
30       return $(html) if html
31     null
32
33   # fetches books content from WL
34   # and pass it to callback
35   fetch: (cb) ->
36     $.ajax Readings.config.get "wlurl" + "/katalog/lektura/#{@slug}.html",
37       type: 'get',
38       dataType: 'html',
39       success: (text) =>
40         text = @mobilize_html text
41         localStorage.setItem("html:#{@id}", text)
42         @_local = 1
43         @db.transaction (tx) =>
44           tx.executeSql "UPDATE _local = 1 WHERE id=?", [@id], (tx, rs) =>
45             cb this
46       error: (er) ->
47         throw Error "Error fetching book text slug=#{@slug}, #{er}"
48
49   mobilize_html: (html) ->
50     html = $(html)
51     book_text = html.find("#book-id")
52     html.find("#download, #header, #themes").delete()
53     book_text
54
55
56 class Readings.Tag
57   constructor: (record, @category) ->
58     $.extend(this, record)
59
60   render: ->
61     "<li><a href=\"books.html?tag_id=#{@id}\">#{@name}</a></li>"
62
63   group: ->
64     @sort_key[0].toUpperCase()
65     # if @category == 'author'
66     #   # last word, first letter
67     #   @name.split(' ').slice(-1)[0][0].toUpperCase()
68     # else
69     #   @name[0].toUpperCase()
70
71
72
73 # $.fn.Readings.BookList = (category, tag) ->
74 #   this.each ->
75 #     $('[data-role=header] h1').text tag.name
76 #     list = $('[data-role=listview]', this)
77 #     $.ajax
78 #       url: "#{tag.href}books/"
79 #       #url: Readings.config.get('wlurl') + "/api/#{category}"
80 #       contentType: "json"
81 #       success: (data) ->
82 #         console.log(data)
83 #         books = $.map data, (rec) -> new Readings.Book(rec)
84 #         list.empty()
85 #         last_separator = null
86 #         show_separator = !(category == 'authors')
87
88 #         for b in books
89 #           # throw a separator in for some categories
90 #           if show_separator
91 #             separator = t.group()
92 #             if last_separator != separator
93 #               list.append "<li data-role=\"list-divider\">#{separator}</li>"
94 #               last_separator = separator
95
96 #           list.append b.render()
97 #           list.listview 'refresh'