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