78429b26a75c3b300cb29f604063b0d16390a441
[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://dev.wolnelektury.pl'
19     initdburl: 'http://dev.wolnelektury.pl/media/api/mobile/initial/initial.sql'
20     categories:
21       'author': 'autor',
22       'epoch': 'epoka',
23       'genre': 'gatunek',
24       'kind': 'rodzaj',
25      # 'themes': 'motyw'
26     show_filter: [ 'authors', 'themes' ]
27     show_dividers: [ 'authors', 'themes']
28     db_version: '1.0'
29   Readings.catalogue = new Readings.Catalogue().open()
30   Readings.initialized = true
31
32 $(document).on 'pageinit', '#page-categories' , (ev) ->
33   Readings.init()
34   $('#list-categories').Readings 'CategoryList'
35
36 rcategory = /category=(\w+)/
37 rtag = /tag=([a-z0-9-]+)/
38 rtagid = /tag_id=([0-9]+)/
39
40 $(document).on 'pageinit', "#page-tags", (ev, ui) ->
41   category = rcategory.exec($(this).attr('data-url'))
42   if category?
43     category = category[1]
44     if category?
45       return $(this).Readings 'list',
46 #        category: category
47 #        url: Readings.config.get('wlurl') + "/api/#{category}"
48         sql: "SELECT * FROM tag WHERE category=? ORDER BY sort_key"
49         params: [category]
50         filter: (Readings.config.get('show_filter').indexOf(category) >= 0)
51         mapper: (rec) -> new Readings.Tag(rec, category)
52         dividers: (Readings.config.get('show_dividers').indexOf(category) >= 0)
53   alert 'no category in query string'
54
55 $(document).on 'pageinit', '#page-books', (ev, ui) ->
56   tag_id_m = rtagid.exec($(this).attr('data-url'))
57   tag_id = tag_id_m[1] if tag_id_m?
58
59   tag = Readings.catalogue.withTag tag_id, (tag) =>
60     $(this).Readings 'list',
61       fetch: (cb) -> Readings.catalogue.withBooks(tag, cb)
62       filter: true
63       dividers: (tag.category != 'author')