b95b86e92c3bb4e89adf145caa26f0735672f32e
[ReadingsJQM.git] / js / app / app.js
1 (function() {
2   var 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   $(document).on('pageinit', "#page-tags", function(ev, ui) {
49     var category;
50     category = rcategory.exec($(this).attr('data-url'));
51     if (category != null) {
52       category = category[1];
53       if (category != null) {
54         return $(this).Readings('list', {
55           sql: "SELECT * FROM tag WHERE category=? ORDER BY sort_key",
56           params: [category],
57           filter: Readings.config.get('show_filter').indexOf(category) >= 0,
58           mapper: function(rec) {
59             return new Readings.Tag(rec, category);
60           },
61           dividers: Readings.config.get('show_dividers').indexOf(category) >= 0
62         });
63       }
64     }
65     return alert('no category in query string');
66   });
67
68   $(document).on('pageinit', '#page-books', function(ev, ui) {
69     var tag, tag_id, tag_id_m,
70       _this = this;
71     tag_id_m = rtagid.exec($(this).attr('data-url'));
72     if (tag_id_m != null) tag_id = tag_id_m[1];
73     return tag = Readings.catalogue.withTag(tag_id, function(tag) {
74       return $(_this).Readings('list', {
75         fetch: function(cb) {
76           return Readings.catalogue.withBooks(tag, cb);
77         },
78         filter: true,
79         dividers: tag.category !== 'author'
80       });
81     });
82   });
83
84 }).call(this);