2f447d5e3a9292bf3fbdbbc5059c5c7a62794ca4
[ReadingsJQM.git] / js / app / app.coffee
1
2 window.Readings = {}
3
4 # Readings jquery plugin dispatcher.
5 $.fn.Readings =  ->
6   meth = arguments[0]
7   args = []
8   for a in arguments
9     args.push a
10   args.shift()
11   $.fn.Readings[meth].apply(this, args)
12
13
14 Readings.init = ->
15   if Readings.initialized?
16     return
17   Readings.config = new Readings.Config
18     wlurl: 'http://readings.local'
19     categories:
20       'author': 'autor',
21       'epoch': 'epoka',
22       'genre': 'gatunek',
23       'kind': 'rodzaj',
24      # 'themes': 'motyw'
25     show_filter: [ 'authors', 'themes' ]
26     show_dividers: [ 'authors', 'themes']
27     db_version: '1.0'
28   Readings.catalogue = new Readings.Catalogue().open()
29   Readings.initialized = true
30
31 $(document).on 'pageinit', '#page-categories' , (ev) ->
32   Readings.init()
33   $('#list-categories').Readings 'CategoryList'
34
35 rcategory = /category=(\w+)/
36 rtag = /tag=([a-z0-9-]+)/
37 rtagid = /tag_id=([0-9]+)/
38
39 $(document).on 'pageinit', "#page-tags", (ev, ui) ->
40   category = rcategory.exec($(this).attr('data-url'))
41   if category?
42     category = category[1]
43     if category?
44       return $(this).Readings 'list',
45 #        category: category
46 #        url: Readings.config.get('wlurl') + "/api/#{category}"
47         sql: "SELECT * FROM tag WHERE category=? ORDER BY sort_key"
48         params: [category]
49         filter: (Readings.config.get('show_filter').indexOf(category) >= 0)
50         mapper: (rec) -> new Readings.Tag(rec, category)
51         dividers: (Readings.config.get('show_dividers').indexOf(category) >= 0)
52   alert 'no category in query string'
53
54 $(document).on 'pageinit', '#page-books', (ev, ui) ->
55   tag_id_m = rtagid.exec($(this).attr('data-url'))
56   tag_id = tag_id_m[1] if tag_id_m?
57
58   tag = Readings.catalogue.withTag tag_id, (tag) =>
59     $(this).Readings 'list',
60       fetch: (cb) -> Readings.catalogue.withBooks(tag, cb)
61       filter: true
62       dividers: (tag.category != 'author')