-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
125 lines (111 loc) · 4.39 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// DOMContentLoaded is fired once the document has been loaded and parsed,
// but without waiting for other external resources to load (css/images/etc)
// That makes the app more responsive and perceived as faster.
// https://developer.mozilla.org/Web/Reference/Events/DOMContentLoaded
window.addEventListener('DOMContentLoaded', function() {
// We'll ask the browser to use strict code to help us catch errors earlier.
// https://developer.mozilla.org/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode
'use strict';
var translate = navigator.mozL10n.get;
checkSettings();
// We want to wait until the localisations library has loaded all the strings.
// So we'll tell it to let us know once it's ready.
//navigator.mozL10n.once(start);
// ---
/*function start() {
var message = document.getElementById('message');
// We're using textContent because inserting content from external sources into your page using innerHTML can be dangerous.
// https://developer.mozilla.org/Web/API/Element.innerHTML#Security_considerations
message.textContent = translate('message');
}*/
});
// TODO Get JS to recognize multiple HTML pages
// Once this is fixed, playing books + settings should work!
// Bugs:
// -Not loading when spaces are used
// -Not working with multiple pages
// -Search results aren't resetting
$( document ).on( "pagecreate", "#homeBook", function( event ) {
// TODO: Load the ID of the book
console.log("Book.html loaded");
});
$( document ).on( "pagecreate", "#homeSettings", function( event ) {
console.log("Settings.html loaded");
});
var volumeAmt = getValue("volume");
// -- Save Settings File (if nonexistent)
function writeToSettings(key, value){
// if(window.localStorage){
localStorage.setItem(key, value);
// }
}
function getValue(key){
// if(window.localStorage){
localStorage.getItem(key);
}
function loadBook(){
var id = getValue("id");
console.log("ID was determined to be " + id);
}
// }
function checkSettings(){
// if(window.localStorage){
if((getValue("volume") == null) || (getValue("volume") == 'undefined')){
writeToSettings("volume", "60"); // -> Fix: Volume is just resetting every app restart
}
// }
}
$("#volumeSlider").change(function(){
writeToSettings("volume", $("#volumeSlider").slider("value").val());
});
$("#newSearch").submit(function(event){
$("#booksList").empty(); // empty the list of any results from previous searches
var input = $("#bookSearch").val();
getJSON("https://librivox.org/api/feed/audiobooks/title/^" + encodeURIComponent(input) + "?&format=json",function(xhr) {
if(typeof (xhr.response.books) === 'undefined'){
// Show "No Available Books" text! Try making your search simpler.
$("#noAvailableBooks").show();
}
else {
console.log("librivox responded with " + xhr.response.books.length + " book(s) and status " + xhr.status);
xhr.response.books.forEach(function(entry){
var title = entry.title;
var id = entry.id;
var description = entry.description;
var text = $.parseHTML(description);
var realText = $(text).text();
var id = entry.id;
if(title != ''){
localStorage.setItem("id", id);
$("#booksList").append('<li><a href="book.html?id=' + id + '"><h2>' + title + '</h2><p>' + realText + '</p></a></li>');
// Keeping ID in URL for possible use later
}
else {
console.log("Nothing to add!");
}
// onClick -> go to book.html, which has play buttons, etc. together, load audiobook
});
}
$("#booksList").listview('refresh');
});
return false; // this cancels the form submit, which stops the page from refreshing.
// Awesome... I wish I had thought of that earlier :p -> Now changing search results into links which will then load the audiobook
});
function getJSON(url, load_callback) {
var xhr = new XMLHttpRequest({ mozSystem: true });
if (xhr.overrideMimeType) {
xhr.overrideMimeType('application/json');
}
var error_callback = function(e) {
console.log("error loading json from url " + url);
console.log(e);
}
xhr.addEventListener('load', function(e) {
load_callback(xhr,e);
});
xhr.addEventListener('error', error_callback);
xhr.addEventListener('timeout', error_callback);
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.send();
}