fe02c9e75fe5c23bf8b89bcfb235141a3fd787bb
[wl-mobile.git] / assets / www / js / view.js
1 /*
2  * This file is part of WolneLektury-Mobile, licensed under GNU Affero GPLv3 or later.
3  * Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4  */
5
6 var View = new function() {
7         var self = this;
8         //self.minOffset = 1000;
9         self.categories = {
10                         autor: 'Autorzy', 
11                         rodzaj: 'Rodzaje',
12                         gatunek: 'Gatunki',
13                         epoka: 'Epoki'
14         };
15         self.category_msc = {
16                 autor: 'autorze',
17                 rodzaj: 'rodzaju',
18                 gatunek: 'gatunku',
19                 epoka: 'epoce'
20         };
21         
22
23         self.init = function(success, error) {
24                 console.log('View.init');
25
26                 self._searchbox = document.getElementById("searchbox");
27                 self._searchinput = document.getElementById("search");
28                 self._content = document.getElementById("content");
29
30                 self.current = '';
31                 self.currentView = '';
32                 self.currentPar = '';
33                 self.currentTitle = '';
34
35                 document.getElementById("cover").style.display = 'none';
36                 self.enter('');
37
38                 success && success();
39         };
40
41
42         this.sanitize = function(text) {
43                 return text.replace(/&/g, "&amp;").replace(/</g, "&lt;");
44         };
45
46         this.showSearch = function() {
47                 self._searchbox.style.display = "block";
48         };
49
50         this.hideSearch = function() {
51                 self._searchbox.style.display = "none";
52         };
53
54         this.spinner = function(text) {
55                 if (!text)
56                         text = "Ładowanie";
57                 self._content.innerHTML = "<div class='spinner'><img src='img/spinner.gif' /><div id='spinnertext'>" + text +"</div></div>";
58                 setOffset(0);
59         };
60
61         this.content = function(text, offset) {
62                 console.log('content');
63
64                 self._content.innerHTML = '';
65                 self._content.innerHTML = text;
66                 setOffset(offset);
67         }
68
69         this.enter = function(url, offset) {
70                 console.log('View.enter: ' + url);
71
72                 var view = 'Index';
73                 var arg = null;
74
75                 if (url.length) {
76                         var slash_index = url.indexOf('/');
77                         if (slash_index != -1) {
78                                 view = url.substr(0, slash_index);
79                                 arg = url.substr(slash_index + 1);
80                         }
81                         else {
82                                 view = url;
83                         }
84                 }
85                 console.log('View.enter: ' + view + ' ' + arg);
86                 self.current = url;
87                 self.currentView = view;
88                 self.currentPar = arg;
89                 self['enter' + view](arg, offset);
90         }
91         
92         this.enterIndex = function(arg, offset) {
93                 console.log('enterIndex');
94                 Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true);
95                 self.showSearch();
96                 self.currentTitle = "Początek";
97                 var html = "";
98
99                 html += "<div class='buttons'>";
100                 html += Links.button('Last', '', 'Ostatnio czytane');
101                 html += Links.button('Bookmarks', '', 'Zakładki');
102
103                 for (category in self.categories)
104                         html += Links.button('Category', category, self.categories[category], 0);
105                 html += "</div>";
106                 
107                 html += "</div>" +"";
108                                 /*"<p id='logo'><img src='img/logo-wl-nq8.png' alt='Wolne Lektury' /><br/>\n" +
109                                 "szkolna biblioteka internetowa" +
110                                 "</p>";*/
111                 self.content(html, offset);
112         };
113         
114         this.enterBook = function(id, offset) {
115                 id = parseInt(id);
116                 console.log('enterBook: ' + id);
117                 Menu.setInfoButton("BookInfo", "Informacje o utworze", true);
118                 self.showSearch();
119
120                 Catalogue.withBook(id, function(book) {
121                         self.currentTitle = book.authors + ', ' + book.title;
122
123                         Catalogue.withChildren(id, function(children) {
124                                 var html = "<h1><span class='subheader'>";
125                                 html += book.authors;
126                                 html += "</span>" + book.title + "</h1>\n";
127                                 if (book.html_file) {
128                                         html += "<div class='buttons'>" + Links.button('BookText', id, "Czytaj tekst") + "</div>";
129                                 }
130                                 if (children.length) {
131                                         html += "<div class='buttons'>";
132                                         for (c in children) {
133                                                 child = children[c];
134                                                 html += Links.bookLink(child);
135                                         }
136                                         html += "</div>";
137                                 }
138                                 self.content(html, offset);                             
139                         });
140                 }, function() {
141                         History.goBack();
142                 });
143         };
144         
145         this.enterBookText = function(id, offset) {
146                 self.hideSearch();
147                 self.spinner("Otwieranie utworu");
148                 console.log('enterBookText: ' + id);
149                 Menu.setInfoButton("BookInfo", "Informacje o utworze", true);
150                 id = parseInt(id);
151
152                 setTimeout("History.addRead("+id+");", 0);
153                 
154                 FileRepo.withHtml(id, function(data) {
155                         self.content(data, offset);
156                 }, function(err) {
157                         alert("Błąd pobierania: nie udało się pobrać treści utworu.");
158                         History.goBack();
159                 });
160                 Catalogue.withBook(id, function(book) {
161                         self.currentTitle = book.authors + ', ' + book.title;
162                 });
163         };
164
165
166         this.enterLast = function(ignored, offset) {
167                 console.log("enterLast");
168                 self.showSearch();
169                 Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true);
170                 self.currentTitle = 'Ostatnio czytane';
171                 var html = "<h1><span class='subheader'>Ostatnio czytane</h1>\n";
172
173                 var last_read = History.lastRead();
174                 var some_books = false;
175
176                 html += "<div class='buttons'>";
177                 var add_books = function() {
178                         if (last_read.length) {
179                                 var id = last_read.shift();
180                                 Catalogue.withBook(id, function(book) {
181                                         html += Links.bookLink(book);
182                                         some_books = true;
183                                         add_books();
184                                 }, function() {
185                                         add_books();
186                                 });
187                         }
188                         else {
189                                 if (!some_books) {
190                                         html += "<p>Nie przeczytano żadnych utworów.</p>";
191                                 }
192                                 html += "</div>";
193                                 self.content(html, offset);
194                         }
195                 };
196                 add_books();
197         };
198
199
200         this.enterBookmarks = function(ignored, offset) {
201                 console.log("enterBookmarks");
202                 self.showSearch();
203                 Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true);
204                 self.currentTitle = 'Zakładki';
205                 var html = "<h1><span class='subheader'>Zakładki</h1>\n";
206
207                 var bookmarks = History.bookmarks();
208                 if (!bookmarks.length) {
209                         html += "<p>Nie utworzono żadnych zakładek.</p>";
210                         self.content(html, offset);
211                         return;
212                 }
213
214                 html += "<div class='buttons bookmarks'>";
215                 for (i in bookmarks) {
216                         var bm = bookmarks[i];
217
218                         var text = bm.name;
219                         text += "<div class='sub'>" + bm.title + "</div>";
220                         html += Links.deleteButton(bm.id);
221                         html += Links.button(bm.view, bm.par, text, bm.offset);
222                 }
223                 html += "</div>";
224                 self.content(html, offset);
225         };
226
227         this.onBookmarkChange = function() {
228                 // TODO: preserve offset
229                 if (self.currentView == 'Bookmarks') {
230                         self.enterBookmarks();
231                 }
232         };
233
234         this.enterTag = function(id, offset) {
235                 id = parseInt(id);
236                 console.log('enterTag: ' + id);
237                 Menu.setInfoButton("TagInfo", "Informacje o...", true);
238                 self.showSearch();
239
240                 self.spinner("Otwieranie listy utworów");
241
242                 Catalogue.withTag(id, function(tag) {
243                         Menu.setInfoButton("TagInfo", "Informacje o " + self.category_msc[tag.category], true);
244                         self.currentTitle = tag.category + ': ' + tag.name;
245                         var html = "<h1><span class='subheader upper'>" + tag.category + ': </span>' + tag.name + "</h1>\n";
246                         html += "<div class='buttons'>";
247                         if (tag.books) {
248                                 Catalogue.withBooks(tag.books, function(books) {
249                                         for (var i in books) {
250                                                 var book = books[i];
251                                                 html += Links.bookLink(book);
252                                         }
253                                         html += "</div>";
254                                         self.content(html, offset);
255                                 });
256                         }
257                 }, function() {
258                         History.goBack();
259                 });
260         };
261
262
263         this.enterCategory = function(category, offset) {
264                 console.log('enterCategory: ' + category);
265                 Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true);
266                 self.spinner("Otwieranie katalogu");
267                 self.showSearch();
268                 self.currentTitle = self.categories[category];
269
270                 Catalogue.withCategory(category, function(tags) {
271                         var html = "<h1>" + self.categories[category] + "</h1>\n";
272                         html += "<div class='buttons'>";
273                         for (i in tags) {
274                                 tag = tags[i];
275                                 html += Links.button('Tag', tag.id, tag.name);
276                         }
277                         html += "</div>";
278                         self.content(html, offset);
279                 });
280         };
281
282
283         this.enterSearch = function(query, offset) {
284                 console.log('enterSearch: ' + query);
285                 Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true);
286                 self.currentTitle = 'Szukaj: ' + query;
287                 self.showSearch();
288
289                 var html = "<h1><span class='subheader'>Szukana fraza:</span>" + View.sanitize(query) + "</h1>\n";
290
291                 if (query.length < 2) {
292                         html += "<p>Szukana fraza musi mieć co najmniej dwa znaki</p>";
293                         self.content(html, offset);
294                         return;
295                 }
296
297                 Catalogue.withSearch(query, function(results) {
298                         if (results.length == 1) {
299                             var result = results[0];
300                             if (result.view == 'Book' && result.item.html_file) {
301                                 self.enter(Links.href('BookText', result.item.id));
302                             }
303                             else {
304                                         self.enter(Links.href(result.view, result.item.id));
305                                 }
306                                 return;
307                         }
308                         if (results.length == 0) {
309                                 html += "<p>Brak wyników wyszukiwania</p>";
310                         }
311                         else {
312                                 html += "<div class='buttons'>";
313                                 for (var i in results) {
314                                         var result = results[i];
315                                         if (result.view == 'Book')
316                                                 html += Links.bookLink(result.item)
317                                         else
318                                                 html += Links.button(result.view, result.item.id, result.item.name+"<div class='sub'>"+result.item.category+"</div>");
319                                 }
320                                 html += "</div>";
321                         }
322                         self.content(html, offset);
323                 });
324         };
325
326
327         /* info */
328
329         this.enterProjectInfo = function(arg, offset) {
330                 console.log('enterProjectInfo');
331                 Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", false);
332                 self.hideSearch();
333                 self.currentTitle = "Informacje o projekcie";
334
335                 var html = "";
336
337                 html += '<div class="info">';
338
339
340                 html += "<p style='text-align:center'><img src='img/logo-wl.png' /></p>";
341                 html += "<p>Biblioteka internetowa Wolne Lektury "+
342 " udostępnia w swoich zbiorach lektury szkolne zalecane do użytku przez" + 
343 " Ministerstwo Edukacji Narodowej i inne dzieła literatury.</p>";
344
345                 html += "<p style='text-align:center'><img src='img/logo-fnp.png' /></p>";
346
347                 html += "<img style='float:left;' src='img/procent.png' />" +
348                         "<p style='margin-left: 50px'>" + 
349                         "Przekaż 1% podatku na rozwój Wolnych Lektur.<br/>" +
350                         "Fundacja Nowoczesna Polska<br/>" +
351                         "KRS 0000070056</p>";
352
353                 html += "<p>Większość pozycji w bibliotece należy do domeny publicznej "+
354                         "co oznacza, że nie są już chronione majatkowym prawem autorskim, "+
355                         "a więc można je swobodnie wykorzystywać, publikować i rozpowszechniać. "+
356                         "Publikujemy również kilka utworów, które autorzy udostępnili na wolnej licencji "+
357                         "<a href='http://creativecommons.org/licenses/by-sa/3.0/deed.pl'>"+
358                         "Creative Commons Uznanie Autorstwa - Na Tych Samych Warunkach 3.0.PL</a>.</p>";
359
360                 html += "<p style='text-align:center'><img src='img/cc-by-sa.png' /></p>";
361
362                 html += "<p>Copyright © 2011 Fundacja Nowoczesna Polska. Aplikacja jest wolnym oprogramowaniem "+
363                                 "dostępnym na licencji GNU Affero GPL w wersji 3 lub późniejszej.</p>";
364
365                 html += "<p>Więcej informacji o projekcie znajduje sie na stronie <a href='http://www.wolnelektury.pl'>http://www.wolnelektury.pl</a>.</p>";
366
367                 html += '</div>';
368
369
370                 self.content(html, offset);
371         };
372         
373         
374         this.enterBookInfo = function(id, offset) {
375                 id = parseInt(id);
376                 console.log('enterBookInfo: ' + id);
377                 Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true);
378                 self.hideSearch();
379
380                 Catalogue.withBook(id, function(book) {
381                         self.currentTitle = "Informacje o: " + book.title;
382
383                         var html = '<h2>' + book.authors + ', ' + book.title + '</h2>';
384
385                         var url = WL + '/api/book/' + id + '/info.html';
386
387                         var xhr = new XMLHttpRequest();
388                         xhr.open("GET", url);
389                         xhr.onload = function() {
390                                 console.log('BookInfo: fetched by ajax: ' + url);
391
392                                 html += '<div class="info">';
393                                 html += xhr.responseText;
394                                 html += '</div>';
395
396                                 self.content(html, offset);
397                         }
398                         xhr.onerror = function(e) {
399                                 self.content("Brak informacji.", offset);
400                         }
401                         xhr.send();
402                 }, function() {
403                         History.goBack();
404                 });
405         };
406
407
408         this.enterTagInfo = function(id, offset) {
409                 id = parseInt(id);
410                 console.log('enterTagInfo: ' + id);
411                 Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true);
412                 self.hideSearch();
413
414                 Catalogue.withTag(id, function(tag) {
415                         self.currentTitle = "Informacje o " + tag.name;
416                         var html = '<h2>' + tag.name + '</h2>';
417
418                         var url = WL + '/api/tag/' + id + '/info.html';
419
420                         var xhr = new XMLHttpRequest();
421                         xhr.open("GET", url);
422                         xhr.onload = function() {
423                                 console.log('TagInfo: fetched by ajax: ' + url);
424
425                                 html += '<div class="info">';
426                                 html += xhr.responseText;
427                                 html += '</div>';
428
429                                 self.content(html, offset);
430                         }
431                         xhr.onerror = function(e) {
432                                 self.content("Brak informacji.", offset);
433                         }
434                         xhr.send();
435                 }, function() {
436                         History.goBack();
437                 });
438         };
439
440
441         /* search form submit callback */
442         this.search = function() {
443                 History.visit('Search/' + self._searchinput.value);
444                 return false;
445         }
446         
447         
448
449 }