Started coding reader
[ReadingsJQM.git] / js / app / app.js
1 (function() {
2   var rbookid, rcategory, rtag, rtagid;
3
4   window.Readings = {};
5
6   $.fn.Readings = function() {
7     var a, args, meth, _i, _len;
8     meth = arguments[0];
9     args = [];
10     for (_i = 0, _len = arguments.length; _i < _len; _i++) {
11       a = arguments[_i];
12       args.push(a);
13     }
14     args.shift();
15     return $.fn.Readings[meth].apply(this, args);
16   };
17
18   Readings.init = function() {
19     if (Readings.initialized != null) return;
20     Readings.config = new Readings.Config({
21       wlurl: 'http://dev.wolnelektury.pl',
22       initdburl: 'http://dev.wolnelektury.pl/media/api/mobile/initial/initial.sql',
23       categories: {
24         'author': 'autor',
25         'epoch': 'epoka',
26         'genre': 'gatunek',
27         'kind': 'rodzaj'
28       },
29       show_filter: ['authors', 'themes'],
30       show_dividers: ['authors', 'themes'],
31       db_version: '1.0'
32     });
33     Readings.catalogue = new Readings.Catalogue().open();
34     return Readings.initialized = true;
35   };
36
37   $(document).on('pageinit', '#page-categories', function(ev) {
38     Readings.init();
39     return $('#list-categories').Readings('CategoryList');
40   });
41
42   rcategory = /category=(\w+)/;
43
44   rtag = /tag=([a-z0-9-]+)/;
45
46   rtagid = /tag_id=([0-9]+)/;
47
48   rbookid = /book_id=([0-9]+)/;
49
50   $(document).on('pageinit', "#page-tags", function(ev, ui) {
51     var category;
52     category = rcategory.exec($(this).attr('data-url'));
53     if (category != null) category = category[1];
54     if (category != null) {
55       $('h1', this).text(Readings.config.get('categories')[category]);
56       return $(this).Readings('list', {
57         category: category,
58         sql: "SELECT * FROM tag WHERE category=? ORDER BY sort_key",
59         params: [category],
60         filter: Readings.config.get('show_filter').indexOf(category) >= 0,
61         mapper: function(rec) {
62           return new Readings.Tag(rec, category);
63         },
64         dividers: Readings.config.get('show_dividers').indexOf(category) >= 0
65       });
66     }
67     return alert('no category in query string');
68   });
69
70   $(document).on('pageinit', '#page-books', function(ev, ui) {
71     var tag, tag_id, tag_id_m,
72       _this = this;
73     tag_id_m = rtagid.exec($(this).attr('data-url'));
74     if (tag_id_m != null) tag_id = tag_id_m[1];
75     return tag = Readings.catalogue.withTag(tag_id, function(tag) {
76       return $(_this).Readings('list', {
77         fetch: function(cb) {
78           return Readings.catalogue.withBooks(tag, cb);
79         },
80         filter: true,
81         dividers: tag.category !== 'author'
82       });
83     });
84   });
85
86   $(document).on('pageinit', '#page-reader', function(ev, ui) {
87     var book_id, book_id_m;
88     book_id_m = rbookid.exec($(this).attr('data-url'));
89     if (book_id_m != null) book_id = book_id_m[1];
90     return $(this).Readings('reader', {
91       book_id: book_id
92     });
93   });
94
95 }).call(this);