X-Git-Url: https://git.mdrn.pl/wl-mobile.git/blobdiff_plain/8b7b42d45674861437a31e9f9125a277ce998721..065d23f9494e1afb7f61ada08b728405b1b7a0cf:/assets/www/js/view.js diff --git a/assets/www/js/view.js b/assets/www/js/view.js new file mode 100644 index 0000000..7106d4b --- /dev/null +++ b/assets/www/js/view.js @@ -0,0 +1,441 @@ +var View = new function() { + var self = this; + //self.minOffset = 1000; + self.categories = { + autor: 'Autorzy', + rodzaj: 'Rodzaje', + gatunek: 'Gatunki', + epoka: 'Epoki' + }; + self.category_msc = { + autor: 'autorze', + rodzaj: 'rodzaju', + gatunek: 'gatunku', + epoka: 'epoce' + }; + + + self.init = function(success, error) { + console.log('View.init'); + + self._searchbox = document.getElementById("searchbox"); + self._searchinput = document.getElementById("search"); + self._content = document.getElementById("content"); + + self.current = ''; + self.currentView = ''; + self.currentPar = ''; + self.currentTitle = ''; + + document.getElementById("cover").style.display = 'none'; + self.enter(''); + + success && success(); + }; + + + this.sanitize = function(text) { + return text.replace(/&/g, "&").replace(/"; + setOffset(0); + }; + + this.content = function(text, offset) { + console.log('content'); + + self._content.innerHTML = ''; + self._content.innerHTML = text; + setOffset(offset); + } + + this.enter = function(url, offset) { + console.log('View.enter: ' + url); + + var view = 'Index'; + var arg = null; + + if (url.length) { + var slash_index = url.indexOf('/'); + if (slash_index != -1) { + view = url.substr(0, slash_index); + arg = url.substr(slash_index + 1); + } + else { + view = url; + } + } + console.log('View.enter: ' + view + ' ' + arg); + self.current = url; + self.currentView = view; + self.currentPar = arg; + self['enter' + view](arg, offset); + } + + this.enterIndex = function(arg, offset) { + console.log('enterIndex'); + Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true); + self.showSearch(); + self.currentTitle = "Początek"; + var html = ""; + + html += "
"; + html += Links.button('Last', '', 'Ostatnio czytane'); + html += Links.button('Bookmarks', '', 'Zakładki'); + + for (category in self.categories) + html += Links.button('Category', category, self.categories[category], 0); + html += "
"; + + html += "" +""; + /*"";*/ + self.content(html, offset); + }; + + this.enterBook = function(id, offset) { + id = parseInt(id); + console.log('enterBook: ' + id); + Menu.setInfoButton("BookInfo", "Informacje o utworze", true); + self.showSearch(); + + Catalogue.withBook(id, function(book) { + self.currentTitle = book.authors + ', ' + book.title; + + Catalogue.withChildren(id, function(children) { + var html = "

"; + html += book.authors; + html += "" + book.title + "

\n"; + if (book.html_file) { + html += "
" + Links.button('BookText', id, "Czytaj tekst") + "
"; + } + if (children.length) { + html += "
"; + for (c in children) { + child = children[c]; + html += Links.bookLink(child); + } + html += "
"; + } + self.content(html, offset); + }); + }, function() { + History.goBack(); + }); + }; + + this.enterBookText = function(id, offset) { + self.hideSearch(); + self.spinner("Otwieranie utworu"); + console.log('enterBookText: ' + id); + Menu.setInfoButton("BookInfo", "Informacje o utworze", true); + id = parseInt(id); + + setTimeout("History.addRead("+id+");", 0); + + FileRepo.withHtml(id, function(data) { + self.content(data, offset); + }, function(err) { + alert("Błąd pobierania: nie udało się pobrać treści utworu."); + History.goBack(); + }); + Catalogue.withBook(id, function(book) { + self.currentTitle = book.authors + ', ' + book.title; + }); + }; + + + this.enterLast = function(ignored, offset) { + console.log("enterLast"); + self.showSearch(); + Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true); + self.currentTitle = 'Ostatnio czytane'; + var html = "

Ostatnio czytane

\n"; + + var last_read = History.lastRead(); + var some_books = false; + + html += "
"; + var add_books = function() { + if (last_read.length) { + var id = last_read.shift(); + Catalogue.withBook(id, function(book) { + html += Links.bookLink(book); + some_books = true; + add_books(); + }, function() { + add_books(); + }); + } + else { + if (!some_books) { + html += "

Nie przeczytano żadnych utworów.

"; + } + html += "
"; + self.content(html, offset); + } + }; + add_books(); + }; + + + this.enterBookmarks = function(ignored, offset) { + console.log("enterBookmarks"); + self.showSearch(); + Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true); + self.currentTitle = 'Zakładki'; + var html = "

Zakładki

\n"; + + var bookmarks = History.bookmarks(); + if (!bookmarks.length) { + html += "

Nie utworzono żadnych zakładek.

"; + self.content(html, offset); + return; + } + + html += "
"; + for (i in bookmarks) { + var bm = bookmarks[i]; + + var text = bm.name; + text += "
" + bm.title + "
"; + html += Links.deleteButton(bm.id); + html += Links.button(bm.view, bm.par, text, bm.offset); + } + html += "
"; + self.content(html, offset); + }; + + this.onBookmarkChange = function() { + // TODO: preserve offset + if (self.currentView == 'Bookmarks') { + self.enterBookmarks(); + } + }; + + this.enterTag = function(id, offset) { + id = parseInt(id); + console.log('enterTag: ' + id); + Menu.setInfoButton("TagInfo", "Informacje o...", true); + self.showSearch(); + + self.spinner("Otwieranie listy utworów"); + + Catalogue.withTag(id, function(tag) { + Menu.setInfoButton("TagInfo", "Informacje o " + self.category_msc[tag.category], true); + self.currentTitle = tag.category + ': ' + tag.name; + var html = "

" + tag.category + ': ' + tag.name + "

\n"; + html += "
"; + if (tag.books) { + Catalogue.withBooks(tag.books, function(books) { + for (var i in books) { + var book = books[i]; + html += Links.bookLink(book); + } + html += "
"; + self.content(html, offset); + }); + } + }, function() { + History.goBack(); + }); + }; + + + this.enterCategory = function(category, offset) { + console.log('enterCategory: ' + category); + Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true); + self.spinner("Otwieranie katalogu"); + self.showSearch(); + self.currentTitle = self.categories[category]; + + Catalogue.withCategory(category, function(tags) { + var html = "

" + self.categories[category] + "

\n"; + html += "
"; + for (i in tags) { + tag = tags[i]; + html += Links.button('Tag', tag.id, tag.name); + } + html += "
"; + self.content(html, offset); + }); + }; + + + this.enterSearch = function(query, offset) { + console.log('enterSearch: ' + query); + Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true); + self.currentTitle = 'Szukaj: ' + query; + self.showSearch(); + + var html = "

Szukana fraza:" + View.sanitize(query) + "

\n"; + + if (query.length < 2) { + html += "

Szukana fraza musi mieć co najmniej dwa znaki

"; + self.content(html, offset); + return; + } + + Catalogue.withSearch(query, function(results) { + if (results.length == 1) { + var result = results[0]; + if (result.view == 'Book' && result.item.html_file) { + self.enter(Links.href('BookText', result.item.id)); + } + else { + self.enter(Links.href(result.view, result.item.id)); + } + return; + } + if (results.length == 0) { + html += "

Brak wyników wyszukiwania

"; + } + else { + html += "
"; + for (var i in results) { + var result = results[i]; + if (result.view == 'Book') + html += Links.bookLink(result.item) + else + html += Links.button(result.view, result.item.id, result.item.name+"
"+result.item.category+"
"); + } + html += "
"; + } + self.content(html, offset); + }); + }; + + + /* info */ + + this.enterProjectInfo = function(arg, offset) { + console.log('enterProjectInfo'); + Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", false); + self.hideSearch(); + self.currentTitle = "Informacje o projekcie"; + + var html = ""; + + html += '
'; + + + html += "

"; + html += "

Biblioteka internetowa Wolne Lektury "+ +" udostępnia w swoich zbiorach lektury szkolne zalecane do użytku przez" + +" Ministerstwo Edukacji Narodowej i inne dzieła literatury.

"; + + html += "

"; + + html += "" + + "

" + + "Przekaż 1% podatku na rozwój Wolnych Lektur.
" + + "Fundacja Nowoczesna Polska
" + + "KRS 0000070056

"; + + html += "

Większość pozycji w bibliotece należy do domeny publicznej "+ + "co oznacza, że nie są już chronione majatkowym prawem autorskim, "+ + "a więc można je swobodnie wykorzystywać, publikować i rozpowszechniać. "+ + "Publikujemy również kilka utworów, które autorzy udostępnili na wolnej licencji "+ + ""+ + "Creative Commons Uznanie Autorstwa - Na Tych Samych Warunkach 3.0.PL.

"; + + html += "

"; + + html += "

Więcej informacji o projekcie znajduje sie na stronie http://www.wolnelektury.pl.

"; + + html += '
'; + + + self.content(html, offset); + }; + + + this.enterBookInfo = function(id, offset) { + id = parseInt(id); + console.log('enterBookInfo: ' + id); + Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true); + self.hideSearch(); + + Catalogue.withBook(id, function(book) { + self.currentTitle = "Informacje o: " + book.title; + + var html = '

' + book.authors + ', ' + book.title + '

'; + + var url = WL + '/api/book/' + id + '/info.html'; + + var xhr = new XMLHttpRequest(); + xhr.open("GET", url); + xhr.onload = function() { + console.log('BookInfo: fetched by ajax: ' + url); + + html += '
'; + html += xhr.responseText; + html += '
'; + + self.content(html, offset); + } + xhr.onerror = function(e) { + self.content("Brak informacji.", offset); + } + xhr.send(); + }, function() { + History.goBack(); + }); + }; + + + this.enterTagInfo = function(id, offset) { + id = parseInt(id); + console.log('enterTagInfo: ' + id); + Menu.setInfoButton("ProjectInfo", "Informacje o projekcie", true); + self.hideSearch(); + + Catalogue.withTag(id, function(tag) { + self.currentTitle = "Informacje o " + tag.name; + var html = '

' + tag.name + '

'; + + var url = WL + '/api/tag/' + id + '/info.html'; + + var xhr = new XMLHttpRequest(); + xhr.open("GET", url); + xhr.onload = function() { + console.log('TagInfo: fetched by ajax: ' + url); + + html += '
'; + html += xhr.responseText; + html += '
'; + + self.content(html, offset); + } + xhr.onerror = function(e) { + self.content("Brak informacji.", offset); + } + xhr.send(); + }, function() { + History.goBack(); + }); + }; + + + /* search form submit callback */ + this.search = function() { + History.visit('Search/' + self._searchinput.value); + return false; + } + + + +}